1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-28 17:37:47 +00:00

LibPDF: Move all font handling to Type1Font and TrueTypeFont classes

It was previously the job of the renderer to create fonts, load
replacements for the standard 14 fonts and to pass the font size back
to the PDFFont when asking for glyph widths.

Now, the renderer tells the font its size at creation, as it doesn't
change throughout the life of the font. The PDFFont itself is now
responsible to decide whether or not it needs to use a replacement
font, which still is Liberation Serif for now.

This means that we can now render embedded TrueType fonts as well :^)

It also makes the renderer's job much more simple and leads to a much
cleaner API design.
This commit is contained in:
Julian Offenhäuser 2022-11-22 19:46:29 +01:00 committed by Andreas Kling
parent e748a94f80
commit 9cb3b23377
10 changed files with 119 additions and 171 deletions

View file

@ -7,6 +7,7 @@
#pragma once
#include <AK/HashMap.h>
#include <AK/Tuple.h>
#include <LibGfx/Forward.h>
#include <LibPDF/Document.h>
#include <LibPDF/Encoding.h>
@ -23,21 +24,22 @@ public:
// This is used both by Type 1 and TrueType fonts.
struct CommonData {
RefPtr<Gfx::Font> font;
RefPtr<StreamObject> to_unicode;
RefPtr<Encoding> encoding;
HashMap<u16, u16> widths;
u16 missing_width;
bool is_standard_font;
PDFErrorOr<void> load_from_dict(Document*, NonnullRefPtr<DictObject>);
PDFErrorOr<void> load_from_dict(Document*, NonnullRefPtr<DictObject>, float font_size);
};
static PDFErrorOr<NonnullRefPtr<PDFFont>> create(Document*, NonnullRefPtr<DictObject>);
static PDFErrorOr<NonnullRefPtr<PDFFont>> create(Document*, NonnullRefPtr<DictObject>, float font_size);
virtual ~PDFFont() = default;
virtual u32 char_code_to_code_point(u16 char_code) const = 0;
virtual float get_char_width(u16 char_code, float font_size) const = 0;
virtual float get_char_width(u16 char_code) const = 0;
virtual void draw_glyph(Gfx::Painter& painter, Gfx::IntPoint const& point, float width, u32 char_code, Color color) = 0;
@ -45,6 +47,8 @@ public:
virtual Type type() const = 0;
protected:
static Tuple<String, String> replacement_for_standard_latin_font(StringView);
bool m_is_standard_font { false };
};