tkmilan.drender#

Renderer for diagrams.

Can support multiple backends, only one implemented so far.

Functions

svg_class(de_tags, extratags)

Generate SVG class arguments, if exists.

svg_dash(dash)

Generate related diagram.D SVG dash arguments.

svg_text_anchor(anchor)

Generate related diagram.A SVG text anchor arguments.

tkcanvas_arrow(option)

Generate related diagram.A Tk canvas arrow arguments

tkcanvas_colors(noprefix[, cnames])

Generate related diagram.C Tk canvas color arguments.

tkcanvas_dash(**options)

Generate related diagram.D Tk canvas dash arguments

tkcanvas_smooth(option)

Generate related diagram.Smooth Tk canvas smooth arguments

tkcanvas_text_angle(angle)

Generate Tk Text angle arguments.

tkcanvas_width(**options)

Generate Tk canvas width arguments

Classes

Exporter_SVG(diagram, *[, widget])

Export object for SVG format, version 1.1.

Renderer_TkCanvas(canvas, diagram, *[, ...])

Renderer object for the Tk canvas backend.

class tkmilan.drender.Exporter_SVG(diagram: Diagram, *, widget: Optional[mixin.MixinWidget] = None)#

Bases: object

Export object for SVG format, version 1.1.

The export function is the primary API.

The widget instance is an optional context provider for Tk-related operations (such as color resolution and size queries). It should be used to improve the render quality.

The produced file has has up to four groups:

  1. World (identified as world)

  2. Background (back layer) (setup_bg_b)

  3. Foreground (setup_fg)

  4. Background (front layer) (setup_bg_f)

Each is included in order, according to the diagram instructions. The world group includes the diagram background, the rest are the diagram “layers”, see diagram.Diagram.

To implement line arrows, the SVG defs section is used to produce reusable SVG definitions. This should keep the size of the file down, even in very complex diagrams with a lot of similar arrows. This is only included if necessary.

Parameters:
export(outfile: Path, *, width: int | None = None, height: int | None = None, pretty: bool = True) None#

Export the diagram to an SVG file.

Parameters:
  • outfile (Path) – Path to the output SVG file.

  • width (int | None) – SVG viewport width. Optional, defaults to the current widget width. If the current widget is not defined, this is required.

  • height (int | None) – SVG viewport height. Optional, defaults to the current widget height. If the current widget is not defined, this is required.

  • pretty (bool) – Whether to pretty-print the XML with indentation. Defaults to True.

render(de: DiagramElement, *, extratags: Sequence[str] = ()) Element | None#
render(de: MultiLine, *, extratags: Sequence[str] = ()) Element | None
render(de: Polygon, *, extratags: Sequence[str] = ()) Element | None
render(de: Rectangle, *, extratags: Sequence[str] = ()) Element | None
render(de: Ellipse, *, extratags: Sequence[str] = ()) Element | None
render(de: Text, *, extratags: Sequence[str] = ()) Element | None
render(de: EllipseSection, *, extratags: Sequence[str] = ()) Element | None

Render a diagram element, with some internal extratags.

This function is defined as a single dispatch method for each supported diagram element.

Parameters:
  • de (DiagramElement) – The diagram element to render. Supported for the given diagram element types.

  • extratags (Sequence[str]) – Additional internal tags to consider, besides the element tags.

class tkmilan.drender.Renderer_TkCanvas(canvas: Canvas, diagram: Diagram, *, saveElements: bool | None = None)#

Bases: object

Renderer object for the Tk canvas backend.

This is just a holding for the rendering process. No “automatic” draws happens when creating this object, all must go through the redraw function.

The possible values for the saveElements argument are:

  • True: Save markers and element as a tuple

  • False: Save only markers

  • None: Disable saving elements or markers

Parameters:
  • canvas (Canvas) – The Tk canvas object to use. Should be an internal Canvas.

  • diagram (Diagram) – The diagram to render.

  • saveElements (bool | None) – Save elements on the eids mapping, see above. Defaults to None.

getElement(eid: int) DiagramElement | None#

Get a saved element, if exists.

Parameters:

eid (int) – The element to consider.

Returns:

Return the DiagramElement, if saved.

Return type:

DiagramElement | None

getMarker(eid: int) str | None#

Get a saved marker string, if exists.

Parameters:

eid (int) – The element to consider.

Returns:

Return the marker str, if exists.

Return type:

str | None

redraw(_a1: Any | None = None, _a2: Any | None = None, *, width: int | None = None, height: int | None = None, force: bool = False)#

Redraw the canvas.

The canvas consists in three layers, as explained in diagram.Diagram.

If the canvas size changed, all layers are redrawn. Othewise, only the foreground is redrawn. Note that redrawing implies removing the old elements and creating new ones, this is done automatically.

When the foreground is redrawn, it is pushed behing the background front layer.

Unless force is given, the first “weird” event (when width and height are both 1) is skipped, since this does not make sense to redraw.

Parameters:
  • width (int | None) – Use this canvas width, instead of getting this information from the canvas object.

  • height (int | None) – Use this canvas height, instead of getting this information from the canvas object.

  • force (bool) – Force redrawing of background and foreground elements.

  • _a1 (Any | None) – Optional, unused. This exists for API compatibility with bindings.

  • _a2 (Any | None) – Optional, unused. This exists for API compatibility with traces.

render(de: DiagramElement, *, extratags: Sequence[str] = ()) int#
render(de: MultiLine, *, extratags: Sequence[str] = ()) int
render(de: Polygon, *, extratags: Sequence[str] = ()) int
render(de: Rectangle, *, extratags: Sequence[str] = ()) int
render(de: Ellipse, *, extratags: Sequence[str] = ()) int
render(de: Text, *, extratags: Sequence[str] = ()) int
render(de: EllipseSection, *, extratags: Sequence[str] = ()) int

Render a diagram element, with some internal extratags.

This function is defined as a single dispatch method for each supported diagram element.

Parameters:
  • de (DiagramElement) – The diagram element to render. Supported for the given diagram element types.

  • extratags (Sequence[str]) – Additional internal tags to consider, besides the element tags.

eids: MutableMapping[int, str | None | Tuple[str | None, DiagramElementSingle]] | None#

Map element ids to the corresponding str marker (if given), and optionally including the DiagramElement as a tuple.

Optional, this can be controlled with the saveElements parameter.

tkmilan.drender.svg_class(de_tags: Sequence[str], extratags: Sequence[str]) Mapping#

Generate SVG class arguments, if exists.

Parameters:
  • de_tags (Sequence[str]) – DiagramElement tags

  • extratags (Sequence[str]) – Extra tags, for rendering

tkmilan.drender.svg_dash(dash: D | None) Mapping#

Generate related diagram.D SVG dash arguments.

tkmilan.drender.svg_text_anchor(anchor: CP) Mapping#

Generate related diagram.A SVG text anchor arguments.

tkmilan.drender.tkcanvas_arrow(option: A | None) Mapping#

Generate related diagram.A Tk canvas arrow arguments

tkmilan.drender.tkcanvas_colors(noprefix: C, cnames: Mapping[str, str] | None = None, **poptions: C) Mapping#

Generate related diagram.C Tk canvas color arguments.

Parameters:
  • noprefix (C) – The color without prefix. Required.

  • poptions (C) – Mapping between prefix and color oboject.

  • cnames (Mapping[str, str] | None) – A mapping between color object option name, and Tk argument. Optional, defaults to fill and outline mapping to the corresponding names.

tkmilan.drender.tkcanvas_dash(**options: D | None) Mapping#

Generate related diagram.D Tk canvas dash arguments

tkmilan.drender.tkcanvas_smooth(option: Smooth | None) Mapping#

Generate related diagram.Smooth Tk canvas smooth arguments

tkmilan.drender.tkcanvas_text_angle(angle: Real) Mapping#

Generate Tk Text angle arguments.

This feature is only supported on later Tk versions.

Parameters:

angle (Real) – The angle value. Required.

tkmilan.drender.tkcanvas_width(**options: int | None) Mapping#

Generate Tk canvas width arguments