diff --git a/demo/demo22.js b/demo/demo22.js new file mode 100644 index 0000000000..ce42f22ccd --- /dev/null +++ b/demo/demo22.js @@ -0,0 +1,14 @@ +const docx = require('../build'); + +var doc = new docx.Document(); + +var textRun = new docx.TextRun("שלום עולם").rtl(); +var paragraph = new docx.Paragraph().bidi(); +paragraph.addRun(textRun); + +doc.addParagraph(paragraph); + +var exporter = new docx.LocalPacker(doc); +exporter.pack('My Document'); + +console.log('Document created successfully at project root!'); diff --git a/src/file/paragraph/formatting/alignment.ts b/src/file/paragraph/formatting/alignment.ts index fbf0fdcdae..b1fa0ca2af 100644 --- a/src/file/paragraph/formatting/alignment.ts +++ b/src/file/paragraph/formatting/alignment.ts @@ -1,7 +1,7 @@ // http://officeopenxml.com/WPalignment.php import { XmlAttributeComponent, XmlComponent } from "file/xml-components"; -export type AlignmentOptions = "left" | "center" | "right" | "both"; +export type AlignmentOptions = "start" | "end" | "center" | "both" | "distribute" | "left" | "right"; export class AlignmentAttributes extends XmlAttributeComponent<{ val: AlignmentOptions }> { protected xmlKeys = { val: "w:val" }; diff --git a/src/file/paragraph/formatting/bidi.ts b/src/file/paragraph/formatting/bidi.ts new file mode 100644 index 0000000000..4a8bbdade9 --- /dev/null +++ b/src/file/paragraph/formatting/bidi.ts @@ -0,0 +1,7 @@ +import { XmlComponent } from "file/xml-components"; + +export class Bidi extends XmlComponent { + constructor() { + super("w:bidi"); + } +} diff --git a/src/file/paragraph/paragraph.spec.ts b/src/file/paragraph/paragraph.spec.ts index dd64f1fed2..a2176f3ecf 100644 --- a/src/file/paragraph/paragraph.spec.ts +++ b/src/file/paragraph/paragraph.spec.ts @@ -338,4 +338,14 @@ describe("Paragraph", () => { }); }); }); + + describe("#bidi", () => { + it("set paragraph right to left layout", () => { + paragraph.bidi(); + const tree = new Formatter().format(paragraph); + expect(tree).to.deep.equal({ + "w:p": [{ "w:pPr": [{ "w:bidi": [] }] }], + }); + }); + }); }); diff --git a/src/file/paragraph/paragraph.ts b/src/file/paragraph/paragraph.ts index f46905207e..3695b70bd5 100644 --- a/src/file/paragraph/paragraph.ts +++ b/src/file/paragraph/paragraph.ts @@ -5,6 +5,7 @@ import { Num } from "file/numbering/num"; import { XmlComponent } from "file/xml-components"; import { Alignment } from "./formatting/alignment"; +import { Bidi } from "./formatting/bidi"; import { ThematicBreak } from "./formatting/border"; import { Indent } from "./formatting/indent"; import { KeepLines, KeepNext } from "./formatting/keep"; @@ -109,6 +110,21 @@ export class Paragraph extends XmlComponent { return this; } + public start(): Paragraph { + this.properties.push(new Alignment("start")); + return this; + } + + public end(): Paragraph { + this.properties.push(new Alignment("end")); + return this; + } + + public distribute(): Paragraph { + this.properties.push(new Alignment("distribute")); + return this; + } + public justified(): Paragraph { this.properties.push(new Alignment("both")); return this; @@ -200,4 +216,9 @@ export class Paragraph extends XmlComponent { this.root.splice(1, 0, run); return this; } + + public bidi(): Paragraph { + this.properties.push(new Bidi()); + return this; + } } diff --git a/src/file/paragraph/run/formatting.ts b/src/file/paragraph/run/formatting.ts index 16fb744e5d..bd9da1c358 100644 --- a/src/file/paragraph/run/formatting.ts +++ b/src/file/paragraph/run/formatting.ts @@ -123,3 +123,25 @@ export class Size extends XmlComponent { ); } } + +export class SizeCs extends XmlComponent { + constructor(size: number) { + super("w:szCs"); + this.root.push( + new Attributes({ + val: size, + }), + ); + } +} + +export class RTL extends XmlComponent { + constructor() { + super("w:rtl"); + this.root.push( + new Attributes({ + val: true, + }), + ); + } +} diff --git a/src/file/paragraph/run/run.spec.ts b/src/file/paragraph/run/run.spec.ts index 050989c941..5fbe93ee92 100644 --- a/src/file/paragraph/run/run.spec.ts +++ b/src/file/paragraph/run/run.spec.ts @@ -134,7 +134,21 @@ describe("Run", () => { run.size(24); const tree = new Formatter().format(run); expect(tree).to.deep.equal({ - "w:r": [{ "w:rPr": [{ "w:sz": [{ _attr: { "w:val": 24 } }] }] }], + "w:r": [ + { + "w:rPr": [{ "w:sz": [{ _attr: { "w:val": 24 } }] }, { "w:szCs": [{ _attr: { "w:val": 24 } }] }], + }, + ], + }); + }); + }); + + describe("#rtl", () => { + it("should set the run to the RTL mode", () => { + run.rtl(); + const tree = new Formatter().format(run); + expect(tree).to.deep.equal({ + "w:r": [{ "w:rPr": [{ "w:rtl": [{ _attr: { "w:val": true } }] }] }], }); }); }); diff --git a/src/file/paragraph/run/run.ts b/src/file/paragraph/run/run.ts index c258c52702..ea45bbcd34 100644 --- a/src/file/paragraph/run/run.ts +++ b/src/file/paragraph/run/run.ts @@ -1,7 +1,7 @@ // http://officeopenxml.com/WPtext.php import { Break } from "./break"; import { Caps, SmallCaps } from "./caps"; -import { Bold, Color, DoubleStrike, Italics, Size, Strike } from "./formatting"; +import { Bold, Color, DoubleStrike, Italics, RTL, Size, SizeCs, Strike } from "./formatting"; import { Begin, End, Page, Separate } from "./page-number"; import { RunProperties } from "./properties"; import { RunFonts } from "./run-fonts"; @@ -43,6 +43,12 @@ export class Run extends XmlComponent { public size(size: number): Run { this.properties.push(new Size(size)); + this.properties.push(new SizeCs(size)); + return this; + } + + public rtl(): Run { + this.properties.push(new RTL()); return this; } @@ -94,8 +100,8 @@ export class Run extends XmlComponent { return this; } - public font(fontName: string): Run { - this.properties.push(new RunFonts(fontName)); + public font(fontName: string, hint?: string | undefined): Run { + this.properties.push(new RunFonts(fontName, hint)); return this; } diff --git a/src/file/styles/defaults/run-properties.ts b/src/file/styles/defaults/run-properties.ts index c00b6ccc79..1f4018dee2 100644 --- a/src/file/styles/defaults/run-properties.ts +++ b/src/file/styles/defaults/run-properties.ts @@ -1,5 +1,5 @@ import { XmlComponent } from "file/xml-components"; -import { Size } from "../../paragraph/run/formatting"; +import { Size, SizeCs } from "../../paragraph/run/formatting"; import { RunProperties } from "../../paragraph/run/properties"; import { RunFonts } from "../../paragraph/run/run-fonts"; @@ -14,6 +14,7 @@ export class RunPropertiesDefaults extends XmlComponent { public size(size: number): RunPropertiesDefaults { this.properties.push(new Size(size)); + this.properties.push(new SizeCs(size)); return this; } diff --git a/src/file/styles/style/index.ts b/src/file/styles/style/index.ts index a3b25b7520..21e60b3d43 100644 --- a/src/file/styles/style/index.ts +++ b/src/file/styles/style/index.ts @@ -74,6 +74,7 @@ export class ParagraphStyle extends Style { public size(twips: number): ParagraphStyle { this.addRunProperty(new formatting.Size(twips)); + this.addRunProperty(new formatting.SizeCs(twips)); return this; } @@ -282,6 +283,7 @@ export class CharacterStyle extends Style { public size(twips: number): CharacterStyle { this.addRunProperty(new formatting.Size(twips)); + this.addRunProperty(new formatting.SizeCs(twips)); return this; } } diff --git a/src/file/styles/styles.spec.ts b/src/file/styles/styles.spec.ts index 4c888a4f02..1c828337b2 100644 --- a/src/file/styles/styles.spec.ts +++ b/src/file/styles/styles.spec.ts @@ -362,7 +362,7 @@ describe("ParagraphStyle", () => { { _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } }, { "w:pPr": [] }, { - "w:rPr": [{ "w:sz": [{ _attr: { "w:val": 24 } }] }], + "w:rPr": [{ "w:sz": [{ _attr: { "w:val": 24 } }] }, { "w:szCs": [{ _attr: { "w:val": 24 } }] }], }, ], });