summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibPDF/Renderer.cpp
diff options
context:
space:
mode:
authorRodrigo Tobar <rtobarc@gmail.com>2023-02-01 00:52:23 +0800
committerLinus Groh <mail@linusgroh.de>2023-02-24 20:16:50 +0100
commitcb04e4e9da7b26980e16a974e2a4ff7bb9810b99 (patch)
treee7d1f98bd615693e000b0cff3ad20e2127aaae35 /Userland/Libraries/LibPDF/Renderer.cpp
parente8f1a2ef025c6ba0e74697fd5c996dd67341c602 (diff)
downloadserenity-cb04e4e9da7b26980e16a974e2a4ff7bb9810b99.zip
LibPDF: Refactor *Font classes
The PDFFont class hierarchy was very simple (a top-level PDFFont class, followed by all the children classes that derived directly from it). While this design was good enough for some things, it didn't correctly model the actual organization of font types: * PDF fonts are first divided between "simple" and "composite" fonts. The latter is the Type0 font, while the rest are all simple. * PDF fonts yield a glyph per "character code". Simple fonts char codes are always 1 byte long, while Type0 char codes are of variable size. To this effect, this commit changes the hierarchy of Font classes, introducing a new SimpleFont class, deriving from PDFFont, and acting as the parent of Type1Font and TrueTypeFont, while Type0 still derives from PDFFont directly. This distinction allows us now to: * Model string rendering differently from simple and composite fonts: PDFFont now offers a generic draw_string method that takes a whole string to be rendered instead of a single char code. SimpleFont implements this as a loop over individual bytes of the string, with T1 and TT implementing draw_glyph for drawing a single char code. * Some common fields between T1 and TT fonts now live under SimpleFont instead of under PDFfont, where they previously resided. * Some other interfaces specific to SimpleFont have been cleaned up, with u16/u32 not appearing on these classes (or in PDFFont) anymore. * Type0Font's rendering still remains unimplemented. As part of this exercise I also took the chance to perform the following cleanups and restructurings: * Refactored the creation and initialisation of fonts. They are all centrally created at PDFFont::create, with a virtual "initialize" method that allows them to initialise their inner members in the correct order (parent first, child later) after creation. * Removed duplicated code. * Cleaned up some public interfaces: receive const refs, removed unnecessary ctro/dtors, etc. * Slightly changed how Type1 and TrueType fonts are implemented: if there's an embedded font that takes priority, otherwise we always look for a replacement. * This means we don't do anything special for the standard fonts. The only behavior previously associated to standard fonts was choosing an encoding, and even that was under questioning.
Diffstat (limited to 'Userland/Libraries/LibPDF/Renderer.cpp')
-rw-r--r--Userland/Libraries/LibPDF/Renderer.cpp17
1 files changed, 3 insertions, 14 deletions
diff --git a/Userland/Libraries/LibPDF/Renderer.cpp b/Userland/Libraries/LibPDF/Renderer.cpp
index 91b93ed0de..98f2d2a206 100644
--- a/Userland/Libraries/LibPDF/Renderer.cpp
+++ b/Userland/Libraries/LibPDF/Renderer.cpp
@@ -730,22 +730,11 @@ PDFErrorOr<void> Renderer::show_text(DeprecatedString const& string)
auto font_size = text_rendering_matrix.x_scale() * text_state().font_size;
- auto glyph_position = text_rendering_matrix.map(Gfx::FloatPoint { 0.0f, 0.0f });
-
- auto original_position = glyph_position;
-
- for (auto char_code : string.bytes()) {
- auto char_width = text_state().font->get_char_width(char_code);
- auto glyph_width = char_width * font_size;
- text_state().font->draw_glyph(m_painter, glyph_position, glyph_width, char_code, state().paint_color);
- auto tx = glyph_width;
- tx += text_state().character_spacing;
- tx *= text_state().horizontal_scaling;
- glyph_position += { tx, 0.0f };
- }
+ 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));
// Update text matrix
- auto delta_x = glyph_position.x() - original_position.x();
+ auto delta_x = end_position.x() - start_position.x();
m_text_rendering_matrix_is_dirty = true;
m_text_matrix.translate(delta_x / text_rendering_matrix.x_scale(), 0.0f);
return {};