classes.c20_box

  1from typing import Literal, Union
  2from loguru import logger
  3import drawBot
  4
  5from lib import layout, helpers, color
  6
  7
  8class KBox:
  9    "A layout primitive with coordinates, dimensions and spatial utilities."
 10
 11    def __init__(self, coords: tuple) -> None:
 12        """
 13        Initialize a KBox with given coordinates.
 14
 15        See `lib.layout.parsePageSize` for possible coordinate types.
 16
 17        Args:
 18            coords: A tuple of (x, y, width, height) coordinates.
 19
 20        Example:
 21        ```python
 22        # Create a new box with coordinates
 23        box = KBox((10, 10, 100, 50))  # x, y, width, height
 24
 25        # Move the box
 26        box.move(x=20, y=10)
 27
 28        # Grow the box by adding margin
 29        box.grow((5, 5, 5, 5))  # left, bottom, right, top margins
 30        ```
 31        """
 32        self.x, self.y, self.width, self.height = layout.toCoords(coords)
 33
 34    @property
 35    def position(self) -> tuple[int, int]:
 36        """
 37        Returns the x and y coordinates of the bottom-left corner.
 38        """
 39        return self.x, self.y
 40
 41    @position.setter
 42    def position(self, value: tuple[int, int]) -> None:
 43        """
 44        Set the position (x, y) of the box.
 45        """
 46        try:
 47            self.x, self.y = value
 48        except:
 49            logger.warning("Unable to set KBox position: {}", value)
 50
 51    @property
 52    def dimensions(self) -> tuple[int, int]:
 53        """
 54        Returns the dimensions (width, height) of the box.
 55        """
 56        return self.width, self.height
 57
 58    @dimensions.setter
 59    def dimensions(self, value: tuple[int, int]) -> None:
 60        """
 61        Set the dimensions (width, height) of the box.
 62        """
 63        try:
 64            self.width, self.height = value
 65        except:
 66            logger.warning("Unable to set KBox dimensions: {}", value)
 67
 68    @property
 69    def coords(self) -> tuple[int, int, int, int]:
 70        """
 71        Returns the complete coordinates (x, y, width, height) of the box.
 72        """
 73        return self.x, self.y, self.width, self.height
 74
 75    @coords.setter
 76    def coords(self, value: tuple[int, int, int, int]) -> None:
 77        """
 78        Set the complete coordinates (x, y, width, height) of the box.
 79        """
 80        try:
 81            self.x, self.y, self.width, self.height = value
 82        except:
 83            logger.warning("Unable to set KBox coords: {}", value)
 84
 85    @property
 86    def left(self) -> int:
 87        """
 88        Returns the x-coordinate of the left edge of the box.
 89        """
 90        return self.x
 91
 92    @property
 93    def right(self) -> int:
 94        """
 95        Returns the x-coordinate of the right edge of the box.
 96        """
 97        return self.x + self.width
 98
 99    @property
100    def top(self) -> int:
101        """
102        Returns the y-coordinate of the top edge of the box.
103        """
104        return self.y + self.height
105
106    @property
107    def bottom(self) -> int:
108        """
109        Returns the y-coordinate of the bottom edge of the box.
110        """
111        return self.y
112
113    def xray(self, draw: bool = True) -> "KBox":
114        """
115        Visualize the box using `lib.layout.xray` function.
116
117        Args:
118            draw: Whether to draw the box. Defaults to True.
119        """
120        if draw:
121            layout.xray(self.coords)
122        return self
123
124    def move(self, x=0, y=0, mode: layout.UnitMode = "mm") -> "KBox":
125        """
126        Move the box by the specified amounts in x and y directions.
127
128        Args:
129            x: Amount to move in the x direction. Defaults to 0.
130            y: Amount to move in the y direction. Defaults to 0.
131            mode: The unit mode ("mm", "pt", etc.). Defaults to "mm".
132        """
133        self.coords = layout.move(self.coords, x=x, y=y, mode=mode)
134        return self
135
136    def grow(self, margin: tuple[int], mode: layout.UnitMode = "mm") -> "KBox":
137        """
138        Grow the box by adding margins on all sides.
139
140        Args:
141            margin: A tuple of (left, bottom, right, top) margins to add.
142            mode: The unit mode ("mm", "pt", etc.). Defaults to "mm".
143        """
144        self.coords = layout.grow(self.coords, margin=margin, mode=mode)
145        return self
146
147    def shrink(self, margin: tuple[int], mode: layout.UnitMode = "mm") -> "KBox":
148        """
149        Shrink the box by adding internal margins on all sides.
150
151        Args:
152            margin: A tuple of (left, bottom, right, top) margins to subtract.
153            mode: The unit mode ("mm", "pt", etc.). Defaults to "mm".
154        """
155        self.coords = layout.shrink(self.coords, margin=margin, mode=mode)
156        return self
157
158    def shrinkWidth(
159        self,
160        amount: int,
161        origin: Literal["left", "right"] = "left",
162        mode: layout.UnitMode = "relative",
163    ) -> "KBox":
164        """
165        Shrink the width of the box by a specified amount.
166
167        Args:
168            amount: The amount to shrink the width by.
169            origin: The side from which to shrink. Defaults to "left".
170            mode: The unit mode ("relative", "mm", "pt", etc.). Defaults to "relative".
171        """
172        self.coords = layout.shrinkWidth(
173            self.coords, amount=amount, origin=origin, mode=mode
174        )
175        return self
176
177    def shrinkHeight(
178        self,
179        amount: int,
180        origin: Literal["top", "bottom"] = "top",
181        mode: layout.UnitMode = "relative",
182    ) -> "KBox":
183        """
184        Shrink the height of the box by a specified amount.
185
186        Args:
187            amount: The amount to shrink the height by.
188            origin: The side from which to shrink. Defaults to "top".
189            mode: The unit mode ("relative", "mm", "pt", etc.). Defaults to "relative".
190        """
191        self.coords = layout.shrinkHeight(
192            self.coords, amount=amount, origin=origin, mode=mode
193        )
194        return self
195
196    def split(
197        self,
198        formula: str = "1/1",
199        gap: int = 0,
200        mode: layout.LayoutDirection = "columns",
201    ) -> list["KBox"]:
202        """Split into child boxes by specified ratio. See `layout.split`."""
203        return map(KBox, layout.split(self.coords, formula, gap, mode))
204
205    def splitAbsolute(
206        self, split: layout.SplitMode, sizeInMm: float, gapInMm: float
207    ) -> list["KBox"]:
208        """Split into child boxes by absolute size. See `layout.splitAbsolute`."""
209        return map(KBox, layout.splitAbsolute(self.coords, split, sizeInMm, gapInMm))
210
211    def alignInside(
212        self,
213        container: Union["KBox", tuple],
214        position: tuple[layout.AlignX, layout.AlignY] = ("center", "center"),
215        mode: layout.AlignMode = "visual",
216    ) -> "KBox":
217        """
218        Align this box inside a container box.
219
220        Args:
221            container: The container `KBox` or `tuple` of coordinates.
222            position: The alignment position (x, y). Defaults to ("center", "center").
223            mode: The alignment mode. Defaults to "visual". See `lib.layout.AlignMode`.
224        """
225        self.coords = layout.align(
226            container.coords if isinstance(container, KBox) else container,
227            self.coords,
228            position=position,
229            mode=mode,
230        )
231        return self
232
233    def addCropMarks(
234        self,
235        edges: list[layout.BoxEdge] = ["top", "right", "bottom", "left"],
236        length: float = 1.5,
237        offset: float = 0.5,
238    ) -> "KBox":
239        """
240        Add crop marks to the box.
241
242        Args:
243            edges: The edges to add crop marks to. Defaults to all four edges.
244            length: The length of crop marks in mm. Defaults to 1.5.
245            offset: The offset of crop marks in mm. Defaults to 0.5.
246        """
247        length, offset = layout.mm(length), layout.mm(offset)
248
249        def _setLineAttrs():
250            drawBot.fill(None)
251            drawBot.cmykStroke(*color.CMYK(k=100).values)
252            drawBot.strokeWidth(0.5)
253
254        def _drawVertical(x: int):
255            drawBot.line((x, self.bottom - offset - length), (x, self.bottom - offset))
256            drawBot.line((x, self.top + offset), (x, self.top + offset + length))
257
258        def _drawHorizontal(y: int):
259            drawBot.line((self.left - offset - length, y), (self.left - offset, y))
260            drawBot.line((self.right + offset, y), (self.right + offset + length, y))
261
262        with drawBot.savedState():
263            _setLineAttrs()
264
265            for edge in helpers.coerceList(edges):
266                func = _drawHorizontal if edge in ["top", "bottom"] else _drawVertical
267                coordinate: int = self.__getattribute__(edge)  # self.top => 123
268                func(coordinate)
269
270        return self
class KBox:
  9class KBox:
 10    "A layout primitive with coordinates, dimensions and spatial utilities."
 11
 12    def __init__(self, coords: tuple) -> None:
 13        """
 14        Initialize a KBox with given coordinates.
 15
 16        See `lib.layout.parsePageSize` for possible coordinate types.
 17
 18        Args:
 19            coords: A tuple of (x, y, width, height) coordinates.
 20
 21        Example:
 22        ```python
 23        # Create a new box with coordinates
 24        box = KBox((10, 10, 100, 50))  # x, y, width, height
 25
 26        # Move the box
 27        box.move(x=20, y=10)
 28
 29        # Grow the box by adding margin
 30        box.grow((5, 5, 5, 5))  # left, bottom, right, top margins
 31        ```
 32        """
 33        self.x, self.y, self.width, self.height = layout.toCoords(coords)
 34
 35    @property
 36    def position(self) -> tuple[int, int]:
 37        """
 38        Returns the x and y coordinates of the bottom-left corner.
 39        """
 40        return self.x, self.y
 41
 42    @position.setter
 43    def position(self, value: tuple[int, int]) -> None:
 44        """
 45        Set the position (x, y) of the box.
 46        """
 47        try:
 48            self.x, self.y = value
 49        except:
 50            logger.warning("Unable to set KBox position: {}", value)
 51
 52    @property
 53    def dimensions(self) -> tuple[int, int]:
 54        """
 55        Returns the dimensions (width, height) of the box.
 56        """
 57        return self.width, self.height
 58
 59    @dimensions.setter
 60    def dimensions(self, value: tuple[int, int]) -> None:
 61        """
 62        Set the dimensions (width, height) of the box.
 63        """
 64        try:
 65            self.width, self.height = value
 66        except:
 67            logger.warning("Unable to set KBox dimensions: {}", value)
 68
 69    @property
 70    def coords(self) -> tuple[int, int, int, int]:
 71        """
 72        Returns the complete coordinates (x, y, width, height) of the box.
 73        """
 74        return self.x, self.y, self.width, self.height
 75
 76    @coords.setter
 77    def coords(self, value: tuple[int, int, int, int]) -> None:
 78        """
 79        Set the complete coordinates (x, y, width, height) of the box.
 80        """
 81        try:
 82            self.x, self.y, self.width, self.height = value
 83        except:
 84            logger.warning("Unable to set KBox coords: {}", value)
 85
 86    @property
 87    def left(self) -> int:
 88        """
 89        Returns the x-coordinate of the left edge of the box.
 90        """
 91        return self.x
 92
 93    @property
 94    def right(self) -> int:
 95        """
 96        Returns the x-coordinate of the right edge of the box.
 97        """
 98        return self.x + self.width
 99
100    @property
101    def top(self) -> int:
102        """
103        Returns the y-coordinate of the top edge of the box.
104        """
105        return self.y + self.height
106
107    @property
108    def bottom(self) -> int:
109        """
110        Returns the y-coordinate of the bottom edge of the box.
111        """
112        return self.y
113
114    def xray(self, draw: bool = True) -> "KBox":
115        """
116        Visualize the box using `lib.layout.xray` function.
117
118        Args:
119            draw: Whether to draw the box. Defaults to True.
120        """
121        if draw:
122            layout.xray(self.coords)
123        return self
124
125    def move(self, x=0, y=0, mode: layout.UnitMode = "mm") -> "KBox":
126        """
127        Move the box by the specified amounts in x and y directions.
128
129        Args:
130            x: Amount to move in the x direction. Defaults to 0.
131            y: Amount to move in the y direction. Defaults to 0.
132            mode: The unit mode ("mm", "pt", etc.). Defaults to "mm".
133        """
134        self.coords = layout.move(self.coords, x=x, y=y, mode=mode)
135        return self
136
137    def grow(self, margin: tuple[int], mode: layout.UnitMode = "mm") -> "KBox":
138        """
139        Grow the box by adding margins on all sides.
140
141        Args:
142            margin: A tuple of (left, bottom, right, top) margins to add.
143            mode: The unit mode ("mm", "pt", etc.). Defaults to "mm".
144        """
145        self.coords = layout.grow(self.coords, margin=margin, mode=mode)
146        return self
147
148    def shrink(self, margin: tuple[int], mode: layout.UnitMode = "mm") -> "KBox":
149        """
150        Shrink the box by adding internal margins on all sides.
151
152        Args:
153            margin: A tuple of (left, bottom, right, top) margins to subtract.
154            mode: The unit mode ("mm", "pt", etc.). Defaults to "mm".
155        """
156        self.coords = layout.shrink(self.coords, margin=margin, mode=mode)
157        return self
158
159    def shrinkWidth(
160        self,
161        amount: int,
162        origin: Literal["left", "right"] = "left",
163        mode: layout.UnitMode = "relative",
164    ) -> "KBox":
165        """
166        Shrink the width of the box by a specified amount.
167
168        Args:
169            amount: The amount to shrink the width by.
170            origin: The side from which to shrink. Defaults to "left".
171            mode: The unit mode ("relative", "mm", "pt", etc.). Defaults to "relative".
172        """
173        self.coords = layout.shrinkWidth(
174            self.coords, amount=amount, origin=origin, mode=mode
175        )
176        return self
177
178    def shrinkHeight(
179        self,
180        amount: int,
181        origin: Literal["top", "bottom"] = "top",
182        mode: layout.UnitMode = "relative",
183    ) -> "KBox":
184        """
185        Shrink the height of the box by a specified amount.
186
187        Args:
188            amount: The amount to shrink the height by.
189            origin: The side from which to shrink. Defaults to "top".
190            mode: The unit mode ("relative", "mm", "pt", etc.). Defaults to "relative".
191        """
192        self.coords = layout.shrinkHeight(
193            self.coords, amount=amount, origin=origin, mode=mode
194        )
195        return self
196
197    def split(
198        self,
199        formula: str = "1/1",
200        gap: int = 0,
201        mode: layout.LayoutDirection = "columns",
202    ) -> list["KBox"]:
203        """Split into child boxes by specified ratio. See `layout.split`."""
204        return map(KBox, layout.split(self.coords, formula, gap, mode))
205
206    def splitAbsolute(
207        self, split: layout.SplitMode, sizeInMm: float, gapInMm: float
208    ) -> list["KBox"]:
209        """Split into child boxes by absolute size. See `layout.splitAbsolute`."""
210        return map(KBox, layout.splitAbsolute(self.coords, split, sizeInMm, gapInMm))
211
212    def alignInside(
213        self,
214        container: Union["KBox", tuple],
215        position: tuple[layout.AlignX, layout.AlignY] = ("center", "center"),
216        mode: layout.AlignMode = "visual",
217    ) -> "KBox":
218        """
219        Align this box inside a container box.
220
221        Args:
222            container: The container `KBox` or `tuple` of coordinates.
223            position: The alignment position (x, y). Defaults to ("center", "center").
224            mode: The alignment mode. Defaults to "visual". See `lib.layout.AlignMode`.
225        """
226        self.coords = layout.align(
227            container.coords if isinstance(container, KBox) else container,
228            self.coords,
229            position=position,
230            mode=mode,
231        )
232        return self
233
234    def addCropMarks(
235        self,
236        edges: list[layout.BoxEdge] = ["top", "right", "bottom", "left"],
237        length: float = 1.5,
238        offset: float = 0.5,
239    ) -> "KBox":
240        """
241        Add crop marks to the box.
242
243        Args:
244            edges: The edges to add crop marks to. Defaults to all four edges.
245            length: The length of crop marks in mm. Defaults to 1.5.
246            offset: The offset of crop marks in mm. Defaults to 0.5.
247        """
248        length, offset = layout.mm(length), layout.mm(offset)
249
250        def _setLineAttrs():
251            drawBot.fill(None)
252            drawBot.cmykStroke(*color.CMYK(k=100).values)
253            drawBot.strokeWidth(0.5)
254
255        def _drawVertical(x: int):
256            drawBot.line((x, self.bottom - offset - length), (x, self.bottom - offset))
257            drawBot.line((x, self.top + offset), (x, self.top + offset + length))
258
259        def _drawHorizontal(y: int):
260            drawBot.line((self.left - offset - length, y), (self.left - offset, y))
261            drawBot.line((self.right + offset, y), (self.right + offset + length, y))
262
263        with drawBot.savedState():
264            _setLineAttrs()
265
266            for edge in helpers.coerceList(edges):
267                func = _drawHorizontal if edge in ["top", "bottom"] else _drawVertical
268                coordinate: int = self.__getattribute__(edge)  # self.top => 123
269                func(coordinate)
270
271        return self

A layout primitive with coordinates, dimensions and spatial utilities.

KBox(coords: tuple)
12    def __init__(self, coords: tuple) -> None:
13        """
14        Initialize a KBox with given coordinates.
15
16        See `lib.layout.parsePageSize` for possible coordinate types.
17
18        Args:
19            coords: A tuple of (x, y, width, height) coordinates.
20
21        Example:
22        ```python
23        # Create a new box with coordinates
24        box = KBox((10, 10, 100, 50))  # x, y, width, height
25
26        # Move the box
27        box.move(x=20, y=10)
28
29        # Grow the box by adding margin
30        box.grow((5, 5, 5, 5))  # left, bottom, right, top margins
31        ```
32        """
33        self.x, self.y, self.width, self.height = layout.toCoords(coords)

Initialize a KBox with given coordinates.

See lib.layout.parsePageSize for possible coordinate types.

Arguments:
  • coords: A tuple of (x, y, width, height) coordinates.

Example:

# Create a new box with coordinates
box = KBox((10, 10, 100, 50))  # x, y, width, height

# Move the box
box.move(x=20, y=10)

# Grow the box by adding margin
box.grow((5, 5, 5, 5))  # left, bottom, right, top margins
position: tuple[int, int]
35    @property
36    def position(self) -> tuple[int, int]:
37        """
38        Returns the x and y coordinates of the bottom-left corner.
39        """
40        return self.x, self.y

Returns the x and y coordinates of the bottom-left corner.

dimensions: tuple[int, int]
52    @property
53    def dimensions(self) -> tuple[int, int]:
54        """
55        Returns the dimensions (width, height) of the box.
56        """
57        return self.width, self.height

Returns the dimensions (width, height) of the box.

coords: tuple[int, int, int, int]
69    @property
70    def coords(self) -> tuple[int, int, int, int]:
71        """
72        Returns the complete coordinates (x, y, width, height) of the box.
73        """
74        return self.x, self.y, self.width, self.height

Returns the complete coordinates (x, y, width, height) of the box.

left: int
86    @property
87    def left(self) -> int:
88        """
89        Returns the x-coordinate of the left edge of the box.
90        """
91        return self.x

Returns the x-coordinate of the left edge of the box.

right: int
93    @property
94    def right(self) -> int:
95        """
96        Returns the x-coordinate of the right edge of the box.
97        """
98        return self.x + self.width

Returns the x-coordinate of the right edge of the box.

top: int
100    @property
101    def top(self) -> int:
102        """
103        Returns the y-coordinate of the top edge of the box.
104        """
105        return self.y + self.height

Returns the y-coordinate of the top edge of the box.

bottom: int
107    @property
108    def bottom(self) -> int:
109        """
110        Returns the y-coordinate of the bottom edge of the box.
111        """
112        return self.y

Returns the y-coordinate of the bottom edge of the box.

def xray(self, draw: bool = True) -> KBox:
114    def xray(self, draw: bool = True) -> "KBox":
115        """
116        Visualize the box using `lib.layout.xray` function.
117
118        Args:
119            draw: Whether to draw the box. Defaults to True.
120        """
121        if draw:
122            layout.xray(self.coords)
123        return self

Visualize the box using lib.layout.xray function.

Arguments:
  • draw: Whether to draw the box. Defaults to True.
def move( self, x=0, y=0, mode: Literal['mm', 'pt', 'relative'] = 'mm') -> KBox:
125    def move(self, x=0, y=0, mode: layout.UnitMode = "mm") -> "KBox":
126        """
127        Move the box by the specified amounts in x and y directions.
128
129        Args:
130            x: Amount to move in the x direction. Defaults to 0.
131            y: Amount to move in the y direction. Defaults to 0.
132            mode: The unit mode ("mm", "pt", etc.). Defaults to "mm".
133        """
134        self.coords = layout.move(self.coords, x=x, y=y, mode=mode)
135        return self

Move the box by the specified amounts in x and y directions.

Arguments:
  • x: Amount to move in the x direction. Defaults to 0.
  • y: Amount to move in the y direction. Defaults to 0.
  • mode: The unit mode ("mm", "pt", etc.). Defaults to "mm".
def grow( self, margin: tuple[int], mode: Literal['mm', 'pt', 'relative'] = 'mm') -> KBox:
137    def grow(self, margin: tuple[int], mode: layout.UnitMode = "mm") -> "KBox":
138        """
139        Grow the box by adding margins on all sides.
140
141        Args:
142            margin: A tuple of (left, bottom, right, top) margins to add.
143            mode: The unit mode ("mm", "pt", etc.). Defaults to "mm".
144        """
145        self.coords = layout.grow(self.coords, margin=margin, mode=mode)
146        return self

Grow the box by adding margins on all sides.

Arguments:
  • margin: A tuple of (left, bottom, right, top) margins to add.
  • mode: The unit mode ("mm", "pt", etc.). Defaults to "mm".
def shrink( self, margin: tuple[int], mode: Literal['mm', 'pt', 'relative'] = 'mm') -> KBox:
148    def shrink(self, margin: tuple[int], mode: layout.UnitMode = "mm") -> "KBox":
149        """
150        Shrink the box by adding internal margins on all sides.
151
152        Args:
153            margin: A tuple of (left, bottom, right, top) margins to subtract.
154            mode: The unit mode ("mm", "pt", etc.). Defaults to "mm".
155        """
156        self.coords = layout.shrink(self.coords, margin=margin, mode=mode)
157        return self

Shrink the box by adding internal margins on all sides.

Arguments:
  • margin: A tuple of (left, bottom, right, top) margins to subtract.
  • mode: The unit mode ("mm", "pt", etc.). Defaults to "mm".
def shrinkWidth( self, amount: int, origin: Literal['left', 'right'] = 'left', mode: Literal['mm', 'pt', 'relative'] = 'relative') -> KBox:
159    def shrinkWidth(
160        self,
161        amount: int,
162        origin: Literal["left", "right"] = "left",
163        mode: layout.UnitMode = "relative",
164    ) -> "KBox":
165        """
166        Shrink the width of the box by a specified amount.
167
168        Args:
169            amount: The amount to shrink the width by.
170            origin: The side from which to shrink. Defaults to "left".
171            mode: The unit mode ("relative", "mm", "pt", etc.). Defaults to "relative".
172        """
173        self.coords = layout.shrinkWidth(
174            self.coords, amount=amount, origin=origin, mode=mode
175        )
176        return self

Shrink the width of the box by a specified amount.

Arguments:
  • amount: The amount to shrink the width by.
  • origin: The side from which to shrink. Defaults to "left".
  • mode: The unit mode ("relative", "mm", "pt", etc.). Defaults to "relative".
def shrinkHeight( self, amount: int, origin: Literal['top', 'bottom'] = 'top', mode: Literal['mm', 'pt', 'relative'] = 'relative') -> KBox:
178    def shrinkHeight(
179        self,
180        amount: int,
181        origin: Literal["top", "bottom"] = "top",
182        mode: layout.UnitMode = "relative",
183    ) -> "KBox":
184        """
185        Shrink the height of the box by a specified amount.
186
187        Args:
188            amount: The amount to shrink the height by.
189            origin: The side from which to shrink. Defaults to "top".
190            mode: The unit mode ("relative", "mm", "pt", etc.). Defaults to "relative".
191        """
192        self.coords = layout.shrinkHeight(
193            self.coords, amount=amount, origin=origin, mode=mode
194        )
195        return self

Shrink the height of the box by a specified amount.

Arguments:
  • amount: The amount to shrink the height by.
  • origin: The side from which to shrink. Defaults to "top".
  • mode: The unit mode ("relative", "mm", "pt", etc.). Defaults to "relative".
def split( self, formula: str = '1/1', gap: int = 0, mode: Literal['columns', 'rows'] = 'columns') -> list[KBox]:
197    def split(
198        self,
199        formula: str = "1/1",
200        gap: int = 0,
201        mode: layout.LayoutDirection = "columns",
202    ) -> list["KBox"]:
203        """Split into child boxes by specified ratio. See `layout.split`."""
204        return map(KBox, layout.split(self.coords, formula, gap, mode))

Split into child boxes by specified ratio. See layout.split.

def splitAbsolute( self, split: Literal['horizontal', 'vertical'], sizeInMm: float, gapInMm: float) -> list[KBox]:
206    def splitAbsolute(
207        self, split: layout.SplitMode, sizeInMm: float, gapInMm: float
208    ) -> list["KBox"]:
209        """Split into child boxes by absolute size. See `layout.splitAbsolute`."""
210        return map(KBox, layout.splitAbsolute(self.coords, split, sizeInMm, gapInMm))

Split into child boxes by absolute size. See layout.splitAbsolute.

def alignInside( self, container: Union[KBox, tuple], position: tuple[typing.Literal['left', 'center', 'right', 'stretch', None], typing.Literal['top', 'center', 'bottom', 'stretch', None]] = ('center', 'center'), mode: Literal['precise', 'visual'] = 'visual') -> KBox:
212    def alignInside(
213        self,
214        container: Union["KBox", tuple],
215        position: tuple[layout.AlignX, layout.AlignY] = ("center", "center"),
216        mode: layout.AlignMode = "visual",
217    ) -> "KBox":
218        """
219        Align this box inside a container box.
220
221        Args:
222            container: The container `KBox` or `tuple` of coordinates.
223            position: The alignment position (x, y). Defaults to ("center", "center").
224            mode: The alignment mode. Defaults to "visual". See `lib.layout.AlignMode`.
225        """
226        self.coords = layout.align(
227            container.coords if isinstance(container, KBox) else container,
228            self.coords,
229            position=position,
230            mode=mode,
231        )
232        return self

Align this box inside a container box.

Arguments:
  • container: The container KBox or tuple of coordinates.
  • position: The alignment position (x, y). Defaults to ("center", "center").
  • mode: The alignment mode. Defaults to "visual". See lib.layout.AlignMode.
def addCropMarks( self, edges: list[typing.Literal['top', 'right', 'bottom', 'left']] = ['top', 'right', 'bottom', 'left'], length: float = 1.5, offset: float = 0.5) -> KBox:
234    def addCropMarks(
235        self,
236        edges: list[layout.BoxEdge] = ["top", "right", "bottom", "left"],
237        length: float = 1.5,
238        offset: float = 0.5,
239    ) -> "KBox":
240        """
241        Add crop marks to the box.
242
243        Args:
244            edges: The edges to add crop marks to. Defaults to all four edges.
245            length: The length of crop marks in mm. Defaults to 1.5.
246            offset: The offset of crop marks in mm. Defaults to 0.5.
247        """
248        length, offset = layout.mm(length), layout.mm(offset)
249
250        def _setLineAttrs():
251            drawBot.fill(None)
252            drawBot.cmykStroke(*color.CMYK(k=100).values)
253            drawBot.strokeWidth(0.5)
254
255        def _drawVertical(x: int):
256            drawBot.line((x, self.bottom - offset - length), (x, self.bottom - offset))
257            drawBot.line((x, self.top + offset), (x, self.top + offset + length))
258
259        def _drawHorizontal(y: int):
260            drawBot.line((self.left - offset - length, y), (self.left - offset, y))
261            drawBot.line((self.right + offset, y), (self.right + offset + length, y))
262
263        with drawBot.savedState():
264            _setLineAttrs()
265
266            for edge in helpers.coerceList(edges):
267                func = _drawHorizontal if edge in ["top", "bottom"] else _drawVertical
268                coordinate: int = self.__getattribute__(edge)  # self.top => 123
269                func(coordinate)
270
271        return self

Add crop marks to the box.

Arguments:
  • edges: The edges to add crop marks to. Defaults to all four edges.
  • length: The length of crop marks in mm. Defaults to 1.5.
  • offset: The offset of crop marks in mm. Defaults to 0.5.