From 384c6cf0f94c915471bdc5aa189d1761ce7c7f86 Mon Sep 17 00:00:00 2001 From: Nico Weber Date: Wed, 31 Jan 2024 21:08:01 -0500 Subject: [PATCH] LibPDF: Tweak vertical position of truetype fonts again See #22821 for a previous attempt. This attempt should settle things once and for all. The opentype render path adjusts by `-font_ascender * -y_scale` in Glyf::Glyph::append_simple_path(), so that's what we need to undo to draw at the font's baseline. (OpenType::Font::metrics() returns ascender scaled by y_scale already, so no need to have the scale here where we undo the shift.) Previously, we called `baseline()` which just returns the font's font size, which is pretty meaningless: https://tonsky.me/blog/font-size/ https://simoncozens.github.io/fonts-and-layout/opentype.html#vertical-metrics-hhea-and-os2 Also, conceptually it makes sense to translate up by the ascender to get from the upper edge of the glyph to the baseline. --- Userland/Libraries/LibPDF/Fonts/TrueTypeFont.cpp | 4 ++-- Userland/Libraries/LibPDF/Fonts/Type1Font.cpp | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Userland/Libraries/LibPDF/Fonts/TrueTypeFont.cpp b/Userland/Libraries/LibPDF/Fonts/TrueTypeFont.cpp index 2941003315..2e61285446 100644 --- a/Userland/Libraries/LibPDF/Fonts/TrueTypeFont.cpp +++ b/Userland/Libraries/LibPDF/Fonts/TrueTypeFont.cpp @@ -52,8 +52,8 @@ PDFErrorOr TrueTypeFont::draw_glyph(Gfx::Painter& painter, Gfx::FloatPoint { auto style = renderer.state().paint_style; - // Account for the reversed font baseline - auto position = point.translated(0, -m_font->pixel_metrics().descent - m_font->baseline()); + // Undo shift in Glyf::Glyph::append_simple_path() via OpenType::Font::rasterize_glyph(). + auto position = point.translated(0, -m_font->pixel_metrics().ascent); if (style.has()) { painter.draw_glyph(position, char_code, *m_font, style.get()); diff --git a/Userland/Libraries/LibPDF/Fonts/Type1Font.cpp b/Userland/Libraries/LibPDF/Fonts/Type1Font.cpp index 2d4b5244d2..9cb62b2357 100644 --- a/Userland/Libraries/LibPDF/Fonts/Type1Font.cpp +++ b/Userland/Libraries/LibPDF/Fonts/Type1Font.cpp @@ -70,8 +70,8 @@ PDFErrorOr Type1Font::draw_glyph(Gfx::Painter& painter, Gfx::FloatPoint po auto style = renderer.state().paint_style; if (!m_font_program) { - // Account for the reversed font baseline - auto position = point.translated(0, -m_font->pixel_metrics().descent - m_font->baseline()); + // Undo shift in Glyf::Glyph::append_simple_path() via OpenType::Font::rasterize_glyph(). + auto position = point.translated(0, -m_font->pixel_metrics().ascent); // FIXME: Bounding box and sample point look to be pretty wrong if (style.has()) { painter.draw_glyph(position, char_code, *m_font, style.get());