Merge pull request #11 from felipeochoa/fonts

Fonts cleanup
This commit is contained in:
Dolan
2017-03-08 20:16:17 +00:00
committed by GitHub
5 changed files with 84 additions and 42 deletions

View File

@ -1,75 +1,81 @@
import {XmlComponent, Attributes} from "../xml-components"; import { Break } from "./break";
import {RunProperties} from "./properties"; import { Caps, SmallCaps } from "./caps";
import {Bold, Italics} from "./formatting"; import { Bold, Italics } from "./formatting";
import {Tab} from "./tab"; import { RunProperties } from "./properties";
import {Break} from "./break"; import { RunFonts } from "./run-fonts";
import {SmallCaps, Caps} from "./caps"; import { SubScript, SuperScript } from "./script";
import {Strike, DoubleStrike} from "./strike"; import { DoubleStrike, Strike } from "./strike";
import {SubScript, SuperScript} from "./script"; import { Tab } from "./tab";
import {Underline} from "./underline" import { Underline } from "./underline";
import { Attributes, XmlComponent } from "../xml-components";
export class Run extends XmlComponent { export class Run extends XmlComponent {
private properties: RunProperties; private properties: RunProperties;
constructor() { constructor() {
super("w:r"); super("w:r");
this.properties = new RunProperties(); this.properties = new RunProperties();
this.root.push(this.properties); this.root.push(this.properties);
} }
bold(): Run { public bold(): Run {
this.properties.push(new Bold()); this.properties.push(new Bold());
return this; return this;
} }
italic(): Run { public italic(): Run {
this.properties.push(new Italics()); this.properties.push(new Italics());
return this; return this;
} }
underline(): Run { public underline(): Run {
this.properties.push(new Underline()); this.properties.push(new Underline());
return this; return this;
} }
break(): Run { public break(): Run {
this.root.splice(1, 0, new Break()); this.root.splice(1, 0, new Break());
return this; return this;
} }
tab(): Run { public tab(): Run {
this.root.splice(1, 0, new Tab()); this.root.splice(1, 0, new Tab());
return this; return this;
} }
smallCaps(): Run { public smallCaps(): Run {
this.properties.push(new SmallCaps()); this.properties.push(new SmallCaps());
return this; return this;
} }
allCaps(): Run { public allCaps(): Run {
this.properties.push(new Caps()); this.properties.push(new Caps());
return this; return this;
} }
strike(): Run { public strike(): Run {
this.properties.push(new Strike()); this.properties.push(new Strike());
return this; return this;
} }
doubleStrike(): Run { public doubleStrike(): Run {
this.properties.push(new DoubleStrike()); this.properties.push(new DoubleStrike());
return this; return this;
} }
subScript(): Run { public subScript(): Run {
this.properties.push(new SubScript()); this.properties.push(new SubScript());
return this; return this;
} }
superScript(): Run { public superScript(): Run {
this.properties.push(new SuperScript()); this.properties.push(new SuperScript());
return this; return this;
} }
}
public font(fontName: string): Run {
this.properties.push(new RunFonts(fontName));
return this;
}
}

View File

@ -1,9 +1,9 @@
import {XmlAttributeComponent, XmlComponent} from "../docx/xml-components"; import {XmlAttributeComponent, XmlComponent} from "../xml-components";
interface IRunFontAttributesProperties { interface IRunFontAttributesProperties {
ascii: string; ascii: string;
hAnsi: string; hAnsi: string;
hint: string; hint?: string;
} }
class RunFontAttributes extends XmlAttributeComponent { class RunFontAttributes extends XmlAttributeComponent {
@ -19,7 +19,7 @@ class RunFontAttributes extends XmlAttributeComponent {
export class RunFonts extends XmlComponent { export class RunFonts extends XmlComponent {
constructor(ascii: string, hint: string) { constructor(ascii: string, hint?: string) {
super("w:rFonts"); super("w:rFonts");
this.root.push(new RunFontAttributes({ this.root.push(new RunFontAttributes({
ascii: ascii, ascii: ascii,

View File

@ -1,11 +1,11 @@
import * as _ from "lodash"; import * as _ from "lodash";
import { DocumentAttributes } from "../docx/document/document-attributes"; import { DocumentAttributes } from "../docx/document/document-attributes";
import { RunFonts } from "../docx/run/run-fonts";
import { MultiPropertyXmlComponent } from "../docx/xml-components"; import { MultiPropertyXmlComponent } from "../docx/xml-components";
import { AbstractNumbering } from "./abstract-numbering"; import { AbstractNumbering } from "./abstract-numbering";
import { Indent } from "./indent"; import { Indent } from "./indent";
import { Level } from "./level"; import { Level } from "./level";
import { Num } from "./num"; import { Num } from "./num";
import { RunFonts } from "./run-fonts";
export class Numbering extends MultiPropertyXmlComponent { export class Numbering extends MultiPropertyXmlComponent {
private nextId: number; private nextId: number;

View File

@ -0,0 +1,22 @@
import { expect } from "chai";
import { RunFonts } from "../../../docx/run/run-fonts";
import { Formatter } from "../../../export/formatter";
describe("RunFonts", () => {
describe("#constructor()", () => {
it("uses the font name for both ascii and hAnsi", () => {
const tree = new Formatter().format(new RunFonts("Times"));
expect(tree).to.deep.equal({
"w:rFonts": [{_attr: {"w:ascii": "Times", "w:hAnsi": "Times"}}],
});
});
it("uses hint if given", () => {
const tree = new Formatter().format(new RunFonts("Times", "default"));
expect(tree).to.deep.equal({
"w:rFonts": [{_attr: {"w:ascii": "Times", "w:hAnsi": "Times", "w:hint": "default"}}],
});
});
});
});

View File

@ -1,10 +1,10 @@
import { assert, expect } from "chai";
import { Run } from "../../../docx/run"; import { Run } from "../../../docx/run";
import { TextRun } from "../../../docx/run/text-run"; import { TextRun } from "../../../docx/run/text-run";
import { assert } from "chai"; import { Formatter } from "../../../export/formatter";
function jsonify(obj: Object) { function jsonify(obj: object) {
let stringifiedJson = JSON.stringify(obj); return JSON.parse(JSON.stringify(obj));
return JSON.parse(stringifiedJson);
} }
describe("Run", () => { describe("Run", () => {
@ -17,7 +17,7 @@ describe("Run", () => {
describe("#bold()", () => { describe("#bold()", () => {
it("it should add bold to the properties", () => { it("it should add bold to the properties", () => {
run.bold(); run.bold();
let newJson = jsonify(run); const newJson = jsonify(run);
assert.equal(newJson.root[0].root[0].rootKey, "w:b"); assert.equal(newJson.root[0].root[0].rootKey, "w:b");
}); });
}); });
@ -25,7 +25,7 @@ describe("Run", () => {
describe("#italic()", () => { describe("#italic()", () => {
it("it should add italics to the properties", () => { it("it should add italics to the properties", () => {
run.italic(); run.italic();
let newJson = jsonify(run); const newJson = jsonify(run);
assert.equal(newJson.root[0].root[0].rootKey, "w:i"); assert.equal(newJson.root[0].root[0].rootKey, "w:i");
}); });
}); });
@ -33,7 +33,7 @@ describe("Run", () => {
describe("#underline()", () => { describe("#underline()", () => {
it("it should add underline to the properties", () => { it("it should add underline to the properties", () => {
run.underline(); run.underline();
let newJson = jsonify(run); const newJson = jsonify(run);
assert.equal(newJson.root[0].root[0].rootKey, "w:u"); assert.equal(newJson.root[0].root[0].rootKey, "w:u");
}); });
}); });
@ -41,7 +41,7 @@ describe("Run", () => {
describe("#smallCaps()", () => { describe("#smallCaps()", () => {
it("it should add smallCaps to the properties", () => { it("it should add smallCaps to the properties", () => {
run.smallCaps(); run.smallCaps();
let newJson = jsonify(run); const newJson = jsonify(run);
assert.equal(newJson.root[0].root[0].rootKey, "w:smallCaps"); assert.equal(newJson.root[0].root[0].rootKey, "w:smallCaps");
}); });
}); });
@ -49,7 +49,7 @@ describe("Run", () => {
describe("#caps()", () => { describe("#caps()", () => {
it("it should add caps to the properties", () => { it("it should add caps to the properties", () => {
run.allCaps(); run.allCaps();
let newJson = jsonify(run); const newJson = jsonify(run);
assert.equal(newJson.root[0].root[0].rootKey, "w:caps"); assert.equal(newJson.root[0].root[0].rootKey, "w:caps");
}); });
}); });
@ -57,7 +57,7 @@ describe("Run", () => {
describe("#strike()", () => { describe("#strike()", () => {
it("it should add strike to the properties", () => { it("it should add strike to the properties", () => {
run.strike(); run.strike();
let newJson = jsonify(run); const newJson = jsonify(run);
assert.equal(newJson.root[0].root[0].rootKey, "w:strike"); assert.equal(newJson.root[0].root[0].rootKey, "w:strike");
}); });
}); });
@ -65,26 +65,40 @@ describe("Run", () => {
describe("#doubleStrike()", () => { describe("#doubleStrike()", () => {
it("it should add caps to the properties", () => { it("it should add caps to the properties", () => {
run.doubleStrike(); run.doubleStrike();
let newJson = jsonify(run); const newJson = jsonify(run);
assert.equal(newJson.root[0].root[0].rootKey, "w:dstrike"); assert.equal(newJson.root[0].root[0].rootKey, "w:dstrike");
}); });
}); });
describe("#break()", () => { describe("#break()", () => {
it("it should add break to the run", () => { it("it should add break to the run", () => {
let run = new Run();
run.break(); run.break();
let newJson = jsonify(run); const newJson = jsonify(run);
assert.equal(newJson.root[1].rootKey, "w:br"); assert.equal(newJson.root[1].rootKey, "w:br");
}); });
}); });
describe("#tab()", () => { describe("#tab()", () => {
it("it should add break to the run", () => { it("it should add break to the run", () => {
let run = new Run();
run.tab(); run.tab();
let newJson = jsonify(run); const newJson = jsonify(run);
assert.equal(newJson.root[1].rootKey, "w:tab"); assert.equal(newJson.root[1].rootKey, "w:tab");
}); });
}); });
});
describe("#font()", () => {
it("should allow chaining calls", () => {
expect(run.font("Times")).to.equal(run);
});
it("should set the font as named", () => {
run.font("Times");
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [
{"w:rPr": [{"w:rFonts": [{_attr: {"w:ascii": "Times", "w:hAnsi": "Times"}}]}]},
],
});
});
});
});