Bug 472566 - Wrong bounding box for vertical text with PMingLiU
Summary: Wrong bounding box for vertical text with PMingLiU
Status: RESOLVED FIXED
Alias: None
Product: krita
Classification: Applications
Component: Tool/Text (show other bugs)
Version: git master (please specify the git hash!)
Platform: Microsoft Windows Microsoft Windows
: NOR normal
Target Milestone: ---
Assignee: Krita Bugs
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2023-07-24 08:04 UTC by Alvin Wong
Modified: 2023-08-08 09:12 UTC (History)
1 user (show)

See Also:
Latest Commit:
Version Fixed In:
Sentry Crash Report:


Attachments
Screenshot 1 (24.84 KB, image/png)
2023-07-24 08:04 UTC, Alvin Wong
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Alvin Wong 2023-07-24 08:04:51 UTC
Created attachment 160492 [details]
Screenshot 1

PMingLiU (Windows 10) wrapped with inline-size:

```svg
<text text-rendering="auto" fill="#000000" stroke-opacity="0" stroke="#000000" 
stroke-width="0" stroke-linecap="square" stroke-linejoin="bevel" letter-spacing="0" 
word-spacing="0" writing-mode="vertical-rl" style="inline-size: 64.67;
text-align: start;text-align-last: auto;font-family: PMingLiU;font-size: 16;
font-size-adjust: 0.429688;white-space: pre-wrap;"
><tspan>佔位文字 Testing
</tspan><tspan>佔位文字 Testing</tspan></text>
```

The rightmost line sticks outside of the bounding box, and the bounding boxes of each hard-broken lines extend beyond the left side of the actual glyphs.

If you change the font size to 16.5, the bottom of the "g" also extends downwards beyond the bounding box.
Comment 1 wolthera 2023-07-24 09:55:39 UTC
Yes, I've seen this several times with different fonts and font-sizes, and I have no clue what is causing it. The line-height thing where it was 'fixed' by adding scaleToPixel to the equasion was one of these situations, and everytime I ask help it either doesn't reproduce or it's like 'well, tweaking this fixes it thus that must be the cause', which makes no sense, the boundingboxes, text-to-shape (in the shape edit tool) and the paintComponent() all use the exact same text-layout result, they should never be different.

I suspect that the potential KisForest refactor that text may have for phase 2 could fix it by making all three of these use the same codepath, but that's a wild guess and as it is, I have no clue what causes this.
Comment 2 Alvin Wong 2023-07-24 18:01:30 UTC
Repeating what's said on IRC:

I think the wrong outline is due to it being generated from using 72 dpi. At 16px it ends up using the bitmap glyphs in PMingLiU which gives drastically different metrics (and the current code is also a bit broken at handling it).

When the shape is to be painted, it is actually cloned and passed to an update job on a separate thread, so when it gets the real resolution in paintComponent, it does not get updated back to the original shape, so it keeps having the wrong outline. (This also means text shapes just gets a relayout every time it is painted.)

A potential solution may be to handle resolution changes in KisShapeLayerCanvas, then funnel it to KoShapeManager and then the shapes. I can try to make a poc of that.
Comment 3 Bug Janitor Service 2023-07-25 06:06:31 UTC
A possibly relevant merge request was started @ https://invent.kde.org/graphics/krita/-/merge_requests/1866
Comment 4 Alvin Wong 2023-07-25 06:59:42 UTC
Git commit d6ccb8708379b7cee72bed48437e3ab7591b121e by Alvin Wong.
Committed on 25/07/2023 at 08:59.
Pushed by alvinwong into branch 'master'.

text: Fix bounding box width for bitmap glyph

M  +1    -1    libs/flake/text/KoSvgTextShape.cpp

https://invent.kde.org/graphics/krita/-/commit/d6ccb8708379b7cee72bed48437e3ab7591b121e
Comment 5 Alvin Wong 2023-07-25 07:31:36 UTC
Git commit 22f81a05ad15d8e24a1dceb174c95fce4e6c90f4 by Alvin Wong.
Committed on 25/07/2023 at 09:31.
Pushed by alvinwong into branch 'krita/5.2'.

text: Fix bounding box width for bitmap glyph
(cherry picked from commit d6ccb8708379b7cee72bed48437e3ab7591b121e)

M  +1    -1    libs/flake/text/KoSvgTextShape.cpp

https://invent.kde.org/graphics/krita/-/commit/22f81a05ad15d8e24a1dceb174c95fce4e6c90f4
Comment 6 Alvin Wong 2023-08-04 15:59:06 UTC
Git commit 85d5143c0c33f0b8e4f4ee3b5b83b88a7c2f0844 by Alvin Wong.
Committed on 04/08/2023 at 17:57.
Pushed by alvinwong into branch 'master'.

Pass image dpi to shapes, fix text shape not updating dpi properly

Funnel the image resolution from KisShapeLayer through
ShapeLayerContainerModel to the KoShape instances. This allows KoSvgTextShape
to get the image resolution outside of paintComponent.

The previous approach of calculating the resolution in paintComponent
did not work well because shapes are cloned to be painted in an update
job. This meant the original shape never actually gets the proper dpi
and layout, which causes the shape outline to become out of sync with
the painted shape. This had significant issues for fonts containing
bitmap glyphs, which gives drastically different metrics under different
dpi. (It also meant text shapes always do a relayout whenever they are
painted.)

M  +4    -0    libs/flake/KoShape.cpp
M  +9    -0    libs/flake/KoShape.h
M  +10   -22   libs/flake/text/KoSvgTextShape.cpp
M  +2    -0    libs/flake/text/KoSvgTextShape.h
M  +34   -0    libs/ui/flake/kis_shape_layer.cc
M  +1    -0    libs/ui/flake/kis_shape_layer.h

https://invent.kde.org/graphics/krita/-/commit/85d5143c0c33f0b8e4f4ee3b5b83b88a7c2f0844
Comment 7 Alvin Wong 2023-08-04 16:57:50 UTC
Git commit bb839b2855b9c336cb6563f6160050970b0295c8 by Alvin Wong.
Committed on 04/08/2023 at 18:00.
Pushed by alvinwong into branch 'krita/5.2'.

Pass image dpi to shapes, fix text shape not updating dpi properly

Funnel the image resolution from KisShapeLayer through
ShapeLayerContainerModel to the KoShape instances. This allows KoSvgTextShape
to get the image resolution outside of paintComponent.

The previous approach of calculating the resolution in paintComponent
did not work well because shapes are cloned to be painted in an update
job. This meant the original shape never actually gets the proper dpi
and layout, which causes the shape outline to become out of sync with
the painted shape. This had significant issues for fonts containing
bitmap glyphs, which gives drastically different metrics under different
dpi. (It also meant text shapes always do a relayout whenever they are
painted.)
(cherry picked from commit 85d5143c0c33f0b8e4f4ee3b5b83b88a7c2f0844)

M  +4    -0    libs/flake/KoShape.cpp
M  +9    -0    libs/flake/KoShape.h
M  +10   -22   libs/flake/text/KoSvgTextShape.cpp
M  +2    -0    libs/flake/text/KoSvgTextShape.h
M  +34   -0    libs/ui/flake/kis_shape_layer.cc
M  +1    -0    libs/ui/flake/kis_shape_layer.h

https://invent.kde.org/graphics/krita/-/commit/bb839b2855b9c336cb6563f6160050970b0295c8
Comment 8 Alvin Wong 2023-08-08 09:11:26 UTC
Git commit 36dcffd1f5d5dac6bdd89783f721793533f4f796 by Alvin Wong.
Committed on 08/08/2023 at 08:59.
Pushed by alvinwong into branch 'master'.

text: Fix bounding box of bitmap glyphs in vertical mode

This separates the bitmap draw rect (which had been used as the
bounding box) and the bounding box for calculating ascent/descent.
The bounding box now comes from font metrics then combined with the
bitmap rect. Doing this fixes the vertical line height of bitmap glyphs.

M  +32   -14   libs/flake/text/KoSvgTextShape.cpp

https://invent.kde.org/graphics/krita/-/commit/36dcffd1f5d5dac6bdd89783f721793533f4f796
Comment 9 Alvin Wong 2023-08-08 09:12:13 UTC
Git commit be074705f69b9ce930061d1aaa11b4dc9b7bf0fa by Alvin Wong.
Committed on 08/08/2023 at 11:12.
Pushed by alvinwong into branch 'krita/5.2'.

text: Fix bounding box of bitmap glyphs in vertical mode

This separates the bitmap draw rect (which had been used as the
bounding box) and the bounding box for calculating ascent/descent.
The bounding box now comes from font metrics then combined with the
bitmap rect. Doing this fixes the vertical line height of bitmap glyphs.
(cherry picked from commit 36dcffd1f5d5dac6bdd89783f721793533f4f796)

M  +32   -14   libs/flake/text/KoSvgTextShape.cpp

https://invent.kde.org/graphics/krita/-/commit/be074705f69b9ce930061d1aaa11b4dc9b7bf0fa