+
+/**
+ * OTHER FUNCTIONS
+ */
+
+/**
+ * hyphenate for a tkTree
+ * - hyphenation => ["bp", "", "-"]
+ * @param arr the tkTree array
+ * @param lang ISO 639 code for the language
+ */
+export function hyphenTkTree(arr : tkTree, lang: string) : tkTree{
+ // import corresponding hyphen language data and function
+ let hyphen = require("hyphen/"+lang);
+
+ let result :tkTree[] = [];
+ for (let i = 0; i < arr.length; i++) {
+ let element = arr[i];
+ let splitter = "分"; // a CJKV
+ if (!Array.isArray(element)){
+ let hyphenatedElement : string = hyphen.hyphenateSync(element, {hyphenChar :splitter});
+ let hyphenatedSplitted : tkTree = hyphenatedElement.split(splitter);
+ var newSplitted : tkTree = [];
+ 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;
+}
+
+/**
+ * calculate the text width and Height with a given `TextStyle`
+ * @param preprocessed
+ * @param defaultFontStyle
+ */
+export function calculateTextWidthHeight(preprocessed : tkTree, style : TextStyle): void {
+ var dom = new 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 = (<HTMLCanvasElement>canvas).getContext("2d");
+ console.log(context);
+ if (context == null){
+ throw new Error('`canvas.getContext("2d");` can\'t be executed.');
+
+ }
+
+ context.font = `normal normal ${style.size}px ${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);
+ }
+
+
+}
+
+
+
+
+/**
+ * whole document-representing class
+ */