summaryrefslogtreecommitdiff
path: root/lib/svg/units.py
diff options
context:
space:
mode:
authorLex Neva <github.com@lexneva.name>2018-05-01 21:21:07 -0400
committerLex Neva <github.com@lexneva.name>2018-05-01 21:21:07 -0400
commit05daffb7e01db55879eb24f3c00532324a5d41af (patch)
tree43ff5d954e035e0e8b5a507b9c1bf9d6b4d3338d /lib/svg/units.py
parent1b31806423c8fec4040fed6d1009db016860b763 (diff)
refactor everything out of lib/__init__.py
Diffstat (limited to 'lib/svg/units.py')
-rw-r--r--lib/svg/units.py105
1 files changed, 105 insertions, 0 deletions
diff --git a/lib/svg/units.py b/lib/svg/units.py
new file mode 100644
index 00000000..015da60e
--- /dev/null
+++ b/lib/svg/units.py
@@ -0,0 +1,105 @@
+import simpletransform
+
+from ..utils import cache
+
+# modern versions of Inkscape use 96 pixels per inch as per the CSS standard
+PIXELS_PER_MM = 96 / 25.4
+
+# cribbed from inkscape-silhouette
+def parse_length_with_units( str ):
+
+ '''
+ Parse an SVG value which may or may not have units attached
+ This version is greatly simplified in that it only allows: no units,
+ units of px, mm, and %. Everything else, it returns None for.
+ There is a more general routine to consider in scour.py if more
+ generality is ever needed.
+ '''
+
+ u = 'px'
+ s = str.strip()
+ if s[-2:] == 'px':
+ s = s[:-2]
+ elif s[-2:] == 'mm':
+ u = 'mm'
+ s = s[:-2]
+ elif s[-2:] == 'pt':
+ u = 'pt'
+ s = s[:-2]
+ elif s[-2:] == 'pc':
+ u = 'pc'
+ s = s[:-2]
+ elif s[-2:] == 'cm':
+ u = 'cm'
+ s = s[:-2]
+ elif s[-2:] == 'in':
+ u = 'in'
+ s = s[:-2]
+ elif s[-1:] == '%':
+ u = '%'
+ s = s[:-1]
+ try:
+ v = float( s )
+ except:
+ raise ValueError(_("parseLengthWithUnits: unknown unit %s") % s)
+
+ return v, u
+
+
+def convert_length(length):
+ value, units = parse_length_with_units(length)
+
+ if not units or units == "px":
+ return value
+
+ if units == 'pt':
+ value /= 72
+ units = 'in'
+
+ if units == 'pc':
+ value /= 6
+ units = 'in'
+
+ if units == 'cm':
+ value *= 10
+ units = 'mm'
+
+ if units == 'mm':
+ value = value / 25.4
+ units = 'in'
+
+ if units == 'in':
+ # modern versions of Inkscape use CSS's 96 pixels per inch. When you
+ # open an old document, inkscape will add a viewbox for you.
+ return value * 96
+
+ raise ValueError(_("Unknown unit: %s") % units)
+
+
+@cache
+def get_doc_size(svg):
+ doc_width = convert_length(svg.get('width'))
+ doc_height = convert_length(svg.get('height'))
+
+ return doc_width, doc_height
+
+@cache
+def get_viewbox_transform(node):
+ # somewhat cribbed from inkscape-silhouette
+ doc_width, doc_height = get_doc_size(node)
+
+ viewbox = node.get('viewBox').strip().replace(',', ' ').split()
+
+ dx = -float(viewbox[0])
+ dy = -float(viewbox[1])
+ transform = simpletransform.parseTransform("translate(%f, %f)" % (dx, dy))
+
+ try:
+ sx = doc_width / float(viewbox[2])
+ sy = doc_height / float(viewbox[3])
+ scale_transform = simpletransform.parseTransform("scale(%f, %f)" % (sx, sy))
+ transform = simpletransform.composeTransform(transform, scale_transform)
+ except ZeroDivisionError:
+ pass
+
+ return transform