1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-28 13:07:46 +00:00

LibPDF: Move font files into their own directory

This commit is contained in:
Matthew Olsson 2022-03-24 22:14:50 -07:00 committed by Andreas Kling
parent d2771eafc5
commit 5f9d35909d
8 changed files with 55 additions and 26 deletions

View file

@ -0,0 +1,24 @@
/*
* Copyright (c) 2022, Matthew Olsson <mattco@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibPDF/CommonNames.h>
#include <LibPDF/Fonts/PDFFont.h>
#include <LibPDF/Fonts/Type1Font.h>
namespace PDF {
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));
dbgln("Unknown font subtype: {}", subtype);
TODO();
}
}

View file

@ -0,0 +1,23 @@
/*
* Copyright (c) 2022, Matthew Olsson <mattco@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibPDF/Document.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;
virtual float get_char_width(u16 char_code) const = 0;
};
}

View file

@ -0,0 +1,103 @@
/*
* Copyright (c) 2022, Matthew Olsson <mattco@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibPDF/CommonNames.h>
#include <LibPDF/Fonts/Type1Font.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<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));
auto first_char = dict->get_value(CommonNames::FirstChar).get<int>();
auto last_char = dict->get_value(CommonNames::LastChar).get<int>();
auto widths_array = MUST(dict->get_array(document, CommonNames::Widths));
VERIFY(widths_array->size() == static_cast<size_t>(last_char - first_char + 1));
HashMap<u16, u16> widths;
for (size_t i = 0; i < widths_array->size(); i++)
widths.set(first_char + i, widths_array->at(i).get<int>());
u16 missing_width = 0;
auto descriptor = MUST(dict->get_dict(document, CommonNames::FontDescriptor));
if (descriptor->contains(CommonNames::MissingWidth))
missing_width = descriptor->get_value(CommonNames::MissingWidth).get<int>();
return adopt_ref(*new Type1Font(to_unicode, encoding.release_nonnull(), widths, missing_width));
}
Type1Font::Type1Font(RefPtr<StreamObject> to_unicode, NonnullRefPtr<Encoding> encoding, HashMap<u16, u16> const& widths, u16 missing_width)
: m_to_unicode(to_unicode)
, m_encoding(encoding)
, m_widths(widths)
, m_missing_width(missing_width)
{
}
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;
}
float Type1Font::get_char_width(u16 char_code) const
{
u16 width;
if (auto char_code_width = m_widths.get(char_code); char_code_width.has_value()) {
width = char_code_width.value();
} else {
width = m_missing_width;
}
return static_cast<float>(width) / 1000.0f;
}
}

View file

@ -0,0 +1,31 @@
/*
* Copyright (c) 2022, Matthew Olsson <mattco@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibPDF/Encoding.h>
#include <LibPDF/Fonts/PDFFont.h>
namespace PDF {
class Type1Font : public PDFFont {
public:
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() override = default;
u32 char_code_to_code_point(u16 char_code) const override;
float get_char_width(u16 char_code) const override;
private:
RefPtr<StreamObject> m_to_unicode;
NonnullRefPtr<Encoding> m_encoding;
HashMap<u16, u16> m_widths;
u16 m_missing_width;
};
}