docx/embedded-font

A w:fontTable.xml entry referencing an embedded TrueType font part via <w:embedRegular> + an r:font relationship.

Library verdicts: python-docx: — docxjs: —

Metadata

Feature iddocx/embedded-font
Formatdocx
Categoryembedded-parts
Spececma-376-5-part-1 § 17.8.3.2 w:embedRegular

XPath assertions

ID / partPredicateXPathpython-docxdocxjs
font-entry-in-table
word/fontTable.xml
exist
A <w:font w:name='CorpusFakeFont'> entry must exist in fontTable.xml.
//w:font[@w:name='CorpusFakeFont']
embed-regular-ref
word/fontTable.xml
exist
The font entry must carry a <w:embedRegular r:id='...'/> child pointing at the font-data relationship.
//w:font[@w:name='CorpusFakeFont']/w:embedRegular/@r:id
font-data-relationship
word/_rels/fontTable.xml.rels
match = fonts/font1\.fntdata
The font-table part must declare a /font relationship targeting word/fonts/font1.fntdata.
//r:Relationship[@Type='http://schemas.openxmlformats.org/officeDocument/2006/relationships/font']/@Target
font-data-default-content-type
[Content_Types].xml
equal = application/x-fontdata
The .fntdata extension must be mapped to application/x-fontdata.
//ct:Default[@Extension='fntdata']/@ContentType

Render assertions

No render assertions declared.

Generator source

scripts/gen_embedded_font.py

#!/usr/bin/env python3
"""Generate ``fixtures/docx/embedded-font.docx``.

A minimal document embedding a stub TrueType font binary through the fork's
``FontTable.add_embedded_font`` API. The API wires together:

- a <w:font> entry in word/fontTable.xml;
- a <w:embedRegular r:id='...'/> child pointing at a font-data relationship;
- a binary part at word/fonts/font1.fntdata of content-type application/x-fontdata;
- an r:font relationship from fontTable.xml.rels to that part.

The embedded payload is a short stub — python-docx does not interrogate font
contents, so an OTTO-signed placeholder is sufficient to exercise every
packaging wire. No real font binary is vendored into the corpus.
"""

from __future__ import annotations

import tempfile
from pathlib import Path

from docx import Document

_REPO_ROOT = Path(__file__).resolve().parent.parent
_OUT = _REPO_ROOT / "fixtures" / "docx" / "embedded-font.docx"


_STUB_FONT = (
    b"OTTO\x00\x00\x00\x00"
    + b"\x00" * 128
    + b"corpus embedded-font fixture payload"
)


def main() -> int:
    # Fixed-name tempfile — python-docx reads the font bytes from disk and
    # copies them into the font part verbatim, so the filename leaks into
    # neither XML nor content. Still, keep a deterministic location.
    tmp = Path(tempfile.gettempdir()) / "corpus-embedded-font.ttf"
    tmp.write_bytes(_STUB_FONT)

    doc = Document()
    doc.add_paragraph(
        "This document embeds a stub font binary via FontTable.add_embedded_font."
    )
    doc.font_table.add_embedded_font(tmp, family="regular", name="CorpusFakeFont")

    _OUT.parent.mkdir(parents=True, exist_ok=True)
    doc.save(_OUT, reproducible=True)

    tmp.unlink(missing_ok=True)
    print(_OUT)
    return 0


if __name__ == "__main__":
    raise SystemExit(main())

Manifest (expanded)

{
  "$schema": "../manifest.schema.json",
  "id": "docx/embedded-font",
  "title": "Embedded font binary",
  "format": "docx",
  "category": "embedded-parts",
  "roles": [
    "authoring"
  ],
  "summary": "A w:fontTable.xml entry referencing an embedded TrueType font part via <w:embedRegular> + an r:font relationship.",
  "spec": {
    "source": "ecma-376-5-part-1",
    "clause": "17.8.3.2",
    "element": "w:embedRegular",
    "rnc_reference": "spec/ecma-376-5/part-1/rnc/WordprocessingML.rnc",
    "xsd_reference": "spec/ecma-376-5/part-1/xsd/wml.xsd",
    "notes": "Word supports embedding the binary of a TrueType/OpenType font inside the package so the document renders correctly on machines that do not have the font installed. The embed mechanism consists of three moving pieces: (1) a <w:font w:name='...'> entry in word/fontTable.xml, (2) a child <w:embedRegular r:id='...'/> (or embedBold/embedItalic/embedBoldItalic) pointing at (3) a binary part at word/fonts/fontN.fntdata of content-type application/x-fontdata. python-docx's FontTable.add_embedded_font glues all three together in one call. This literal fixture embeds a stub OTTO-signed font blob so only the packaging is asserted — the library does not validate font contents."
  },
  "fixtures": {
    "machine": "docx/embedded-font"
  },
  "generator": {
    "python": "scripts/gen_embedded_font.py"
  },
  "assertions": [
    {
      "id": "font-entry-in-table",
      "part": "word/fontTable.xml",
      "namespaces": {
        "w": "http://schemas.openxmlformats.org/wordprocessingml/2006/main"
      },
      "xpath": "//w:font[@w:name='CorpusFakeFont']",
      "must": "exist",
      "description": "A <w:font w:name='CorpusFakeFont'> entry must exist in fontTable.xml."
    },
    {
      "id": "embed-regular-ref",
      "part": "word/fontTable.xml",
      "namespaces": {
        "w": "http://schemas.openxmlformats.org/wordprocessingml/2006/main",
        "r": "http://schemas.openxmlformats.org/officeDocument/2006/relationships"
      },
      "xpath": "//w:font[@w:name='CorpusFakeFont']/w:embedRegular/@r:id",
      "must": "exist",
      "description": "The font entry must carry a <w:embedRegular r:id='...'/> child pointing at the font-data relationship."
    },
    {
      "id": "font-data-relationship",
      "part": "word/_rels/fontTable.xml.rels",
      "namespaces": {
        "r": "http://schemas.openxmlformats.org/package/2006/relationships"
      },
      "xpath": "//r:Relationship[@Type='http://schemas.openxmlformats.org/officeDocument/2006/relationships/font']/@Target",
      "must": "match",
      "value": "fonts/font1\\.fntdata",
      "description": "The font-table part must declare a /font relationship targeting word/fonts/font1.fntdata."
    },
    {
      "id": "font-data-default-content-type",
      "part": "[Content_Types].xml",
      "namespaces": {
        "ct": "http://schemas.openxmlformats.org/package/2006/content-types"
      },
      "xpath": "//ct:Default[@Extension='fntdata']/@ContentType",
      "must": "equal",
      "value": "application/x-fontdata",
      "description": "The .fntdata extension must be mapped to application/x-fontdata."
    }
  ]
}

Fixture

Download embedded-font.docx (19.1 KB)

Reference preview

No rendered reference is available for this case.

Spec notes

Word supports embedding the binary of a TrueType/OpenType font inside the package so the document renders correctly on machines that do not have the font installed. The embed mechanism consists of three moving pieces: (1) a <w:font w:name='...'> entry in word/fontTable.xml, (2) a child <w:embedRegular r:id='...'/> (or embedBold/embedItalic/embedBoldItalic) pointing at (3) a binary part at word/fonts/fontN.fntdata of content-type application/x-fontdata. python-docx's FontTable.add_embedded_font glues all three together in one call. This literal fixture embeds a stub OTTO-signed font blob so only the packaging is asserted — the library does not validate font contents.