X-Git-Url: https://git.kianting.info/?a=blobdiff_plain;f=src%2Flibclo%2FbreakLines.js;h=14a7836178190690f79c86f462067fa58022a4aa;hb=9548c51651d010ec8fd8fbf0213a2d7c60e051ce;hp=b94d123680be05e16ef7ff3f6650fb4cdc82acba;hpb=134485a6d637ce5b422098c08bccb274c73bca86;p=clo diff --git a/src/libclo/breakLines.js b/src/libclo/breakLines.js index b94d123..14a7836 100644 --- a/src/libclo/breakLines.js +++ b/src/libclo/breakLines.js @@ -35,9 +35,9 @@ class BreakLineAlgorithm { } } segmentedNodes(items, lineWidth) { - this.totalCost(items, lineWidth); + let lineWidthFixed = lineWidth; + this.totalCost(items, lineWidthFixed); let nodeList = this.generateBreakLineNodeList(); - console.log("~~~", nodeList); let res = []; let low = -1; let up = nodeList[0]; @@ -72,16 +72,22 @@ class BreakLineAlgorithm { * check all the total cost of paragraphes of the segnemt */ totalCost(items, lineWidth) { + let lineWidthFixed = lineWidth * 0.75; let itemsLength = items.length; this.lineCostStorage = Array(itemsLength); this.prevNodes = Array(itemsLength).fill(null); for (var i = 0; i < itemsLength; i++) { - this.lineCostStorage[i] = Array(itemsLength).fill(undefined); + this.lineCostStorage[i] = Array(itemsLength).fill(null); + } + this.totalCostAuxStorage = Array(itemsLength).fill(null); + let a = Infinity; + for (var k = itemsLength - 2; this.lineCost(items, k + 1, itemsLength - 1, lineWidthFixed) < Infinity; k--) { + let tmp = this.totalCostAux(items, k, lineWidthFixed); + if (a > tmp) { + this.prevNodes[itemsLength - 1] = k; + a = tmp; + } } - this.totalCostAuxStorage = Array(itemsLength).fill(undefined); - console.log("===", itemsLength); - let a = this.totalCostAux(items, itemsLength - 1, lineWidth); - console.log(this.lineCostStorage); return a; } /** @@ -91,18 +97,18 @@ class BreakLineAlgorithm { * @param lineWidth */ totalCostAux(items, j, lineWidth) { - if (this.totalCostAuxStorage[j] !== undefined) { + if (this.totalCostAuxStorage[j] !== null) { return this.totalCostAuxStorage[j]; } let rawLineCost = this.lineCost(items, 0, j, lineWidth); if (rawLineCost != Infinity) { - this.totalCostAuxStorage[j] = rawLineCost; - return rawLineCost; + this.totalCostAuxStorage[j] = rawLineCost ** 3.0; + return rawLineCost ** 3.0; } else { var returnCost = Infinity; for (var k = 0; k < j; k++) { - let tmp = this.totalCostAux(items, k, lineWidth) + this.lineCost(items, k + 1, j, lineWidth); + let tmp = this.totalCostAux(items, k, lineWidth) + this.lineCost(items, k + 1, j, lineWidth) ** 3.0; if (returnCost > tmp) { this.prevNodes[j] = k; returnCost = tmp; @@ -111,7 +117,6 @@ class BreakLineAlgorithm { this.totalCostAuxStorage[j] = returnCost; return returnCost; } - return returnCost; } /** * check the line cost of a line containing items[i..j] @@ -121,7 +126,8 @@ class BreakLineAlgorithm { * @param lineWidth line width */ lineCost(items, i, j, lineWidth) { - if (this.lineCostStorage[i] !== undefined && this.lineCostStorage[i][j] !== undefined) { + if (this.lineCostStorage[i][j] !== null) { + console.log("AA"); return this.lineCostStorage[i][j]; } if (!this.isBreakPoint(items[j])) { @@ -139,7 +145,7 @@ class BreakLineAlgorithm { return Infinity; } else { - let returnValue = (lineWidth - tmpItemWidth) ** 3.0; + let returnValue = (lineWidth - tmpItemWidth); this.lineCostStorage[i][j] = returnValue; return returnValue; }