X-Git-Url: https://git.kianting.info/?a=blobdiff_plain;f=src%2Flibclo%2FbreakLines.ts;fp=src%2Flibclo%2FbreakLines.ts;h=0392a23062743c6031a8d885e15af3cea3dfa357;hb=f12842e5a4ef531e38024e16f91b21c99ce1de92;hp=989ba935f64fba3a8bced08861a040cf78d79cae;hpb=a4f79a3761539f45ac7d86fe919bdc32cf290db0;p=clo diff --git a/src/libclo/breakLines.ts b/src/libclo/breakLines.ts index 989ba93..0392a23 100644 --- a/src/libclo/breakLines.ts +++ b/src/libclo/breakLines.ts @@ -3,107 +3,182 @@ */ import { join } from "path"; import {BreakPoint, BoxesItem, HGlue} from "./index.js"; +import { listenerCount } from "process"; +import { unwatchFile } from "fs"; /** - * Algorithms in LATEX language -TotalCost(i) = min_{j}~TotalCost(j) + LineCost(j, i)~~~~j=0, 1, ..., i-1 - -LineCost(j, i)= \begin{cases} -\infty ~~~ if~~LineWidth - \sum_{k=j+1}^{i-1} OrigWidth(item[k]) - newLineWidth(item[i]) < 0 \\ -\infty~~if~~NOT~~breakable(item[i]) \\ -(LineWidth - \sum_{k=j+1}^{i-1} OrigWidth(item[k]) - newLineWidth(item[i]))^3 ~~elsewhere -\end{cases} */ - -/**check if a boeitem is BreakPoint Type */ -function isBreakPoint (item : any) : item is BreakPoint{ - return (item as BreakPoint).newLined !== undefined; -} - -/**check if a boeitem is BreakPoint Type */ -function isHGlue (item : any) : item is HGlue{ - return (item as HGlue).stretchFactor !== undefined; -} - - -/** measuring original advance width */ -function origWidth(item : BoxesItem) : number{ - if (isBreakPoint(item)){ - console.log(item); - return origWidth(item.original); - }else if(Array.isArray(item)){ - return item.map((x)=>origWidth(x)) - .reduce((acc, current) => acc + current, - 0.0,) - }else if(isHGlue(item)){ - return 0.0; + * Algorithms in LATEX-like language + */ + +export class BreakLineAlgorithm { + + lineCostStorage : number[][]; + totalCostAuxStorage : number[]; + prevNodes : number[]; + + + + constructor(){ + this.prevNodes = []; + this.totalCostAuxStorage = []; + this.lineCostStorage = [[]]; } - else{ - return item.width; + + /**check if a boeitem is BreakPoint Type */ + isBreakPoint (item : any) : item is BreakPoint{ + return (item as BreakPoint).newLined !== undefined; } -} - -/** measuring new-line triggered advance width */ -function newLineWidth(item : BoxesItem) : number{ - if (isBreakPoint(item)){ - return origWidth(item.newLined); - }else{ - // impossible to make a new line - return Infinity; + + /**check if a boeitem is HGlue Type */ + isHGlue (item : any) : item is HGlue{ + return (item as HGlue).stretchFactor !== undefined; } -} -let lineCostStorage : any = new Object(); -/** - * check the total cost item[0..j]. - * @param items - * @param i - * @param lineWidth - */ -export function totalCost(items : BoxesItem[], j : number, lineWidth: number){ - if (j in lineCostStorage){ - return lineCostStorage[j]; + /** measuring original advance width */ + origWidth(item : BoxesItem) : number{ + if (this.isBreakPoint(item)){ + return this.origWidth(item.original); + }else if(Array.isArray(item)){ + return item.map((x)=>this.origWidth(x)) + .reduce((acc, current) => acc + current, + 0.0,) + }else if(this.isHGlue(item)){ + return 0.0; + } + else{ + return item.width; + } + } + + segmentedNodes(items : BoxesItem[], lineWidth : number) : BoxesItem[][]{ + this.totalCost(items ,lineWidth); + let nodeList = this.generateBreakLineNodeList(); + console.log("~~~", nodeList); + let res = []; + let low = -1; + let up = nodeList[0]; + + for(var i=0; i lCost){ - returnCost = lCost; + /**genrate the list of point of breaking line. it returns a correct list ascending*/ + generateBreakLineNodeList() : number[]{ + let res : number[] = []; + var pointer = this.prevNodes.length-1; + while (this.prevNodes[pointer] !== undefined){ + res.push(pointer); + pointer = this.prevNodes[pointer]; } + return res.reverse(); } - lineCostStorage[j] = returnCost; - return returnCost; + /** measuring new-line triggered advance width */ + newLineWidth(item : BoxesItem) : number{ + if (this.isBreakPoint(item)){ + return this.origWidth(item.newLined); + }else{ + // impossible to make a new line + return Infinity; + } + } + /** + * check all the total cost of paragraphes of the segnemt + */ + totalCost(items : BoxesItem[], lineWidth: number) : number{ + + let itemsLength = items.length; + this.lineCostStorage = Array(itemsLength); + this.prevNodes = Array(itemsLength).fill(null); + + + for (var i=0; i tmp){ + this.prevNodes[j] = k; + returnCost = tmp; + } + } + this.totalCostAuxStorage[j] = returnCost; + return returnCost; -/** - * check the line cost of a line containing items[i+1..j] - * @param items items of box - * @param i beginning (excluded) - * @param j end of the line - * @param lineWidth line width - */ -function lineCost(items : BoxesItem[], i : number, j : number, lineWidth: number){ - if (!isBreakPoint(items[j])){ - return Infinity; - }else{ - var tmpItemWidth = 0; - for (var k = i+1; k lineWidth){ + + /** + * check the line cost of a line containing items[i..j] + * @param items items of box + * @param i beginning (excluded) + * @param j end of the line + * @param lineWidth line width + */ + lineCost(items : BoxesItem[], i : number, j : number, lineWidth: number) : number{ + if (this.lineCostStorage[i] !== undefined && this.lineCostStorage[i][j] !== undefined){ + return this.lineCostStorage[i][j]; + } + + if (!this.isBreakPoint(items[j])){ + this.lineCostStorage[i][j] = Infinity; return Infinity; }else{ - return (lineWidth - tmpItemWidth)**3.0; + var tmpItemWidth = 0; + for (var k = i; k lineWidth){ + this.lineCostStorage[i][j] = Infinity; + return Infinity; + }else{ + let returnValue = (lineWidth - tmpItemWidth)**3.0; + this.lineCostStorage[i][j] = returnValue; + return returnValue; + } } + } } \ No newline at end of file