sketchingpy.state_struct
Data structures describing sketch state including styling and transformation matrix.
License:
BSD
1"""Data structures describing sketch state including styling and transformation matrix. 2 3License: 4 BSD 5""" 6 7import sketchingpy.const 8 9 10class TextAlign: 11 """Structure describing text alignment / anchors, possibly using renderer-native values.""" 12 13 def __init__(self, horizontal_align, vertical_align): 14 """Create a new text alignment structure. 15 16 Args: 17 horizontal_align: Description of text horizontal alignment. 18 vertical_align: Description of text vertical alignment. 19 """ 20 self._horizontal_align = horizontal_align 21 self._vertical_align = vertical_align 22 23 def get_horizontal_align(self): 24 """Get the horizontal anchor for text. 25 26 Returns: 27 Description of text horizontal alignment which may be renderer-specific. 28 """ 29 return self._horizontal_align 30 31 def get_vertical_align(self): 32 """Get the vertical anchor for text. 33 34 Returns: 35 Description of text vertical alignment which may be renderer-specific. 36 """ 37 return self._vertical_align 38 39 40class Font: 41 """Structure describing a font using renderer-native values.""" 42 43 def __init__(self, identifier, size): 44 """Create a record describing a font. 45 46 Args: 47 identifier: Name of the font which may be a filename. 48 size: The size of the text in px. 49 """ 50 self._identifier = identifier 51 self._size = size 52 53 def get_identifier(self): 54 """Get the name of this font. 55 56 Returns: 57 Name of the font which may be a filename. 58 """ 59 return self._identifier 60 61 def get_size(self): 62 """Get the desired size of text. 63 64 Returns: 65 The size of the text in px. 66 """ 67 return self._size 68 69 70class SketchStateMachine: 71 """Abstract base class for sketch state.""" 72 73 def __init__(self): 74 """Create a new state machine.""" 75 self._fill_enabled = True 76 self._fill_str = '#F0F0F0' 77 self._stroke_enabled = True 78 self._stroke_str = '#333333' 79 self._stroke_weight = 1 80 81 self._angle_mode = 'radians' 82 self._angle_mode_enum = sketchingpy.const.ANGLE_MODES[self._angle_mode] 83 84 self._arc_mode = 'radius' 85 self._arc_mode_enum = sketchingpy.const.SHAPE_MODES[self._arc_mode] 86 87 self._ellipse_mode = 'radius' 88 self._ellipse_mode_enum = sketchingpy.const.SHAPE_MODES[self._ellipse_mode] 89 90 self._rect_mode = 'corner' 91 self._rect_mode_enum = sketchingpy.const.SHAPE_MODES[self._rect_mode] 92 93 self._text_font = None 94 self._text_align = TextAlign('left', 'baseline') 95 self._text_align_enum = TextAlign( 96 sketchingpy.const.ALIGN_OPTIONS['left'], 97 sketchingpy.const.ALIGN_OPTIONS['baseline'] 98 ) 99 100 self._image_mode = 'corner' 101 self._image_mode_enum = sketchingpy.const.SHAPE_MODES[self._image_mode] 102 103 ########## 104 # Colors # 105 ########## 106 107 def set_fill(self, fill: str): 108 """Set the fill color. 109 110 Set the color to use for filling shapes and figures. 111 112 Args: 113 fill: Name of the color or a hex code. 114 """ 115 self._fill_enabled = True 116 self._fill_str = fill 117 118 def get_fill(self) -> str: 119 """Get the current fill color. 120 121 Returns: 122 Name of the color or a hex code. Undefined if get_fill_enabled() is False. 123 """ 124 return self._fill_str 125 126 def get_fill_native(self): 127 """Get the renderer-native version of the fill color. 128 129 Returns: 130 Renderer-specific value. Undefined if get_fill_enabled() is False. 131 """ 132 return self._fill_str 133 134 def get_fill_enabled(self) -> bool: 135 """Determine if fill is enabled. 136 137 Returns: 138 True if fill enabled and false if transparent (drawing outlines). 139 """ 140 return self._fill_enabled 141 142 def clear_fill(self): 143 """Indicate that the fill should be transparent / draw outlines.""" 144 self._fill_enabled = False 145 146 def set_stroke(self, stroke: str): 147 """Set the stroke color. 148 149 Set the color to use for drawing outlines for shapes and figures as well as lines. 150 151 Args: 152 stroke: Name of the color or a hex code. 153 """ 154 self._stroke_enabled = True 155 self._stroke_str = stroke 156 157 def get_stroke(self) -> str: 158 """Get the current stroke color. 159 160 Returns: 161 Name of the color or a hex code. Undefined if get_stroke_enabled() is False. 162 """ 163 return self._stroke_str 164 165 def get_stroke_native(self): 166 """Get the renderer-native version of the stroke color. 167 168 Returns: 169 Renderer-specific value. Undefined if get_stroke_enabled() is False. 170 """ 171 return self._stroke_str 172 173 def get_stroke_enabled(self) -> bool: 174 """Determine if outline (stroke) should be drawn. 175 176 Returns: 177 True if it should be drawn (non-zero stroke weight) and False otherwise. 178 """ 179 return self._stroke_enabled and self._stroke_weight > 0 180 181 def clear_stroke(self): 182 """Indicate stroke should not be drawn.""" 183 self._stroke_enabled = False 184 185 ########### 186 # Drawing # 187 ########### 188 189 def set_arc_mode(self, mode: str): 190 """Specify how Sketchingpy should interpret the position and size arguments for arcs. 191 192 Args: 193 mode: String describing the mode to use. 194 """ 195 if mode not in sketchingpy.const.SHAPE_MODES: 196 raise RuntimeError('Unknown arc mode: ' + mode) 197 198 self._arc_mode = mode 199 self._arc_mode_enum = sketchingpy.const.SHAPE_MODES[self._arc_mode] 200 201 def get_arc_mode(self) -> str: 202 """Get mode describing how to interpret arc parameters. 203 204 Returns: 205 String describing the mode to use. 206 """ 207 return self._arc_mode 208 209 def get_arc_mode_native(self): 210 """Get mode describing how to interpret arc parameters. 211 212 Returns: 213 Renderer-specific value. 214 """ 215 return self._arc_mode_enum 216 217 def set_ellipse_mode(self, mode: str): 218 """Specify how Sketchingpy should interpret the position and size arguments. 219 220 Args: 221 mode: String describing the mode to use. 222 """ 223 if mode not in sketchingpy.const.SHAPE_MODES: 224 raise RuntimeError('Unknown ellipse mode: ' + mode) 225 226 self._ellipse_mode = mode 227 self._ellipse_mode_enum = sketchingpy.const.SHAPE_MODES[self._ellipse_mode] 228 229 def get_ellipse_mode(self) -> str: 230 """Get the mode describing how to interpret parameters for ellipsees. 231 232 Returns: 233 String describing the mode to use. 234 """ 235 return self._ellipse_mode 236 237 def get_ellipse_mode_native(self): 238 """Get the mode describing how to interpret parameters for ellipses. 239 240 Returns: 241 Renderer specific value. 242 """ 243 return self._ellipse_mode_enum 244 245 def set_rect_mode(self, mode: str): 246 """Specify how Sketchingpy should interpret the position and size arguments. 247 248 Args: 249 mode: String describing the mode to use. 250 """ 251 if mode not in sketchingpy.const.SHAPE_MODES: 252 raise RuntimeError('Unknown rect mode: ' + mode) 253 254 self._rect_mode = mode 255 self._rect_mode_enum = sketchingpy.const.SHAPE_MODES[self._rect_mode] 256 257 def get_rect_mode(self) -> str: 258 """Get the mode describing how to interpret parameters for rectangles. 259 260 Returns: 261 String describing the mode to use. 262 """ 263 return self._rect_mode 264 265 def get_rect_mode_native(self): 266 """Get the mode describing how to interpret parameters for rectangles. 267 268 Returns: 269 Renderer-specific value. 270 """ 271 return self._rect_mode_enum 272 273 def set_stroke_weight(self, stroke_weight: float): 274 """Set the stroke size. 275 276 Args: 277 stroke_weight: Number of pixels for the stroke weight. 278 """ 279 if stroke_weight < 0: 280 raise RuntimeError('Stroke weight must be zero or positive.') 281 282 self._stroke_weight = stroke_weight 283 284 def get_stroke_weight(self) -> float: 285 """Get the size of the stroke. 286 287 Returns: 288 Number of pixels for the stroke weight. 289 """ 290 if not self._stroke_enabled: 291 return 0 292 293 return self._stroke_weight 294 295 def get_stroke_weight_native(self): 296 """Get the size of the stroke. 297 298 Returns: 299 Renderer-specific value 300 """ 301 return self._stroke_weight 302 303 def set_text_font(self, font: Font): 304 """Set the type and size for text drawing. 305 306 Args: 307 font: Description of the font and font size to use. 308 """ 309 self._text_font = font 310 311 def get_text_font(self) -> Font: 312 """Get the type and size for text drawing. 313 314 Returns: 315 Description of the font and font size to use. 316 """ 317 if self._text_font is None: 318 raise RuntimeError('Font not yet set.') 319 320 return self._text_font 321 322 def get_text_font_native(self): 323 """Get the type and size for text drawing. 324 325 Returns: 326 Renderer-specific value. 327 """ 328 return self.get_text_font() 329 330 def set_text_align(self, text_align: TextAlign): 331 """Indicate the alignment to use when drawing text. 332 333 Args: 334 text_align: Structure describing horizontal and vertical text alignment. 335 """ 336 def check_align(name): 337 if name not in sketchingpy.const.ALIGN_OPTIONS: 338 raise RuntimeError('Unknown align: %d' % name) 339 340 check_align(text_align.get_horizontal_align()) 341 check_align(text_align.get_vertical_align()) 342 343 self._text_align = text_align 344 self._text_align_enum = TextAlign( 345 sketchingpy.const.ALIGN_OPTIONS[self._text_align.get_horizontal_align()], 346 sketchingpy.const.ALIGN_OPTIONS[self._text_align.get_vertical_align()] 347 ) 348 349 def get_text_align(self) -> TextAlign: 350 """Get the alignment to use when drawing text. 351 352 Returns: 353 Structure describing horizontal and vertical text alignment. 354 """ 355 return self._text_align 356 357 def get_text_align_native(self): 358 """Get the alignment to use when drawing text. 359 360 Returns: 361 Renderer-specific value. 362 """ 363 return self._text_align_enum 364 365 ######### 366 # Image # 367 ######### 368 369 def set_image_mode(self, mode: str): 370 """Specify how Sketchingpy should place images. 371 372 Args: 373 mode: String describing the mode to use. 374 """ 375 if mode not in ['center', 'corner']: 376 raise RuntimeError('Unknown image mode: ' + mode) 377 378 self._image_mode = mode 379 self._image_mode_enum = sketchingpy.const.SHAPE_MODES[self._image_mode] 380 381 def get_image_mode(self) -> str: 382 """Get how Sketchingpy should place images. 383 384 Returns: 385 String describing the mode to use. 386 """ 387 return self._image_mode 388 389 def get_image_mode_native(self): 390 """Get how Sketchingpy should place images. 391 392 Returns: 393 Renderer-specific value. 394 """ 395 return self._image_mode_enum 396 397 ################ 398 # Other Params # 399 ################ 400 401 def set_angle_mode(self, mode: str): 402 """Indicate how angles should be provided to sketchingpy. 403 404 Args: 405 mode: The units (either 'degrees' or 'radians') in which to supply angles. 406 """ 407 if mode not in sketchingpy.const.ANGLE_MODES: 408 raise RuntimeError('Unknown angle mode: ' + mode) 409 410 self._angle_mode = mode 411 self._angle_mode_enum = sketchingpy.const.ANGLE_MODES[self._angle_mode] 412 413 def get_angle_mode(self) -> str: 414 """Get how angles are to be provided to sketchingpy. 415 416 Return: 417 The units (either 'degrees' or 'radians') in which angles are to be supplied. 418 """ 419 return self._angle_mode 420 421 def get_angle_mode_native(self): 422 """Get how angles are to be provided to sketchingpy. 423 424 Return: 425 Renderer-specific value. 426 """ 427 return self._angle_mode_enum
11class TextAlign: 12 """Structure describing text alignment / anchors, possibly using renderer-native values.""" 13 14 def __init__(self, horizontal_align, vertical_align): 15 """Create a new text alignment structure. 16 17 Args: 18 horizontal_align: Description of text horizontal alignment. 19 vertical_align: Description of text vertical alignment. 20 """ 21 self._horizontal_align = horizontal_align 22 self._vertical_align = vertical_align 23 24 def get_horizontal_align(self): 25 """Get the horizontal anchor for text. 26 27 Returns: 28 Description of text horizontal alignment which may be renderer-specific. 29 """ 30 return self._horizontal_align 31 32 def get_vertical_align(self): 33 """Get the vertical anchor for text. 34 35 Returns: 36 Description of text vertical alignment which may be renderer-specific. 37 """ 38 return self._vertical_align
Structure describing text alignment / anchors, possibly using renderer-native values.
14 def __init__(self, horizontal_align, vertical_align): 15 """Create a new text alignment structure. 16 17 Args: 18 horizontal_align: Description of text horizontal alignment. 19 vertical_align: Description of text vertical alignment. 20 """ 21 self._horizontal_align = horizontal_align 22 self._vertical_align = vertical_align
Create a new text alignment structure.
Arguments:
- horizontal_align: Description of text horizontal alignment.
- vertical_align: Description of text vertical alignment.
24 def get_horizontal_align(self): 25 """Get the horizontal anchor for text. 26 27 Returns: 28 Description of text horizontal alignment which may be renderer-specific. 29 """ 30 return self._horizontal_align
Get the horizontal anchor for text.
Returns:
Description of text horizontal alignment which may be renderer-specific.
32 def get_vertical_align(self): 33 """Get the vertical anchor for text. 34 35 Returns: 36 Description of text vertical alignment which may be renderer-specific. 37 """ 38 return self._vertical_align
Get the vertical anchor for text.
Returns:
Description of text vertical alignment which may be renderer-specific.
41class Font: 42 """Structure describing a font using renderer-native values.""" 43 44 def __init__(self, identifier, size): 45 """Create a record describing a font. 46 47 Args: 48 identifier: Name of the font which may be a filename. 49 size: The size of the text in px. 50 """ 51 self._identifier = identifier 52 self._size = size 53 54 def get_identifier(self): 55 """Get the name of this font. 56 57 Returns: 58 Name of the font which may be a filename. 59 """ 60 return self._identifier 61 62 def get_size(self): 63 """Get the desired size of text. 64 65 Returns: 66 The size of the text in px. 67 """ 68 return self._size
Structure describing a font using renderer-native values.
44 def __init__(self, identifier, size): 45 """Create a record describing a font. 46 47 Args: 48 identifier: Name of the font which may be a filename. 49 size: The size of the text in px. 50 """ 51 self._identifier = identifier 52 self._size = size
Create a record describing a font.
Arguments:
- identifier: Name of the font which may be a filename.
- size: The size of the text in px.
71class SketchStateMachine: 72 """Abstract base class for sketch state.""" 73 74 def __init__(self): 75 """Create a new state machine.""" 76 self._fill_enabled = True 77 self._fill_str = '#F0F0F0' 78 self._stroke_enabled = True 79 self._stroke_str = '#333333' 80 self._stroke_weight = 1 81 82 self._angle_mode = 'radians' 83 self._angle_mode_enum = sketchingpy.const.ANGLE_MODES[self._angle_mode] 84 85 self._arc_mode = 'radius' 86 self._arc_mode_enum = sketchingpy.const.SHAPE_MODES[self._arc_mode] 87 88 self._ellipse_mode = 'radius' 89 self._ellipse_mode_enum = sketchingpy.const.SHAPE_MODES[self._ellipse_mode] 90 91 self._rect_mode = 'corner' 92 self._rect_mode_enum = sketchingpy.const.SHAPE_MODES[self._rect_mode] 93 94 self._text_font = None 95 self._text_align = TextAlign('left', 'baseline') 96 self._text_align_enum = TextAlign( 97 sketchingpy.const.ALIGN_OPTIONS['left'], 98 sketchingpy.const.ALIGN_OPTIONS['baseline'] 99 ) 100 101 self._image_mode = 'corner' 102 self._image_mode_enum = sketchingpy.const.SHAPE_MODES[self._image_mode] 103 104 ########## 105 # Colors # 106 ########## 107 108 def set_fill(self, fill: str): 109 """Set the fill color. 110 111 Set the color to use for filling shapes and figures. 112 113 Args: 114 fill: Name of the color or a hex code. 115 """ 116 self._fill_enabled = True 117 self._fill_str = fill 118 119 def get_fill(self) -> str: 120 """Get the current fill color. 121 122 Returns: 123 Name of the color or a hex code. Undefined if get_fill_enabled() is False. 124 """ 125 return self._fill_str 126 127 def get_fill_native(self): 128 """Get the renderer-native version of the fill color. 129 130 Returns: 131 Renderer-specific value. Undefined if get_fill_enabled() is False. 132 """ 133 return self._fill_str 134 135 def get_fill_enabled(self) -> bool: 136 """Determine if fill is enabled. 137 138 Returns: 139 True if fill enabled and false if transparent (drawing outlines). 140 """ 141 return self._fill_enabled 142 143 def clear_fill(self): 144 """Indicate that the fill should be transparent / draw outlines.""" 145 self._fill_enabled = False 146 147 def set_stroke(self, stroke: str): 148 """Set the stroke color. 149 150 Set the color to use for drawing outlines for shapes and figures as well as lines. 151 152 Args: 153 stroke: Name of the color or a hex code. 154 """ 155 self._stroke_enabled = True 156 self._stroke_str = stroke 157 158 def get_stroke(self) -> str: 159 """Get the current stroke color. 160 161 Returns: 162 Name of the color or a hex code. Undefined if get_stroke_enabled() is False. 163 """ 164 return self._stroke_str 165 166 def get_stroke_native(self): 167 """Get the renderer-native version of the stroke color. 168 169 Returns: 170 Renderer-specific value. Undefined if get_stroke_enabled() is False. 171 """ 172 return self._stroke_str 173 174 def get_stroke_enabled(self) -> bool: 175 """Determine if outline (stroke) should be drawn. 176 177 Returns: 178 True if it should be drawn (non-zero stroke weight) and False otherwise. 179 """ 180 return self._stroke_enabled and self._stroke_weight > 0 181 182 def clear_stroke(self): 183 """Indicate stroke should not be drawn.""" 184 self._stroke_enabled = False 185 186 ########### 187 # Drawing # 188 ########### 189 190 def set_arc_mode(self, mode: str): 191 """Specify how Sketchingpy should interpret the position and size arguments for arcs. 192 193 Args: 194 mode: String describing the mode to use. 195 """ 196 if mode not in sketchingpy.const.SHAPE_MODES: 197 raise RuntimeError('Unknown arc mode: ' + mode) 198 199 self._arc_mode = mode 200 self._arc_mode_enum = sketchingpy.const.SHAPE_MODES[self._arc_mode] 201 202 def get_arc_mode(self) -> str: 203 """Get mode describing how to interpret arc parameters. 204 205 Returns: 206 String describing the mode to use. 207 """ 208 return self._arc_mode 209 210 def get_arc_mode_native(self): 211 """Get mode describing how to interpret arc parameters. 212 213 Returns: 214 Renderer-specific value. 215 """ 216 return self._arc_mode_enum 217 218 def set_ellipse_mode(self, mode: str): 219 """Specify how Sketchingpy should interpret the position and size arguments. 220 221 Args: 222 mode: String describing the mode to use. 223 """ 224 if mode not in sketchingpy.const.SHAPE_MODES: 225 raise RuntimeError('Unknown ellipse mode: ' + mode) 226 227 self._ellipse_mode = mode 228 self._ellipse_mode_enum = sketchingpy.const.SHAPE_MODES[self._ellipse_mode] 229 230 def get_ellipse_mode(self) -> str: 231 """Get the mode describing how to interpret parameters for ellipsees. 232 233 Returns: 234 String describing the mode to use. 235 """ 236 return self._ellipse_mode 237 238 def get_ellipse_mode_native(self): 239 """Get the mode describing how to interpret parameters for ellipses. 240 241 Returns: 242 Renderer specific value. 243 """ 244 return self._ellipse_mode_enum 245 246 def set_rect_mode(self, mode: str): 247 """Specify how Sketchingpy should interpret the position and size arguments. 248 249 Args: 250 mode: String describing the mode to use. 251 """ 252 if mode not in sketchingpy.const.SHAPE_MODES: 253 raise RuntimeError('Unknown rect mode: ' + mode) 254 255 self._rect_mode = mode 256 self._rect_mode_enum = sketchingpy.const.SHAPE_MODES[self._rect_mode] 257 258 def get_rect_mode(self) -> str: 259 """Get the mode describing how to interpret parameters for rectangles. 260 261 Returns: 262 String describing the mode to use. 263 """ 264 return self._rect_mode 265 266 def get_rect_mode_native(self): 267 """Get the mode describing how to interpret parameters for rectangles. 268 269 Returns: 270 Renderer-specific value. 271 """ 272 return self._rect_mode_enum 273 274 def set_stroke_weight(self, stroke_weight: float): 275 """Set the stroke size. 276 277 Args: 278 stroke_weight: Number of pixels for the stroke weight. 279 """ 280 if stroke_weight < 0: 281 raise RuntimeError('Stroke weight must be zero or positive.') 282 283 self._stroke_weight = stroke_weight 284 285 def get_stroke_weight(self) -> float: 286 """Get the size of the stroke. 287 288 Returns: 289 Number of pixels for the stroke weight. 290 """ 291 if not self._stroke_enabled: 292 return 0 293 294 return self._stroke_weight 295 296 def get_stroke_weight_native(self): 297 """Get the size of the stroke. 298 299 Returns: 300 Renderer-specific value 301 """ 302 return self._stroke_weight 303 304 def set_text_font(self, font: Font): 305 """Set the type and size for text drawing. 306 307 Args: 308 font: Description of the font and font size to use. 309 """ 310 self._text_font = font 311 312 def get_text_font(self) -> Font: 313 """Get the type and size for text drawing. 314 315 Returns: 316 Description of the font and font size to use. 317 """ 318 if self._text_font is None: 319 raise RuntimeError('Font not yet set.') 320 321 return self._text_font 322 323 def get_text_font_native(self): 324 """Get the type and size for text drawing. 325 326 Returns: 327 Renderer-specific value. 328 """ 329 return self.get_text_font() 330 331 def set_text_align(self, text_align: TextAlign): 332 """Indicate the alignment to use when drawing text. 333 334 Args: 335 text_align: Structure describing horizontal and vertical text alignment. 336 """ 337 def check_align(name): 338 if name not in sketchingpy.const.ALIGN_OPTIONS: 339 raise RuntimeError('Unknown align: %d' % name) 340 341 check_align(text_align.get_horizontal_align()) 342 check_align(text_align.get_vertical_align()) 343 344 self._text_align = text_align 345 self._text_align_enum = TextAlign( 346 sketchingpy.const.ALIGN_OPTIONS[self._text_align.get_horizontal_align()], 347 sketchingpy.const.ALIGN_OPTIONS[self._text_align.get_vertical_align()] 348 ) 349 350 def get_text_align(self) -> TextAlign: 351 """Get the alignment to use when drawing text. 352 353 Returns: 354 Structure describing horizontal and vertical text alignment. 355 """ 356 return self._text_align 357 358 def get_text_align_native(self): 359 """Get the alignment to use when drawing text. 360 361 Returns: 362 Renderer-specific value. 363 """ 364 return self._text_align_enum 365 366 ######### 367 # Image # 368 ######### 369 370 def set_image_mode(self, mode: str): 371 """Specify how Sketchingpy should place images. 372 373 Args: 374 mode: String describing the mode to use. 375 """ 376 if mode not in ['center', 'corner']: 377 raise RuntimeError('Unknown image mode: ' + mode) 378 379 self._image_mode = mode 380 self._image_mode_enum = sketchingpy.const.SHAPE_MODES[self._image_mode] 381 382 def get_image_mode(self) -> str: 383 """Get how Sketchingpy should place images. 384 385 Returns: 386 String describing the mode to use. 387 """ 388 return self._image_mode 389 390 def get_image_mode_native(self): 391 """Get how Sketchingpy should place images. 392 393 Returns: 394 Renderer-specific value. 395 """ 396 return self._image_mode_enum 397 398 ################ 399 # Other Params # 400 ################ 401 402 def set_angle_mode(self, mode: str): 403 """Indicate how angles should be provided to sketchingpy. 404 405 Args: 406 mode: The units (either 'degrees' or 'radians') in which to supply angles. 407 """ 408 if mode not in sketchingpy.const.ANGLE_MODES: 409 raise RuntimeError('Unknown angle mode: ' + mode) 410 411 self._angle_mode = mode 412 self._angle_mode_enum = sketchingpy.const.ANGLE_MODES[self._angle_mode] 413 414 def get_angle_mode(self) -> str: 415 """Get how angles are to be provided to sketchingpy. 416 417 Return: 418 The units (either 'degrees' or 'radians') in which angles are to be supplied. 419 """ 420 return self._angle_mode 421 422 def get_angle_mode_native(self): 423 """Get how angles are to be provided to sketchingpy. 424 425 Return: 426 Renderer-specific value. 427 """ 428 return self._angle_mode_enum
Abstract base class for sketch state.
74 def __init__(self): 75 """Create a new state machine.""" 76 self._fill_enabled = True 77 self._fill_str = '#F0F0F0' 78 self._stroke_enabled = True 79 self._stroke_str = '#333333' 80 self._stroke_weight = 1 81 82 self._angle_mode = 'radians' 83 self._angle_mode_enum = sketchingpy.const.ANGLE_MODES[self._angle_mode] 84 85 self._arc_mode = 'radius' 86 self._arc_mode_enum = sketchingpy.const.SHAPE_MODES[self._arc_mode] 87 88 self._ellipse_mode = 'radius' 89 self._ellipse_mode_enum = sketchingpy.const.SHAPE_MODES[self._ellipse_mode] 90 91 self._rect_mode = 'corner' 92 self._rect_mode_enum = sketchingpy.const.SHAPE_MODES[self._rect_mode] 93 94 self._text_font = None 95 self._text_align = TextAlign('left', 'baseline') 96 self._text_align_enum = TextAlign( 97 sketchingpy.const.ALIGN_OPTIONS['left'], 98 sketchingpy.const.ALIGN_OPTIONS['baseline'] 99 ) 100 101 self._image_mode = 'corner' 102 self._image_mode_enum = sketchingpy.const.SHAPE_MODES[self._image_mode]
Create a new state machine.
108 def set_fill(self, fill: str): 109 """Set the fill color. 110 111 Set the color to use for filling shapes and figures. 112 113 Args: 114 fill: Name of the color or a hex code. 115 """ 116 self._fill_enabled = True 117 self._fill_str = fill
Set the fill color.
Set the color to use for filling shapes and figures.
Arguments:
- fill: Name of the color or a hex code.
119 def get_fill(self) -> str: 120 """Get the current fill color. 121 122 Returns: 123 Name of the color or a hex code. Undefined if get_fill_enabled() is False. 124 """ 125 return self._fill_str
Get the current fill color.
Returns:
Name of the color or a hex code. Undefined if get_fill_enabled() is False.
127 def get_fill_native(self): 128 """Get the renderer-native version of the fill color. 129 130 Returns: 131 Renderer-specific value. Undefined if get_fill_enabled() is False. 132 """ 133 return self._fill_str
Get the renderer-native version of the fill color.
Returns:
Renderer-specific value. Undefined if get_fill_enabled() is False.
135 def get_fill_enabled(self) -> bool: 136 """Determine if fill is enabled. 137 138 Returns: 139 True if fill enabled and false if transparent (drawing outlines). 140 """ 141 return self._fill_enabled
Determine if fill is enabled.
Returns:
True if fill enabled and false if transparent (drawing outlines).
143 def clear_fill(self): 144 """Indicate that the fill should be transparent / draw outlines.""" 145 self._fill_enabled = False
Indicate that the fill should be transparent / draw outlines.
147 def set_stroke(self, stroke: str): 148 """Set the stroke color. 149 150 Set the color to use for drawing outlines for shapes and figures as well as lines. 151 152 Args: 153 stroke: Name of the color or a hex code. 154 """ 155 self._stroke_enabled = True 156 self._stroke_str = stroke
Set the stroke color.
Set the color to use for drawing outlines for shapes and figures as well as lines.
Arguments:
- stroke: Name of the color or a hex code.
158 def get_stroke(self) -> str: 159 """Get the current stroke color. 160 161 Returns: 162 Name of the color or a hex code. Undefined if get_stroke_enabled() is False. 163 """ 164 return self._stroke_str
Get the current stroke color.
Returns:
Name of the color or a hex code. Undefined if get_stroke_enabled() is False.
166 def get_stroke_native(self): 167 """Get the renderer-native version of the stroke color. 168 169 Returns: 170 Renderer-specific value. Undefined if get_stroke_enabled() is False. 171 """ 172 return self._stroke_str
Get the renderer-native version of the stroke color.
Returns:
Renderer-specific value. Undefined if get_stroke_enabled() is False.
174 def get_stroke_enabled(self) -> bool: 175 """Determine if outline (stroke) should be drawn. 176 177 Returns: 178 True if it should be drawn (non-zero stroke weight) and False otherwise. 179 """ 180 return self._stroke_enabled and self._stroke_weight > 0
Determine if outline (stroke) should be drawn.
Returns:
True if it should be drawn (non-zero stroke weight) and False otherwise.
182 def clear_stroke(self): 183 """Indicate stroke should not be drawn.""" 184 self._stroke_enabled = False
Indicate stroke should not be drawn.
190 def set_arc_mode(self, mode: str): 191 """Specify how Sketchingpy should interpret the position and size arguments for arcs. 192 193 Args: 194 mode: String describing the mode to use. 195 """ 196 if mode not in sketchingpy.const.SHAPE_MODES: 197 raise RuntimeError('Unknown arc mode: ' + mode) 198 199 self._arc_mode = mode 200 self._arc_mode_enum = sketchingpy.const.SHAPE_MODES[self._arc_mode]
Specify how Sketchingpy should interpret the position and size arguments for arcs.
Arguments:
- mode: String describing the mode to use.
202 def get_arc_mode(self) -> str: 203 """Get mode describing how to interpret arc parameters. 204 205 Returns: 206 String describing the mode to use. 207 """ 208 return self._arc_mode
Get mode describing how to interpret arc parameters.
Returns:
String describing the mode to use.
210 def get_arc_mode_native(self): 211 """Get mode describing how to interpret arc parameters. 212 213 Returns: 214 Renderer-specific value. 215 """ 216 return self._arc_mode_enum
Get mode describing how to interpret arc parameters.
Returns:
Renderer-specific value.
218 def set_ellipse_mode(self, mode: str): 219 """Specify how Sketchingpy should interpret the position and size arguments. 220 221 Args: 222 mode: String describing the mode to use. 223 """ 224 if mode not in sketchingpy.const.SHAPE_MODES: 225 raise RuntimeError('Unknown ellipse mode: ' + mode) 226 227 self._ellipse_mode = mode 228 self._ellipse_mode_enum = sketchingpy.const.SHAPE_MODES[self._ellipse_mode]
Specify how Sketchingpy should interpret the position and size arguments.
Arguments:
- mode: String describing the mode to use.
230 def get_ellipse_mode(self) -> str: 231 """Get the mode describing how to interpret parameters for ellipsees. 232 233 Returns: 234 String describing the mode to use. 235 """ 236 return self._ellipse_mode
Get the mode describing how to interpret parameters for ellipsees.
Returns:
String describing the mode to use.
238 def get_ellipse_mode_native(self): 239 """Get the mode describing how to interpret parameters for ellipses. 240 241 Returns: 242 Renderer specific value. 243 """ 244 return self._ellipse_mode_enum
Get the mode describing how to interpret parameters for ellipses.
Returns:
Renderer specific value.
246 def set_rect_mode(self, mode: str): 247 """Specify how Sketchingpy should interpret the position and size arguments. 248 249 Args: 250 mode: String describing the mode to use. 251 """ 252 if mode not in sketchingpy.const.SHAPE_MODES: 253 raise RuntimeError('Unknown rect mode: ' + mode) 254 255 self._rect_mode = mode 256 self._rect_mode_enum = sketchingpy.const.SHAPE_MODES[self._rect_mode]
Specify how Sketchingpy should interpret the position and size arguments.
Arguments:
- mode: String describing the mode to use.
258 def get_rect_mode(self) -> str: 259 """Get the mode describing how to interpret parameters for rectangles. 260 261 Returns: 262 String describing the mode to use. 263 """ 264 return self._rect_mode
Get the mode describing how to interpret parameters for rectangles.
Returns:
String describing the mode to use.
266 def get_rect_mode_native(self): 267 """Get the mode describing how to interpret parameters for rectangles. 268 269 Returns: 270 Renderer-specific value. 271 """ 272 return self._rect_mode_enum
Get the mode describing how to interpret parameters for rectangles.
Returns:
Renderer-specific value.
274 def set_stroke_weight(self, stroke_weight: float): 275 """Set the stroke size. 276 277 Args: 278 stroke_weight: Number of pixels for the stroke weight. 279 """ 280 if stroke_weight < 0: 281 raise RuntimeError('Stroke weight must be zero or positive.') 282 283 self._stroke_weight = stroke_weight
Set the stroke size.
Arguments:
- stroke_weight: Number of pixels for the stroke weight.
285 def get_stroke_weight(self) -> float: 286 """Get the size of the stroke. 287 288 Returns: 289 Number of pixels for the stroke weight. 290 """ 291 if not self._stroke_enabled: 292 return 0 293 294 return self._stroke_weight
Get the size of the stroke.
Returns:
Number of pixels for the stroke weight.
296 def get_stroke_weight_native(self): 297 """Get the size of the stroke. 298 299 Returns: 300 Renderer-specific value 301 """ 302 return self._stroke_weight
Get the size of the stroke.
Returns:
Renderer-specific value
304 def set_text_font(self, font: Font): 305 """Set the type and size for text drawing. 306 307 Args: 308 font: Description of the font and font size to use. 309 """ 310 self._text_font = font
Set the type and size for text drawing.
Arguments:
- font: Description of the font and font size to use.
312 def get_text_font(self) -> Font: 313 """Get the type and size for text drawing. 314 315 Returns: 316 Description of the font and font size to use. 317 """ 318 if self._text_font is None: 319 raise RuntimeError('Font not yet set.') 320 321 return self._text_font
Get the type and size for text drawing.
Returns:
Description of the font and font size to use.
323 def get_text_font_native(self): 324 """Get the type and size for text drawing. 325 326 Returns: 327 Renderer-specific value. 328 """ 329 return self.get_text_font()
Get the type and size for text drawing.
Returns:
Renderer-specific value.
331 def set_text_align(self, text_align: TextAlign): 332 """Indicate the alignment to use when drawing text. 333 334 Args: 335 text_align: Structure describing horizontal and vertical text alignment. 336 """ 337 def check_align(name): 338 if name not in sketchingpy.const.ALIGN_OPTIONS: 339 raise RuntimeError('Unknown align: %d' % name) 340 341 check_align(text_align.get_horizontal_align()) 342 check_align(text_align.get_vertical_align()) 343 344 self._text_align = text_align 345 self._text_align_enum = TextAlign( 346 sketchingpy.const.ALIGN_OPTIONS[self._text_align.get_horizontal_align()], 347 sketchingpy.const.ALIGN_OPTIONS[self._text_align.get_vertical_align()] 348 )
Indicate the alignment to use when drawing text.
Arguments:
- text_align: Structure describing horizontal and vertical text alignment.
350 def get_text_align(self) -> TextAlign: 351 """Get the alignment to use when drawing text. 352 353 Returns: 354 Structure describing horizontal and vertical text alignment. 355 """ 356 return self._text_align
Get the alignment to use when drawing text.
Returns:
Structure describing horizontal and vertical text alignment.
358 def get_text_align_native(self): 359 """Get the alignment to use when drawing text. 360 361 Returns: 362 Renderer-specific value. 363 """ 364 return self._text_align_enum
Get the alignment to use when drawing text.
Returns:
Renderer-specific value.
370 def set_image_mode(self, mode: str): 371 """Specify how Sketchingpy should place images. 372 373 Args: 374 mode: String describing the mode to use. 375 """ 376 if mode not in ['center', 'corner']: 377 raise RuntimeError('Unknown image mode: ' + mode) 378 379 self._image_mode = mode 380 self._image_mode_enum = sketchingpy.const.SHAPE_MODES[self._image_mode]
Specify how Sketchingpy should place images.
Arguments:
- mode: String describing the mode to use.
382 def get_image_mode(self) -> str: 383 """Get how Sketchingpy should place images. 384 385 Returns: 386 String describing the mode to use. 387 """ 388 return self._image_mode
Get how Sketchingpy should place images.
Returns:
String describing the mode to use.
390 def get_image_mode_native(self): 391 """Get how Sketchingpy should place images. 392 393 Returns: 394 Renderer-specific value. 395 """ 396 return self._image_mode_enum
Get how Sketchingpy should place images.
Returns:
Renderer-specific value.
402 def set_angle_mode(self, mode: str): 403 """Indicate how angles should be provided to sketchingpy. 404 405 Args: 406 mode: The units (either 'degrees' or 'radians') in which to supply angles. 407 """ 408 if mode not in sketchingpy.const.ANGLE_MODES: 409 raise RuntimeError('Unknown angle mode: ' + mode) 410 411 self._angle_mode = mode 412 self._angle_mode_enum = sketchingpy.const.ANGLE_MODES[self._angle_mode]
Indicate how angles should be provided to sketchingpy.
Arguments:
- mode: The units (either 'degrees' or 'radians') in which to supply angles.
414 def get_angle_mode(self) -> str: 415 """Get how angles are to be provided to sketchingpy. 416 417 Return: 418 The units (either 'degrees' or 'radians') in which angles are to be supplied. 419 """ 420 return self._angle_mode
Get how angles are to be provided to sketchingpy.
Return:
The units (either 'degrees' or 'radians') in which angles are to be supplied.