kopia lustrzana https://github.com/inkstitch/inkstitch
fixup! refactor shared stuff into inkstitch.py
rodzic
a0819f60c2
commit
c6d01f8faa
88
embroider.py
88
embroider.py
|
@ -35,93 +35,7 @@ import networkx
|
|||
from pprint import pformat
|
||||
|
||||
import inkstitch
|
||||
from inkstitch import cache, dbg, param, EmbroideryElement, get_nodes, SVG_POLYLINE_TAG, SVG_GROUP_TAG, PIXELS_PER_MM
|
||||
|
||||
# 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 == '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_viewbox_transform(node):
|
||||
# somewhat cribbed from inkscape-silhouette
|
||||
|
||||
doc_width = convert_length(node.get('width'))
|
||||
doc_height = convert_length(node.get('height'))
|
||||
|
||||
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
|
||||
from inkstitch import cache, dbg, param, EmbroideryElement, get_nodes, SVG_POLYLINE_TAG, SVG_GROUP_TAG, PIXELS_PER_MM, get_viewbox_transform
|
||||
|
||||
|
||||
class Fill(EmbroideryElement):
|
||||
|
|
Ładowanie…
Reference in New Issue