]>
git.kianting.info Git - clo/blob - src/libclo/index.ts
7fd58c67868378a167ac574afca354868eaddaab
1 import { isKeyObject
, isStringObject
} from
"util/types";
2 import {tkTree
} from
"../parser";
3 import {TextStyle
, FontStyle
, TextWeight
} from
"../canva";
4 import { isString
} from
"util";
24 * frame box is a subclass of box
25 * - directionInsideLine : text direction inside a line
26 * - baselineskip : the distance between baselines in px
28 interface FrameBox
extends Box
{
29 directionInsideLine
: Direction
,
30 baseLineskip
: number | null,
39 fontStyle
: FontStyle
| null,
40 direction
: Direction
,
43 content
: string | Box
[] | null,
50 const A4_IN_PX
= {"width" : 793.7,
53 const defaultTextStyle
: FontStyle
= {
56 textWeight
: TextWeight
.REGULAR
,
57 textStyle
: TextStyle
.ITALIC
,
60 const defaultFrameStyle
: FrameBox
= {
61 directionInsideLine
: Direction
.LTR
,
62 direction
: Direction
.TTB
,
63 baseLineskip
: ptToPx(15),
64 fontStyle
: defaultTextStyle
,
65 x
: A4_IN_PX
.width
* 0.10,
66 y
: A4_IN_PX
.height
* 0.10,
67 width
: A4_IN_PX
.width
* 0.80,
68 height
: A4_IN_PX
.height
* 0.80,
72 const cjkvBlocksInRegex
= ["Hani"];
74 const cjkvRegexPattern
= new RegExp("((?:" +
75 cjkvBlocksInRegex
.map((x
)=>"\\p{Script_Extensions="+x
+"}").join("|") + ")+)", "gu");
81 * @param pt pt size value
82 * @returns the corresponding px value
84 function ptToPx(pt
: number) : number{
96 * split CJKV and non-CJKV
98 * @param arr : input tkTree
101 function splitCJKV(arr
: tkTree
): tkTree
{
102 var result
: tkTree
= [];
103 for (let i
= 0; i
< arr
.length
; i
++) {
105 if (!Array.isArray(item
)){
106 console
.log(item
.split(cjkvRegexPattern
));
107 result
= result
.concat(item
.split(cjkvRegexPattern
));
118 mainStream
: Array<string>;
119 preprocessors
: Array<Function>;
120 attributes
: {[index
: string]:any} ; // a4 size(x,y)
124 this.preprocessors
= [];
125 this.mainStream
= [];
126 this.attributes
= {"page" : A4_IN_PX
};
130 // register the precessor functions
131 this.preprocessorRegister(splitCJKV
);
134 public setAttr(attr
: string, val
: any):void{
135 Object.assign(this.attributes
, attr
, val
);
138 public getAttr(attr
:string) : any{
139 if (Object.keys(this.attributes
).length
=== 0){
140 return this.attributes
[attr
];
148 * register a function of preprocessor
149 * @param f a function
151 public preprocessorRegister(f
: Function){
152 this.preprocessors
.push(f
);
155 public generatePdf(){
157 var prepro
= this.mainStream
;
158 for (var i
= 0; i
<this.preprocessors
.length
; i
++){
159 prepro
= this.preprocessors
[i
](prepro
);
162 console
.log("test"+prepro
);
168 export let a
= new Clo();