GNU TeXmacsUser manualReferenceAboutEnglish
Fonts and text in PDF output

1.Drawing a glyph

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:

  1. 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.

  2. 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.

  3. 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.

2.Native fonts

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:

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).

3.Type 3 fonts

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

.

4.What to check

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

).

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with no Invariant Sections, with no Front-Cover Texts, and with no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License".