Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions backend/pdf/font.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func (e *embedded) write(d *document, compress bool) error {
// CFF outlines are embedded whole and described as OpenType, because
// this package does not cut charstrings up — see internal/sfnt.
dict = "/Subtype /CIDFontType0"
fileKey = "/FontFile3 "
fileKey = "/FontFile3"
}

stream := fmt.Sprintf("/Length1 %d", len(prog))
Expand All @@ -121,7 +121,7 @@ func (e *embedded) write(d *document, compress bool) error {
xMin, yMin, xMax, yMax := f.BBox()
desc := d.add([]byte(fmt.Sprintf(
"<< /Type /FontDescriptor /FontName /%s /Flags 4 /FontBBox [%d %d %d %d] "+
"/ItalicAngle %s /Ascent %d /Descent %d /CapHeight %d /StemV %d %s%d 0 R >>",
"/ItalicAngle %s /Ascent %d /Descent %d /CapHeight %d /StemV %d %s %d 0 R >>",
name, k(xMin), k(yMin), k(xMax), k(yMax),
fmtNum(f.ItalicAngle()), k(f.Ascent()), k(f.Descent()), k(f.CapHeight()),
stemV(f.WeightClass()), fileKey, file)))
Expand Down
30 changes: 30 additions & 0 deletions backend/pdf/font_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pdf_test

import (
"regexp"
"strings"
"testing"

Expand Down Expand Up @@ -58,6 +59,35 @@ func TestAnEmbeddedFontIsACIDFont(t *testing.T) {
}
}

// The descriptor points at the font program by reference. A key run into the
// object number ("/FontFile22 0 R") is a different name followed by a stray
// "0 R": readers then treat the font as not embedded, and a PDF importer that
// re-serialises the dictionary scrambles it.
func TestTheDescriptorReferencesTheFontProgram(t *testing.T) {
for _, tc := range []struct {
name, key string
font []byte
}{
{"TrueType", "/FontFile2", testTTF()},
{"CFF", "/FontFile3", sfnttest.CFFFont()},
} {
t.Run(tc.name, func(t *testing.T) {
doc := drawLabel(t, "AB", pdf.WithFont(tc.font, nil, nil), pdf.Uncompressed())
m := regexp.MustCompile(regexp.QuoteMeta(tc.key) + ` (\d+) 0 R`).FindStringSubmatch(doc)
if m == nil {
t.Fatalf("the descriptor carries no %s reference", tc.key)
}
obj := regexp.MustCompile(`(?s)\n` + m[1] + ` 0 obj\n<<(.*?)>>\nstream`).FindStringSubmatch(doc)
if obj == nil {
t.Fatalf("%s points at object %s, which is not a stream", tc.key, m[1])
}
if tc.key == "/FontFile2" && !strings.Contains(obj[1], "/Length1") {
t.Errorf("object %s is not the font program: %s", m[1], obj[1])
}
})
}
}

// The glyphs are numbered as they are first met, which is what makes writing
// a PDF one pass: the content stream names an id before the subset that
// defines it exists.
Expand Down
Loading