1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 16:47:36 +00:00

LibPDF: Add initial support for Type1 fonts

This is enough to get a char code -> code point mapping
This commit is contained in:
Matthew Olsson 2022-03-08 12:08:12 -07:00 committed by Andreas Kling
parent 8441fa2bc4
commit 0624472768
4 changed files with 133 additions and 0 deletions

View file

@ -4,6 +4,7 @@ set(SOURCES
Document.cpp
Encoding.cpp
Filter.cpp
Fonts.cpp
ObjectDerivatives.cpp
Parser.cpp
Renderer.cpp

View file

@ -36,11 +36,13 @@
V(DeviceRGB) \
V(Differences) \
V(E) \
V(Encoding) \
V(ExtGState) \
V(F) \
V(FL) \
V(Filter) \
V(First) \
V(FirstChar) \
V(Fit) \
V(FitB) \
V(FitBH) \
@ -50,6 +52,11 @@
V(FitV) \
V(FlateDecode) \
V(Font) \
V(FontDescriptor) \
V(FontFamily) \
V(FontFile1) \
V(FontFile2) \
V(FontFile3) \
V(Gamma) \
V(H) \
V(HT) \
@ -63,6 +70,7 @@
V(LW) \
V(LZWDecode) \
V(Last) \
V(LastChar) \
V(Length) \
V(Linearized) \
V(ML) \
@ -87,16 +95,19 @@
V(SA) \
V(SM) \
V(SMask) \
V(Subtype) \
V(T) \
V(TK) \
V(TR) \
V(TR2) \
V(Title) \
V(ToUnicode) \
V(Type) \
V(UCR) \
V(UseBlackPTComp) \
V(UserUnit) \
V(WhitePoint) \
V(Widths) \
V(XYZ) \
V(ca) \
V(op)

View file

@ -0,0 +1,84 @@
/*
* Copyright (c) 2022, Matthew Olsson <mattco@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibPDF/CommonNames.h>
#include <LibPDF/Fonts.h>
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<NonnullRefPtr<PDFFont>> PDFFont::create(Document* document, NonnullRefPtr<DictObject> dict)
{
auto subtype = TRY(dict->get_name(document, CommonNames::Subtype))->name();
if (subtype == "Type1")
return TRY(Type1Font::create(document, dict));
TODO();
}
PDFErrorOr<NonnullRefPtr<Type1Font>> Type1Font::create(Document* document, NonnullRefPtr<DictObject> dict)
{
// FIXME: "Required except for the standard 14 fonts"...
// "Beginning with PDF 1.5, the special treatment given to the standard 14
// fonts is deprecated. [...] For backwards capability, conforming readers
// shall still provide the special treatment identifier for the standard
// 14 fonts."
RefPtr<Encoding> encoding;
if (dict->contains(CommonNames::Encoding)) {
auto encoding_object = MUST(dict->get_object(document, CommonNames::Encoding));
encoding = TRY(Encoding::from_object(document, encoding_object));
} else {
auto base_font = MUST(dict->get_name(document, CommonNames::BaseFont))->name();
if (is_standard_latin_font(base_font)) {
// FIXME: The spec doesn't specify what the encoding should be in this case
encoding = Encoding::standard_encoding();
} else {
TODO();
}
}
RefPtr<StreamObject> to_unicode;
if (dict->contains(CommonNames::ToUnicode))
to_unicode = MUST(dict->get_stream(document, CommonNames::ToUnicode));
return adopt_ref(*new Type1Font(to_unicode, encoding.release_nonnull()));
}
Type1Font::Type1Font(RefPtr<StreamObject> to_unicode, NonnullRefPtr<Encoding> encoding)
: m_to_unicode(to_unicode)
, m_encoding(encoding)
{
}
u32 Type1Font::char_code_to_code_point(u16 char_code) const
{
if (m_to_unicode)
TODO();
auto descriptor = m_encoding->get_char_code_descriptor(char_code);
return descriptor.code_point;
}
}

View file

@ -0,0 +1,37 @@
/*
* Copyright (c) 2022, Matthew Olsson <mattco@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibPDF/Encoding.h>
#include <LibPDF/ObjectDerivatives.h>
namespace PDF {
class PDFFont : public RefCounted<PDFFont> {
public:
static PDFErrorOr<NonnullRefPtr<PDFFont>> create(Document*, NonnullRefPtr<DictObject>);
virtual ~PDFFont() = default;
virtual u32 char_code_to_code_point(u16 char_code) const = 0;
};
class Type1Font : public PDFFont {
public:
static PDFErrorOr<NonnullRefPtr<Type1Font>> create(Document*, NonnullRefPtr<DictObject>);
Type1Font(RefPtr<StreamObject> to_unicode, NonnullRefPtr<Encoding>);
~Type1Font() override = default;
u32 char_code_to_code_point(u16 char_code) const override;
private:
RefPtr<StreamObject> m_to_unicode;
NonnullRefPtr<Encoding> m_encoding;
};
}