diff --git a/Userland/Libraries/LibPDF/Fonts/PDFFont.h b/Userland/Libraries/LibPDF/Fonts/PDFFont.h index d322f74562..bed0cb3c0d 100644 --- a/Userland/Libraries/LibPDF/Fonts/PDFFont.h +++ b/Userland/Libraries/LibPDF/Fonts/PDFFont.h @@ -14,6 +14,8 @@ namespace PDF { +class Renderer; + class PDFFont : public RefCounted { public: static PDFErrorOr> create(Document*, NonnullRefPtr const&, float font_size); @@ -21,7 +23,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 word_spacing, float horizontal_scaling) = 0; + virtual PDFErrorOr draw_string(Gfx::Painter&, Gfx::FloatPoint, DeprecatedString const&, Renderer const&) = 0; protected: virtual PDFErrorOr initialize(Document* document, NonnullRefPtr const& dict, float font_size); diff --git a/Userland/Libraries/LibPDF/Fonts/SimpleFont.cpp b/Userland/Libraries/LibPDF/Fonts/SimpleFont.cpp index 69c94f8c8a..8d2cec0fa1 100644 --- a/Userland/Libraries/LibPDF/Fonts/SimpleFont.cpp +++ b/Userland/Libraries/LibPDF/Fonts/SimpleFont.cpp @@ -11,6 +11,7 @@ #include #include #include +#include namespace PDF { @@ -45,8 +46,17 @@ 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 word_spacing, float horizontal_scaling) +PDFErrorOr SimpleFont::draw_string(Gfx::Painter& painter, Gfx::FloatPoint glyph_position, DeprecatedString const& string, Renderer const& renderer) { + Color const& paint_color = renderer.state().paint_color; + + auto const& text_rendering_matrix = renderer.calculate_text_rendering_matrix(); + auto font_size = text_rendering_matrix.x_scale() * renderer.text_state().font_size; + + auto character_spacing = text_rendering_matrix.x_scale() * renderer.text_state().character_spacing; + auto word_spacing = text_rendering_matrix.x_scale() * renderer.text_state().word_spacing; + auto horizontal_scaling = renderer.text_state().horizontal_scaling; + for (auto char_code : string.bytes()) { // Use the width specified in the font's dictionary if available, // and use the default width for the given font otherwise. diff --git a/Userland/Libraries/LibPDF/Fonts/SimpleFont.h b/Userland/Libraries/LibPDF/Fonts/SimpleFont.h index 47673d7671..e1b599e8bc 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 word_spacing, float horizontal_scaling) override; + PDFErrorOr draw_string(Gfx::Painter&, Gfx::FloatPoint, DeprecatedString const&, Renderer const&) 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 184b0999f1..ecb953caef 100644 --- a/Userland/Libraries/LibPDF/Fonts/Type0Font.cpp +++ b/Userland/Libraries/LibPDF/Fonts/Type0Font.cpp @@ -13,15 +13,15 @@ namespace PDF { class CIDFontType { public: virtual ~CIDFontType() = default; - virtual PDFErrorOr draw_string(Gfx::Painter&, Gfx::FloatPoint, DeprecatedString const&, Color const&, float, float, float, float) = 0; + virtual PDFErrorOr draw_string(Gfx::Painter&, Gfx::FloatPoint, DeprecatedString const&) = 0; }; class CIDFontType0 : public CIDFontType { public: - PDFErrorOr draw_string(Gfx::Painter&, Gfx::FloatPoint, DeprecatedString const&, Color const&, float, float, float, float) override; + PDFErrorOr draw_string(Gfx::Painter&, Gfx::FloatPoint, DeprecatedString const&) override; }; -PDFErrorOr CIDFontType0::draw_string(Gfx::Painter&, Gfx::FloatPoint, DeprecatedString const&, Color const&, float, float, float, float) +PDFErrorOr CIDFontType0::draw_string(Gfx::Painter&, Gfx::FloatPoint, DeprecatedString const&) { // ISO 32000 (PDF 2.0) 9.7.4.2 Glyph selection in CIDFonts // "When the CIDFont contains an embedded font program that is represented in the Compact Font Format (CFF), @@ -39,7 +39,7 @@ class CIDFontType2 : public CIDFontType { public: static PDFErrorOr> create(Document*, NonnullRefPtr const& descendant, float font_size); - PDFErrorOr draw_string(Gfx::Painter&, Gfx::FloatPoint, DeprecatedString const&, Color const&, float, float, float, float) override; + PDFErrorOr draw_string(Gfx::Painter&, Gfx::FloatPoint, DeprecatedString const&) override; }; PDFErrorOr> CIDFontType2::create(Document* document, NonnullRefPtr const& descendant, float font_size) @@ -70,7 +70,7 @@ PDFErrorOr> CIDFontType2::create(Document* document, return TRY(adopt_nonnull_own_or_enomem(new (nothrow) CIDFontType2())); } -PDFErrorOr CIDFontType2::draw_string(Gfx::Painter&, Gfx::FloatPoint, DeprecatedString const&, Color const&, float, float, float, float) +PDFErrorOr CIDFontType2::draw_string(Gfx::Painter&, Gfx::FloatPoint, DeprecatedString const&) { // ISO 32000 (PDF 2.0) 9.7.4.2 Glyph selection in CIDFonts // "For Type 2, the CIDFont program is actually a TrueType font program, which has no native notion of CIDs. @@ -175,7 +175,7 @@ void Type0Font::set_font_size(float) { } -PDFErrorOr Type0Font::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) +PDFErrorOr Type0Font::draw_string(Gfx::Painter& painter, Gfx::FloatPoint glyph_position, DeprecatedString const& string, Renderer const&) { // Type0 fonts map bytes to character IDs ("CIDs"), and then CIDs to glyphs. @@ -196,7 +196,7 @@ PDFErrorOr Type0Font::draw_string(Gfx::Painter& painter, Gfx::F // FIXME: Map string data to CIDs, then call m_cid_font_type with CIDs. - return m_cid_font_type->draw_string(painter, glyph_position, string, paint_color, font_size, character_spacing, word_spacing, horizontal_scaling); + return m_cid_font_type->draw_string(painter, glyph_position, string); } } diff --git a/Userland/Libraries/LibPDF/Fonts/Type0Font.h b/Userland/Libraries/LibPDF/Fonts/Type0Font.h index 2db8472a4a..ebaf233fab 100644 --- a/Userland/Libraries/LibPDF/Fonts/Type0Font.h +++ b/Userland/Libraries/LibPDF/Fonts/Type0Font.h @@ -26,7 +26,7 @@ public: ~Type0Font(); void set_font_size(float font_size) override; - PDFErrorOr draw_string(Gfx::Painter&, Gfx::FloatPoint pos, DeprecatedString const&, Color const&, float, float, float, float) override; + PDFErrorOr draw_string(Gfx::Painter&, Gfx::FloatPoint, DeprecatedString const&, Renderer const&) override; DeprecatedFlyString base_font_name() const { return m_base_font_name; } diff --git a/Userland/Libraries/LibPDF/Renderer.cpp b/Userland/Libraries/LibPDF/Renderer.cpp index 37773924eb..08f33a3c1a 100644 --- a/Userland/Libraries/LibPDF/Renderer.cpp +++ b/Userland/Libraries/LibPDF/Renderer.cpp @@ -802,12 +802,10 @@ PDFErrorOr Renderer::show_text(DeprecatedString const& string) if (!text_state().font) return Error::rendering_unsupported_error("Can't draw text because an invalid font was in use"); - auto& text_rendering_matrix = calculate_text_rendering_matrix(); - - auto font_size = text_rendering_matrix.x_scale() * text_state().font_size; + auto const& text_rendering_matrix = calculate_text_rendering_matrix(); 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_rendering_matrix.x_scale(), text_state().word_spacing * text_rendering_matrix.x_scale(), text_state().horizontal_scaling)); + auto end_position = TRY(text_state().font->draw_string(m_painter, start_position, string, *this)); // Update text matrix auto delta_x = end_position.x() - start_position.x(); @@ -983,7 +981,7 @@ PDFErrorOr> Renderer::get_color_space_from_document(No return ColorSpace::create(m_document, color_space_object); } -Gfx::AffineTransform const& Renderer::calculate_text_rendering_matrix() +Gfx::AffineTransform const& Renderer::calculate_text_rendering_matrix() const { if (m_text_rendering_matrix_is_dirty) { m_text_rendering_matrix = Gfx::AffineTransform( diff --git a/Userland/Libraries/LibPDF/Renderer.h b/Userland/Libraries/LibPDF/Renderer.h index 21250a82f3..53ab30e425 100644 --- a/Userland/Libraries/LibPDF/Renderer.h +++ b/Userland/Libraries/LibPDF/Renderer.h @@ -107,6 +107,10 @@ public: bool operator==(FontCacheKey const&) const = default; }; + ALWAYS_INLINE GraphicsState const& state() const { return m_graphics_state_stack.last(); } + ALWAYS_INLINE TextState const& text_state() const { return state().text_state; } + + Gfx::AffineTransform const& calculate_text_rendering_matrix() const; private: Renderer(RefPtr, Page const&, RefPtr, RenderingPreferences); @@ -130,9 +134,7 @@ private: PDFErrorOr> get_color_space_from_resources(Value const&, NonnullRefPtr); PDFErrorOr> get_color_space_from_document(NonnullRefPtr); - ALWAYS_INLINE GraphicsState const& state() const { return m_graphics_state_stack.last(); } ALWAYS_INLINE GraphicsState& state() { return m_graphics_state_stack.last(); } - ALWAYS_INLINE TextState const& text_state() const { return state().text_state; } ALWAYS_INLINE TextState& text_state() { return state().text_state; } template @@ -144,7 +146,6 @@ private: template ALWAYS_INLINE Gfx::Rect map(Gfx::Rect) const; - Gfx::AffineTransform const& calculate_text_rendering_matrix(); Gfx::AffineTransform calculate_image_space_transformation(int width, int height); PDFErrorOr> get_font(FontCacheKey const&); @@ -161,8 +162,8 @@ private: Gfx::AffineTransform m_text_matrix; Gfx::AffineTransform m_text_line_matrix; - bool m_text_rendering_matrix_is_dirty { true }; - Gfx::AffineTransform m_text_rendering_matrix; + bool mutable m_text_rendering_matrix_is_dirty { true }; + Gfx::AffineTransform mutable m_text_rendering_matrix; HashMap> m_font_cache; };