import { IShadingAttributesProperties, Shading } from "file/shading"; import { HpsMeasureElement, IgnoreIfEmptyXmlComponent, OnOffElement, StringValueElement, XmlComponent } from "file/xml-components"; import { EmphasisMark, EmphasisMarkType } from "./emphasis-mark"; import { CharacterSpacing, Color, Highlight, HighlightComplexScript } from "./formatting"; import { IFontAttributesProperties, RunFonts } from "./run-fonts"; import { SubScript, SuperScript } from "./script"; import { Underline, UnderlineType } from "./underline"; interface IFontOptions { readonly name: string; readonly hint?: string; } export interface IRunStylePropertiesOptions { readonly bold?: boolean; readonly boldComplexScript?: boolean; readonly italics?: boolean; readonly italicsComplexScript?: boolean; readonly underline?: { readonly color?: string; readonly type?: UnderlineType; }; readonly emphasisMark?: { readonly type?: EmphasisMarkType; }; readonly color?: string; readonly size?: number | string; readonly sizeComplexScript?: boolean | number | string; readonly rightToLeft?: boolean; readonly smallCaps?: boolean; readonly allCaps?: boolean; readonly strike?: boolean; readonly doubleStrike?: boolean; readonly subScript?: boolean; readonly superScript?: boolean; readonly font?: string | IFontOptions | IFontAttributesProperties; readonly highlight?: string; readonly highlightComplexScript?: boolean | string; readonly characterSpacing?: number; readonly shading?: IShadingAttributesProperties; readonly emboss?: boolean; readonly imprint?: boolean; } export interface IRunPropertiesOptions extends IRunStylePropertiesOptions { readonly style?: string; } // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // export class RunProperties extends IgnoreIfEmptyXmlComponent { constructor(options?: IRunPropertiesOptions) { super("w:rPr"); if (!options) { return; } if (options.bold !== undefined) { this.push(new OnOffElement("w:b", options.bold)); } if ((options.boldComplexScript === undefined && options.bold !== undefined) || options.boldComplexScript) { this.push(new OnOffElement("w:bCs", options.boldComplexScript ?? options.bold)); } if (options.italics !== undefined) { this.push(new OnOffElement("w:i", options.italics)); } if ((options.italicsComplexScript === undefined && options.italics !== undefined) || options.italicsComplexScript) { this.push(new OnOffElement("w:iCs", options.italicsComplexScript ?? options.italics)); } if (options.underline) { this.push(new Underline(options.underline.type, options.underline.color)); } if (options.emphasisMark) { this.push(new EmphasisMark(options.emphasisMark.type)); } if (options.color) { this.push(new Color(options.color)); } if (options.size !== undefined) { this.push(new HpsMeasureElement("w:sz", options.size)); } const szCs = options.sizeComplexScript === undefined || options.sizeComplexScript === true ? options.size : options.sizeComplexScript; if (szCs) { this.push(new HpsMeasureElement("w:szCs", szCs)); } if (options.rightToLeft !== undefined) { this.push(new OnOffElement("w:rtl", options.rightToLeft)); } // These two are mutually exclusive if (options.smallCaps !== undefined) { this.push(new OnOffElement("w:smallCaps", options.smallCaps)); } else if (options.allCaps !== undefined) { this.push(new OnOffElement("w:caps", options.allCaps)); } if (options.strike !== undefined) { this.push(new OnOffElement("w:strike", options.strike)); } if (options.doubleStrike !== undefined) { this.push(new OnOffElement("w:dstrike", options.doubleStrike)); } if (options.subScript) { this.push(new SubScript()); } if (options.superScript) { this.push(new SuperScript()); } if (options.style) { this.push(new StringValueElement("w:rStyle", options.style)); } if (options.font) { if (typeof options.font === "string") { this.push(new RunFonts(options.font)); } else if ("name" in options.font) { this.push(new RunFonts(options.font.name, options.font.hint)); } else { this.push(new RunFonts(options.font)); } } if (options.highlight) { this.push(new Highlight(options.highlight)); } const highlightCs = options.highlightComplexScript === undefined || options.highlightComplexScript === true ? options.highlight : options.highlightComplexScript; if (highlightCs) { this.push(new HighlightComplexScript(highlightCs)); } if (options.characterSpacing) { this.push(new CharacterSpacing(options.characterSpacing)); } if (options.emboss !== undefined) { this.push(new OnOffElement("w:emboss", options.emboss)); } if (options.imprint !== undefined) { this.push(new OnOffElement("w:imprint", options.imprint)); } if (options.shading) { this.push(new Shading(options.shading)); } } public push(item: XmlComponent): void { this.root.push(item); } }