mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 19:57:44 +00:00
LibPDF: Move all font handling to Type1Font and TrueTypeFont classes
It was previously the job of the renderer to create fonts, load replacements for the standard 14 fonts and to pass the font size back to the PDFFont when asking for glyph widths. Now, the renderer tells the font its size at creation, as it doesn't change throughout the life of the font. The PDFFont itself is now responsible to decide whether or not it needs to use a replacement font, which still is Liberation Serif for now. This means that we can now render embedded TrueType fonts as well :^) It also makes the renderer's job much more simple and leads to a much cleaner API design.
This commit is contained in:
parent
e748a94f80
commit
9cb3b23377
10 changed files with 119 additions and 171 deletions
|
@ -4,6 +4,7 @@
|
|||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <LibGfx/Font/FontDatabase.h>
|
||||
#include <LibPDF/CommonNames.h>
|
||||
#include <LibPDF/Fonts/PDFFont.h>
|
||||
#include <LibPDF/Fonts/TrueTypeFont.h>
|
||||
|
@ -29,10 +30,14 @@ static bool is_standard_latin_font(FlyString const& font)
|
|||
"Courier-BoldOblique");
|
||||
}
|
||||
|
||||
PDFErrorOr<void> PDFFont::CommonData::load_from_dict(Document* document, NonnullRefPtr<DictObject> dict)
|
||||
PDFErrorOr<void> PDFFont::CommonData::load_from_dict(Document* document, NonnullRefPtr<DictObject> dict, float font_size)
|
||||
{
|
||||
auto base_font = TRY(dict->get_name(document, CommonNames::BaseFont))->name();
|
||||
is_standard_font = is_standard_latin_font(base_font);
|
||||
if ((is_standard_font = is_standard_latin_font(base_font))) {
|
||||
auto replacement = replacement_for_standard_latin_font(base_font.to_lowercase());
|
||||
font = Gfx::FontDatabase::the().get(replacement.get<0>(), replacement.get<1>(), font_size);
|
||||
VERIFY(font);
|
||||
}
|
||||
|
||||
if (dict->contains(CommonNames::Encoding)) {
|
||||
auto encoding_object = MUST(dict->get_object(document, CommonNames::Encoding));
|
||||
|
@ -67,19 +72,39 @@ PDFErrorOr<void> PDFFont::CommonData::load_from_dict(Document* document, Nonnull
|
|||
return {};
|
||||
}
|
||||
|
||||
PDFErrorOr<NonnullRefPtr<PDFFont>> PDFFont::create(Document* document, NonnullRefPtr<DictObject> dict)
|
||||
PDFErrorOr<NonnullRefPtr<PDFFont>> PDFFont::create(Document* document, NonnullRefPtr<DictObject> dict, float font_size)
|
||||
{
|
||||
auto subtype = TRY(dict->get_name(document, CommonNames::Subtype))->name();
|
||||
|
||||
if (subtype == "Type0")
|
||||
return TRY(Type0Font::create(document, dict));
|
||||
if (subtype == "Type1")
|
||||
return TRY(Type1Font::create(document, dict));
|
||||
return TRY(Type1Font::create(document, dict, font_size));
|
||||
if (subtype == "TrueType")
|
||||
return TRY(TrueTypeFont::create(document, dict));
|
||||
return TRY(TrueTypeFont::create(document, dict, font_size));
|
||||
|
||||
dbgln("Unknown font subtype: {}", subtype);
|
||||
TODO();
|
||||
}
|
||||
|
||||
Tuple<String, String> PDFFont::replacement_for_standard_latin_font(StringView name)
|
||||
{
|
||||
bool is_bold = name.contains("bold"sv);
|
||||
bool is_italic = name.contains("italic"sv);
|
||||
|
||||
String font_variant;
|
||||
|
||||
if (is_bold && is_italic) {
|
||||
font_variant = "BoldItalic";
|
||||
} else if (is_bold) {
|
||||
font_variant = "Bold";
|
||||
} else if (is_italic) {
|
||||
font_variant = "Italic";
|
||||
} else {
|
||||
font_variant = "Regular";
|
||||
}
|
||||
|
||||
return { "Liberation Serif", font_variant };
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue