2021-03-12 04:17:19 +00:00
|
|
|
# Authors: see git history
|
|
|
|
#
|
|
|
|
# Copyright (c) 2010 Authors
|
|
|
|
# Licensed under the GNU GPL version 3.0 or later. See the file LICENSE for details.
|
|
|
|
|
2021-10-09 16:25:29 +00:00
|
|
|
from inkex.units import convert_unit
|
2018-11-15 01:23:06 +00:00
|
|
|
|
2021-03-04 17:40:53 +00:00
|
|
|
from ..utils import Point, cache, string_to_floats
|
|
|
|
from .tags import INKSCAPE_LABEL, SODIPODI_GUIDE, SODIPODI_NAMEDVIEW
|
2018-11-15 01:23:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
class InkscapeGuide(object):
|
|
|
|
def __init__(self, node):
|
|
|
|
self.node = node
|
|
|
|
self.svg = node.getroottree().getroot()
|
|
|
|
|
|
|
|
self._parse()
|
|
|
|
|
|
|
|
def _parse(self):
|
|
|
|
self.label = self.node.get(INKSCAPE_LABEL, "")
|
|
|
|
|
2021-10-09 16:25:29 +00:00
|
|
|
doc_size = self.svg.get_page_bbox()
|
2018-11-15 01:23:06 +00:00
|
|
|
|
2021-10-09 16:25:29 +00:00
|
|
|
# inkscape's Y axis is reversed from SVG's, and the guide is in inkscape coordinates
|
2018-11-15 01:23:06 +00:00
|
|
|
self.position = Point(*string_to_floats(self.node.get('position')))
|
2021-10-09 16:25:29 +00:00
|
|
|
self.position.y = doc_size.y.size - self.position.y
|
2018-11-15 01:23:06 +00:00
|
|
|
|
2021-10-09 16:25:29 +00:00
|
|
|
# convert units to px
|
|
|
|
unit = self.svg.unit
|
|
|
|
self.position.y = convert_unit(self.position.y, 'px', unit)
|
2018-11-15 01:23:06 +00:00
|
|
|
|
|
|
|
# This one baffles me. I think inkscape might have gotten the order of
|
|
|
|
# their vector wrong?
|
|
|
|
parts = string_to_floats(self.node.get('orientation'))
|
|
|
|
self.direction = Point(parts[1], parts[0])
|
|
|
|
|
|
|
|
|
|
|
|
@cache
|
|
|
|
def get_guides(svg):
|
|
|
|
"""Find all Inkscape guides and return as InkscapeGuide instances."""
|
|
|
|
|
|
|
|
namedview = svg.find(SODIPODI_NAMEDVIEW)
|
|
|
|
if namedview is None:
|
|
|
|
return []
|
|
|
|
|
|
|
|
return [InkscapeGuide(node) for node in namedview.findall(SODIPODI_GUIDE)]
|