def get_document_dimensions(tree, max_area=(11.0, 8.5)):
"""
Return the dimensions of this document in inches as to be plotted. If the
document specifies physical units, they will be converted to inches, and
asserted to be less than the working area of the AxiDraw. If the document
does not specify physical units (e.g. the width and height are in pixels
only) it will be scaled to the working area.
Returns a tuple of (width, height) in inches.
"""
max_width, max_height = max_area
raw_width = tree.get('width')
raw_height = tree.get('height')
if not (raw_width and raw_height):
log.warn("This document does not have width and height attributes. "
"Extracting viewbox dimensions.")
svg_width = svg_height = None
raw_width, raw_height = get_viewbox_dimensions(tree.get('viewBox'))
else:
svg_width = convert_to_inches(raw_width)
svg_height = convert_to_inches(raw_height)
if not (svg_width and svg_height):
log.warn("This document does not specify physical units. "
"Auto-scaling it to fit the drawing area.")
width = parse_pixels(raw_width)
height = parse_pixels(raw_height)
aspect_ratio = width / height
max_ratio = max_width / max_height
if aspect_ratio > max_ratio:
# Wider than working area, constrained by width
scale = max_width / width
else:
# Taller than working area, constrained by height
scale = max_height / height
svg_width = scale * width
svg_height = scale * height
assert svg_width <= max_width or math.isclose(svg_width, max_width), \
"SVG width of %s must be <= %s" % (svg_width, max_width)
assert svg_height <= max_height or math.isclose(svg_height, max_height), \
"SVG height of %s must be <= %s" % (svg_height, max_height)
return svg_width, svg_height
评论列表
文章目录