Test image.width and image.height without using 'is not None'

- Simplification requested by @formatc1702 in review of #153
- Inspection of size_html_cell() in Graphviz source code
  https://gitlab.com/graphviz/graphviz/-/blob/master/lib/common/htmltable.c#L1210-1221
  supports that a zero value is treated as not specified.
This commit is contained in:
KV 2020-08-18 18:10:45 +02:00
parent 0c6b6f390d
commit c21707980e
2 changed files with 12 additions and 10 deletions

View File

@ -16,23 +16,25 @@ class Image:
width: Optional[int] = None width: Optional[int] = None
height: Optional[int] = None height: Optional[int] = None
fixedsize: Optional[bool] = None fixedsize: Optional[bool] = None
# Contents of the cell <td> just below the image cell: # Contents of the text 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): def __post_init__(self):
if self.scale is None: if self.scale is None:
self.scale = "false" if self.width is None and self.height is None \ self.scale = "false" if not self.width and not self.height \
else "both" if self.width is not None and self.height is not None \ else "both" if self.width and self.height \
else "true" # When only one dimension is specified. else "true" # When only one dimension is specified.
if self.fixedsize is None: if self.fixedsize is None:
self.fixedsize = self.width is not None or self.height is not None self.fixedsize = self.width or self.height
if self.width is None and self.height is not None: if self.height:
if not self.width:
self.width = self.height # Assuming 1:1 aspect ratio for now. self.width = self.height # Assuming 1:1 aspect ratio for now.
elif self.height is None and self.width is not None: else:
if self.width:
self.height = self.width # Assuming 1:1 aspect ratio for now. self.height = self.width # Assuming 1:1 aspect ratio for now.

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 is not None else '') return ((f' width="{image.width}"' if image.width else '')
+ (f' height="{image.height}"' if image.height is not None else '') + (f' height="{image.height}"' if image.height else '')
+ ( ' fixedsize="true"' if image.fixedsize else '')) if image else '' + ( ' fixedsize="true"' if image.fixedsize else '')) if image else ''