+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)) {
+ result.push(['bp', item, ""]); // push a newline command to the result `tkTree`
+ }
+ 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(preprocessed, style) {
+ var dom = new jsdom_1.JSDOM(`<!DOCTYPE html><html><head></head>
+ <body><canvas id="canvas"></canvas></body></html>`);
+ try {
+ let canvas = dom.window.document.getElementById("canvas");
+ console.log(canvas);
+ /*if (!(canvas instanceof HTMLElement)){
+ throw new Error('the <canvas="canvas"> in the jsdom\'s DOM is not found.');
+
+ }*/
+ let context = canvas.getContext("2d");
+ console.log(context);
+ if (context == null) {
+ throw new Error('`canvas.getContext("2d");` can\'t be executed.');
+ }
+ context.font = `normal normal 10pt ${style.family}`;
+ console.log(context.font);
+ let txt = `Hello john`;
+ console.log(txt);
+ let measured = context.measureText(txt);
+ let width = measured.width;
+ let height = measured.actualBoundingBoxAscent;
+ let depth = measured.actualBoundingBoxDescent;
+ console.log("width: " + width);
+ console.log("height: " + height);
+ console.log("depth: " + depth);
+ }
+ catch (error) {
+ console.log("Exception " + error);
+ }
+}
+exports.calculateTextWidthHeight = calculateTextWidthHeight;
+/**
+ * whole document-representing class
+ */