mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 08:57:34 +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:
parent
8441fa2bc4
commit
0624472768
4 changed files with 133 additions and 0 deletions
|
@ -4,6 +4,7 @@ set(SOURCES
|
||||||
Document.cpp
|
Document.cpp
|
||||||
Encoding.cpp
|
Encoding.cpp
|
||||||
Filter.cpp
|
Filter.cpp
|
||||||
|
Fonts.cpp
|
||||||
ObjectDerivatives.cpp
|
ObjectDerivatives.cpp
|
||||||
Parser.cpp
|
Parser.cpp
|
||||||
Renderer.cpp
|
Renderer.cpp
|
||||||
|
|
|
@ -36,11 +36,13 @@
|
||||||
V(DeviceRGB) \
|
V(DeviceRGB) \
|
||||||
V(Differences) \
|
V(Differences) \
|
||||||
V(E) \
|
V(E) \
|
||||||
|
V(Encoding) \
|
||||||
V(ExtGState) \
|
V(ExtGState) \
|
||||||
V(F) \
|
V(F) \
|
||||||
V(FL) \
|
V(FL) \
|
||||||
V(Filter) \
|
V(Filter) \
|
||||||
V(First) \
|
V(First) \
|
||||||
|
V(FirstChar) \
|
||||||
V(Fit) \
|
V(Fit) \
|
||||||
V(FitB) \
|
V(FitB) \
|
||||||
V(FitBH) \
|
V(FitBH) \
|
||||||
|
@ -50,6 +52,11 @@
|
||||||
V(FitV) \
|
V(FitV) \
|
||||||
V(FlateDecode) \
|
V(FlateDecode) \
|
||||||
V(Font) \
|
V(Font) \
|
||||||
|
V(FontDescriptor) \
|
||||||
|
V(FontFamily) \
|
||||||
|
V(FontFile1) \
|
||||||
|
V(FontFile2) \
|
||||||
|
V(FontFile3) \
|
||||||
V(Gamma) \
|
V(Gamma) \
|
||||||
V(H) \
|
V(H) \
|
||||||
V(HT) \
|
V(HT) \
|
||||||
|
@ -63,6 +70,7 @@
|
||||||
V(LW) \
|
V(LW) \
|
||||||
V(LZWDecode) \
|
V(LZWDecode) \
|
||||||
V(Last) \
|
V(Last) \
|
||||||
|
V(LastChar) \
|
||||||
V(Length) \
|
V(Length) \
|
||||||
V(Linearized) \
|
V(Linearized) \
|
||||||
V(ML) \
|
V(ML) \
|
||||||
|
@ -87,16 +95,19 @@
|
||||||
V(SA) \
|
V(SA) \
|
||||||
V(SM) \
|
V(SM) \
|
||||||
V(SMask) \
|
V(SMask) \
|
||||||
|
V(Subtype) \
|
||||||
V(T) \
|
V(T) \
|
||||||
V(TK) \
|
V(TK) \
|
||||||
V(TR) \
|
V(TR) \
|
||||||
V(TR2) \
|
V(TR2) \
|
||||||
V(Title) \
|
V(Title) \
|
||||||
|
V(ToUnicode) \
|
||||||
V(Type) \
|
V(Type) \
|
||||||
V(UCR) \
|
V(UCR) \
|
||||||
V(UseBlackPTComp) \
|
V(UseBlackPTComp) \
|
||||||
V(UserUnit) \
|
V(UserUnit) \
|
||||||
V(WhitePoint) \
|
V(WhitePoint) \
|
||||||
|
V(Widths) \
|
||||||
V(XYZ) \
|
V(XYZ) \
|
||||||
V(ca) \
|
V(ca) \
|
||||||
V(op)
|
V(op)
|
||||||
|
|
84
Userland/Libraries/LibPDF/Fonts.cpp
Normal file
84
Userland/Libraries/LibPDF/Fonts.cpp
Normal 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
37
Userland/Libraries/LibPDF/Fonts.h
Normal file
37
Userland/Libraries/LibPDF/Fonts.h
Normal 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;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue