2 var __createBinding
= (this && this.__createBinding
) || (Object
.create
? (function(o
, m
, k
, k2
) {
3 if (k2
=== undefined) k2
= k
;
4 var desc
= Object
.getOwnPropertyDescriptor(m
, k
);
5 if (!desc
|| ("get" in desc
? !m
.__esModule
: desc
.writable
|| desc
.configurable
)) {
6 desc
= { enumerable
: true, get: function() { return m
[k
]; } };
8 Object
.defineProperty(o
, k2
, desc
);
9 }) : (function(o
, m
, k
, k2
) {
10 if (k2
=== undefined) k2
= k
;
13 var __setModuleDefault
= (this && this.__setModuleDefault
) || (Object
.create
? (function(o
, v
) {
14 Object
.defineProperty(o
, "default", { enumerable
: true, value
: v
});
18 var __importStar
= (this && this.__importStar
) || function (mod
) {
19 if (mod
&& mod
.__esModule
) return mod
;
21 if (mod
!= null) for (var k
in mod
) if (k
!== "default" && Object
.prototype.hasOwnProperty
.call(mod
, k
)) __createBinding(result
, mod
, k
);
22 __setModuleDefault(result
, mod
);
25 var __awaiter
= (this && this.__awaiter
) || function (thisArg
, _arguments
, P
, generator
) {
26 function adopt(value
) { return value
instanceof P
? value
: new P(function (resolve
) { resolve(value
); }); }
27 return new (P
|| (P
= Promise
))(function (resolve
, reject
) {
28 function fulfilled(value
) { try { step(generator
.next(value
)); } catch (e
) { reject(e
); } }
29 function rejected(value
) { try { step(generator
["throw"](value
)); } catch (e
) { reject(e
); } }
30 function step(result
) { result
.done
? resolve(result
.value
) : adopt(result
.value
).then(fulfilled
, rejected
); }
31 step((generator
= generator
.apply(thisArg
, _arguments
|| [])).next());
34 Object
.defineProperty(exports
, "__esModule", { value
: true });
35 exports
.Clo
= exports
.calculateTextWidthHeightAux
= exports
.calculateTextWidthHeight
= exports
.hyphenTkTree
= exports
.filterEmptyString
= exports
.spacesToBreakpoint
= exports
.hyphenForClo
= exports
.splitCJKV
= exports
.twoReturnsToNewline
= exports
.ptToPx
= exports
.cjkvRegexPattern
= exports
.cjkvBlocksInRegex
= exports
.defaultFrameStyle
= exports
.defaultTextStyle
= exports
.A4_IN_PX
= exports
.Direction
= void 0;
36 const canva_1
= require("../canva");
37 const fontkit
= __importStar(require("fontkit"));
48 (function (Direction
) {
49 Direction
[Direction
["LTR"] = 0] = "LTR";
50 Direction
[Direction
["RTL"] = 1] = "RTL";
51 Direction
[Direction
["TTB"] = 2] = "TTB";
52 Direction
[Direction
["BTT"] = 3] = "BTT";
53 })(Direction
|| (exports
.Direction
= Direction
= {}));
57 exports
.A4_IN_PX
= { "width": 793.7,
59 exports
.defaultTextStyle
= {
62 textWeight
: canva_1
.TextWeight
.REGULAR
,
63 fontStyle
: canva_1
.FontStyle
.ITALIC
,
65 exports
.defaultFrameStyle
= {
66 directionInsideLine
: Direction
.LTR
,
67 direction
: Direction
.TTB
,
68 baseLineskip
: ptToPx(15),
69 textStyle
: exports
.defaultTextStyle
,
70 x
: exports
.A4_IN_PX
.width
* 0.10,
71 y
: exports
.A4_IN_PX
.height
* 0.10,
72 width
: exports
.A4_IN_PX
.width
* 0.80,
73 height
: exports
.A4_IN_PX
.height
* 0.80,
77 * definition for cjk scripts
78 * - Hani : Han Character
84 exports
.cjkvBlocksInRegex
= ["Hani", "Hang", "Bopo", "Kana", "Hira"];
85 exports
.cjkvRegexPattern
= new RegExp("((?:" +
86 exports
.cjkvBlocksInRegex
.map((x
) => "\\p{Script_Extensions=" + x
+ "}").join("|") + ")+)", "gu");
92 * @param pt pt size value
93 * @returns the corresponding px value
96 return pt
* 4.0 / 3.0;
98 exports
.ptToPx
= ptToPx
;
103 * convert '\n\n' to newline command ["nl"]
104 * @param arr the input `tkTree`
105 * @param clo the `Clo` object
106 * @returns the input tktree
108 function twoReturnsToNewline(arr
, clo
) {
110 for (let i
= 0; i
< arr
.length
; i
++) {
112 if (!Array
.isArray(item
)) {
113 middle
= middle
.concat(item
.split(/(\n\n)/g));
120 for (let j
= 0; j
< middle
.length
; j
++) {
121 var item
= middle
[j
];
122 if (!Array
.isArray(item
) && item
== "\n\n") {
123 result
.push(["nl"]); // push a newline command to the result `tkTree`
126 result
.push(middle
[j
]);
131 exports
.twoReturnsToNewline
= twoReturnsToNewline
;
133 * split CJKV and non-CJKV
135 * @param arr : input tkTree
136 * @returns a splitted tkTree (by CJK and NonCJK)
139 * [`many臺中daylight`] => [`many`, `臺中`, `dahylight`]
142 function splitCJKV(arr
, clo
) {
144 for (let i
= 0; i
< arr
.length
; i
++) {
146 if (!Array
.isArray(item
)) {
147 result
= result
.concat(item
.split(exports
.cjkvRegexPattern
));
155 exports
.splitCJKV
= splitCJKV
;
157 * hyphenation for a clo document
158 * @param arr the array for a `tkTree`
159 * @param clo the Clo object
161 function hyphenForClo(arr
, clo
) {
162 let hyphenLanguage
= clo
.attrs
["hyphenLanguage"];
163 let res
= hyphenTkTree(arr
, hyphenLanguage
);
166 exports
.hyphenForClo
= hyphenForClo
;
168 * convert spaces to Breakpoint
169 * \s+ => ["bp" [\s+] ""]
170 * @param arr the tkTree input text stream
171 * @param clo the Clo object
172 * @returns the converted object
174 function spacesToBreakpoint(arr
, clo
) {
175 let spacePattern
= /^([ \t]+)$/g;
177 for (let i
= 0; i
< arr
.length
; i
++) {
179 if (!Array
.isArray(item
) && item
.match(spacePattern
)) {
180 result
.push(['bp', item
, ""]); // push a newline command to the result `tkTree`
188 exports
.spacesToBreakpoint
= spacesToBreakpoint
;
190 * remove all the `` (empty string) in the arr
191 * @param arr the tkTree to be filtered
192 * @param clo the Clo file
194 function filterEmptyString(arr
, clo
) {
195 if (Array
.isArray(arr
)) {
196 arr
.filter((x
) => { return x
!= ``; });
200 exports
.filterEmptyString
= filterEmptyString
;
205 * hyphenate for a tkTree
206 * - hyphenation => ["bp", "", "-"]
207 * @param arr the tkTree array
208 * @param lang ISO 639 code for the language
210 function hyphenTkTree(arr
, lang
) {
211 // import corresponding hyphen language data and function
212 let hyphen
= require("hyphen/" + lang
);
214 for (let i
= 0; i
< arr
.length
; i
++) {
215 let element
= arr
[i
];
216 let splitter
= "分"; // a CJKV
217 if (!Array
.isArray(element
)) {
218 let hyphenatedElement
= hyphen
.hyphenateSync(element
, { hyphenChar
: splitter
});
219 let hyphenatedSplitted
= hyphenatedElement
.split(splitter
);
220 var newSplitted
= [];
221 for (var j
= 0; j
< hyphenatedSplitted
.length
- 1; j
++) {
222 newSplitted
.push(hyphenatedSplitted
[j
]);
223 // "bp" for breakpoint
224 newSplitted
.push(["bp", "", "-"]); //insert a breakable point (bp) mark
226 newSplitted
.push(hyphenatedSplitted
[hyphenatedSplitted
.length
- 1]);
227 result
= result
.concat(newSplitted
);
230 result
.push(element
);
235 exports
.hyphenTkTree
= hyphenTkTree
;
237 * calculate the text width and Height with a given `TextStyle`
238 * @param preprocessed
239 * @param defaultFontStyle
241 function calculateTextWidthHeight(element
, style
) {
242 return __awaiter(this, void 0, void 0, function* () {
244 for (var i
= 0; i
< element
.length
; i
++) {
245 res
.push(yield calculateTextWidthHeightAux(element
[i
], style
));
251 exports
.calculateTextWidthHeight
= calculateTextWidthHeight
;
253 * calculate the text width and Height with a given `TextStyle`
254 * @param preprocessed
255 * @param defaultFontStyle
257 function calculateTextWidthHeightAux(element
, style
) {
258 return __awaiter(this, void 0, void 0, function* () {
260 let fontPair
= (0, canva_1
.fontStyleTofont
)(style
);
261 if (fontPair
.path
.match(/\.ttc$/)) {
262 var font
= yield fontkit
.openSync(fontPair
.path
, fontPair
.psName
);
265 var font
= yield fontkit
.openSync(fontPair
.path
);
267 if (!Array
.isArray(element
)) {
268 var run
= font
.layout(element
, undefined, undefined, undefined, "ltr");
269 for (var j
= 0; j
< run
.glyphs
.length
; j
++) {
270 let runGlyphsItem
= run
.glyphs
[j
];
275 direction
: Direction
.LTR
,
276 width
: (runGlyphsItem
.advanceWidth
) * (style
.size
) / 1000,
277 height
: (runGlyphsItem
.bbox
.maxY
- runGlyphsItem
.bbox
.minY
) * (style
.size
) / 1000,
279 minX
: runGlyphsItem
.bbox
.minX
,
280 maxX
: runGlyphsItem
.bbox
.maxX
,
281 minY
: runGlyphsItem
.bbox
.minY
,
282 maxY
: runGlyphsItem
.bbox
.maxY
288 else if (element
[0] == "bp") {
289 let beforeNewLine
= yield calculateTextWidthHeightAux(element
[1], style
);
290 let afterNewLine
= yield calculateTextWidthHeightAux(element
[2], style
);
291 return ["bp", beforeNewLine
, afterNewLine
];
294 return calculateTextWidthHeight(element
[1], style
);
298 exports
.calculateTextWidthHeightAux
= calculateTextWidthHeightAux
;
300 * whole document-representing class
304 this.preprocessors
= [];
305 this.mainStream
= [];
307 "page": exports
.A4_IN_PX
,
308 "defaultFrameStyle": exports
.defaultFrameStyle
,
309 "hyphenLanguage": 'en' // hyphenated in the language (in ISO 639)
311 // register the precessor functions
312 this.preprocessorRegister(splitCJKV
);
313 this.preprocessorRegister(hyphenForClo
);
314 this.preprocessorRegister(twoReturnsToNewline
);
315 this.preprocessorRegister(spacesToBreakpoint
);
316 this.preprocessorRegister(filterEmptyString
);
319 Object
.assign(this.attrs
, attr
, val
);
322 if (Object
.keys(this.attrs
).length
=== 0) {
323 return this.attrs
[attr
];
330 * register a function of preprocessor
331 * @param f a function
333 preprocessorRegister(f
) {
334 this.preprocessors
.push(f
);
338 var preprocessed
= this.mainStream
;
339 for (var i
= 0; i
< this.preprocessors
.length
; i
++) {
340 preprocessed
= this.preprocessors
[i
](preprocessed
, this);
342 // generate the width and height of the stream
343 let defaultFontStyle
= this.attrs
["defaultFrameStyle"].textStyle
;
344 calculateTextWidthHeight(preprocessed
, defaultFontStyle
);
346 console
.log(preprocessed
);
351 export let a = new Clo();