mirror of
https://github.com/dolanmiu/docx.git
synced 2026-11-30 16:47:55 +01:00
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
// Two lines in one and horizontal in vertical, Word's East Asian typography for runs of text
|
||||
|
||||
import * as fs from "fs";
|
||||
import { Document, Packer, PageTextDirectionType, Paragraph, TextRun, TwoLinesInOneBrackets } from "docx";
|
||||
|
||||
const doc = new Document({
|
||||
styles: {
|
||||
default: {
|
||||
document: {
|
||||
run: {
|
||||
font: { ascii: "Calibri", eastAsia: "SimSun" },
|
||||
size: 32,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
sections: [
|
||||
{
|
||||
children: [
|
||||
new Paragraph({
|
||||
children: [
|
||||
new TextRun("财政部"),
|
||||
new TextRun({ text: "国家税务总局", eastAsianLayout: { twoLinesInOne: true } }),
|
||||
new TextRun("文件"),
|
||||
],
|
||||
}),
|
||||
new Paragraph({
|
||||
children: [
|
||||
new TextRun("会议"),
|
||||
new TextRun({
|
||||
text: "二〇二六年十月",
|
||||
eastAsianLayout: { twoLinesInOne: true, brackets: TwoLinesInOneBrackets.ROUND },
|
||||
}),
|
||||
new TextRun("纪要"),
|
||||
],
|
||||
}),
|
||||
new Paragraph({
|
||||
children: [
|
||||
new TextRun("Runs in one group: "),
|
||||
new TextRun({ text: "bold ", bold: true, eastAsianLayout: { twoLinesInOne: true, group: 1 } }),
|
||||
new TextRun({ text: "and italic", italics: true, eastAsianLayout: { twoLinesInOne: true, group: 1 } }),
|
||||
],
|
||||
}),
|
||||
],
|
||||
},
|
||||
{
|
||||
properties: {
|
||||
page: { textDirection: PageTextDirectionType.TOP_TO_BOTTOM_RIGHT_TO_LEFT },
|
||||
},
|
||||
children: [
|
||||
new Paragraph({
|
||||
children: [
|
||||
new TextRun("令和"),
|
||||
new TextRun({ text: "8", eastAsianLayout: { horizontalInVertical: true } }),
|
||||
new TextRun("年"),
|
||||
new TextRun({ text: "10", eastAsianLayout: { horizontalInVertical: true } }),
|
||||
new TextRun("月"),
|
||||
],
|
||||
}),
|
||||
],
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
Packer.toBuffer(doc).then((buffer) => {
|
||||
fs.writeFileSync("My Document.docx", buffer);
|
||||
});
|
||||
File diff suppressed because one or more lines are too long
@@ -155,6 +155,74 @@ const doc = new Document({
|
||||
});
|
||||
```
|
||||
|
||||
### Two Lines in One
|
||||
|
||||
`eastAsianLayout` gives a run Word's East Asian typography (`w:eastAsianLayout`). `twoLinesInOne` sets the run's text in two lines within one line, each at half the text's size, as Word's Two Lines in One does, and `brackets` draws `"round"`, `"square"`, `"angle"` or `"curly"` brackets around it. Runs next to each other with the same `group` are set together, so the two lines can be formatted differently.
|
||||
|
||||
```ts live
|
||||
import { Document, Paragraph, TextRun, TwoLinesInOneBrackets } from "docx";
|
||||
|
||||
const doc = new Document({
|
||||
sections: [
|
||||
{
|
||||
children: [
|
||||
new Paragraph({
|
||||
children: [
|
||||
new TextRun({ text: "财政部", size: 32 }),
|
||||
new TextRun({ text: "国家税务总局", size: 32, eastAsianLayout: { twoLinesInOne: true } }),
|
||||
new TextRun({ text: "文件", size: 32 }),
|
||||
],
|
||||
}),
|
||||
new Paragraph({
|
||||
children: [
|
||||
new TextRun({ text: "Meeting ", size: 32 }),
|
||||
new TextRun({
|
||||
text: "October 2026",
|
||||
size: 32,
|
||||
eastAsianLayout: { twoLinesInOne: true, brackets: TwoLinesInOneBrackets.ROUND },
|
||||
}),
|
||||
],
|
||||
}),
|
||||
new Paragraph({
|
||||
children: [
|
||||
new TextRun({ text: "One group: ", size: 32 }),
|
||||
new TextRun({ text: "bold ", size: 32, bold: true, eastAsianLayout: { twoLinesInOne: true, group: 1 } }),
|
||||
new TextRun({ text: "and italic", size: 32, italics: true, eastAsianLayout: { twoLinesInOne: true, group: 1 } }),
|
||||
],
|
||||
}),
|
||||
],
|
||||
},
|
||||
],
|
||||
});
|
||||
```
|
||||
|
||||
In text that runs down the page, `horizontalInVertical` sets the run's text across the line, as Word's Horizontal in Vertical does. Word ignores it on a run that is also `twoLinesInOne`. `fitToLine` (`w:vertCompress`) asks for that text to be squeezed into the line's width, but Word ignores it ([MS-OI29500](https://learn.microsoft.com/en-us/openspecs/office_standards/ms-oi29500/efbc3ed1-1349-4572-93a0-a4e0f71e2a8b)), so keep text set across the line to a character or two:
|
||||
|
||||
```ts live
|
||||
import { Document, PageTextDirectionType, Paragraph, TextRun } from "docx";
|
||||
|
||||
const doc = new Document({
|
||||
sections: [
|
||||
{
|
||||
properties: {
|
||||
page: { textDirection: PageTextDirectionType.TOP_TO_BOTTOM_RIGHT_TO_LEFT },
|
||||
},
|
||||
children: [
|
||||
new Paragraph({
|
||||
children: [
|
||||
new TextRun("令和"),
|
||||
new TextRun({ text: "8", eastAsianLayout: { horizontalInVertical: true } }),
|
||||
new TextRun("年"),
|
||||
new TextRun({ text: "10", eastAsianLayout: { horizontalInVertical: true } }),
|
||||
new TextRun("月"),
|
||||
],
|
||||
}),
|
||||
],
|
||||
},
|
||||
],
|
||||
});
|
||||
```
|
||||
|
||||
### Shading and Highlighting
|
||||
|
||||
```ts live
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { Formatter } from "@export/formatter";
|
||||
|
||||
import { TwoLinesInOneBrackets, createEastAsianLayout } from "./east-asian-layout";
|
||||
|
||||
describe("createEastAsianLayout", () => {
|
||||
it("should write two lines in one as w:combine", () => {
|
||||
const tree = new Formatter().format(createEastAsianLayout({ twoLinesInOne: true }));
|
||||
expect(tree).to.deep.equal({
|
||||
"w:eastAsianLayout": { _attr: { "w:combine": true } },
|
||||
});
|
||||
});
|
||||
|
||||
it("should write the brackets around two lines in one as w:combineBrackets", () => {
|
||||
const tree = new Formatter().format(createEastAsianLayout({ twoLinesInOne: true, brackets: TwoLinesInOneBrackets.CURLY }));
|
||||
expect(tree).to.deep.equal({
|
||||
"w:eastAsianLayout": { _attr: { "w:combine": true, "w:combineBrackets": "curly" } },
|
||||
});
|
||||
});
|
||||
|
||||
it("should write horizontal in vertical fitted to the line as w:vert and w:vertCompress", () => {
|
||||
const tree = new Formatter().format(createEastAsianLayout({ horizontalInVertical: true, fitToLine: true }));
|
||||
expect(tree).to.deep.equal({
|
||||
"w:eastAsianLayout": { _attr: { "w:vert": true, "w:vertCompress": true } },
|
||||
});
|
||||
});
|
||||
|
||||
it("should write the group as w:id, first as the schema has it", () => {
|
||||
const tree = new Formatter().format(createEastAsianLayout({ group: 1, twoLinesInOne: true }));
|
||||
expect(Object.keys(tree["w:eastAsianLayout"]._attr)).to.deep.equal(["w:id", "w:combine"]);
|
||||
expect(tree).to.deep.equal({
|
||||
"w:eastAsianLayout": { _attr: { "w:id": 1, "w:combine": true } },
|
||||
});
|
||||
});
|
||||
|
||||
it("should write a group of 0", () => {
|
||||
const tree = new Formatter().format(createEastAsianLayout({ group: 0, horizontalInVertical: true }));
|
||||
expect(tree).to.deep.equal({
|
||||
"w:eastAsianLayout": { _attr: { "w:id": 0, "w:vert": true } },
|
||||
});
|
||||
});
|
||||
|
||||
it("should write false to turn off two lines in one that a style turns on", () => {
|
||||
const tree = new Formatter().format(createEastAsianLayout({ twoLinesInOne: false }));
|
||||
expect(tree).to.deep.equal({
|
||||
"w:eastAsianLayout": { _attr: { "w:combine": false } },
|
||||
});
|
||||
});
|
||||
|
||||
it("should throw for a group that isn't a number, rather than write it as w:id", () => {
|
||||
expect(() => createEastAsianLayout({ group: NaN })).to.throw();
|
||||
});
|
||||
|
||||
it("should throw for an infinite group, rather than write it as w:id", () => {
|
||||
expect(() => createEastAsianLayout({ group: Infinity })).to.throw();
|
||||
expect(() => createEastAsianLayout({ group: -Infinity })).to.throw();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,115 @@
|
||||
/**
|
||||
* East Asian layout module for WordprocessingML run properties.
|
||||
*
|
||||
* This module provides Word's two lines in one, which sets a run's text in two half-size lines within one line, and
|
||||
* horizontal in vertical, which sets a run's text across the line in text that runs down the page.
|
||||
*
|
||||
* Reference: ISO/IEC 29500-1, 17.3.2.10 eastAsianLayout
|
||||
*
|
||||
* @module
|
||||
*/
|
||||
import { BuilderElement, type XmlComponent } from "@file/xml-components";
|
||||
import { decimalNumber } from "@util/values";
|
||||
|
||||
/**
|
||||
* Brackets Word draws around text set as two lines in one.
|
||||
*
|
||||
* ## XSD Schema
|
||||
* ```xml
|
||||
* <xsd:simpleType name="ST_CombineBrackets">
|
||||
* <xsd:restriction base="xsd:string">
|
||||
* <xsd:enumeration value="none"/>
|
||||
* <xsd:enumeration value="round"/>
|
||||
* <xsd:enumeration value="square"/>
|
||||
* <xsd:enumeration value="angle"/>
|
||||
* <xsd:enumeration value="curly"/>
|
||||
* </xsd:restriction>
|
||||
* </xsd:simpleType>
|
||||
* ```
|
||||
*
|
||||
* @publicApi
|
||||
*/
|
||||
export const TwoLinesInOneBrackets = {
|
||||
/** No brackets */
|
||||
NONE: "none",
|
||||
/** Round brackets: ( ) */
|
||||
ROUND: "round",
|
||||
/** Square brackets: [ ] */
|
||||
SQUARE: "square",
|
||||
/** Angle brackets: < > */
|
||||
ANGLE: "angle",
|
||||
/** Curly brackets: { } */
|
||||
CURLY: "curly",
|
||||
} as const;
|
||||
|
||||
/**
|
||||
* Options for the East Asian layout of a run's text.
|
||||
*
|
||||
* @publicApi
|
||||
*/
|
||||
export type IEastAsianLayoutOptions = {
|
||||
/** Sets the run's text in two lines within one line, each at half the text's size, as Word's Two Lines in One does (`w:combine`) */
|
||||
readonly twoLinesInOne?: boolean;
|
||||
/** The brackets drawn around text set as two lines in one (`w:combineBrackets`). Without them, none are drawn. */
|
||||
readonly brackets?: (typeof TwoLinesInOneBrackets)[keyof typeof TwoLinesInOneBrackets];
|
||||
/**
|
||||
* Sets the run's text across the line in text that runs down the page, as Word's Horizontal in Vertical does (`w:vert`).
|
||||
* Word ignores it when `twoLinesInOne` is on.
|
||||
*/
|
||||
readonly horizontalInVertical?: boolean;
|
||||
/**
|
||||
* Asks for text set across the line in vertical text to be squeezed so that it fits in the width of the line
|
||||
* (`w:vertCompress`). Word ignores it (MS-OI29500, 17.3.2.10), so only other applications may squeeze the text.
|
||||
*/
|
||||
readonly fitToLine?: boolean;
|
||||
/**
|
||||
* Runs next to each other with the same group are laid out together, as one two lines in one or one piece of
|
||||
* horizontal in vertical text, so it can be made of runs formatted differently (`w:id`)
|
||||
*/
|
||||
readonly group?: number;
|
||||
};
|
||||
|
||||
type IEastAsianLayoutAttributes = {
|
||||
readonly id?: number;
|
||||
readonly combine?: boolean;
|
||||
readonly combineBrackets?: (typeof TwoLinesInOneBrackets)[keyof typeof TwoLinesInOneBrackets];
|
||||
readonly vert?: boolean;
|
||||
readonly vertCompress?: boolean;
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates the East Asian layout (`w:eastAsianLayout`) of a run's text.
|
||||
*
|
||||
* ## XSD Schema
|
||||
* ```xml
|
||||
* <xsd:complexType name="CT_EastAsianLayout">
|
||||
* <xsd:attribute name="id" type="ST_DecimalNumber" use="optional"/>
|
||||
* <xsd:attribute name="combine" type="s:ST_OnOff" use="optional"/>
|
||||
* <xsd:attribute name="combineBrackets" type="ST_CombineBrackets" use="optional"/>
|
||||
* <xsd:attribute name="vert" type="s:ST_OnOff" use="optional"/>
|
||||
* <xsd:attribute name="vertCompress" type="s:ST_OnOff" use="optional"/>
|
||||
* </xsd:complexType>
|
||||
* ```
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* createEastAsianLayout({ twoLinesInOne: true, brackets: TwoLinesInOneBrackets.ROUND });
|
||||
* ```
|
||||
*/
|
||||
export const createEastAsianLayout = ({
|
||||
twoLinesInOne,
|
||||
brackets,
|
||||
horizontalInVertical,
|
||||
fitToLine,
|
||||
group,
|
||||
}: IEastAsianLayoutOptions): XmlComponent =>
|
||||
new BuilderElement<IEastAsianLayoutAttributes>({
|
||||
name: "w:eastAsianLayout",
|
||||
attributes: {
|
||||
id: { key: "w:id", value: group === undefined ? undefined : decimalNumber(group) },
|
||||
combine: { key: "w:combine", value: twoLinesInOne },
|
||||
combineBrackets: { key: "w:combineBrackets", value: brackets },
|
||||
vert: { key: "w:vert", value: horizontalInVertical },
|
||||
vertCompress: { key: "w:vertCompress", value: fitToLine },
|
||||
},
|
||||
});
|
||||
@@ -14,6 +14,7 @@ export * from "./run-fonts";
|
||||
export * from "./sequential-identifier";
|
||||
export * from "./underline";
|
||||
export * from "./emphasis-mark";
|
||||
export * from "./east-asian-layout";
|
||||
export * from "./simple-field";
|
||||
export * from "./comment-run";
|
||||
export * from "./comments-extended";
|
||||
|
||||
@@ -25,6 +25,7 @@ import {
|
||||
} from "@file/xml-components";
|
||||
import type { PositiveUniversalMeasure, UniversalMeasure } from "@util/values";
|
||||
|
||||
import { type IEastAsianLayoutOptions, createEastAsianLayout } from "./east-asian-layout";
|
||||
import { type EmphasisMarkType, createEmphasisMark } from "./emphasis-mark";
|
||||
import { CharacterSpacing, Color, Highlight } from "./formatting";
|
||||
import { type ILanguageOptions, createLanguageComponent } from "./language";
|
||||
@@ -200,6 +201,12 @@ export type IRunStylePropertiesOptions = {
|
||||
readonly imprint?: boolean;
|
||||
readonly revision?: IRunPropertiesChangeOptions;
|
||||
readonly language?: ILanguageOptions;
|
||||
/**
|
||||
* Word's East Asian typography for the run's text: two lines in one, which sets it in two half-size lines within
|
||||
* one line, optionally in brackets, and horizontal in vertical, which sets it across the line in text that runs
|
||||
* down the page
|
||||
*/
|
||||
readonly eastAsianLayout?: IEastAsianLayoutOptions;
|
||||
readonly border?: IBorderOptions;
|
||||
readonly snapToGrid?: boolean;
|
||||
/** Hides the text. `false` shows text that the run's style hides. */
|
||||
@@ -402,6 +409,10 @@ export class RunProperties extends IgnoreIfEmptyXmlComponent {
|
||||
this.push(createLanguageComponent(options.language));
|
||||
}
|
||||
|
||||
if (options.eastAsianLayout) {
|
||||
this.push(createEastAsianLayout(options.eastAsianLayout));
|
||||
}
|
||||
|
||||
if (options.specVanish !== undefined) {
|
||||
// https://c-rex.net/projects/samples/ooxml/e1/Part4/OOXML_P4_DOCX_specVanish_topic_ID0EIE1O.html
|
||||
this.push(new OnOffElement("w:specVanish", options.specVanish));
|
||||
|
||||
@@ -6,6 +6,7 @@ import { FootnoteReferenceRun } from "@file/footnotes/footnote/run/reference-run
|
||||
import { Paragraph } from "@file/paragraph";
|
||||
import { ShadingType } from "@file/shading";
|
||||
|
||||
import { TwoLinesInOneBrackets } from "./east-asian-layout";
|
||||
import { EmphasisMarkType } from "./emphasis-mark";
|
||||
import { HighlightColor, TextEffect } from "./properties";
|
||||
import { PageNumber, Run } from "./run";
|
||||
@@ -119,6 +120,38 @@ describe("Run", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("#eastAsianLayout()", () => {
|
||||
it("should set the text as two lines in one in brackets", () => {
|
||||
const run = new Run({
|
||||
eastAsianLayout: { twoLinesInOne: true, brackets: TwoLinesInOneBrackets.ROUND },
|
||||
});
|
||||
const tree = new Formatter().format(run);
|
||||
expect(tree).to.deep.equal({
|
||||
"w:r": [{ "w:rPr": [{ "w:eastAsianLayout": { _attr: { "w:combine": true, "w:combineBrackets": "round" } } }] }],
|
||||
});
|
||||
});
|
||||
|
||||
it("should write w:eastAsianLayout after w:lang, as the schema orders them", () => {
|
||||
const run = new Run({
|
||||
eastAsianLayout: { horizontalInVertical: true },
|
||||
language: { eastAsia: "ja-JP" },
|
||||
specVanish: true,
|
||||
});
|
||||
const tree = new Formatter().format(run);
|
||||
expect(tree).to.deep.equal({
|
||||
"w:r": [
|
||||
{
|
||||
"w:rPr": [
|
||||
{ "w:lang": { _attr: { "w:eastAsia": "ja-JP" } } },
|
||||
{ "w:eastAsianLayout": { _attr: { "w:vert": true } } },
|
||||
{ "w:specVanish": {} },
|
||||
],
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("#smallCaps()", () => {
|
||||
it("it should add smallCaps to the properties", () => {
|
||||
const run = new Run({
|
||||
|
||||
@@ -2,6 +2,7 @@ import { describe, expect, it } from "vitest";
|
||||
|
||||
import {
|
||||
dateTimeValue,
|
||||
decimalNumber,
|
||||
hexColorValue,
|
||||
hpsMeasureValue,
|
||||
longHexNumber,
|
||||
@@ -86,6 +87,18 @@ describe("values", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("decimalNumber", () => {
|
||||
it("should floor a number to an integer", () => {
|
||||
expect(decimalNumber(10.7)).to.eq(10);
|
||||
expect(decimalNumber(-5.3)).to.eq(-6);
|
||||
});
|
||||
it("should throw on NaN and infinities, which aren't integers ST_DecimalNumber can hold", () => {
|
||||
expect(() => decimalNumber(NaN)).to.throw();
|
||||
expect(() => decimalNumber(Infinity)).to.throw();
|
||||
expect(() => decimalNumber(-Infinity)).to.throw();
|
||||
});
|
||||
});
|
||||
|
||||
describe("unsignedDecimalNumber", () => {
|
||||
it("should allow valid values", () => {
|
||||
expect(unsignedDecimalNumber(1243)).to.eq(1243);
|
||||
@@ -94,6 +107,7 @@ describe("values", () => {
|
||||
});
|
||||
it("should throw on invalid values", () => {
|
||||
expect(() => unsignedDecimalNumber(NaN)).to.throw();
|
||||
expect(() => unsignedDecimalNumber(Infinity)).to.throw();
|
||||
expect(() => unsignedDecimalNumber(-10)).to.throw();
|
||||
});
|
||||
});
|
||||
|
||||
+4
-4
@@ -88,16 +88,16 @@ export type RelativeMeasure = `${"-" | ""}${number}${"em" | "ex"}`;
|
||||
*
|
||||
* @param val - The number to validate and convert
|
||||
* @returns The floored integer value
|
||||
* @throws Error if the value is NaN
|
||||
* @throws Error if the value is NaN or infinite, which no integer can be written as
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* const num = decimalNumber(10.7); // Returns 10
|
||||
* const negative = decimalNumber(-5.3); // Returns -5
|
||||
* const negative = decimalNumber(-5.3); // Returns -6
|
||||
* ```
|
||||
*/
|
||||
export const decimalNumber = (val: number): number => {
|
||||
if (isNaN(val)) {
|
||||
if (!Number.isFinite(val)) {
|
||||
throw new Error(`Invalid value '${val}' specified. Must be an integer.`);
|
||||
}
|
||||
return Math.floor(val);
|
||||
@@ -110,7 +110,7 @@ export const decimalNumber = (val: number): number => {
|
||||
*
|
||||
* @param val - The number to validate and convert
|
||||
* @returns The floored positive integer value
|
||||
* @throws Error if the value is NaN or negative
|
||||
* @throws Error if the value is NaN, infinite or negative
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
|
||||
Reference in New Issue
Block a user