Skip to content

Commit

Permalink
LibPDF: Add glyph drawing and type info methods to PDFFont
Browse files Browse the repository at this point in the history
A PDFFont can now be asked for its specific type and whether it is part
of the standard 14 fonts. It now also contains a method to draw a
glyph, which is stubbed-out for now.

This will be useful for the renderer to take into consideration when
drawing text, since we don't include replacements for the standard set
of fonts yet, but still want to make use of embedded fonts when
available.
  • Loading branch information
janso3 authored and awesomekling committed Oct 16, 2022
1 parent 36f83ce commit e6f2930
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 1 deletion.
15 changes: 15 additions & 0 deletions Userland/Libraries/LibPDF/Fonts/PDFFont.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,33 @@

#pragma once

#include <LibGfx/Forward.h>
#include <LibPDF/Document.h>

namespace PDF {

class PDFFont : public RefCounted<PDFFont> {
public:
enum class Type {
Type0,
Type1,
TrueType
};

static PDFErrorOr<NonnullRefPtr<PDFFont>> create(Document*, NonnullRefPtr<DictObject>);

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 void draw_glyph(Gfx::Painter& painter, Gfx::IntPoint const& point, float width, u32 code_point, Color color) = 0;

virtual bool is_standard_font() const { return m_is_standard_font; }
virtual Type type() const = 0;

protected:
bool m_is_standard_font { false };
};

}
4 changes: 4 additions & 0 deletions Userland/Libraries/LibPDF/Fonts/TrueTypeFont.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ class TrueTypeFont : public PDFFont {
u32 char_code_to_code_point(u16 char_code) const override;
float get_char_width(u16 char_code, float font_size) const override;

void draw_glyph(Gfx::Painter&, Gfx::IntPoint const&, float, u32, Color) override {};

Type type() const override { return PDFFont::Type::TrueType; }

private:
NonnullRefPtr<TTF::Font> m_ttf_font;
Type1Font::Data m_data;
Expand Down
4 changes: 4 additions & 0 deletions Userland/Libraries/LibPDF/Fonts/Type0Font.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ class Type0Font : public PDFFont {
u32 char_code_to_code_point(u16 char_code) const override;
float get_char_width(u16 char_code, float font_size) const override;

void draw_glyph(Gfx::Painter&, Gfx::IntPoint const&, float, u32, Color) override {};

Type type() const override { return PDFFont::Type::Type0; }

private:
CIDSystemInfo m_system_info;
HashMap<u16, u16> m_widths;
Expand Down
3 changes: 2 additions & 1 deletion Userland/Libraries/LibPDF/Fonts/Type1Font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ PDFErrorOr<Type1Font::Data> Type1Font::parse_data(Document* document, NonnullRef
if (descriptor->contains(CommonNames::MissingWidth))
missing_width = descriptor->get_value(CommonNames::MissingWidth).to_int();

return Type1Font::Data { to_unicode, encoding.release_nonnull(), move(widths), missing_width };
return Type1Font::Data { to_unicode, encoding.release_nonnull(), move(widths), missing_width, false };
}

PDFErrorOr<NonnullRefPtr<Type1Font>> Type1Font::create(Document* document, NonnullRefPtr<DictObject> dict)
Expand All @@ -80,6 +80,7 @@ PDFErrorOr<NonnullRefPtr<Type1Font>> Type1Font::create(Document* document, Nonnu
Type1Font::Type1Font(Data data)
: m_data(move(data))
{
m_is_standard_font = data.is_standard_font;
}

u32 Type1Font::char_code_to_code_point(u16 char_code) const
Expand Down
5 changes: 5 additions & 0 deletions Userland/Libraries/LibPDF/Fonts/Type1Font.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Type1Font : public PDFFont {
NonnullRefPtr<Encoding> encoding;
HashMap<u16, u16> widths;
u16 missing_width;
bool is_standard_font;
};

static PDFErrorOr<Data> parse_data(Document*, NonnullRefPtr<DictObject> font_dict);
Expand All @@ -31,6 +32,10 @@ class Type1Font : public PDFFont {
u32 char_code_to_code_point(u16 char_code) const override;
float get_char_width(u16 char_code, float font_size) const override;

void draw_glyph(Gfx::Painter&, Gfx::IntPoint const&, float, u32, Color) override {};

Type type() const override { return PDFFont::Type::Type1; }

private:
Data m_data;
};
Expand Down

0 comments on commit e6f2930

Please sign in to comment.