simetri.graphics.sketch
This module creates sketch objects with a neutral format for drawing. Every other format is converted from this format. If you need to save as a different format, you can use these sketch objects to convert to the format you need. Sketches are not meant to be modified. They preserve the state of graphics objects at the time of drawing. They are snapshots of the state of the objects and the Canvas at the time of drawing.
1""" 2This module creates sketch objects with a neutral format for drawing. 3Every other format is converted from this format. 4If you need to save as a different format, you can use these 5sketch objects to convert to the format you need. 6Sketches are not meant to be modified. 7They preserve the state of graphics objects at the time of drawing. 8They are snapshots of the state of the objects and the Canvas at the time of drawing. 9""" 10 11from dataclasses import dataclass 12from typing import List, Any 13 14import numpy as np 15from numpy import ndarray 16 17from ..colors import colors 18from .affine import identity_matrix 19from .common import common_properties, Point 20from .all_enums import Types, Anchor, FrameShape, CurveMode, TexLoc 21from ..settings.settings import defaults 22from ..geometry.geometry import homogenize 23from ..helpers.utilities import decompose_transformations 24from .pattern import Pattern 25 26Color = colors.Color 27 28np.set_printoptions(legacy="1.21") 29 30 31@dataclass 32class CircleSketch: 33 """CircleSketch is a dataclass for creating a circle sketch object. 34 35 Attributes: 36 center (tuple): The center of the circle. 37 radius (float): The radius of the circle. 38 xform_matrix (ndarray, optional): The transformation matrix. Defaults to None. 39 """ 40 41 center: tuple 42 radius: float 43 xform_matrix: ndarray = None 44 45 46 def __post_init__(self): 47 """Initialize the CircleSketch object.""" 48 self.type = Types.SKETCH 49 self.subtype = Types.CIRCLE_SKETCH 50 if self.xform_matrix is None: 51 self.xform_matrix = identity_matrix() 52 center = self.center 53 else: 54 center = homogenize([self.center]) 55 center = (center @ self.xform_matrix).tolist()[0][:2] 56 self.center = center 57 self.closed = True 58 59 60@dataclass 61class EllipseSketch: 62 """EllipseSketch is a dataclass for creating an ellipse sketch object. 63 64 Attributes: 65 center (tuple): The center of the ellipse. 66 x_radius (float): The x-axis radius of the ellipse. 67 y_radius (float): The y-axis radius of the ellipse. 68 angle (float, optional): The orientation angle. Defaults to 0. 69 xform_matrix (ndarray, optional): The transformation matrix. Defaults to None. 70 """ 71 72 center: tuple 73 x_radius: float 74 y_radius: float 75 angle: float = 0 # orientation angle 76 xform_matrix: ndarray = None 77 78 def __post_init__(self): 79 """Initialize the EllipseSketch object.""" 80 self.type = Types.SKETCH 81 self.subtype = Types.ELLIPSE_SKETCH 82 if self.xform_matrix is None: 83 self.xform_matrix = identity_matrix() 84 center = self.center 85 else: 86 center = homogenize([self.center]) 87 center = (center @ self.xform_matrix).tolist()[0][:2] 88 self.center = center 89 self.closed = True 90 91 92@dataclass 93class LineSketch: 94 """LineSketch is a dataclass for creating a line sketch object. 95 96 Attributes: 97 vertices (list): The vertices of the line. 98 xform_matrix (ndarray, optional): The transformation matrix. Defaults to None. 99 """ 100 101 vertices: list 102 xform_matrix: ndarray = None 103 104 def __post_init__(self): 105 """Initialize the LineSketch object.""" 106 self.type = Types.SKETCH 107 self.subtype = Types.LINE_SKETCH 108 109 if self.xform_matrix is None: 110 self.xform_matrix = identity_matrix() 111 vertices = self.vertices 112 else: 113 vertices = homogenize(self.vertices) 114 vertices = vertices @ self.xform_matrix 115 self.vertices = [tuple(x) for x in vertices[:, :2]] 116 117 118@dataclass 119class PatternSketch: 120 """PatternSketch is a dataclass for creating a pattern sketch object. 121 122 Attributes: 123 pattern Pattern: The pattern object. 124 xform_matrix (ndarray, optional): The transformation matrix. Defaults to None. 125 """ 126 127 pattern: Pattern = None 128 xform_matrix: ndarray = None 129 130 def __post_init__(self): 131 """Initialize the PatternSketch object.""" 132 self.type = Types.SKETCH 133 self.subtype = Types.PATTERN_SKETCH 134 if self.xform_matrix is None: 135 self.xform_matrix = identity_matrix() 136 self.kernel_vertices = self.pattern.kernel.final_coords 137 self.all_matrices = self.pattern.composite 138 self.count = self.pattern.count 139 self.closed = self.pattern.closed 140 141@dataclass 142class TexSketch: 143 """TexSketch is a dataclass for inserting code into the tex file. 144 145 Attributes: 146 code (str, optional): The code to be inserted. Defaults to None. 147 location (TexLoc, optional): The location of the code. Defaults to TexLoc.NONE. 148 149 Returns: 150 None 151 """ 152 153 code: str = None 154 location: TexLoc = TexLoc.NONE 155 156 def __post_init__(self): 157 """Initialize the TexSketch object.""" 158 self.type = Types.SKETCH 159 self.subtype = Types.TEX_SKETCH 160 161@dataclass 162class ShapeSketch: 163 """ShapeSketch is a neutral format for drawing. 164 165 It contains geometry (only vertices for shapes) and style properties. 166 Style properties are not assigned during initialization. 167 They are not meant to be transformed, only to be drawn. 168 Sketches have no methods, only data. 169 They do not check anything, they just store data. 170 They are populated during sketch creation. 171 You should make sure the data is correct before creating a sketch. 172 173 Attributes: 174 vertices (list, optional): The vertices of the shape. Defaults to None. 175 xform_matrix (ndarray, optional): The transformation matrix. Defaults to None. 176 """ 177 178 vertices: list = None 179 xform_matrix: ndarray = None 180 181 def __post_init__(self): 182 """Initialize the ShapeSketch object.""" 183 self.type = Types.SKETCH 184 self.subtype = Types.SHAPE_SKETCH 185 if self.xform_matrix is None: 186 vertices = self.vertices 187 self.xform_matrix = identity_matrix() 188 else: 189 vertices = homogenize(self.vertices) 190 vertices = vertices @ self.xform_matrix 191 self.vertices = [tuple(x) for x in vertices[:, :2]] 192 193 194@dataclass 195class BezierSketch: 196 """BezierSketch is a dataclass for creating a bezier sketch object. 197 198 Attributes: 199 control_points (list): The control points of the bezier curve. 200 xform_matrix (ndarray, optional): The transformation matrix. Defaults to None. 201 mode (CurveMode, optional): The mode of the curve. Defaults to CurveMode.OPEN. 202 """ 203 204 control_points: list 205 xform_matrix: ndarray = None 206 mode: CurveMode = CurveMode.OPEN 207 208 def __post_init__(self): 209 """Initialize the BezierSketch object.""" 210 self.type = Types.SKETCH 211 self.subtype = Types.BEZIER_SKETCH 212 213 if self.xform_matrix is None: 214 self.xform_matrix = identity_matrix() 215 control_points = self.control_points 216 else: 217 control_points = homogenize(self.control_points) 218 control_points = control_points @ self.xform_matrix 219 self.control_points = [tuple(x) for x in control_points[:, :3]] 220 self.closed = False 221 222 223@dataclass 224class ArcSketch: 225 """ArcSketch is a dataclass for creating an arc sketch object. 226 227 Attributes: 228 vertices (list, optional): The vertices of the shape. Defaults to None. 229 xform_matrix (ndarray, optional): The transformation matrix. Defaults to None. 230 mode (CurveMode, optional): The mode of the curve. Defaults to CurveMode.OPEN. 231 """ 232 233 vertices: list = None 234 xform_matrix: ndarray = None 235 mode: CurveMode = CurveMode.OPEN 236 237 def __post_init__(self): 238 """Initialize the ArcSketch object.""" 239 if self.xform_matrix is None: 240 self.xform_matrix = identity_matrix() 241 vertices = self.vertices 242 else: 243 vertices = homogenize(self.vertices) 244 vertices = vertices @ self.xform_matrix 245 self.vertices = [tuple(x) for x in vertices[:, :2]] 246 247 self.type = Types.SKETCH 248 self.subtype = Types.ARC_SKETCH 249 self.closed = self.mode != CurveMode.OPEN 250 251 252 253 254@dataclass 255class BatchSketch: 256 """BatchSketch is a dataclass for creating a batch sketch object. 257 258 Attributes: 259 sketches (List[Types.SKETCH]): The list of sketches. 260 xform_matrix (ndarray, optional): The transformation matrix. Defaults to None. 261 """ 262 263 sketches: List[Types.SKETCH] 264 xform_matrix: ndarray = None 265 266 def __post_init__(self): 267 """Initialize the BatchSketch object.""" 268 self.type = Types.SKETCH 269 self.subtype = Types.BATCH_SKETCH 270 self.sketches = self.sketches 271 272 273@dataclass 274class PathSketch: 275 """PathSketch is a dataclass for creating a path sketch object. 276 277 Attributes: 278 sketches (List[Types.SKETCH]): The list of sketches. 279 xform_matrix (ndarray, optional): The transformation matrix. Defaults to None. 280 """ 281 282 sketches: List[Types.SKETCH] 283 xform_matrix: ndarray = None 284 285 def __post_init__(self): 286 """Initialize the PathSketch object.""" 287 self.type = Types.SKETCH 288 self.subtype = Types.PATH_SKETCH 289 if self.xform_matrix is None: 290 self.xform_matrix = identity_matrix() 291 292 293@dataclass 294class LaceSketch: 295 """LaceSketch is a dataclass for creating a lace sketch object. 296 297 Attributes: 298 fragment_sketches (List[ShapeSketch]): The list of fragment sketches. 299 plait_sketches (List[ShapeSketch]): The list of plait sketches. 300 xform_matrix (ndarray, optional): The transformation matrix. Defaults to None. 301 """ 302 303 fragment_sketches: List[ShapeSketch] 304 plait_sketches: List[ShapeSketch] 305 xform_matrix: ndarray = None 306 307 def __post_init__(self): 308 """Initialize the LaceSketch object.""" 309 self.type = Types.SKETCH 310 self.subtype = Types.LACESKETCH 311 if self.xform_matrix is None: 312 self.xform_matrix = identity_matrix() 313 314 315@dataclass 316class FrameSketch: 317 """FrameSketch is a dataclass for creating a frame sketch object. 318 319 Attributes: 320 frame_shape (FrameShape, optional): The shape of the frame. Defaults to "rectangle". 321 line_width (float, optional): The width of the line. Defaults to 1. 322 line_dash_array (list, optional): The dash array for the line. Defaults to None. 323 line_color (Color, optional): The color of the line. Defaults to colors.black. 324 back_color (Color, optional): The background color. Defaults to colors.white. 325 fill (bool, optional): Whether to fill the frame. Defaults to False. 326 stroke (bool, optional): Whether to stroke the frame. Defaults to True. 327 double (bool, optional): Whether to draw a double line. Defaults to False. 328 double_distance (float, optional): The distance between double lines. Defaults to 2. 329 inner_sep (float, optional): The inner separation. Defaults to 10. 330 outer_sep (float, optional): The outer separation. Defaults to 10. 331 smooth (bool, optional): Whether to smooth the frame. Defaults to False. 332 rounded_corners (bool, optional): Whether to round the corners. Defaults to False. 333 fillet_radius (float, optional): The radius of the fillet. Defaults to 10. 334 draw_fillets (bool, optional): Whether to draw fillets. Defaults to False. 335 blend_mode (str, optional): The blend mode. Defaults to None. 336 gradient (str, optional): The gradient. Defaults to None. 337 pattern (str, optional): The pattern. Defaults to None. 338 visible (bool, optional): Whether the frame is visible. Defaults to True. 339 min_width (float, optional): The minimum width. Defaults to 0. 340 min_height (float, optional): The minimum height. Defaults to 0. 341 min_radius (float, optional): The minimum radius. Defaults to 0. 342 """ 343 344 frame_shape: FrameShape = ( 345 "rectangle" # default value cannot be FrameShape.RECTANGLE! 346 ) 347 line_width: float = 1 348 line_dash_array: list = None 349 line_color: Color = colors.black 350 back_color: Color = colors.white 351 fill: bool = False 352 stroke: bool = True 353 double: bool = False 354 double_distance: float = 2 355 inner_sep: float = 10 356 outer_sep: float = 10 357 smooth: bool = False 358 rounded_corners: bool = False 359 fillet_radius: float = 10 360 draw_fillets: bool = False 361 blend_mode: str = None 362 gradient: str = None 363 pattern: str = None 364 visible: bool = True 365 min_width: float = 0 366 min_height: float = 0 367 min_radius: float = 0 368 369 def __post_init__(self): 370 """Initialize the FrameSketch object.""" 371 self.type = Types.SKETCH 372 self.subtype = Types.FRAME_SKETCH 373 common_properties(self) 374 375 376@dataclass 377class TagSketch: 378 """TagSketch is a dataclass for creating a tag sketch object. 379 380 Attributes: 381 text (str, optional): The text of the tag. Defaults to None. 382 pos (Point, optional): The position of the tag. Defaults to None. 383 anchor (Anchor, optional): The anchor of the tag. Defaults to None. 384 font_family (str, optional): The font family. Defaults to None. 385 font_size (float, optional): The font size. Defaults to None. 386 minimum_width (float, optional): The minimum width. Defaults to None. 387 xform_matrix (ndarray, optional): The transformation matrix. Defaults to None. 388 """ 389 390 text: str = None 391 pos: Point = None 392 anchor: Anchor = None 393 font_family: str = None 394 font_size: float = None 395 minimum_width: float = None 396 xform_matrix: ndarray = None 397 398 def __post_init__(self): 399 """Initialize the TagSketch object.""" 400 self.type = Types.SKETCH 401 self.subtype = Types.TAG_SKETCH 402 if self.xform_matrix is None: 403 self.xform_matrix = identity_matrix() 404 pos = self.pos 405 else: 406 pos = homogenize([self.pos]) 407 pos = (pos @ self.xform_matrix).tolist()[0][:2] 408 self.pos = pos 409 410 411@dataclass 412class RectSketch: 413 """RectSketch is a dataclass for creating a rectangle sketch object. 414 415 Attributes: 416 pos (Point): The position of the rectangle. 417 width (float): The width of the rectangle. 418 height (float): The height of the rectangle. 419 xform_matrix (ndarray, optional): The transformation matrix. Defaults to None. 420 """ 421 422 pos: Point 423 width: float 424 height: float 425 xform_matrix: ndarray = None 426 427 def __post_init__(self): 428 """Initialize the RectSketch object. 429 430 Args: 431 pos (Point): The position of the rectangle. 432 width (float): The width of the rectangle. 433 height (float): The height of the rectangle. 434 xform_matrix (ndarray, optional): The transformation matrix. Defaults to None. 435 """ 436 self.type = Types.SKETCH 437 self.subtype = Types.RECT_SKETCH 438 if self.xform_matrix is None: 439 self.xform_matrix = identity_matrix() 440 pos = self.pos 441 else: 442 pos = homogenize([self.pos]) 443 pos = (pos @ self.xform_matrix).tolist()[0][:2] 444 self.pos = pos 445 h2 = self.height / 2 446 w2 = self.width / 2 447 self.vertices = [ 448 (pos[0] - w2, pos[1] - h2), 449 (pos[0] + w2, pos[1] - h2), 450 (pos[0] + w2, pos[1] + h2), 451 (pos[0] - w2, pos[1] + h2), 452 ] 453 self.closed = True
116@dataclass 117class Color: 118 """A class representing an RGB or RGBA color. 119 120 This class represents a color in RGB or RGBA color space. The default values 121 for the components are normalized between 0.0 and 1.0. Values outside this range 122 are automatically converted from the 0-255 range. 123 124 Attributes: 125 red: The red component of the color (0.0 to 1.0). 126 green: The green component of the color (0.0 to 1.0). 127 blue: The blue component of the color (0.0 to 1.0). 128 alpha: The alpha (transparency) component (0.0 to 1.0), default is 1. 129 space: The color space, default is "rgb". 130 131 Examples: 132 >>> red = Color(1.0, 0.0, 0.0) 133 >>> transparent_blue = Color(0.0, 0.0, 1.0, 0.5) 134 >>> rgb255 = Color(255, 0, 128) # Will be automatically normalized 135 """ 136 red: int = 0 137 green: int = 0 138 blue: int = 0 139 alpha: int = 1 140 space: ColorSpace = "rgb" # for future use 141 142 def __post_init__(self): 143 """Post-initialization to ensure color values are in the correct range.""" 144 r, g, b = self.red, self.green, self.blue 145 if r < 0 or r > 1 or g < 0 or g > 1 or b < 0 or b > 1: 146 self.red = r / 255 147 self.green = g / 255 148 self.blue = b / 255 149 if self.alpha < 0 or self.alpha > 1: 150 self.alpha = self.alpha / 255 151 common_properties(self) 152 153 def __str__(self): 154 return f"Color({self.red}, {self.green}, {self.blue})" 155 156 def __repr__(self): 157 return f"Color({self.red}, {self.green}, {self.blue})" 158 159 def copy(self): 160 return Color(self.red, self.green, self.blue, self.alpha) 161 162 @property 163 def __key__(self): 164 return (self.red, self.green, self.blue) 165 166 def __hash__(self): 167 return hash(self.__key__) 168 169 @property 170 def name(self): 171 # search for the color in the named colors 172 pass 173 174 def __eq__(self, other): 175 if isinstance(other, Color): 176 return self.__key__ == other.__key__ 177 else: 178 return False 179 180 @property 181 def rgb(self): 182 return (self.red, self.green, self.blue) 183 184 @property 185 def rgba(self): 186 return (self.red, self.green, self.blue, self.alpha) 187 188 @property 189 def rgb255(self): 190 r, g, b = self.rgb 191 if r > 1 or g > 1 or b > 1: 192 return (r, g, b) 193 return tuple(round(i * 255) for i in self.rgb) 194 195 @property 196 def rgba255(self): 197 return tuple(round(i * 255) for i in self.rgba)
A class representing an RGB or RGBA color.
This class represents a color in RGB or RGBA color space. The default values for the components are normalized between 0.0 and 1.0. Values outside this range are automatically converted from the 0-255 range.
Attributes:
- red: The red component of the color (0.0 to 1.0).
- green: The green component of the color (0.0 to 1.0).
- blue: The blue component of the color (0.0 to 1.0).
- alpha: The alpha (transparency) component (0.0 to 1.0), default is 1.
- space: The color space, default is "rgb".
Examples:
>>> red = Color(1.0, 0.0, 0.0) >>> transparent_blue = Color(0.0, 0.0, 1.0, 0.5) >>> rgb255 = Color(255, 0, 128) # Will be automatically normalized
32@dataclass 33class CircleSketch: 34 """CircleSketch is a dataclass for creating a circle sketch object. 35 36 Attributes: 37 center (tuple): The center of the circle. 38 radius (float): The radius of the circle. 39 xform_matrix (ndarray, optional): The transformation matrix. Defaults to None. 40 """ 41 42 center: tuple 43 radius: float 44 xform_matrix: ndarray = None 45 46 47 def __post_init__(self): 48 """Initialize the CircleSketch object.""" 49 self.type = Types.SKETCH 50 self.subtype = Types.CIRCLE_SKETCH 51 if self.xform_matrix is None: 52 self.xform_matrix = identity_matrix() 53 center = self.center 54 else: 55 center = homogenize([self.center]) 56 center = (center @ self.xform_matrix).tolist()[0][:2] 57 self.center = center 58 self.closed = True
CircleSketch is a dataclass for creating a circle sketch object.
Attributes:
- center (tuple): The center of the circle.
- radius (float): The radius of the circle.
- xform_matrix (ndarray, optional): The transformation matrix. Defaults to None.
61@dataclass 62class EllipseSketch: 63 """EllipseSketch is a dataclass for creating an ellipse sketch object. 64 65 Attributes: 66 center (tuple): The center of the ellipse. 67 x_radius (float): The x-axis radius of the ellipse. 68 y_radius (float): The y-axis radius of the ellipse. 69 angle (float, optional): The orientation angle. Defaults to 0. 70 xform_matrix (ndarray, optional): The transformation matrix. Defaults to None. 71 """ 72 73 center: tuple 74 x_radius: float 75 y_radius: float 76 angle: float = 0 # orientation angle 77 xform_matrix: ndarray = None 78 79 def __post_init__(self): 80 """Initialize the EllipseSketch object.""" 81 self.type = Types.SKETCH 82 self.subtype = Types.ELLIPSE_SKETCH 83 if self.xform_matrix is None: 84 self.xform_matrix = identity_matrix() 85 center = self.center 86 else: 87 center = homogenize([self.center]) 88 center = (center @ self.xform_matrix).tolist()[0][:2] 89 self.center = center 90 self.closed = True
EllipseSketch is a dataclass for creating an ellipse sketch object.
Attributes:
- center (tuple): The center of the ellipse.
- x_radius (float): The x-axis radius of the ellipse.
- y_radius (float): The y-axis radius of the ellipse.
- angle (float, optional): The orientation angle. Defaults to 0.
- xform_matrix (ndarray, optional): The transformation matrix. Defaults to None.
93@dataclass 94class LineSketch: 95 """LineSketch is a dataclass for creating a line sketch object. 96 97 Attributes: 98 vertices (list): The vertices of the line. 99 xform_matrix (ndarray, optional): The transformation matrix. Defaults to None. 100 """ 101 102 vertices: list 103 xform_matrix: ndarray = None 104 105 def __post_init__(self): 106 """Initialize the LineSketch object.""" 107 self.type = Types.SKETCH 108 self.subtype = Types.LINE_SKETCH 109 110 if self.xform_matrix is None: 111 self.xform_matrix = identity_matrix() 112 vertices = self.vertices 113 else: 114 vertices = homogenize(self.vertices) 115 vertices = vertices @ self.xform_matrix 116 self.vertices = [tuple(x) for x in vertices[:, :2]]
LineSketch is a dataclass for creating a line sketch object.
Attributes:
- vertices (list): The vertices of the line.
- xform_matrix (ndarray, optional): The transformation matrix. Defaults to None.
119@dataclass 120class PatternSketch: 121 """PatternSketch is a dataclass for creating a pattern sketch object. 122 123 Attributes: 124 pattern Pattern: The pattern object. 125 xform_matrix (ndarray, optional): The transformation matrix. Defaults to None. 126 """ 127 128 pattern: Pattern = None 129 xform_matrix: ndarray = None 130 131 def __post_init__(self): 132 """Initialize the PatternSketch object.""" 133 self.type = Types.SKETCH 134 self.subtype = Types.PATTERN_SKETCH 135 if self.xform_matrix is None: 136 self.xform_matrix = identity_matrix() 137 self.kernel_vertices = self.pattern.kernel.final_coords 138 self.all_matrices = self.pattern.composite 139 self.count = self.pattern.count 140 self.closed = self.pattern.closed
PatternSketch is a dataclass for creating a pattern sketch object.
Attributes:
- pattern Pattern: The pattern object.
- xform_matrix (ndarray, optional): The transformation matrix. Defaults to None.
142@dataclass 143class TexSketch: 144 """TexSketch is a dataclass for inserting code into the tex file. 145 146 Attributes: 147 code (str, optional): The code to be inserted. Defaults to None. 148 location (TexLoc, optional): The location of the code. Defaults to TexLoc.NONE. 149 150 Returns: 151 None 152 """ 153 154 code: str = None 155 location: TexLoc = TexLoc.NONE 156 157 def __post_init__(self): 158 """Initialize the TexSketch object.""" 159 self.type = Types.SKETCH 160 self.subtype = Types.TEX_SKETCH
TexSketch is a dataclass for inserting code into the tex file.
Attributes:
- code (str, optional): The code to be inserted. Defaults to None.
- location (TexLoc, optional): The location of the code. Defaults to TexLoc.NONE.
Returns:
None
162@dataclass 163class ShapeSketch: 164 """ShapeSketch is a neutral format for drawing. 165 166 It contains geometry (only vertices for shapes) and style properties. 167 Style properties are not assigned during initialization. 168 They are not meant to be transformed, only to be drawn. 169 Sketches have no methods, only data. 170 They do not check anything, they just store data. 171 They are populated during sketch creation. 172 You should make sure the data is correct before creating a sketch. 173 174 Attributes: 175 vertices (list, optional): The vertices of the shape. Defaults to None. 176 xform_matrix (ndarray, optional): The transformation matrix. Defaults to None. 177 """ 178 179 vertices: list = None 180 xform_matrix: ndarray = None 181 182 def __post_init__(self): 183 """Initialize the ShapeSketch object.""" 184 self.type = Types.SKETCH 185 self.subtype = Types.SHAPE_SKETCH 186 if self.xform_matrix is None: 187 vertices = self.vertices 188 self.xform_matrix = identity_matrix() 189 else: 190 vertices = homogenize(self.vertices) 191 vertices = vertices @ self.xform_matrix 192 self.vertices = [tuple(x) for x in vertices[:, :2]]
ShapeSketch is a neutral format for drawing.
It contains geometry (only vertices for shapes) and style properties. Style properties are not assigned during initialization. They are not meant to be transformed, only to be drawn. Sketches have no methods, only data. They do not check anything, they just store data. They are populated during sketch creation. You should make sure the data is correct before creating a sketch.
Attributes:
- vertices (list, optional): The vertices of the shape. Defaults to None.
- xform_matrix (ndarray, optional): The transformation matrix. Defaults to None.
195@dataclass 196class BezierSketch: 197 """BezierSketch is a dataclass for creating a bezier sketch object. 198 199 Attributes: 200 control_points (list): The control points of the bezier curve. 201 xform_matrix (ndarray, optional): The transformation matrix. Defaults to None. 202 mode (CurveMode, optional): The mode of the curve. Defaults to CurveMode.OPEN. 203 """ 204 205 control_points: list 206 xform_matrix: ndarray = None 207 mode: CurveMode = CurveMode.OPEN 208 209 def __post_init__(self): 210 """Initialize the BezierSketch object.""" 211 self.type = Types.SKETCH 212 self.subtype = Types.BEZIER_SKETCH 213 214 if self.xform_matrix is None: 215 self.xform_matrix = identity_matrix() 216 control_points = self.control_points 217 else: 218 control_points = homogenize(self.control_points) 219 control_points = control_points @ self.xform_matrix 220 self.control_points = [tuple(x) for x in control_points[:, :3]] 221 self.closed = False
BezierSketch is a dataclass for creating a bezier sketch object.
Attributes:
- control_points (list): The control points of the bezier curve.
- xform_matrix (ndarray, optional): The transformation matrix. Defaults to None.
- mode (CurveMode, optional): The mode of the curve. Defaults to CurveMode.OPEN.
224@dataclass 225class ArcSketch: 226 """ArcSketch is a dataclass for creating an arc sketch object. 227 228 Attributes: 229 vertices (list, optional): The vertices of the shape. Defaults to None. 230 xform_matrix (ndarray, optional): The transformation matrix. Defaults to None. 231 mode (CurveMode, optional): The mode of the curve. Defaults to CurveMode.OPEN. 232 """ 233 234 vertices: list = None 235 xform_matrix: ndarray = None 236 mode: CurveMode = CurveMode.OPEN 237 238 def __post_init__(self): 239 """Initialize the ArcSketch object.""" 240 if self.xform_matrix is None: 241 self.xform_matrix = identity_matrix() 242 vertices = self.vertices 243 else: 244 vertices = homogenize(self.vertices) 245 vertices = vertices @ self.xform_matrix 246 self.vertices = [tuple(x) for x in vertices[:, :2]] 247 248 self.type = Types.SKETCH 249 self.subtype = Types.ARC_SKETCH 250 self.closed = self.mode != CurveMode.OPEN
ArcSketch is a dataclass for creating an arc sketch object.
Attributes:
- vertices (list, optional): The vertices of the shape. Defaults to None.
- xform_matrix (ndarray, optional): The transformation matrix. Defaults to None.
- mode (CurveMode, optional): The mode of the curve. Defaults to CurveMode.OPEN.
255@dataclass 256class BatchSketch: 257 """BatchSketch is a dataclass for creating a batch sketch object. 258 259 Attributes: 260 sketches (List[Types.SKETCH]): The list of sketches. 261 xform_matrix (ndarray, optional): The transformation matrix. Defaults to None. 262 """ 263 264 sketches: List[Types.SKETCH] 265 xform_matrix: ndarray = None 266 267 def __post_init__(self): 268 """Initialize the BatchSketch object.""" 269 self.type = Types.SKETCH 270 self.subtype = Types.BATCH_SKETCH 271 self.sketches = self.sketches
BatchSketch is a dataclass for creating a batch sketch object.
Attributes:
- sketches (List[Types.SKETCH]): The list of sketches.
- xform_matrix (ndarray, optional): The transformation matrix. Defaults to None.
274@dataclass 275class PathSketch: 276 """PathSketch is a dataclass for creating a path sketch object. 277 278 Attributes: 279 sketches (List[Types.SKETCH]): The list of sketches. 280 xform_matrix (ndarray, optional): The transformation matrix. Defaults to None. 281 """ 282 283 sketches: List[Types.SKETCH] 284 xform_matrix: ndarray = None 285 286 def __post_init__(self): 287 """Initialize the PathSketch object.""" 288 self.type = Types.SKETCH 289 self.subtype = Types.PATH_SKETCH 290 if self.xform_matrix is None: 291 self.xform_matrix = identity_matrix()
PathSketch is a dataclass for creating a path sketch object.
Attributes:
- sketches (List[Types.SKETCH]): The list of sketches.
- xform_matrix (ndarray, optional): The transformation matrix. Defaults to None.
294@dataclass 295class LaceSketch: 296 """LaceSketch is a dataclass for creating a lace sketch object. 297 298 Attributes: 299 fragment_sketches (List[ShapeSketch]): The list of fragment sketches. 300 plait_sketches (List[ShapeSketch]): The list of plait sketches. 301 xform_matrix (ndarray, optional): The transformation matrix. Defaults to None. 302 """ 303 304 fragment_sketches: List[ShapeSketch] 305 plait_sketches: List[ShapeSketch] 306 xform_matrix: ndarray = None 307 308 def __post_init__(self): 309 """Initialize the LaceSketch object.""" 310 self.type = Types.SKETCH 311 self.subtype = Types.LACESKETCH 312 if self.xform_matrix is None: 313 self.xform_matrix = identity_matrix()
LaceSketch is a dataclass for creating a lace sketch object.
Attributes:
- fragment_sketches (List[ShapeSketch]): The list of fragment sketches.
- plait_sketches (List[ShapeSketch]): The list of plait sketches.
- xform_matrix (ndarray, optional): The transformation matrix. Defaults to None.
316@dataclass 317class FrameSketch: 318 """FrameSketch is a dataclass for creating a frame sketch object. 319 320 Attributes: 321 frame_shape (FrameShape, optional): The shape of the frame. Defaults to "rectangle". 322 line_width (float, optional): The width of the line. Defaults to 1. 323 line_dash_array (list, optional): The dash array for the line. Defaults to None. 324 line_color (Color, optional): The color of the line. Defaults to colors.black. 325 back_color (Color, optional): The background color. Defaults to colors.white. 326 fill (bool, optional): Whether to fill the frame. Defaults to False. 327 stroke (bool, optional): Whether to stroke the frame. Defaults to True. 328 double (bool, optional): Whether to draw a double line. Defaults to False. 329 double_distance (float, optional): The distance between double lines. Defaults to 2. 330 inner_sep (float, optional): The inner separation. Defaults to 10. 331 outer_sep (float, optional): The outer separation. Defaults to 10. 332 smooth (bool, optional): Whether to smooth the frame. Defaults to False. 333 rounded_corners (bool, optional): Whether to round the corners. Defaults to False. 334 fillet_radius (float, optional): The radius of the fillet. Defaults to 10. 335 draw_fillets (bool, optional): Whether to draw fillets. Defaults to False. 336 blend_mode (str, optional): The blend mode. Defaults to None. 337 gradient (str, optional): The gradient. Defaults to None. 338 pattern (str, optional): The pattern. Defaults to None. 339 visible (bool, optional): Whether the frame is visible. Defaults to True. 340 min_width (float, optional): The minimum width. Defaults to 0. 341 min_height (float, optional): The minimum height. Defaults to 0. 342 min_radius (float, optional): The minimum radius. Defaults to 0. 343 """ 344 345 frame_shape: FrameShape = ( 346 "rectangle" # default value cannot be FrameShape.RECTANGLE! 347 ) 348 line_width: float = 1 349 line_dash_array: list = None 350 line_color: Color = colors.black 351 back_color: Color = colors.white 352 fill: bool = False 353 stroke: bool = True 354 double: bool = False 355 double_distance: float = 2 356 inner_sep: float = 10 357 outer_sep: float = 10 358 smooth: bool = False 359 rounded_corners: bool = False 360 fillet_radius: float = 10 361 draw_fillets: bool = False 362 blend_mode: str = None 363 gradient: str = None 364 pattern: str = None 365 visible: bool = True 366 min_width: float = 0 367 min_height: float = 0 368 min_radius: float = 0 369 370 def __post_init__(self): 371 """Initialize the FrameSketch object.""" 372 self.type = Types.SKETCH 373 self.subtype = Types.FRAME_SKETCH 374 common_properties(self)
FrameSketch is a dataclass for creating a frame sketch object.
Attributes:
- frame_shape (FrameShape, optional): The shape of the frame. Defaults to "rectangle".
- line_width (float, optional): The width of the line. Defaults to 1.
- line_dash_array (list, optional): The dash array for the line. Defaults to None.
- line_color (Color, optional): The color of the line. Defaults to colors.black.
- back_color (Color, optional): The background color. Defaults to colors.white.
- fill (bool, optional): Whether to fill the frame. Defaults to False.
- stroke (bool, optional): Whether to stroke the frame. Defaults to True.
- double (bool, optional): Whether to draw a double line. Defaults to False.
- double_distance (float, optional): The distance between double lines. Defaults to 2.
- inner_sep (float, optional): The inner separation. Defaults to 10.
- outer_sep (float, optional): The outer separation. Defaults to 10.
- smooth (bool, optional): Whether to smooth the frame. Defaults to False.
- rounded_corners (bool, optional): Whether to round the corners. Defaults to False.
- fillet_radius (float, optional): The radius of the fillet. Defaults to 10.
- draw_fillets (bool, optional): Whether to draw fillets. Defaults to False.
- blend_mode (str, optional): The blend mode. Defaults to None.
- gradient (str, optional): The gradient. Defaults to None.
- pattern (str, optional): The pattern. Defaults to None.
- visible (bool, optional): Whether the frame is visible. Defaults to True.
- min_width (float, optional): The minimum width. Defaults to 0.
- min_height (float, optional): The minimum height. Defaults to 0.
- min_radius (float, optional): The minimum radius. Defaults to 0.
377@dataclass 378class TagSketch: 379 """TagSketch is a dataclass for creating a tag sketch object. 380 381 Attributes: 382 text (str, optional): The text of the tag. Defaults to None. 383 pos (Point, optional): The position of the tag. Defaults to None. 384 anchor (Anchor, optional): The anchor of the tag. Defaults to None. 385 font_family (str, optional): The font family. Defaults to None. 386 font_size (float, optional): The font size. Defaults to None. 387 minimum_width (float, optional): The minimum width. Defaults to None. 388 xform_matrix (ndarray, optional): The transformation matrix. Defaults to None. 389 """ 390 391 text: str = None 392 pos: Point = None 393 anchor: Anchor = None 394 font_family: str = None 395 font_size: float = None 396 minimum_width: float = None 397 xform_matrix: ndarray = None 398 399 def __post_init__(self): 400 """Initialize the TagSketch object.""" 401 self.type = Types.SKETCH 402 self.subtype = Types.TAG_SKETCH 403 if self.xform_matrix is None: 404 self.xform_matrix = identity_matrix() 405 pos = self.pos 406 else: 407 pos = homogenize([self.pos]) 408 pos = (pos @ self.xform_matrix).tolist()[0][:2] 409 self.pos = pos
TagSketch is a dataclass for creating a tag sketch object.
Attributes:
- text (str, optional): The text of the tag. Defaults to None.
- pos (Point, optional): The position of the tag. Defaults to None.
- anchor (Anchor, optional): The anchor of the tag. Defaults to None.
- font_family (str, optional): The font family. Defaults to None.
- font_size (float, optional): The font size. Defaults to None.
- minimum_width (float, optional): The minimum width. Defaults to None.
- xform_matrix (ndarray, optional): The transformation matrix. Defaults to None.
412@dataclass 413class RectSketch: 414 """RectSketch is a dataclass for creating a rectangle sketch object. 415 416 Attributes: 417 pos (Point): The position of the rectangle. 418 width (float): The width of the rectangle. 419 height (float): The height of the rectangle. 420 xform_matrix (ndarray, optional): The transformation matrix. Defaults to None. 421 """ 422 423 pos: Point 424 width: float 425 height: float 426 xform_matrix: ndarray = None 427 428 def __post_init__(self): 429 """Initialize the RectSketch object. 430 431 Args: 432 pos (Point): The position of the rectangle. 433 width (float): The width of the rectangle. 434 height (float): The height of the rectangle. 435 xform_matrix (ndarray, optional): The transformation matrix. Defaults to None. 436 """ 437 self.type = Types.SKETCH 438 self.subtype = Types.RECT_SKETCH 439 if self.xform_matrix is None: 440 self.xform_matrix = identity_matrix() 441 pos = self.pos 442 else: 443 pos = homogenize([self.pos]) 444 pos = (pos @ self.xform_matrix).tolist()[0][:2] 445 self.pos = pos 446 h2 = self.height / 2 447 w2 = self.width / 2 448 self.vertices = [ 449 (pos[0] - w2, pos[1] - h2), 450 (pos[0] + w2, pos[1] - h2), 451 (pos[0] + w2, pos[1] + h2), 452 (pos[0] - w2, pos[1] + h2), 453 ] 454 self.closed = True
RectSketch is a dataclass for creating a rectangle sketch object.
Attributes:
- pos (Point): The position of the rectangle.
- width (float): The width of the rectangle.
- height (float): The height of the rectangle.
- xform_matrix (ndarray, optional): The transformation matrix. Defaults to None.