+/**
+ * hyphenation for a clo document
+ * @param arr the array for a `tkTree`
+ * @param clo the Clo object
+ */
+export function hyphenForClo(arr : tkTree, clo : Clo): tkTree{
+ let hyphenLanguage : string = clo.attrs["hyphenLanguage"];
+ let res = hyphenTkTree(arr, hyphenLanguage);
+ return res;
+
+}
+
+/**
+ * convert spaces to Breakpoint
+ * \s+ => ["bp" [\s+] ""]
+ * @param arr the tkTree input text stream
+ * @param clo the Clo object
+ * @returns the converted object
+ */
+export function spacesToBreakpoint(arr : tkTree, clo : Clo) : tkTree{
+ let spacePattern = /^([ \t]+)$/g;
+ var result : tkTree = [];
+ 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;
+}
+
+/**
+ * remove all the `` (empty string) in the arr
+ * @param arr the tkTree to be filtered
+ * @param clo the Clo file
+ */
+export function filterEmptyString(arr : tkTree, clo : Clo) : tkTree{
+ if (Array.isArray(arr)){
+ arr.filter((x)=>{return x != ``;});
+ }
+
+ return arr;
+}
+
+
+/**
+ * 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 async function calculateTextWidthHeight(element : tkTree, style : TextStyle): Promise<BoxesItem[]> {
+ var res = [];
+
+ for (var i=0; i<element.length; i++){
+ res.push(await calculateTextWidthHeightAux(element[i], style));
+ }
+
+ res = res.flat();
+
+ return res;
+}
+
+
+/**
+ * calculate the text width and Height with a given `TextStyle`
+ * @param preprocessed
+ * @param defaultFontStyle
+ */
+export async function calculateTextWidthHeightAux(element : tkTree, style : TextStyle): Promise<BoxesItem> {
+ var result : BoxesItem = [];
+
+
+
+ let fontPair = fontStyleTofont(style);
+ if (fontPair.path.match(/\.ttc$/)){
+ var font = await fontkit.openSync(fontPair.path, fontPair.psName);
+ }
+ else{
+ var font = await 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 : CharBox = {
+ 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 = await calculateTextWidthHeightAux(element[1], style);
+ if (Array.isArray(beforeNewLine)){
+ beforeNewLine = beforeNewLine.flat();
+ }
+
+ let afterNewLine = await calculateTextWidthHeightAux(element[2], style);
+ if (Array.isArray(afterNewLine)){
+ afterNewLine = afterNewLine.flat();
+ }
+
+ let breakPointNode : BreakPoint = {
+ original : beforeNewLine,
+ newLined : afterNewLine,
+ }
+
+ return breakPointNode;
+ }else if(element[0] == "hglue" && !Array.isArray(element[1])){
+ let hGlue : HGlue = {stretchFactor : parseFloat(element[1])}
+ return hGlue;
+ }
+ else{
+ return calculateTextWidthHeight(element, style);
+ }
+}
+
+
+
+
+/**
+ * whole document-representing class
+ */