inkscape-applytransforms icon indicating copy to clipboard operation
inkscape-applytransforms copied to clipboard

text transformation

Open raszpl opened this issue 1 year ago • 14 comments

fixing https://github.com/Klowner/inkscape-applytransforms/pull/59 by applying same way of accessing attributes as used by transformRectangle Works on both text and tspan in tests/data/svg/applytransform.svg

raszpl avatar Dec 08 '24 20:12 raszpl

I was soo overthinking this, now applies scaling to tspan. The only thing missing is scaling font size, dont know how to approach it since scaling is X Y independent but we can only control font size using one parameter. Example:

transform="matrix(1.3333333,0,0,1.6,229.22667,101.70267)"><tspan
         id="tspan70"
         style="font-variant:normal;font-weight:normal;font-size:4.8px

X 1.3, Y 1.6. What do? Scale 4.8px by average? :|

raszpl avatar Dec 08 '24 22:12 raszpl

Instead of overthinking it I adopted scaleStrokeWidth to scale any attribute passed as parameter. self.scaleStyleAttrib(node, transf, 'font-size') works surprisingly well on Text :o My imported Schematics dont differ all that much after applytransforms from originals. Ill call it a win.

raszpl avatar Dec 08 '24 22:12 raszpl

this PR is good for text without rotations, but it will mess around with this example:

grafik

vmario89 avatar Dec 09 '24 16:12 vmario89

You are correct :( My example diagram sheet had no rotated text, checked on another one and this is losing rotation. This patch also doesnt scale DX DY.

Looking into both.

raszpl avatar Dec 09 '24 17:12 raszpl

basically i really like the idea to transform text, but i think the rotation is not possible (technically, due to specification of SVG i think). but it could be done to filter if text has rotation or not: if the text has rotation, skip it, else apply transform. So thats a lot more than nothing

vmario89 avatar Dec 09 '24 18:12 vmario89

Im having trouble coercing Inkskape to set exactly what I want transform="matrix(0,1.3333333,-1.5555556,0,290.30667,236.53333)" should be turned into

       x="290.307"
       y="236.533"
       transform="rotate(90.000 290.307 236.533)"

but for some reason node.attrib['transform'] = "rotate(90.000 290.307 236.533)" automagically mangles it back into a weird matrix :|

       x="290.307"
       y="236.533"
       transform="matrix(6.12323e-17 1 -1 6.12323e-17 526.84 -53.774)"

Mathematically its the same thing, but I really want clean rotate(angle x y) in the output :( I node.attrib['transform1'] = "rotate(90.000 290.307 236.533)" it doesnt touch it, its some deliberate automation.

I feel like Im really close to the solution. I want SVG with flattened applied matrix equations for easier automatic scale detection when importing to Kicad. Source dest6 still slightly mangled, but almost there output right now dest6zzz

raszpl avatar Dec 10 '24 02:12 raszpl

Added scaling dx dy. Looks almost perfect now, or as perfect as Inkscape can do with converted pdf. original: dest6 result: dest6zzz

Two problems:

  • Still dont know how to force it to output "rotate(a x y)". Keeps translating this to matrix in the background :( Whole point of me doing this patch was getting svg with nice rotate transforms. There is nothing about it in https://gitlab.com/inkscape/extensions/-/blob/master/inkex/elements/_base.py#L179 tr = str(f"rotate({angle:.3f} {new_x:.3f} {new_y:.3f})") tr is rotate(-90.000 404.499 96.693) node.set('transform',tr) node.get('transform') returns matrix(6.12323e-17 -1 1 6.12323e-17 307.806 501.192) Doesnt do it to node.set('transform1',tr) or any other random attribute name, doesnt do it to pure rotate(-90.000) with no pivot point. :| For my own use I resigned to outputting node.set('transform1',tr) and renaming in later pass.

  • Useless clip-path="url(#clipPathxxx" that Inkscape inserted in random places to individual letters (][ and g) when converting from PDF. Those become invisible when matrix is replaced with xy coordinates. Regex clip-path=.*" takes care of that garbage.

raszpl avatar Dec 10 '24 04:12 raszpl

First problem fixed :] node.attrib['transform'] = str(f"rotate({angle:.3f} {new_x:.3f} {new_y:.3f})") bypasses magic transmutations.

Second problem can be fixed by

if 'clip-path' in node.attrib:
			del node.attrib['clip-path']

but thats not universal, someone might want to keep 'clip-path's

raszpl avatar Dec 10 '24 07:12 raszpl

thats cool! i messed around and the rotation seems to work now! i found another thing which might be problematic - skewing / non-uniform transformation does the following

before:

grafik

after applying transform:

grafik

source file: test

vmario89 avatar Dec 11 '24 08:12 vmario89

Yes, it only cleanly handles uniform scaling. In my Example rotated pin numbers have slightly wrong offsets. ~~I suspect they move from their original location because we can only scale Fonts with one number while example text has 1.3:1.6 scaled ratio. Probably could be computed better, but this is good enough for my original purpose.~~

For things like skewing there definitely should be an option of detecting and skipping.

raszpl avatar Dec 11 '24 20:12 raszpl

I copied

		sx = math.sqrt(a**2 + c**2)
		sy = math.sqrt(b**2 + d**2)

straight from transformRectangle and didnt even think about it, but its Wrong. It doesnt extract real X Y scaling! Cant even notyice because test rectangle is 1 white, and browsers usually renders SVG on white background so cant see it 2 is uniformly scaled by -1,1! correct calculation is

		sx = math.sqrt(a**2 + b**2)
		sy = math.sqrt(c**2 + d**2)

and now both transformRectangle and my rotated text works!

raszpl avatar Dec 12 '24 01:12 raszpl

have some new simple test file where scaling messes up

TEST

grafik

gets

grafik

vmario89 avatar Aug 25 '25 22:08 vmario89

your

https://private-user-images.githubusercontent.com/5631071/481832060-c42bde55-d57c-4059-9f45-9f487aa90ba1.svg

  <text
     id="text350"
     xml:space="preserve"
     x="28.402536"
     y="2.6447694"><tspan
       id="tspan350"
       x="21.988474"
       y="2.6447694">Kontext Kommunikation</tspan></text>

doesnt have any scaling?

raszpl avatar Aug 26 '25 12:08 raszpl

very strange. now when i open again i cannot reproduce ... :O

vmario89 avatar Aug 26 '25 12:08 vmario89