1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 06:27:45 +00:00

LibPDF: Abstract Type1 font data

TTF font types will use the same data
This commit is contained in:
Matthew Olsson 2022-03-25 08:19:26 -07:00 committed by Andreas Kling
parent 058cf5f7f7
commit e831c374f4
2 changed files with 26 additions and 16 deletions

View file

@ -26,7 +26,7 @@ static bool is_standard_latin_font(FlyString const& font)
"Courier-BoldOblique"); "Courier-BoldOblique");
} }
PDFErrorOr<NonnullRefPtr<Type1Font>> Type1Font::create(Document* document, NonnullRefPtr<DictObject> dict) PDFErrorOr<Type1Font::Data> Type1Font::parse_data(Document* document, NonnullRefPtr<DictObject> dict)
{ {
// FIXME: "Required except for the standard 14 fonts"... // FIXME: "Required except for the standard 14 fonts"...
// "Beginning with PDF 1.5, the special treatment given to the standard 14 // "Beginning with PDF 1.5, the special treatment given to the standard 14
@ -68,33 +68,36 @@ PDFErrorOr<NonnullRefPtr<Type1Font>> Type1Font::create(Document* document, Nonnu
if (descriptor->contains(CommonNames::MissingWidth)) if (descriptor->contains(CommonNames::MissingWidth))
missing_width = descriptor->get_value(CommonNames::MissingWidth).get<int>(); missing_width = descriptor->get_value(CommonNames::MissingWidth).get<int>();
return adopt_ref(*new Type1Font(to_unicode, encoding.release_nonnull(), widths, missing_width)); return Type1Font::Data { to_unicode, encoding.release_nonnull(), move(widths), missing_width };
} }
Type1Font::Type1Font(RefPtr<StreamObject> to_unicode, NonnullRefPtr<Encoding> encoding, HashMap<u16, u16> const& widths, u16 missing_width) PDFErrorOr<NonnullRefPtr<Type1Font>> Type1Font::create(Document* document, NonnullRefPtr<DictObject> dict)
: m_to_unicode(to_unicode) {
, m_encoding(encoding) auto data = TRY(Type1Font::parse_data(document, dict));
, m_widths(widths) return adopt_ref(*new Type1Font(data));
, m_missing_width(missing_width) }
Type1Font::Type1Font(Data data)
: m_data(move(data))
{ {
} }
u32 Type1Font::char_code_to_code_point(u16 char_code) const u32 Type1Font::char_code_to_code_point(u16 char_code) const
{ {
if (m_to_unicode) if (m_data.to_unicode)
TODO(); TODO();
auto descriptor = m_encoding->get_char_code_descriptor(char_code); auto descriptor = m_data.encoding->get_char_code_descriptor(char_code);
return descriptor.code_point; return descriptor.code_point;
} }
float Type1Font::get_char_width(u16 char_code, float) const float Type1Font::get_char_width(u16 char_code, float) const
{ {
u16 width; u16 width;
if (auto char_code_width = m_widths.get(char_code); char_code_width.has_value()) { if (auto char_code_width = m_data.widths.get(char_code); char_code_width.has_value()) {
width = char_code_width.value(); width = char_code_width.value();
} else { } else {
width = m_missing_width; width = m_data.missing_width;
} }
return static_cast<float>(width) / 1000.0f; return static_cast<float>(width) / 1000.0f;

View file

@ -13,19 +13,26 @@ namespace PDF {
class Type1Font : public PDFFont { class Type1Font : public PDFFont {
public: public:
// Also used by TrueTypeFont, which is very similar to Type1
struct Data {
RefPtr<StreamObject> to_unicode;
NonnullRefPtr<Encoding> encoding;
HashMap<u16, u16> widths;
u16 missing_width;
};
static PDFErrorOr<Data> parse_data(Document*, NonnullRefPtr<DictObject> font_dict);
static PDFErrorOr<NonnullRefPtr<Type1Font>> create(Document*, NonnullRefPtr<DictObject>); static PDFErrorOr<NonnullRefPtr<Type1Font>> create(Document*, NonnullRefPtr<DictObject>);
Type1Font(RefPtr<StreamObject> to_unicode, NonnullRefPtr<Encoding>, HashMap<u16, u16> const& m_widths, u16 missing_width); Type1Font(Data);
~Type1Font() override = default; ~Type1Font() override = default;
u32 char_code_to_code_point(u16 char_code) const override; u32 char_code_to_code_point(u16 char_code) const override;
float get_char_width(u16 char_code, float font_size) const override; float get_char_width(u16 char_code, float font_size) const override;
private: private:
RefPtr<StreamObject> m_to_unicode; Data m_data;
NonnullRefPtr<Encoding> m_encoding;
HashMap<u16, u16> m_widths;
u16 m_missing_width;
}; };
} }