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,39 +1,14 @@
// http://officeopenxml.com/WPindentation.php
import { XmlAttributeComponent, XmlComponent } from "@file/xml-components";
import { signedTwipsMeasureValue, twipsMeasureValue } from "@util/values";
import { NextAttributeComponent, XmlComponent } from "@file/xml-components";
import { PositiveUniversalMeasure, signedTwipsMeasureValue, twipsMeasureValue, UniversalMeasure } from "@util/values";
export interface IIndentAttributesProperties {
readonly start?: number | string;
readonly end?: number | string;
readonly left?: number | string;
readonly right?: number | string;
readonly hanging?: number | string;
readonly firstLine?: number | string;
}
// <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",
};
readonly start?: number | UniversalMeasure;
readonly end?: number | UniversalMeasure;
readonly left?: number | UniversalMeasure;
readonly right?: number | UniversalMeasure;
readonly hanging?: number | PositiveUniversalMeasure;
readonly firstLine?: number | PositiveUniversalMeasure;
}
// <xsd:complexType name="CT_PPrBase">
@ -43,14 +18,53 @@ class IndentAttributes extends XmlAttributeComponent<IIndentAttributesProperties
export class Indent extends XmlComponent {
public constructor({ start, end, left, right, hanging, firstLine }: IIndentAttributesProperties) {
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(
new IndentAttributes({
start: start === undefined ? undefined : signedTwipsMeasureValue(start),
end: end === undefined ? undefined : signedTwipsMeasureValue(end),
left: left === undefined ? undefined : signedTwipsMeasureValue(left),
right: right === undefined ? undefined : signedTwipsMeasureValue(right),
hanging: hanging === undefined ? undefined : twipsMeasureValue(hanging),
firstLine: firstLine === undefined ? undefined : twipsMeasureValue(firstLine),
new NextAttributeComponent<{
readonly start?: number | UniversalMeasure;
readonly end?: number | UniversalMeasure;
readonly left?: number | UniversalMeasure;
readonly right?: number | UniversalMeasure;
readonly hanging?: number | PositiveUniversalMeasure;
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 { hexColorValue, signedTwipsMeasureValue } from "@util/values";
import { hexColorValue, signedTwipsMeasureValue, UniversalMeasure } from "@util/values";
export class CharacterSpacing extends XmlComponent {
public constructor(value: number | string) {
public constructor(value: number | UniversalMeasure) {
super("w:spacing");
this.root.push(
new Attributes({

View File

@ -9,6 +9,7 @@ import {
StringValueElement,
XmlComponent,
} from "@file/xml-components";
import { PositiveUniversalMeasure, UniversalMeasure } from "@util/values";
import { EmphasisMark, EmphasisMarkType } from "./emphasis-mark";
import { CharacterSpacing, Color, Highlight, HighlightComplexScript } from "./formatting";
@ -22,6 +23,16 @@ interface IFontOptions {
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 {
readonly bold?: boolean;
readonly boldComplexScript?: boolean;
@ -31,12 +42,15 @@ export interface IRunStylePropertiesOptions {
readonly color?: string;
readonly type?: UnderlineType;
};
readonly effect?: TextEffect;
readonly emphasisMark?: {
readonly type?: EmphasisMarkType;
};
readonly color?: string;
readonly size?: number | string;
readonly sizeComplexScript?: boolean | number | string;
readonly kern?: number | PositiveUniversalMeasure;
readonly position?: UniversalMeasure;
readonly size?: number | PositiveUniversalMeasure;
readonly sizeComplexScript?: boolean | number | PositiveUniversalMeasure;
readonly rightToLeft?: boolean;
readonly smallCaps?: boolean;
readonly allCaps?: boolean;
@ -54,9 +68,11 @@ export interface IRunStylePropertiesOptions {
readonly revision?: IRunPropertiesChangeOptions;
readonly language?: ILanguageOptions;
readonly border?: IBorderOptions;
readonly snapToGrid?: boolean;
readonly vanish?: boolean;
readonly specVanish?: boolean;
readonly scale?: number;
readonly math?: boolean;
}
export interface IRunPropertiesOptions extends IRunStylePropertiesOptions {
@ -135,6 +151,10 @@ export class RunProperties extends IgnoreIfEmptyXmlComponent {
this.push(new Underline(options.underline.type, options.underline.color));
}
if (options.effect) {
this.push(new StringValueElement("w:effect", options.effect));
}
if (options.emphasisMark) {
this.push(new EmphasisMark(options.emphasisMark.type));
}
@ -143,6 +163,14 @@ export class RunProperties extends IgnoreIfEmptyXmlComponent {
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) {
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));
}
if (options.snapToGrid) {
this.push(new OnOffElement("w:snapToGrid", options.snapToGrid));
}
if (options.vanish) {
// 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
@ -246,6 +278,10 @@ export class RunProperties extends IgnoreIfEmptyXmlComponent {
if (options.language) {
this.push(createLanguageComponent(options.language));
}
if (options.math) {
this.push(new OnOffElement("w:oMath", options.math));
}
}
public push(item: XmlComponent): void {

View File

@ -7,6 +7,7 @@ import { ShadingType } from "@file/shading";
import { EmphasisMarkType } from "./emphasis-mark";
import { PageNumber, Run } from "./run";
import { UnderlineType } from "./underline";
import { TextEffect } from "./properties";
describe("Run", () => {
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": {},
},
],
},
],
});
});
});
});
});