Browse Source

LibPDF: Store a PDFFont in the Renderer's text state

Matthew Olsson 3 years ago
parent
commit
9a4a3318a9
2 changed files with 9 additions and 4 deletions
  1. 7 4
      Userland/Libraries/LibPDF/Renderer.cpp
  2. 2 0
      Userland/Libraries/LibPDF/Renderer.h

+ 7 - 4
Userland/Libraries/LibPDF/Renderer.cpp

@@ -6,6 +6,7 @@
 
 #include <AK/Utf8View.h>
 #include <LibPDF/CommonNames.h>
+#include <LibPDF/Fonts.h>
 #include <LibPDF/Renderer.h>
 
 #define RENDERER_HANDLER(name) \
@@ -340,9 +341,11 @@ RENDERER_HANDLER(text_set_font)
     auto target_font_name = MUST(m_document->resolve_to<NameObject>(args[0]))->name();
     auto fonts_dictionary = MUST(m_page.resources->get_dict(m_document, CommonNames::Font));
     auto font_dictionary = MUST(fonts_dictionary->get_dict(m_document, target_font_name));
+    auto font = TRY(PDFFont::create(m_document, font_dictionary));
+    text_state().font = font;
 
     // FIXME: We do not yet have the standard 14 fonts, as some of them are not open fonts,
-    // so we just use LiberationSerif for everything
+    //        so we just use LiberationSerif for everything
 
     auto font_name = MUST(font_dictionary->get_name(m_document, CommonNames::BaseFont))->name().to_lowercase();
     auto font_view = font_name.view();
@@ -614,8 +617,6 @@ PDFErrorOr<void> Renderer::set_graphics_state_from_dict(NonnullRefPtr<DictObject
 
 void Renderer::show_text(String const& string, float shift)
 {
-    auto utf = Utf8View(string);
-
     auto& text_rendering_matrix = calculate_text_rendering_matrix();
 
     auto font_size = static_cast<int>(text_rendering_matrix.x_scale() * text_state().font_size);
@@ -628,7 +629,9 @@ void Renderer::show_text(String const& string, float shift)
 
     auto original_position = glyph_position;
 
-    for (auto code_point : utf) {
+    for (auto char_code : string.bytes()) {
+        auto code_point = text_state().font->char_code_to_code_point(char_code);
+
         if (code_point != 0x20)
             m_painter.draw_glyph(glyph_position.to_type<int>(), code_point, *font, state().paint_color);
 

+ 2 - 0
Userland/Libraries/LibPDF/Renderer.h

@@ -18,6 +18,7 @@
 #include <LibGfx/Size.h>
 #include <LibPDF/ColorSpace.h>
 #include <LibPDF/Document.h>
+#include <LibPDF/Fonts.h>
 #include <LibPDF/Object.h>
 
 namespace PDF {
@@ -58,6 +59,7 @@ struct TextState {
     FlyString font_family { "Liberation Serif" };
     String font_variant { "Regular" };
     float font_size { 12.0f };
+    RefPtr<PDFFont> font;
     TextRenderingMode rendering_mode { TextRenderingMode::Fill };
     float rise { 0.0f };
     bool knockout { true };