Skip to content

Commit bd74447

Browse files
nicotrflynn89
authored andcommitted
LibPDF: Initial support for drawing CFF-based Type0 fonts
Together with the already-merged #23122, #23128, #23135, #23136, #23162, and #23167, #23179, #23190, #23194 this adds initial support for rendering some CFF-based Type0 fonts :^) There's a long list of things that still need improving after this: * A small number of CFF programs contain the charstring command 0, which is invalid. Currently, this makes us reject the whole font. * Type1FontProgram::rasterize_glyph() is name-based. For CID-based fonts, we want a version that takes CIDs (character IDs) instead. For now, I'm printing the CID to a string and using that, yuck. (I looked into doing this nicely. I do want to do that, but I need to read up on how the `seac` type1 charstring command uses character names to identify parts of an accented character. Also, it looks like `seac`'s accented character handling moved over to `endchar` in type2 charstring commands (i.e. in CFF data), and it looks like we don't implement that at all. So I need to do more reading first, and I didn't want to block this on that.) * The name for the first string in name-based CFF fonts looks wrong; added a FIXME for that for now. * This supports the named Identity-H cmap only for now. Identity-H maps UTF16-BE values to glyph IDs with the idenity function, and assumes it's horizontal text. Other named cmaps in my test files are UniJIS-UCS2-H, UniCNS-UCS2-H, Identity-V, UniGB-UCS2-H, UniKS-UCS2-H. (There are also 2 files using the stream-based cmaps instead of the name-based ones.) * In particular, we can't draw vertical text (`-V`) yet * Passing in the encoding to CFF::create() is awkward (it's nullptr for CID-keyed fonts), and it's also not necessary since `Type1Font::draw_glyph()` already does the "take encoding from PDF, and only from font if the PDF doesn't store one" dance. * This doesn't cache glyphs but re-rasterizes them each time. Easy to add, but maybe I want to look at rotation first. And things don't feel glacial as-is. * Type0Font::draw_glyph() is pretty similar to second half of Type1Font::draw_glyph()
1 parent c9d48bb commit bd74447

File tree

2 files changed

+50
-7
lines changed

2 files changed

+50
-7
lines changed

Userland/Libraries/LibPDF/Fonts/CFF.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -246,11 +246,24 @@ PDFErrorOr<NonnullRefPtr<CFF>> CFF::create(ReadonlyBytes const& cff_bytes, RefPt
246246

247247
for (size_t i = 0; i < glyphs.size(); i++) {
248248
if (i == 0) {
249-
TRY(cff->add_glyph(0, move(glyphs[0])));
249+
if (top_dict.is_cid_keyed) {
250+
// FIXME: Do better than printing the cid to a string.
251+
auto cid = 0;
252+
TRY(cff->add_glyph(ByteString::formatted("{}", cid), move(glyphs[0])));
253+
} else {
254+
// FIXME: Shouldn't this use resolve_sid(0, strings) (".notdef") as name?
255+
TRY(cff->add_glyph(0, move(glyphs[0])));
256+
}
250257
continue;
251258
}
252-
auto const& name = charset_names[i - 1];
253-
TRY(cff->add_glyph(name, move(glyphs[i])));
259+
if (top_dict.is_cid_keyed) {
260+
// FIXME: Do better than printing the cid to a string.
261+
auto cid = charset[i - 1];
262+
TRY(cff->add_glyph(ByteString::formatted("{}", cid), move(glyphs[i])));
263+
} else {
264+
auto const& name = charset_names[i - 1];
265+
TRY(cff->add_glyph(name, move(glyphs[i])));
266+
}
254267
}
255268
cff->consolidate_glyphs();
256269

Userland/Libraries/LibPDF/Fonts/Type0Font.cpp

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ PDFErrorOr<NonnullOwnPtr<CIDFontType0>> CIDFontType0::create(Document* document,
4747
if (font_file_dict->contains(CommonNames::Subtype))
4848
subtype = font_file_dict->get_name(CommonNames::Subtype)->name();
4949
if (subtype == CommonNames::CIDFontType0C) {
50-
// FIXME: Call CFF::create() and assign the result to font_program once CFF::create() can handle CID-keyed fonts.
51-
return Error::rendering_unsupported_error("Type0 font CIDFontType0: support for CIDFontType0C not yet implemented");
50+
// FIXME: Stop passing an external encoding to CFF::create().
51+
font_program = TRY(CFF::create(font_file_stream->bytes(), /*encoding=*/nullptr));
5252
} else {
5353
// FIXME: Add support for /OpenType.
5454
dbgln("CIDFontType0: unsupported FontFile3 subtype '{}'", subtype);
@@ -64,7 +64,7 @@ PDFErrorOr<NonnullOwnPtr<CIDFontType0>> CIDFontType0::create(Document* document,
6464
return TRY(adopt_nonnull_own_or_enomem(new (nothrow) CIDFontType0(move(font_program))));
6565
}
6666

67-
PDFErrorOr<void> CIDFontType0::draw_glyph(Gfx::Painter&, Gfx::FloatPoint, float, u32, Renderer const&)
67+
PDFErrorOr<void> CIDFontType0::draw_glyph(Gfx::Painter& painter, Gfx::FloatPoint point, float width, u32 cid, Renderer const& renderer)
6868
{
6969
// ISO 32000 (PDF 2.0) 9.7.4.2 Glyph selection in CIDFonts
7070
// "When the CIDFont contains an embedded font program that is represented in the Compact Font Format (CFF),
@@ -75,7 +75,37 @@ PDFErrorOr<void> CIDFontType0::draw_glyph(Gfx::Painter&, Gfx::FloatPoint, float,
7575
// The GID value shall then be used to look up the glyph procedure using the CharStrings INDEX table [...]
7676
// * The "CFF" font program has a Top DICT that does not use CIDFont operators: The CIDs shall be used
7777
// directly as GID values, and the glyph procedure shall be retrieved using the CharStrings INDEX"
78-
return Error::rendering_unsupported_error("Type0 font CIDFontType0 not implemented yet");
78+
79+
// FIXME: We currently only do the first.
80+
81+
// FIXME: Do better than printing the cid to a string.
82+
auto char_name = ByteString::formatted("{}", cid);
83+
auto translation = m_font_program->glyph_translation(char_name, width);
84+
point = point.translated(translation);
85+
86+
auto glyph_position = Gfx::GlyphRasterPosition::get_nearest_fit_for(point);
87+
88+
// FIXME: Cache the font bitmap (but probably want to figure out rotation first).
89+
auto bitmap = m_font_program->rasterize_glyph(char_name, width, glyph_position.subpixel_offset);
90+
if (!bitmap)
91+
return Error::rendering_unsupported_error("Type0 font CIDFontType0: failed to rasterize glyph");
92+
93+
auto style = renderer.state().paint_style;
94+
95+
if (style.has<Color>()) {
96+
painter.blit_filtered(glyph_position.blit_position, *bitmap, bitmap->rect(), [style](Color pixel) -> Color {
97+
return pixel.multiply(style.get<Color>());
98+
});
99+
} else {
100+
style.get<NonnullRefPtr<Gfx::PaintStyle>>()->paint(bitmap->physical_rect(), [&](auto sample) {
101+
painter.blit_filtered(glyph_position.blit_position, *bitmap, bitmap->rect(), [&](Color pixel) -> Color {
102+
// FIXME: Presumably we need to sample at every point in the glyph, not just the top left?
103+
return pixel.multiply(sample(glyph_position.blit_position));
104+
});
105+
});
106+
}
107+
108+
return {};
79109
}
80110

81111
class CIDFontType2 : public CIDFontType {

0 commit comments

Comments
 (0)