From c142dadbe8d00221da414f77f3f30a311373ec34 Mon Sep 17 00:00:00 2001 From: Matthew Olsson Date: Thu, 27 May 2021 14:53:10 -0700 Subject: [PATCH] LibPDF: Handle the TJ graphical operator --- Userland/Libraries/LibPDF/Renderer.cpp | 24 +++++++++++++++++++++--- Userland/Libraries/LibPDF/Renderer.h | 2 +- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/Userland/Libraries/LibPDF/Renderer.cpp b/Userland/Libraries/LibPDF/Renderer.cpp index 69187e8296..efaa2c3cb1 100644 --- a/Userland/Libraries/LibPDF/Renderer.cpp +++ b/Userland/Libraries/LibPDF/Renderer.cpp @@ -385,7 +385,25 @@ RENDERER_HANDLER(text_next_line_show_string) } RENDERER_TODO(text_next_line_show_string_set_spacing); -RENDERER_TODO(text_show_string_array); + +RENDERER_HANDLER(text_show_string_array) +{ + auto elements = m_document->resolve_to(args[0])->elements(); + float next_shift = 0.0f; + + for (auto& element : elements) { + if (element.is_number()) { + next_shift = element.to_float(); + } else { + VERIFY(element.is_object()); + auto obj = element.as_object(); + VERIFY(obj->is_string()); + auto str = object_cast(obj)->string(); + show_text(str, next_shift); + } + } +} + RENDERER_TODO(type3_font_set_glyph_width); RENDERER_TODO(type3_font_set_glyph_width_and_bbox); @@ -504,7 +522,7 @@ void Renderer::set_graphics_state_from_dict(NonnullRefPtr dict) handle_set_flatness_tolerance({ dict->get_value(CommonNames::FL) }); } -void Renderer::show_text(const String& string, int shift) +void Renderer::show_text(const String& string, float shift) { auto utf = Utf8View(string); auto& font = text_state().font; @@ -522,7 +540,7 @@ void Renderer::show_text(const String& string, int shift) m_painter.draw_glyph(text_position.to_type(), code_point, *text_state().font, state().paint_color); auto glyph_width = static_cast(font->glyph_width(code_point)); - auto tx = (glyph_width - static_cast(shift) / 1000.0f); + auto tx = (glyph_width - shift / 1000.0f); tx += text_state().character_spacing; if (code_point == ' ') diff --git a/Userland/Libraries/LibPDF/Renderer.h b/Userland/Libraries/LibPDF/Renderer.h index 9e662f0e83..b1f61eae4f 100644 --- a/Userland/Libraries/LibPDF/Renderer.h +++ b/Userland/Libraries/LibPDF/Renderer.h @@ -94,7 +94,7 @@ private: void set_graphics_state_from_dict(NonnullRefPtr); // shift is the manual advance given in the TJ command array - void show_text(const String&, int shift = 0); + void show_text(const String&, float shift = 0.0f); RefPtr get_color_space(const Value&); ALWAYS_INLINE const GraphicsState& state() const { return m_graphics_state_stack.last(); }