:feat: Font for eastAsia

This commit is contained in:
wangfengming
2020-06-07 12:38:03 +08:00
parent 88340aa336
commit fdfce79e87
6 changed files with 53 additions and 29 deletions

View File

@ -3,7 +3,7 @@ import { Attributes, XmlComponent } from "file/xml-components";
export { Underline } from "./underline"; export { Underline } from "./underline";
export { EmphasisMark } from "./emphasis-mark"; export { EmphasisMark } from "./emphasis-mark";
export { SubScript, SuperScript } from "./script"; export { SubScript, SuperScript } from "./script";
export { RunFonts } from "./run-fonts"; export { RunFonts, IFontAttributesProperties } from "./run-fonts";
export class Bold extends XmlComponent { export class Bold extends XmlComponent {
constructor() { constructor() {

View File

@ -1,14 +1,14 @@
import { XmlAttributeComponent, XmlComponent } from "file/xml-components"; import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
interface IRunFontAttributesProperties { export interface IFontAttributesProperties {
readonly ascii: string; readonly ascii?: string;
readonly cs: string; readonly cs?: string;
readonly eastAsia: string; readonly eastAsia?: string;
readonly hAnsi: string; readonly hAnsi?: string;
readonly hint?: string; readonly hint?: string;
} }
class RunFontAttributes extends XmlAttributeComponent<IRunFontAttributesProperties> { class RunFontAttributes extends XmlAttributeComponent<IFontAttributesProperties> {
protected readonly xmlKeys = { protected readonly xmlKeys = {
ascii: "w:ascii", ascii: "w:ascii",
cs: "w:cs", cs: "w:cs",
@ -19,16 +19,26 @@ class RunFontAttributes extends XmlAttributeComponent<IRunFontAttributesProperti
} }
export class RunFonts extends XmlComponent { export class RunFonts extends XmlComponent {
constructor(ascii: string, hint?: string) { constructor(name: string, hint?: string);
constructor(attrs: string | IFontAttributesProperties);
constructor(nameOrAttrs: string | IFontAttributesProperties, hint?: string) {
super("w:rFonts"); super("w:rFonts");
this.root.push( if (typeof nameOrAttrs === "string") {
new RunFontAttributes({ // use constructor(name: string, hint?: string);
ascii: ascii, const name = nameOrAttrs;
cs: ascii, this.root.push(
eastAsia: ascii, new RunFontAttributes({
hAnsi: ascii, ascii: name,
hint: hint, cs: name,
}), eastAsia: name,
); hAnsi: name,
hint: hint,
}),
);
} else {
// use constructor(attrs: IRunFontAttributesProperties);
const attrs = nameOrAttrs;
this.root.push(new RunFontAttributes(attrs));
}
} }
} }

View File

@ -27,11 +27,16 @@ import {
import { NumberOfPages, NumberOfPagesSection, Page } from "./page-number"; import { NumberOfPages, NumberOfPagesSection, Page } from "./page-number";
import { RunProperties } from "./properties"; import { RunProperties } from "./properties";
import { Text } from "./run-components/text"; import { Text } from "./run-components/text";
import { RunFonts } from "./run-fonts"; import { IFontAttributesProperties, RunFonts } from "./run-fonts";
import { SubScript, SuperScript } from "./script"; import { SubScript, SuperScript } from "./script";
import { Style } from "./style"; import { Style } from "./style";
import { Underline, UnderlineType } from "./underline"; import { Underline, UnderlineType } from "./underline";
interface IFontOptions {
readonly name: string;
readonly hint?: string;
}
export interface IRunOptions { export interface IRunOptions {
readonly bold?: true; readonly bold?: true;
readonly italics?: true; readonly italics?: true;
@ -52,10 +57,7 @@ export interface IRunOptions {
readonly subScript?: boolean; readonly subScript?: boolean;
readonly superScript?: boolean; readonly superScript?: boolean;
readonly style?: string; readonly style?: string;
readonly font?: { readonly font?: IFontOptions | IFontAttributesProperties;
readonly name: string;
readonly hint?: string;
};
readonly highlight?: string; readonly highlight?: string;
readonly shading?: { readonly shading?: {
readonly type: ShadingType; readonly type: ShadingType;
@ -140,7 +142,11 @@ export class Run extends XmlComponent {
} }
if (options.font) { if (options.font) {
this.properties.push(new RunFonts(options.font.name, options.font.hint)); if ("name" in options.font) {
this.properties.push(new RunFonts(options.font.name, options.font.hint));
} else {
this.properties.push(new RunFonts(options.font));
}
} }
if (options.highlight) { if (options.highlight) {

View File

@ -1,6 +1,6 @@
import { Size, SizeComplexScript } from "file/paragraph/run/formatting"; import { Size, SizeComplexScript } from "file/paragraph/run/formatting";
import { RunProperties } from "file/paragraph/run/properties"; import { RunProperties } from "file/paragraph/run/properties";
import { RunFonts } from "file/paragraph/run/run-fonts"; import { IFontAttributesProperties, RunFonts } from "file/paragraph/run/run-fonts";
import { XmlComponent } from "file/xml-components"; import { XmlComponent } from "file/xml-components";
export class RunPropertiesDefaults extends XmlComponent { export class RunPropertiesDefaults extends XmlComponent {
@ -18,8 +18,8 @@ export class RunPropertiesDefaults extends XmlComponent {
return this; return this;
} }
public font(fontName: string): RunPropertiesDefaults { public font(font: string | IFontAttributesProperties): RunPropertiesDefaults {
this.properties.push(new RunFonts(fontName)); this.properties.push(new RunFonts(font));
return this; return this;
} }
} }

View File

@ -1,4 +1,11 @@
import { AlignmentType, EmphasisMarkType, IIndentAttributesProperties, ISpacingProperties, UnderlineType } from "../paragraph"; import {
AlignmentType,
EmphasisMarkType,
IFontAttributesProperties,
IIndentAttributesProperties,
ISpacingProperties,
UnderlineType,
} from "../paragraph";
import { ShadingType } from "../table"; import { ShadingType } from "../table";
export interface IRunStyleOptions { export interface IRunStyleOptions {
@ -19,7 +26,7 @@ export interface IRunStyleOptions {
readonly type?: EmphasisMarkType; readonly type?: EmphasisMarkType;
}; };
readonly color?: string; readonly color?: string;
readonly font?: string; readonly font?: string | IFontAttributesProperties;
readonly characterSpacing?: number; readonly characterSpacing?: number;
readonly highlight?: string; readonly highlight?: string;
readonly shadow?: { readonly shadow?: {

View File

@ -1,6 +1,7 @@
import { EmphasisMarkType } from "file/paragraph/run/emphasis-mark"; import { EmphasisMarkType } from "file/paragraph/run/emphasis-mark";
import * as formatting from "file/paragraph/run/formatting"; import * as formatting from "file/paragraph/run/formatting";
import { RunProperties } from "file/paragraph/run/properties"; import { RunProperties } from "file/paragraph/run/properties";
import { IFontAttributesProperties } from "file/paragraph/run/run-fonts";
import { UnderlineType } from "file/paragraph/run/underline"; import { UnderlineType } from "file/paragraph/run/underline";
import { BasedOn, Link, SemiHidden, UiPriority, UnhideWhenUsed } from "./components"; import { BasedOn, Link, SemiHidden, UiPriority, UnhideWhenUsed } from "./components";
@ -28,7 +29,7 @@ export interface IBaseCharacterStyleOptions {
readonly type?: EmphasisMarkType; readonly type?: EmphasisMarkType;
}; };
readonly color?: string; readonly color?: string;
readonly font?: string; readonly font?: string | IFontAttributesProperties;
readonly characterSpacing?: number; readonly characterSpacing?: number;
readonly highlight?: string; readonly highlight?: string;
readonly shadow?: { readonly shadow?: {