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 util
= __importStar(require("node:util"));
39 const breakLines
= __importStar(require("./breakLines"));
50 (function (Direction
) {
51 Direction
[Direction
["LTR"] = 0] = "LTR";
52 Direction
[Direction
["RTL"] = 1] = "RTL";
53 Direction
[Direction
["TTB"] = 2] = "TTB";
54 Direction
[Direction
["BTT"] = 3] = "BTT";
55 })(Direction
|| (exports
.Direction
= Direction
= {}));
59 exports
.A4_IN_PX
= { "width": 793.7,
61 exports
.defaultTextStyle
= {
64 textWeight
: canva_1
.TextWeight
.REGULAR
,
65 fontStyle
: canva_1
.FontStyle
.ITALIC
,
67 exports
.defaultFrameStyle
= {
68 directionInsideLine
: Direction
.LTR
,
69 direction
: Direction
.TTB
,
70 baseLineskip
: ptToPx(15),
71 textStyle
: exports
.defaultTextStyle
,
72 x
: exports
.A4_IN_PX
.width
* 0.10,
73 y
: exports
.A4_IN_PX
.height
* 0.10,
74 width
: exports
.A4_IN_PX
.width
* 0.80,
75 height
: exports
.A4_IN_PX
.height
* 0.80,
79 * definition for cjk scripts
80 * - Hani : Han Character
86 exports
.cjkvBlocksInRegex
= ["Hani", "Hang", "Bopo", "Kana", "Hira"];
87 exports
.cjkvRegexPattern
= new RegExp("((?:" +
88 exports
.cjkvBlocksInRegex
.map((x
) => "\\p{Script_Extensions=" + x
+ "}").join("|") + ")+)", "gu");
94 * @param pt pt size value
95 * @returns the corresponding px value
98 return pt
* 4.0 / 3.0;
100 exports
.ptToPx
= ptToPx
;
105 * convert '\n\n' to newline command ["nl"]
106 * @param arr the input `tkTree`
107 * @param clo the `Clo` object
108 * @returns the input tktree
110 function twoReturnsToNewline(arr
, clo
) {
112 for (let i
= 0; i
< arr
.length
; i
++) {
114 if (!Array
.isArray(item
)) {
115 middle
= middle
.concat(item
.split(/(\n\n)/g));
122 for (let j
= 0; j
< middle
.length
; j
++) {
123 var item
= middle
[j
];
124 if (!Array
.isArray(item
) && item
== "\n\n") {
125 result
.push(["nl"]); // push a newline command to the result `tkTree`
128 result
.push(middle
[j
]);
133 exports
.twoReturnsToNewline
= twoReturnsToNewline
;
135 * split CJKV and non-CJKV
137 * @param arr : input tkTree
138 * @returns a splitted tkTree (by CJK and NonCJK)
141 * [`many臺中daylight`] => [`many`, `臺中`, `dahylight`]
144 function splitCJKV(arr
, clo
) {
146 for (let i
= 0; i
< arr
.length
; i
++) {
148 if (!Array
.isArray(item
)) {
149 result
= result
.concat(item
.split(exports
.cjkvRegexPattern
));
157 exports
.splitCJKV
= splitCJKV
;
159 * hyphenation for a clo document
160 * @param arr the array for a `tkTree`
161 * @param clo the Clo object
163 function hyphenForClo(arr
, clo
) {
164 let hyphenLanguage
= clo
.attrs
["hyphenLanguage"];
165 let res
= hyphenTkTree(arr
, hyphenLanguage
);
168 exports
.hyphenForClo
= hyphenForClo
;
170 * convert spaces to Breakpoint
171 * \s+ => ["bp" [\s+] ""]
172 * @param arr the tkTree input text stream
173 * @param clo the Clo object
174 * @returns the converted object
176 function spacesToBreakpoint(arr
, clo
) {
177 let spacePattern
= /^([ \t]+)$/g;
179 for (let i
= 0; i
< arr
.length
; i
++) {
181 if (!Array
.isArray(item
) && item
.match(spacePattern
)) {
182 // push a breakpoint command to the result `tkTree`
183 result
.push(['bp', [["hglue", "0.1"], item
], ""]);
191 exports
.spacesToBreakpoint
= spacesToBreakpoint
;
193 * remove all the `` (empty string) in the arr
194 * @param arr the tkTree to be filtered
195 * @param clo the Clo file
197 function filterEmptyString(arr
, clo
) {
198 if (Array
.isArray(arr
)) {
199 arr
.filter((x
) => { return x
!= ``; });
203 exports
.filterEmptyString
= filterEmptyString
;
208 * hyphenate for a tkTree
209 * - hyphenation => ["bp", "", "-"]
210 * @param arr the tkTree array
211 * @param lang ISO 639 code for the language
213 function hyphenTkTree(arr
, lang
) {
214 // import corresponding hyphen language data and function
215 let hyphen
= require("hyphen/" + lang
);
217 for (let i
= 0; i
< arr
.length
; i
++) {
218 let element
= arr
[i
];
219 let splitter
= "分"; // a CJKV
220 if (!Array
.isArray(element
)) {
221 let hyphenatedElement
= hyphen
.hyphenateSync(element
, { hyphenChar
: splitter
});
222 let hyphenatedSplitted
= hyphenatedElement
.split(splitter
);
223 var newSplitted
= [];
224 for (var j
= 0; j
< hyphenatedSplitted
.length
- 1; j
++) {
225 newSplitted
.push(hyphenatedSplitted
[j
]);
226 // "bp" for breakpoint
227 newSplitted
.push(["bp", "", "-"]); //insert a breakable point (bp) mark
229 newSplitted
.push(hyphenatedSplitted
[hyphenatedSplitted
.length
- 1]);
230 result
= result
.concat(newSplitted
);
233 result
.push(element
);
238 exports
.hyphenTkTree
= hyphenTkTree
;
240 * calculate the text width and Height with a given `TextStyle`
241 * @param preprocessed
242 * @param defaultFontStyle
244 function calculateTextWidthHeight(element
, style
) {
245 return __awaiter(this, void 0, void 0, function* () {
247 for (var i
= 0; i
< element
.length
; i
++) {
248 res
.push(yield calculateTextWidthHeightAux(element
[i
], style
));
254 exports
.calculateTextWidthHeight
= calculateTextWidthHeight
;
256 * calculate the text width and Height with a given `TextStyle`
257 * @param preprocessed
258 * @param defaultFontStyle
260 function calculateTextWidthHeightAux(element
, style
) {
261 return __awaiter(this, void 0, void 0, function* () {
263 let fontPair
= (0, canva_1
.fontStyleTofont
)(style
);
264 if (fontPair
.path
.match(/\.ttc$/)) {
265 var font
= yield fontkit
.openSync(fontPair
.path
, fontPair
.psName
);
268 var font
= yield fontkit
.openSync(fontPair
.path
);
270 if (!Array
.isArray(element
)) {
271 var run
= font
.layout(element
, undefined, undefined, undefined, "ltr");
272 for (var j
= 0; j
< run
.glyphs
.length
; j
++) {
273 let runGlyphsItem
= run
.glyphs
[j
];
278 direction
: Direction
.LTR
,
279 width
: (runGlyphsItem
.advanceWidth
) * (style
.size
) / 1000,
280 height
: (runGlyphsItem
.bbox
.maxY
- runGlyphsItem
.bbox
.minY
) * (style
.size
) / 1000,
282 minX
: runGlyphsItem
.bbox
.minX
,
283 maxX
: runGlyphsItem
.bbox
.maxX
,
284 minY
: runGlyphsItem
.bbox
.minY
,
285 maxY
: runGlyphsItem
.bbox
.maxY
291 else if (element
[0] == "bp") {
292 var beforeNewLine
= yield calculateTextWidthHeightAux(element
[1], style
);
293 if (Array
.isArray(beforeNewLine
)) {
294 beforeNewLine
= beforeNewLine
.flat();
296 let afterNewLine
= yield calculateTextWidthHeightAux(element
[2], style
);
297 if (Array
.isArray(afterNewLine
)) {
298 afterNewLine
= afterNewLine
.flat();
300 let breakPointNode
= {
301 original
: beforeNewLine
,
302 newLined
: afterNewLine
,
304 return breakPointNode
;
306 else if (element
[0] == "hglue" && !Array
.isArray(element
[1])) {
307 let hGlue
= { stretchFactor
: parseFloat(element
[1]) };
311 return calculateTextWidthHeight(element
, style
);
315 exports
.calculateTextWidthHeightAux
= calculateTextWidthHeightAux
;
317 * whole document-representing class
321 this.preprocessors
= [];
322 this.mainStream
= [];
324 "page": exports
.A4_IN_PX
,
325 "defaultFrameStyle": exports
.defaultFrameStyle
,
326 "hyphenLanguage": 'en' // hyphenated in the language (in ISO 639)
328 // register the precessor functions
329 this.preprocessorRegister(splitCJKV
);
330 this.preprocessorRegister(hyphenForClo
);
331 this.preprocessorRegister(twoReturnsToNewline
);
332 this.preprocessorRegister(spacesToBreakpoint
);
333 this.preprocessorRegister(filterEmptyString
);
336 Object
.assign(this.attrs
, attr
, val
);
339 if (Object
.keys(this.attrs
).length
=== 0) {
340 return this.attrs
[attr
];
347 * register a function of preprocessor
348 * @param f a function
350 preprocessorRegister(f
) {
351 this.preprocessors
.push(f
);
354 return __awaiter(this, void 0, void 0, function* () {
356 var preprocessed
= this.mainStream
;
357 for (var i
= 0; i
< this.preprocessors
.length
; i
++) {
358 preprocessed
= this.preprocessors
[i
](preprocessed
, this);
360 // generate the width and height of the stream
361 let defaultFontStyle
= this.attrs
["defaultFrameStyle"].textStyle
;
362 let a
= yield calculateTextWidthHeight(preprocessed
, defaultFontStyle
);
364 console
.log(util
.inspect(a
, true, 100));
365 console
.log(breakLines
.totalCost(a
, 3, 100));
371 export let a = new Clo();