Skip to content

text transformation - #70

Open
raszpl wants to merge 10 commits into
Klowner:masterfrom
raszpl:patch-1
Open

text transformation#70
raszpl wants to merge 10 commits into
Klowner:masterfrom
raszpl:patch-1

Conversation

@raszpl

@raszpl raszpl commented Dec 8, 2024

Copy link
Copy Markdown
Contributor

fixing #59 by applying same way of accessing attributes as used by transformRectangle
Works on both text and tspan in tests/data/svg/applytransform.svg

fixing Klowner#59 by applying same way of accessing attributes as used by transformRectangle
@raszpl

raszpl commented Dec 8, 2024

Copy link
Copy Markdown
Contributor Author

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? :|

adopted scaleStrokeWidth to scale any attribute passed as parameter
@raszpl

raszpl commented Dec 8, 2024

Copy link
Copy Markdown
Contributor Author

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.

round to two places and dont forget about 'px'
@vmario89

vmario89 commented Dec 9, 2024

Copy link
Copy Markdown
Contributor

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

grafik

@raszpl

raszpl commented Dec 9, 2024

Copy link
Copy Markdown
Contributor Author

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.

@vmario89

vmario89 commented Dec 9, 2024

Copy link
Copy Markdown
Contributor

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

@raszpl

raszpl commented Dec 10, 2024

Copy link
Copy Markdown
Contributor Author

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

raszpl commented Dec 10, 2024

Copy link
Copy Markdown
Contributor Author

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

raszpl commented Dec 10, 2024

Copy link
Copy Markdown
Contributor Author

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

@vmario89

Copy link
Copy Markdown
Contributor

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

@raszpl

raszpl commented Dec 11, 2024

Copy link
Copy Markdown
Contributor Author

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

raszpl commented Dec 12, 2024

Copy link
Copy Markdown
Contributor Author

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!

@vmario89

Copy link
Copy Markdown
Contributor

have some new simple test file where scaling messes up

TEST

grafik

gets

grafik

@raszpl

raszpl commented Aug 26, 2025

Copy link
Copy Markdown
Contributor Author

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?

@vmario89

Copy link
Copy Markdown
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants