From 3eaa27f53ad5f6d99e984d693085dce9cf51261b Mon Sep 17 00:00:00 2001 From: Rodrigo Tobar Date: Sun, 5 Feb 2023 14:23:07 +0800 Subject: [PATCH] LibPDF: Add infrastructure for accented character glyphs Type1 accented character glyphs are composed of two other glyphs in the same font: a base glyph and an accent glyph, given as char codes in the standard encoding. These two glyphs are then composed together to form the accented character. This commit adds the data structures to hold the information for accented characters, and also the routine that composes the final glyph path out of the two individual components. All glyphs must have been loaded by the time this composition takes place, and thus a new protected consolidate_glyphs() routine has been added to perform this calculation. --- Userland/Libraries/LibPDF/Fonts/CFF.cpp | 1 + .../Libraries/LibPDF/Fonts/PS1FontProgram.cpp | 1 + .../LibPDF/Fonts/Type1FontProgram.cpp | 18 +++++++++++++++ .../Libraries/LibPDF/Fonts/Type1FontProgram.h | 23 +++++++++++++++++++ 4 files changed, 43 insertions(+) diff --git a/Userland/Libraries/LibPDF/Fonts/CFF.cpp b/Userland/Libraries/LibPDF/Fonts/CFF.cpp index eade5fc207..9c8ba8fe9c 100644 --- a/Userland/Libraries/LibPDF/Fonts/CFF.cpp +++ b/Userland/Libraries/LibPDF/Fonts/CFF.cpp @@ -116,6 +116,7 @@ PDFErrorOr> CFF::create(ReadonlyBytes const& cff_bytes, RefPt auto const& name = charset[i - 1]; TRY(cff->add_glyph(name, move(glyphs[i]))); } + cff->consolidate_glyphs(); // Encoding given or read if (encoding) { diff --git a/Userland/Libraries/LibPDF/Fonts/PS1FontProgram.cpp b/Userland/Libraries/LibPDF/Fonts/PS1FontProgram.cpp index 38e87a5b55..5676e5ff9e 100644 --- a/Userland/Libraries/LibPDF/Fonts/PS1FontProgram.cpp +++ b/Userland/Libraries/LibPDF/Fonts/PS1FontProgram.cpp @@ -98,6 +98,7 @@ PDFErrorOr PS1FontProgram::parse_encrypted_portion(ByteBuffer const& buffe } } + consolidate_glyphs(); return {}; } diff --git a/Userland/Libraries/LibPDF/Fonts/Type1FontProgram.cpp b/Userland/Libraries/LibPDF/Fonts/Type1FontProgram.cpp index 19b6a2075f..649fec3728 100644 --- a/Userland/Libraries/LibPDF/Fonts/Type1FontProgram.cpp +++ b/Userland/Libraries/LibPDF/Fonts/Type1FontProgram.cpp @@ -110,6 +110,24 @@ Gfx::AffineTransform Type1FontProgram::glyph_transform_to_device_space(Glyph con return transform; } +void Type1FontProgram::consolidate_glyphs() +{ + for (auto& [name, glyph] : m_glyph_map) { + if (!glyph.is_accented_character()) + continue; + auto maybe_base_glyph = m_glyph_map.get(glyph.accented_character().base_character); + if (!maybe_base_glyph.has_value()) + continue; + auto glyph_path = maybe_base_glyph.value().path(); + auto maybe_accent_glyph = m_glyph_map.get(glyph.accented_character().accent_character); + if (maybe_accent_glyph.has_value()) { + auto path = maybe_accent_glyph.value().path(); + glyph_path.append_path(move(path)); + } + glyph.path() = glyph_path; + } +} + PDFErrorOr Type1FontProgram::parse_glyph(ReadonlyBytes const& data, Vector const& subroutines, GlyphParserState& state, bool is_type2) { auto push = [&](float value) -> PDFErrorOr { diff --git a/Userland/Libraries/LibPDF/Fonts/Type1FontProgram.h b/Userland/Libraries/LibPDF/Fonts/Type1FontProgram.h index 7509f23be6..c06ed5c2c7 100644 --- a/Userland/Libraries/LibPDF/Fonts/Type1FontProgram.h +++ b/Userland/Libraries/LibPDF/Fonts/Type1FontProgram.h @@ -25,6 +25,19 @@ public: RefPtr encoding() const { return m_encoding; } protected: + struct AccentedCharacter { + AccentedCharacter(u8 base_char_code, u8 accent_char_code, float adx, float ady) + : base_character(Encoding::standard_encoding()->get_name(base_char_code)) + , accent_character(Encoding::standard_encoding()->get_name(accent_char_code)) + , accent_origin(adx, ady) + { + } + + DeprecatedFlyString base_character; + DeprecatedFlyString accent_character; + Gfx::FloatPoint accent_origin; + }; + class Glyph { public: @@ -38,9 +51,17 @@ protected: Gfx::Path& path() { return m_path; } Gfx::Path const& path() const { return m_path; } + bool is_accented_character() const { return m_accented_character.has_value(); } + AccentedCharacter const& accented_character() const { return m_accented_character.value(); } + void set_accented_character(AccentedCharacter&& accented_character) + { + m_accented_character = move(accented_character); + } + private: Gfx::Path m_path; Optional m_width; + Optional m_accented_character; }; struct GlyphParserState { @@ -86,6 +107,8 @@ protected: return {}; } + void consolidate_glyphs(); + private: HashMap m_glyph_map; Gfx::AffineTransform m_font_matrix;