]>
git.kianting.info Git - clo/blob - src/libclo/breakLines.js
cdd4f6e99d3d25f1ee90cb24cf4bfa4ba2d50f2b
2 var __awaiter
= (this && this.__awaiter
) || function (thisArg
, _arguments
, P
, generator
) {
3 function adopt(value
) { return value
instanceof P
? value
: new P(function (resolve
) { resolve(value
); }); }
4 return new (P
|| (P
= Promise
))(function (resolve
, reject
) {
5 function fulfilled(value
) { try { step(generator
.next(value
)); } catch (e
) { reject(e
); } }
6 function rejected(value
) { try { step(generator
["throw"](value
)); } catch (e
) { reject(e
); } }
7 function step(result
) { result
.done
? resolve(result
.value
) : adopt(result
.value
).then(fulfilled
, rejected
); }
8 step((generator
= generator
.apply(thisArg
, _arguments
|| [])).next());
11 Object
.defineProperty(exports
, "__esModule", { value
: true });
12 exports
.BreakLineAlgorithm
= void 0;
14 * Algorithms in LATEX-like language
16 class BreakLineAlgorithm
{
19 this.totalCostAuxStorage
= [];
20 this.lineCostStorage
= [[]];
22 /**check if a boeitem is BreakPoint Type */
24 return item
.newLined
!== undefined;
26 /**check if a boeitem is HGlue Type */
28 return item
.stretchFactor
!== undefined;
30 /** measuring original advance width */
32 if (this.isBreakPoint(item
)) {
33 return this.origWidth(item
.original
);
35 else if (Array
.isArray(item
)) {
36 return item
.map((x
) => this.origWidth(x
))
37 .reduce((acc
, current
) => acc
+ current
, 0.0);
39 else if (this.isHGlue(item
)) {
46 segmentedNodes(items
, lineWidth
) {
47 let lineWidthFixed
= lineWidth
;
48 this.totalCost(items
, lineWidthFixed
);
49 let nodeList
= this.generateBreakLineNodeList();
53 for (var i
= 0; i
< nodeList
.length
; i
++) {
54 res
.push(items
.slice(low
+ 1, up
+ 1));
60 /**genrate the list of point of breaking line. it returns a correct list ascending*/
61 generateBreakLineNodeList() {
63 var pointer
= this.prevNodes
.length
- 1;
64 while (this.prevNodes
[pointer
] !== undefined) {
66 pointer
= this.prevNodes
[pointer
];
70 /** measuring new-line triggered advance width */
72 if (this.isBreakPoint(item
)) {
73 return this.origWidth(item
.newLined
);
76 // impossible to make a new line
81 * check all the total cost of paragraphes of the segnemt
83 totalCost(items
, lineWidth
) {
84 return __awaiter(this, void 0, void 0, function* () {
85 let lineWidthFixed
= lineWidth
* 0.75;
86 let itemsLength
= items
.length
;
87 this.lineCostStorage
= Array(itemsLength
);
88 this.prevNodes
= Array(itemsLength
).fill(null);
89 for (var i
= 0; i
< itemsLength
; i
++) {
90 this.lineCostStorage
[i
] = Array(itemsLength
).fill(null);
92 this.totalCostAuxStorage
= Array(itemsLength
).fill(null);
94 for (var k
= itemsLength
- 2; (yield this.lineCost(items
, k
+ 1, itemsLength
- 1, lineWidthFixed
)) < Infinity
; k
--) {
95 let tmp
= yield this.totalCostAux(items
, k
, lineWidthFixed
);
97 this.prevNodes
[itemsLength
- 1] = k
;
101 console
.log("~~~", lineWidth
);
102 console
.log(items
[itemsLength
- 2]);
107 * check the total cost item[0..j].
112 totalCostAux(items
, j
, lineWidth
) {
113 return __awaiter(this, void 0, void 0, function* () {
114 if (this.totalCostAuxStorage
[j
] !== null) {
115 return this.totalCostAuxStorage
[j
];
117 let rawLineCost
= yield this.lineCost(items
, 0, j
, lineWidth
);
118 if (rawLineCost
!= Infinity
) {
119 this.totalCostAuxStorage
[j
] = rawLineCost
** 3.0;
120 return rawLineCost
** 3.0;
123 var returnCost
= Infinity
;
124 for (var k
= 0; k
< j
; k
++) {
125 let tmp1
= yield Promise
.all([this.totalCostAux(items
, k
, lineWidth
), this.lineCost(items
, k
+ 1, j
, lineWidth
)]);
126 let tmp
= tmp1
[0] + tmp1
[1] ** 3;
127 if (returnCost
> tmp
) {
128 this.prevNodes
[j
] = k
;
132 this.totalCostAuxStorage
[j
] = returnCost
;
138 * check the line cost of a line containing items[i..j]
139 * @param items items of box
140 * @param i beginning (excluded)
141 * @param j end of the line
142 * @param lineWidth line width
144 lineCost(items
, i
, j
, lineWidth
) {
145 return __awaiter(this, void 0, void 0, function* () {
146 if (this.lineCostStorage
[i
] !== null && this.lineCostStorage
[i
][j
] !== null) {
147 return this.lineCostStorage
[i
][j
];
149 if (!this.isBreakPoint(items
[j
])) {
150 this.lineCostStorage
[i
][j
] = Infinity
;
154 var tmpItemWidth
= 0;
155 for (var k
= i
; k
< j
; k
++) {
156 tmpItemWidth
+= this.origWidth(items
[k
]);
158 tmpItemWidth
+= this.newLineWidth(items
[j
]);
159 if (tmpItemWidth
> lineWidth
) {
160 this.lineCostStorage
[i
][j
] = Infinity
;
164 let returnValue
= (lineWidth
- tmpItemWidth
);
165 this.lineCostStorage
[i
][j
] = returnValue
;
172 exports
.BreakLineAlgorithm
= BreakLineAlgorithm
;