]> git.kianting.info Git - clo/blob - src/libclo/index.ts
add cjk splitter
[clo] / src / libclo / index.ts
1 import { isKeyObject, isStringObject } from "util/types";
2 import {tkTree} from "../parser";
3 import {TextStyle, FontStyle, TextWeight} from "../canva";
4 import { isString } from "util";
5
6 /**
7 * TYPES
8 */
9
10 /**
11 * text direction
12 * LTR - left to right
13 * TTB - top to bottom
14 * etc.
15 */
16 enum Direction{
17 LTR,
18 RTL,
19 TTB,
20 BTT,
21 }
22
23 /**
24 * frame box is a subclass of box
25 * - directionInsideLine : text direction inside a line
26 * - baselineskip : the distance between baselines in px
27 */
28 interface FrameBox extends Box{
29 directionInsideLine : Direction,
30 baseLineskip : number | null,
31 }
32
33 /**
34 * a basic Box
35 */
36 interface Box{
37 x : number | null,
38 y : number | null,
39 fontStyle : FontStyle | null,
40 direction : Direction,
41 width : number,
42 height : number,
43 content : string | Box[] | null,
44 }
45
46
47 /**
48 * DEFAULT CONST PART
49 */
50 const A4_IN_PX = {"width" : 793.7,
51 "height" : 1122.5};
52
53 const defaultTextStyle : FontStyle = {
54 family : "FreeSans",
55 size : 12,
56 textWeight : TextWeight.REGULAR,
57 textStyle : TextStyle.ITALIC,
58 }
59
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,
69 content : null,
70 };
71
72 const cjkvBlocksInRegex = ["Hani"];
73
74 const cjkvRegexPattern = new RegExp("((?:" +
75 cjkvBlocksInRegex.map((x)=>"\\p{Script_Extensions="+x+"}").join("|") + ")+)", "gu");
76 /**
77 * FUNCTION PART
78 */
79 /**
80 * convert from ptToPx
81 * @param pt pt size value
82 * @returns the corresponding px value
83 */
84 function ptToPx(pt : number) : number{
85 return pt * 4 / 3.0;
86 }
87
88
89
90 /**
91 * REGISTER PART
92 */
93
94
95 /**
96 * split CJKV and non-CJKV
97 *
98 * @param arr : input tkTree
99 * @returns
100 */
101 function splitCJKV(arr : tkTree): tkTree{
102 var result : tkTree = [];
103 for (let i = 0; i < arr.length; i++) {
104 var item = arr[i];
105 if (!Array.isArray(item)){
106 console.log(item.split(cjkvRegexPattern));
107 result = result.concat(item.split(cjkvRegexPattern));
108 }
109 else{
110 result.push(item);
111 }
112 }
113
114 return result;
115 }
116
117 export class Clo{
118 mainStream : Array<string>;
119 preprocessors : Array<Function>;
120 attributes: {[index: string]:any} ; // a4 size(x,y)
121
122
123 constructor(){
124 this.preprocessors = [];
125 this.mainStream = [];
126 this.attributes = {"page" : A4_IN_PX};
127
128
129
130 // register the precessor functions
131 this.preprocessorRegister(splitCJKV);
132 }
133
134 public setAttr(attr : string, val : any):void{
135 Object.assign(this.attributes, attr, val);
136 }
137
138 public getAttr(attr:string) : any{
139 if (Object.keys(this.attributes).length === 0){
140 return this.attributes[attr];
141 }else{
142 return undefined;
143 }
144
145 }
146
147 /**
148 * register a function of preprocessor
149 * @param f a function
150 */
151 public preprocessorRegister(f : Function){
152 this.preprocessors.push(f);
153 }
154
155 public generatePdf(){
156 // preprocessed
157 var prepro = this.mainStream;
158 for (var i = 0; i<this.preprocessors.length; i++){
159 prepro = this.preprocessors[i](prepro);
160 }
161 // TODO
162 console.log("test"+prepro);
163 }
164
165
166 }
167
168 export let a = new Clo();
169 export default a;