From 4f4bd3793faa91c3532656536536d431764acdee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian=20Offenh=C3=A4user?= Date: Thu, 9 Feb 2023 14:16:22 +0100 Subject: [PATCH] LibPDF: Fix glyph sizing bug that caused incorrect spacing When loading OpenType fonts, either as a replacement for the standard 14 fonts or an embedded one, we previously passed the font size as the _point_ size to the loader class. The difference is quite subtle, being that Gfx::ScaledFont uses the optional dpi parameter to convert the input from inches to pixels. This meant that our glyphs were exactly 1.333% too large, causing them to overlap in places. --- Userland/Libraries/LibPDF/Fonts/PDFFont.cpp | 9 +++++---- Userland/Libraries/LibPDF/Fonts/TrueTypeFont.cpp | 14 +++++++------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/Userland/Libraries/LibPDF/Fonts/PDFFont.cpp b/Userland/Libraries/LibPDF/Fonts/PDFFont.cpp index 6fb17c17ae..5fdf3a4b63 100644 --- a/Userland/Libraries/LibPDF/Fonts/PDFFont.cpp +++ b/Userland/Libraries/LibPDF/Fonts/PDFFont.cpp @@ -33,10 +33,11 @@ static bool is_standard_latin_font(DeprecatedFlyString const& font) PDFErrorOr PDFFont::CommonData::load_from_dict(Document* document, NonnullRefPtr dict, float font_size) { - base_font_name = TRY(dict->get_name(document, CommonNames::BaseFont))->name(); - if ((is_standard_font = is_standard_latin_font(base_font_name))) { - auto replacement = replacement_for_standard_latin_font(base_font_name.to_lowercase()); - font = Gfx::FontDatabase::the().get(replacement.get<0>(), replacement.get<1>(), font_size); + auto base_font = TRY(dict->get_name(document, CommonNames::BaseFont))->name(); + if ((is_standard_font = is_standard_latin_font(base_font))) { + auto replacement = replacement_for_standard_latin_font(base_font); + float point_size = (font_size * POINTS_PER_INCH) / DEFAULT_DPI; + font = Gfx::FontDatabase::the().get(replacement.get<0>(), replacement.get<1>(), point_size); VERIFY(font); } diff --git a/Userland/Libraries/LibPDF/Fonts/TrueTypeFont.cpp b/Userland/Libraries/LibPDF/Fonts/TrueTypeFont.cpp index 527f11bd6c..5cd2bc7bee 100644 --- a/Userland/Libraries/LibPDF/Fonts/TrueTypeFont.cpp +++ b/Userland/Libraries/LibPDF/Fonts/TrueTypeFont.cpp @@ -19,13 +19,13 @@ PDFErrorOr TrueTypeFont::parse_data(Document* document, Non TRY(data.load_from_dict(document, dict, font_size)); if (!data.is_standard_font) { - auto descriptor = MUST(dict->get_dict(document, CommonNames::FontDescriptor)); - if (!descriptor->contains(CommonNames::FontFile2)) - return data; - - auto font_file_stream = TRY(descriptor->get_stream(document, CommonNames::FontFile2)); - auto ttf_font = TRY(OpenType::Font::try_load_from_externally_owned_memory(font_file_stream->bytes())); - data.font = adopt_ref(*new Gfx::ScaledFont(*ttf_font, font_size, font_size)); + auto descriptor = TRY(dict->get_dict(document, CommonNames::FontDescriptor)); + if (descriptor->contains(CommonNames::FontFile2)) { + auto font_file_stream = TRY(descriptor->get_stream(document, CommonNames::FontFile2)); + auto ttf_font = TRY(OpenType::Font::try_load_from_externally_owned_memory(font_file_stream->bytes())); + float point_size = (font_size * POINTS_PER_INCH) / DEFAULT_DPI; + data.font = adopt_ref(*new Gfx::ScaledFont(*ttf_font, point_size, point_size)); + } } return data;