]>
git.kianting.info Git - clo/blob - breakLines.js
14a7836178190690f79c86f462067fa58022a4aa
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
;
39 this.totalCost(items
, lineWidthFixed
);
40 let nodeList
= this.generateBreakLineNodeList();
44 for (var i
= 0; i
< nodeList
.length
; i
++) {
45 res
.push(items
.slice(low
+ 1, up
+ 1));
51 /**genrate the list of point of breaking line. it returns a correct list ascending*/
52 generateBreakLineNodeList() {
54 var pointer
= this.prevNodes
.length
- 1;
55 while (this.prevNodes
[pointer
] !== undefined) {
57 pointer
= this.prevNodes
[pointer
];
61 /** measuring new-line triggered advance width */
63 if (this.isBreakPoint(item
)) {
64 return this.origWidth(item
.newLined
);
67 // impossible to make a new line
72 * check all the total cost of paragraphes of the segnemt
74 totalCost(items
, lineWidth
) {
75 let lineWidthFixed
= lineWidth
* 0.75;
76 let itemsLength
= items
.length
;
77 this.lineCostStorage
= Array(itemsLength
);
78 this.prevNodes
= Array(itemsLength
).fill(null);
79 for (var i
= 0; i
< itemsLength
; i
++) {
80 this.lineCostStorage
[i
] = Array(itemsLength
).fill(null);
82 this.totalCostAuxStorage
= Array(itemsLength
).fill(null);
84 for (var k
= itemsLength
- 2; this.lineCost(items
, k
+ 1, itemsLength
- 1, lineWidthFixed
) < Infinity
; k
--) {
85 let tmp
= this.totalCostAux(items
, k
, lineWidthFixed
);
87 this.prevNodes
[itemsLength
- 1] = k
;
94 * check the total cost item[0..j].
99 totalCostAux(items
, j
, lineWidth
) {
100 if (this.totalCostAuxStorage
[j
] !== null) {
101 return this.totalCostAuxStorage
[j
];
103 let rawLineCost
= this.lineCost(items
, 0, j
, lineWidth
);
104 if (rawLineCost
!= Infinity
) {
105 this.totalCostAuxStorage
[j
] = rawLineCost
** 3.0;
106 return rawLineCost
** 3.0;
109 var returnCost
= Infinity
;
110 for (var k
= 0; k
< j
; k
++) {
111 let tmp
= this.totalCostAux(items
, k
, lineWidth
) + this.lineCost(items
, k
+ 1, j
, lineWidth
) ** 3.0;
112 if (returnCost
> tmp
) {
113 this.prevNodes
[j
] = k
;
117 this.totalCostAuxStorage
[j
] = returnCost
;
122 * check the line cost of a line containing items[i..j]
123 * @param items items of box
124 * @param i beginning (excluded)
125 * @param j end of the line
126 * @param lineWidth line width
128 lineCost(items
, i
, j
, lineWidth
) {
129 if (this.lineCostStorage
[i
][j
] !== null) {
131 return this.lineCostStorage
[i
][j
];
133 if (!this.isBreakPoint(items
[j
])) {
134 this.lineCostStorage
[i
][j
] = Infinity
;
138 var tmpItemWidth
= 0;
139 for (var k
= i
; k
< j
; k
++) {
140 tmpItemWidth
+= this.origWidth(items
[k
]);
142 tmpItemWidth
+= this.newLineWidth(items
[j
]);
143 if (tmpItemWidth
> lineWidth
) {
144 this.lineCostStorage
[i
][j
] = Infinity
;
148 let returnValue
= (lineWidth
- tmpItemWidth
);
149 this.lineCostStorage
[i
][j
] = returnValue
;
155 exports
.BreakLineAlgorithm
= BreakLineAlgorithm
;