]>
git.kianting.info Git - clo/blob - src/libclo/breakLines.js
2 Object
.defineProperty(exports
, "__esModule", { value
: true });
3 exports
.totalCost
= void 0;
5 * Algorithms in LATEX language
6 TotalCost(i) = min_{j}~TotalCost(j) + LineCost(j, i)~~~~j=0, 1, ..., i-1
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
13 /**check if a boeitem is BreakPoint Type */
14 function isBreakPoint(item
) {
15 return item
.newLined
!== undefined;
17 /**check if a boeitem is BreakPoint Type */
18 function isHGlue(item
) {
19 return item
.stretchFactor
!== undefined;
21 /** measuring original advance width */
22 function origWidth(item
) {
23 if (isBreakPoint(item
)) {
25 return origWidth(item
.original
);
27 else if (Array
.isArray(item
)) {
28 return item
.map((x
) => origWidth(x
))
29 .reduce((acc
, current
) => acc
+ current
, 0.0);
31 else if (isHGlue(item
)) {
38 /** measuring new-line triggered advance width */
39 function newLineWidth(item
) {
40 if (isBreakPoint(item
)) {
41 return origWidth(item
.newLined
);
44 // impossible to make a new line
48 let lineCostStorage
= new Object();
50 * check the total cost item[0..j].
55 function totalCost(items
, j
, lineWidth
) {
56 if (j
in lineCostStorage
) {
57 return lineCostStorage
[j
];
59 var returnCost
= Infinity
;
60 for (var i
= -1; i
<= j
; i
++) {
62 let lCost
= lineCost(items
, i
, j
, lineWidth
);
63 if (returnCost
> lCost
) {
67 lineCostStorage
[j
] = returnCost
;
70 exports
.totalCost
= totalCost
;
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
78 function lineCost(items
, i
, j
, lineWidth
) {
79 if (!isBreakPoint(items
[j
])) {
84 for (var k
= i
+ 1; k
< j
; k
++) {
85 tmpItemWidth
+= origWidth(items
[k
]);
87 tmpItemWidth
+= newLineWidth(items
[j
]);
88 if (tmpItemWidth
> lineWidth
) {
92 return (lineWidth
- tmpItemWidth
) ** 3.0;