Write Carlito to Word as Calibri, Caladea as Cambria, and read them back
Paul's default 5. Docs has no DOCX import or export yet (spec §11.2, §11.3), so internal/docx is only the part the font rulings decide. It writes paragraphs, headings and runs with their family, weight, slope and size. The default family goes in w:docDefaults, and fontTable.xml names every family with its generic family, pitch, panose and nearest Office face. Carlito is written as Calibri, Caladea as Cambria, and Liberation as Arial, Times New Roman and Courier New. Every other family is named, not embedded. Read reverses each mapping. A run's face is its own w:rFonts or else the document's default, so a Word file whose default is Cambria is Caladea throughout. A face Docs does not have is reported for the import report, not silently replaced. Nothing routes to the package yet. Lists, tables, images and links belong to the DOCX feature itself. TestPDFDrawsKernedText holds the PDF to drawing what it measured: glyph ids with TJ adjustments, copying out as the text. AGENTS.md gains a fonts section: the manifest, the vendoring, the shaper pin and its HarfBuzz golden test, the gate, and how to push a branch this large to the hub.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
4 files changed,
+557
−0
AGENTS.md+44 −0internal/docx/docx.go+327 −0internal/docx/docx_test.go+163 −0internal/export/pdf_test.go+23 −0
CI passed — run details
Intent
diff --git a/AGENTS.md b/AGENTS.md| index fba97f0..b4e3f6b 100644 |
| --- a/AGENTS.md |
| +++ b/AGENTS.md |
| @@ -468,6 +468,50 @@ would have caught it.) |
| invisible in a unit test, which is what `scripts/browser-drive.sh` is |
| for. |
| +## Fonts: one manifest, and never a flash (Paul, 2026-10-24) |
| + |
| +The default font is **Carlito** ("it kind of has to be Carlito": Calibri's |
| +metrics, so documents break lines in Word as they do here). **There is |
| +never a flash of unstyled text, ever**, and that includes a visible gap of |
| +invisible text. The font menu shows each font in its own face. The design |
| +is `polish-audit/fonts/NO-FOUT.md`; this is how it is built. |
| + |
| +- **`internal/fonts/fonts.json` is the vocabulary.** It holds sixteen |
| + families in picker order, with each key (mark `font_<key>`), name, Word |
| + name and fallback. internal/fonts embeds it and `fixtures/fonts.mjs` |
| + imports it. `core/tests/fragment.rs` holds MARKS to it through |
| + `families.bin`. `sans`, `serif` and `mono` are Liberation, for ever. To |
| + add a family, add a manifest entry and a MARKS row, rerun the vendoring |
| + and the two generators below, then regenerate the fragment vectors. |
| +- **Files come from `scripts/vendor-fonts.py`** (fontTools, outside the |
| + repo's Python), using `sources.json` and `files/SOURCES.lock`. Its output |
| + is byte-reproducible. A family whose licence reserves its name is its |
| + authors' static files, unchanged. Each style is a TTF (PDF and Mac) plus |
| + a lossless woff2 of it (browser). The picker previews are renamed |
| + subsets. |
| +- **The PDF shapes with go-text/typesetting**, pinned past v0.3.5, which |
| + applied none of Inter's kerning. `TestShapingMatchesHarfBuzz` holds it |
| + to HarfBuzz's own advances (`scripts/harfbuzz-golden.py`); rerun that |
| + script whenever the fonts change. |
| +- **The gate.** A document page preloads exactly the faces |
| + `export.FacesUsed` finds and renders the sheet held. `fixtures/fonts-gate.mjs` |
| + shows it once they have loaded and the editor has paginated. A face that |
| + misses 2.5 s commits to its fallback for the visit. Every other path to |
| + the screen goes through `fixtures/font-session.mjs`: a pick, a |
| + collaborator's update, a paste, bold and italic. **The chrome uses no |
| + webfont of Docs'**, so the toolbar glyphs and page numbers are system |
| + faces; keep it that way. (estilo's Onest is the exception, and it is |
| + estilo's: it is `font-display: swap`.) |
| +- The browser checks (`go test -tags browser -run Font ./internal/docs/`) |
| + slow the fonts down through the production middleware and fail on any |
| + frame that shows text in a face that has not loaded. |
| + |
| +**Pushing a branch that adds fonts:** the hub drops a chunked |
| +receive-pack and answers 200 with an empty body, which git reports as |
| +"unexpected disconnect while reading sideband packet". Push with |
| +`git -c http.postBuffer=157286400 push` so the pack goes out with a |
| +length. |
| + |
| ## Work in a worktree, and catch main up first |
| Take a worktree for the work. Do not work in the shared checkout. |
diff --git a/internal/docx/docx.go b/internal/docx/docx.go| new file mode 100644 |
| index 0000000..680a24c |
| --- /dev/null |
| +++ b/internal/docx/docx.go |
| @@ -0,0 +1,327 @@ |
| +// Package docx is the font half of Docs' Word files: how a document's |
| +// families are written into a .docx and read back out of one. |
| +// |
| +// Paul's default 5 (2026-10-24): Carlito is written as Calibri and |
| +// Caladea as Cambria, the Office faces they are metric twins of, so a |
| +// document breaks its lines in Word where it does here; every other |
| +// family is named, not embedded; and on the way in Calibri becomes |
| +// Carlito and Cambria Caladea. The names are internal/fonts' manifest |
| +// (Docx.Name, and ByDocxName for the way back). |
| +// |
| +// Docs has no DOCX import or export yet (spec §11.2/§11.3). This package |
| +// is deliberately only what the font rulings need, so that the feature |
| +// builds on it rather than re-deciding them: paragraphs and headings, |
| +// runs with their family, weight, slope and size, the document default |
| +// in w:docDefaults and every family named in fontTable.xml. Lists, |
| +// tables, images and links are not written yet, and Read reads only |
| +// paragraph text and run formatting. Nothing routes to it. |
| +package docx |
| + |
| +import ( |
| + "archive/zip" |
| + "bytes" |
| + "encoding/xml" |
| + "fmt" |
| + "io" |
| + "sort" |
| + "strconv" |
| + "strings" |
| + |
| + "github.com/carlosframework/docs/internal/export" |
| + "github.com/carlosframework/docs/internal/fonts" |
| +) |
| + |
| +const wNS = "http://schemas.openxmlformats.org/wordprocessingml/2006/main" |
| + |
| +// Write writes doc as a .docx. |
| +func Write(doc export.Document) ([]byte, error) { |
| + used := map[*fonts.Family]bool{fonts.Default: true} |
| + var body strings.Builder |
| + var walk func(nodes []export.Node) |
| + walk = func(nodes []export.Node) { |
| + for _, n := range nodes { |
| + switch n.Type { |
| + case "paragraph", "heading": |
| + body.WriteString("<w:p>") |
| + if n.Type == "heading" { |
| + level := n.Attrs.Level |
| + if level < 1 || level > 6 { |
| + level = 1 |
| + } |
| + fmt.Fprintf(&body, `<w:pPr><w:pStyle w:val="Heading%d"/></w:pPr>`, level) |
| + } |
| + for _, run := range n.Content { |
| + if run.IsText() && run.Text != "" { |
| + writeRun(&body, run, used) |
| + } |
| + } |
| + body.WriteString("</w:p>") |
| + default: |
| + walk(n.Content) |
| + } |
| + } |
| + } |
| + walk(doc.Root.Content) |
| + |
| + families := make([]*fonts.Family, 0, len(used)) |
| + for f := range used { |
| + families = append(families, f) |
| + } |
| + sort.Slice(families, func(i, j int) bool { return families[i].Index < families[j].Index }) |
| + |
| + var buf bytes.Buffer |
| + z := zip.NewWriter(&buf) |
| + files := []struct{ name, body string }{ |
| + {"[Content_Types].xml", contentTypes}, |
| + {"_rels/.rels", rootRels}, |
| + {"word/_rels/document.xml.rels", documentRels}, |
| + {"word/document.xml", `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>` + |
| + `<w:document xmlns:w="` + wNS + `"><w:body>` + body.String() + `</w:body></w:document>`}, |
| + {"word/styles.xml", styles()}, |
| + {"word/fontTable.xml", fontTable(families)}, |
| + } |
| + for _, f := range files { |
| + w, err := z.Create(f.name) |
| + if err != nil { |
| + return nil, err |
| + } |
| + if _, err := io.WriteString(w, f.body); err != nil { |
| + return nil, err |
| + } |
| + } |
| + if err := z.Close(); err != nil { |
| + return nil, err |
| + } |
| + return buf.Bytes(), nil |
| +} |
| + |
| +func writeRun(b *strings.Builder, run export.Node, used map[*fonts.Family]bool) { |
| + names := make([]string, len(run.Marks)) |
| + for i, m := range run.Marks { |
| + names[i] = m.Type |
| + } |
| + family := fonts.ForMarks(names) |
| + if run.Has("code") { |
| + family = fonts.Code |
| + } |
| + used[family] = true |
| + var props strings.Builder |
| + if family != fonts.Default { |
| + n := xmlAttr(family.Docx.Name) |
| + fmt.Fprintf(&props, `<w:rFonts w:ascii="%s" w:hAnsi="%s" w:cs="%s" w:eastAsia="%s"/>`, n, n, n, n) |
| + } |
| + if run.Has("strong") { |
| + props.WriteString("<w:b/>") |
| + } |
| + if run.Has("em") { |
| + props.WriteString("<w:i/>") |
| + } |
| + for _, mark := range export.SizeMarks() { |
| + if run.Has(mark) { |
| + if pt, ok := export.SizePt(mark); ok { |
| + fmt.Fprintf(&props, `<w:sz w:val="%d"/>`, int(pt*2)) |
| + } |
| + break |
| + } |
| + } |
| + b.WriteString("<w:r>") |
| + if props.Len() > 0 { |
| + b.WriteString("<w:rPr>" + props.String() + "</w:rPr>") |
| + } |
| + b.WriteString(`<w:t xml:space="preserve">` + xmlText(run.Text) + "</w:t></w:r>") |
| +} |
| + |
| +// styles puts the default family in w:docDefaults, where Word and an |
| +// importer read a document's own default from, at Docs' 12pt body. |
| +func styles() string { |
| + n := xmlAttr(fonts.Default.Docx.Name) |
| + var b strings.Builder |
| + b.WriteString(`<?xml version="1.0" encoding="UTF-8" standalone="yes"?><w:styles xmlns:w="` + wNS + `">`) |
| + fmt.Fprintf(&b, `<w:docDefaults><w:rPrDefault><w:rPr><w:rFonts w:ascii="%s" w:hAnsi="%s" w:cs="%s" w:eastAsia="%s"/>`+ |
| + `<w:sz w:val="%d"/><w:szCs w:val="%d"/></w:rPr></w:rPrDefault></w:docDefaults>`, n, n, n, n, int(export.BodySizePt*2), int(export.BodySizePt*2)) |
| + b.WriteString(`<w:style w:type="paragraph" w:default="1" w:styleId="Normal"><w:name w:val="Normal"/></w:style>`) |
| + for level := 1; level <= 6; level++ { |
| + fmt.Fprintf(&b, `<w:style w:type="paragraph" w:styleId="Heading%d"><w:name w:val="heading %d"/><w:basedOn w:val="Normal"/>`+ |
| + `<w:next w:val="Normal"/><w:qFormat/><w:rPr><w:b/></w:rPr></w:style>`, level, level) |
| + } |
| + b.WriteString(`</w:styles>`) |
| + return b.String() |
| +} |
| + |
| +// fontTable names every family the document uses, with what Word needs |
| +// to substitute sensibly when it does not have one: its generic family, |
| +// pitch, panose and nearest Office face. |
| +func fontTable(families []*fonts.Family) string { |
| + var b strings.Builder |
| + b.WriteString(`<?xml version="1.0" encoding="UTF-8" standalone="yes"?><w:fonts xmlns:w="` + wNS + `">`) |
| + for _, f := range families { |
| + fmt.Fprintf(&b, `<w:font w:name="%s">`, xmlAttr(f.Docx.Name)) |
| + if f.Docx.AltName != "" { |
| + fmt.Fprintf(&b, `<w:altName w:val="%s"/>`, xmlAttr(f.Docx.AltName)) |
| + } |
| + if f.Docx.Panose != "" { |
| + fmt.Fprintf(&b, `<w:panose1 w:val="%s"/>`, xmlAttr(f.Docx.Panose)) |
| + } |
| + fmt.Fprintf(&b, `<w:family w:val="%s"/><w:pitch w:val="%s"/></w:font>`, xmlAttr(f.Docx.Family), xmlAttr(f.Docx.Pitch)) |
| + } |
| + b.WriteString(`</w:fonts>`) |
| + return b.String() |
| +} |
| + |
| +func xmlText(s string) string { |
| + var b bytes.Buffer |
| + _ = xml.EscapeText(&b, []byte(s)) |
| + return b.String() |
| +} |
| + |
| +func xmlAttr(s string) string { return strings.ReplaceAll(xmlText(s), `"`, """) } |
| + |
| +const contentTypes = `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>` + |
| + `<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">` + |
| + `<Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>` + |
| + `<Default Extension="xml" ContentType="application/xml"/>` + |
| + `<Override PartName="/word/document.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"/>` + |
| + `<Override PartName="/word/styles.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml"/>` + |
| + `<Override PartName="/word/fontTable.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.fontTable+xml"/>` + |
| + `</Types>` |
| + |
| +const rootRels = `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>` + |
| + `<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">` + |
| + `<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="word/document.xml"/>` + |
| + `</Relationships>` |
| + |
| +const documentRels = `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>` + |
| + `<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">` + |
| + `<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/>` + |
| + `<Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable" Target="fontTable.xml"/>` + |
| + `</Relationships>` |
| + |
| +// Read reads a .docx's paragraphs and their runs' formatting. Each run's |
| +// face comes from its own w:rFonts, else the document's w:docDefaults; |
| +// a Word face with a twin here becomes the twin (Calibri is Carlito, |
| +// Cambria is Caladea), and a run in Docs' own default carries no mark. |
| +// A face Docs does not have reads as the default and is reported in |
| +// unknown, for the import report spec §11.2 requires. |
| +func Read(data []byte) (doc export.Document, unknown []string, err error) { |
| + z, err := zip.NewReader(bytes.NewReader(data), int64(len(data))) |
| + if err != nil { |
| + return doc, nil, fmt.Errorf("docx: %w", err) |
| + } |
| + part := func(name string) ([]byte, error) { |
| + for _, f := range z.File { |
| + if f.Name == name { |
| + r, err := f.Open() |
| + if err != nil { |
| + return nil, err |
| + } |
| + defer r.Close() |
| + return io.ReadAll(io.LimitReader(r, 64<<20)) |
| + } |
| + } |
| + return nil, nil |
| + } |
| + missing := map[string]bool{} |
| + resolve := func(name string) *fonts.Family { |
| + if name == "" { |
| + return nil |
| + } |
| + f, ok := fonts.ByDocxName(name) |
| + if !ok { |
| + missing[name] = true |
| + return nil |
| + } |
| + return f |
| + } |
| + |
| + base := fonts.Default |
| + if s, err := part("word/styles.xml"); err != nil { |
| + return doc, nil, err |
| + } else if s != nil { |
| + var st struct { |
| + Fonts struct { |
| + ASCII string `xml:"ascii,attr"` |
| + } `xml:"docDefaults>rPrDefault>rPr>rFonts"` |
| + } |
| + if err := xml.Unmarshal(s, &st); err != nil { |
| + return doc, nil, fmt.Errorf("docx: styles.xml: %w", err) |
| + } |
| + if f := resolve(st.Fonts.ASCII); f != nil { |
| + base = f |
| + } |
| + } |
| + |
| + d, err := part("word/document.xml") |
| + if err != nil { |
| + return doc, nil, err |
| + } |
| + if d == nil { |
| + return doc, nil, fmt.Errorf("docx: no word/document.xml") |
| + } |
| + type val struct { |
| + Val string `xml:"val,attr"` |
| + } |
| + var parsed struct { |
| + Body struct { |
| + Paragraphs []struct { |
| + Style val `xml:"pPr>pStyle"` |
| + Runs []struct { |
| + Fonts struct { |
| + ASCII string `xml:"ascii,attr"` |
| + } `xml:"rPr>rFonts"` |
| + Bold *struct{} `xml:"rPr>b"` |
| + Italic *struct{} `xml:"rPr>i"` |
| + Size val `xml:"rPr>sz"` |
| + Text []string `xml:"t"` |
| + } `xml:"r"` |
| + } `xml:"p"` |
| + } `xml:"body"` |
| + } |
| + if err := xml.Unmarshal(d, &parsed); err != nil { |
| + return doc, nil, fmt.Errorf("docx: document.xml: %w", err) |
| + } |
| + |
| + doc.Root = export.Node{Type: "doc"} |
| + for _, p := range parsed.Body.Paragraphs { |
| + block := export.Node{Type: "paragraph"} |
| + if lvl, ok := strings.CutPrefix(p.Style.Val, "Heading"); ok { |
| + if n, err := strconv.Atoi(lvl); err == nil && n >= 1 && n <= 6 { |
| + block = export.Node{Type: "heading", Attrs: export.Attrs{Level: n}} |
| + } |
| + } |
| + for _, r := range p.Runs { |
| + text := strings.Join(r.Text, "") |
| + if text == "" { |
| + continue |
| + } |
| + run := export.Node{Type: "text", Text: text} |
| + family := base |
| + if f := resolve(r.Fonts.ASCII); f != nil { |
| + family = f |
| + } |
| + if family != fonts.Default { |
| + run.Marks = append(run.Marks, export.Mark{Type: family.Mark()}) |
| + } |
| + if half, err := strconv.Atoi(r.Size.Val); err == nil { |
| + for _, mark := range export.SizeMarks() { |
| + if pt, ok := export.SizePt(mark); ok && int(pt*2) == half { |
| + run.Marks = append(run.Marks, export.Mark{Type: mark}) |
| + break |
| + } |
| + } |
| + } |
| + if r.Bold != nil { |
| + run.Marks = append(run.Marks, export.Mark{Type: "strong"}) |
| + } |
| + if r.Italic != nil { |
| + run.Marks = append(run.Marks, export.Mark{Type: "em"}) |
| + } |
| + block.Content = append(block.Content, run) |
| + } |
| + doc.Root.Content = append(doc.Root.Content, block) |
| + } |
| + for name := range missing { |
| + unknown = append(unknown, name) |
| + } |
| + sort.Strings(unknown) |
| + return doc, unknown, nil |
| +} |
diff --git a/internal/docx/docx_test.go b/internal/docx/docx_test.go| new file mode 100644 |
| index 0000000..b551df6 |
| --- /dev/null |
| +++ b/internal/docx/docx_test.go |
| @@ -0,0 +1,163 @@ |
| +package docx_test |
| + |
| +import ( |
| + "archive/zip" |
| + "bytes" |
| + "io" |
| + "strings" |
| + "testing" |
| + |
| + "github.com/carlosframework/docs/internal/docx" |
| + "github.com/carlosframework/docs/internal/export" |
| +) |
| + |
| +func part(t *testing.T, data []byte, name string) string { |
| + t.Helper() |
| + z, err := zip.NewReader(bytes.NewReader(data), int64(len(data))) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + for _, f := range z.File { |
| + if f.Name == name { |
| + r, _ := f.Open() |
| + b, _ := io.ReadAll(r) |
| + return string(b) |
| + } |
| + } |
| + t.Fatalf("no %s in the .docx", name) |
| + return "" |
| +} |
| + |
| +// Paul's default 5: Carlito goes to Word as Calibri and Caladea as |
| +// Cambria, so the document breaks its lines in Word where it does here, |
| +// and both come back as themselves. Carlito is the default, so it is |
| +// the document's own default (w:docDefaults) and its runs carry no |
| +// mark either way. |
| +func TestCarlitoAndCaladeaRoundTripAsCalibriAndCambria(t *testing.T) { |
| + doc, err := export.Parse("", `{"type":"doc","content":[ |
| + {"type":"heading","attrs":{"level":2},"content":[{"type":"text","text":"Minutes"}]}, |
| + {"type":"paragraph","content":[ |
| + {"type":"text","text":"Body text, "}, |
| + {"type":"text","text":"bold","marks":[{"type":"strong"}]}, |
| + {"type":"text","text":" and a Cambria twin","marks":[{"type":"font_caladea"},{"type":"em"}]}, |
| + {"type":"text","text":" and Inter at 14","marks":[{"type":"font_inter"},{"type":"size_14"}]}, |
| + {"type":"text","text":" and Arial's twin","marks":[{"type":"font_sans"}]} |
| + ]}]}`) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + data, err := docx.Write(doc) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + |
| + styles := part(t, data, "word/styles.xml") |
| + if !strings.Contains(styles, `<w:rPrDefault><w:rPr><w:rFonts w:ascii="Calibri"`) { |
| + t.Errorf("the document default is not Calibri: %s", styles) |
| + } |
| + body := part(t, data, "word/document.xml") |
| + if strings.Contains(body, "Carlito") || strings.Contains(body, "Caladea") { |
| + t.Error("the open-source names leaked into the Word file; Word would substitute them") |
| + } |
| + if !strings.Contains(body, `<w:rFonts w:ascii="Cambria"`) { |
| + t.Error("the Caladea run is not written as Cambria") |
| + } |
| + if !strings.Contains(body, `<w:rFonts w:ascii="Inter"`) || !strings.Contains(body, `<w:rFonts w:ascii="Arial"`) { |
| + t.Error("Inter is named as itself and Liberation Sans as Arial") |
| + } |
| + table := part(t, data, "word/fontTable.xml") |
| + for _, name := range []string{"Calibri", "Cambria", "Inter", "Arial"} { |
| + if !strings.Contains(table, `<w:font w:name="`+name+`">`) { |
| + t.Errorf("fontTable.xml does not name %s", name) |
| + } |
| + } |
| + if !strings.Contains(table, `<w:font w:name="Inter"><w:altName w:val="Arial"/>`) { |
| + t.Error("Inter does not tell Word what to fall back to") |
| + } |
| + |
| + back, unknown, err := docx.Read(data) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if len(unknown) > 0 { |
| + t.Errorf("faces Docs ships came back unknown: %v", unknown) |
| + } |
| + runs := back.Root.Content[1].Content |
| + want := []struct { |
| + text string |
| + marks string |
| + }{ |
| + {"Body text, ", ""}, |
| + {"bold", "strong"}, |
| + {" and a Cambria twin", "font_caladea em"}, |
| + {" and Inter at 14", "font_inter size_14"}, |
| + {" and Arial's twin", "font_sans"}, |
| + } |
| + if back.Root.Content[0].Type != "heading" || back.Root.Content[0].Attrs.Level != 2 { |
| + t.Errorf("the heading came back as %+v", back.Root.Content[0]) |
| + } |
| + if len(runs) != len(want) { |
| + t.Fatalf("got %d runs back, want %d: %+v", len(runs), len(want), runs) |
| + } |
| + for i, w := range want { |
| + var marks []string |
| + for _, m := range runs[i].Marks { |
| + marks = append(marks, m.Type) |
| + } |
| + if runs[i].Text != w.text || strings.Join(marks, " ") != w.marks { |
| + t.Errorf("run %d came back %q %v, want %q [%s]", i, runs[i].Text, marks, w.text, w.marks) |
| + } |
| + } |
| +} |
| + |
| +// A Word document whose own default is Calibri, written by Word, is a |
| +// Carlito document here; one whose default is Cambria is Caladea |
| +// throughout; and a face Docs does not have is reported for the import |
| +// report rather than silently replaced. |
| +func TestImportingWordsDefaults(t *testing.T) { |
| + for _, c := range []struct { |
| + defaultFace, runFace string |
| + marks string |
| + unknown string |
| + }{ |
| + {"Calibri", "", "", ""}, |
| + {"Cambria", "", "font_caladea", ""}, |
| + {"Calibri", "Cambria", "font_caladea", ""}, |
| + {"Cambria", "Calibri", "", ""}, |
| + {"Calibri", "Wingdings", "", "Wingdings"}, |
| + } { |
| + data := wordFile(t, c.defaultFace, c.runFace) |
| + doc, unknown, err := docx.Read(data) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + var marks []string |
| + for _, m := range doc.Root.Content[0].Content[0].Marks { |
| + marks = append(marks, m.Type) |
| + } |
| + if strings.Join(marks, " ") != c.marks { |
| + t.Errorf("default %s, run %q: marks %v, want [%s]", c.defaultFace, c.runFace, marks, c.marks) |
| + } |
| + if strings.Join(unknown, ",") != c.unknown { |
| + t.Errorf("default %s, run %q: unknown %v, want %q", c.defaultFace, c.runFace, unknown, c.unknown) |
| + } |
| + } |
| +} |
| + |
| +// wordFile is a minimal .docx the way Word writes one: the default in |
| +// styles.xml, an optional run override. |
| +func wordFile(t *testing.T, defaultFace, runFace string) []byte { |
| + t.Helper() |
| + var buf bytes.Buffer |
| + z := zip.NewWriter(&buf) |
| + w, _ := z.Create("word/styles.xml") |
| + io.WriteString(w, `<w:styles xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"><w:docDefaults><w:rPrDefault><w:rPr><w:rFonts w:ascii="`+defaultFace+`" w:hAnsi="`+defaultFace+`"/></w:rPr></w:rPrDefault></w:docDefaults></w:styles>`) |
| + rpr := "" |
| + if runFace != "" { |
| + rpr = `<w:rPr><w:rFonts w:ascii="` + runFace + `" w:hAnsi="` + runFace + `"/></w:rPr>` |
| + } |
| + w, _ = z.Create("word/document.xml") |
| + io.WriteString(w, `<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"><w:body><w:p><w:r>`+rpr+`<w:t>Hello</w:t></w:r></w:p></w:body></w:document>`) |
| + z.Close() |
| + return buf.Bytes() |
| +} |
diff --git a/internal/export/pdf_test.go b/internal/export/pdf_test.go| index 9ebdcd4..095e5d3 100644 |
| --- a/internal/export/pdf_test.go |
| +++ b/internal/export/pdf_test.go |
| @@ -696,3 +696,26 @@ func TestPDFDropsAnAlignmentItDoesNotKnow(t *testing.T) { |
| } |
| } |
| } |
| + |
| +// The PDF draws what it measured: kerned. Each glyph's own advance is |
| +// in the font's /W array, and the shaped one differs by a TJ adjustment |
| +// between glyphs, so "AVATAR" in Carlito carries adjustments and a run |
| +// in a pair-free string carries none. Without them the PDF would wrap |
| +// at the screen's line breaks and then draw the words wider than the |
| +// line it put them on. |
| +func TestPDFDrawsKernedText(t *testing.T) { |
| + js := `{"type":"doc","content":[{"type":"paragraph","content":[{"type":"text","text":"AVATAR"}]}]}` |
| + data, _ := mustPDF(t, mustParse(t, "", js)) |
| + f := readPDF(t, data) |
| + content := f.content(t, f.pages(t)[0]) |
| + m := regexp.MustCompile(`\[([^\]]*)\] TJ`).FindStringSubmatch(content) |
| + if m == nil { |
| + t.Fatalf("no TJ in the page: %s", content) |
| + } |
| + if !regexp.MustCompile(`> -?[\d.]+ <`).MatchString(m[1]) { |
| + t.Errorf("AVATAR is drawn with no kerning adjustments: %s", m[1]) |
| + } |
| + if got := f.text(t, f.pages(t)[0]); got != "AVATAR" { |
| + t.Errorf("the kerned run copies out as %q", got) |
| + } |
| +} |