Add more run properties and Universal measure

This commit is contained in:
Dolan Miu
2022-12-29 09:57:15 +00:00
parent 5907132062
commit 4513bb529b
15 changed files with 342 additions and 190 deletions

View File

@ -1,25 +1,23 @@
import { XmlAttributeComponent, XmlComponent } from "@file/xml-components"; import { NextAttributeComponent, XmlComponent } from "@file/xml-components";
import { twipsMeasureValue } from "@util/values"; import { PositiveUniversalMeasure, twipsMeasureValue } from "@util/values";
export interface IColumnAttributes { // <xsd:complexType name="CT_Column">
readonly width: number | string; // <xsd:attribute name="w" type="s:ST_TwipsMeasure" use="optional" />
readonly space?: number | string; // <xsd:attribute name="space" type="s:ST_TwipsMeasure" use="optional" default="0" />
} // </xsd:complexType>
export class ColumnAttributes extends XmlAttributeComponent<IColumnAttributes> { type IColumnAttributes = {
protected readonly xmlKeys = { readonly width: number | PositiveUniversalMeasure;
width: "w:w", readonly space?: number | PositiveUniversalMeasure;
space: "w:space", };
};
}
export class Column extends XmlComponent { export class Column extends XmlComponent {
public constructor({ width, space }: IColumnAttributes) { public constructor({ width, space }: IColumnAttributes) {
super("w:col"); super("w:col");
this.root.push( this.root.push(
new ColumnAttributes({ new NextAttributeComponent<IColumnAttributes>({
width: twipsMeasureValue(width), width: { key: "w:w", value: twipsMeasureValue(width) },
space: space === undefined ? undefined : twipsMeasureValue(space), space: { key: "w:space", value: space === undefined ? undefined : twipsMeasureValue(space) },
}), }),
); );
} }

View File

@ -1,5 +1,5 @@
import { XmlAttributeComponent, XmlComponent } from "@file/xml-components"; import { NextAttributeComponent, XmlComponent } from "@file/xml-components";
import { decimalNumber, twipsMeasureValue } from "@util/values"; import { decimalNumber, PositiveUniversalMeasure, twipsMeasureValue } from "@util/values";
import { Column } from "./column"; import { Column } from "./column";
@ -12,32 +12,23 @@ import { Column } from "./column";
// <xsd:attribute name="num" type="ST_DecimalNumber" use="optional" default="1"/> // <xsd:attribute name="num" type="ST_DecimalNumber" use="optional" default="1"/>
// <xsd:attribute name="sep" type="s:ST_OnOff" use="optional"/> // <xsd:attribute name="sep" type="s:ST_OnOff" use="optional"/>
// </xsd:complexType> // </xsd:complexType>
export interface IColumnsAttributes { export type IColumnsAttributes = {
readonly space?: number | string; readonly space?: number | PositiveUniversalMeasure;
readonly count?: number; readonly count?: number;
readonly separate?: boolean; readonly separate?: boolean;
readonly equalWidth?: boolean; readonly equalWidth?: boolean;
readonly children?: readonly Column[]; readonly children?: readonly Column[];
} };
export class ColumnsAttributes extends XmlAttributeComponent<IColumnsAttributes> {
protected readonly xmlKeys = {
space: "w:space",
count: "w:num",
separate: "w:sep",
equalWidth: "w:equalWidth",
};
}
export class Columns extends XmlComponent { export class Columns extends XmlComponent {
public constructor({ space, count, separate, equalWidth, children }: IColumnsAttributes) { public constructor({ space, count, separate, equalWidth, children }: IColumnsAttributes) {
super("w:cols"); super("w:cols");
this.root.push( this.root.push(
new ColumnsAttributes({ new NextAttributeComponent<Omit<IColumnsAttributes, "children">>({
space: space === undefined ? undefined : twipsMeasureValue(space), space: { key: "w:space", value: space === undefined ? undefined : twipsMeasureValue(space) },
count: count === undefined ? undefined : decimalNumber(count), count: { key: "w:num", value: count === undefined ? undefined : decimalNumber(count) },
separate, separate: { key: "w:sep", value: separate },
equalWidth, equalWidth: { key: "w:equalWidth", value: equalWidth },
}), }),
); );

View File

@ -1,6 +1,6 @@
// http://officeopenxml.com/WPsectionLineNumbering.php // http://officeopenxml.com/WPsectionLineNumbering.php
import { XmlAttributeComponent, XmlComponent } from "@file/xml-components"; import { NextAttributeComponent, XmlComponent } from "@file/xml-components";
import { decimalNumber, twipsMeasureValue } from "@util/values"; import { decimalNumber, PositiveUniversalMeasure, twipsMeasureValue } from "@util/values";
// <xsd:simpleType name="ST_LineNumberRestart"> // <xsd:simpleType name="ST_LineNumberRestart">
// <xsd:restriction base="xsd:string"> // <xsd:restriction base="xsd:string">
@ -26,27 +26,23 @@ export interface ILineNumberAttributes {
readonly countBy?: number; readonly countBy?: number;
readonly start?: number; readonly start?: number;
readonly restart?: LineNumberRestartFormat; readonly restart?: LineNumberRestartFormat;
readonly distance?: number | string; readonly distance?: number | PositiveUniversalMeasure;
}
export class LineNumberAttributes extends XmlAttributeComponent<ILineNumberAttributes> {
protected readonly xmlKeys = {
countBy: "w:countBy",
start: "w:start",
restart: "w:restart",
distance: "w:distance",
};
} }
export class LineNumberType extends XmlComponent { export class LineNumberType extends XmlComponent {
public constructor({ countBy, start, restart, distance }: ILineNumberAttributes) { public constructor({ countBy, start, restart, distance }: ILineNumberAttributes) {
super("w:lnNumType"); super("w:lnNumType");
this.root.push( this.root.push(
new LineNumberAttributes({ new NextAttributeComponent<{
countBy: countBy === undefined ? undefined : decimalNumber(countBy), readonly countBy?: number;
start: start === undefined ? undefined : decimalNumber(start), readonly start?: number;
restart, readonly restart?: LineNumberRestartFormat;
distance: distance === undefined ? undefined : twipsMeasureValue(distance), readonly distance?: number | PositiveUniversalMeasure;
}>({
countBy: { key: "w:countBy", value: countBy === undefined ? undefined : decimalNumber(countBy) },
start: { key: "w:start", value: start === undefined ? undefined : decimalNumber(start) },
restart: { key: "w:restart", value: restart },
distance: { key: "w:distance", value: distance === undefined ? undefined : twipsMeasureValue(distance) },
}), }),
); );
} }

View File

@ -1,5 +1,5 @@
import { XmlAttributeComponent, XmlComponent } from "@file/xml-components"; import { NextAttributeComponent, XmlComponent } from "@file/xml-components";
import { signedTwipsMeasureValue, twipsMeasureValue } from "@util/values"; import { PositiveUniversalMeasure, signedTwipsMeasureValue, twipsMeasureValue, UniversalMeasure } from "@util/values";
// <xsd:complexType name="CT_PageMar"> // <xsd:complexType name="CT_PageMar">
// <xsd:attribute name="top" type="ST_SignedTwipsMeasure" use="required"/> // <xsd:attribute name="top" type="ST_SignedTwipsMeasure" use="required"/>
@ -10,48 +10,36 @@ import { signedTwipsMeasureValue, twipsMeasureValue } from "@util/values";
// <xsd:attribute name="footer" type="s:ST_TwipsMeasure" use="required"/> // <xsd:attribute name="footer" type="s:ST_TwipsMeasure" use="required"/>
// <xsd:attribute name="gutter" type="s:ST_TwipsMeasure" use="required"/> // <xsd:attribute name="gutter" type="s:ST_TwipsMeasure" use="required"/>
// </xsd:complexType> // </xsd:complexType>
export interface IPageMarginAttributes { export type IPageMarginAttributes = {
readonly top?: number | string; readonly top?: number | UniversalMeasure;
readonly right?: number | string; readonly right?: number | PositiveUniversalMeasure;
readonly bottom?: number | string; readonly bottom?: number | UniversalMeasure;
readonly left?: number | string; readonly left?: number | PositiveUniversalMeasure;
readonly header?: number | string; readonly header?: number | PositiveUniversalMeasure;
readonly footer?: number | string; readonly footer?: number | PositiveUniversalMeasure;
readonly gutter?: number | string; readonly gutter?: number | PositiveUniversalMeasure;
} };
export class PageMarginAttributes extends XmlAttributeComponent<IPageMarginAttributes> {
protected readonly xmlKeys = {
top: "w:top",
right: "w:right",
bottom: "w:bottom",
left: "w:left",
header: "w:header",
footer: "w:footer",
gutter: "w:gutter",
};
}
export class PageMargin extends XmlComponent { export class PageMargin extends XmlComponent {
public constructor( public constructor(
top: number | string, top: number | UniversalMeasure,
right: number | string, right: number | PositiveUniversalMeasure,
bottom: number | string, bottom: number | UniversalMeasure,
left: number | string, left: number | PositiveUniversalMeasure,
header: number | string, header: number | PositiveUniversalMeasure,
footer: number | string, footer: number | PositiveUniversalMeasure,
gutter: number | string, gutter: number | PositiveUniversalMeasure,
) { ) {
super("w:pgMar"); super("w:pgMar");
this.root.push( this.root.push(
new PageMarginAttributes({ new NextAttributeComponent<IPageMarginAttributes>({
top: signedTwipsMeasureValue(top), top: { key: "w:top", value: signedTwipsMeasureValue(top) },
right: twipsMeasureValue(right), right: { key: "w:right", value: twipsMeasureValue(right) },
bottom: signedTwipsMeasureValue(bottom), bottom: { key: "w:bottom", value: signedTwipsMeasureValue(bottom) },
left: twipsMeasureValue(left), left: { key: "w:left", value: twipsMeasureValue(left) },
header: twipsMeasureValue(header), header: { key: "w:header", value: twipsMeasureValue(header) },
footer: twipsMeasureValue(footer), footer: { key: "w:footer", value: twipsMeasureValue(footer) },
gutter: twipsMeasureValue(gutter), gutter: { key: "w:gutter", value: twipsMeasureValue(gutter) },
}), }),
); );
} }

View File

@ -1,5 +1,5 @@
import { XmlAttributeComponent, XmlComponent } from "@file/xml-components"; import { NextAttributeComponent, XmlComponent } from "@file/xml-components";
import { twipsMeasureValue } from "@util/values"; import { PositiveUniversalMeasure, twipsMeasureValue } from "@util/values";
// <xsd:simpleType name="ST_PageOrientation"> // <xsd:simpleType name="ST_PageOrientation">
// <xsd:restriction base="xsd:string"> // <xsd:restriction base="xsd:string">
@ -18,22 +18,14 @@ export enum PageOrientation {
// <xsd:attribute name="orient" type="ST_PageOrientation" use="optional"/> // <xsd:attribute name="orient" type="ST_PageOrientation" use="optional"/>
// <xsd:attribute name="code" type="ST_DecimalNumber" use="optional"/> // <xsd:attribute name="code" type="ST_DecimalNumber" use="optional"/>
// </xsd:complexType> // </xsd:complexType>
export interface IPageSizeAttributes { export type IPageSizeAttributes = {
readonly width?: number | string; readonly width?: number | PositiveUniversalMeasure;
readonly height?: number | string; readonly height?: number | PositiveUniversalMeasure;
readonly orientation?: PageOrientation; readonly orientation?: PageOrientation;
} };
export class PageSizeAttributes extends XmlAttributeComponent<IPageSizeAttributes> {
protected readonly xmlKeys = {
width: "w:w",
height: "w:h",
orientation: "w:orient",
};
}
export class PageSize extends XmlComponent { export class PageSize extends XmlComponent {
public constructor(width: number | string, height: number | string, orientation: PageOrientation) { public constructor(width: number | PositiveUniversalMeasure, height: number | PositiveUniversalMeasure, orientation: PageOrientation) {
super("w:pgSz"); super("w:pgSz");
const flip = orientation === PageOrientation.LANDSCAPE; const flip = orientation === PageOrientation.LANDSCAPE;
@ -42,10 +34,10 @@ export class PageSize extends XmlComponent {
const heightTwips = twipsMeasureValue(height); const heightTwips = twipsMeasureValue(height);
this.root.push( this.root.push(
new PageSizeAttributes({ new NextAttributeComponent<IPageSizeAttributes>({
width: flip ? heightTwips : widthTwips, width: { key: "w:w", value: flip ? heightTwips : widthTwips },
height: flip ? widthTwips : heightTwips, height: { key: "w:h", value: flip ? widthTwips : heightTwips },
orientation: orientation, orientation: { key: "w:orient", value: orientation },
}), }),
); );
} }

View File

@ -17,6 +17,7 @@ import { IPageNumberTypeAttributes, PageNumberType } from "./properties/page-num
import { IPageSizeAttributes, PageOrientation, PageSize } from "./properties/page-size"; import { IPageSizeAttributes, PageOrientation, PageSize } from "./properties/page-size";
import { PageTextDirection, PageTextDirectionType } from "./properties/page-text-direction"; import { PageTextDirection, PageTextDirectionType } from "./properties/page-text-direction";
import { SectionType, Type } from "./properties/section-type"; import { SectionType, Type } from "./properties/section-type";
import { PositiveUniversalMeasure, UniversalMeasure } from "@util/values";
export interface IHeaderFooterGroup<T> { export interface IHeaderFooterGroup<T> {
readonly default?: T; readonly default?: T;
@ -76,10 +77,10 @@ export interface ISectionPropertiesOptions {
// </xsd:group> // </xsd:group>
export const sectionMarginDefaults = { export const sectionMarginDefaults = {
TOP: "1in", TOP: "1in" as UniversalMeasure,
RIGHT: "1in", RIGHT: "1in" as PositiveUniversalMeasure,
BOTTOM: "1in", BOTTOM: "1in" as UniversalMeasure,
LEFT: "1in", LEFT: "1in" as PositiveUniversalMeasure,
HEADER: 708, HEADER: 708,
FOOTER: 708, FOOTER: 708,
GUTTER: 0, GUTTER: 0,

View File

@ -1,39 +1,14 @@
// http://officeopenxml.com/WPindentation.php // http://officeopenxml.com/WPindentation.php
import { XmlAttributeComponent, XmlComponent } from "@file/xml-components"; import { NextAttributeComponent, XmlComponent } from "@file/xml-components";
import { signedTwipsMeasureValue, twipsMeasureValue } from "@util/values"; import { PositiveUniversalMeasure, signedTwipsMeasureValue, twipsMeasureValue, UniversalMeasure } from "@util/values";
export interface IIndentAttributesProperties { export interface IIndentAttributesProperties {
readonly start?: number | string; readonly start?: number | UniversalMeasure;
readonly end?: number | string; readonly end?: number | UniversalMeasure;
readonly left?: number | string; readonly left?: number | UniversalMeasure;
readonly right?: number | string; readonly right?: number | UniversalMeasure;
readonly hanging?: number | string; readonly hanging?: number | PositiveUniversalMeasure;
readonly firstLine?: number | string; readonly firstLine?: number | PositiveUniversalMeasure;
}
// <xsd:complexType name="CT_Ind">
// <xsd:attribute name="start" type="ST_SignedTwipsMeasure" use="optional"/>
// <xsd:attribute name="startChars" type="ST_DecimalNumber" use="optional"/>
// <xsd:attribute name="end" type="ST_SignedTwipsMeasure" use="optional"/>
// <xsd:attribute name="endChars" type="ST_DecimalNumber" use="optional"/>
// <xsd:attribute name="left" type="ST_SignedTwipsMeasure" use="optional"/>
// <xsd:attribute name="leftChars" type="ST_DecimalNumber" use="optional"/>
// <xsd:attribute name="right" type="ST_SignedTwipsMeasure" use="optional"/>
// <xsd:attribute name="rightChars" type="ST_DecimalNumber" use="optional"/>
// <xsd:attribute name="hanging" type="s:ST_TwipsMeasure" use="optional"/>
// <xsd:attribute name="hangingChars" type="ST_DecimalNumber" use="optional"/>
// <xsd:attribute name="firstLine" type="s:ST_TwipsMeasure" use="optional"/>
// <xsd:attribute name="firstLineChars" type="ST_DecimalNumber" use="optional"/>
// </xsd:complexType>
class IndentAttributes extends XmlAttributeComponent<IIndentAttributesProperties> {
protected readonly xmlKeys = {
start: "w:start",
end: "w:end",
left: "w:left",
right: "w:right",
hanging: "w:hanging",
firstLine: "w:firstLine",
};
} }
// <xsd:complexType name="CT_PPrBase"> // <xsd:complexType name="CT_PPrBase">
@ -43,14 +18,53 @@ class IndentAttributes extends XmlAttributeComponent<IIndentAttributesProperties
export class Indent extends XmlComponent { export class Indent extends XmlComponent {
public constructor({ start, end, left, right, hanging, firstLine }: IIndentAttributesProperties) { public constructor({ start, end, left, right, hanging, firstLine }: IIndentAttributesProperties) {
super("w:ind"); super("w:ind");
// <xsd:complexType name="CT_Ind">
// <xsd:attribute name="start" type="ST_SignedTwipsMeasure" use="optional"/>
// <xsd:attribute name="startChars" type="ST_DecimalNumber" use="optional"/>
// <xsd:attribute name="end" type="ST_SignedTwipsMeasure" use="optional"/>
// <xsd:attribute name="endChars" type="ST_DecimalNumber" use="optional"/>
// <xsd:attribute name="left" type="ST_SignedTwipsMeasure" use="optional"/>
// <xsd:attribute name="leftChars" type="ST_DecimalNumber" use="optional"/>
// <xsd:attribute name="right" type="ST_SignedTwipsMeasure" use="optional"/>
// <xsd:attribute name="rightChars" type="ST_DecimalNumber" use="optional"/>
// <xsd:attribute name="hanging" type="s:ST_TwipsMeasure" use="optional"/>
// <xsd:attribute name="hangingChars" type="ST_DecimalNumber" use="optional"/>
// <xsd:attribute name="firstLine" type="s:ST_TwipsMeasure" use="optional"/>
// <xsd:attribute name="firstLineChars" type="ST_DecimalNumber" use="optional"/>
// </xsd:complexType>
this.root.push( this.root.push(
new IndentAttributes({ new NextAttributeComponent<{
start: start === undefined ? undefined : signedTwipsMeasureValue(start), readonly start?: number | UniversalMeasure;
end: end === undefined ? undefined : signedTwipsMeasureValue(end), readonly end?: number | UniversalMeasure;
left: left === undefined ? undefined : signedTwipsMeasureValue(left), readonly left?: number | UniversalMeasure;
right: right === undefined ? undefined : signedTwipsMeasureValue(right), readonly right?: number | UniversalMeasure;
hanging: hanging === undefined ? undefined : twipsMeasureValue(hanging), readonly hanging?: number | PositiveUniversalMeasure;
firstLine: firstLine === undefined ? undefined : twipsMeasureValue(firstLine), readonly firstLine?: number | PositiveUniversalMeasure;
}>({
start: {
key: "w:start",
value: start === undefined ? undefined : signedTwipsMeasureValue(start),
},
end: {
key: "w:end",
value: end === undefined ? undefined : signedTwipsMeasureValue(end),
},
left: {
key: "w:left",
value: left === undefined ? undefined : signedTwipsMeasureValue(left),
},
right: {
key: "w:right",
value: right === undefined ? undefined : signedTwipsMeasureValue(right),
},
hanging: {
key: "w:hanging",
value: hanging === undefined ? undefined : twipsMeasureValue(hanging),
},
firstLine: {
key: "w:firstLine",
value: firstLine === undefined ? undefined : twipsMeasureValue(firstLine),
},
}), }),
); );
} }

View File

@ -1,8 +1,8 @@
import { Attributes, XmlComponent } from "@file/xml-components"; import { Attributes, XmlComponent } from "@file/xml-components";
import { hexColorValue, signedTwipsMeasureValue } from "@util/values"; import { hexColorValue, signedTwipsMeasureValue, UniversalMeasure } from "@util/values";
export class CharacterSpacing extends XmlComponent { export class CharacterSpacing extends XmlComponent {
public constructor(value: number | string) { public constructor(value: number | UniversalMeasure) {
super("w:spacing"); super("w:spacing");
this.root.push( this.root.push(
new Attributes({ new Attributes({

View File

@ -9,6 +9,7 @@ import {
StringValueElement, StringValueElement,
XmlComponent, XmlComponent,
} from "@file/xml-components"; } from "@file/xml-components";
import { PositiveUniversalMeasure, UniversalMeasure } from "@util/values";
import { EmphasisMark, EmphasisMarkType } from "./emphasis-mark"; import { EmphasisMark, EmphasisMarkType } from "./emphasis-mark";
import { CharacterSpacing, Color, Highlight, HighlightComplexScript } from "./formatting"; import { CharacterSpacing, Color, Highlight, HighlightComplexScript } from "./formatting";
@ -22,6 +23,16 @@ interface IFontOptions {
readonly hint?: string; readonly hint?: string;
} }
export enum TextEffect {
BLINK_BACKGROUND = "blinkBackground",
LIGHTS = "lights",
ANTS_BLACK = "antsBlack",
ANTS_RED = "antsRed",
SHIMMER = "shimmer",
SPARKLE = "sparkle",
NONE = "none",
}
export interface IRunStylePropertiesOptions { export interface IRunStylePropertiesOptions {
readonly bold?: boolean; readonly bold?: boolean;
readonly boldComplexScript?: boolean; readonly boldComplexScript?: boolean;
@ -31,12 +42,15 @@ export interface IRunStylePropertiesOptions {
readonly color?: string; readonly color?: string;
readonly type?: UnderlineType; readonly type?: UnderlineType;
}; };
readonly effect?: TextEffect;
readonly emphasisMark?: { readonly emphasisMark?: {
readonly type?: EmphasisMarkType; readonly type?: EmphasisMarkType;
}; };
readonly color?: string; readonly color?: string;
readonly size?: number | string; readonly kern?: number | PositiveUniversalMeasure;
readonly sizeComplexScript?: boolean | number | string; readonly position?: UniversalMeasure;
readonly size?: number | PositiveUniversalMeasure;
readonly sizeComplexScript?: boolean | number | PositiveUniversalMeasure;
readonly rightToLeft?: boolean; readonly rightToLeft?: boolean;
readonly smallCaps?: boolean; readonly smallCaps?: boolean;
readonly allCaps?: boolean; readonly allCaps?: boolean;
@ -54,9 +68,11 @@ export interface IRunStylePropertiesOptions {
readonly revision?: IRunPropertiesChangeOptions; readonly revision?: IRunPropertiesChangeOptions;
readonly language?: ILanguageOptions; readonly language?: ILanguageOptions;
readonly border?: IBorderOptions; readonly border?: IBorderOptions;
readonly snapToGrid?: boolean;
readonly vanish?: boolean; readonly vanish?: boolean;
readonly specVanish?: boolean; readonly specVanish?: boolean;
readonly scale?: number; readonly scale?: number;
readonly math?: boolean;
} }
export interface IRunPropertiesOptions extends IRunStylePropertiesOptions { export interface IRunPropertiesOptions extends IRunStylePropertiesOptions {
@ -135,6 +151,10 @@ export class RunProperties extends IgnoreIfEmptyXmlComponent {
this.push(new Underline(options.underline.type, options.underline.color)); this.push(new Underline(options.underline.type, options.underline.color));
} }
if (options.effect) {
this.push(new StringValueElement("w:effect", options.effect));
}
if (options.emphasisMark) { if (options.emphasisMark) {
this.push(new EmphasisMark(options.emphasisMark.type)); this.push(new EmphasisMark(options.emphasisMark.type));
} }
@ -143,6 +163,14 @@ export class RunProperties extends IgnoreIfEmptyXmlComponent {
this.push(new Color(options.color)); this.push(new Color(options.color));
} }
if (options.kern) {
this.push(new HpsMeasureElement("w:kern", options.kern));
}
if (options.position) {
this.push(new StringValueElement("w:position", options.position));
}
if (options.size !== undefined) { if (options.size !== undefined) {
this.push(new HpsMeasureElement("w:sz", options.size)); this.push(new HpsMeasureElement("w:sz", options.size));
} }
@ -228,6 +256,10 @@ export class RunProperties extends IgnoreIfEmptyXmlComponent {
this.push(new BorderElement("w:bdr", options.border)); this.push(new BorderElement("w:bdr", options.border));
} }
if (options.snapToGrid) {
this.push(new OnOffElement("w:snapToGrid", options.snapToGrid));
}
if (options.vanish) { if (options.vanish) {
// https://c-rex.net/projects/samples/ooxml/e1/Part4/OOXML_P4_DOCX_vanish_topic_ID0E6W3O.html // https://c-rex.net/projects/samples/ooxml/e1/Part4/OOXML_P4_DOCX_vanish_topic_ID0E6W3O.html
// http://www.datypic.com/sc/ooxml/e-w_vanish-1.html // http://www.datypic.com/sc/ooxml/e-w_vanish-1.html
@ -246,6 +278,10 @@ export class RunProperties extends IgnoreIfEmptyXmlComponent {
if (options.language) { if (options.language) {
this.push(createLanguageComponent(options.language)); this.push(createLanguageComponent(options.language));
} }
if (options.math) {
this.push(new OnOffElement("w:oMath", options.math));
}
} }
public push(item: XmlComponent): void { public push(item: XmlComponent): void {

View File

@ -7,6 +7,7 @@ import { ShadingType } from "@file/shading";
import { EmphasisMarkType } from "./emphasis-mark"; import { EmphasisMarkType } from "./emphasis-mark";
import { PageNumber, Run } from "./run"; import { PageNumber, Run } from "./run";
import { UnderlineType } from "./underline"; import { UnderlineType } from "./underline";
import { TextEffect } from "./properties";
describe("Run", () => { describe("Run", () => {
describe("#bold()", () => { describe("#bold()", () => {
@ -610,5 +611,117 @@ describe("Run", () => {
}); });
}); });
}); });
describe("#position", () => {
it("should correctly set the position", () => {
const run = new Run({
position: "2mm",
});
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [
{
"w:rPr": [
{
"w:position": {
_attr: {
"w:val": "2mm",
},
},
},
],
},
],
});
});
});
describe("#effect", () => {
it("should correctly set the effect", () => {
const run = new Run({
effect: TextEffect.ANTS_BLACK,
});
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [
{
"w:rPr": [
{
"w:effect": {
_attr: {
"w:val": "antsBlack",
},
},
},
],
},
],
});
});
});
describe("#math", () => {
it("should correctly set the math", () => {
const run = new Run({
math: true,
});
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [
{
"w:rPr": [
{
"w:oMath": {},
},
],
},
],
});
});
});
describe("#kern", () => {
it("should correctly set the kern", () => {
const run = new Run({
kern: "2mm",
});
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [
{
"w:rPr": [
{
"w:kern": {
_attr: {
"w:val": "2mm",
},
},
},
],
},
],
});
});
});
describe("#snapToGrid", () => {
it("should correctly set the snapToGrid", () => {
const run = new Run({
snapToGrid: true,
});
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [
{
"w:rPr": [
{
"w:snapToGrid": {},
},
],
},
],
});
});
});
}); });
}); });

View File

@ -9,11 +9,11 @@
// </xsd:sequence> // </xsd:sequence>
// </xsd:complexType> // </xsd:complexType>
import { XmlAttributeComponent, XmlComponent } from "@file/xml-components"; import { NextAttributeComponent, XmlComponent } from "@file/xml-components";
import { twipsMeasureValue } from "@util/values"; import { PositiveUniversalMeasure, twipsMeasureValue } from "@util/values";
export class TableGrid extends XmlComponent { export class TableGrid extends XmlComponent {
public constructor(widths: readonly number[] | readonly string[]) { public constructor(widths: readonly number[] | readonly PositiveUniversalMeasure[]) {
super("w:tblGrid"); super("w:tblGrid");
for (const width of widths) { for (const width of widths) {
this.root.push(new GridCol(width)); this.root.push(new GridCol(width));
@ -21,15 +21,15 @@ export class TableGrid extends XmlComponent {
} }
} }
class GridColAttributes extends XmlAttributeComponent<{ readonly w: number | string }> {
protected readonly xmlKeys = { w: "w:w" };
}
export class GridCol extends XmlComponent { export class GridCol extends XmlComponent {
public constructor(width?: number | string) { public constructor(width?: number | PositiveUniversalMeasure) {
super("w:gridCol"); super("w:gridCol");
if (width !== undefined) { if (width !== undefined) {
this.root.push(new GridColAttributes({ w: twipsMeasureValue(width) })); this.root.push(
new NextAttributeComponent<{ readonly width: number | PositiveUniversalMeasure }>({
width: { key: "w:w", value: twipsMeasureValue(width) },
}),
);
} }
} }
} }

View File

@ -1,6 +1,6 @@
// http://officeopenxml.com/WPtableWidth.php // http://officeopenxml.com/WPtableWidth.php
import { XmlAttributeComponent, XmlComponent } from "@file/xml-components"; import { NextAttributeComponent, XmlComponent } from "@file/xml-components";
import { measurementOrPercentValue } from "@util/values"; import { measurementOrPercentValue, Percentage, UniversalMeasure } from "@util/values";
// <xsd:simpleType name="ST_TblWidth"> // <xsd:simpleType name="ST_TblWidth">
// <xsd:restriction base="xsd:string"> // <xsd:restriction base="xsd:string">
@ -25,14 +25,10 @@ export enum WidthType {
// <xsd:attribute name="w" type="ST_MeasurementOrPercent"/> // <xsd:attribute name="w" type="ST_MeasurementOrPercent"/>
// <xsd:attribute name="type" type="ST_TblWidth"/> // <xsd:attribute name="type" type="ST_TblWidth"/>
// </xsd:complexType> // </xsd:complexType>
export interface ITableWidthProperties { export type ITableWidthProperties = {
readonly size: string | number; readonly size: number | Percentage | UniversalMeasure;
readonly type?: WidthType; readonly type?: WidthType;
} };
class TableWidthAttributes extends XmlAttributeComponent<ITableWidthProperties> {
protected readonly xmlKeys = { type: "w:type", size: "w:w" };
}
export class TableWidthElement extends XmlComponent { export class TableWidthElement extends XmlComponent {
public constructor(name: string, { type = WidthType.AUTO, size }: ITableWidthProperties) { public constructor(name: string, { type = WidthType.AUTO, size }: ITableWidthProperties) {
@ -42,6 +38,12 @@ export class TableWidthElement extends XmlComponent {
if (type === WidthType.PERCENTAGE && typeof size === "number") { if (type === WidthType.PERCENTAGE && typeof size === "number") {
tableWidthValue = `${size}%`; tableWidthValue = `${size}%`;
} }
this.root.push(new TableWidthAttributes({ type: type, size: measurementOrPercentValue(tableWidthValue) }));
this.root.push(
new NextAttributeComponent<ITableWidthProperties>({
type: { key: "w:type", value: type },
size: { key: "w:w", value: measurementOrPercentValue(tableWidthValue) },
}),
);
} }
} }

View File

@ -1,6 +1,6 @@
import { AttributeData, AttributePayload, Attributes, NextAttributeComponent, XmlComponent } from "@file/xml-components"; import { AttributeData, AttributePayload, Attributes, NextAttributeComponent, XmlComponent } from "@file/xml-components";
import { hpsMeasureValue } from "@util/values"; import { hpsMeasureValue, PositiveUniversalMeasure } from "@util/values";
// This represents element type CT_OnOff, which indicate a boolean value. // This represents element type CT_OnOff, which indicate a boolean value.
// //
@ -26,8 +26,13 @@ export class OnOffElement extends XmlComponent {
// <xsd:complexType name="CT_HpsMeasure"> // <xsd:complexType name="CT_HpsMeasure">
// <xsd:attribute name="val" type="ST_HpsMeasure" use="required"/> // <xsd:attribute name="val" type="ST_HpsMeasure" use="required"/>
// </xsd:complexType> // </xsd:complexType>
// <xsd:simpleType name="ST_HpsMeasure">
// <xsd:union memberTypes="s:ST_UnsignedDecimalNumber s:ST_PositiveUniversalMeasure" />
// </xsd:simpleType>
export class HpsMeasureElement extends XmlComponent { export class HpsMeasureElement extends XmlComponent {
public constructor(name: string, val: number | string) { public constructor(name: string, val: number | PositiveUniversalMeasure) {
super(name); super(name);
this.root.push(new Attributes({ val: hpsMeasureValue(val) })); this.root.push(new Attributes({ val: hpsMeasureValue(val) }));
} }

View File

@ -116,7 +116,6 @@ describe("values", () => {
}); });
it("should throw on invalid values", () => { it("should throw on invalid values", () => {
expect(() => signedTwipsMeasureValue(NaN)).to.throw(); expect(() => signedTwipsMeasureValue(NaN)).to.throw();
expect(() => signedTwipsMeasureValue("foo")).to.throw();
}); });
}); });

View File

@ -4,6 +4,23 @@
// Most of the rest of the types not defined here are either aliases of existing types or enumerations. // Most of the rest of the types not defined here are either aliases of existing types or enumerations.
// Enumerations should probably just be implemented as enums, with instructions to end-users, without a runtime check. // Enumerations should probably just be implemented as enums, with instructions to end-users, without a runtime check.
// -?[0-9]+(\.[0-9]+)?(mm|cm|in|pt|pc|pi)
export type UniversalMeasure = `${"-" | ""}${number}${"mm" | "cm" | "in" | "pt" | "pc" | "pi"}`;
// <xsd:simpleType name="ST_PositiveUniversalMeasure">
// <xsd:restriction base="ST_UniversalMeasure">
// <xsd:pattern value="[0-9]+(\.[0-9]+)?(mm|cm|in|pt|pc|pi)" />
// </xsd:restriction>
// </xsd:simpleType>
export type PositiveUniversalMeasure = `${number}${"mm" | "cm" | "in" | "pt" | "pc" | "pi"}`;
// <xsd:simpleType name="ST_Percentage">
// <xsd:restriction base="xsd:string">
// <xsd:pattern value="-?[0-9]+(\.[0-9]+)?%"/>
// </xsd:restriction>
// </xsd:simpleType>
export type Percentage = `${"-" | ""}${number}%`;
// <xsd:simpleType name="ST_DecimalNumber"> // <xsd:simpleType name="ST_DecimalNumber">
// <xsd:restriction base="xsd:integer"/> // <xsd:restriction base="xsd:integer"/>
// </xsd:simpleType> // </xsd:simpleType>
@ -70,7 +87,7 @@ export const uCharHexNumber = (val: string): string => hexBinary(val, 1);
// <xsd:pattern value="-?[0-9]+(\.[0-9]+)?(mm|cm|in|pt|pc|pi)"/> // <xsd:pattern value="-?[0-9]+(\.[0-9]+)?(mm|cm|in|pt|pc|pi)"/>
// </xsd:restriction> // </xsd:restriction>
// </xsd:simpleType> // </xsd:simpleType>
export const universalMeasureValue = (val: string): string => { export const universalMeasureValue = (val: UniversalMeasure): string => {
const unit = val.slice(-2); const unit = val.slice(-2);
if (!universalMeasureUnits.includes(unit)) { if (!universalMeasureUnits.includes(unit)) {
throw new Error(`Invalid unit '${unit}' specified. Valid units are ${universalMeasureUnits.join(", ")}`); throw new Error(`Invalid unit '${unit}' specified. Valid units are ${universalMeasureUnits.join(", ")}`);
@ -88,7 +105,7 @@ const universalMeasureUnits = ["mm", "cm", "in", "pt", "pc", "pi"];
// <xsd:pattern value="[0-9]+(\.[0-9]+)?(mm|cm|in|pt|pc|pi)"/> // <xsd:pattern value="[0-9]+(\.[0-9]+)?(mm|cm|in|pt|pc|pi)"/>
// </xsd:restriction> // </xsd:restriction>
// </xsd:simpleType> // </xsd:simpleType>
export const positiveUniversalMeasureValue = (val: string): string => { export const positiveUniversalMeasureValue = (val: PositiveUniversalMeasure): string => {
const value = universalMeasureValue(val); const value = universalMeasureValue(val);
if (parseFloat(value) < 0) { if (parseFloat(value) < 0) {
throw new Error(`Invalid value '${value}' specified. Expected a positive number.`); throw new Error(`Invalid value '${value}' specified. Expected a positive number.`);
@ -123,33 +140,33 @@ export const hexColorValue = (val: string): string => {
// <xsd:simpleType name="ST_SignedTwipsMeasure"> // <xsd:simpleType name="ST_SignedTwipsMeasure">
// <xsd:union memberTypes="xsd:integer s:ST_UniversalMeasure"/> // <xsd:union memberTypes="xsd:integer s:ST_UniversalMeasure"/>
// </xsd:simpleType> // </xsd:simpleType>
export const signedTwipsMeasureValue = (val: string | number): string | number => export const signedTwipsMeasureValue = (val: UniversalMeasure | number): UniversalMeasure | number =>
typeof val === "string" ? universalMeasureValue(val) : decimalNumber(val); typeof val === "string" ? val : decimalNumber(val);
// <xsd:simpleType name="ST_HpsMeasure"> // <xsd:simpleType name="ST_HpsMeasure">
// <xsd:union memberTypes="s:ST_UnsignedDecimalNumber s:ST_PositiveUniversalMeasure"/> // <xsd:union memberTypes="s:ST_UnsignedDecimalNumber s:ST_PositiveUniversalMeasure"/>
// </xsd:simpleType> // </xsd:simpleType>
export const hpsMeasureValue = (val: string | number): string | number => export const hpsMeasureValue = (val: PositiveUniversalMeasure | number): string | number =>
typeof val === "string" ? positiveUniversalMeasureValue(val) : unsignedDecimalNumber(val); typeof val === "string" ? positiveUniversalMeasureValue(val) : unsignedDecimalNumber(val);
// <xsd:simpleType name="ST_SignedHpsMeasure"> // <xsd:simpleType name="ST_SignedHpsMeasure">
// <xsd:union memberTypes="xsd:integer s:ST_UniversalMeasure"/> // <xsd:union memberTypes="xsd:integer s:ST_UniversalMeasure"/>
// </xsd:simpleType> // </xsd:simpleType>
export const signedHpsMeasureValue = (val: string | number): string | number => export const signedHpsMeasureValue = (val: UniversalMeasure | number): string | number =>
typeof val === "string" ? universalMeasureValue(val) : decimalNumber(val); typeof val === "string" ? universalMeasureValue(val) : decimalNumber(val);
// <xsd:simpleType name="ST_TwipsMeasure"> // <xsd:simpleType name="ST_TwipsMeasure">
// <xsd:union memberTypes="ST_UnsignedDecimalNumber ST_PositiveUniversalMeasure"/> // <xsd:union memberTypes="ST_UnsignedDecimalNumber ST_PositiveUniversalMeasure"/>
// </xsd:simpleType> // </xsd:simpleType>
export const twipsMeasureValue = (val: string | number): string | number => export const twipsMeasureValue = (val: PositiveUniversalMeasure | number): PositiveUniversalMeasure | number =>
typeof val === "string" ? positiveUniversalMeasureValue(val) : unsignedDecimalNumber(val); typeof val === "string" ? val : unsignedDecimalNumber(val);
// <xsd:simpleType name="ST_Percentage"> // <xsd:simpleType name="ST_Percentage">
// <xsd:restriction base="xsd:string"> // <xsd:restriction base="xsd:string">
// <xsd:pattern value="-?[0-9]+(\.[0-9]+)?%"/> // <xsd:pattern value="-?[0-9]+(\.[0-9]+)?%"/>
// </xsd:restriction> // </xsd:restriction>
// </xsd:simpleType> // </xsd:simpleType>
export const percentageValue = (val: string): string => { export const percentageValue = (val: Percentage): Percentage => {
if (val.slice(-1) !== "%") { if (val.slice(-1) !== "%") {
throw new Error(`Invalid value '${val}'. Expected percentage value (eg '55%')`); throw new Error(`Invalid value '${val}'. Expected percentage value (eg '55%')`);
} }
@ -172,14 +189,14 @@ export const percentageValue = (val: string): string => {
// <xsd:restriction base="xsd:integer"/> // <xsd:restriction base="xsd:integer"/>
// </xsd:simpleType> // </xsd:simpleType>
export const measurementOrPercentValue = (val: number | string): number | string => { export const measurementOrPercentValue = (val: number | Percentage | UniversalMeasure): number | UniversalMeasure | Percentage => {
if (typeof val === "number") { if (typeof val === "number") {
return decimalNumber(val); return decimalNumber(val);
} }
if (val.slice(-1) === "%") { if (val.slice(-1) === "%") {
return percentageValue(val); return percentageValue(val as Percentage);
} }
return universalMeasureValue(val); return val as UniversalMeasure;
}; };
// <xsd:simpleType name="ST_EighthPointMeasure"> // <xsd:simpleType name="ST_EighthPointMeasure">