]> git.kianting.info Git - clo/blob - src/libclo/breakLines.js
27513f3c0f8e901060c66fc4f7643745c6a125fb
[clo] / src / libclo / breakLines.js
1 "use strict";
2 Object.defineProperty(exports, "__esModule", { value: true });
3 exports.totalCost = void 0;
4 /**
5 * Algorithms in LATEX language
6 TotalCost(i) = min_{j}~TotalCost(j) + LineCost(j, i)~~~~j=0, 1, ..., i-1
7
8 LineCost(j, i)= \begin{cases}
9 \infty ~~~ if~~LineWidth - \sum_{k=j+1}^{i-1} OrigWidth(item[k]) - newLineWidth(item[i]) < 0 \\
10 \infty~~if~~NOT~~breakable(item[i]) \\
11 (LineWidth - \sum_{k=j+1}^{i-1} OrigWidth(item[k]) - newLineWidth(item[i]))^3 ~~elsewhere
12 \end{cases} */
13 /**check if a boeitem is BreakPoint Type */
14 function isBreakPoint(item) {
15 return item.newLined !== undefined;
16 }
17 /**check if a boeitem is BreakPoint Type */
18 function isHGlue(item) {
19 return item.stretchFactor !== undefined;
20 }
21 /** measuring original advance width */
22 function origWidth(item) {
23 if (isBreakPoint(item)) {
24 console.log(item);
25 return origWidth(item.original);
26 }
27 else if (Array.isArray(item)) {
28 return item.map((x) => origWidth(x))
29 .reduce((acc, current) => acc + current, 0.0);
30 }
31 else if (isHGlue(item)) {
32 return 0.0;
33 }
34 else {
35 return item.width;
36 }
37 }
38 /** measuring new-line triggered advance width */
39 function newLineWidth(item) {
40 if (isBreakPoint(item)) {
41 return origWidth(item.newLined);
42 }
43 else {
44 // impossible to make a new line
45 return Infinity;
46 }
47 }
48 let lineCostStorage = new Object();
49 /**
50 * check the total cost item[0..j].
51 * @param items
52 * @param i
53 * @param lineWidth
54 */
55 function totalCost(items, j, lineWidth) {
56 if (j in lineCostStorage) {
57 return lineCostStorage[j];
58 }
59 var returnCost = Infinity;
60 for (var i = -1; i <= j; i++) {
61 // lineCost
62 let lCost = lineCost(items, i, j, lineWidth);
63 if (returnCost > lCost) {
64 returnCost = lCost;
65 }
66 }
67 lineCostStorage[j] = returnCost;
68 return returnCost;
69 }
70 exports.totalCost = totalCost;
71 /**
72 * check the line cost of a line containing items[i+1..j]
73 * @param items items of box
74 * @param i beginning (excluded)
75 * @param j end of the line
76 * @param lineWidth line width
77 */
78 function lineCost(items, i, j, lineWidth) {
79 if (!isBreakPoint(items[j])) {
80 return Infinity;
81 }
82 else {
83 var tmpItemWidth = 0;
84 for (var k = i + 1; k < j; k++) {
85 tmpItemWidth += origWidth(items[k]);
86 }
87 tmpItemWidth += newLineWidth(items[j]);
88 if (tmpItemWidth > lineWidth) {
89 return Infinity;
90 }
91 else {
92 return (lineWidth - tmpItemWidth) ** 3.0;
93 }
94 }
95 }