![]() |
Fonts and text in PDF output | ![]() |
The typesetter draws text one glyph at a time: the boxes of strings call draw (ch, fn, x, y) on the renderer with a character code ch and a font_glyphs object fn, the low-level glyph set of a font at a given size and resolution (see fonts). The name of that glyph set, fn->res_name, identifies the font in the PDF file: family:size.dpitt for glyphs read with FreeType (tt_font_glyphs,
tt_face.cpp:241
) and familysize.dpipk for Metafont bitmaps (
load_tex.cpp:215
). font_size reads back the size from the name, scaled to the printing resolution.
draw (
pdf_hummus_renderer.cpp:1468
) proceeds as follows:
The first time a font is used, make_pdf_font decides whether it can be written as a native font: the part of the name before : is looked up with tt_font_find, and the file is loaded with PDFWriter::GetFontForFile, which uses FreeType and accepts Type 1, TrueType, OpenType and CFF fonts. Files listed in
fonts/pdf-font-issues.scm
, files which FreeType cannot open and fonts without a font file (the Metafont fonts rendered to bitmaps) are put in not_native_fonts instead.
When the font changes, a text object is opened (BT) and the font is selected with Tf: the native font at its size, or the Type 3 font of the current chunk at the conventional size 100.
The glyph is positioned with Td, relative to the previous glyph, and written with Tj. For a native font, the renderer passes to PDFHummus the index of the glyph in the font file (gl->index) together with the character it stands for, as a GlyphUnicodeMapping.
Every glyph is placed individually, so the content stream contains no spaces: PDF viewers reconstruct words and spaces from the distances between glyphs.
PDFHummus collects the glyphs used in each native font and, at EndPDF, embeds a subset of the font (hence names like AAAAAB+CMMI10) together with a ToUnicode map built from the GlyphUnicodeMappings. This map is what makes the text searchable and copyable. Its quality depends on the character which the renderer passes, and the renderer passes the character code ch of the glyph set, which is a Unicode code point only for Unicode fonts:
For Unicode fonts (TrueType and OpenType fonts used through unicode_font), ch is the code point, and the text layer is right.
For the European Computer Modern fonts (recognized by their PostScript name, EuropeanComputerModern_fonts) the code is a Cork code. The ligature glyphs (glyph indices 27 to 31) are mapped to U+FB00 to U+FB04. From PDF 1.5 on, every character from U+FB00 to U+FB04, in any font, is also wrapped in a /Span with an /ActualText such as (ffi) (
pdf_hummus_renderer.cpp:1544
). The other Cork codes are passed as they are, which is wrong for the ligature oe, the sharp s, the inverted question and exclamation marks and the quotes; wip_fixes translates them with cork_to_utf8 (pull request #157, which also handles the T2A Cyrillic fonts).
For the other TeX fonts, in particular the math fonts (cmmi, cmsy, cmex, msam, msbm, …), the position in the TeX encoding is passed as a code point, so letters such as \(\alpha\) are lost or replaced when the text is copied (issue #302).
Two workarounds remain from older PDF viewers: character 0 of the Computer Modern fonts is drawn as character 161 (requires_hack_notdef_for_tex_font), and HelveticaNeue.0.ttf is listed in
pdf-font-issues.scm
, so it is written as a Type 3 font (see the PDFHummus library).
Fonts which cannot be embedded natively are written as Type 3 fonts whose glyphs are the bitmaps of the font_glyphs, as inline images with one bit per pixel (t3font_rep::write_char,
pdf_hummus_renderer.cpp:1113
). A Type 3 font has at most 256 codes, so a glyph set is split into chunks (codes 0 to 255 in the first one, 254 codes in each of the others), each a separate font named res_name-chunkn (t3font_font_chunk, t3font_get_local_glyph). The glyphs are bitmaps at the printing resolution; the font matrix scales them by 1/100, and the font is selected at size 100, so that one unit of the glyph is one device pixel.
write_definition writes, for each chunk, the glyph procedures, the widths, the bounding box and a ToUnicode map which maps each local code back to the character code of the glyph set. As for native fonts, this character code is only meaningful as Unicode for Unicode fonts.
Type 3 fonts are not scalable: they look grainy when zoomed, and a document full of them is large. They usually appear when a font is missing and TeXmacs falls back to Metafont, or when a font file is listed in
pdf-font-issues.scm
.
The text layer of an exported file can be checked without a viewer:
mutool draw -F txt -o - file.pdf # the text as a viewer copies it mutool info -F file.pdf # the fonts and their types mutool draw -F stext -o - file.pdf # fonts and characters, glyph by glyph |
The document regression tests compare the text layer of sample documents with references (
tests/documents/check.sh
).