Compute sensible default values for unspecified image attributes

The goal is to enable the user to avoid specifying more attributes
than strictly needed.
This commit is contained in:
KV 2020-08-16 23:53:12 +02:00
parent d289f95bc3
commit 0c6b6f390d
3 changed files with 20 additions and 6 deletions

View File

@ -9,8 +9,6 @@ connectors:
show_pincount: false show_pincount: false
image: image:
src: resources/stereo-phone-plug-TRS.png src: resources/stereo-phone-plug-TRS.png
scale: true
height: 100
caption: Tip, Ring, and Sleeve caption: Tip, Ring, and Sleeve
cables: cables:
@ -23,6 +21,7 @@ cables:
shield: SN shield: SN
image: image:
src: resources/cable-WH+BN+GN+shield.png src: resources/cable-WH+BN+GN+shield.png
height: 75
caption: Cross-section caption: Cross-section
connections: connections:

View File

@ -11,15 +11,30 @@ from wireviz import wv_colors
class Image: class Image:
# Attributes of the image object <img>: # Attributes of the image object <img>:
src: str src: str
scale: Optional[str] = "false" # false | true | width | height | both scale: Optional[str] = None # false | true | width | height | both
# Attributes of the image cell <td> containing the image: # Attributes of the image cell <td> containing the image:
width: Optional[int] = None width: Optional[int] = None
height: Optional[int] = None height: Optional[int] = None
fixedsize: Optional[bool] = False fixedsize: Optional[bool] = None
# Contents of the cell <td> just below the image cell: # Contents of the cell <td> just below the image cell:
caption: Optional[str] = None caption: Optional[str] = None
# See also HTML doc at https://graphviz.org/doc/info/shapes.html#html # See also HTML doc at https://graphviz.org/doc/info/shapes.html#html
def __post_init__(self):
if self.scale is None:
self.scale = "false" if self.width is None and self.height is None \
else "both" if self.width is not None and self.height is not None \
else "true" # When only one dimension is specified.
if self.fixedsize is None:
self.fixedsize = self.width is not None or self.height is not None
if self.width is None and self.height is not None:
self.width = self.height # Assuming 1:1 aspect ratio for now.
elif self.height is None and self.width is not None:
self.height = self.width # Assuming 1:1 aspect ratio for now.
@dataclass @dataclass
class Connector: class Connector:

View File

@ -75,8 +75,8 @@ def html_caption(image):
def html_size_attr(image): def html_size_attr(image):
# Return Graphviz HTML attributes to specify minimum or fixed size of a TABLE or TD object # Return Graphviz HTML attributes to specify minimum or fixed size of a TABLE or TD object
return ((f' width="{image.width}"' if image.width else '') return ((f' width="{image.width}"' if image.width is not None else '')
+ (f' height="{image.height}"' if image.height else '') + (f' height="{image.height}"' if image.height is not None else '')
+ ( ' fixedsize="true"' if image.fixedsize else '')) if image else '' + ( ' fixedsize="true"' if image.fixedsize else '')) if image else ''