]>
git.kianting.info Git - clo/blob - libclo/breakLines.ts
2 * Algorithms and functions for LineBreaking
4 import { join
} from
"path";
5 import {BreakPoint
, BoxesItem
, HGlue
} from
"./index.js";
6 import { listenerCount
} from
"process";
7 import { unwatchFile
} from
"fs";
9 * Algorithms in LATEX-like language
12 export class BreakLineAlgorithm
{
14 lineCostStorage
: number[][];
15 totalCostAuxStorage
: number[];
22 this.totalCostAuxStorage
= [];
23 this.lineCostStorage
= [[]];
26 /**check if a boeitem is BreakPoint Type */
27 isBreakPoint (item
: any) : item
is BreakPoint
{
28 return (item
as BreakPoint
).newLined
!== undefined;
31 /**check if a boeitem is HGlue Type */
32 isHGlue (item
: any) : item
is HGlue
{
33 return (item
as HGlue
).stretchFactor
!== undefined;
37 /** measuring original advance width */
38 origWidth(item
: BoxesItem
) : number{
39 if (this.isBreakPoint(item
)){
40 return this.origWidth(item
.original
);
41 }else if(Array.isArray(item
)){
42 return item
.map((x
)=>this.origWidth(x
))
43 .reduce((acc
, current
) => acc
+ current
,
45 }else if(this.isHGlue(item
)){
53 segmentedNodes(items
: BoxesItem
[], lineWidth
: number) : BoxesItem
[][]{
54 this.totalCost(items
,lineWidth
);
55 let nodeList
= this.generateBreakLineNodeList();
56 console
.log("~~~", nodeList
);
61 for(var i
=0; i
<nodeList
.length
; i
++){
62 res
.push(items
.slice(low
+1, up
+1));
70 /**genrate the list of point of breaking line. it returns a correct list ascending*/
71 generateBreakLineNodeList() : number[]{
72 let res
: number[] = [];
73 var pointer
= this.prevNodes
.length
-1;
74 while (this.prevNodes
[pointer
] !== undefined){
76 pointer
= this.prevNodes
[pointer
];
81 /** measuring new-line triggered advance width */
82 newLineWidth(item
: BoxesItem
) : number{
83 if (this.isBreakPoint(item
)){
84 return this.origWidth(item
.newLined
);
86 // impossible to make a new line
91 * check all the total cost of paragraphes of the segnemt
93 totalCost(items
: BoxesItem
[], lineWidth
: number) : number{
95 let itemsLength
= items
.length
;
96 this.lineCostStorage
= Array(itemsLength
);
97 this.prevNodes
= Array(itemsLength
).fill(null);
100 for (var i
=0; i
<itemsLength
; i
++){
101 this.lineCostStorage
[i
] = Array(itemsLength
).fill(undefined);
104 this.totalCostAuxStorage
= Array(itemsLength
).fill(undefined);
105 console
.log("===", itemsLength
);
106 let a
= this.totalCostAux(items
, itemsLength
-1, lineWidth
);
107 console
.log(this.lineCostStorage
);
113 * check the total cost item[0..j].
118 totalCostAux(items
: BoxesItem
[], j
: number, lineWidth
: number): number{
120 if (this.totalCostAuxStorage
[j
] !== undefined){
121 return this.totalCostAuxStorage
[j
];
124 let rawLineCost
= this.lineCost(items
, 0, j
, lineWidth
);
125 if (rawLineCost
!= Infinity){
126 this.totalCostAuxStorage
[j
] = rawLineCost
;
129 var returnCost
= Infinity;
130 for(var k
=0; k
<j
; k
++){
131 let tmp
= this.totalCostAux(items
, k
, lineWidth
) + this.lineCost(items
, k
+1,j
, lineWidth
);
132 if (returnCost
> tmp
){
133 this.prevNodes
[j
] = k
;
137 this.totalCostAuxStorage
[j
] = returnCost
;
150 * check the line cost of a line containing items[i..j]
151 * @param items items of box
152 * @param i beginning (excluded)
153 * @param j end of the line
154 * @param lineWidth line width
156 lineCost(items
: BoxesItem
[], i
: number, j
: number, lineWidth
: number) : number{
157 if (this.lineCostStorage
[i
] !== undefined && this.lineCostStorage
[i
][j
] !== undefined){
158 return this.lineCostStorage
[i
][j
];
161 if (!this.isBreakPoint(items
[j
])){
162 this.lineCostStorage
[i
][j
] = Infinity;
165 var tmpItemWidth
= 0;
166 for (var k
= i
; k
<j
; k
++){
167 tmpItemWidth
+= this.origWidth(items
[k
]);
170 tmpItemWidth
+= this.newLineWidth(items
[j
]);
172 if (tmpItemWidth
> lineWidth
){
173 this.lineCostStorage
[i
][j
] = Infinity;
176 let returnValue
= (lineWidth
- tmpItemWidth
)**3.0;
177 this.lineCostStorage
[i
][j
] = returnValue
;