]>
git.kianting.info Git - clo/blob - src/libclo/breakLines.js
9367d53395a09495884d65ef01a953f24955f66f
2 Object
.defineProperty(exports
, "__esModule", { value
: true });
3 exports
.BreakLineAlgorithm
= void 0;
5 * Algorithms in LATEX-like language
7 class BreakLineAlgorithm
{
10 this.totalCostAuxStorage
= [];
11 this.lineCostStorage
= [[]];
13 /**check if a boeitem is BreakPoint Type */
15 return item
.newLined
!== undefined;
17 /**check if a boeitem is HGlue Type */
19 return item
.stretchFactor
!== undefined;
21 /** measuring original advance width */
23 if (this.isBreakPoint(item
)) {
24 return this.origWidth(item
.original
);
26 else if (Array
.isArray(item
)) {
27 return item
.map((x
) => this.origWidth(x
))
28 .reduce((acc
, current
) => acc
+ current
, 0.0);
30 else if (this.isHGlue(item
)) {
37 segmentedNodes(items
, lineWidth
) {
38 let lineWidthFixed
= lineWidth
* 0.75;
39 this.totalCost(items
, lineWidthFixed
);
40 let nodeList
= this.generateBreakLineNodeList();
41 console
.log("~~~", nodeList
);
45 for (var i
= 0; i
< nodeList
.length
; i
++) {
46 res
.push(items
.slice(low
+ 1, up
+ 1));
50 console
.log("===", res
.length
);
53 /**genrate the list of point of breaking line. it returns a correct list ascending*/
54 generateBreakLineNodeList() {
56 var pointer
= this.prevNodes
.length
- 1;
57 while (this.prevNodes
[pointer
] !== undefined) {
59 pointer
= this.prevNodes
[pointer
];
63 /** measuring new-line triggered advance width */
65 if (this.isBreakPoint(item
)) {
66 return this.origWidth(item
.newLined
);
69 // impossible to make a new line
74 * check all the total cost of paragraphes of the segnemt
76 totalCost(items
, lineWidth
) {
77 let itemsLength
= items
.length
;
78 this.lineCostStorage
= Array(itemsLength
);
79 this.prevNodes
= Array(itemsLength
).fill(null);
80 for (var i
= 0; i
< itemsLength
; i
++) {
81 this.lineCostStorage
[i
] = Array(itemsLength
).fill(null);
83 this.totalCostAuxStorage
= Array(itemsLength
).fill(null);
84 let a
= this.totalCostAux(items
, itemsLength
- 1, lineWidth
);
88 * check the total cost item[0..j].
93 totalCostAux(items
, j
, lineWidth
) {
94 if (this.totalCostAuxStorage
[j
] !== null) {
95 return this.totalCostAuxStorage
[j
];
97 let rawLineCost
= this.lineCost(items
, 0, j
, lineWidth
);
98 if (rawLineCost
!= Infinity
) {
99 this.totalCostAuxStorage
[j
] = rawLineCost
;
103 var returnCost
= Infinity
;
104 for (var k
= 0; k
< j
; k
++) {
105 let tmp
= this.totalCostAux(items
, k
, lineWidth
) + this.lineCost(items
, k
+ 1, j
, lineWidth
);
106 if (returnCost
> tmp
) {
107 this.prevNodes
[j
] = k
;
111 this.totalCostAuxStorage
[j
] = returnCost
;
117 * check the line cost of a line containing items[i..j]
118 * @param items items of box
119 * @param i beginning (excluded)
120 * @param j end of the line
121 * @param lineWidth line width
123 lineCost(items
, i
, j
, lineWidth
) {
124 if (this.lineCostStorage
[i
] !== null && this.lineCostStorage
[i
][j
] !== null) {
125 return this.lineCostStorage
[i
][j
];
127 if (!this.isBreakPoint(items
[j
])) {
128 this.lineCostStorage
[i
][j
] = Infinity
;
132 var tmpItemWidth
= 0;
133 for (var k
= i
; k
< j
; k
++) {
134 tmpItemWidth
+= this.origWidth(items
[k
]);
136 tmpItemWidth
+= this.newLineWidth(items
[j
]);
137 if (tmpItemWidth
> lineWidth
) {
138 this.lineCostStorage
[i
][j
] = Infinity
;
142 let returnValue
= (lineWidth
- tmpItemWidth
) ** 3.0;
143 this.lineCostStorage
[i
][j
] = returnValue
;
149 exports
.BreakLineAlgorithm
= BreakLineAlgorithm
;