From 9283c939bbb5edda1db93cbf9c4c89e4cf555b6f Mon Sep 17 00:00:00 2001 From: Nico Weber Date: Thu, 20 Jul 2023 21:43:00 -0400 Subject: [PATCH] LibPDF: Include `width` in Type1Font glyph cache key LibGfx's ScaledFont doesn't do this, but in ScaledFont m_x_scale and m_y_scale are immutable once the class is created, so it can get away with not doing it. In Type1Font, `width` changes in different calls to Type1Font::draw_glyph(), so we need to make it part of the cache key. Fixes rendering of the word "Version" on the first page of pdf_reference_1-7.pdf. --- Userland/Libraries/LibPDF/Fonts/Type1Font.cpp | 2 +- Userland/Libraries/LibPDF/Fonts/Type1Font.h | 22 ++++++++++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/Userland/Libraries/LibPDF/Fonts/Type1Font.cpp b/Userland/Libraries/LibPDF/Fonts/Type1Font.cpp index 6dad4fcbb6..b9c4e94494 100644 --- a/Userland/Libraries/LibPDF/Fonts/Type1Font.cpp +++ b/Userland/Libraries/LibPDF/Fonts/Type1Font.cpp @@ -79,7 +79,7 @@ void Type1Font::draw_glyph(Gfx::Painter& painter, Gfx::FloatPoint point, float w point = point.translated(translation); auto glyph_position = Gfx::GlyphRasterPosition::get_nearest_fit_for(point); - Gfx::GlyphIndexWithSubpixelOffset index { char_code, glyph_position.subpixel_offset }; + Type1GlyphCacheKey index { char_code, glyph_position.subpixel_offset, width }; RefPtr bitmap; auto maybe_bitmap = m_glyph_cache.get(index); diff --git a/Userland/Libraries/LibPDF/Fonts/Type1Font.h b/Userland/Libraries/LibPDF/Fonts/Type1Font.h index ced7adce69..911626f6e1 100644 --- a/Userland/Libraries/LibPDF/Fonts/Type1Font.h +++ b/Userland/Libraries/LibPDF/Fonts/Type1Font.h @@ -12,6 +12,14 @@ namespace PDF { +struct Type1GlyphCacheKey { + u32 glyph_id; + Gfx::GlyphSubpixelOffset subpixel_offset; + float width; + + bool operator==(Type1GlyphCacheKey const&) const = default; +}; + class Type1Font : public SimpleFont { public: float get_glyph_width(u8 char_code) const override; @@ -25,7 +33,19 @@ protected: private: RefPtr m_font_program; RefPtr m_font; - HashMap> m_glyph_cache; + HashMap> m_glyph_cache; +}; + +} + +namespace AK { + +template<> +struct Traits : public GenericTraits { + static unsigned hash(PDF::Type1GlyphCacheKey const& index) + { + return pair_int_hash(pair_int_hash(index.glyph_id, (index.subpixel_offset.x << 8) | index.subpixel_offset.y), int_hash(bit_cast(index.width))); + } }; }