linebender / resvg

An SVG rendering library.
Mozilla Public License 2.0
2.79k stars 225 forks source link

usvg: Produce Text nodes in the Tree, even when text feature is diabled? #802

Closed maxmelander closed 2 months ago

maxmelander commented 2 months ago

Would it be possible to change things so that we still get Text nodes in the tree even when the text feature is disabled?

In our case, we want to do our own shaping and scaling of text after having converted svg text elements to our own internal types, and don't need the text bounds or paths. What do you think of always including Text nodes in the Tree and only fencing off the functions/fields depending on shaping behind the text feature?

Note that I'm only talking about usvg here, not resvg:)

RazrFalcon commented 2 months ago

The way usvg works is that it requires to know a bounding box of each element. To get a bbox of text node we need to outline it. As simple as that.

For now, you can simply enable the text feature and do no use flattened output. The raw SVG Text data will still be available.

If the goal is to avoid the text feature dependencies then I'm afraid it's not possible and I don't see an easy way of implementing this.

maxmelander commented 2 months ago

Thank you for the super quick reply!

Okay, so if I naively just let the bounding box of the text node stay as the dummy and never do the outlining, other elements in the svg would become incorrect as a result? Or if I don't resolve fonts and just use some standard fallback, that would also lead to less accurate layouts?

RazrFalcon commented 2 months ago

usvg requires to know elements bboxes to resolve objectBoundingBox units. If you skip text elements - you will get incorrect results.

SVG is a horrible, horrible file format and the purpose of usvg is to remove all the complexity from it. usvg is not an SVG parser, it's a converter. If you do not like its representation of SVG - nothing we can do about it. It's intentionally high-level.

RazrFalcon commented 2 months ago

Or if I don't resolve fonts and just use some standard fallback, that would also lead to less accurate layouts?

You can provide a fontdb with a single font and it should work ok-ish. But all uses of text + objectBoundingBox will be broken/inaccurate.

maxmelander commented 2 months ago

Gotcha! Thank you for taking the time to explain. I'll just go ahead and use it as intended and load in the correct fonts when needed. Didn't even know about objectBoundingBox units, and am very happy that this crate allows me to stay blissfully ignorant!

Thanks again:)

RazrFalcon commented 2 months ago

Yeah, text bounding boxes are pretty nightmarish: https://razrfalcon.github.io/notes-on-svg-parsing/text/bbox.html