Dolan f03253eb22 feat(layout): lay out equations Word builds up, as Word does (#3685)
* feat(layout): lay out equations Word builds up, as Word does

Lay out the equations Word builds up (fractions, scripts, roots, sums and
integrals with their limits, brackets that grow, matrices and cases,
accents, bars, functions and limits, and braces) by the rules of Cambria
Math's OpenType MATH table, with the font's own widths, ink and
constants, as Word's PDFs of word-stops-equations, -equation-settings
and -equation-font show. Each probe's width in its line of text is
Word's to within 0.03 points and its line's height within Word's own
drawing grid, but two roots, which are within 0.1 points.

What of Cambria Math the layout needs is generated from Word's own copy
of the font by scripts/generate-cambria-math.ts into
src/layout/cambria-math.ts, which replaces the widths read from Word's
PDFs (equation-widths.ts) and the table of Cambria Math's own widths
within 1.5 thousandths of an em, the integrals with their italic
correction, and ◇, which Cambria Math hasn't, stops.

The layout keeps stopping where Word's PDFs leave the result unsettled:
scripts in sizes other than 11 points, superscripts in cramped parts,
brackets between two sizes, matrices whose rows are too tall to be
single spaced, thin spaces beside fractions, brackets, sums and
functions, boxes, normal text, equations too long for their line, maths
fonts other than Cambria Math, margins of displayed equations and space
between equations side by side in one m:oMathPara. Guessing past a stop,
an equation alone in its paragraph is laid out displayed first.
word-stops-equations2.ts writes the probes that settle them, each
validated against ooxml-schemas/.

* fix(layout): stop at a digit in a bold run or another alphabet, which Word hasn't been seen drawing

A digit in a bold or bold italic run was laid out as the plain digit, though Word draws a bold run's letters in
Unicode's bold alphabet (word-stops-equations.docx EQ19), which has bold digits too, wider than the plain ones. No
probe has shown which Word draws, so the layout stops there, as it does for a digit in another alphabet, and
word-stops-equations2.ts's EQ45 asks Word. The alphabets' unused first digits are gone.

* fix(layout): lay out an equation in a line of text in a numbered paragraph, stopping only at one alone after its list's number

* test(layout): probe an equation displayed (m:oMathPara) alone after its list's number too, as EQ38d
2026-10-05 15:41:50 +01:00
2021-12-02 14:57:35 -06:00
2023-03-18 21:27:08 +00:00
2022-11-26 19:33:58 +00:00
2020-08-01 17:49:25 +01:00
2016-03-26 23:43:57 +00:00
2024-10-14 00:09:03 +01:00

clippy the assistant

Easily generate and modify .docx files with JS/TS. Works for Node and on the Browser.


NPM version Downloads per month GitHub Action Workflow Status Known Vulnerabilities PRs Welcome codecov Docx.js Editor

drawing

Demo

Browser

Here are examples of docx being used with basic HTML/JS in a browser environment:

Here are examples of docx working in Angular:

Here are examples of docx working in React:

Here is an example of docx working in Vue.js:

Node

Press endpoint on the RunKit website:

RunKit Instructions

More here

How to use & Documentation

Please refer to the documentation at https://docx.js.org/ for details on how to use this library, examples and much more!

Shapes, watermarks and charts

Shapes, groups of shapes and diagrams with connectors come with docx, from docx/shapes, watermarks from docx/watermarks, and native Word charts from docx/charts. They are left out of docx itself, so documents that don't use them don't carry their code:

import { Document, Header, Paragraph } from "docx";
import { ChartRun } from "docx/charts";
import { ShapeRun } from "docx/shapes";
import { TextWatermark } from "docx/watermarks";

const doc = new Document({
    sections: [
        {
            headers: { default: new Header({ children: [new Paragraph({ children: [new TextWatermark({ text: "DRAFT" })] })] }) },
            children: [
                new Paragraph({ children: [new ShapeRun({ type: "ellipse", transformation: { width: 100, height: 60 } })] }),
                new Paragraph({
                    children: [new ChartRun({ type: "column", categories: ["Q1", "Q2"], series: [{ name: "Sales", values: [10, 20] }] })],
                }),
            ],
        },
    ],
});

In a page without a bundler, load dist/shapes.umd.cjs, dist/watermarks.umd.cjs or dist/charts.umd.cjs after dist/index.umd.cjs. They add the docxShapes, docxWatermarks and docxCharts globals. See the shapes, watermarks and charts documentation.

Playground

Experience docx in action through Docx.js Editor, an interactive playground where you can code and preview the results in real-time.

Examples

Check the demo folder for examples.

Contributing

Read the contribution guidelines here.

Used by

drawing drawing drawing drawing drawing drawing drawing drawing drawing drawing drawing drawing drawing drawing drawing

...and many more!


patreon browserstack

Made with 💖

S
Description
Lustro github.com/dolanmiu/docx
Readme MIT
414 MiB
Languages
TypeScript 96.8%
Python 3.1%