From e3cc05b93553fbb7fd59a9c9afe23c8876c86132 Mon Sep 17 00:00:00 2001 From: Nico Weber Date: Sat, 22 Jul 2023 10:38:52 -0400 Subject: [PATCH] LibPDF: Don't ignore word_spacing --- Userland/Libraries/LibPDF/Fonts/PDFFont.h | 2 +- Userland/Libraries/LibPDF/Fonts/SimpleFont.cpp | 11 ++++++++++- Userland/Libraries/LibPDF/Fonts/SimpleFont.h | 2 +- Userland/Libraries/LibPDF/Fonts/Type0Font.cpp | 2 +- Userland/Libraries/LibPDF/Fonts/Type0Font.h | 2 +- Userland/Libraries/LibPDF/Renderer.cpp | 2 +- 6 files changed, 15 insertions(+), 6 deletions(-) diff --git a/Userland/Libraries/LibPDF/Fonts/PDFFont.h b/Userland/Libraries/LibPDF/Fonts/PDFFont.h index 99d4f20f7f..0ec890c557 100644 --- a/Userland/Libraries/LibPDF/Fonts/PDFFont.h +++ b/Userland/Libraries/LibPDF/Fonts/PDFFont.h @@ -27,7 +27,7 @@ public: virtual ~PDFFont() = default; virtual void set_font_size(float font_size) = 0; - virtual PDFErrorOr draw_string(Gfx::Painter&, Gfx::FloatPoint, DeprecatedString const&, Color const&, float font_size, float character_spacing, float horizontal_scaling) = 0; + virtual PDFErrorOr draw_string(Gfx::Painter&, Gfx::FloatPoint, DeprecatedString const&, Color const&, float font_size, float character_spacing, float word_spacing, float horizontal_scaling) = 0; virtual Type type() const = 0; DeprecatedFlyString base_font_name() const { return m_base_font_name; } diff --git a/Userland/Libraries/LibPDF/Fonts/SimpleFont.cpp b/Userland/Libraries/LibPDF/Fonts/SimpleFont.cpp index 81fd1190bb..f54e92a7ea 100644 --- a/Userland/Libraries/LibPDF/Fonts/SimpleFont.cpp +++ b/Userland/Libraries/LibPDF/Fonts/SimpleFont.cpp @@ -45,7 +45,7 @@ PDFErrorOr SimpleFont::initialize(Document* document, NonnullRefPtr SimpleFont::draw_string(Gfx::Painter& painter, Gfx::FloatPoint glyph_position, DeprecatedString const& string, Color const& paint_color, float font_size, float character_spacing, float horizontal_scaling) +PDFErrorOr SimpleFont::draw_string(Gfx::Painter& painter, Gfx::FloatPoint glyph_position, DeprecatedString const& string, Color const& paint_color, float font_size, float character_spacing, float word_spacing, float horizontal_scaling) { for (auto char_code : string.bytes()) { // Use the width specified in the font's dictionary if available, @@ -57,8 +57,17 @@ PDFErrorOr SimpleFont::draw_string(Gfx::Painter& painter, Gfx:: glyph_width = get_glyph_width(char_code); draw_glyph(painter, glyph_position, glyph_width, char_code, paint_color); + auto tx = glyph_width; tx += character_spacing; + + // ISO 32000 (PDF 2.0), 9.3.3 Wordspacing + // "Word spacing shall be applied to every occurrence of the single-byte character code 32 + // in a string when using a simple font (including Type 3) or a composite font that defines + // code 32 as a single-byte code." + if (char_code == ' ') + tx += word_spacing; + tx *= horizontal_scaling; glyph_position += { tx, 0.0f }; } diff --git a/Userland/Libraries/LibPDF/Fonts/SimpleFont.h b/Userland/Libraries/LibPDF/Fonts/SimpleFont.h index 8137a2ef46..3c8745293a 100644 --- a/Userland/Libraries/LibPDF/Fonts/SimpleFont.h +++ b/Userland/Libraries/LibPDF/Fonts/SimpleFont.h @@ -13,7 +13,7 @@ namespace PDF { class SimpleFont : public PDFFont { public: - PDFErrorOr draw_string(Gfx::Painter&, Gfx::FloatPoint, DeprecatedString const&, Color const&, float font_size, float character_spacing, float horizontal_scaling) override; + PDFErrorOr draw_string(Gfx::Painter&, Gfx::FloatPoint, DeprecatedString const&, Color const&, float font_size, float character_spacing, float word_spacing, float horizontal_scaling) override; protected: PDFErrorOr initialize(Document* document, NonnullRefPtr const& dict, float font_size) override; diff --git a/Userland/Libraries/LibPDF/Fonts/Type0Font.cpp b/Userland/Libraries/LibPDF/Fonts/Type0Font.cpp index 62f04ad4ac..0e31fdb377 100644 --- a/Userland/Libraries/LibPDF/Fonts/Type0Font.cpp +++ b/Userland/Libraries/LibPDF/Fonts/Type0Font.cpp @@ -91,7 +91,7 @@ void Type0Font::set_font_size(float) { } -PDFErrorOr Type0Font::draw_string(Gfx::Painter&, Gfx::FloatPoint, DeprecatedString const&, Color const&, float, float, float) +PDFErrorOr Type0Font::draw_string(Gfx::Painter&, Gfx::FloatPoint, DeprecatedString const&, Color const&, float, float, float, float) { return Error::rendering_unsupported_error("Type0 font not implemented yet"); } diff --git a/Userland/Libraries/LibPDF/Fonts/Type0Font.h b/Userland/Libraries/LibPDF/Fonts/Type0Font.h index 078fa4c21a..50e25c8282 100644 --- a/Userland/Libraries/LibPDF/Fonts/Type0Font.h +++ b/Userland/Libraries/LibPDF/Fonts/Type0Font.h @@ -20,7 +20,7 @@ struct CIDSystemInfo { class Type0Font : public PDFFont { public: void set_font_size(float font_size) override; - PDFErrorOr draw_string(Gfx::Painter&, Gfx::FloatPoint pos, DeprecatedString const&, Color const&, float, float, float) override; + PDFErrorOr draw_string(Gfx::Painter&, Gfx::FloatPoint pos, DeprecatedString const&, Color const&, float, float, float, float) override; Type type() const override { return PDFFont::Type::Type0; } protected: diff --git a/Userland/Libraries/LibPDF/Renderer.cpp b/Userland/Libraries/LibPDF/Renderer.cpp index e33da38799..692422ac62 100644 --- a/Userland/Libraries/LibPDF/Renderer.cpp +++ b/Userland/Libraries/LibPDF/Renderer.cpp @@ -754,7 +754,7 @@ PDFErrorOr Renderer::show_text(DeprecatedString const& string) auto font_size = text_rendering_matrix.x_scale() * text_state().font_size; auto start_position = text_rendering_matrix.map(Gfx::FloatPoint { 0.0f, 0.0f }); - auto end_position = TRY(text_state().font->draw_string(m_painter, start_position, string, state().paint_color, font_size, text_state().character_spacing, text_state().horizontal_scaling)); + auto end_position = TRY(text_state().font->draw_string(m_painter, start_position, string, state().paint_color, font_size, text_state().character_spacing, text_state().word_spacing, text_state().horizontal_scaling)); // Update text matrix auto delta_x = end_position.x() - start_position.x();