SVG export improvements

Discussions about the development of the TechDraw workbench
tpavlicek
Posts: 27
Joined: Sun Jan 07, 2018 2:15 am

SVG export improvements

Postby tpavlicek » Fri Jun 07, 2019 9:33 pm

Hello,

I was exporting several SVG files from TechDraw and I did notice the resulting file is unexpectedly large. This led me to (sometimes quite deep) research and finally to refactoring the SVG handling code. Here is a list of issues and related changes:

  • Updating a XML document by a pack of regular expressions is rather an emergency solution. Qt offers XML DOM and XML Query modules. These can be interconnected via QDomNodeModel (BSD licensed) by Stanislaw Adaszewski. I have added proper XML namespaces handling to his model and now we can fully use the advantages of DOM and XQuery for e.g. working with editable texts.
  • Qt QSvgGenerator class does not generate (not even remotely) the same code as the QSvgRenderer previously accepted. This is particularly displeasing with template texts, which end up as (huge) series of paths, due to QSvgRenderer's unkind SVG stroke handling. Needless to say, later change of such a beast is almost impossible. Thus in my pull request, I offer inserting the template SVG code directly into the resulting file, bypassing the QSvgRenderer -> QSvgGenerator roundtrip. The template keeps its whole structure, just editable texts are updated.
  • For some unknown reason (comfortableness?) Qt inserts a lot of empty SVG group <g> elements with no childs, but a lot of useless attributes. We can simply remove these, as they are of no significance. Together with changes above, an exported A4 ISO 7200 page with a single drawing is reduced from 142kB to reasonable 8kB.
  • After "resolving" the screen/SVG font size issue last week, I have found out there is much simpler and better solution: To set the font size in pixels (i.e. tenths of virtual mm in our case), instead of "points" computed from logical DPI. This change is also neccessary, if we want to include the SVG template directly and not squeezed through warped Qt SVG processing.
  • Last but not least, I understand the usage of xml:space="preserve" attribute in SVG, but it should be put directly on the <tspan> elements and not on template's <text> items. Any SVG processing changing the amount of spaces (e.g. different indentation) then breaks the actual position of affected labels. I have removed xml:space attributes from all SVG templates and my solution is to add them programatically to each editable <tspan> during the editable texts replacement phase. Inner spaces are preserved, but whole texts are never shifted.
I hope You will like these improvements, kind regards,

Tomas
tpavlicek
Posts: 27
Joined: Sun Jan 07, 2018 2:15 am

Re: SVG export improvements

Postby tpavlicek » Fri Jun 07, 2019 9:36 pm

And my pull request for the changes above: https://github.com/FreeCAD/FreeCAD/pull/2238

Tomas
User avatar
wandererfan
Posts: 3486
Joined: Tue Nov 06, 2012 5:42 pm

Re: SVG export improvements

Postby wandererfan » Sat Jun 08, 2019 12:33 am

tpavlicek wrote:
Fri Jun 07, 2019 9:36 pm
And my pull request for the changes above: https://github.com/FreeCAD/FreeCAD/pull/2238
This looks fantastic, Tomas. Just waiting on a build now.

wf
User avatar
wandererfan
Posts: 3486
Joined: Tue Nov 06, 2012 5:42 pm

Re: SVG export improvements

Postby wandererfan » Mon Jun 10, 2019 12:31 am

tpavlicek wrote:
Fri Jun 07, 2019 9:36 pm
And my pull request for the changes above: https://github.com/FreeCAD/FreeCAD/pull/2238
Some very minor points:
- no license in QDomNodeModel.h
- license text doesn't say "under BSD License". I think the type of license has to be specified, but I am not a lawyer.

Maybe a bigger point for custom templates and Annotations - the text is sometimes the wrong size. I didn't investigate very deeply yet.
custom template in v0.18
ansiBv018.png
ansiBv018.png (15.57 KiB) Viewed 462 times
custom template in PR
ansiBvTomas.png
ansiBvTomas.png (24.76 KiB) Viewed 462 times
exported text
8mmTextinInkscape.png
8mmTextinInkscape.png (133.54 KiB) Viewed 462 times
Attachments
ANSIB_Portrait.svg
(25.12 KiB) Downloaded 7 times
User avatar
wandererfan
Posts: 3486
Joined: Tue Nov 06, 2012 5:42 pm

Re: SVG export improvements

Postby wandererfan » Mon Jun 10, 2019 12:54 pm

tpavlicek wrote:
Fri Jun 07, 2019 9:33 pm
Last but not least, I understand the usage of xml:space="preserve" attribute in SVG, but it should be put directly on the <tspan> elements and not on template's <text> items. Any SVG processing changing the amount of spaces (e.g. different indentation) then breaks the actual position of affected labels. I have removed xml:space attributes from all SVG templates and my solution is to add them programatically to each editable <tspan> during the editable texts replacement phase. Inner spaces are preserved, but whole texts are never shifted.
I added the following:
"The xml:space="preserve" clause sometimes causes problems with text size and positioning. It is best to avoid/remove this clause from your custom template's SVG code."
to TechDraw_Templates.

Is this accurate for the new code?

wf
tpavlicek
Posts: 27
Joined: Sun Jan 07, 2018 2:15 am

Re: SVG export improvements

Postby tpavlicek » Mon Jun 10, 2019 1:12 pm

Hi,

exactly - I'd say >>No xml:space="preserve" attributes, unless FreeCAD sets it itself.<< otherwise the template author risks bad label positioning, because the amount of spaces in the input XML can significantly differ from the XML produced by Qt XML DOM writer with 2 spaces as default block indent. The library code is something out of our reach, so let's stick to well formed XML not using the xml:space attribute and let the FreeCAD code to put it only where needed, i.e. only on the editable <tspan> elements, so the users can enter several successive spaces, which won't be condensed by SVG renderer.

As for the QDomNodeModel BSD license, I am not a lawyer either, but the author clearly says on the Qt Centre webpage https://www.qtcentre.org/threads/37645- ... -QXmlQuery:
I have prepared an implementation (BSD licensed) of QAbstractXmlNodeModel class from QtXmlPatterns module which makes it possible to run QXmlQuery-ies on a QDomDocument. I've called it QDomNodeModel.
So I decided to believe him ;-)

Anyway, I will try to look on the ANSIB template, hopefully only simple tweak is neccessary...

Tomas
Last edited by tpavlicek on Mon Jun 10, 2019 1:43 pm, edited 1 time in total.
User avatar
wandererfan
Posts: 3486
Joined: Tue Nov 06, 2012 5:42 pm

Re: SVG export improvements

Postby wandererfan » Mon Jun 10, 2019 1:17 pm

tpavlicek wrote:
Mon Jun 10, 2019 1:12 pm
Anyway, I will try to look on the ANSIB template, hopefully only simple tweak is neccessary...
I'm losing track of which message is which. Must be getting old. :(

I removed the xml:space="preserve" from the ANSIB template and now it works fine.
tpavlicek
Posts: 27
Joined: Sun Jan 07, 2018 2:15 am

Re: SVG export improvements

Postby tpavlicek » Mon Jun 10, 2019 1:43 pm

Oops, sorry, I was commenting the issues mentioned in this thread above, namely the BSD license question and the ANSIB template problem, whose editable fields font is huge, at least from what I can see on the pictures...
tpavlicek
Posts: 27
Joined: Sun Jan 07, 2018 2:15 am

Re: SVG export improvements

Postby tpavlicek » Tue Jun 11, 2019 11:26 am

Hi again,

I have closely looked on the ANSIB templates and basically, there were font style attributes attached to the <tspan> with editable value. As my initial replacement design was to create a new <tspan> and fill in just the text, the font attibutes were discarded and the font style taken from parent <text> elelement, which was by default 12mm, i.e. pretty big.

Now the new replacement strategy is just to remove all child nodes from <tspan> (leaving the attributes untouched) and insert single text child node. After that, everything within ANSIB templates looks good.

Kind regards,

Tomas
User avatar
wandererfan
Posts: 3486
Joined: Tue Nov 06, 2012 5:42 pm

Re: SVG export improvements

Postby wandererfan » Tue Jun 11, 2019 11:59 am

tpavlicek wrote:
Tue Jun 11, 2019 11:26 am
I have closely looked on the ANSIB templates and basically, there were font style attributes attached to the <tspan> with editable value. As my initial replacement design was to create a new <tspan> and fill in just the text, the font attibutes were discarded and the font style taken from parent <text> elelement, which was by default 12mm, i.e. pretty big.
You're much more subtle than me. I just took a hammer to the SVG code. In any case it looks good now.

Thanks again for your hard work on this.
Attachments
ANSIB_Fixed.png
ANSIB_Fixed.png (30.78 KiB) Viewed 295 times