+exports.splitCJKV = splitCJKV;
+/**
+ * hyphenation for a clo document
+ * @param arr the array for a `tkTree`
+ * @param clo the Clo object
+ */
+function hyphenForClo(arr, clo) {
+ let hyphenLanguage = clo.attrs["hyphenLanguage"];
+ let res = hyphenTkTree(arr, hyphenLanguage);
+ return res;
+}
+exports.hyphenForClo = hyphenForClo;
+/**
+ * convert spaces to Breakpoint
+ * \s+ => ["bp" [\s+] ""]
+ * @param arr the tkTree input text stream
+ * @param clo the Clo object
+ * @returns the converted object
+ */
+function spacesToBreakpoint(arr, clo) {
+ let spacePattern = /^([ \t]+)$/g;
+ var result = [];
+ for (let i = 0; i < arr.length; i++) {
+ var item = arr[i];
+ if (!Array.isArray(item) && item.match(spacePattern)) {
+ // push a breakpoint command to the result `tkTree`
+ result.push(['bp', [["hglue", "0.1"], item], ""]);
+ }
+ else {
+ result.push(item);
+ }
+ }
+ return result;
+}
+exports.spacesToBreakpoint = spacesToBreakpoint;
+/**
+ * remove all the `` (empty string) in the arr
+ * @param arr the tkTree to be filtered
+ * @param clo the Clo file
+ */
+function filterEmptyString(arr, clo) {
+ if (Array.isArray(arr)) {
+ arr.filter((x) => { return x != ``; });
+ }
+ return arr;
+}
+exports.filterEmptyString = filterEmptyString;
+/**
+ * OTHER FUNCTIONS
+ */
+/**
+ * hyphenate for a tkTree
+ * - hyphenation => ["bp", "", "-"]
+ * @param arr the tkTree array
+ * @param lang ISO 639 code for the language
+ */
+function hyphenTkTree(arr, lang) {
+ // import corresponding hyphen language data and function
+ let hyphen = require("hyphen/" + lang);
+ let result = [];
+ for (let i = 0; i < arr.length; i++) {
+ let element = arr[i];
+ let splitter = "分"; // a CJKV
+ if (!Array.isArray(element)) {
+ let hyphenatedElement = hyphen.hyphenateSync(element, { hyphenChar: splitter });
+ let hyphenatedSplitted = hyphenatedElement.split(splitter);
+ var newSplitted = [];
+ for (var j = 0; j < hyphenatedSplitted.length - 1; j++) {
+ newSplitted.push(hyphenatedSplitted[j]);
+ // "bp" for breakpoint
+ newSplitted.push(["bp", "", "-"]); //insert a breakable point (bp) mark
+ }
+ newSplitted.push(hyphenatedSplitted[hyphenatedSplitted.length - 1]);
+ result = result.concat(newSplitted);
+ }
+ else {
+ result.push(element);
+ }
+ }
+ return result;
+}
+exports.hyphenTkTree = hyphenTkTree;
+/**
+ * calculate the text width and Height with a given `TextStyle`
+ * @param preprocessed
+ * @param defaultFontStyle
+ */
+function calculateTextWidthHeight(element, style) {
+ return __awaiter(this, void 0, void 0, function* () {
+ var res = [];
+ for (var i = 0; i < element.length; i++) {
+ res.push(yield calculateTextWidthHeightAux(element[i], style));
+ }
+ res = res.flat();
+ return res;
+ });
+}
+exports.calculateTextWidthHeight = calculateTextWidthHeight;
+/**
+ * calculate the text width and Height with a given `TextStyle`
+ * @param preprocessed
+ * @param defaultFontStyle
+ */
+function calculateTextWidthHeightAux(element, style) {
+ return __awaiter(this, void 0, void 0, function* () {
+ var result = [];
+ let fontPair = (0, canva_1.fontStyleTofont)(style);
+ if (fontPair.path.match(/\.ttc$/)) {
+ var font = yield fontkit.openSync(fontPair.path, fontPair.psName);
+ }
+ else {
+ var font = yield fontkit.openSync(fontPair.path);
+ }
+ if (!Array.isArray(element)) {
+ var run = font.layout(element, undefined, undefined, undefined, "ltr");
+ for (var j = 0; j < run.glyphs.length; j++) {
+ let runGlyphsItem = run.glyphs[j];
+ let item = {
+ x: null,
+ y: null,
+ textStyle: style,
+ direction: Direction.LTR,
+ width: (runGlyphsItem.advanceWidth) * (style.size) / 1000,
+ height: (runGlyphsItem.bbox.maxY - runGlyphsItem.bbox.minY) * (style.size) / 1000,
+ content: element[j],
+ minX: runGlyphsItem.bbox.minX,
+ maxX: runGlyphsItem.bbox.maxX,
+ minY: runGlyphsItem.bbox.minY,
+ maxY: runGlyphsItem.bbox.maxY
+ };
+ result.push(item);
+ }
+ return result;
+ }
+ else if (element[0] == "bp") {
+ var beforeNewLine = yield calculateTextWidthHeightAux(element[1], style);
+ if (Array.isArray(beforeNewLine)) {
+ beforeNewLine = beforeNewLine.flat();
+ }
+ let afterNewLine = yield calculateTextWidthHeightAux(element[2], style);
+ if (Array.isArray(afterNewLine)) {
+ afterNewLine = afterNewLine.flat();
+ }
+ let breakPointNode = {
+ original: beforeNewLine,
+ newLined: afterNewLine,
+ };
+ return breakPointNode;
+ }
+ else if (element[0] == "hglue" && !Array.isArray(element[1])) {
+ let hGlue = { stretchFactor: parseFloat(element[1]) };
+ return hGlue;
+ }
+ else {
+ return calculateTextWidthHeight(element, style);
+ }
+ });
+}
+exports.calculateTextWidthHeightAux = calculateTextWidthHeightAux;
+/**
+ * whole document-representing class
+ */