Skip to content

Truly independent web browser

LibGfx: Negate HarfBuzz’s vertical glyph offsets

sideshowbarker3c3783ccd1f0
diff --git a/Libraries/LibGfx/TextLayout.cpp b/Libraries/LibGfx/TextLayout.cpp
index c4fbf6625e9..6556c8c97fe 100644
--- a/Libraries/LibGfx/TextLayout.cpp
+++ b/Libraries/LibGfx/TextLayout.cpp
@@ -504,11 +504,13 @@ static ShapedGlyphs build_origin_relative_shape(Utf16View const& string, Font co
return !Unicode::code_point_has_default_ignorable_code_point_property(string.code_point_at(starting_offset));
};
+ // NB: HarfBuzz positions glyphs in font space, where y grows upward, so its y offsets and advances are negated
+ // for our coordinates, where y grows downward.
for (size_t i = 0; i < glyph_count; ++i) {
bool should_paint = should_paint_glyph(i);
auto position = point
- FloatPoint { 0, metrics.ascent }
- + FloatPoint { positions[i].x_offset, positions[i].y_offset } / text_shaping_resolution;
+ + FloatPoint { positions[i].x_offset, -positions[i].y_offset } / text_shaping_resolution;
auto extra_advance = letter_spacing;
if (auto starting_offset = glyph_info[i].cluster; starting_offset < string.length_in_code_units()
@@ -528,7 +530,7 @@ static ShapedGlyphs build_origin_relative_shape(Utf16View const& string, Font co
if (!should_paint)
continue;
- point += FloatPoint { positions[i].x_advance, positions[i].y_advance } / text_shaping_resolution;
+ point += FloatPoint { positions[i].x_advance, -positions[i].y_advance } / text_shaping_resolution;
// NOTE: The spec says that we "really should not" apply letter-spacing to the trailing edge of a line but
// other browsers do so we will as well. https://drafts.csswg.org/css-text/#example-7880704e
diff --git a/Tests/LibWeb/Assets/NotoSansArabic-yeh-subset.ttf b/Tests/LibWeb/Assets/NotoSansArabic-yeh-subset.ttf
new file mode 100644
index 00000000000..c31785ce82f
Binary files /dev/null and b/Tests/LibWeb/Assets/NotoSansArabic-yeh-subset.ttf differ
diff --git a/Tests/LibWeb/Screenshot/expected/text-direction.png b/Tests/LibWeb/Screenshot/expected/text-direction.png
index a45a931e341..dd25e94fd8e 100644
Binary files a/Tests/LibWeb/Screenshot/expected/text-direction.png and b/Tests/LibWeb/Screenshot/expected/text-direction.png differ
diff --git a/Tests/LibWeb/Text/expected/canvas/measure-text-mark-offset-below-base.txt b/Tests/LibWeb/Text/expected/canvas/measure-text-mark-offset-below-base.txt
new file mode 100644
index 00000000000..d11b9cc8773
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/canvas/measure-text-mark-offset-below-base.txt
@@ -0,0 +1,2 @@
+yeh: descent 43
+dotless yeh: descent 23
diff --git a/Tests/LibWeb/Text/input/canvas/measure-text-mark-offset-below-base.html b/Tests/LibWeb/Text/input/canvas/measure-text-mark-offset-below-base.html
new file mode 100644
index 00000000000..a64529193cb
--- /dev/null
+++ b/Tests/LibWeb/Text/input/canvas/measure-text-mark-offset-below-base.html
@@ -0,0 +1,16 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+ promiseTest(async () => {
+ // In this font, the yeh is the dotless yeh followed by a mark for its two dots, which shaping offsets 0.232em
+ // down, so they hang 0.434em below the baseline, where the dotless yeh's ink stops 0.232em below it.
+ const face = new FontFace("YehSubset", 'url("../../../Assets/NotoSansArabic-yeh-subset.ttf")');
+ document.fonts.add(face);
+ await face.load();
+
+ const context = document.createElement("canvas").getContext("2d");
+ context.font = "100px YehSubset";
+ for (const [name, text] of [["yeh", "ي"], ["dotless yeh", "ى"]])
+ println(`${name}: descent ${Math.round(context.measureText(text).actualBoundingBoxDescent)}`);
+ });
+</script>