tdf#160702: improve text positioning
commitcc3663bbaed4f65d64154e5f9abb51a5f622f710
authorMike Kaganski <mike.kaganski@collabora.com>
Tue, 16 Apr 2024 05:48:06 +0000 (16 10:48 +0500)
committerMike Kaganski <mike.kaganski@collabora.com>
Sat, 20 Apr 2024 09:28:34 +0000 (20 11:28 +0200)
treeb364a63443222b6690a5ec1359f12cb2d7c8fa0a
parentcfa9990d470b10548c7fed64eb1182fea11d41e0
tdf#160702: improve text positioning

Opening an SVG with text in different elements (e.g., tspans) in the
same text element performs calculations to position the parts properly
(i.e., the next part will start where the previous part ended, unless
the position in overridden explicitly). These calculations require to
know the text widths. The first problem leas here: the text width was
calculated for a typically small text size (numerically equal to the
pixel size defined in the SVG), but these calculations aren't truly
linear, because font rendering may change depending on font height.
Additionally, the rounding gives much higher error in smaller sizes
than in larger. There was already a workaround for a similar problem
in ViewRedirector::createRedirectedPrimitive2DSequence, where a large
font (with 100 times greater height) was used to increase correctness.
This was also used here, with the same large height (50000) used as a
reference.

Then, at the time of wrawing the text at different zoom levels, the
code in VclProcessor2D::RenderTextSimpleOrDecoratedPortionPrimitive2D
creates a font of a calculated size, and uses it to output the text.
But the font is always created with an integral height, which means,
that for a wanted height of 2.5 (in a zoomed out view), the really
used height will be 3, which is 20% larger; or for wanted height of
2.4, the actual height will be 2 (20% smaller). This resulted in odd
jumps of the text widths, when the text may overlap the following
part, or conversely, create a big gap before the next gap. To try to
mitigate that, the function now takes the difference between the
wanted and the actual font size into account, and adjusts the MapMode
accordingly. This doesn't fix the jumping completely (e.g., because
of the mentioned special handling of small font sizes in the fonts
thenselves, like hinting), but still makes the calculations much more
stable, decreasing the amount of jumping. Similar changes are made in
TextLayouterDevice.

Use of the functions that return text size as a double, not rounded
value, should additionally help improving stability.

Change-Id: I455845d8ca43ee9c06a0fc980947f35d8a25797a
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/166238
Tested-by: Jenkins
Reviewed-by: Mike Kaganski <mike.kaganski@collabora.com>
drawinglayer/source/primitive2d/textlayoutdevice.cxx
drawinglayer/source/processor2d/vclprocessor2d.cxx
include/drawinglayer/primitive2d/textlayoutdevice.hxx
svgio/qa/cppunit/SvgImportTest.cxx
svgio/source/svgreader/svgcharacternode.cxx