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

LibPDF: Introduce loading of common font data in PDFFont base class

This font data is shared between Type 1 and TrueType fonts, which is
why we can now load it in the base class that they both use.
This commit is contained in:
Julian Offenhäuser 2022-11-22 19:41:04 +01:00 committed by Andreas Kling
parent dd82a026f8
commit e748a94f80
2 changed files with 68 additions and 0 deletions

View file

@ -12,6 +12,61 @@
namespace PDF { namespace PDF {
static bool is_standard_latin_font(FlyString const& font)
{
return font.is_one_of(
"Times-Roman",
"Helvetica",
"Courier",
"Times-Bold",
"Helvetica-Bold",
"Courier-Bold",
"Times-Italic",
"Helvetica-Oblique",
"Courier-Oblique",
"Times-BoldItalic",
"Helvetica-BoldOblique",
"Courier-BoldOblique");
}
PDFErrorOr<void> PDFFont::CommonData::load_from_dict(Document* document, NonnullRefPtr<DictObject> dict)
{
auto base_font = TRY(dict->get_name(document, CommonNames::BaseFont))->name();
is_standard_font = is_standard_latin_font(base_font);
if (dict->contains(CommonNames::Encoding)) {
auto encoding_object = MUST(dict->get_object(document, CommonNames::Encoding));
encoding = TRY(Encoding::from_object(document, encoding_object));
} else {
// FIXME: The spec doesn't specify what the encoding should be in this case
if (is_standard_font)
encoding = Encoding::standard_encoding();
// Otherwise, use the built-in encoding of the font.
}
if (dict->contains(CommonNames::ToUnicode))
to_unicode = TRY(dict->get_stream(document, CommonNames::ToUnicode));
if (dict->contains(CommonNames::FirstChar) && dict->contains(CommonNames::LastChar) && dict->contains(CommonNames::Widths)) {
auto first_char = dict->get_value(CommonNames::FirstChar).get<int>();
auto last_char = dict->get_value(CommonNames::LastChar).get<int>();
auto widths_array = TRY(dict->get_array(document, CommonNames::Widths));
VERIFY(widths_array->size() == static_cast<size_t>(last_char - first_char + 1));
for (size_t i = 0; i < widths_array->size(); i++)
widths.set(first_char + i, widths_array->at(i).to_int());
}
if (dict->contains(CommonNames::FontDescriptor)) {
auto descriptor = TRY(dict->get_dict(document, CommonNames::FontDescriptor));
if (descriptor->contains(CommonNames::MissingWidth))
missing_width = descriptor->get_value(CommonNames::MissingWidth).to_int();
}
return {};
}
PDFErrorOr<NonnullRefPtr<PDFFont>> PDFFont::create(Document* document, NonnullRefPtr<DictObject> dict) PDFErrorOr<NonnullRefPtr<PDFFont>> PDFFont::create(Document* document, NonnullRefPtr<DictObject> dict)
{ {
auto subtype = TRY(dict->get_name(document, CommonNames::Subtype))->name(); auto subtype = TRY(dict->get_name(document, CommonNames::Subtype))->name();

View file

@ -6,8 +6,10 @@
#pragma once #pragma once
#include <AK/HashMap.h>
#include <LibGfx/Forward.h> #include <LibGfx/Forward.h>
#include <LibPDF/Document.h> #include <LibPDF/Document.h>
#include <LibPDF/Encoding.h>
namespace PDF { namespace PDF {
@ -19,6 +21,17 @@ public:
TrueType TrueType
}; };
// This is used both by Type 1 and TrueType fonts.
struct CommonData {
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>);
};
static PDFErrorOr<NonnullRefPtr<PDFFont>> create(Document*, NonnullRefPtr<DictObject>); static PDFErrorOr<NonnullRefPtr<PDFFont>> create(Document*, NonnullRefPtr<DictObject>);
virtual ~PDFFont() = default; virtual ~PDFFont() = default;