docx/subscript-text

Vertically align a run's glyphs below the baseline at a reduced size.

Library verdicts: python-docx: — docxjs: pass

Metadata

Feature iddocx/subscript-text
Formatdocx
Categorytext-formatting
Spececma-376-5-part-1 § 17.3.2.42 w:vertAlign

XPath assertions

ID / partPredicateXPathpython-docxdocxjs
subscript-element-present
word/document.xml
exist
The run carrying the subscript text must have a <w:vertAlign> child inside <w:rPr>.
//w:r[w:t[normalize-space()='Subscript text']]/w:rPr/w:vertAlign
subscript-val-is-subscript
word/document.xml
equal = subscript
w:val must equal 'subscript'.
//w:r[w:t[normalize-space()='Subscript text']]/w:rPr/w:vertAlign/@w:val

Render assertions

IDPredicateSelectorpython-docxdocxjs
subscript-text-renderedcss_selector/ match-text = Subscript text
A DOM node marked subscript (via <sub> or vertical-align: sub) must contain the fixture text.
.docx-wrapper sub, .docx-wrapper [style*='vertical-align: sub'], .docx-wrapper [style*='vertical-align:sub']pass
subscript-text-visiblecomputed_style = ^sub(?:$|\b) style=vertical-align
The computed vertical-align on the subscript run must be sub (or a sub-offset length).
.docx-wrapper :is(sub, span):not(:has(*))pass

Generator source

scripts/gen_subscript_text.py

#!/usr/bin/env python3
"""Generate ``fixtures/docx/subscript-text.docx``.

A minimal document exercising ``w:vertAlign w:val="subscript"``. One
paragraph containing one subscript run.
Satisfies the assertions in ``features/docx/subscript-text.json``.
"""

from __future__ import annotations

from pathlib import Path

from docx import Document

_REPO_ROOT = Path(__file__).resolve().parent.parent
_OUT = _REPO_ROOT / "fixtures" / "docx" / "subscript-text.docx"


def main() -> int:
    doc = Document()
    paragraph = doc.add_paragraph()
    run = paragraph.add_run("Subscript text")
    run.font.subscript = True
    _OUT.parent.mkdir(parents=True, exist_ok=True)
    doc.save(_OUT, reproducible=True)
    print(_OUT)
    return 0


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

Manifest (expanded)

{
  "$schema": "../manifest.schema.json",
  "id": "docx/subscript-text",
  "title": "Subscript text",
  "format": "docx",
  "category": "text-formatting",
  "summary": "Vertically align a run's glyphs below the baseline at a reduced size.",
  "spec": {
    "source": "ecma-376-5-part-1",
    "clause": "17.3.2.42",
    "element": "w:vertAlign",
    "rnc_reference": "spec/ecma-376-5/part-1/rnc/WordprocessingML.rnc",
    "xsd_reference": "spec/ecma-376-5/part-1/xsd/wml.xsd",
    "notes": "<w:vertAlign w:val=\"subscript\"/> lowers the run below the baseline and reduces its size. Valid w:val values are 'baseline' (default), 'superscript', 'subscript'. Unlike the toggle properties (w:b, w:i), w:val is required."
  },
  "fixtures": {
    "machine": "docx/subscript-text",
    "office": "docx/subscript-text"
  },
  "generator": {
    "python": "scripts/gen_subscript_text.py"
  },
  "assertions": [
    {
      "id": "subscript-element-present",
      "part": "word/document.xml",
      "namespaces": {
        "w": "http://schemas.openxmlformats.org/wordprocessingml/2006/main"
      },
      "xpath": "//w:r[w:t[normalize-space()='Subscript text']]/w:rPr/w:vertAlign",
      "must": "exist",
      "description": "The run carrying the subscript text must have a <w:vertAlign> child inside <w:rPr>."
    },
    {
      "id": "subscript-val-is-subscript",
      "part": "word/document.xml",
      "namespaces": {
        "w": "http://schemas.openxmlformats.org/wordprocessingml/2006/main"
      },
      "xpath": "//w:r[w:t[normalize-space()='Subscript text']]/w:rPr/w:vertAlign/@w:val",
      "must": "equal",
      "value": "subscript",
      "description": "w:val must equal 'subscript'."
    }
  ],
  "render_assertions": [
    {
      "id": "subscript-text-rendered",
      "kind": "css_selector",
      "selector": ".docx-wrapper sub, .docx-wrapper [style*='vertical-align: sub'], .docx-wrapper [style*='vertical-align:sub']",
      "must": "match-text",
      "value": "Subscript text",
      "description": "A DOM node marked subscript (via <sub> or vertical-align: sub) must contain the fixture text."
    },
    {
      "id": "subscript-text-visible",
      "kind": "computed_style",
      "selector": ".docx-wrapper :is(sub, span):not(:has(*))",
      "style_property": "vertical-align",
      "value": "^sub(?:$|\\b)",
      "description": "The computed vertical-align on the subscript run must be sub (or a sub-offset length)."
    }
  ]
}

Fixture

Download subscript-text.docx (18.6 KB)

Reference preview

Reference (machine, page 1 PNG)

docx/subscript-text page 1 reference

Reference PDF


Download PDF Open in PDF.js

Spec notes

<w:vertAlign w:val="subscript"/> lowers the run below the baseline and reduces its size. Valid w:val values are 'baseline' (default), 'superscript', 'subscript'. Unlike the toggle properties (w:b, w:i), w:val is required.