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"));
38 const breakLines
= __importStar(require("./breakLines"));
49 (function (Direction
) {
50 Direction
[Direction
["LTR"] = 0] = "LTR";
51 Direction
[Direction
["RTL"] = 1] = "RTL";
52 Direction
[Direction
["TTB"] = 2] = "TTB";
53 Direction
[Direction
["BTT"] = 3] = "BTT";
54 })(Direction
|| (exports
.Direction
= Direction
= {}));
58 exports
.A4_IN_PX
= { "width": 793.7,
60 exports
.defaultTextStyle
= {
63 textWeight
: canva_1
.TextWeight
.REGULAR
,
64 fontStyle
: canva_1
.FontStyle
.ITALIC
,
66 exports
.defaultFrameStyle
= {
67 directionInsideLine
: Direction
.LTR
,
68 direction
: Direction
.TTB
,
69 baseLineskip
: ptToPx(15),
70 textStyle
: exports
.defaultTextStyle
,
71 x
: exports
.A4_IN_PX
.width
* 0.10,
72 y
: exports
.A4_IN_PX
.height
* 0.10,
73 width
: exports
.A4_IN_PX
.width
* 0.80,
74 height
: exports
.A4_IN_PX
.height
* 0.80,
78 * definition for cjk scripts
79 * - Hani : Han Character
85 exports
.cjkvBlocksInRegex
= ["Hani", "Hang", "Bopo", "Kana", "Hira"];
86 exports
.cjkvRegexPattern
= new RegExp("((?:" +
87 exports
.cjkvBlocksInRegex
.map((x
) => "\\p{Script_Extensions=" + x
+ "}").join("|") + ")+)", "gu");
93 * @param pt pt size value
94 * @returns the corresponding px value
97 return pt
* 4.0 / 3.0;
99 exports
.ptToPx
= ptToPx
;
104 * convert '\n\n' to newline command ["nl"]
105 * @param arr the input `tkTree`
106 * @param clo the `Clo` object
107 * @returns the input tktree
109 function twoReturnsToNewline(arr
, clo
) {
111 for (let i
= 0; i
< arr
.length
; i
++) {
113 if (!Array
.isArray(item
)) {
114 middle
= middle
.concat(item
.split(/(\n\n)/g));
121 for (let j
= 0; j
< middle
.length
; j
++) {
122 var item
= middle
[j
];
123 if (!Array
.isArray(item
) && item
== "\n\n") {
124 result
.push(["nl"]); // push a newline command to the result `tkTree`
127 result
.push(middle
[j
]);
132 exports
.twoReturnsToNewline
= twoReturnsToNewline
;
134 * split CJKV and non-CJKV
136 * @param arr : input tkTree
137 * @returns a splitted tkTree (by CJK and NonCJK)
140 * [`many臺中daylight`] => [`many`, `臺中`, `dahylight`]
143 function splitCJKV(arr
, clo
) {
145 for (let i
= 0; i
< arr
.length
; i
++) {
147 if (!Array
.isArray(item
)) {
148 result
= result
.concat(item
.split(exports
.cjkvRegexPattern
));
156 exports
.splitCJKV
= splitCJKV
;
158 * hyphenation for a clo document
159 * @param arr the array for a `tkTree`
160 * @param clo the Clo object
162 function hyphenForClo(arr
, clo
) {
163 let hyphenLanguage
= clo
.attrs
["hyphenLanguage"];
164 let res
= hyphenTkTree(arr
, hyphenLanguage
);
167 exports
.hyphenForClo
= hyphenForClo
;
169 * convert spaces to Breakpoint
170 * \s+ => ["bp" [\s+] ""]
171 * @param arr the tkTree input text stream
172 * @param clo the Clo object
173 * @returns the converted object
175 function spacesToBreakpoint(arr
, clo
) {
176 let spacePattern
= /^([ \t]+)$/g;
178 for (let i
= 0; i
< arr
.length
; i
++) {
180 if (!Array
.isArray(item
) && item
.match(spacePattern
)) {
181 // push a breakpoint command to the result `tkTree`
182 result
.push(['bp', [["hglue", "0.1"], item
], ""]);
190 exports
.spacesToBreakpoint
= spacesToBreakpoint
;
192 * remove all the `` (empty string) in the arr
193 * @param arr the tkTree to be filtered
194 * @param clo the Clo file
196 function filterEmptyString(arr
, clo
) {
197 if (Array
.isArray(arr
)) {
198 arr
.filter((x
) => { return x
!= ``; });
202 exports
.filterEmptyString
= filterEmptyString
;
207 * hyphenate for a tkTree
208 * - hyphenation => ["bp", "", "-"]
209 * @param arr the tkTree array
210 * @param lang ISO 639 code for the language
212 function hyphenTkTree(arr
, lang
) {
213 // import corresponding hyphen language data and function
214 let hyphen
= require("hyphen/" + lang
);
216 for (let i
= 0; i
< arr
.length
; i
++) {
217 let element
= arr
[i
];
218 let splitter
= "分"; // a CJKV
219 if (!Array
.isArray(element
)) {
220 let hyphenatedElement
= hyphen
.hyphenateSync(element
, { hyphenChar
: splitter
});
221 let hyphenatedSplitted
= hyphenatedElement
.split(splitter
);
222 var newSplitted
= [];
223 for (var j
= 0; j
< hyphenatedSplitted
.length
- 1; j
++) {
224 newSplitted
.push(hyphenatedSplitted
[j
]);
225 // "bp" for breakpoint
226 newSplitted
.push(["bp", "", "-"]); //insert a breakable point (bp) mark
228 newSplitted
.push(hyphenatedSplitted
[hyphenatedSplitted
.length
- 1]);
229 result
= result
.concat(newSplitted
);
232 result
.push(element
);
237 exports
.hyphenTkTree
= hyphenTkTree
;
239 * calculate the text width and Height with a given `TextStyle`
240 * @param preprocessed
241 * @param defaultFontStyle
243 function calculateTextWidthHeight(element
, style
) {
244 return __awaiter(this, void 0, void 0, function* () {
246 for (var i
= 0; i
< element
.length
; i
++) {
247 res
.push(yield calculateTextWidthHeightAux(element
[i
], style
));
253 exports
.calculateTextWidthHeight
= calculateTextWidthHeight
;
255 * calculate the text width and Height with a given `TextStyle`
256 * @param preprocessed
257 * @param defaultFontStyle
259 function calculateTextWidthHeightAux(element
, style
) {
260 return __awaiter(this, void 0, void 0, function* () {
262 let fontPair
= (0, canva_1
.fontStyleTofont
)(style
);
263 if (fontPair
.path
.match(/\.ttc$/)) {
264 var font
= yield fontkit
.openSync(fontPair
.path
, fontPair
.psName
);
267 var font
= yield fontkit
.openSync(fontPair
.path
);
269 if (!Array
.isArray(element
)) {
270 var run
= font
.layout(element
, undefined, undefined, undefined, "ltr");
271 for (var j
= 0; j
< run
.glyphs
.length
; j
++) {
272 let runGlyphsItem
= run
.glyphs
[j
];
277 direction
: Direction
.LTR
,
278 width
: (runGlyphsItem
.advanceWidth
) * (style
.size
) / 1000,
279 height
: (runGlyphsItem
.bbox
.maxY
- runGlyphsItem
.bbox
.minY
) * (style
.size
) / 1000,
281 minX
: runGlyphsItem
.bbox
.minX
,
282 maxX
: runGlyphsItem
.bbox
.maxX
,
283 minY
: runGlyphsItem
.bbox
.minY
,
284 maxY
: runGlyphsItem
.bbox
.maxY
290 else if (element
[0] == "bp") {
291 var beforeNewLine
= yield calculateTextWidthHeightAux(element
[1], style
);
292 if (Array
.isArray(beforeNewLine
)) {
293 beforeNewLine
= beforeNewLine
.flat();
295 let afterNewLine
= yield calculateTextWidthHeightAux(element
[2], style
);
296 if (Array
.isArray(afterNewLine
)) {
297 afterNewLine
= afterNewLine
.flat();
299 let breakPointNode
= {
300 original
: beforeNewLine
,
301 newLined
: afterNewLine
,
303 return breakPointNode
;
305 else if (element
[0] == "hglue" && !Array
.isArray(element
[1])) {
306 let hGlue
= { stretchFactor
: parseFloat(element
[1]) };
310 return calculateTextWidthHeight(element
, style
);
314 exports
.calculateTextWidthHeightAux
= calculateTextWidthHeightAux
;
316 * whole document-representing class
320 this.preprocessors
= [];
321 this.mainStream
= [];
323 "page": exports
.A4_IN_PX
,
324 "defaultFrameStyle": exports
.defaultFrameStyle
,
325 "hyphenLanguage": 'en' // hyphenated in the language (in ISO 639)
327 // register the precessor functions
328 this.preprocessorRegister(splitCJKV
);
329 this.preprocessorRegister(hyphenForClo
);
330 this.preprocessorRegister(twoReturnsToNewline
);
331 this.preprocessorRegister(spacesToBreakpoint
);
332 this.preprocessorRegister(filterEmptyString
);
335 Object
.assign(this.attrs
, attr
, val
);
338 if (Object
.keys(this.attrs
).length
=== 0) {
339 return this.attrs
[attr
];
346 * register a function of preprocessor
347 * @param f a function
349 preprocessorRegister(f
) {
350 this.preprocessors
.push(f
);
353 return __awaiter(this, void 0, void 0, function* () {
355 var preprocessed
= this.mainStream
;
356 for (var i
= 0; i
< this.preprocessors
.length
; i
++) {
357 preprocessed
= this.preprocessors
[i
](preprocessed
, this);
359 // generate the width and height of the stream
360 let defaultFontStyle
= this.attrs
["defaultFrameStyle"].textStyle
;
361 let a
= yield calculateTextWidthHeight(preprocessed
, defaultFontStyle
);
362 let breakLineAlgorithms
= new breakLines
.BreakLineAlgorithm();
364 //console.log(breakLineAlgorithms.totalCost(a,70));
365 let segmentedNodes
= breakLineAlgorithms
.segmentedNodes(a
, 70);
366 console
.log(this.segmentedNodesToFrameBox(segmentedNodes
, this.attrs
["defaultFrameStyle"]));
369 segmentedNodesToFrameBox(segmentedNodes
, frame
) {
370 let baseLineskip
= frame
.baseLineskip
;
371 let boxArrayEmpty
= [];
375 textStyle
: frame
.textStyle
,
376 direction
: frame
.direction
,
378 height
: frame
.height
,
379 content
: boxArrayEmpty
,
381 var bigBoxContent
= boxArrayEmpty
;
382 let segmentedNodesFixed
= segmentedNodes
.map((x
) => this.removeBreakPoints(x
).flat());
383 let segmentedNodeUnglue
= segmentedNodesFixed
.map((x
) => this.removeGlue(x
, frame
).flat());
384 for (var i
= 0; i
< segmentedNodesFixed
.length
- 1; i
++) {
385 var currentLineSkip
= baseLineskip
;
386 var glyphMaxHeight
= this.getGlyphMaxHeight(segmentedNodesFixed
[i
]);
387 if (currentLineSkip
=== null || glyphMaxHeight
> currentLineSkip
) {
388 currentLineSkip
= glyphMaxHeight
;
390 var currentLineBox
= {
393 textStyle
: exports
.defaultTextStyle
,
394 direction
: frame
.directionInsideLine
,
396 height
: currentLineSkip
,
397 content
: segmentedNodeUnglue
[i
],
399 bigBoxContent
.push(currentLineBox
);
401 bigBox
.content
= bigBoxContent
;
405 * get the max height of the glyph`[a, b, c]`
406 * @param nodeLine the node line [a, b, c, ...]
409 getGlyphMaxHeight(nodeLine
) {
410 let segmentedNodeLineHeight
= nodeLine
.map((x
) => { if ("height" in x
&& x
.height
> 0.0) {
416 let maxHeight
= Math
.max(...segmentedNodeLineHeight
);
419 removeGlue(nodeLine
, frame
) {
420 let breakLineAlgorithms
= new breakLines
.BreakLineAlgorithm();
421 let glueRemoved
= nodeLine
.filter((x
) => !breakLineAlgorithms
.isHGlue(x
));
422 let onlyGlue
= nodeLine
.filter((x
) => breakLineAlgorithms
.isHGlue(x
));
423 let sumStretchFactor
= onlyGlue
.map((x
) => { if ("stretchFactor" in x
) {
424 return x
.stretchFactor
;
429 .reduce((acc
, cur
) => acc
+ cur
, 0);
430 let glueRemovedWidth
= glueRemoved
.map((x
) => { if ("width" in x
) {
436 .reduce((acc
, cur
) => acc
+ cur
, 0);
437 let offset
= frame
.width
- glueRemovedWidth
;
439 for (var i
= 0; i
< nodeLine
.length
; i
++) {
440 var ele
= nodeLine
[i
];
441 if (breakLineAlgorithms
.isHGlue(ele
)) {
446 direction
: frame
.directionInsideLine
,
447 width
: ele
.stretchFactor
/ sumStretchFactor
* offset
,
461 * @param boxitemline boxitem in a line with a breakpoint
462 * @returns boxitemline with break points removed
464 removeBreakPoints(boxitemline
) {
466 let breakLineAlgorithms
= new breakLines
.BreakLineAlgorithm();
467 for (var i
= 0; i
< boxitemline
.length
; i
++) {
468 let ele
= boxitemline
[i
];
469 if (breakLineAlgorithms
.isBreakPoint(ele
)) {
470 if (i
== boxitemline
.length
- 1) {
471 res
.push(ele
.newLined
);
474 res
.push(ele
.original
);
486 export let a = new Clo();