@quiredoc/layout 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +18 -2
- package/dist/index.js +2744 -0
- package/dist/index.js.map +1 -0
- package/dist/types/document.d.ts +498 -0
- package/dist/types/index.d.ts +9 -0
- package/dist/types/paragraph.d.ts +246 -0
- package/dist/types/sections.d.ts +113 -0
- package/dist/types/table.d.ts +124 -0
- package/package.json +38 -4
package/dist/index.js
ADDED
|
@@ -0,0 +1,2744 @@
|
|
|
1
|
+
// packages/layout/src/paragraph.ts
|
|
2
|
+
var PLAIN_STYLE = Object.freeze({
|
|
3
|
+
face: 0,
|
|
4
|
+
scale: 1,
|
|
5
|
+
line: 1
|
|
6
|
+
});
|
|
7
|
+
var NO_KOREAN = {
|
|
8
|
+
char: false,
|
|
9
|
+
hang: false,
|
|
10
|
+
squeeze: 0
|
|
11
|
+
};
|
|
12
|
+
/** 개체 글자 */
|
|
13
|
+
var OBJ_CHAR = "";
|
|
14
|
+
/** 목록 번호를 잰다 (셰이퍼가 있으면 조각 하나로 셰이핑) */
|
|
15
|
+
function measureMarker(m, format) {
|
|
16
|
+
const mk = format.marker;
|
|
17
|
+
if (!mk || !mk.text) return null;
|
|
18
|
+
const r = format.style(mk.id);
|
|
19
|
+
let w = 0;
|
|
20
|
+
let fonts = null;
|
|
21
|
+
if (m.advances) {
|
|
22
|
+
const a = r.face === 0 ? m.advances(mk.text) : m.advances(mk.text, r.face);
|
|
23
|
+
for (let i = 0; i < a.length; i++) w += a[i];
|
|
24
|
+
fonts = (r.face === 0 ? m.fontsOf?.(mk.text) : m.fontsOf?.(mk.text, r.face)) ?? null;
|
|
25
|
+
} else for (const g of segmenter.segment(mk.text)) w += r.face === 0 ? m.width(g.segment) : m.width(g.segment, r.face);
|
|
26
|
+
w *= r.scale;
|
|
27
|
+
const gap = (r.face === 0 ? m.width(" ") : m.width(" ", r.face)) * r.scale;
|
|
28
|
+
const shift = Math.max(0, w + gap - mk.hanging);
|
|
29
|
+
return {
|
|
30
|
+
text: mk.text,
|
|
31
|
+
id: mk.id,
|
|
32
|
+
width: w,
|
|
33
|
+
x: -mk.hanging,
|
|
34
|
+
shift,
|
|
35
|
+
fonts
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
/** 표시 텍스트의 코드포인트 구간 [a, b) */
|
|
39
|
+
function cpSlice(pl, a, b) {
|
|
40
|
+
if (!pl.u16) return pl.text.slice(a, b);
|
|
41
|
+
return pl.text.slice(pl.u16[a], pl.u16[b]);
|
|
42
|
+
}
|
|
43
|
+
var segmenter = new Intl.Segmenter("ko", { granularity: "grapheme" });
|
|
44
|
+
/**
|
|
45
|
+
* 모든 글자가 그 자체로 grapheme 하나인 텍스트 (결합 문자·서로게이트·ZWJ·이형 선택자 없음).
|
|
46
|
+
* 이 범위만 쓴 문단은 Intl.Segmenter를 건너뛴다.
|
|
47
|
+
* 탭, ASCII, Latin-1, 일반 문장부호(ZW* 제외), CJK 기호, 한글 호환 자모, 한글 음절, CJK 통합 한자, 전각 형태
|
|
48
|
+
*/
|
|
49
|
+
var SIMPLE_TEXT = /^[\t\x20-\x7e -ÿ‐-‧‰-⁞ -〿-가-힣一-鿿!-]*$/;
|
|
50
|
+
/**
|
|
51
|
+
* 금칙 (12 §9.1, O-30): 줄 앞에 올 수 없는 문자. Word·한글 프로그램의 한국어 기본 목록을 참고한 초안 + 한글 조판 문장부호
|
|
52
|
+
*/
|
|
53
|
+
var NO_LINE_START = new Set(Array.from("!%),.:;?]}¢°’”‰′″℃、。〉》」』】〕〗〙〛ᅠ…‥·・,.>"));
|
|
54
|
+
/** 줄 끝에 올 수 없는 문자 (여는 괄호·따옴표) */
|
|
55
|
+
var NO_LINE_END = new Set(Array.from("$([{£¥‘“〈《「『【〔〖〘〚$"));
|
|
56
|
+
/** 줄 끝 여백에 걸칠 수 있는 문장부호 (12 §9.3) */
|
|
57
|
+
var HANGABLE = new Set(Array.from(",.、。,."));
|
|
58
|
+
/** 한글 음절·자모, 한자, 가나 (글자 단위로 끊을 수 있는 글자) */
|
|
59
|
+
function isCJK(ch) {
|
|
60
|
+
const c = ch.codePointAt(0);
|
|
61
|
+
return c >= 44032 && c <= 55203 || c >= 4352 && c <= 4607 || c >= 12592 && c <= 12687 || c >= 19968 && c <= 40959 || c >= 13312 && c <= 19903 || c >= 12352 && c <= 12543;
|
|
62
|
+
}
|
|
63
|
+
/** a 뒤, b 앞에서 줄을 바꿀 수 있는가 (공백 뒤·개체 앞뒤, 글자 단위면 한글·한자 사이). 금칙이면 안 된다 */
|
|
64
|
+
function canBreakBetween(a, b, opts) {
|
|
65
|
+
if (isSpace(b)) return false;
|
|
66
|
+
if (NO_LINE_START.has(b) || NO_LINE_END.has(a)) return false;
|
|
67
|
+
if (isSpace(a) || a === "" || b === "") return true;
|
|
68
|
+
return opts.char && (isCJK(a) || isCJK(b));
|
|
69
|
+
}
|
|
70
|
+
function isSpace(ch) {
|
|
71
|
+
return ch === " " || ch === " " || ch === " ";
|
|
72
|
+
}
|
|
73
|
+
function layoutParagraph(text, width, measurer, preedit = null, format = null) {
|
|
74
|
+
let display = text;
|
|
75
|
+
let pre = null;
|
|
76
|
+
let fmt = format?.fmt ?? null;
|
|
77
|
+
if (preedit && preedit.text) {
|
|
78
|
+
const base = Array.from(text);
|
|
79
|
+
const plen = Array.from(preedit.text).length;
|
|
80
|
+
display = base.slice(0, preedit.at).join("") + preedit.text + base.slice(preedit.at).join("");
|
|
81
|
+
pre = [preedit.at, preedit.at + plen];
|
|
82
|
+
const id = preedit.fmt ?? (fmt ? fmt[preedit.at - 1] ?? fmt[preedit.at] ?? 0 : base.length === 0 ? format?.mark ?? 0 : 0);
|
|
83
|
+
if (fmt || id !== 0) {
|
|
84
|
+
const f = new Uint16Array(base.length + plen);
|
|
85
|
+
if (fmt) {
|
|
86
|
+
f.set(fmt.subarray(0, preedit.at));
|
|
87
|
+
f.set(fmt.subarray(preedit.at), preedit.at + plen);
|
|
88
|
+
}
|
|
89
|
+
f.fill(id, preedit.at, preedit.at + plen);
|
|
90
|
+
fmt = f;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
const box = format?.box ?? null;
|
|
94
|
+
const marker = format ? measureMarker(measurer, format) : null;
|
|
95
|
+
const restW = box ? Math.max(1, width - box.left - box.right) : width;
|
|
96
|
+
const firstW = box ? Math.max(1, restW - box.first - (marker?.shift ?? 0)) : width;
|
|
97
|
+
let objs = null;
|
|
98
|
+
if (format?.objs && format.objs.size) {
|
|
99
|
+
objs = /* @__PURE__ */ new Map();
|
|
100
|
+
const shift = pre ? pre[1] - pre[0] : 0;
|
|
101
|
+
for (const [k, o] of format.objs) {
|
|
102
|
+
const w = Math.min(o.w, restW);
|
|
103
|
+
objs.set(pre && k >= pre[0] ? k + shift : k, {
|
|
104
|
+
w,
|
|
105
|
+
h: o.w > 0 ? o.h * w / o.w : o.h
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
let floats = null;
|
|
110
|
+
if (format?.floats && format.floats.size) {
|
|
111
|
+
floats = /* @__PURE__ */ new Map();
|
|
112
|
+
const shift = pre ? pre[1] - pre[0] : 0;
|
|
113
|
+
for (const [k, f] of format.floats) floats.set(pre && k >= pre[0] ? k + shift : k, f);
|
|
114
|
+
objs ??= /* @__PURE__ */ new Map();
|
|
115
|
+
for (const k of floats.keys()) objs.set(k, {
|
|
116
|
+
w: 0,
|
|
117
|
+
h: 0
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
let notes = null;
|
|
121
|
+
if (format?.notes && format.notes.size) {
|
|
122
|
+
const shift = pre ? pre[1] - pre[0] : 0;
|
|
123
|
+
notes = [...format.notes].map(([k, n]) => ({
|
|
124
|
+
k: pre && k >= pre[0] ? k + shift : k,
|
|
125
|
+
id: n.id,
|
|
126
|
+
lines: n.lines,
|
|
127
|
+
total: n.lines.reduce((a, b) => a + b, 0)
|
|
128
|
+
})).sort((a, b) => a.k - b.k);
|
|
129
|
+
}
|
|
130
|
+
const style = format?.style;
|
|
131
|
+
const dfmt = fmt;
|
|
132
|
+
const st = dfmt && style ? (k) => style(dfmt[k]) : null;
|
|
133
|
+
let n = 0;
|
|
134
|
+
let hasAstral = false;
|
|
135
|
+
for (let i = 0; i < display.length; i++) {
|
|
136
|
+
const c = display.charCodeAt(i);
|
|
137
|
+
if (c >= 55296 && c <= 56319 && i + 1 < display.length) {
|
|
138
|
+
hasAstral = true;
|
|
139
|
+
i++;
|
|
140
|
+
}
|
|
141
|
+
n++;
|
|
142
|
+
}
|
|
143
|
+
let u16 = null;
|
|
144
|
+
if (hasAstral) {
|
|
145
|
+
u16 = new Uint32Array(n + 1);
|
|
146
|
+
let cp = 0;
|
|
147
|
+
for (let i = 0; i < display.length; i++) {
|
|
148
|
+
u16[cp++] = i;
|
|
149
|
+
const c = display.charCodeAt(i);
|
|
150
|
+
if (c >= 55296 && c <= 56319 && i + 1 < display.length) i++;
|
|
151
|
+
}
|
|
152
|
+
u16[n] = display.length;
|
|
153
|
+
}
|
|
154
|
+
const xs = new Float32Array(n + 1);
|
|
155
|
+
const stops = new Uint8Array(n + 1);
|
|
156
|
+
stops[n] = 1;
|
|
157
|
+
const gStart = [];
|
|
158
|
+
const gFirst = [];
|
|
159
|
+
let cp = 0;
|
|
160
|
+
let x = 0;
|
|
161
|
+
if (SIMPLE_TEXT.test(display)) {
|
|
162
|
+
for (let i = 0; i < display.length; i++) {
|
|
163
|
+
const ch = display[i];
|
|
164
|
+
gStart.push(i);
|
|
165
|
+
gFirst.push(ch);
|
|
166
|
+
stops[i] = 1;
|
|
167
|
+
xs[i] = x;
|
|
168
|
+
const ob = objs?.get(i);
|
|
169
|
+
if (ob) x += ob.w;
|
|
170
|
+
else if (!measurer.advances) {
|
|
171
|
+
if (st) {
|
|
172
|
+
const r = st(i);
|
|
173
|
+
x += measurer.width(ch, r.face) * r.scale;
|
|
174
|
+
} else x += measurer.width(ch);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
xs[n] = x;
|
|
178
|
+
const fonts = measurer.advances ? shapeXs(display, n, null, xs, measurer, fmt, st, objs) : null;
|
|
179
|
+
const lines = breakLines(n, xs, gStart, gFirst, restW, firstW, koreanOpts(box));
|
|
180
|
+
lineScales(lines, st, format);
|
|
181
|
+
if (objs) lineObjects(lines, objs);
|
|
182
|
+
const cpLine = st ? Float32Array.from({ length: n }, (_, k) => st(k).line) : null;
|
|
183
|
+
const markLine = format ? format.style(format.mark).line : 1;
|
|
184
|
+
return {
|
|
185
|
+
text: display,
|
|
186
|
+
n,
|
|
187
|
+
u16: null,
|
|
188
|
+
xs,
|
|
189
|
+
stops,
|
|
190
|
+
preedit: pre,
|
|
191
|
+
lines,
|
|
192
|
+
fonts,
|
|
193
|
+
fmt,
|
|
194
|
+
box,
|
|
195
|
+
widths: [firstW, restW],
|
|
196
|
+
marker,
|
|
197
|
+
objs: objs && floats ? withoutFloats(objs, floats) : objs,
|
|
198
|
+
floats,
|
|
199
|
+
notes,
|
|
200
|
+
cpLine,
|
|
201
|
+
markLine,
|
|
202
|
+
table: format?.table ?? null
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
for (const seg of segmenter.segment(display)) {
|
|
206
|
+
const s = seg.segment;
|
|
207
|
+
let len = 0;
|
|
208
|
+
for (let i = 0; i < s.length; i++) {
|
|
209
|
+
const c = s.charCodeAt(i);
|
|
210
|
+
if (c >= 55296 && c <= 56319 && i + 1 < s.length) i++;
|
|
211
|
+
len++;
|
|
212
|
+
}
|
|
213
|
+
gStart.push(cp);
|
|
214
|
+
gFirst.push(String.fromCodePoint(s.codePointAt(0)));
|
|
215
|
+
stops[cp] = 1;
|
|
216
|
+
for (let k = 0; k < len; k++) xs[cp + k] = x;
|
|
217
|
+
const ob = objs?.get(cp);
|
|
218
|
+
if (ob) x += ob.w;
|
|
219
|
+
else if (!measurer.advances) {
|
|
220
|
+
if (st) {
|
|
221
|
+
const r = st(cp);
|
|
222
|
+
x += measurer.width(s, r.face) * r.scale;
|
|
223
|
+
} else x += measurer.width(s);
|
|
224
|
+
}
|
|
225
|
+
cp += len;
|
|
226
|
+
}
|
|
227
|
+
xs[n] = x;
|
|
228
|
+
const fonts = measurer.advances ? shapeXs(display, n, u16, xs, measurer, fmt, st, objs) : null;
|
|
229
|
+
const lines = breakLines(n, xs, gStart, gFirst, restW, firstW, koreanOpts(box));
|
|
230
|
+
lineScales(lines, st, format);
|
|
231
|
+
if (objs) lineObjects(lines, objs);
|
|
232
|
+
const cpLine = st ? Float32Array.from({ length: n }, (_, k) => st(k).line) : null;
|
|
233
|
+
const markLine = format ? format.style(format.mark).line : 1;
|
|
234
|
+
return {
|
|
235
|
+
text: display,
|
|
236
|
+
n,
|
|
237
|
+
u16,
|
|
238
|
+
xs,
|
|
239
|
+
stops,
|
|
240
|
+
preedit: pre,
|
|
241
|
+
lines,
|
|
242
|
+
fonts,
|
|
243
|
+
fmt,
|
|
244
|
+
box,
|
|
245
|
+
widths: [firstW, restW],
|
|
246
|
+
marker,
|
|
247
|
+
objs: objs && floats ? withoutFloats(objs, floats) : objs,
|
|
248
|
+
floats,
|
|
249
|
+
notes,
|
|
250
|
+
cpLine,
|
|
251
|
+
markLine,
|
|
252
|
+
table: format?.table ?? null
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
/** 그리기용 인라인 개체 표: 앵커(폭 0)는 뺀다 */
|
|
256
|
+
function withoutFloats(objs, floats) {
|
|
257
|
+
const out = new Map([...objs].filter(([k]) => !floats.has(k)));
|
|
258
|
+
return out.size ? out : null;
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* 어울림 줄바꿈 (11 §4.2): start에서 시작해 폭 width에 들어가는 줄의 끝. breakLines와 같은 규칙 (어절·개체 앞뒤, 어절이 줄보다
|
|
262
|
+
* 길면 글자 단위 + 금칙). 적어도 grapheme 하나는 넣는다.
|
|
263
|
+
*/
|
|
264
|
+
function breakFrom(pl, start, width) {
|
|
265
|
+
const { n, stops } = pl;
|
|
266
|
+
if (start >= n) return n;
|
|
267
|
+
const starts = [];
|
|
268
|
+
const first = [];
|
|
269
|
+
for (let i = start; i < n; i++) if (stops[i] || i === start) {
|
|
270
|
+
starts.push(i);
|
|
271
|
+
first.push(cpSlice(pl, i, i + 1));
|
|
272
|
+
}
|
|
273
|
+
return breakLines(n, pl.xs, starts, first, width, width, koreanOpts(pl.box), start, 1)[0].end;
|
|
274
|
+
}
|
|
275
|
+
function koreanOpts(box) {
|
|
276
|
+
const k = box?.korean;
|
|
277
|
+
if (!k) return NO_KOREAN;
|
|
278
|
+
return {
|
|
279
|
+
char: !!k.char,
|
|
280
|
+
hang: !!k.hang,
|
|
281
|
+
squeeze: box?.align === "justify" ? k.squeeze ?? 0 : 0
|
|
282
|
+
};
|
|
283
|
+
}
|
|
284
|
+
/** 줄 끝에 걸친 문장부호 (12 §9.3): 줄 [start, end)의 마지막 공백 아닌 글자가 걸칠 수 있는 글자면 그 위치, 아니면 -1 */
|
|
285
|
+
function hangingAt(pl, start, end) {
|
|
286
|
+
if (!pl.box?.korean?.hang) return -1;
|
|
287
|
+
let e = end;
|
|
288
|
+
while (e > start && isSpace(cpSlice(pl, e - 1, e))) e--;
|
|
289
|
+
return e > start && HANGABLE.has(cpSlice(pl, e - 1, e)) ? e - 1 : -1;
|
|
290
|
+
}
|
|
291
|
+
/** 줄 [start, end)의 높이 배율과 개체 높이 (어울림 줄바꿈으로 새로 나눈 줄) */
|
|
292
|
+
function lineMetrics(pl, start, end, last) {
|
|
293
|
+
let scale = last ? pl.markLine : 0;
|
|
294
|
+
if (pl.cpLine) for (let k = start; k < end; k++) scale = Math.max(scale, pl.cpLine[k]);
|
|
295
|
+
else if (end > start) scale = Math.max(scale, 1);
|
|
296
|
+
let objH = 0;
|
|
297
|
+
if (pl.objs) {
|
|
298
|
+
for (const [k, o] of pl.objs) if (k >= start && k < end) objH = Math.max(objH, o.h);
|
|
299
|
+
}
|
|
300
|
+
return {
|
|
301
|
+
scale: scale > 0 ? scale : 1,
|
|
302
|
+
objH
|
|
303
|
+
};
|
|
304
|
+
}
|
|
305
|
+
/** 줄마다 개체의 가장 큰 높이 */
|
|
306
|
+
function lineObjects(lines, objs) {
|
|
307
|
+
for (const ln of lines) {
|
|
308
|
+
let h = 0;
|
|
309
|
+
for (const [k, o] of objs) if (k >= ln.start && k < ln.end) h = Math.max(h, o.h);
|
|
310
|
+
if (h > 0) ln.objH = h;
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
/** 줄마다 높이 배율: 줄 글자의 가장 큰 line 배율. 마지막 줄은 ¶도 센다 (빈 줄은 ¶만) */
|
|
314
|
+
function lineScales(lines, st, format) {
|
|
315
|
+
const markScale = format ? format.style(format.mark).line : 1;
|
|
316
|
+
if (!st && markScale === 1) return;
|
|
317
|
+
for (let l = 0; l < lines.length; l++) {
|
|
318
|
+
const ln = lines[l];
|
|
319
|
+
let m = l === lines.length - 1 ? markScale : 0;
|
|
320
|
+
if (st) for (let k = ln.start; k < ln.end; k++) m = Math.max(m, st(k).line);
|
|
321
|
+
else if (ln.end > ln.start) m = Math.max(m, 1);
|
|
322
|
+
ln.scale = m > 0 ? m : 1;
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
/**
|
|
326
|
+
* 셰이퍼로 xs를 채운다: 공백·탭이 아닌 글자가 이어진 조각마다 advances, 공백·탭은 width.
|
|
327
|
+
* (빠른 경로에서 width로 채운 값은 덮어쓴다. 빠른 경로의 width 호출은 grapheme 판정용이 아니므로 셰이퍼 쪽에서는 쓰지 않음)
|
|
328
|
+
*/
|
|
329
|
+
function shapeXs(display, n, u16, xs, m, fmt, st, objs = null) {
|
|
330
|
+
let fonts = null;
|
|
331
|
+
let x = 0;
|
|
332
|
+
let cp = 0;
|
|
333
|
+
const at = (k) => u16 ? u16[k] : k;
|
|
334
|
+
while (cp < n) {
|
|
335
|
+
const ch = display[at(cp)];
|
|
336
|
+
const ob = objs?.get(cp);
|
|
337
|
+
if (ob) {
|
|
338
|
+
xs[cp] = x;
|
|
339
|
+
x += ob.w;
|
|
340
|
+
cp++;
|
|
341
|
+
continue;
|
|
342
|
+
}
|
|
343
|
+
const r = st ? st(cp) : PLAIN_STYLE;
|
|
344
|
+
if (isSpace(ch)) {
|
|
345
|
+
xs[cp] = x;
|
|
346
|
+
x += r === PLAIN_STYLE ? m.width(ch) : m.width(ch, r.face) * r.scale;
|
|
347
|
+
cp++;
|
|
348
|
+
continue;
|
|
349
|
+
}
|
|
350
|
+
let end = cp + 1;
|
|
351
|
+
while (end < n) {
|
|
352
|
+
const c = display[at(end)];
|
|
353
|
+
if (isSpace(c) || c === "" || fmt && fmt[end] !== fmt[cp]) break;
|
|
354
|
+
end++;
|
|
355
|
+
}
|
|
356
|
+
const chunk = display.slice(at(cp), at(end));
|
|
357
|
+
const adv = r.face === 0 ? m.advances(chunk) : m.advances(chunk, r.face);
|
|
358
|
+
const f = (r.face === 0 ? m.fontsOf?.(chunk) : m.fontsOf?.(chunk, r.face)) ?? null;
|
|
359
|
+
const scale = r.scale;
|
|
360
|
+
for (let k = 0; k < end - cp; k++) {
|
|
361
|
+
xs[cp + k] = x;
|
|
362
|
+
x += scale === 1 ? adv[k] ?? 0 : (adv[k] ?? 0) * scale;
|
|
363
|
+
}
|
|
364
|
+
if (f) {
|
|
365
|
+
fonts ??= new Uint8Array(n);
|
|
366
|
+
fonts.set(f.subarray(0, end - cp), cp);
|
|
367
|
+
}
|
|
368
|
+
cp = end;
|
|
369
|
+
}
|
|
370
|
+
xs[n] = x;
|
|
371
|
+
return fonts;
|
|
372
|
+
}
|
|
373
|
+
/**
|
|
374
|
+
* 줄 나누기 (02 §4, 12 §9): 한 줄씩(탐욕). 끊을 수 있는 곳 = 공백 뒤, 개체 앞뒤, 글자 단위(char)면 한글·한자 사이. 금칙(줄 앞·줄 끝
|
|
375
|
+
* 금지)인 곳에서는 끊지 않는다 — 넘치면 앞의 끊을 곳으로 밀어낸다. 끊을 곳이 없으면 (어절이 줄보다 김) 글자 단위로 끊되 줄 앞 금지
|
|
376
|
+
* 글자는 한 글자 당긴다. squeeze면 줄 안 어간 수 × squeeze만큼 넘쳐도 된다 (양쪽 정렬에서 어간을 줄인다). hang이면 줄 끝을 넘는
|
|
377
|
+
* 쉼표·마침표 하나는 그 줄에 둔다 (여백에 걸침).
|
|
378
|
+
* startAt·maxLines: 어울림 줄바꿈 (breakFrom)
|
|
379
|
+
*/
|
|
380
|
+
function breakLines(n, xs, starts, first, restWidth, firstWidth = restWidth, opts = NO_KOREAN, startAt = 0, maxLines = Infinity) {
|
|
381
|
+
if (n === 0) return [{
|
|
382
|
+
start: 0,
|
|
383
|
+
end: 0,
|
|
384
|
+
scale: 1
|
|
385
|
+
}];
|
|
386
|
+
const lines = [];
|
|
387
|
+
let lineStart = startAt;
|
|
388
|
+
let lastBreak = -1;
|
|
389
|
+
let lastBreakG = -1;
|
|
390
|
+
/** 줄 안 어간 수 (공백 뒤에 글자가 온 곳) */
|
|
391
|
+
let gaps = 0;
|
|
392
|
+
let g = 0;
|
|
393
|
+
while (g < starts.length) {
|
|
394
|
+
const i = starts[g];
|
|
395
|
+
const next = g + 1 < starts.length ? starts[g + 1] : n;
|
|
396
|
+
const ch = first[g];
|
|
397
|
+
if (g > 0 && i > lineStart) {
|
|
398
|
+
const a = first[g - 1];
|
|
399
|
+
if (canBreakBetween(a, ch, opts)) {
|
|
400
|
+
lastBreak = i;
|
|
401
|
+
lastBreakG = g;
|
|
402
|
+
}
|
|
403
|
+
if (isSpace(a) && !isSpace(ch)) gaps++;
|
|
404
|
+
}
|
|
405
|
+
const width = (lines.length === 0 ? firstWidth : restWidth) + (opts.squeeze > 0 ? gaps * opts.squeeze : 0);
|
|
406
|
+
if (!isSpace(ch) && xs[next] - xs[lineStart] > width && i > lineStart) {
|
|
407
|
+
if (opts.hang && HANGABLE.has(ch) && xs[i] - xs[lineStart] <= width) {
|
|
408
|
+
g++;
|
|
409
|
+
continue;
|
|
410
|
+
}
|
|
411
|
+
let breakAt;
|
|
412
|
+
let bg;
|
|
413
|
+
if (lastBreak > lineStart) {
|
|
414
|
+
breakAt = lastBreak;
|
|
415
|
+
bg = lastBreakG;
|
|
416
|
+
} else {
|
|
417
|
+
breakAt = i;
|
|
418
|
+
bg = g;
|
|
419
|
+
for (let back = 1; back <= 3; back++) {
|
|
420
|
+
if (!NO_LINE_START.has(first[bg]) && !(bg > 0 && NO_LINE_END.has(first[bg - 1]))) break;
|
|
421
|
+
if (g - back <= 0 || starts[g - back] <= lineStart) {
|
|
422
|
+
bg = g;
|
|
423
|
+
break;
|
|
424
|
+
}
|
|
425
|
+
bg = g - back;
|
|
426
|
+
}
|
|
427
|
+
breakAt = starts[bg];
|
|
428
|
+
}
|
|
429
|
+
lines.push({
|
|
430
|
+
start: lineStart,
|
|
431
|
+
end: breakAt,
|
|
432
|
+
scale: 1
|
|
433
|
+
});
|
|
434
|
+
if (lines.length >= maxLines) return lines;
|
|
435
|
+
lineStart = breakAt;
|
|
436
|
+
lastBreak = -1;
|
|
437
|
+
gaps = 0;
|
|
438
|
+
g = bg;
|
|
439
|
+
continue;
|
|
440
|
+
}
|
|
441
|
+
g++;
|
|
442
|
+
}
|
|
443
|
+
lines.push({
|
|
444
|
+
start: lineStart,
|
|
445
|
+
end: n,
|
|
446
|
+
scale: 1
|
|
447
|
+
});
|
|
448
|
+
return lines;
|
|
449
|
+
}
|
|
450
|
+
// packages/layout/src/document.ts
|
|
451
|
+
/**
|
|
452
|
+
* 쪽 조립과 위치 질의 (02 §6–7, 03 §5)
|
|
453
|
+
*
|
|
454
|
+
* 0단계 단순화: 구역 1개, 고정 용지, 머리말·각주·다단 없음
|
|
455
|
+
*
|
|
456
|
+
* PagedLayout은 상태를 가진 증분 레이아웃이다.
|
|
457
|
+
*
|
|
458
|
+
* 1. update(): 이전 결과와 새 문단 목록을 앞뒤에서 비교해 바뀐 구간 [lo, hi)를 찾는다.
|
|
459
|
+
* 문단 객체가 같으면(모델이 바뀌지 않은 문단은 같은 객체를 유지) 내용도 같다.
|
|
460
|
+
* 2. lo부터 순서대로 배치한다. 텍스트가 바뀐 문단은 줄을 다시 나누고(reflow),
|
|
461
|
+
* 그대로인 문단은 줄 나눔을 재사용해 위치만 다시 놓는다(reposition).
|
|
462
|
+
* 그대로인 문단의 시작 상태(쪽, y)가 이전과 같으면 그 뒤는 전부 이전 결과와 같으므로 멈춘다(수렴).
|
|
463
|
+
* 3. 수렴하지 않으면(앞 문단 높이가 바뀌어 뒤가 전부 밀림) 화면 근처까지만 즉시 배치하고
|
|
464
|
+
* 나머지는 pending으로 남겨 step()으로 유휴 시간에 처리한다 (지연 쪽 조립).
|
|
465
|
+
* 4. 위치 질의 메서드는 필요한 구간이 아직 배치되지 않았으면 먼저 그 구간까지 배치한다.
|
|
466
|
+
*
|
|
467
|
+
* 불변식: [0, validCount) 문단은 현재 문서 기준으로 정확하다.
|
|
468
|
+
* [validCount, staleFrom) 문단은 직전 레이아웃에서 정확했던 결과다 (수렴 판단에 쓸 수 있음).
|
|
469
|
+
* [staleFrom, 끝) 문단은 줄 나눔만 믿을 수 있고 위치는 믿을 수 없다.
|
|
470
|
+
*/
|
|
471
|
+
/** 글자 서식만 쓰는 FormatSource (서식 번호 → 영향) */
|
|
472
|
+
function charFormats(style) {
|
|
473
|
+
return (info, force) => info.fmt || info.mark || force ? {
|
|
474
|
+
fmt: info.fmt ?? null,
|
|
475
|
+
mark: info.mark ?? 0,
|
|
476
|
+
style
|
|
477
|
+
} : null;
|
|
478
|
+
}
|
|
479
|
+
var NO_NOTES = Object.freeze({
|
|
480
|
+
fn: 0,
|
|
481
|
+
carry: Object.freeze([])
|
|
482
|
+
});
|
|
483
|
+
/** 다음 프레임으로: 각주 영역은 비고 이어 쓸 각주는 그대로 */
|
|
484
|
+
function nextFrame(ns) {
|
|
485
|
+
return ns.fn === 0 ? ns : ns.carry.length ? {
|
|
486
|
+
fn: 0,
|
|
487
|
+
carry: ns.carry
|
|
488
|
+
} : NO_NOTES;
|
|
489
|
+
}
|
|
490
|
+
function sameNotes(a, b) {
|
|
491
|
+
if (a === b) return true;
|
|
492
|
+
if (a.fn !== b.fn || a.carry.length !== b.carry.length) return false;
|
|
493
|
+
for (let i = 0; i < a.carry.length; i++) {
|
|
494
|
+
const x = a.carry[i];
|
|
495
|
+
const y = b.carry[i];
|
|
496
|
+
if (x.id !== y.id || x.from !== y.from || x.lines.length !== y.lines.length) return false;
|
|
497
|
+
}
|
|
498
|
+
return true;
|
|
499
|
+
}
|
|
500
|
+
/** 새 프레임 맨 위: 이어 쓸 각주를 limit 높이까지 놓는다 (각주마다 첫 줄은 늘 놓는다) */
|
|
501
|
+
function flushCarry(ns, frame, limit, sep, out) {
|
|
502
|
+
let fn = sep;
|
|
503
|
+
const carry = [];
|
|
504
|
+
for (const c of ns.carry) {
|
|
505
|
+
if (carry.length) {
|
|
506
|
+
carry.push(c);
|
|
507
|
+
continue;
|
|
508
|
+
}
|
|
509
|
+
let i = c.from;
|
|
510
|
+
let h = 0;
|
|
511
|
+
while (i < c.lines.length && (fn + c.lines[i] <= limit || i === c.from)) {
|
|
512
|
+
fn += c.lines[i];
|
|
513
|
+
h += c.lines[i];
|
|
514
|
+
i++;
|
|
515
|
+
}
|
|
516
|
+
out?.push({
|
|
517
|
+
frame,
|
|
518
|
+
id: c.id,
|
|
519
|
+
from: c.from,
|
|
520
|
+
to: i,
|
|
521
|
+
h,
|
|
522
|
+
cont: c.from > 0
|
|
523
|
+
});
|
|
524
|
+
if (i < c.lines.length) carry.push({
|
|
525
|
+
...c,
|
|
526
|
+
from: i
|
|
527
|
+
});
|
|
528
|
+
}
|
|
529
|
+
return {
|
|
530
|
+
fn,
|
|
531
|
+
carry
|
|
532
|
+
};
|
|
533
|
+
}
|
|
534
|
+
/**
|
|
535
|
+
* 줄 하나의 각주들을 지금 프레임 각주 영역에 놓는다. room: 그 줄 아래 남은 높이. 다 들어가지 않으면 줄 경계에서 나누고 나머지를
|
|
536
|
+
* 이어 쓴다. 앞 각주를 아직 이어 쓰는 중이면 새 각주는 그 뒤에 (차례를 지킨다)
|
|
537
|
+
*/
|
|
538
|
+
function placeNotes(ns, refs, r0, r1, frame, room, sep, out) {
|
|
539
|
+
if (ns.carry.length) return {
|
|
540
|
+
fn: ns.fn,
|
|
541
|
+
carry: [...ns.carry, ...refs.slice(r0, r1).map((r) => ({
|
|
542
|
+
id: r.id,
|
|
543
|
+
lines: r.lines,
|
|
544
|
+
from: 0
|
|
545
|
+
}))]
|
|
546
|
+
};
|
|
547
|
+
let fn = ns.fn;
|
|
548
|
+
const carry = [];
|
|
549
|
+
for (let j = r0; j < r1; j++) {
|
|
550
|
+
const r = refs[j];
|
|
551
|
+
if (carry.length) {
|
|
552
|
+
carry.push({
|
|
553
|
+
id: r.id,
|
|
554
|
+
lines: r.lines,
|
|
555
|
+
from: 0
|
|
556
|
+
});
|
|
557
|
+
continue;
|
|
558
|
+
}
|
|
559
|
+
const s = fn === 0 ? sep : 0;
|
|
560
|
+
if (fn + s + r.total <= room) {
|
|
561
|
+
fn += s + r.total;
|
|
562
|
+
out?.push({
|
|
563
|
+
frame,
|
|
564
|
+
id: r.id,
|
|
565
|
+
from: 0,
|
|
566
|
+
to: r.lines.length,
|
|
567
|
+
h: r.total,
|
|
568
|
+
cont: false
|
|
569
|
+
});
|
|
570
|
+
continue;
|
|
571
|
+
}
|
|
572
|
+
let i = 0;
|
|
573
|
+
let h = 0;
|
|
574
|
+
while (i < r.lines.length && fn + s + h + r.lines[i] <= room) h += r.lines[i++];
|
|
575
|
+
if (i > 0) {
|
|
576
|
+
fn += s + h;
|
|
577
|
+
out?.push({
|
|
578
|
+
frame,
|
|
579
|
+
id: r.id,
|
|
580
|
+
from: 0,
|
|
581
|
+
to: i,
|
|
582
|
+
h,
|
|
583
|
+
cont: false
|
|
584
|
+
});
|
|
585
|
+
}
|
|
586
|
+
carry.push({
|
|
587
|
+
id: r.id,
|
|
588
|
+
lines: r.lines,
|
|
589
|
+
from: i
|
|
590
|
+
});
|
|
591
|
+
}
|
|
592
|
+
return {
|
|
593
|
+
fn: carry.length ? Math.max(fn, room) : fn,
|
|
594
|
+
carry
|
|
595
|
+
};
|
|
596
|
+
}
|
|
597
|
+
/** 줄 안 표시 인덱스 d의 x (쪽 왼쪽 기준): 줄 시작 x + 셰이핑 위치 + 양쪽 맞춤 추가 폭 */
|
|
598
|
+
function lineX(pl, ln, d) {
|
|
599
|
+
const x = ln.x + pl.xs[d] - pl.xs[ln.start];
|
|
600
|
+
if (!ln.adj) return x;
|
|
601
|
+
const k = Math.max(0, Math.min(ln.adj.length - 1, d - ln.start));
|
|
602
|
+
return x + ln.adj[k];
|
|
603
|
+
}
|
|
604
|
+
var isJustifySpace = (c) => c === " " || c === " ";
|
|
605
|
+
/** 글자 사이 맞춤의 글자당 상한 (em 비율) */
|
|
606
|
+
var MAX_CHAR_STRETCH = .3;
|
|
607
|
+
/** 줄 끝 공백을 뺀 끝 인덱스 */
|
|
608
|
+
function trimEnd(pl, start, end) {
|
|
609
|
+
while (end > start) {
|
|
610
|
+
const c = cpSlice(pl, end - 1, end);
|
|
611
|
+
if (c !== " " && c !== " " && c !== " ") break;
|
|
612
|
+
end--;
|
|
613
|
+
}
|
|
614
|
+
return end;
|
|
615
|
+
}
|
|
616
|
+
/**
|
|
617
|
+
* 모든 쪽이 같고 쪽마다 본문 영역 하나 (2단계까지의 모양). 프레임 k = 쪽 k. 원점(PageSetup.originX/Y, 표 셀)이 바뀌면
|
|
618
|
+
* 캐시를 비운다
|
|
619
|
+
*/
|
|
620
|
+
var UniformFrames = class {
|
|
621
|
+
setup;
|
|
622
|
+
cache = [];
|
|
623
|
+
ox = 0;
|
|
624
|
+
oy = 0;
|
|
625
|
+
constructor(setup) {
|
|
626
|
+
this.setup = setup;
|
|
627
|
+
}
|
|
628
|
+
check() {
|
|
629
|
+
const s = this.setup;
|
|
630
|
+
if ((s.originX ?? 0) !== this.ox || (s.originY ?? 0) !== this.oy) {
|
|
631
|
+
this.ox = s.originX ?? 0;
|
|
632
|
+
this.oy = s.originY ?? 0;
|
|
633
|
+
this.cache = [];
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
frame(k) {
|
|
637
|
+
this.check();
|
|
638
|
+
let f = this.cache[k];
|
|
639
|
+
if (!f) {
|
|
640
|
+
const s = this.setup;
|
|
641
|
+
f = {
|
|
642
|
+
page: k,
|
|
643
|
+
column: 0,
|
|
644
|
+
x: leftOf(s),
|
|
645
|
+
top: pageTop(s, k) + s.margin,
|
|
646
|
+
width: s.width - s.margin * 2,
|
|
647
|
+
height: s.height - s.margin * 2
|
|
648
|
+
};
|
|
649
|
+
if (k < 1 << 20) this.cache[k] = f;
|
|
650
|
+
}
|
|
651
|
+
return f;
|
|
652
|
+
}
|
|
653
|
+
page(p) {
|
|
654
|
+
const s = this.setup;
|
|
655
|
+
return {
|
|
656
|
+
x: s.originX ?? 0,
|
|
657
|
+
top: pageTop(s, p),
|
|
658
|
+
width: s.width,
|
|
659
|
+
height: s.height
|
|
660
|
+
};
|
|
661
|
+
}
|
|
662
|
+
pageAt(y) {
|
|
663
|
+
const s = this.setup;
|
|
664
|
+
return Math.max(0, Math.floor((y - (s.originY ?? 0) - s.gap) / (s.height + s.gap)));
|
|
665
|
+
}
|
|
666
|
+
height(pages) {
|
|
667
|
+
return pageTop(this.setup, pages);
|
|
668
|
+
}
|
|
669
|
+
};
|
|
670
|
+
/** 쪽마다 배제 영역 (11 §4.2). 문단이 다시 놓이면 그 문단 것을 지우고 다시 넣는다 */
|
|
671
|
+
var FloatRegistry = class {
|
|
672
|
+
byPage = /* @__PURE__ */ new Map();
|
|
673
|
+
pagesOf = /* @__PURE__ */ new Map();
|
|
674
|
+
/** 떠 있는 개체를 가진 문단 (글 앞·뒤 개체 포함: 그리기용) */
|
|
675
|
+
owners = /* @__PURE__ */ new Set();
|
|
676
|
+
remove(owner) {
|
|
677
|
+
this.owners.delete(owner);
|
|
678
|
+
const pages = this.pagesOf.get(owner);
|
|
679
|
+
if (!pages) return;
|
|
680
|
+
for (const pg of pages) {
|
|
681
|
+
const m = this.byPage.get(pg);
|
|
682
|
+
m?.delete(owner);
|
|
683
|
+
if (m && m.size === 0) this.byPage.delete(pg);
|
|
684
|
+
}
|
|
685
|
+
this.pagesOf.delete(owner);
|
|
686
|
+
}
|
|
687
|
+
add(owner, page, ex) {
|
|
688
|
+
let m = this.byPage.get(page);
|
|
689
|
+
if (!m) this.byPage.set(page, m = /* @__PURE__ */ new Map());
|
|
690
|
+
let list = m.get(owner);
|
|
691
|
+
if (!list) m.set(owner, list = []);
|
|
692
|
+
list.push(ex);
|
|
693
|
+
const pages = this.pagesOf.get(owner) ?? [];
|
|
694
|
+
if (!pages.includes(page)) pages.push(page);
|
|
695
|
+
this.pagesOf.set(owner, pages);
|
|
696
|
+
}
|
|
697
|
+
/** 쪽 page에서 문단 para에 영향을 주는 배제 영역 (para와 그 앞 문단이 만든 것) */
|
|
698
|
+
forPage(page, para) {
|
|
699
|
+
const m = this.byPage.get(page);
|
|
700
|
+
if (!m) return NO_EXCL;
|
|
701
|
+
const out = [];
|
|
702
|
+
const at = para.info.start;
|
|
703
|
+
for (const [owner, list] of m) if (owner === para || owner.info.start < at) out.push(...list);
|
|
704
|
+
return out;
|
|
705
|
+
}
|
|
706
|
+
clear() {
|
|
707
|
+
this.byPage.clear();
|
|
708
|
+
this.pagesOf.clear();
|
|
709
|
+
this.owners.clear();
|
|
710
|
+
}
|
|
711
|
+
hasAny() {
|
|
712
|
+
return this.byPage.size > 0;
|
|
713
|
+
}
|
|
714
|
+
/** 문단 para가 쪽 p0–p1에서 본 배제 영역의 서명 (수렴 판단) */
|
|
715
|
+
sig(para, p0, p1) {
|
|
716
|
+
if (this.byPage.size === 0) return "";
|
|
717
|
+
let sig = "";
|
|
718
|
+
for (let pg = p0; pg <= p1; pg++) {
|
|
719
|
+
const ex = this.forPage(pg, para);
|
|
720
|
+
if (ex.length) sig += `${pg}:${ex.map((e) => `${e.x0},${e.x1},${e.y0},${e.y1}`).join(";")}|`;
|
|
721
|
+
}
|
|
722
|
+
return sig;
|
|
723
|
+
}
|
|
724
|
+
};
|
|
725
|
+
var NO_EXCL = [];
|
|
726
|
+
var NO_BOX = {
|
|
727
|
+
align: "left",
|
|
728
|
+
left: 0,
|
|
729
|
+
right: 0,
|
|
730
|
+
first: 0,
|
|
731
|
+
before: 0,
|
|
732
|
+
after: 0,
|
|
733
|
+
line: null
|
|
734
|
+
};
|
|
735
|
+
var NO_FLOATS = [];
|
|
736
|
+
/** [y0, y1)에서 줄이 들어갈 빈 구간. 막혔으면 null과 막힌 영역의 아래 끝 */
|
|
737
|
+
function slotAt(ex, width, y0, y1) {
|
|
738
|
+
const hits = ex.filter((e) => e.y0 < y1 && e.y1 > y0);
|
|
739
|
+
if (hits.length === 0) return {
|
|
740
|
+
x: 0,
|
|
741
|
+
w: width
|
|
742
|
+
};
|
|
743
|
+
let free = [[0, width]];
|
|
744
|
+
for (const e of hits) {
|
|
745
|
+
const next = [];
|
|
746
|
+
for (const [a, b] of free) if (e.x1 <= a || e.x0 >= b) next.push([a, b]);
|
|
747
|
+
else {
|
|
748
|
+
if (e.x0 > a) next.push([a, e.x0]);
|
|
749
|
+
if (e.x1 < b) next.push([e.x1, b]);
|
|
750
|
+
}
|
|
751
|
+
free = next;
|
|
752
|
+
}
|
|
753
|
+
const side = hits[0].side;
|
|
754
|
+
const ex0 = Math.min(...hits.map((e) => e.x0));
|
|
755
|
+
const ex1 = Math.max(...hits.map((e) => e.x1));
|
|
756
|
+
if (side === "left") free = free.filter(([, b]) => b <= ex0);
|
|
757
|
+
else if (side === "right") free = free.filter(([a]) => a >= ex1);
|
|
758
|
+
const best = free.reduce((m, f) => !m || f[1] - f[0] > m[1] - m[0] ? f : m, null);
|
|
759
|
+
if (!best || best[1] - best[0] < 24) return { blockedUntil: Math.min(...hits.map((e) => e.y1)) };
|
|
760
|
+
return {
|
|
761
|
+
x: best[0],
|
|
762
|
+
w: best[1] - best[0]
|
|
763
|
+
};
|
|
764
|
+
}
|
|
765
|
+
/**
|
|
766
|
+
* 배치된 문단. 위치는 시작 상태(쪽, y)와 끝 상태만 숫자로 들고 있고,
|
|
767
|
+
* 줄별 위치(lines)는 읽을 때 계산해 캐시한다. 다시 배치할 때 객체를 새로 만들지 않고 숫자만 바꾼다
|
|
768
|
+
* → 앞 문단 높이 변화로 문서 전체가 밀려도 문단당 할당이 없다.
|
|
769
|
+
*/
|
|
770
|
+
var PlacedPara = class {
|
|
771
|
+
info;
|
|
772
|
+
length;
|
|
773
|
+
layout;
|
|
774
|
+
geo;
|
|
775
|
+
width;
|
|
776
|
+
/** 배치 직전·직후 상태 (문단 간격 적용 전). 수렴 판단에 쓴다 */
|
|
777
|
+
pageBefore = 0;
|
|
778
|
+
yBefore = 0;
|
|
779
|
+
pageAfter = 0;
|
|
780
|
+
yAfter = 0;
|
|
781
|
+
first = false;
|
|
782
|
+
/** 첫 줄을 놓기 시작한 상태 (앞에서 쪽 나눔·함께·나누지 않음·고아 줄로 다음 프레임에 옮겼으면 그 프레임 맨 위) */
|
|
783
|
+
p0 = 0;
|
|
784
|
+
y0 = 0;
|
|
785
|
+
/** 과부 줄 막기로 다음 프레임에 넘긴 줄 (그 줄부터 새 프레임) */
|
|
786
|
+
push = null;
|
|
787
|
+
_lines = null;
|
|
788
|
+
/** 각주 배치 상태 (12 §6.2): 놓기 전·후, 첫 줄을 놓기 시작할 때 */
|
|
789
|
+
nsBefore = NO_NOTES;
|
|
790
|
+
nsAfter = NO_NOTES;
|
|
791
|
+
ns0 = NO_NOTES;
|
|
792
|
+
/** 문서 마지막 문단 (남은 이어 쓸 각주를 뒤 프레임에 다 놓는다) */
|
|
793
|
+
last = false;
|
|
794
|
+
_notes = null;
|
|
795
|
+
wrapNotes = null;
|
|
796
|
+
/** 이 문단의 떠 있는 개체 (놓인 자리) */
|
|
797
|
+
floats = NO_FLOATS;
|
|
798
|
+
/** 어울림으로 다시 나눈 줄 (배제 영역과 겹치지 않으면 null: 캐시한 줄 나눔을 그대로) */
|
|
799
|
+
wrapped = null;
|
|
800
|
+
/** 놓을 때 본 배제 영역 (수렴 판단: 같은 자리라도 영역이 바뀌면 다시 놓는다) */
|
|
801
|
+
exclSig = "";
|
|
802
|
+
constructor(info, length, layout, geo, width = 0) {
|
|
803
|
+
this.info = info;
|
|
804
|
+
this.length = length;
|
|
805
|
+
this.layout = layout;
|
|
806
|
+
this.geo = geo;
|
|
807
|
+
this.width = width;
|
|
808
|
+
}
|
|
809
|
+
/**
|
|
810
|
+
* 시작 상태를 정하고 끝 상태를 계산한다 (줄 객체는 만들지 않음).
|
|
811
|
+
* toNext: 다음 문단과 함께(keepWithNext) 사슬이 이 프레임에 들어가지 않는다 (PagedLayout이 미리 보고 정한다)
|
|
812
|
+
*/
|
|
813
|
+
placeAt(page, y, first, toNext = false, ns = NO_NOTES, last = false) {
|
|
814
|
+
this.pageBefore = page;
|
|
815
|
+
this.yBefore = y;
|
|
816
|
+
this.nsBefore = ns;
|
|
817
|
+
this.last = last;
|
|
818
|
+
this.first = first;
|
|
819
|
+
this._lines = null;
|
|
820
|
+
this._notes = null;
|
|
821
|
+
this.wrapped = null;
|
|
822
|
+
this.wrapNotes = null;
|
|
823
|
+
this.push = null;
|
|
824
|
+
const { setup, floats: reg } = this.geo;
|
|
825
|
+
const H = this.geo.h;
|
|
826
|
+
reg.remove(this);
|
|
827
|
+
this.floats = NO_FLOATS;
|
|
828
|
+
const box = this.layout.box;
|
|
829
|
+
const keep = box?.keep;
|
|
830
|
+
const F = this.geo.frames;
|
|
831
|
+
if (keep?.breakBefore && (y > 0 || F.frame(page).column > 0 || F.frame(page).band)) {
|
|
832
|
+
do {
|
|
833
|
+
page++;
|
|
834
|
+
ns = nextFrame(ns);
|
|
835
|
+
} while (F.frame(page).column !== 0);
|
|
836
|
+
y = 0;
|
|
837
|
+
} else if ((keep?.column || toNext) && y > 0) {
|
|
838
|
+
page++;
|
|
839
|
+
y = 0;
|
|
840
|
+
ns = nextFrame(ns);
|
|
841
|
+
}
|
|
842
|
+
if (keep?.together && y > 0) {
|
|
843
|
+
const h = this.contentHeight();
|
|
844
|
+
const sp = first ? 0 : box.before;
|
|
845
|
+
if (y + sp + h > H(page) - ns.fn && h <= H(page + 1)) {
|
|
846
|
+
page++;
|
|
847
|
+
y = 0;
|
|
848
|
+
ns = nextFrame(ns);
|
|
849
|
+
}
|
|
850
|
+
}
|
|
851
|
+
this.p0 = page;
|
|
852
|
+
this.y0 = y;
|
|
853
|
+
this.ns0 = ns;
|
|
854
|
+
const src = this.layout.lines;
|
|
855
|
+
if (this.layout.table) {
|
|
856
|
+
[page, y, ns] = this.tableFlow(page, y, ns, null);
|
|
857
|
+
if (box && box.after > 0) y += box.after;
|
|
858
|
+
this.exclSig = reg.sig(this, this.pageBefore, page);
|
|
859
|
+
this.pageAfter = page;
|
|
860
|
+
this.yAfter = y;
|
|
861
|
+
this.nsAfter = ns;
|
|
862
|
+
return;
|
|
863
|
+
}
|
|
864
|
+
if (this.layout.floats) {
|
|
865
|
+
const lh0 = src.length ? lineHeight(setup, src[0].scale, box?.line) + objExtra(setup, src[0], box?.line) : setup.lineHeight;
|
|
866
|
+
let pg = page;
|
|
867
|
+
let top = y;
|
|
868
|
+
if (!first && top > 0) top += box ? box.before : setup.paraSpacing;
|
|
869
|
+
if (top + lh0 > H(pg) && top > 0) {
|
|
870
|
+
pg++;
|
|
871
|
+
top = 0;
|
|
872
|
+
}
|
|
873
|
+
this.placeFloats(pg, top);
|
|
874
|
+
}
|
|
875
|
+
let f = this.flow(page, y, ns, null, null);
|
|
876
|
+
if (setup.widowControl && f.wrapK < 0 && !this.layout.floats && src.length >= 2) {
|
|
877
|
+
const n = src.length;
|
|
878
|
+
for (let t = 0; t < 3 && f.splits.length; t++) {
|
|
879
|
+
const s = f.splits;
|
|
880
|
+
const last = s[s.length - 1];
|
|
881
|
+
const prev = s.length > 1 ? s[s.length - 2] : 0;
|
|
882
|
+
if (s[0] === 1 && this.y0 > 0) {
|
|
883
|
+
this.p0++;
|
|
884
|
+
this.y0 = 0;
|
|
885
|
+
this.ns0 = nextFrame(this.ns0);
|
|
886
|
+
this.push = null;
|
|
887
|
+
} else if (n - last === 1 && last - prev >= 3) this.push = [...this.push ?? [], last - 1];
|
|
888
|
+
else if (n - last === 1 && prev === 0 && this.y0 > 0) {
|
|
889
|
+
this.p0++;
|
|
890
|
+
this.y0 = 0;
|
|
891
|
+
this.ns0 = nextFrame(this.ns0);
|
|
892
|
+
this.push = null;
|
|
893
|
+
} else break;
|
|
894
|
+
f = this.flow(this.p0, this.y0, this.ns0, null, null);
|
|
895
|
+
}
|
|
896
|
+
}
|
|
897
|
+
page = f.page;
|
|
898
|
+
y = f.y;
|
|
899
|
+
ns = f.ns;
|
|
900
|
+
if (f.wrapK >= 0) [page, y, ns] = this.wrapFrom(f.wrapK);
|
|
901
|
+
if (box && box.after > 0) y += box.after;
|
|
902
|
+
this.exclSig = reg.sig(this, this.pageBefore, page);
|
|
903
|
+
this.pageAfter = page;
|
|
904
|
+
this.yAfter = y;
|
|
905
|
+
this.nsAfter = ns;
|
|
906
|
+
}
|
|
907
|
+
/** 이 문단이 각주 영역에 놓은 각주 (참조한 각주, 이어 쓴 각주) */
|
|
908
|
+
get notePlaces() {
|
|
909
|
+
if (this.layout.table) return [];
|
|
910
|
+
this.lines;
|
|
911
|
+
return this.wrapped ? this.wrapNotes ?? [] : this._notes ?? [];
|
|
912
|
+
}
|
|
913
|
+
/** 각주를 놓았을 수 있는가 (참조가 있거나 이어 쓸 각주를 받았다) */
|
|
914
|
+
get mayHaveNotes() {
|
|
915
|
+
return !!this.layout.notes?.length || this.ns0.carry.length > 0 || this.nsBefore.carry.length > 0;
|
|
916
|
+
}
|
|
917
|
+
/** 문단 내용 높이 (앞뒤 간격 빼고): 줄 높이 합, 표면 행 높이 합 */
|
|
918
|
+
contentHeight() {
|
|
919
|
+
const tbl = this.layout.table;
|
|
920
|
+
if (tbl) return tbl.rows.reduce((a, b) => a + b, 0);
|
|
921
|
+
const { setup } = this.geo;
|
|
922
|
+
const box = this.layout.box;
|
|
923
|
+
let h = 0;
|
|
924
|
+
for (const ln of this.layout.lines) h += lineHeight(setup, ln.scale, box?.line) + objExtra(setup, ln, box?.line);
|
|
925
|
+
return h;
|
|
926
|
+
}
|
|
927
|
+
/**
|
|
928
|
+
* 줄을 (page, y)부터 프레임에 흘린다: 넘치거나 과부 줄 막기로 넘긴 줄(push)에서 새 프레임. out을 주면 줄 위치를 채우고, 없으면
|
|
929
|
+
* 배제 영역(어울림)에 닿는 첫 줄에서 멈춘다 (wrapK). placeAt과 lines가 같은 흐름을 쓴다
|
|
930
|
+
*/
|
|
931
|
+
flow(page, y, ns, out, nout) {
|
|
932
|
+
const { setup, floats: reg, frames } = this.geo;
|
|
933
|
+
const H = this.geo.h;
|
|
934
|
+
const pl = this.layout;
|
|
935
|
+
const box = pl.box;
|
|
936
|
+
const src = pl.lines;
|
|
937
|
+
const push = this.push;
|
|
938
|
+
const refs = pl.notes ?? null;
|
|
939
|
+
const sep = setup.noteSeparator ?? 16;
|
|
940
|
+
let ri = 0;
|
|
941
|
+
if (!this.first && y > 0) y += box ? box.before : setup.paraSpacing;
|
|
942
|
+
const splits = [];
|
|
943
|
+
for (let k = 0; k < src.length; k++) {
|
|
944
|
+
const ln = src[k];
|
|
945
|
+
const extra = objExtra(setup, ln, box?.line);
|
|
946
|
+
const lh = lineHeight(setup, ln.scale, box?.line) + extra;
|
|
947
|
+
const r0 = ri;
|
|
948
|
+
let nh = 0;
|
|
949
|
+
if (refs) while (ri < refs.length && (refs[ri].k < ln.end || k === src.length - 1)) nh += refs[ri++].total;
|
|
950
|
+
for (let again = false;; again = true) {
|
|
951
|
+
if (ns.carry.length && ns.fn === 0 && y === 0) ns = flushCarry(ns, page, H(page) / 2, sep, nout);
|
|
952
|
+
const need = nh === 0 || ns.carry.length ? 0 : (ns.fn === 0 ? sep : 0) + nh;
|
|
953
|
+
const Hk = H(page);
|
|
954
|
+
const over = y + lh + ns.fn + need > Hk;
|
|
955
|
+
const split = over && need > Hk / 2 && y + lh + ns.fn + (ns.fn === 0 ? sep : 0) + refs[r0].lines[0] <= Hk;
|
|
956
|
+
if (y > 0 && (!again && push !== null && push.includes(k) || over && !split)) {
|
|
957
|
+
page++;
|
|
958
|
+
y = 0;
|
|
959
|
+
ns = nextFrame(ns);
|
|
960
|
+
splits.push(k);
|
|
961
|
+
continue;
|
|
962
|
+
}
|
|
963
|
+
break;
|
|
964
|
+
}
|
|
965
|
+
if (out) {
|
|
966
|
+
const fr = frames.frame(page);
|
|
967
|
+
const top = fr.top + y;
|
|
968
|
+
out[k] = {
|
|
969
|
+
index: k,
|
|
970
|
+
start: ln.start,
|
|
971
|
+
end: ln.end,
|
|
972
|
+
page: fr.page,
|
|
973
|
+
x: fr.x,
|
|
974
|
+
y: top,
|
|
975
|
+
height: lh,
|
|
976
|
+
baseline: top + baselineOffset(setup, lh - extra, ln.scale) + extra,
|
|
977
|
+
adj: null,
|
|
978
|
+
adjChars: false
|
|
979
|
+
};
|
|
980
|
+
if (box) this.alignLine(out[k], k === src.length - 1);
|
|
981
|
+
} else {
|
|
982
|
+
const ex = reg.forPage(page, this);
|
|
983
|
+
if (ex.length && ex.some((e) => e.y0 < y + lh && e.y1 > y)) return {
|
|
984
|
+
page,
|
|
985
|
+
y,
|
|
986
|
+
splits,
|
|
987
|
+
wrapK: k,
|
|
988
|
+
ns
|
|
989
|
+
};
|
|
990
|
+
}
|
|
991
|
+
if (ri > r0) ns = placeNotes(ns, refs, r0, ri, page, H(page) - y - lh, sep, nout);
|
|
992
|
+
y += lh;
|
|
993
|
+
}
|
|
994
|
+
if (this.last) [page, y, ns] = this.flushTail(page, y, ns, nout);
|
|
995
|
+
return {
|
|
996
|
+
page,
|
|
997
|
+
y,
|
|
998
|
+
splits,
|
|
999
|
+
wrapK: -1,
|
|
1000
|
+
ns
|
|
1001
|
+
};
|
|
1002
|
+
}
|
|
1003
|
+
/** 문서 끝: 남은 이어 쓸 각주를 뒤 프레임들에 다 놓는다 */
|
|
1004
|
+
flushTail(page, y, ns, nout) {
|
|
1005
|
+
const sep = this.geo.setup.noteSeparator ?? 16;
|
|
1006
|
+
for (let guard = 0; ns.carry.length && guard < 1e4; guard++) {
|
|
1007
|
+
page++;
|
|
1008
|
+
y = 0;
|
|
1009
|
+
ns = flushCarry(nextFrame(ns), page, this.geo.h(page), sep, nout);
|
|
1010
|
+
}
|
|
1011
|
+
return [
|
|
1012
|
+
page,
|
|
1013
|
+
y,
|
|
1014
|
+
ns
|
|
1015
|
+
];
|
|
1016
|
+
}
|
|
1017
|
+
/** 떠 있는 개체의 자리 (11 §2.3): 문단 위쪽 (pg, top) 기준. 배제 영역을 등록한다 */
|
|
1018
|
+
placeFloats(pg, top) {
|
|
1019
|
+
const { floats: reg, frames } = this.geo;
|
|
1020
|
+
const fr = frames.frame(pg);
|
|
1021
|
+
const pb = frames.page(fr.page);
|
|
1022
|
+
const cw = fr.width;
|
|
1023
|
+
const out = [];
|
|
1024
|
+
for (const [k, f] of this.layout.floats) {
|
|
1025
|
+
const baseW = f.relX === "page" ? pb.width : cw;
|
|
1026
|
+
const x = (f.relX === "page" ? pb.x - fr.x : 0) + (f.x === "left" ? 0 : f.x === "center" ? (baseW - f.w) / 2 : f.x === "right" ? baseW - f.w : f.x);
|
|
1027
|
+
const y = f.relY === "paragraph" ? top + f.y : f.relY === "margin" ? f.y : f.y - (fr.top - pb.top);
|
|
1028
|
+
out.push({
|
|
1029
|
+
k,
|
|
1030
|
+
page: pg,
|
|
1031
|
+
x: fr.x + x,
|
|
1032
|
+
y: fr.top + y,
|
|
1033
|
+
w: f.w,
|
|
1034
|
+
h: f.h,
|
|
1035
|
+
spec: f
|
|
1036
|
+
});
|
|
1037
|
+
if (f.wrap === "none") continue;
|
|
1038
|
+
const g = f.gap;
|
|
1039
|
+
reg.add(this, pg, {
|
|
1040
|
+
x0: f.wrap === "topBottom" ? -Infinity : x - g,
|
|
1041
|
+
x1: f.wrap === "topBottom" ? Infinity : x + f.w + g,
|
|
1042
|
+
y0: y - g,
|
|
1043
|
+
y1: y + f.h + g,
|
|
1044
|
+
side: f.side,
|
|
1045
|
+
owner: this
|
|
1046
|
+
});
|
|
1047
|
+
}
|
|
1048
|
+
this.floats = out;
|
|
1049
|
+
if (out.length) reg.owners.add(this);
|
|
1050
|
+
}
|
|
1051
|
+
/**
|
|
1052
|
+
* 줄 k부터 배제 영역을 피해 다시 나눈다 (11 §4.2): 줄마다 그 y의 빈 구간 폭으로 breakFrom. 빈 구간을 찾을 때는 기본 줄 높이로
|
|
1053
|
+
* 보고, 나눈 뒤 실제 높이를 쓴다. 막힌 y는 배제 영역 아래로 건너뛴다.
|
|
1054
|
+
* @returns 끝 쪽, 끝 y
|
|
1055
|
+
*/
|
|
1056
|
+
wrapFrom(k0) {
|
|
1057
|
+
const { setup, floats: reg } = this.geo;
|
|
1058
|
+
const H = this.geo.h;
|
|
1059
|
+
const pl = this.layout;
|
|
1060
|
+
const box = pl.box;
|
|
1061
|
+
const src = pl.lines;
|
|
1062
|
+
const cw = this.geo.frames.frame(this.p0).width;
|
|
1063
|
+
const lines = [];
|
|
1064
|
+
const refs = pl.notes ?? null;
|
|
1065
|
+
const sep = setup.noteSeparator ?? 16;
|
|
1066
|
+
const nout = [];
|
|
1067
|
+
let ri = 0;
|
|
1068
|
+
let ns = this.ns0;
|
|
1069
|
+
const account = (end, pg, yy, lh, lastLine) => {
|
|
1070
|
+
if (ns.carry.length && ns.fn === 0 && yy === 0) ns = flushCarry(ns, pg, H(pg) / 2, sep, nout);
|
|
1071
|
+
const r0 = ri;
|
|
1072
|
+
if (refs) while (ri < refs.length && (refs[ri].k < end || lastLine)) ri++;
|
|
1073
|
+
if (ri > r0) ns = placeNotes(ns, refs, r0, ri, pg, H(pg) - yy - lh, sep, nout);
|
|
1074
|
+
};
|
|
1075
|
+
let page = this.p0;
|
|
1076
|
+
let y = this.y0;
|
|
1077
|
+
{
|
|
1078
|
+
let pg = this.p0;
|
|
1079
|
+
let yy = this.y0;
|
|
1080
|
+
if (!this.first && yy > 0) yy += box ? box.before : setup.paraSpacing;
|
|
1081
|
+
for (let k = 0; k < k0; k++) {
|
|
1082
|
+
const ln = src[k];
|
|
1083
|
+
const lh = lineHeight(setup, ln.scale, box?.line) + objExtra(setup, ln, box?.line);
|
|
1084
|
+
if (yy > 0 && (yy + lh > H(pg) - ns.fn || this.push !== null && this.push.includes(k))) {
|
|
1085
|
+
pg++;
|
|
1086
|
+
yy = 0;
|
|
1087
|
+
ns = nextFrame(ns);
|
|
1088
|
+
}
|
|
1089
|
+
lines.push({
|
|
1090
|
+
start: ln.start,
|
|
1091
|
+
end: ln.end,
|
|
1092
|
+
scale: ln.scale,
|
|
1093
|
+
objH: ln.objH,
|
|
1094
|
+
page: pg,
|
|
1095
|
+
y: yy,
|
|
1096
|
+
slotX: 0,
|
|
1097
|
+
slotW: cw
|
|
1098
|
+
});
|
|
1099
|
+
account(ln.end, pg, yy, lh, false);
|
|
1100
|
+
yy += lh;
|
|
1101
|
+
}
|
|
1102
|
+
page = pg;
|
|
1103
|
+
y = yy;
|
|
1104
|
+
}
|
|
1105
|
+
let start = src[k0].start;
|
|
1106
|
+
const n = pl.n;
|
|
1107
|
+
let guard = 0;
|
|
1108
|
+
do {
|
|
1109
|
+
const probe = lineHeight(setup, 1, box?.line);
|
|
1110
|
+
if (y + probe > H(page) - ns.fn && y > 0) {
|
|
1111
|
+
page++;
|
|
1112
|
+
y = 0;
|
|
1113
|
+
ns = nextFrame(ns);
|
|
1114
|
+
}
|
|
1115
|
+
const slot = slotAt(reg.forPage(page, this), cw, y, y + probe);
|
|
1116
|
+
if ("blockedUntil" in slot) {
|
|
1117
|
+
y = Math.max(y + 1, slot.blockedUntil);
|
|
1118
|
+
if (++guard > 1e4) break;
|
|
1119
|
+
continue;
|
|
1120
|
+
}
|
|
1121
|
+
const firstLine = lines.length === 0;
|
|
1122
|
+
const indent = (box?.left ?? 0) + (box?.right ?? 0) + (firstLine ? (box?.first ?? 0) + (pl.marker?.shift ?? 0) : 0);
|
|
1123
|
+
const avail = Math.max(1, slot.w - indent);
|
|
1124
|
+
let end = breakFrom(pl, start, avail);
|
|
1125
|
+
if (end <= start && start < n) end = start + 1;
|
|
1126
|
+
const last = end >= n;
|
|
1127
|
+
const m = lineMetrics(pl, start, end, last);
|
|
1128
|
+
const ln = {
|
|
1129
|
+
scale: m.scale,
|
|
1130
|
+
objH: m.objH || void 0
|
|
1131
|
+
};
|
|
1132
|
+
const lh = lineHeight(setup, m.scale, box?.line) + objExtra(setup, ln, box?.line);
|
|
1133
|
+
if (y + lh > H(page) - ns.fn && y > 0) {
|
|
1134
|
+
page++;
|
|
1135
|
+
y = 0;
|
|
1136
|
+
ns = nextFrame(ns);
|
|
1137
|
+
continue;
|
|
1138
|
+
}
|
|
1139
|
+
lines.push({
|
|
1140
|
+
start,
|
|
1141
|
+
end,
|
|
1142
|
+
scale: m.scale,
|
|
1143
|
+
objH: ln.objH,
|
|
1144
|
+
page,
|
|
1145
|
+
y,
|
|
1146
|
+
slotX: slot.x,
|
|
1147
|
+
slotW: slot.w
|
|
1148
|
+
});
|
|
1149
|
+
account(end, page, y, lh, last);
|
|
1150
|
+
y += lh;
|
|
1151
|
+
start = end;
|
|
1152
|
+
} while (start < n && ++guard < 1e5);
|
|
1153
|
+
if (this.last) [page, y, ns] = this.flushTail(page, y, ns, nout);
|
|
1154
|
+
this.wrapped = lines;
|
|
1155
|
+
this.wrapNotes = nout;
|
|
1156
|
+
return [
|
|
1157
|
+
page,
|
|
1158
|
+
y,
|
|
1159
|
+
ns
|
|
1160
|
+
];
|
|
1161
|
+
}
|
|
1162
|
+
/** 이 문단이 놓인 프레임의 왼쪽 (쪽 그리기 기준 x, 구역마다 여백이 다르다) */
|
|
1163
|
+
get frameLeft() {
|
|
1164
|
+
return this.geo.frames.frame(this.p0).x;
|
|
1165
|
+
}
|
|
1166
|
+
/** 문서 기준 마지막 줄 아래쪽 y */
|
|
1167
|
+
get bottom() {
|
|
1168
|
+
return this.geo.frames.frame(this.pageAfter).top + this.yAfter;
|
|
1169
|
+
}
|
|
1170
|
+
/** 원점이 바뀌었다: 줄 위치를 다시 계산하게 한다 */
|
|
1171
|
+
dropLines() {
|
|
1172
|
+
this._lines = null;
|
|
1173
|
+
this._notes = null;
|
|
1174
|
+
}
|
|
1175
|
+
/** 표 문단의 행 조각 = 줄 (표시 인덱스는 모두 [0, 1): 표 원소 하나) */
|
|
1176
|
+
tableLines() {
|
|
1177
|
+
const out = [];
|
|
1178
|
+
this.tableFlow(this.p0, this.y0, this.ns0, out);
|
|
1179
|
+
return out;
|
|
1180
|
+
}
|
|
1181
|
+
/**
|
|
1182
|
+
* 표 행을 프레임에 흘린다 (12 §8, 배치와 줄 위치가 같은 흐름). 들어가면 통째로, 아니면:
|
|
1183
|
+
* - 쪼갤 수 있는 행 (행 나누지 않음·제목 행·여러 행 병합이 아님): 셀 내용을 줄 단위로 조각들에 나눈다 (TableSpec.split).
|
|
1184
|
+
* 첫 조각에 각 셀의 첫 줄이 들어가지 않으면 다음 프레임으로.
|
|
1185
|
+
* - 행 나누지 않음: 다음 프레임으로. 빈 프레임보다 크면 쪼갠다 (넘치게 두지 않는다).
|
|
1186
|
+
* - 표가 다음 프레임으로 이어지면 맨 위에 제목 행들(표 맨 위에서 이어진 header 행)을 다시 놓는다.
|
|
1187
|
+
*/
|
|
1188
|
+
tableFlow(page, y, ns, out) {
|
|
1189
|
+
const { setup, frames } = this.geo;
|
|
1190
|
+
const H = this.geo.h;
|
|
1191
|
+
const tbl = this.layout.table;
|
|
1192
|
+
const box = this.layout.box;
|
|
1193
|
+
const rows = tbl.rows;
|
|
1194
|
+
const flags = tbl.flags;
|
|
1195
|
+
const pad = tbl.pad ?? 0;
|
|
1196
|
+
if (!this.first && y > 0) y += box ? box.before : setup.paraSpacing;
|
|
1197
|
+
let nh = 0;
|
|
1198
|
+
while (nh < rows.length - 1 && flags?.[nh]?.header) nh++;
|
|
1199
|
+
let headerH = 0;
|
|
1200
|
+
for (let k = 0; k < nh; k++) headerH += rows[k];
|
|
1201
|
+
/** 이 프레임에 표 행이 (다시 그린 제목 행 말고) 아직 없다 */
|
|
1202
|
+
let fresh = y === 0;
|
|
1203
|
+
const push = (row, part, h, repeat = false) => {
|
|
1204
|
+
if (out) {
|
|
1205
|
+
const fr = frames.frame(page);
|
|
1206
|
+
const free = Math.max(0, fr.width - (box?.left ?? 0) - (box?.right ?? 0) - tbl.width);
|
|
1207
|
+
const x = fr.x + (box?.left ?? 0) + (box?.align === "center" ? free / 2 : box?.align === "right" ? free : 0);
|
|
1208
|
+
const top = fr.top + y;
|
|
1209
|
+
out.push({
|
|
1210
|
+
index: out.length,
|
|
1211
|
+
start: 0,
|
|
1212
|
+
end: 1,
|
|
1213
|
+
page: fr.page,
|
|
1214
|
+
x,
|
|
1215
|
+
y: top,
|
|
1216
|
+
height: h,
|
|
1217
|
+
baseline: top + h,
|
|
1218
|
+
adj: null,
|
|
1219
|
+
adjChars: false,
|
|
1220
|
+
row,
|
|
1221
|
+
part,
|
|
1222
|
+
...repeat ? { repeat } : {}
|
|
1223
|
+
});
|
|
1224
|
+
}
|
|
1225
|
+
y += h;
|
|
1226
|
+
};
|
|
1227
|
+
const nextFrame_ = (r) => {
|
|
1228
|
+
page++;
|
|
1229
|
+
y = 0;
|
|
1230
|
+
ns = nextFrame(ns);
|
|
1231
|
+
fresh = true;
|
|
1232
|
+
if (nh > 0 && r >= nh && headerH < H(page) / 2) for (let k = 0; k < nh; k++) push(k, 0, rows[k], true);
|
|
1233
|
+
};
|
|
1234
|
+
for (let r = 0; r < rows.length; r++) {
|
|
1235
|
+
const rh = rows[r];
|
|
1236
|
+
const avail = H(page) - ns.fn - y;
|
|
1237
|
+
if (rh <= avail) {
|
|
1238
|
+
push(r, 0, rh);
|
|
1239
|
+
fresh = false;
|
|
1240
|
+
continue;
|
|
1241
|
+
}
|
|
1242
|
+
const f = flags?.[r];
|
|
1243
|
+
const repeatH = nh > 0 && r >= nh && headerH < H(page + 1) / 2 ? headerH : 0;
|
|
1244
|
+
const empty = H(page + 1) - repeatH;
|
|
1245
|
+
if (!!tbl.split && !f?.merged && r >= nh && (!f?.cantSplit || rh > empty)) {
|
|
1246
|
+
const caps = [avail - 2 * pad];
|
|
1247
|
+
for (let j = 1; j < 64; j++) caps.push(H(page + j) - repeatH - 2 * pad);
|
|
1248
|
+
const used = caps[0] > 0 ? tbl.split(r, caps) : null;
|
|
1249
|
+
if (used && used.length > 1) {
|
|
1250
|
+
used.forEach((u, j) => {
|
|
1251
|
+
if (j > 0) nextFrame_(r + 1);
|
|
1252
|
+
push(r, j, u + 2 * pad);
|
|
1253
|
+
});
|
|
1254
|
+
fresh = false;
|
|
1255
|
+
continue;
|
|
1256
|
+
}
|
|
1257
|
+
}
|
|
1258
|
+
if (!fresh) {
|
|
1259
|
+
nextFrame_(r);
|
|
1260
|
+
r--;
|
|
1261
|
+
continue;
|
|
1262
|
+
}
|
|
1263
|
+
push(r, 0, rh);
|
|
1264
|
+
fresh = false;
|
|
1265
|
+
}
|
|
1266
|
+
return [
|
|
1267
|
+
page,
|
|
1268
|
+
y,
|
|
1269
|
+
ns
|
|
1270
|
+
];
|
|
1271
|
+
}
|
|
1272
|
+
get lines() {
|
|
1273
|
+
if (this._lines) return this._lines;
|
|
1274
|
+
if (this.layout.table) return this._lines = this.tableLines();
|
|
1275
|
+
if (this.wrapped) return this._lines = this.wrappedLines(this.wrapped);
|
|
1276
|
+
const lines = new Array(this.layout.lines.length);
|
|
1277
|
+
const notes = [];
|
|
1278
|
+
this.flow(this.p0, this.y0, this.ns0, lines, notes);
|
|
1279
|
+
this._lines = lines;
|
|
1280
|
+
this._notes = notes;
|
|
1281
|
+
return lines;
|
|
1282
|
+
}
|
|
1283
|
+
wrappedLines(src) {
|
|
1284
|
+
const { setup } = this.geo;
|
|
1285
|
+
const box = this.layout.box;
|
|
1286
|
+
return src.map((w, k) => {
|
|
1287
|
+
const extra = objExtra(setup, w, box?.line);
|
|
1288
|
+
const lh = lineHeight(setup, w.scale, box?.line) + extra;
|
|
1289
|
+
const fr = this.geo.frames.frame(w.page);
|
|
1290
|
+
const top = fr.top + w.y;
|
|
1291
|
+
const line = {
|
|
1292
|
+
index: k,
|
|
1293
|
+
start: w.start,
|
|
1294
|
+
end: w.end,
|
|
1295
|
+
page: fr.page,
|
|
1296
|
+
x: fr.x + w.slotX,
|
|
1297
|
+
y: top,
|
|
1298
|
+
height: lh,
|
|
1299
|
+
baseline: top + baselineOffset(setup, lh - extra, w.scale) + extra,
|
|
1300
|
+
adj: null,
|
|
1301
|
+
adjChars: false
|
|
1302
|
+
};
|
|
1303
|
+
const indent = (box?.left ?? 0) + (box?.right ?? 0) + (k === 0 ? (box?.first ?? 0) + (this.layout.marker?.shift ?? 0) : 0);
|
|
1304
|
+
this.alignLine(line, k === src.length - 1, Math.max(1, w.slotW - indent));
|
|
1305
|
+
return line;
|
|
1306
|
+
});
|
|
1307
|
+
}
|
|
1308
|
+
/** 들여쓰기·정렬·양쪽 맞춤 (10 §4). avail: 줄에 쓸 수 있는 폭 (어울림 줄이면 빈 구간에서 들여쓰기를 뺀 것) */
|
|
1309
|
+
alignLine(line, last, availOverride) {
|
|
1310
|
+
const pl = this.layout;
|
|
1311
|
+
const box = pl.box ?? NO_BOX;
|
|
1312
|
+
const avail = availOverride ?? pl.widths[line.index === 0 ? 0 : 1];
|
|
1313
|
+
line.x += box.left + (line.index === 0 ? box.first + (pl.marker?.shift ?? 0) : 0);
|
|
1314
|
+
const end = trimEnd(pl, line.start, line.end);
|
|
1315
|
+
const hang = hangingAt(pl, line.start, end);
|
|
1316
|
+
const contentEnd = hang >= 0 && pl.xs[end] - pl.xs[line.start] > avail ? hang : end;
|
|
1317
|
+
const free = avail - (pl.xs[contentEnd] - pl.xs[line.start]);
|
|
1318
|
+
const squeeze = box.align === "justify" && !last ? box.korean?.squeeze ?? 0 : 0;
|
|
1319
|
+
if (free <= 0 && !(free < 0 && squeeze > 0)) return;
|
|
1320
|
+
if (box.align === "center") line.x += free / 2;
|
|
1321
|
+
else if (box.align === "right") line.x += free;
|
|
1322
|
+
else if (box.align === "justify" && !last) {
|
|
1323
|
+
const n = end - line.start;
|
|
1324
|
+
const spaces = [];
|
|
1325
|
+
for (let d = line.start; d < contentEnd; d++) if (isJustifySpace(cpSlice(pl, d, d + 1))) spaces.push(d);
|
|
1326
|
+
const em = this.geo.setup.fontSize ?? this.geo.setup.lineHeight / 1.7;
|
|
1327
|
+
let sEach = 0;
|
|
1328
|
+
let gEach = 0;
|
|
1329
|
+
const gaps = [];
|
|
1330
|
+
if (free < 0) {
|
|
1331
|
+
if (!spaces.length) return;
|
|
1332
|
+
sEach = Math.max(free / spaces.length, -squeeze);
|
|
1333
|
+
} else {
|
|
1334
|
+
const S = spaces.length;
|
|
1335
|
+
if (S) sEach = Math.min(free / S, em * .5);
|
|
1336
|
+
let rest = free - sEach * S;
|
|
1337
|
+
if (rest > 1e-6) {
|
|
1338
|
+
for (let d = line.start + 1; d < contentEnd; d++) if (pl.stops[d] && !isJustifySpace(cpSlice(pl, d - 1, d)) && !isJustifySpace(cpSlice(pl, d, d + 1))) gaps.push(d);
|
|
1339
|
+
if (gaps.length) {
|
|
1340
|
+
gEach = Math.min(rest / gaps.length, em * .1);
|
|
1341
|
+
rest -= gEach * gaps.length;
|
|
1342
|
+
}
|
|
1343
|
+
if (rest > 1e-6) {
|
|
1344
|
+
if (S) sEach += rest / S;
|
|
1345
|
+
else if (gaps.length) gEach = Math.min(gEach + rest / gaps.length, em * MAX_CHAR_STRETCH);
|
|
1346
|
+
}
|
|
1347
|
+
}
|
|
1348
|
+
if (sEach === 0 && gEach === 0) return;
|
|
1349
|
+
}
|
|
1350
|
+
const adj = new Float32Array(n + 1);
|
|
1351
|
+
let acc = 0;
|
|
1352
|
+
let s = 0;
|
|
1353
|
+
let g = 0;
|
|
1354
|
+
for (let d = line.start; d <= end; d++) {
|
|
1355
|
+
if (g < gaps.length && gaps[g] === d) {
|
|
1356
|
+
acc += gEach;
|
|
1357
|
+
g++;
|
|
1358
|
+
}
|
|
1359
|
+
adj[d - line.start] = acc;
|
|
1360
|
+
if (s < spaces.length && spaces[s] === d) {
|
|
1361
|
+
acc += sEach;
|
|
1362
|
+
s++;
|
|
1363
|
+
}
|
|
1364
|
+
}
|
|
1365
|
+
if (gEach > 0) line.adjChars = true;
|
|
1366
|
+
line.adj = adj;
|
|
1367
|
+
}
|
|
1368
|
+
}
|
|
1369
|
+
};
|
|
1370
|
+
/** 줄 높이 배율 → 줄 높이 (px). 배율 1이면 setup.lineHeight 그대로 */
|
|
1371
|
+
function lineHeight(setup, scale, rule) {
|
|
1372
|
+
const natural = scale === 1 ? setup.lineHeight : Math.round(setup.lineHeight * scale);
|
|
1373
|
+
if (!rule) return natural;
|
|
1374
|
+
if (rule.rule === "multiple") return rule.value === 1 ? natural : Math.round(setup.lineHeight * scale * rule.value);
|
|
1375
|
+
if (rule.rule === "exact") return Math.round(rule.value);
|
|
1376
|
+
return Math.max(natural, Math.round(rule.value));
|
|
1377
|
+
}
|
|
1378
|
+
/**
|
|
1379
|
+
* 개체가 글자보다 높으면 줄을 그만큼 늘린다 (11 §4.1): 개체는 기준선 위에 서므로 늘어나는 몫 = 개체 높이 − 글자 ascent.
|
|
1380
|
+
* 고정 줄 간격(exact)은 늘리지 않는다
|
|
1381
|
+
*/
|
|
1382
|
+
function objExtra(setup, ln, rule) {
|
|
1383
|
+
if (!ln.objH || rule?.rule === "exact") return 0;
|
|
1384
|
+
const em = (setup.fontSize ?? setup.lineHeight / 1.7) * ln.scale;
|
|
1385
|
+
return Math.max(0, Math.round(ln.objH - (setup.ascent ?? .8) * em));
|
|
1386
|
+
}
|
|
1387
|
+
/**
|
|
1388
|
+
* 줄 위쪽에서 기준선까지 (10 §4): 줄 가운데에 그 줄 가장 큰 글자의 글꼴 상자(ascent + descent)를 세운다.
|
|
1389
|
+
* 크기가 섞인 줄도 기준선 하나에 모든 글자를 놓는다.
|
|
1390
|
+
*/
|
|
1391
|
+
function baselineOffset(setup, height, scale) {
|
|
1392
|
+
const em = (setup.fontSize ?? setup.lineHeight / 1.7) * scale;
|
|
1393
|
+
const asc = setup.ascent ?? .8;
|
|
1394
|
+
const desc = setup.descent ?? .2;
|
|
1395
|
+
return height / 2 + (asc - desc) / 2 * em;
|
|
1396
|
+
}
|
|
1397
|
+
function pageTop(setup, page) {
|
|
1398
|
+
return (setup.originY ?? 0) + setup.gap + page * (setup.height + setup.gap);
|
|
1399
|
+
}
|
|
1400
|
+
/** 줄 x의 기준 (쪽 왼쪽 기준 본문 시작) */
|
|
1401
|
+
function leftOf(setup) {
|
|
1402
|
+
return setup.margin + (setup.originX ?? 0);
|
|
1403
|
+
}
|
|
1404
|
+
var LayoutCache = class {
|
|
1405
|
+
maxEntries;
|
|
1406
|
+
map = /* @__PURE__ */ new Map();
|
|
1407
|
+
hits = 0;
|
|
1408
|
+
misses = 0;
|
|
1409
|
+
constructor(maxEntries = 4e3) {
|
|
1410
|
+
this.maxEntries = maxEntries;
|
|
1411
|
+
}
|
|
1412
|
+
get(text, width, measurer, preedit, format = null) {
|
|
1413
|
+
if (preedit) {
|
|
1414
|
+
this.misses++;
|
|
1415
|
+
return layoutParagraph(text, width, measurer, preedit, format);
|
|
1416
|
+
}
|
|
1417
|
+
const key = format ? `${width}|${formatKey(format)}|${text}` : `${width}|${text}`;
|
|
1418
|
+
let hit = this.map.get(key);
|
|
1419
|
+
if (hit) {
|
|
1420
|
+
this.hits++;
|
|
1421
|
+
return hit;
|
|
1422
|
+
}
|
|
1423
|
+
this.misses++;
|
|
1424
|
+
hit = layoutParagraph(text, width, measurer, null, format);
|
|
1425
|
+
if (this.map.size >= this.maxEntries) {
|
|
1426
|
+
const oldest = this.map.keys().next().value;
|
|
1427
|
+
if (oldest !== void 0) this.map.delete(oldest);
|
|
1428
|
+
}
|
|
1429
|
+
this.map.set(key, hit);
|
|
1430
|
+
return hit;
|
|
1431
|
+
}
|
|
1432
|
+
clear() {
|
|
1433
|
+
this.map.clear();
|
|
1434
|
+
}
|
|
1435
|
+
};
|
|
1436
|
+
/** 서식 번호 열을 런으로 줄인 문자열 ("번호x길이," … "¶번호") */
|
|
1437
|
+
function formatKey(f) {
|
|
1438
|
+
let out = "";
|
|
1439
|
+
const fmt = f.fmt;
|
|
1440
|
+
if (fmt) {
|
|
1441
|
+
let k = 0;
|
|
1442
|
+
while (k < fmt.length) {
|
|
1443
|
+
let j = k + 1;
|
|
1444
|
+
while (j < fmt.length && fmt[j] === fmt[k]) j++;
|
|
1445
|
+
out += `${fmt[k]}x${j - k},`;
|
|
1446
|
+
k = j;
|
|
1447
|
+
}
|
|
1448
|
+
}
|
|
1449
|
+
const b = f.box;
|
|
1450
|
+
const bk = (b ? `|${JSON.stringify(b)}` : "") + (f.marker ? `|#${f.marker.id},${f.marker.hanging},${f.marker.text}` : "") + (f.objs && f.objs.size ? `|o${[...f.objs].map(([k, o]) => `${k}:${o.w}x${o.h}`).join(",")}` : "") + (f.floats && f.floats.size ? `|f${JSON.stringify([...f.floats])}` : "") + (f.notes && f.notes.size ? `|n${[...f.notes].map(([k, n]) => `${k}:${n.id}:${n.lines.join("/")}`).join(",")}` : "") + (f.table ? `|t${f.table.width}:${f.table.rows.join(",")}${f.table.flags ? `:${f.table.flags.map((g) => g ? `${g.header ? "h" : ""}${g.cantSplit ? "c" : ""}${g.merged ? "m" : ""}` : "").join(",")}` : ""}` : "");
|
|
1451
|
+
return `${out}¶${f.mark}${bk}${f.key ? `|${f.key}` : ""}`;
|
|
1452
|
+
}
|
|
1453
|
+
var PagedLayout = class {
|
|
1454
|
+
setup;
|
|
1455
|
+
measurer;
|
|
1456
|
+
cache;
|
|
1457
|
+
format;
|
|
1458
|
+
frames;
|
|
1459
|
+
/** 현재 문서의 문단 목록 (모델 소유, 읽기만 함) */
|
|
1460
|
+
model = [];
|
|
1461
|
+
/** 문단별 배치 결과. pending 이후는 낡았거나(null 포함) 아직 계산되지 않았다 */
|
|
1462
|
+
paras = [];
|
|
1463
|
+
counts = {
|
|
1464
|
+
reflowed: 0,
|
|
1465
|
+
repositioned: 0,
|
|
1466
|
+
reused: 0
|
|
1467
|
+
};
|
|
1468
|
+
pending = null;
|
|
1469
|
+
staleFrom = Infinity;
|
|
1470
|
+
preedit = null;
|
|
1471
|
+
preeditPara = -1;
|
|
1472
|
+
contentWidth;
|
|
1473
|
+
geo;
|
|
1474
|
+
constructor(setup, measurer, cache = new LayoutCache(), format = null, frames = new UniformFrames(setup)) {
|
|
1475
|
+
this.setup = setup;
|
|
1476
|
+
this.measurer = measurer;
|
|
1477
|
+
this.cache = cache;
|
|
1478
|
+
this.format = format;
|
|
1479
|
+
this.frames = frames;
|
|
1480
|
+
this.contentWidth = frames.frame(0).width;
|
|
1481
|
+
this.geo = {
|
|
1482
|
+
setup,
|
|
1483
|
+
frames,
|
|
1484
|
+
h: (k) => frames.frame(k).height,
|
|
1485
|
+
floats: new FloatRegistry()
|
|
1486
|
+
};
|
|
1487
|
+
}
|
|
1488
|
+
/** 쪽 p의 상자 (문서 좌표) */
|
|
1489
|
+
pageBox(p) {
|
|
1490
|
+
return this.frames.page(p);
|
|
1491
|
+
}
|
|
1492
|
+
/** 문서 y가 든 쪽 */
|
|
1493
|
+
pageAt(y) {
|
|
1494
|
+
return this.frames.pageAt(y);
|
|
1495
|
+
}
|
|
1496
|
+
/** 좌표 원점을 옮긴다 (표 셀, PageSetup.originX/Y). 배치는 그대로, 줄 위치만 다시 계산한다 */
|
|
1497
|
+
setOrigin(x, y) {
|
|
1498
|
+
const s = this.setup;
|
|
1499
|
+
if ((s.originX ?? 0) === x && (s.originY ?? 0) === y) return;
|
|
1500
|
+
s.originX = x;
|
|
1501
|
+
s.originY = y;
|
|
1502
|
+
for (const pp of this.paras) pp?.dropLines();
|
|
1503
|
+
}
|
|
1504
|
+
/** 내용 높이 (모두 배치한 뒤, 쪽 하나로 조판한 셀에서): 마지막 문단 아래 끝 */
|
|
1505
|
+
get contentBottom() {
|
|
1506
|
+
this.finish();
|
|
1507
|
+
const last = this.paras[this.paras.length - 1];
|
|
1508
|
+
return last ? last.yAfter : 0;
|
|
1509
|
+
}
|
|
1510
|
+
/** 모든 문단이 배치되었는가 */
|
|
1511
|
+
get complete() {
|
|
1512
|
+
return this.pending === null;
|
|
1513
|
+
}
|
|
1514
|
+
/** 정확하게 배치된 앞쪽 문단 수 */
|
|
1515
|
+
get validCount() {
|
|
1516
|
+
return this.pending ? this.pending.next : this.paras.length;
|
|
1517
|
+
}
|
|
1518
|
+
/** 쪽 수. 배치 중이면 지금까지의 비율로 추정한다 */
|
|
1519
|
+
get pageCount() {
|
|
1520
|
+
const valid = this.validCount;
|
|
1521
|
+
if (valid === 0) return 1;
|
|
1522
|
+
const lastPage = this.frames.frame(this.paras[valid - 1].pageAfter).page + 1;
|
|
1523
|
+
if (!this.pending) return lastPage;
|
|
1524
|
+
return Math.max(lastPage, Math.ceil(lastPage * this.paras.length / valid));
|
|
1525
|
+
}
|
|
1526
|
+
get totalHeight() {
|
|
1527
|
+
return this.frames.height(this.pageCount);
|
|
1528
|
+
}
|
|
1529
|
+
/**
|
|
1530
|
+
* 문서가 바뀐 뒤 호출한다.
|
|
1531
|
+
* @param syncUntilY 이 문서 y까지는 즉시 배치한다 (나머지는 step()/질의 시 배치)
|
|
1532
|
+
*/
|
|
1533
|
+
update(model, preedit = null, syncUntilY = Infinity, changes = null) {
|
|
1534
|
+
this.counts = {
|
|
1535
|
+
reflowed: 0,
|
|
1536
|
+
repositioned: 0,
|
|
1537
|
+
reused: 0
|
|
1538
|
+
};
|
|
1539
|
+
const old = this.paras;
|
|
1540
|
+
const oldLen = old.length;
|
|
1541
|
+
const newLen = model.length;
|
|
1542
|
+
const oldValid = this.validCount;
|
|
1543
|
+
const oldPending = this.pending;
|
|
1544
|
+
const prevPi = this.preeditPara;
|
|
1545
|
+
this.model = model;
|
|
1546
|
+
this.preedit = preedit;
|
|
1547
|
+
const pi = preedit ? findByStart(model, preedit.offset) : -1;
|
|
1548
|
+
this.preeditPara = pi;
|
|
1549
|
+
let lo;
|
|
1550
|
+
let s;
|
|
1551
|
+
if (changes) {
|
|
1552
|
+
lo = Math.min(changes.from, oldValid, oldLen, newLen);
|
|
1553
|
+
s = Math.min(changes.suffix, oldLen - lo, newLen - lo);
|
|
1554
|
+
const delta = newLen - oldLen;
|
|
1555
|
+
for (const [idx, isOld] of [[prevPi, true], [pi, false]]) {
|
|
1556
|
+
if (idx < 0) continue;
|
|
1557
|
+
let n = idx;
|
|
1558
|
+
if (isOld) {
|
|
1559
|
+
if (idx >= oldLen - s) n = idx + delta;
|
|
1560
|
+
else if (idx >= lo) continue;
|
|
1561
|
+
}
|
|
1562
|
+
if (n < lo) lo = Math.min(n, oldValid);
|
|
1563
|
+
if (n >= newLen - s) s = Math.max(0, newLen - n - 1);
|
|
1564
|
+
}
|
|
1565
|
+
s = Math.min(s, oldLen - lo, newLen - lo);
|
|
1566
|
+
} else {
|
|
1567
|
+
const same = (o, n, idx) => o !== null && o.info === n && o.layout.preedit === null && idx !== pi;
|
|
1568
|
+
const lim = Math.min(oldLen, newLen);
|
|
1569
|
+
lo = 0;
|
|
1570
|
+
while (lo < lim && lo < oldValid && same(old[lo], model[lo], lo)) lo++;
|
|
1571
|
+
s = 0;
|
|
1572
|
+
while (s < lim - lo && same(old[oldLen - 1 - s], model[newLen - 1 - s], newLen - 1 - s)) s++;
|
|
1573
|
+
}
|
|
1574
|
+
const hi = newLen - s;
|
|
1575
|
+
const oldHi = oldLen - s;
|
|
1576
|
+
if (lo === hi && lo === oldHi) {
|
|
1577
|
+
this.run((_m, y) => y > syncUntilY);
|
|
1578
|
+
return;
|
|
1579
|
+
}
|
|
1580
|
+
if (newLen !== oldLen) lo = Math.min(lo, Math.max(0, Math.min(oldLen, newLen) - 1));
|
|
1581
|
+
const delta = newLen - oldLen;
|
|
1582
|
+
if (oldPending === null) this.staleFrom = Infinity;
|
|
1583
|
+
else if (oldPending.next >= oldHi) this.staleFrom = oldPending.next + delta;
|
|
1584
|
+
else {
|
|
1585
|
+
const oldStale = this.staleFrom;
|
|
1586
|
+
this.staleFrom = Math.max(hi, oldStale >= oldHi ? oldStale + delta : hi);
|
|
1587
|
+
}
|
|
1588
|
+
while (lo > 0 && old[lo - 1]?.layout.box?.keep?.next) lo--;
|
|
1589
|
+
for (let k = lo; k < oldHi; k++) {
|
|
1590
|
+
const o = old[k];
|
|
1591
|
+
if (o) this.geo.floats.remove(o);
|
|
1592
|
+
}
|
|
1593
|
+
spliceFill(old, lo, oldHi - lo, hi - lo);
|
|
1594
|
+
const prev = lo > 0 ? old[lo - 1] : null;
|
|
1595
|
+
this.pending = {
|
|
1596
|
+
next: lo,
|
|
1597
|
+
page: prev ? prev.pageAfter : 0,
|
|
1598
|
+
y: prev ? prev.yAfter : 0,
|
|
1599
|
+
ns: prev ? prev.nsAfter : NO_NOTES
|
|
1600
|
+
};
|
|
1601
|
+
this.run((_m, y) => y > syncUntilY);
|
|
1602
|
+
}
|
|
1603
|
+
/** 남은 배치를 시간 예산만큼 진행한다. 남은 일이 있으면 true */
|
|
1604
|
+
step(budgetMs) {
|
|
1605
|
+
const end = performance.now() + budgetMs;
|
|
1606
|
+
this.run((m) => (m & 15) === 0 && performance.now() > end);
|
|
1607
|
+
return !this.complete;
|
|
1608
|
+
}
|
|
1609
|
+
/**
|
|
1610
|
+
* 문서 y까지 배치를 끝낸다. 시간 예산을 주면 그 안에서만 진행한다.
|
|
1611
|
+
* @returns y까지 배치가 끝났으면 true
|
|
1612
|
+
*/
|
|
1613
|
+
ensureThroughY(y, budgetMs = Infinity) {
|
|
1614
|
+
if (!this.pending) return true;
|
|
1615
|
+
if (budgetMs === Infinity) this.run((_m, top) => top > y);
|
|
1616
|
+
else {
|
|
1617
|
+
const end = performance.now() + budgetMs;
|
|
1618
|
+
let timedOut = false;
|
|
1619
|
+
this.run((m, top) => {
|
|
1620
|
+
if (top > y) return true;
|
|
1621
|
+
if ((m & 15) === 0 && performance.now() > end) timedOut = true;
|
|
1622
|
+
return timedOut;
|
|
1623
|
+
});
|
|
1624
|
+
if (timedOut) return false;
|
|
1625
|
+
}
|
|
1626
|
+
return true;
|
|
1627
|
+
}
|
|
1628
|
+
/** 정확하게 배치된 영역의 아래쪽 끝 (문서 y). 모두 배치되었으면 Infinity */
|
|
1629
|
+
get validBottom() {
|
|
1630
|
+
if (!this.pending) return Infinity;
|
|
1631
|
+
const v = this.validCount;
|
|
1632
|
+
if (v === 0) return 0;
|
|
1633
|
+
const last = this.paras[v - 1];
|
|
1634
|
+
if (this.frames.multi?.()) return this.frames.page(this.frames.frame(last.pageAfter).page).top;
|
|
1635
|
+
return last.bottom;
|
|
1636
|
+
}
|
|
1637
|
+
/** 문단 p까지 배치를 끝낸다 */
|
|
1638
|
+
ensureThroughPara(p) {
|
|
1639
|
+
if (this.pending && this.pending.next <= p) this.run((m) => m > p);
|
|
1640
|
+
}
|
|
1641
|
+
finish() {
|
|
1642
|
+
if (this.pending) this.run(() => false);
|
|
1643
|
+
}
|
|
1644
|
+
/** 오프셋이 속한 문단 번호 (문단 끝 ¶ 위치 포함) */
|
|
1645
|
+
paraIndexAt(offset) {
|
|
1646
|
+
return findByStart(this.model, offset);
|
|
1647
|
+
}
|
|
1648
|
+
para(p) {
|
|
1649
|
+
this.ensureThroughPara(p);
|
|
1650
|
+
return this.paras[p];
|
|
1651
|
+
}
|
|
1652
|
+
lineAt(offset) {
|
|
1653
|
+
const p = this.paraIndexAt(offset);
|
|
1654
|
+
const para = this.para(p);
|
|
1655
|
+
const d = docToDisplay(para, offset);
|
|
1656
|
+
const lines = para.lines;
|
|
1657
|
+
for (let k = 0; k < lines.length; k++) if (d < lines[k].end || k === lines.length - 1) return {
|
|
1658
|
+
para: p,
|
|
1659
|
+
line: k
|
|
1660
|
+
};
|
|
1661
|
+
return {
|
|
1662
|
+
para: p,
|
|
1663
|
+
line: 0
|
|
1664
|
+
};
|
|
1665
|
+
}
|
|
1666
|
+
line(ref) {
|
|
1667
|
+
return this.para(ref.para).lines[ref.line];
|
|
1668
|
+
}
|
|
1669
|
+
/** 다음/이전 줄 (문단 경계를 넘음). 범위를 벗어나면 null */
|
|
1670
|
+
stepLine(ref, dir) {
|
|
1671
|
+
let { para, line } = ref;
|
|
1672
|
+
let n = Math.abs(dir);
|
|
1673
|
+
const s = Math.sign(dir);
|
|
1674
|
+
while (n-- > 0) {
|
|
1675
|
+
line += s;
|
|
1676
|
+
if (line < 0) {
|
|
1677
|
+
para--;
|
|
1678
|
+
if (para < 0) return null;
|
|
1679
|
+
line = this.para(para).lines.length - 1;
|
|
1680
|
+
} else if (line >= this.para(para).lines.length) {
|
|
1681
|
+
para++;
|
|
1682
|
+
if (para >= this.model.length) return null;
|
|
1683
|
+
line = 0;
|
|
1684
|
+
}
|
|
1685
|
+
}
|
|
1686
|
+
return {
|
|
1687
|
+
para,
|
|
1688
|
+
line
|
|
1689
|
+
};
|
|
1690
|
+
}
|
|
1691
|
+
firstLineBelow(y, ensure = true) {
|
|
1692
|
+
if (ensure) this.ensureThroughY(y);
|
|
1693
|
+
else if (y >= this.validBottom || this.validCount === 0) return null;
|
|
1694
|
+
if (this.frames.multi?.()) return this.firstLineBelowPaged(y);
|
|
1695
|
+
const ps = this.paras;
|
|
1696
|
+
let lo = 0;
|
|
1697
|
+
let hi = this.validCount - 1;
|
|
1698
|
+
while (lo < hi) {
|
|
1699
|
+
const mid = lo + hi >> 1;
|
|
1700
|
+
if (ps[mid].bottom <= y) lo = mid + 1;
|
|
1701
|
+
else hi = mid;
|
|
1702
|
+
}
|
|
1703
|
+
const lines = ps[lo].lines;
|
|
1704
|
+
let k = 0;
|
|
1705
|
+
while (k < lines.length - 1 && lines[k].y + lines[k].height <= y) k++;
|
|
1706
|
+
return {
|
|
1707
|
+
para: lo,
|
|
1708
|
+
line: k
|
|
1709
|
+
};
|
|
1710
|
+
}
|
|
1711
|
+
/** 다단 (12 §7): y가 든 쪽에서 문서 순서로 처음 y 아래에 걸친 줄 (뒤 단의 줄은 그 뒤에 온다) */
|
|
1712
|
+
firstLineBelowPaged(y) {
|
|
1713
|
+
const F = this.frames;
|
|
1714
|
+
const ps = this.paras;
|
|
1715
|
+
const n = this.validCount;
|
|
1716
|
+
const P = this.pageAt(y);
|
|
1717
|
+
const pageOf = (k) => F.frame(k).page;
|
|
1718
|
+
let lo = 0;
|
|
1719
|
+
let hi = n - 1;
|
|
1720
|
+
while (lo < hi) {
|
|
1721
|
+
const mid = lo + hi >> 1;
|
|
1722
|
+
if (pageOf(ps[mid].pageAfter) < P) lo = mid + 1;
|
|
1723
|
+
else hi = mid;
|
|
1724
|
+
}
|
|
1725
|
+
for (let i = lo; i < n; i++) {
|
|
1726
|
+
const lines = ps[i].lines;
|
|
1727
|
+
for (let k = 0; k < lines.length; k++) {
|
|
1728
|
+
const ln = lines[k];
|
|
1729
|
+
if (ln.page > P || ln.page === P && ln.y + ln.height > y) return {
|
|
1730
|
+
para: i,
|
|
1731
|
+
line: k
|
|
1732
|
+
};
|
|
1733
|
+
}
|
|
1734
|
+
}
|
|
1735
|
+
const last = n - 1;
|
|
1736
|
+
return {
|
|
1737
|
+
para: last,
|
|
1738
|
+
line: ps[last].lines.length - 1
|
|
1739
|
+
};
|
|
1740
|
+
}
|
|
1741
|
+
/** 다단: 쪽에서 x가 든 단(가장 가까운 단)의 줄 중 y에 가장 가까운 줄 */
|
|
1742
|
+
lineAtPoint(x, y) {
|
|
1743
|
+
const F = this.frames;
|
|
1744
|
+
const P = this.pageAt(y);
|
|
1745
|
+
const cols = F.columnsOn?.(P) ?? [];
|
|
1746
|
+
if (!cols.length) return null;
|
|
1747
|
+
let inBand = cols.filter((c) => y >= c.frame.top && y < c.frame.top + c.frame.height);
|
|
1748
|
+
if (inBand.length) {
|
|
1749
|
+
const top = Math.max(...inBand.map((c) => c.frame.top));
|
|
1750
|
+
inBand = inBand.filter((c) => c.frame.top === top);
|
|
1751
|
+
}
|
|
1752
|
+
const pool = inBand.length ? inBand : cols;
|
|
1753
|
+
const dist = (c) => x < c.frame.x ? c.frame.x - x : x > c.frame.x + c.frame.width ? x - c.frame.x - c.frame.width : 0;
|
|
1754
|
+
const f = pool.reduce((a, b) => dist(b) < dist(a) ? b : a).frame;
|
|
1755
|
+
const ps = this.paras;
|
|
1756
|
+
const n = this.validCount;
|
|
1757
|
+
const pageOf = (k) => F.frame(k).page;
|
|
1758
|
+
let lo = 0;
|
|
1759
|
+
let hi = n - 1;
|
|
1760
|
+
while (lo < hi) {
|
|
1761
|
+
const mid = lo + hi >> 1;
|
|
1762
|
+
if (pageOf(ps[mid].pageAfter) < P) lo = mid + 1;
|
|
1763
|
+
else hi = mid;
|
|
1764
|
+
}
|
|
1765
|
+
let best = null;
|
|
1766
|
+
let bestD = Infinity;
|
|
1767
|
+
for (let i = lo; i < n; i++) {
|
|
1768
|
+
const pp = ps[i];
|
|
1769
|
+
if (pageOf(pp.pageBefore) > P) break;
|
|
1770
|
+
const lines = pp.lines;
|
|
1771
|
+
for (let k = 0; k < lines.length; k++) {
|
|
1772
|
+
const ln = lines[k];
|
|
1773
|
+
if (ln.page !== P || ln.x < f.x - 1 || ln.x > f.x + f.width || ln.y < f.top - 1 || ln.y >= f.top + f.height) continue;
|
|
1774
|
+
const d = y < ln.y ? ln.y - y : y > ln.y + ln.height ? y - ln.y - ln.height : 0;
|
|
1775
|
+
if (d < bestD) {
|
|
1776
|
+
bestD = d;
|
|
1777
|
+
best = {
|
|
1778
|
+
para: i,
|
|
1779
|
+
line: k
|
|
1780
|
+
};
|
|
1781
|
+
}
|
|
1782
|
+
}
|
|
1783
|
+
}
|
|
1784
|
+
return best;
|
|
1785
|
+
}
|
|
1786
|
+
caretRect(offset) {
|
|
1787
|
+
const ref = this.lineAt(offset);
|
|
1788
|
+
const para = this.paras[ref.para];
|
|
1789
|
+
const ln = para.lines[ref.line];
|
|
1790
|
+
const d = Math.min(docToDisplay(para, offset), ln.end);
|
|
1791
|
+
return {
|
|
1792
|
+
page: ln.page,
|
|
1793
|
+
x: lineX(para.layout, ln, d),
|
|
1794
|
+
y: ln.y,
|
|
1795
|
+
height: ln.height,
|
|
1796
|
+
ref
|
|
1797
|
+
};
|
|
1798
|
+
}
|
|
1799
|
+
/** 줄 안에서 x에 가장 가까운 캐럿 오프셋 */
|
|
1800
|
+
offsetInLine(ref, x) {
|
|
1801
|
+
const para = this.para(ref.para);
|
|
1802
|
+
const ln = para.lines[ref.line];
|
|
1803
|
+
const { stops } = para.layout;
|
|
1804
|
+
const maxD = ref.line === para.lines.length - 1 ? ln.end : ln.end - 1;
|
|
1805
|
+
let best = ln.start;
|
|
1806
|
+
let bestDist = Infinity;
|
|
1807
|
+
for (let d = ln.start; d <= Math.max(ln.start, maxD); d++) {
|
|
1808
|
+
if (!stops[d]) continue;
|
|
1809
|
+
const dist = Math.abs(lineX(para.layout, ln, d) - x);
|
|
1810
|
+
if (dist < bestDist) {
|
|
1811
|
+
bestDist = dist;
|
|
1812
|
+
best = d;
|
|
1813
|
+
}
|
|
1814
|
+
}
|
|
1815
|
+
return displayToDoc(para, best);
|
|
1816
|
+
}
|
|
1817
|
+
/** 문서 좌표 (쪽 왼쪽 기준 x, 문서 y) → 캐럿 오프셋 */
|
|
1818
|
+
hitTest(x, y) {
|
|
1819
|
+
if (this.frames.multi?.()) {
|
|
1820
|
+
this.ensureThroughY(y);
|
|
1821
|
+
const at = this.lineAtPoint(x, y);
|
|
1822
|
+
if (at) return this.offsetInLine(at, x);
|
|
1823
|
+
}
|
|
1824
|
+
let ref = this.firstLineBelow(y);
|
|
1825
|
+
const cur = this.line(ref);
|
|
1826
|
+
if (y < cur.y) {
|
|
1827
|
+
const prevRef = this.stepLine(ref, -1);
|
|
1828
|
+
if (prevRef) {
|
|
1829
|
+
const prev = this.line(prevRef);
|
|
1830
|
+
if (y - (prev.y + prev.height) < cur.y - y) ref = prevRef;
|
|
1831
|
+
}
|
|
1832
|
+
}
|
|
1833
|
+
return this.offsetInLine(ref, x);
|
|
1834
|
+
}
|
|
1835
|
+
/** 이전 grapheme 경계 (문단 경계를 넘으면 ¶ 한 칸) */
|
|
1836
|
+
prevStop(offset) {
|
|
1837
|
+
const para = this.para(this.paraIndexAt(offset));
|
|
1838
|
+
const start = para.info.start;
|
|
1839
|
+
const d = offset - start;
|
|
1840
|
+
if (d <= 0) return Math.max(0, offset - 1);
|
|
1841
|
+
const stops = para.layout.preedit ? null : para.layout.stops;
|
|
1842
|
+
let k = d - 1;
|
|
1843
|
+
while (stops && k > 0 && !stops[k]) k--;
|
|
1844
|
+
return start + k;
|
|
1845
|
+
}
|
|
1846
|
+
/** 다음 grapheme 경계 */
|
|
1847
|
+
nextStop(offset) {
|
|
1848
|
+
const para = this.para(this.paraIndexAt(offset));
|
|
1849
|
+
const start = para.info.start;
|
|
1850
|
+
const d = offset - start;
|
|
1851
|
+
if (d >= para.length) return offset + 1;
|
|
1852
|
+
const stops = para.layout.preedit ? null : para.layout.stops;
|
|
1853
|
+
let k = d + 1;
|
|
1854
|
+
while (stops && k < para.length && !stops[k]) k++;
|
|
1855
|
+
return start + k;
|
|
1856
|
+
}
|
|
1857
|
+
/** 수렴 판단: pp가 놓일 때 본 배제 영역이 지금도 같은가 (떠 있는 개체가 없는 쪽은 늘 같다) */
|
|
1858
|
+
sameExclusions(pp) {
|
|
1859
|
+
if (pp.exclSig === "" && !this.geo.floats.hasAny()) return true;
|
|
1860
|
+
return pp.exclSig === this.geo.floats.sig(pp, pp.pageBefore, pp.pageAfter);
|
|
1861
|
+
}
|
|
1862
|
+
/** 문서 y 범위 [y0, y1)에 걸친 쪽의 떠 있는 개체와 그 문단 */
|
|
1863
|
+
floatsIn(y0, y1) {
|
|
1864
|
+
const out = [];
|
|
1865
|
+
for (const pp of this.geo.floats.owners) for (const f of pp.floats) if (f.y < y1 && f.y + f.h > y0) out.push({
|
|
1866
|
+
para: pp,
|
|
1867
|
+
float: f
|
|
1868
|
+
});
|
|
1869
|
+
return out.sort((a, b) => a.para.info.start - b.para.info.start || a.float.k - b.float.k);
|
|
1870
|
+
}
|
|
1871
|
+
/**
|
|
1872
|
+
* 쪽 p의 각주 영역 (12 §6.2): 놓인 차례의 각주 조각들. 영역 높이 = 구분선 띠 + 조각 높이 합, 영역은 그 프레임 맨 아래에 놓인다.
|
|
1873
|
+
* 배치된 부분에서만 찾는다
|
|
1874
|
+
*/
|
|
1875
|
+
notesOnPage(p) {
|
|
1876
|
+
const out = [];
|
|
1877
|
+
const n = this.validCount;
|
|
1878
|
+
if (n === 0) return out;
|
|
1879
|
+
const F = this.geo.frames;
|
|
1880
|
+
const pg = (k) => F.frame(k).page;
|
|
1881
|
+
const ps = this.paras;
|
|
1882
|
+
let lo = 0;
|
|
1883
|
+
let hi = n - 1;
|
|
1884
|
+
while (lo < hi) {
|
|
1885
|
+
const mid = lo + hi >> 1;
|
|
1886
|
+
if (pg(ps[mid].pageAfter) < p) lo = mid + 1;
|
|
1887
|
+
else hi = mid;
|
|
1888
|
+
}
|
|
1889
|
+
for (let i = lo; i < n; i++) {
|
|
1890
|
+
const pp = ps[i];
|
|
1891
|
+
if (pg(pp.pageBefore) > p) break;
|
|
1892
|
+
if (!pp.mayHaveNotes) continue;
|
|
1893
|
+
for (const place of pp.notePlaces) if (pg(place.frame) === p) out.push(place);
|
|
1894
|
+
}
|
|
1895
|
+
return out;
|
|
1896
|
+
}
|
|
1897
|
+
/**
|
|
1898
|
+
* 다음 문단과 함께 (12 §4): 문단 m부터 keepWithNext 사슬 전체와 사슬 다음 문단의 첫 두 줄(표면 두 행)이 지금 프레임에 들어가지
|
|
1899
|
+
* 않으면 true (m을 다음 프레임 맨 위로). 사슬이 빈 프레임보다 크면 무시. 사슬은 앞에서 쪽 나눔·구역 시작에서 끊긴다
|
|
1900
|
+
*/
|
|
1901
|
+
keepNext(m, layout, page, y, fn = 0) {
|
|
1902
|
+
if (y <= 0 || !layout.box?.keep?.next) return false;
|
|
1903
|
+
const { setup } = this.geo;
|
|
1904
|
+
const H = this.geo.h;
|
|
1905
|
+
const sp = (L) => L.box ? L.box.before : setup.paraSpacing;
|
|
1906
|
+
const lines = (L, n) => {
|
|
1907
|
+
if (L.table) return L.table.rows.slice(0, n).reduce((a, b) => a + b, 0);
|
|
1908
|
+
let h = 0;
|
|
1909
|
+
const box = L.box;
|
|
1910
|
+
for (let k = 0; k < Math.min(n, L.lines.length); k++) h += lineHeight(setup, L.lines[k].scale, box?.line) + objExtra(setup, L.lines[k], box?.line);
|
|
1911
|
+
return h;
|
|
1912
|
+
};
|
|
1913
|
+
let need = 0;
|
|
1914
|
+
let L = layout;
|
|
1915
|
+
let j = m;
|
|
1916
|
+
for (let guard = 0; guard < 64; guard++) {
|
|
1917
|
+
need += (j === m && m === 0 ? 0 : sp(L)) + lines(L, Infinity) + (L.box?.after ?? 0);
|
|
1918
|
+
if (j + 1 >= this.model.length || this.frames.startsSection?.(j + 1)) break;
|
|
1919
|
+
const N = this.layoutFor(j + 1);
|
|
1920
|
+
if (N.box?.keep?.breakBefore) break;
|
|
1921
|
+
if (!N.box?.keep?.next) {
|
|
1922
|
+
need += sp(N) + lines(N, 2);
|
|
1923
|
+
break;
|
|
1924
|
+
}
|
|
1925
|
+
L = N;
|
|
1926
|
+
j++;
|
|
1927
|
+
}
|
|
1928
|
+
return y + need > H(page) - fn && need <= H(page + 1);
|
|
1929
|
+
}
|
|
1930
|
+
/**
|
|
1931
|
+
* 단 균형 (12 §7.2): 앞 구역 끝 쪽의 단 높이 상한을 이분 탐색으로 정한다 (그 쪽에 다 들어가는 가장 작은 높이). 그 쪽에 줄이 있는
|
|
1932
|
+
* 문단들만 다시 놓는다 (비용은 띠 크기 × 탐색 횟수). balance가 false면 지난 균형을 풀기만 한다
|
|
1933
|
+
*/
|
|
1934
|
+
balance(m, t) {
|
|
1935
|
+
const fs = this.frames;
|
|
1936
|
+
const paras = this.paras;
|
|
1937
|
+
const pageOf = (k) => fs.frame(k).page;
|
|
1938
|
+
const firstOn = (p) => {
|
|
1939
|
+
let i = m - 1;
|
|
1940
|
+
while (i > 0 && !fs.startsSection?.(i) && pageOf(paras[i - 1].pageAfter) >= p) i--;
|
|
1941
|
+
return i;
|
|
1942
|
+
};
|
|
1943
|
+
const replace = (i0) => {
|
|
1944
|
+
const s = paras[i0];
|
|
1945
|
+
let page = s.pageBefore;
|
|
1946
|
+
let y = s.yBefore;
|
|
1947
|
+
let ns = s.nsBefore;
|
|
1948
|
+
for (let i = i0; i < m; i++) {
|
|
1949
|
+
const pp = paras[i];
|
|
1950
|
+
pp.placeAt(page, y, i === 0, this.keepNext(i, pp.layout, page, y, ns.fn), ns, false);
|
|
1951
|
+
this.counts.repositioned++;
|
|
1952
|
+
page = pp.pageAfter;
|
|
1953
|
+
y = pp.yAfter;
|
|
1954
|
+
ns = pp.nsAfter;
|
|
1955
|
+
}
|
|
1956
|
+
return page;
|
|
1957
|
+
};
|
|
1958
|
+
fs.setCap(t.run, null);
|
|
1959
|
+
let i0 = firstOn(t.page);
|
|
1960
|
+
const end = replace(i0);
|
|
1961
|
+
const P = pageOf(end);
|
|
1962
|
+
if (!t.balance) return;
|
|
1963
|
+
const full = fs.frame(end).height;
|
|
1964
|
+
if (P !== t.page) i0 = firstOn(P);
|
|
1965
|
+
const fits = (h) => {
|
|
1966
|
+
fs.setCap(t.run, P, h);
|
|
1967
|
+
return pageOf(replace(i0)) === P;
|
|
1968
|
+
};
|
|
1969
|
+
let lo = 0;
|
|
1970
|
+
for (let i = i0; i < m; i++) for (const ln of paras[i].lines) if (ln.page === P) lo = Math.max(lo, Math.ceil(ln.height));
|
|
1971
|
+
let hi = Math.max(lo, Math.ceil(full));
|
|
1972
|
+
while (lo < hi) {
|
|
1973
|
+
const mid = lo + hi >> 1;
|
|
1974
|
+
if (fits(mid)) hi = mid;
|
|
1975
|
+
else lo = mid + 1;
|
|
1976
|
+
}
|
|
1977
|
+
fits(hi);
|
|
1978
|
+
}
|
|
1979
|
+
/** 문단 j의 줄 나눔 (미리 보기용: 배치된 것이 맞으면 그것, 아니면 캐시) */
|
|
1980
|
+
layoutFor(j) {
|
|
1981
|
+
const info = this.model[j];
|
|
1982
|
+
const pp = this.paras[j];
|
|
1983
|
+
const w = this.frames.widthOf ? this.frames.widthOf(j) : this.contentWidth;
|
|
1984
|
+
if (pp && pp.info === info && pp.layout.preedit === null && pp.width === w) return pp.layout;
|
|
1985
|
+
return this.cache.get(info.text, w, this.measurer, null, this.format ? this.format(info, false) : null);
|
|
1986
|
+
}
|
|
1987
|
+
/** pending에서 시작해 stop이 true가 될 때까지(또는 끝까지) 배치한다 */
|
|
1988
|
+
run(stop) {
|
|
1989
|
+
const { model, paras } = this;
|
|
1990
|
+
const len = model.length;
|
|
1991
|
+
while (this.pending) {
|
|
1992
|
+
let { next: m, page, y, ns } = this.pending;
|
|
1993
|
+
if (m >= len) {
|
|
1994
|
+
this.pending = null;
|
|
1995
|
+
this.staleFrom = Infinity;
|
|
1996
|
+
this.frames.done?.();
|
|
1997
|
+
break;
|
|
1998
|
+
}
|
|
1999
|
+
const fs = this.frames;
|
|
2000
|
+
const fr = fs.frame(page);
|
|
2001
|
+
if (stop(m, fs.multi?.() ? fs.page(fr.page).top : fr.top + y)) break;
|
|
2002
|
+
const bt = fs.balanceTarget?.(m, page);
|
|
2003
|
+
if (bt) {
|
|
2004
|
+
this.balance(m, bt);
|
|
2005
|
+
const last = paras[m - 1];
|
|
2006
|
+
page = last.pageAfter;
|
|
2007
|
+
y = last.yAfter;
|
|
2008
|
+
ns = last.nsAfter;
|
|
2009
|
+
}
|
|
2010
|
+
if (fs.enter) {
|
|
2011
|
+
const e = fs.enter(m, page, y);
|
|
2012
|
+
if (e) {
|
|
2013
|
+
if (e.frame !== page && this.geo.frames.frame(e.frame).page !== this.geo.frames.frame(page).page) ns = nextFrame(ns);
|
|
2014
|
+
page = e.frame;
|
|
2015
|
+
y = e.y;
|
|
2016
|
+
}
|
|
2017
|
+
}
|
|
2018
|
+
const width = fs.widthOf ? fs.widthOf(m) : this.contentWidth;
|
|
2019
|
+
const info = model[m];
|
|
2020
|
+
const pp = paras[m] ?? null;
|
|
2021
|
+
let layout;
|
|
2022
|
+
if (pp && pp.info === info && pp.layout.preedit === null && m !== this.preeditPara && pp.width === width) {
|
|
2023
|
+
if (m < this.staleFrom && pp.pageBefore === page && pp.yBefore === y && sameNotes(pp.nsBefore, ns) && pp.last === (m === len - 1) && this.sameExclusions(pp) && (fs.stable?.() ?? true)) {
|
|
2024
|
+
const until = Math.min(this.staleFrom, len);
|
|
2025
|
+
this.counts.reused += until - m;
|
|
2026
|
+
if (until >= len) {
|
|
2027
|
+
this.pending = null;
|
|
2028
|
+
this.staleFrom = Infinity;
|
|
2029
|
+
fs.done?.();
|
|
2030
|
+
break;
|
|
2031
|
+
}
|
|
2032
|
+
const last = paras[until - 1];
|
|
2033
|
+
this.pending = {
|
|
2034
|
+
next: until,
|
|
2035
|
+
page: last.pageAfter,
|
|
2036
|
+
y: last.yAfter,
|
|
2037
|
+
ns: last.nsAfter
|
|
2038
|
+
};
|
|
2039
|
+
this.staleFrom = until;
|
|
2040
|
+
continue;
|
|
2041
|
+
}
|
|
2042
|
+
pp.placeAt(page, y, m === 0, this.keepNext(m, pp.layout, page, y, ns.fn), ns, m === len - 1);
|
|
2043
|
+
this.counts.repositioned++;
|
|
2044
|
+
this.pending = {
|
|
2045
|
+
next: m + 1,
|
|
2046
|
+
page: pp.pageAfter,
|
|
2047
|
+
y: pp.yAfter,
|
|
2048
|
+
ns: pp.nsAfter
|
|
2049
|
+
};
|
|
2050
|
+
continue;
|
|
2051
|
+
} else {
|
|
2052
|
+
let pe = null;
|
|
2053
|
+
if (m === this.preeditPara && this.preedit) pe = {
|
|
2054
|
+
at: this.preedit.offset - info.start,
|
|
2055
|
+
text: this.preedit.text,
|
|
2056
|
+
fmt: this.preedit.fmt
|
|
2057
|
+
};
|
|
2058
|
+
const format = this.format ? this.format(info, pe?.fmt !== void 0) : null;
|
|
2059
|
+
layout = this.cache.get(info.text, width, this.measurer, pe, format);
|
|
2060
|
+
this.counts.reflowed++;
|
|
2061
|
+
}
|
|
2062
|
+
const length = pp && pp.info === info ? pp.length : layout.n - (layout.preedit ? layout.preedit[1] - layout.preedit[0] : 0);
|
|
2063
|
+
if (pp) this.geo.floats.remove(pp);
|
|
2064
|
+
const placed = new PlacedPara(info, length, layout, this.geo, width);
|
|
2065
|
+
placed.placeAt(page, y, m === 0, this.keepNext(m, layout, page, y, ns.fn), ns, m === len - 1);
|
|
2066
|
+
paras[m] = placed;
|
|
2067
|
+
this.pending = {
|
|
2068
|
+
next: m + 1,
|
|
2069
|
+
page: placed.pageAfter,
|
|
2070
|
+
y: placed.yAfter,
|
|
2071
|
+
ns: placed.nsAfter
|
|
2072
|
+
};
|
|
2073
|
+
}
|
|
2074
|
+
}
|
|
2075
|
+
};
|
|
2076
|
+
function displayToDoc(para, d) {
|
|
2077
|
+
const start = para.info.start;
|
|
2078
|
+
const pre = para.layout.preedit;
|
|
2079
|
+
if (!pre) return start + d;
|
|
2080
|
+
if (d <= pre[0]) return start + d;
|
|
2081
|
+
if (d >= pre[1]) return start + d - (pre[1] - pre[0]);
|
|
2082
|
+
return start + pre[0];
|
|
2083
|
+
}
|
|
2084
|
+
function docToDisplay(para, offset) {
|
|
2085
|
+
const d = offset - para.info.start;
|
|
2086
|
+
const pre = para.layout.preedit;
|
|
2087
|
+
if (pre && d >= pre[0]) return d + (pre[1] - pre[0]);
|
|
2088
|
+
return d;
|
|
2089
|
+
}
|
|
2090
|
+
/** start ≤ offset인 마지막 문단 */
|
|
2091
|
+
function findByStart(paras, offset) {
|
|
2092
|
+
let lo = 0;
|
|
2093
|
+
let hi = paras.length - 1;
|
|
2094
|
+
while (lo < hi) {
|
|
2095
|
+
const mid = lo + hi + 1 >> 1;
|
|
2096
|
+
if (paras[mid].start <= offset) lo = mid;
|
|
2097
|
+
else hi = mid - 1;
|
|
2098
|
+
}
|
|
2099
|
+
return lo;
|
|
2100
|
+
}
|
|
2101
|
+
/** arr[at, at+remove)를 null fill개로 바꾼다 (큰 배열에서도 인자 수 제한 없이) */
|
|
2102
|
+
function spliceFill(arr, at, remove, fill) {
|
|
2103
|
+
if (remove === fill) {
|
|
2104
|
+
for (let k = 0; k < fill; k++) arr[at + k] = null;
|
|
2105
|
+
return;
|
|
2106
|
+
}
|
|
2107
|
+
const tail = arr.slice(at + remove);
|
|
2108
|
+
arr.length = at;
|
|
2109
|
+
for (let k = 0; k < fill; k++) arr.push(null);
|
|
2110
|
+
for (let k = 0; k < tail.length; k++) arr.push(tail[k]);
|
|
2111
|
+
}
|
|
2112
|
+
// packages/layout/src/table.ts
|
|
2113
|
+
/**
|
|
2114
|
+
* 표 조판 (11 §4.3): 셀마다 쪽 하나(높이 무한)로 조판한 PagedLayout, 행 높이, 셀 자리.
|
|
2115
|
+
*
|
|
2116
|
+
* - 셀 내용은 스토리다 (모델의 가벼운 스토리). 셀 레이아웃은 셀 안쪽 폭으로 만들고, 폭이 바뀌면 새로 만든다.
|
|
2117
|
+
* - 행 높이 = 그 행에서 시작하는 한 행짜리 셀의 (내용 + 안쪽 여백) 가운데 가장 큰 것, 최소 높이 이상. 여러 행을 덮는 병합 셀은
|
|
2118
|
+
* 덮는 행 높이 합이 모자라면 마지막 행에 더한다.
|
|
2119
|
+
* - 병합으로 덮인 셀의 글은 옮기지 않으므로 (스파이크 2-7) 주인 셀 내용 아래에 이어 그린다 (비지 않은 것만).
|
|
2120
|
+
* - 셀 자리(원점)는 본문 레이아웃이 표 문단의 행을 놓은 뒤 place()로 정한다. 셀 레이아웃의 좌표는 본문과 같은 문서 좌표다.
|
|
2121
|
+
*
|
|
2122
|
+
* 모델에 의존하지 않도록 격자는 구조 타입으로 받는다 (편집기가 normalizeTable 결과를 px로 바꿔 준다).
|
|
2123
|
+
*/
|
|
2124
|
+
/** 셀 안쪽 여백 (px) */
|
|
2125
|
+
var CELL_PAD = 5;
|
|
2126
|
+
var TableLayouter = class {
|
|
2127
|
+
base;
|
|
2128
|
+
measurer;
|
|
2129
|
+
cache;
|
|
2130
|
+
source;
|
|
2131
|
+
format;
|
|
2132
|
+
/** 스토리 ID → 셀 레이아웃 */
|
|
2133
|
+
cells = /* @__PURE__ */ new Map();
|
|
2134
|
+
/** 행 쪼개기 결과 (셀 스토리 판·폭·용량 → 조각 높이) */
|
|
2135
|
+
splits = /* @__PURE__ */ new Map();
|
|
2136
|
+
constructor(base, measurer, cache, source, format) {
|
|
2137
|
+
this.base = base;
|
|
2138
|
+
this.measurer = measurer;
|
|
2139
|
+
this.cache = cache;
|
|
2140
|
+
this.source = source;
|
|
2141
|
+
this.format = format;
|
|
2142
|
+
}
|
|
2143
|
+
/** 셀 레이아웃 (없거나 폭이 바뀌었으면 만든다). 내용 변경을 반영한다 */
|
|
2144
|
+
cell(story, width, preedit = null) {
|
|
2145
|
+
let c = this.cells.get(story);
|
|
2146
|
+
const src = this.source(story);
|
|
2147
|
+
if (!c || c.width !== width) {
|
|
2148
|
+
const layout = new PagedLayout({
|
|
2149
|
+
...this.base,
|
|
2150
|
+
width,
|
|
2151
|
+
height: 1e9,
|
|
2152
|
+
margin: 0,
|
|
2153
|
+
gap: 0,
|
|
2154
|
+
originX: 0,
|
|
2155
|
+
originY: 0
|
|
2156
|
+
}, this.measurer, this.cache, this.format(story));
|
|
2157
|
+
c = {
|
|
2158
|
+
story,
|
|
2159
|
+
width,
|
|
2160
|
+
layout,
|
|
2161
|
+
view: layout,
|
|
2162
|
+
viewKey: "",
|
|
2163
|
+
preedit: null,
|
|
2164
|
+
version: -1,
|
|
2165
|
+
height: 0
|
|
2166
|
+
};
|
|
2167
|
+
this.cells.set(story, c);
|
|
2168
|
+
src.takeParagraphChanges();
|
|
2169
|
+
c.layout.update(src.paragraphs(), preedit, Infinity, null);
|
|
2170
|
+
} else {
|
|
2171
|
+
const v = src.version;
|
|
2172
|
+
if (v !== void 0 && !preedit && c.version === v) return c;
|
|
2173
|
+
c.layout.update(src.paragraphs(), preedit, Infinity, src.takeParagraphChanges());
|
|
2174
|
+
}
|
|
2175
|
+
c.version = preedit || src.version === void 0 ? -1 : src.version;
|
|
2176
|
+
c.preedit = preedit;
|
|
2177
|
+
c.height = c.layout.contentBottom;
|
|
2178
|
+
return c;
|
|
2179
|
+
}
|
|
2180
|
+
/** 셀 스토리를 주어진 프레임들에 흘린 레이아웃 */
|
|
2181
|
+
framed(story, width, frames, preedit) {
|
|
2182
|
+
const L = new PagedLayout({
|
|
2183
|
+
...this.base,
|
|
2184
|
+
width,
|
|
2185
|
+
height: 1e9,
|
|
2186
|
+
margin: 0,
|
|
2187
|
+
gap: 0,
|
|
2188
|
+
originX: 0,
|
|
2189
|
+
originY: 0
|
|
2190
|
+
}, this.measurer, this.cache, this.format(story), new ListFrames(frames.map((f) => ({
|
|
2191
|
+
...f,
|
|
2192
|
+
width
|
|
2193
|
+
}))));
|
|
2194
|
+
L.update(this.source(story).paragraphs(), preedit, Infinity, null);
|
|
2195
|
+
L.finish();
|
|
2196
|
+
return L;
|
|
2197
|
+
}
|
|
2198
|
+
/**
|
|
2199
|
+
* 행 쪼개기 (12 §8, TableSpec.split): 행 r의 셀마다 내용을 조각 용량들(caps)에 줄 단위로 흘려 조각마다 쓴 높이 (셀마다 가장 큰 것).
|
|
2200
|
+
* 내용이 있는 셀의 첫 줄이 첫 조각에 들어가지 않거나 덮인 셀 글이 있으면 null
|
|
2201
|
+
*/
|
|
2202
|
+
splitRow(grid, m, r, caps) {
|
|
2203
|
+
const row = grid.cells[r];
|
|
2204
|
+
if (!row) return null;
|
|
2205
|
+
let key = caps.join(",");
|
|
2206
|
+
for (let x = 0; x < row.length; x++) {
|
|
2207
|
+
const cell = row[x];
|
|
2208
|
+
if (!cell) continue;
|
|
2209
|
+
const v = this.source(cell.story).version;
|
|
2210
|
+
if (v === void 0) {
|
|
2211
|
+
key = "";
|
|
2212
|
+
break;
|
|
2213
|
+
}
|
|
2214
|
+
key += `|${cell.story}@${v}:${m.colX[x + cell.span[1]] - m.colX[x]}`;
|
|
2215
|
+
for (const s of cell.covered) key += `+${s}@${this.source(s).version}`;
|
|
2216
|
+
}
|
|
2217
|
+
const memo = key ? this.splits.get(key) : void 0;
|
|
2218
|
+
if (memo !== void 0) return memo;
|
|
2219
|
+
const out = this.splitRowFresh(row, m, caps);
|
|
2220
|
+
if (key) {
|
|
2221
|
+
if (this.splits.size > 4e3) this.splits.clear();
|
|
2222
|
+
this.splits.set(key, out);
|
|
2223
|
+
}
|
|
2224
|
+
return out;
|
|
2225
|
+
}
|
|
2226
|
+
splitRowFresh(row, m, caps) {
|
|
2227
|
+
const frames = caps.map((h, j) => ({
|
|
2228
|
+
x: 0,
|
|
2229
|
+
top: j * 1e7,
|
|
2230
|
+
height: Math.max(1, h)
|
|
2231
|
+
}));
|
|
2232
|
+
const used = [];
|
|
2233
|
+
for (let x = 0; x < row.length; x++) {
|
|
2234
|
+
const cell = row[x];
|
|
2235
|
+
if (!cell) continue;
|
|
2236
|
+
if (cell.covered.some((s) => !isEmpty(this.source(s)))) return null;
|
|
2237
|
+
if (isEmpty(this.source(cell.story))) continue;
|
|
2238
|
+
const w = Math.max(1, m.colX[x + cell.span[1]] - m.colX[x] - 10);
|
|
2239
|
+
const L = this.framed(cell.story, w, frames, null);
|
|
2240
|
+
for (let i = 0; i < L.paras.length; i++) for (const ln of L.paras[i].lines) {
|
|
2241
|
+
if (i === 0 && ln.index === 0 && ln.page !== 0) return null;
|
|
2242
|
+
used[ln.page] = Math.max(used[ln.page] ?? 0, ln.y + ln.height - frames[ln.page].top);
|
|
2243
|
+
}
|
|
2244
|
+
}
|
|
2245
|
+
for (let j = 0; j < used.length; j++) used[j] ??= 0;
|
|
2246
|
+
return used.length ? used : null;
|
|
2247
|
+
}
|
|
2248
|
+
/** 셀 내용 높이 (덮인 셀 포함) */
|
|
2249
|
+
contentHeight(cell, width, pre) {
|
|
2250
|
+
let h = this.cell(cell.story, width, pre && pre.story === cell.story ? pre.preedit : null).height;
|
|
2251
|
+
for (const s of cell.covered) {
|
|
2252
|
+
const c = this.cell(s, width);
|
|
2253
|
+
if (!isEmpty(this.source(s))) h += c.height;
|
|
2254
|
+
}
|
|
2255
|
+
return h;
|
|
2256
|
+
}
|
|
2257
|
+
/** 행 높이·열 자리를 구한다 (셀 레이아웃을 갱신한다) */
|
|
2258
|
+
measure(grid, minRow, pre = null) {
|
|
2259
|
+
const colX = [0];
|
|
2260
|
+
for (const w of grid.widths) colX.push(colX[colX.length - 1] + w);
|
|
2261
|
+
const nr = grid.cells.length;
|
|
2262
|
+
const rows = Array.from({ length: nr }, (_, y) => Math.max(minRow, grid.minHeights?.[y] ?? 0));
|
|
2263
|
+
const tall = [];
|
|
2264
|
+
for (let y = 0; y < nr; y++) {
|
|
2265
|
+
const row = grid.cells[y];
|
|
2266
|
+
for (let x = 0; x < row.length; x++) {
|
|
2267
|
+
const cell = row[x];
|
|
2268
|
+
if (!cell) continue;
|
|
2269
|
+
const w = colX[x + cell.span[1]] - colX[x] - 10;
|
|
2270
|
+
const h = this.contentHeight(cell, Math.max(1, w), pre) + 10;
|
|
2271
|
+
if (cell.span[0] === 1) rows[y] = Math.max(rows[y], h);
|
|
2272
|
+
else tall.push([
|
|
2273
|
+
y,
|
|
2274
|
+
cell.span[0],
|
|
2275
|
+
h
|
|
2276
|
+
]);
|
|
2277
|
+
}
|
|
2278
|
+
}
|
|
2279
|
+
for (const [y, n, h] of tall) {
|
|
2280
|
+
let sum = 0;
|
|
2281
|
+
for (let k = y; k < y + n; k++) sum += rows[k];
|
|
2282
|
+
if (sum < h) rows[y + n - 1] += h - sum;
|
|
2283
|
+
}
|
|
2284
|
+
return {
|
|
2285
|
+
rows,
|
|
2286
|
+
colX,
|
|
2287
|
+
width: colX[colX.length - 1]
|
|
2288
|
+
};
|
|
2289
|
+
}
|
|
2290
|
+
/**
|
|
2291
|
+
* 셀 원점을 정한다: lines는 본문 레이아웃이 놓은 표 문단의 행 (PlacedPara.lines). 여러 행 병합 셀은 첫 행 자리에 놓는다
|
|
2292
|
+
* (쪽을 넘으면 넘쳐 그린다)
|
|
2293
|
+
*/
|
|
2294
|
+
place(grid, m, lines) {
|
|
2295
|
+
const parts = rowParts(grid.cells.length, lines);
|
|
2296
|
+
for (let y = 0; y < grid.cells.length; y++) {
|
|
2297
|
+
const row = grid.cells[y];
|
|
2298
|
+
for (let x = 0; x < row.length; x++) {
|
|
2299
|
+
const cell = row[x];
|
|
2300
|
+
if (!cell) continue;
|
|
2301
|
+
const w = Math.max(1, m.colX[x + cell.span[1]] - m.colX[x] - 10);
|
|
2302
|
+
const only = cell.span[0] === 1 && parts[y].length === 1 ? parts[y][0] : null;
|
|
2303
|
+
if (only && !cell.covered.length) {
|
|
2304
|
+
const own = this.existing(cell.story, w);
|
|
2305
|
+
own.layout.setOrigin(only.x + m.colX[x] + 5, only.y + 5);
|
|
2306
|
+
own.view = own.layout;
|
|
2307
|
+
own.viewKey = "";
|
|
2308
|
+
continue;
|
|
2309
|
+
}
|
|
2310
|
+
const mine = [];
|
|
2311
|
+
for (let k = y; k < y + cell.span[0]; k++) mine.push(...parts[k] ?? []);
|
|
2312
|
+
if (!mine.length) continue;
|
|
2313
|
+
const ox = mine[0].x + m.colX[x] + 5;
|
|
2314
|
+
const own = this.existing(cell.story, w);
|
|
2315
|
+
const groups = cellGroups(mine);
|
|
2316
|
+
if (groups.length === 1) {
|
|
2317
|
+
let oy = mine[0].y + 5;
|
|
2318
|
+
own.layout.setOrigin(ox, oy);
|
|
2319
|
+
own.view = own.layout;
|
|
2320
|
+
own.viewKey = "";
|
|
2321
|
+
oy += own.height;
|
|
2322
|
+
for (const s of cell.covered) {
|
|
2323
|
+
if (isEmpty(this.source(s))) continue;
|
|
2324
|
+
const c = this.existing(s, w);
|
|
2325
|
+
c.layout.setOrigin(ox, oy);
|
|
2326
|
+
c.view = c.layout;
|
|
2327
|
+
oy += c.height;
|
|
2328
|
+
}
|
|
2329
|
+
continue;
|
|
2330
|
+
}
|
|
2331
|
+
const frames = groups.map((g) => ({
|
|
2332
|
+
x: g.x + m.colX[x] + 5,
|
|
2333
|
+
top: g.top + 5,
|
|
2334
|
+
height: Math.max(1, g.height - 10)
|
|
2335
|
+
}));
|
|
2336
|
+
const key = `${JSON.stringify(frames)}:${own.version}:${own.preedit ? JSON.stringify(own.preedit) : ""}`;
|
|
2337
|
+
if (own.viewKey !== key || own.view === own.layout) {
|
|
2338
|
+
own.view = this.framed(cell.story, w, frames, own.preedit);
|
|
2339
|
+
own.viewKey = key;
|
|
2340
|
+
}
|
|
2341
|
+
for (const s of cell.covered) {
|
|
2342
|
+
const c = this.existing(s, w);
|
|
2343
|
+
c.layout.setOrigin(ox, -1e9);
|
|
2344
|
+
c.view = c.layout;
|
|
2345
|
+
}
|
|
2346
|
+
}
|
|
2347
|
+
}
|
|
2348
|
+
}
|
|
2349
|
+
existing(story, width) {
|
|
2350
|
+
const c = this.cells.get(story);
|
|
2351
|
+
return c && c.width === width ? c : this.cell(story, width);
|
|
2352
|
+
}
|
|
2353
|
+
/** 더 쓰지 않는 셀 레이아웃을 버린다 (keep에 없는 스토리) */
|
|
2354
|
+
prune(keep) {
|
|
2355
|
+
for (const id of this.cells.keys()) if (!keep.has(id)) this.cells.delete(id);
|
|
2356
|
+
}
|
|
2357
|
+
};
|
|
2358
|
+
/** 행마다 그 행의 조각들 (다시 그린 제목 행 빼고). 행 번호가 없는 줄(옛 모양)은 줄 = 행 */
|
|
2359
|
+
function rowParts(rows, lines) {
|
|
2360
|
+
const out = Array.from({ length: rows }, () => []);
|
|
2361
|
+
lines.forEach((ln, k) => {
|
|
2362
|
+
if (ln.repeat) return;
|
|
2363
|
+
out[ln.row ?? k]?.push(ln);
|
|
2364
|
+
});
|
|
2365
|
+
return out;
|
|
2366
|
+
}
|
|
2367
|
+
/** 셀의 조각들을 이어진 것끼리 묶는다 (같은 쪽에서 바로 이어지면 하나): 그리기 사각형, 셀 프레임 */
|
|
2368
|
+
function cellGroups(parts) {
|
|
2369
|
+
const out = [];
|
|
2370
|
+
for (const p of parts) {
|
|
2371
|
+
const g = out[out.length - 1];
|
|
2372
|
+
if (g && g.page === p.page && g.x === p.x && Math.abs(g.top + g.height - p.y) < .5 && !(p.part && p.part > 0)) g.height = p.y + p.height - g.top;
|
|
2373
|
+
else out.push({
|
|
2374
|
+
page: p.page,
|
|
2375
|
+
x: p.x,
|
|
2376
|
+
top: p.y,
|
|
2377
|
+
height: p.height
|
|
2378
|
+
});
|
|
2379
|
+
}
|
|
2380
|
+
return out;
|
|
2381
|
+
}
|
|
2382
|
+
/** 주어진 사각형들을 차례로 프레임으로 (셀 조각, 12 §8). 다 쓰면 마지막 아래로 같은 크기를 잇는다 */
|
|
2383
|
+
var ListFrames = class {
|
|
2384
|
+
list;
|
|
2385
|
+
constructor(list) {
|
|
2386
|
+
this.list = list;
|
|
2387
|
+
}
|
|
2388
|
+
frame(k) {
|
|
2389
|
+
const n = this.list.length;
|
|
2390
|
+
const f = this.list[Math.min(k, n - 1)];
|
|
2391
|
+
if (k < n) return {
|
|
2392
|
+
page: k,
|
|
2393
|
+
column: 0,
|
|
2394
|
+
x: f.x,
|
|
2395
|
+
top: f.top,
|
|
2396
|
+
width: f.width,
|
|
2397
|
+
height: f.height
|
|
2398
|
+
};
|
|
2399
|
+
return {
|
|
2400
|
+
page: k,
|
|
2401
|
+
column: 0,
|
|
2402
|
+
x: f.x,
|
|
2403
|
+
top: f.top + (k - n + 1) * (f.height + 1),
|
|
2404
|
+
width: f.width,
|
|
2405
|
+
height: f.height
|
|
2406
|
+
};
|
|
2407
|
+
}
|
|
2408
|
+
page(p) {
|
|
2409
|
+
const f = this.frame(p);
|
|
2410
|
+
return {
|
|
2411
|
+
x: f.x,
|
|
2412
|
+
top: f.top,
|
|
2413
|
+
width: f.width,
|
|
2414
|
+
height: f.height
|
|
2415
|
+
};
|
|
2416
|
+
}
|
|
2417
|
+
pageAt(y) {
|
|
2418
|
+
let k = 0;
|
|
2419
|
+
while (k + 1 < this.list.length && this.list[k + 1].top <= y) k++;
|
|
2420
|
+
return k;
|
|
2421
|
+
}
|
|
2422
|
+
height(pages) {
|
|
2423
|
+
const f = this.frame(Math.max(0, pages - 1));
|
|
2424
|
+
return f.top + f.height;
|
|
2425
|
+
}
|
|
2426
|
+
};
|
|
2427
|
+
/** 빈 스토리 (빈 문단 하나) */
|
|
2428
|
+
function isEmpty(s) {
|
|
2429
|
+
const ps = s.paragraphs();
|
|
2430
|
+
return ps.length === 1 && ps[0].text === "";
|
|
2431
|
+
}
|
|
2432
|
+
// packages/layout/src/sections.ts
|
|
2433
|
+
/** 구역 모양 (px) → 키 */
|
|
2434
|
+
function geomKey(g) {
|
|
2435
|
+
const cols = (g.cols ?? 1) > 1 ? `|c${g.cols}/${g.gap ?? 0}${g.sep ? "/s" : ""}` : "";
|
|
2436
|
+
return `${g.width}x${g.height}|${g.top},${g.right},${g.bottom},${g.left}|${g.start}${cols}`;
|
|
2437
|
+
}
|
|
2438
|
+
/** 구역 모양의 단 폭 (px) */
|
|
2439
|
+
function columnWidth(g) {
|
|
2440
|
+
const cw = g.width - g.left - g.right;
|
|
2441
|
+
const n = g.cols ?? 1;
|
|
2442
|
+
return n <= 1 ? cw : (cw - (n - 1) * (g.gap ?? 0)) / n;
|
|
2443
|
+
}
|
|
2444
|
+
var SectionFrames = class {
|
|
2445
|
+
setup;
|
|
2446
|
+
source;
|
|
2447
|
+
runs = [];
|
|
2448
|
+
/** 이 차례부터의 런은 이번 배치에서 아직 다시 정하지 않았고 앞 런이 바뀌어 믿을 수 없다 (비교에만 쓴다) */
|
|
2449
|
+
dirtyFrom = Infinity;
|
|
2450
|
+
/** 지금 배치 중인 문단의 구역: 뒤 구역의 런은 아직 이번 배치에서 정하지 않았으므로 찾기에 쓰지 않는다 (다 배치하면 Infinity) */
|
|
2451
|
+
cur = Infinity;
|
|
2452
|
+
/** 다단 구역이 있다 (찾기를 쪽 단위로) */
|
|
2453
|
+
multiCols = false;
|
|
2454
|
+
constructor(setup, source) {
|
|
2455
|
+
this.setup = setup;
|
|
2456
|
+
this.source = source;
|
|
2457
|
+
}
|
|
2458
|
+
/** 찾기에 쓸 수 있는 런 수 */
|
|
2459
|
+
get live() {
|
|
2460
|
+
return Math.min(this.runs.length, this.dirtyFrom, this.cur + 1);
|
|
2461
|
+
}
|
|
2462
|
+
/** 런이 없을 때 (배치 전): 첫 구역 */
|
|
2463
|
+
first() {
|
|
2464
|
+
const r = this.runs[0];
|
|
2465
|
+
if (r) return r;
|
|
2466
|
+
const g = this.source.sectionOf(0).geom;
|
|
2467
|
+
return this.mk(0, g, 0, 0, 0, (this.setup.originY ?? 0) + this.setup.gap);
|
|
2468
|
+
}
|
|
2469
|
+
mk(index, geom, k0, p0, own, ownTop, shared = "", band = 0) {
|
|
2470
|
+
const cols = Math.max(1, geom.cols ?? 1);
|
|
2471
|
+
if (cols > 1) this.multiCols = true;
|
|
2472
|
+
return {
|
|
2473
|
+
index,
|
|
2474
|
+
geom,
|
|
2475
|
+
k0,
|
|
2476
|
+
p0,
|
|
2477
|
+
own,
|
|
2478
|
+
ownTop,
|
|
2479
|
+
shared,
|
|
2480
|
+
cols,
|
|
2481
|
+
gap: cols > 1 ? geom.gap ?? 0 : 0,
|
|
2482
|
+
band,
|
|
2483
|
+
cap: null,
|
|
2484
|
+
frames: []
|
|
2485
|
+
};
|
|
2486
|
+
}
|
|
2487
|
+
/** 프레임 k가 든 런 */
|
|
2488
|
+
runOfFrame(k) {
|
|
2489
|
+
const n = this.live;
|
|
2490
|
+
if (n === 0) return this.first();
|
|
2491
|
+
let lo = 0;
|
|
2492
|
+
let hi = n - 1;
|
|
2493
|
+
while (lo < hi) {
|
|
2494
|
+
const mid = lo + hi + 1 >> 1;
|
|
2495
|
+
if (this.runs[mid].k0 <= k) lo = mid;
|
|
2496
|
+
else hi = mid - 1;
|
|
2497
|
+
}
|
|
2498
|
+
return this.runs[lo];
|
|
2499
|
+
}
|
|
2500
|
+
/** 쪽 p의 주인 런 (쪽 모양을 정하는 구역) */
|
|
2501
|
+
ownerOf(p) {
|
|
2502
|
+
const n = this.live;
|
|
2503
|
+
if (n === 0) return this.first();
|
|
2504
|
+
let lo = 0;
|
|
2505
|
+
let hi = n - 1;
|
|
2506
|
+
while (lo < hi) {
|
|
2507
|
+
const mid = lo + hi + 1 >> 1;
|
|
2508
|
+
if (this.runs[mid].own <= p) lo = mid;
|
|
2509
|
+
else hi = mid - 1;
|
|
2510
|
+
}
|
|
2511
|
+
return this.runs[lo];
|
|
2512
|
+
}
|
|
2513
|
+
topOf(p) {
|
|
2514
|
+
const r = this.ownerOf(p);
|
|
2515
|
+
return r.ownTop + (p - r.own) * (r.geom.height + this.setup.gap);
|
|
2516
|
+
}
|
|
2517
|
+
pageX(g) {
|
|
2518
|
+
return (this.setup.originX ?? 0) + (this.setup.width - g.width) / 2;
|
|
2519
|
+
}
|
|
2520
|
+
frame(k) {
|
|
2521
|
+
const r = this.runOfFrame(k);
|
|
2522
|
+
const i = k - r.k0;
|
|
2523
|
+
let f = r.frames[i];
|
|
2524
|
+
if (f) return f;
|
|
2525
|
+
const n = r.cols;
|
|
2526
|
+
const page = r.p0 + (n === 1 ? i : Math.floor(i / n));
|
|
2527
|
+
const column = n === 1 ? 0 : i % n;
|
|
2528
|
+
const o = this.ownerOf(page).geom;
|
|
2529
|
+
const g = r.geom;
|
|
2530
|
+
let top = this.topOf(page) + o.top;
|
|
2531
|
+
let height = o.height - o.top - o.bottom;
|
|
2532
|
+
if (r.band > 0 && page === r.p0) {
|
|
2533
|
+
top += r.band;
|
|
2534
|
+
height -= r.band;
|
|
2535
|
+
}
|
|
2536
|
+
if (r.cap && r.cap.page === page) height = Math.min(height, r.cap.h);
|
|
2537
|
+
const w = columnWidth(g);
|
|
2538
|
+
f = {
|
|
2539
|
+
page,
|
|
2540
|
+
column,
|
|
2541
|
+
x: this.pageX(o) + g.left + (n === 1 ? 0 : column * (w + r.gap)),
|
|
2542
|
+
top,
|
|
2543
|
+
width: w,
|
|
2544
|
+
height
|
|
2545
|
+
};
|
|
2546
|
+
if (r.band > 0 && page === r.p0) f.band = true;
|
|
2547
|
+
if (i < 1 << 20) r.frames[i] = f;
|
|
2548
|
+
return f;
|
|
2549
|
+
}
|
|
2550
|
+
/** 다단 구역이 있는가 (PagedLayout이 쪽 단위로 찾는다) */
|
|
2551
|
+
multi() {
|
|
2552
|
+
return this.multiCols;
|
|
2553
|
+
}
|
|
2554
|
+
/** 쪽 p의 프레임들 (단, 띠마다)과 그 구역의 구분선 여부: 단 구분선·각주 영역 그리기 */
|
|
2555
|
+
columnsOn(p) {
|
|
2556
|
+
const out = [];
|
|
2557
|
+
const n = this.live;
|
|
2558
|
+
for (let j = 0; j < n; j++) {
|
|
2559
|
+
const r = this.runs[j];
|
|
2560
|
+
const next = j + 1 < n ? this.runs[j + 1] : null;
|
|
2561
|
+
if (r.p0 > p) break;
|
|
2562
|
+
const offset = (p - r.p0) * r.cols;
|
|
2563
|
+
for (let c = 0; c < r.cols; c++) {
|
|
2564
|
+
const k = r.k0 + offset + c;
|
|
2565
|
+
if (next && k >= next.k0) break;
|
|
2566
|
+
out.push({
|
|
2567
|
+
frame: this.frame(k),
|
|
2568
|
+
k,
|
|
2569
|
+
sep: !!r.geom.sep && r.cols > 1
|
|
2570
|
+
});
|
|
2571
|
+
}
|
|
2572
|
+
}
|
|
2573
|
+
return out.filter((x) => x.frame.page === p);
|
|
2574
|
+
}
|
|
2575
|
+
/**
|
|
2576
|
+
* 단 균형 (12 §7.2): 문단 m이 새 구역을 시작하는데, 같은 쪽에서 이어지는(continuous) 새 구역이고 앞 구역이 다단이면 앞 구역의
|
|
2577
|
+
* 끝 쪽(frame이 든 쪽)을 균형 맞출 대상으로. 앞 구역에 지난 균형이 남았으면 풀 대상으로 (balance false). full: 그 쪽 프레임 높이
|
|
2578
|
+
*/
|
|
2579
|
+
balanceTarget(m, frame) {
|
|
2580
|
+
if (m === 0) return null;
|
|
2581
|
+
const s = this.source.sectionOf(m);
|
|
2582
|
+
const prev = this.source.sectionOf(m - 1);
|
|
2583
|
+
if (prev.index === s.index) return null;
|
|
2584
|
+
this.cur = prev.index;
|
|
2585
|
+
const r = this.runs[prev.index];
|
|
2586
|
+
if (!r || prev.index >= this.live) return null;
|
|
2587
|
+
const page = this.frame(frame).page;
|
|
2588
|
+
const g = s.geom;
|
|
2589
|
+
const o = this.ownerOf(page).geom;
|
|
2590
|
+
const balance = r.cols > 1 && g.start === "continuous" && o.width === g.width && o.height === g.height;
|
|
2591
|
+
if (!balance && !r.cap) return null;
|
|
2592
|
+
let full = o.height - o.top - o.bottom;
|
|
2593
|
+
if (r.band > 0 && page === r.p0) full -= r.band;
|
|
2594
|
+
return {
|
|
2595
|
+
run: prev.index,
|
|
2596
|
+
page: r.cap ? Math.min(r.cap.page, page) : page,
|
|
2597
|
+
full,
|
|
2598
|
+
balance
|
|
2599
|
+
};
|
|
2600
|
+
}
|
|
2601
|
+
/** 구역 run의 쪽 page 프레임 높이 상한 (page null이면 푼다) */
|
|
2602
|
+
setCap(run, page, h = Infinity) {
|
|
2603
|
+
const r = this.runs[run];
|
|
2604
|
+
if (!r) return;
|
|
2605
|
+
r.cap = page === null ? null : {
|
|
2606
|
+
page,
|
|
2607
|
+
h
|
|
2608
|
+
};
|
|
2609
|
+
r.frames = [];
|
|
2610
|
+
}
|
|
2611
|
+
page(p) {
|
|
2612
|
+
const o = this.ownerOf(p).geom;
|
|
2613
|
+
return {
|
|
2614
|
+
x: this.pageX(o),
|
|
2615
|
+
top: this.topOf(p),
|
|
2616
|
+
width: o.width,
|
|
2617
|
+
height: o.height
|
|
2618
|
+
};
|
|
2619
|
+
}
|
|
2620
|
+
pageAt(y) {
|
|
2621
|
+
const n = this.live;
|
|
2622
|
+
if (n === 0) {
|
|
2623
|
+
const r = this.first();
|
|
2624
|
+
return Math.max(0, Math.floor((y - r.ownTop) / (r.geom.height + this.setup.gap)));
|
|
2625
|
+
}
|
|
2626
|
+
let lo = 0;
|
|
2627
|
+
let hi = n - 1;
|
|
2628
|
+
while (lo < hi) {
|
|
2629
|
+
const mid = lo + hi + 1 >> 1;
|
|
2630
|
+
if (this.runs[mid].ownTop <= y) lo = mid;
|
|
2631
|
+
else hi = mid - 1;
|
|
2632
|
+
}
|
|
2633
|
+
let r = this.runs[lo];
|
|
2634
|
+
while (lo > 0 && r.ownTop > y) r = this.runs[--lo];
|
|
2635
|
+
return Math.max(0, r.own + Math.floor((y - r.ownTop) / (r.geom.height + this.setup.gap)));
|
|
2636
|
+
}
|
|
2637
|
+
height(pages) {
|
|
2638
|
+
return this.topOf(pages);
|
|
2639
|
+
}
|
|
2640
|
+
/**
|
|
2641
|
+
* 쪽 p를 가진 구역 (쪽 모양을 정하는 구역, 12 §5.3): 차례, 그 구역의 첫 쪽, 다음 구역의 첫 쪽 (없으면 Infinity).
|
|
2642
|
+
* 짝·홀수 시작으로 넣은 빈 쪽은 새 구역의 쪽이다
|
|
2643
|
+
*/
|
|
2644
|
+
/** 구역 차례 i의 첫 쪽 (아직 놓지 않았으면 -1) */
|
|
2645
|
+
firstPageOf(i) {
|
|
2646
|
+
return i < this.live ? this.runs[i].own : -1;
|
|
2647
|
+
}
|
|
2648
|
+
pageOwner(p) {
|
|
2649
|
+
const r = this.ownerOf(p);
|
|
2650
|
+
const n = this.live;
|
|
2651
|
+
let end = Infinity;
|
|
2652
|
+
for (let j = this.runs.indexOf(r) + 1; j < n; j++) if (this.runs[j].own > r.own) {
|
|
2653
|
+
end = this.runs[j].own;
|
|
2654
|
+
break;
|
|
2655
|
+
}
|
|
2656
|
+
return {
|
|
2657
|
+
index: r.index,
|
|
2658
|
+
first: r.own,
|
|
2659
|
+
end
|
|
2660
|
+
};
|
|
2661
|
+
}
|
|
2662
|
+
startsSection(m) {
|
|
2663
|
+
return m > 0 && this.source.sectionOf(m - 1).index !== this.source.sectionOf(m).index;
|
|
2664
|
+
}
|
|
2665
|
+
widthOf(m) {
|
|
2666
|
+
return columnWidth(this.source.sectionOf(m).geom);
|
|
2667
|
+
}
|
|
2668
|
+
stable() {
|
|
2669
|
+
return this.dirtyFrom === Infinity;
|
|
2670
|
+
}
|
|
2671
|
+
/** 모두 배치했다: 없어진 구역의 런을 버린다 */
|
|
2672
|
+
done() {
|
|
2673
|
+
const n = this.source.count();
|
|
2674
|
+
if (this.runs.length > n) this.runs.length = n;
|
|
2675
|
+
this.multiCols = this.runs.some((r) => r.cols > 1);
|
|
2676
|
+
if (this.dirtyFrom >= this.runs.length) this.dirtyFrom = Infinity;
|
|
2677
|
+
this.cur = Infinity;
|
|
2678
|
+
}
|
|
2679
|
+
/**
|
|
2680
|
+
* 문단 m을 놓기 전: m이 구역의 첫 문단이면 그 구역의 런을 정하고 첫 배치 상태를 돌려준다. (frame, y)는 앞 문단이 끝난 상태
|
|
2681
|
+
*/
|
|
2682
|
+
enter(m, frame, y) {
|
|
2683
|
+
const s = this.source.sectionOf(m);
|
|
2684
|
+
if (m > 0 && this.source.sectionOf(m - 1).index === s.index) {
|
|
2685
|
+
this.cur = s.index;
|
|
2686
|
+
return null;
|
|
2687
|
+
}
|
|
2688
|
+
this.cur = s.index - 1;
|
|
2689
|
+
const g = s.geom;
|
|
2690
|
+
let run;
|
|
2691
|
+
let out;
|
|
2692
|
+
if (m === 0) {
|
|
2693
|
+
run = this.mk(s.index, g, 0, 0, 0, (this.setup.originY ?? 0) + this.setup.gap);
|
|
2694
|
+
out = {
|
|
2695
|
+
frame,
|
|
2696
|
+
y
|
|
2697
|
+
};
|
|
2698
|
+
} else {
|
|
2699
|
+
const prevPage = this.frame(frame).page;
|
|
2700
|
+
const o = this.ownerOf(prevPage).geom;
|
|
2701
|
+
const nextTop = this.topOf(prevPage) + o.height + this.setup.gap;
|
|
2702
|
+
const prevRun = this.runOfFrame(frame);
|
|
2703
|
+
const cols = Math.max(1, g.cols ?? 1);
|
|
2704
|
+
const fr = this.frame(frame);
|
|
2705
|
+
const bodyTop = this.topOf(prevPage) + o.top;
|
|
2706
|
+
const band = prevRun.cap && prevRun.cap.page === prevPage ? fr.top - bodyTop + prevRun.cap.h : fr.top - bodyTop + y;
|
|
2707
|
+
const continuous = g.start === "continuous" && o.width === g.width && o.height === g.height;
|
|
2708
|
+
if (continuous && prevRun.cols === 1 && cols === 1) {
|
|
2709
|
+
run = this.mk(s.index, g, frame + 1, prevPage, prevPage + 1, nextTop, o.key);
|
|
2710
|
+
out = {
|
|
2711
|
+
frame: frame + 1,
|
|
2712
|
+
y
|
|
2713
|
+
};
|
|
2714
|
+
} else if (continuous && band < o.height - o.top - o.bottom - 1) {
|
|
2715
|
+
run = this.mk(s.index, g, frame + 1, prevPage, prevPage + 1, nextTop, o.key, band);
|
|
2716
|
+
out = {
|
|
2717
|
+
frame: frame + 1,
|
|
2718
|
+
y: 0
|
|
2719
|
+
};
|
|
2720
|
+
} else {
|
|
2721
|
+
let p = prevPage + 1;
|
|
2722
|
+
if (g.start === "evenPage" && (p + 1) % 2 !== 0) p++;
|
|
2723
|
+
if (g.start === "oddPage" && (p + 1) % 2 !== 1) p++;
|
|
2724
|
+
run = this.mk(s.index, g, frame + 1, p, prevPage + 1, nextTop);
|
|
2725
|
+
out = {
|
|
2726
|
+
frame: frame + 1,
|
|
2727
|
+
y: 0
|
|
2728
|
+
};
|
|
2729
|
+
}
|
|
2730
|
+
}
|
|
2731
|
+
const old = this.runs[s.index];
|
|
2732
|
+
if (!!old && old.geom.key === g.key && old.k0 === run.k0 && old.p0 === run.p0 && old.own === run.own && old.ownTop === run.ownTop && old.shared === run.shared && old.band === run.band) this.dirtyFrom = Infinity;
|
|
2733
|
+
else {
|
|
2734
|
+
if (s.index <= this.runs.length) this.runs[s.index] = run;
|
|
2735
|
+
else this.runs.push(run);
|
|
2736
|
+
this.dirtyFrom = Math.min(s.index, this.runs.length - 1) + 1;
|
|
2737
|
+
}
|
|
2738
|
+
this.cur = s.index;
|
|
2739
|
+
return out;
|
|
2740
|
+
}
|
|
2741
|
+
};
|
|
2742
|
+
export { CELL_PAD, LayoutCache, NO_LINE_END, NO_LINE_START, NO_NOTES, OBJ_CHAR, PLAIN_STYLE, PagedLayout, SectionFrames, TableLayouter, UniformFrames, baselineOffset, breakFrom, cellGroups, charFormats, columnWidth, cpSlice, displayToDoc, docToDisplay, geomKey, hangingAt, layoutParagraph, lineHeight, lineMetrics, lineX, objExtra, pageTop, rowParts };
|
|
2743
|
+
|
|
2744
|
+
//# sourceMappingURL=index.js.map
|