mathandy / svgpathtools

A collection of tools for manipulating and analyzing SVG Path objects and Bezier curves.
MIT License
557 stars 142 forks source link

How to access <g> transform data #167

Closed jaggzh closed 2 years ago

jaggzh commented 2 years ago

In this inkscape-generated svg, the second tag contains the paths, yet the also has a transform matrix that I need access to in order to calculate the path coordinates in the document. (Actually, the 2nd contains another which then contains the paths, but only one has transform data.)

How do we handle obtaining that matrix data? Thanks a bunch. :)

</metadata>
  <g
     id="g11"
     transform="matrix(0.35770326,0,0,0.35770326,112.84376,93.009103)">
    <title
       id="title6">Layer 1</title>
    <g
       id="layer1" />
  </g>
  <g
     id="g11-6"
     transform="matrix(0.35770326,0,0,0.35770326,305.16741,213.14742)">
    <title
       id="title6-7">Layer 1</title>
    <g
       id="layer1-5">
      <path
         style="fill:#0000ff;fill-rule:evenodd;stroke-width:2.79561"
        d="m -202.12524,256.36824 c 0,0 4.60796,-28.44592 5.30011,-62.10765
        0.69256,-33.68217 -2.53914,-72.51899 -17.94775,-93.4029
        -2.06966,-2.805098 -6.28679,42.8144 -10.8686,89.89778 -4.5818,47.08337
        -9.54114,95.63062 -12.26855,98.68104 2.09927,-2.34789 3.77276,-56.11723
        0.83363,-111.16883 -2.93984,-55.06495 -10.47556,-111.372085
...
jaggzh commented 2 years ago

I couldn't find a way, but looking through the source I found the Document api applies group transforms; now I'm stuck trying to find how to get path attributes (stroke/color) through the Doc interface. :/

mathandy commented 2 years ago

If I remember correctly, the transforms are only applied if you want to output a list of svgpathtools.Path objects, e.g. using the Document.paths() method.

To match up those output paths with the (already applied) transform data, you can search for the relevant attributes using the ElementTree -- the root node is stored for convenience in root attribute of the Document object.

I hope that helps. I wouldn't be opposed to adding an easier way to do this if someone wants to make a pull request, especially given the several potential points of confusion related to how the (still experimental) Document class handles transforms.