Skip to content

init

StrategyContext()

Context class for drawing strategies.

This class is responsible for selecting the correct drawing strategy.

Attributes:

Name Type Description
_strategies dict

A dictionary of strategies with shape type as key and strategy as value.

Source code in src/layerforge/svg/drawing/strategy_context.py
23
24
def __init__(self) -> None:
    self._strategies: dict[str, ShapeDrawingStrategy] = {}

draw(dwg, shape, **attribs)

Draws a shape using the appropriate drawing strategy.

The strategy makes the element and this method styles it, so the strokes of every shape are set in one place.

Parameters:

Name Type Description Default
dwg Drawing

The SVG drawing to draw the shape on.

required
shape BaseShape

The shape to draw.

required
**attribs str

SVG attributes for the element, for example stroke="red" or class_="mark".

{}

Returns:

Type Description
None

Raises:

Type Description
ValueError

If no strategy is found for the shape type.

Source code in src/layerforge/svg/drawing/strategy_context.py
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
def draw(self, dwg: Drawing, shape: BaseShape, **attribs: str) -> None:
    """Draws a shape using the appropriate drawing strategy.

    The strategy makes the element and this method styles it, so the strokes of every
    shape are set in one place.

    Parameters
    ----------
    dwg : Drawing
        The SVG drawing to draw the shape on.
    shape : BaseShape
        The shape to draw.
    **attribs : str
        SVG attributes for the element, for example ``stroke="red"`` or ``class_="mark"``.

    Returns
    -------
    None

    Raises
    ------
    ValueError
        If no strategy is found for the shape type.
    """
    strategy = self._strategies.get(type(shape).__name__.lower())
    if strategy is None:
        raise ValueError(f"No strategy found for shape type: {type(shape).__name__}")
    element = strategy.element(dwg, shape)
    # svgwrite's `update` turns `class_` into `class` and `stroke_width` into `stroke-width`.
    # Pyright cannot see the method (it is there at run time), so it is called through Any.
    cast(Any, element).update(attribs)
    dwg.add(element)

register_strategy(shape_type, strategy)

Registers a drawing strategy for a specific shape type.

Parameters:

Name Type Description Default
shape_type str

The type of shape to register the strategy for.

required
strategy ShapeDrawingStrategy

The drawing strategy to register.

required

Returns:

Type Description
None
Source code in src/layerforge/svg/drawing/strategy_context.py
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
def register_strategy(self, shape_type: str, strategy: ShapeDrawingStrategy) -> None:
    """Registers a drawing strategy for a specific shape type.

    Parameters
    ----------
    shape_type : str
        The type of shape to register the strategy for.
    strategy : ShapeDrawingStrategy
        The drawing strategy to register.

    Returns
    -------
    None
    """
    self._strategies[shape_type.lower()] = strategy