pdf-skryba 1.0.0 → 1.0.1
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/CHANGELOG.md +22 -0
- package/dist/first-party/reader.js +177 -58
- package/dist/first-party/reader.js.map +3 -3
- package/dist/first-party.js +1 -0
- package/dist/first-party.js.map +2 -2
- package/dist/index.js +177 -58
- package/dist/index.js.map +3 -3
- package/dist/index.min.js +4 -4
- package/dist/light.js +1 -0
- package/dist/light.js.map +2 -2
- package/dist/light.min.js +1 -1
- package/dist/types/extractor/font-weight.d.ts +1 -6
- package/dist/types/extractor/pdf-bands.d.ts +1 -0
- package/dist/types/extractor/pdf-font-objects.d.ts +2 -3
- package/dist/types/extractor/pdf-glyph-source.d.ts +1 -1
- package/dist/types/extractor/pdf-hidden-text.d.ts +0 -4
- package/dist/types/extractor/pdf-lines.d.ts +2 -0
- package/dist/types/extractor/pdf-objects/refusal.d.ts +1 -1
- package/dist/types/extractor/pdf-ops-ours.d.ts +1 -0
- package/dist/types/extractor/pdf-page-plan.d.ts +4 -0
- package/dist/types/extractor/pdf-reading-order.d.ts +7 -0
- package/dist/types/extractor/pdf-source.d.ts +2 -0
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -11,6 +11,27 @@ The package was named `skryba` from 0.1.0 to 0.6.2; `skryba` stays frozen at 0.6
|
|
|
11
11
|
with the message « Renamed to pdf-skryba ». Before 0.1.0 the same reader was published as
|
|
12
12
|
`@miksumia/reader`, which the registry no longer serves; those versions are not reconstructed here.
|
|
13
13
|
|
|
14
|
+
## [1.0.1] - 2026-11-29
|
|
15
|
+
|
|
16
|
+
### Fixed
|
|
17
|
+
|
|
18
|
+
- A page that paints more than 100 000 glyphs kept no text at all (above 1 000 000 the whole document was refused); it
|
|
19
|
+
now keeps the text of its first 100 000 glyphs and carries the existing `pdf-text-unread` notice with reason
|
|
20
|
+
`limit`. No glyph past the bound is decoded.
|
|
21
|
+
- Work that a hostile page could make quadratic is now bounded or near-linear, with the same output on real pages:
|
|
22
|
+
the tie components of the reading order, the images of a table cell, the most frequent line start of a band, and
|
|
23
|
+
the drop-cap search (above 1 000 candidates in one call — 55 at most measured on 197 real PDFs — no drop cap is
|
|
24
|
+
lifted or seated).
|
|
25
|
+
- The glued-word repair no longer throws on an empty source-word key; such a token is left as is.
|
|
26
|
+
- Images inside a footnote, a section or a tab are now registered like the others (no such block reaches the
|
|
27
|
+
published output today).
|
|
28
|
+
- One rule decides « bold from the font » (the name says bold, black or heavy, or the declared weight does); the
|
|
29
|
+
narrower second rule, called by nothing in production, is removed.
|
|
30
|
+
|
|
31
|
+
### Removed
|
|
32
|
+
|
|
33
|
+
- `MAX_SHOWN_GLYPHS_PER_PAGE` and the refusal reason `glyphs-too-many`: the glyph cut above makes them unreachable.
|
|
34
|
+
|
|
14
35
|
## [1.0.0] - 2026-11-29
|
|
15
36
|
|
|
16
37
|
The first stable version. Every published name now follows one vocabulary: functions are verbs, booleans start
|
|
@@ -275,6 +296,7 @@ publishing. It is not a release.
|
|
|
275
296
|
|
|
276
297
|
[Unreleased]: https://github.com/numiastowski-design/pdf-skryba
|
|
277
298
|
|
|
299
|
+
[1.0.1]: https://www.npmjs.com/package/pdf-skryba/v/1.0.1
|
|
278
300
|
[1.0.0]: https://www.npmjs.com/package/pdf-skryba/v/1.0.0
|
|
279
301
|
[0.10.2]: https://www.npmjs.com/package/pdf-skryba/v/0.10.2
|
|
280
302
|
[0.10.1]: https://www.npmjs.com/package/pdf-skryba/v/0.10.1
|
|
@@ -1435,7 +1435,6 @@ var IS_BOUND_REFUSAL = {
|
|
|
1435
1435
|
"to-unicode": false,
|
|
1436
1436
|
"value-too-large": true,
|
|
1437
1437
|
"document-too-large": true,
|
|
1438
|
-
"glyphs-too-many": true,
|
|
1439
1438
|
"forms-too-many": true,
|
|
1440
1439
|
"font-tables-too-many": true,
|
|
1441
1440
|
"page-missing": false
|
|
@@ -7909,9 +7908,14 @@ function decodePathSubpaths(packed) {
|
|
|
7909
7908
|
return subpaths;
|
|
7910
7909
|
}
|
|
7911
7910
|
|
|
7911
|
+
// src/extractor/pdf-hidden-text.ts
|
|
7912
|
+
var MAX_FILTERED_GLYPHS = 1e5;
|
|
7913
|
+
var shouldDropModeHidden = (glyphs) => glyphs.some(
|
|
7914
|
+
(glyph) => glyph.hiddenReason === void 0 && glyph.isUnpainted === void 0 && !glyph.isSpace && glyph.text.trim() !== ""
|
|
7915
|
+
);
|
|
7916
|
+
|
|
7912
7917
|
// src/extractor/pdf-glyph-source.ts
|
|
7913
7918
|
var MAX_FORM_DEPTH = 8;
|
|
7914
|
-
var MAX_SHOWN_GLYPHS_PER_PAGE = 1e6;
|
|
7915
7919
|
var readName5 = (value) => value?.kind === "name" ? value.value : void 0;
|
|
7916
7920
|
var FALLBACK_FONT = /* @__PURE__ */ new Map([
|
|
7917
7921
|
["BaseFont", { kind: "name", value: "Helvetica" }],
|
|
@@ -7978,10 +7982,15 @@ function chargeFormCall(budget) {
|
|
|
7978
7982
|
var isHighSurrogate = (text) => text?.length === 1 && text.charCodeAt(0) >= 55296 && text.charCodeAt(0) <= 56319;
|
|
7979
7983
|
var isLowSurrogate = (text) => text?.length === 1 && text.charCodeAt(0) >= 56320 && text.charCodeAt(0) <= 57343;
|
|
7980
7984
|
function appendStringGlyphs(bytes, state, into, budget) {
|
|
7981
|
-
const
|
|
7985
|
+
const room = MAX_FILTERED_GLYPHS - budget.painted;
|
|
7986
|
+
const prefixLength = (Math.max(room, 0) + 2) * MAX_CODE_BYTES;
|
|
7987
|
+
const codes = state.codesOf(bytes.length > prefixLength ? bytes.subarray(0, prefixLength) : bytes);
|
|
7982
7988
|
for (const [index, code] of codes.entries()) {
|
|
7989
|
+
if (budget.painted >= MAX_FILTERED_GLYPHS) {
|
|
7990
|
+
budget.cut = true;
|
|
7991
|
+
return;
|
|
7992
|
+
}
|
|
7983
7993
|
budget.painted += 1;
|
|
7984
|
-
if (budget.painted > MAX_SHOWN_GLYPHS_PER_PAGE) throw new PdfObjectsRefusal("glyphs-too-many");
|
|
7985
7994
|
let unicode = state.text(code);
|
|
7986
7995
|
if (unicode === "") {
|
|
7987
7996
|
const ownHalf = state.declared(code);
|
|
@@ -7998,6 +8007,7 @@ function appendStringGlyphs(bytes, state, into, budget) {
|
|
|
7998
8007
|
});
|
|
7999
8008
|
}
|
|
8000
8009
|
}
|
|
8010
|
+
var MAX_CODE_BYTES = 4;
|
|
8001
8011
|
function applyGraphicsStateOperator(op, state, saved) {
|
|
8002
8012
|
if (op === "q") {
|
|
8003
8013
|
if (saved.length < GRAPHICS_STATE_MAX_DEPTH) saved.push(state);
|
|
@@ -8075,7 +8085,7 @@ function openGlyphSource(bytes) {
|
|
|
8075
8085
|
if (page === void 0) throw new PdfObjectsRefusal("page-missing");
|
|
8076
8086
|
const into = [];
|
|
8077
8087
|
walkContentStream(
|
|
8078
|
-
{ doc, into, memo, budget: { painted: 0, forms: 0 } },
|
|
8088
|
+
{ doc, into, memo, budget: { painted: 0, cut: false, forms: 0 } },
|
|
8079
8089
|
readContentOperators(pageContentOf(doc, page)),
|
|
8080
8090
|
page.resources,
|
|
8081
8091
|
0,
|
|
@@ -8282,7 +8292,7 @@ var verticalGlyphs = (glyphs) => glyphs.map((glyph) => ({ ...glyph, vmetric: tru
|
|
|
8282
8292
|
function readPageOperators(doc, page, memo = createGlyphMemo()) {
|
|
8283
8293
|
const out = [];
|
|
8284
8294
|
const bytes = pageContentOf(doc, page);
|
|
8285
|
-
const context = { doc, memo, budget: { painted: 0, forms: 0 } };
|
|
8295
|
+
const context = { doc, memo, budget: { painted: 0, cut: false, forms: 0 } };
|
|
8286
8296
|
emitContentStream(
|
|
8287
8297
|
context,
|
|
8288
8298
|
{
|
|
@@ -8295,8 +8305,10 @@ function readPageOperators(doc, page, memo = createGlyphMemo()) {
|
|
|
8295
8305
|
},
|
|
8296
8306
|
out
|
|
8297
8307
|
);
|
|
8308
|
+
if (context.budget.cut) out.push({ operator: GLYPHS_CUT_OPERATOR, operands: [] });
|
|
8298
8309
|
return out;
|
|
8299
8310
|
}
|
|
8311
|
+
var GLYPHS_CUT_OPERATOR = "glyphsCut";
|
|
8300
8312
|
function emitContentStream(context, frame, out) {
|
|
8301
8313
|
const builder = new PathBuilder();
|
|
8302
8314
|
for (const operator of frame.content instanceof Uint8Array ? readContentOperators(frame.content) : frame.content) {
|
|
@@ -9173,23 +9185,6 @@ function collapseOverpaint(glyphs) {
|
|
|
9173
9185
|
return collapsed;
|
|
9174
9186
|
}
|
|
9175
9187
|
|
|
9176
|
-
// src/extractor/pdf-hidden-text.ts
|
|
9177
|
-
var MAX_FILTERED_GLYPHS = 1e5;
|
|
9178
|
-
function countShownGlyphs(ops) {
|
|
9179
|
-
let glyphCount = 0;
|
|
9180
|
-
for (const { operator, operands } of ops) {
|
|
9181
|
-
if (operator !== "showText" && operator !== "showSpacedText") continue;
|
|
9182
|
-
const items = Array.isArray(operands) ? operands[0] : void 0;
|
|
9183
|
-
if (!Array.isArray(items)) continue;
|
|
9184
|
-
for (const item of items) if (typeof item !== "number") glyphCount += 1;
|
|
9185
|
-
if (glyphCount > MAX_FILTERED_GLYPHS) return glyphCount;
|
|
9186
|
-
}
|
|
9187
|
-
return glyphCount;
|
|
9188
|
-
}
|
|
9189
|
-
var shouldDropModeHidden = (glyphs) => glyphs.some(
|
|
9190
|
-
(glyph) => glyph.hiddenReason === void 0 && glyph.isUnpainted === void 0 && !glyph.isSpace && glyph.text.trim() !== ""
|
|
9191
|
-
);
|
|
9192
|
-
|
|
9193
9188
|
// src/layout/word-gap.ts
|
|
9194
9189
|
var WORD_GAP_EM = 0.1;
|
|
9195
9190
|
|
|
@@ -9458,13 +9453,17 @@ var inkOfLine = (line) => {
|
|
|
9458
9453
|
};
|
|
9459
9454
|
};
|
|
9460
9455
|
var isOverhungBy = (cap, ink) => ink !== void 0 && ink.size * MIN_DROP_CAP_RATIO <= cap.fontSize && ink.y > cap.y && ink.y < cap.y + cap.fontSize && ink.left > cap.x;
|
|
9456
|
+
var MAX_DROP_CAP_CANDIDATES = 1e3;
|
|
9461
9457
|
var liftDropCaps = (lines) => {
|
|
9462
9458
|
const out = lines.map((line) => [...line]);
|
|
9463
9459
|
const inks = out.map(inkOfLine);
|
|
9460
|
+
let candidates = 0;
|
|
9464
9461
|
for (const [lineIndex, line] of out.entries()) {
|
|
9465
9462
|
for (const cap of [...line]) {
|
|
9466
9463
|
if (!isDropCap(cap, out[lineIndex])) continue;
|
|
9467
|
-
|
|
9464
|
+
candidates += 1;
|
|
9465
|
+
if (candidates > MAX_DROP_CAP_CANDIDATES) return lines.map((kept) => [...kept]).filter(isNotEmpty);
|
|
9466
|
+
const target = firstOverhungLine(inks, lineIndex, cap);
|
|
9468
9467
|
if (target < 0) continue;
|
|
9469
9468
|
out[lineIndex] = out[lineIndex].filter((item) => item !== cap);
|
|
9470
9469
|
out[target].unshift(cap);
|
|
@@ -9472,8 +9471,13 @@ var liftDropCaps = (lines) => {
|
|
|
9472
9471
|
inks[target] = inkOfLine(out[target]);
|
|
9473
9472
|
}
|
|
9474
9473
|
}
|
|
9475
|
-
return out.filter(
|
|
9474
|
+
return out.filter(isNotEmpty);
|
|
9475
|
+
};
|
|
9476
|
+
var firstOverhungLine = (inks, end, cap) => {
|
|
9477
|
+
for (let index = 0; index < end; index++) if (isOverhungBy(cap, inks[index])) return index;
|
|
9478
|
+
return -1;
|
|
9476
9479
|
};
|
|
9480
|
+
var isNotEmpty = (line) => line.length > 0;
|
|
9477
9481
|
var trimBlankEnds = (group) => {
|
|
9478
9482
|
const isInk2 = (item) => item.text.trim().length > 0;
|
|
9479
9483
|
const first = group.findIndex(isInk2);
|
|
@@ -9581,6 +9585,16 @@ var linesFromItems = (pageItems, isBold, firstWordWidthAt) => linesFromGroups(gr
|
|
|
9581
9585
|
var READABLE = /[^\s\u0000-\u001F\u007F-\u009F]/;
|
|
9582
9586
|
var hasReadableText = (lines) => lines.some((line) => READABLE.test(line.text));
|
|
9583
9587
|
|
|
9588
|
+
// src/extractor/font-weight.ts
|
|
9589
|
+
function classifyFontWeight(weightValue, name) {
|
|
9590
|
+
if (weightValue !== void 0 && Number.isFinite(weightValue) && weightValue >= 100 && weightValue <= 900) {
|
|
9591
|
+
return weightValue >= 600 ? "bold" : "normal";
|
|
9592
|
+
}
|
|
9593
|
+
if (name === void 0) return "unknown";
|
|
9594
|
+
if (/bold|black|heavy/iu.test(name)) return "bold";
|
|
9595
|
+
return standardFontOf(name) !== void 0 ? "normal" : "unknown";
|
|
9596
|
+
}
|
|
9597
|
+
|
|
9584
9598
|
// src/extractor/pdf-font-objects.ts
|
|
9585
9599
|
var readName8 = (value) => value?.kind === "name" ? value.value : void 0;
|
|
9586
9600
|
var FONT_FILE_KEYS = ["FontFile", "FontFile2", "FontFile3"];
|
|
@@ -9595,14 +9609,6 @@ function readFontMatrix(doc, font) {
|
|
|
9595
9609
|
const numbers = matrix.items.map((item) => doc.resolve(item)).map((item) => item?.kind === "number" ? item.value : Number.NaN);
|
|
9596
9610
|
return numbers.length === 6 && numbers.every((value) => Number.isFinite(value)) ? numbers : void 0;
|
|
9597
9611
|
}
|
|
9598
|
-
function classifyFontWeight(weightValue, name) {
|
|
9599
|
-
if (weightValue !== void 0 && Number.isFinite(weightValue) && weightValue >= 100 && weightValue <= 900) {
|
|
9600
|
-
return weightValue >= 600 ? "bold" : "normal";
|
|
9601
|
-
}
|
|
9602
|
-
if (name === void 0) return "unknown";
|
|
9603
|
-
if (/bold|black|heavy/iu.test(name)) return "bold";
|
|
9604
|
-
return standardFontOf(name) !== void 0 ? "normal" : "unknown";
|
|
9605
|
-
}
|
|
9606
9612
|
function classifyItalic(flags, italicAngle, name) {
|
|
9607
9613
|
if (flags !== void 0 && Number.isFinite(flags) && (flags & 64) !== 0) return true;
|
|
9608
9614
|
if (italicAngle !== void 0 && Number.isFinite(italicAngle) && italicAngle !== 0) return true;
|
|
@@ -10524,6 +10530,7 @@ function readPageText(ctx, pageIndex) {
|
|
|
10524
10530
|
const pageObjectId = pageObjectIdOf(entry);
|
|
10525
10531
|
const [x0 = 0, y0 = 0, x1 = 0, y1 = 0] = entry.visibleBox;
|
|
10526
10532
|
const { glyphs, hide, fontUnread, glyphsUnavailable } = readPageGlyphs(ops, ctx, fontScaleOf, symbolFontOf);
|
|
10533
|
+
const cut = glyphsUnavailable === void 0 && ops.at(-1)?.operator === GLYPHS_CUT_OPERATOR;
|
|
10527
10534
|
const sameFont = createSameFontTest(fonts);
|
|
10528
10535
|
const runs = glyphsUnavailable === void 0 ? groupGlyphsIntoRuns(glyphs, {
|
|
10529
10536
|
pageObjectId,
|
|
@@ -10541,6 +10548,7 @@ function readPageText(ctx, pageIndex) {
|
|
|
10541
10548
|
glyphs: ctx.keepGlyphs ? glyphs : [],
|
|
10542
10549
|
...glyphsUnavailable !== void 0 ? { glyphsUnavailable } : {},
|
|
10543
10550
|
...fontUnread ? { fontUnread: true } : {},
|
|
10551
|
+
...cut ? { textCut: true } : {},
|
|
10544
10552
|
...unreadObjects > 0 ? { unreadObjects } : {},
|
|
10545
10553
|
runs,
|
|
10546
10554
|
reference
|
|
@@ -10550,7 +10558,12 @@ function readPageGlyphs(ops, ctx, fontScaleOf, symbolFontOf) {
|
|
|
10550
10558
|
try {
|
|
10551
10559
|
return { ...buildPageGlyphs(ops, ctx, fontScaleOf, symbolFontOf), glyphsUnavailable: void 0 };
|
|
10552
10560
|
} catch (error) {
|
|
10553
|
-
return {
|
|
10561
|
+
return {
|
|
10562
|
+
glyphs: [],
|
|
10563
|
+
hide: void 0,
|
|
10564
|
+
fontUnread: false,
|
|
10565
|
+
glyphsUnavailable: classifyTextUnreadReason(error)
|
|
10566
|
+
};
|
|
10554
10567
|
}
|
|
10555
10568
|
}
|
|
10556
10569
|
function createSameFontTest(fonts) {
|
|
@@ -10570,8 +10583,6 @@ function classifyTextUnreadReason(error) {
|
|
|
10570
10583
|
return "unexpected";
|
|
10571
10584
|
}
|
|
10572
10585
|
function buildPageGlyphs(ops, ctx, fontScaleOf, symbolFontOf) {
|
|
10573
|
-
if (countShownGlyphs(ops) > MAX_FILTERED_GLYPHS)
|
|
10574
|
-
throw new ReaderError4("FILE_UNSAFE", "the page has too many shown glyphs to be read");
|
|
10575
10586
|
const { candidate, namesLayer } = scanHiddenCandidates(ops);
|
|
10576
10587
|
const isLayerVisible = candidate ? createLayerVisibility(ctx, namesLayer) : void 0;
|
|
10577
10588
|
const all = readGlyphs(ops, isLayerVisible, fontScaleOf, symbolFontOf);
|
|
@@ -10626,6 +10637,7 @@ function readPageSource(text, pageIndex, wants, ctx) {
|
|
|
10626
10637
|
glyphs,
|
|
10627
10638
|
...text.glyphsUnavailable !== void 0 ? { glyphsUnavailableReason: text.glyphsUnavailable } : {},
|
|
10628
10639
|
...text.fontUnread === true ? { hasUnreadFont: true } : {},
|
|
10640
|
+
...text.textCut === true ? { hasCutText: true } : {},
|
|
10629
10641
|
...text.unreadObjects !== void 0 ? { unreadObjects: text.unreadObjects } : {},
|
|
10630
10642
|
...declaredOrder.length > 0 ? { declaredOrder: hoistAgainstTree(declaredOrder, runs) } : {},
|
|
10631
10643
|
...noteIds.size > 0 ? { noteIds } : {},
|
|
@@ -11537,7 +11549,9 @@ var groupNearbyCuts = (cuts) => {
|
|
|
11537
11549
|
};
|
|
11538
11550
|
var pickRepresentative = (group) => group[Math.floor(group.length / 2)];
|
|
11539
11551
|
var findLargestGroup = (groups) => {
|
|
11540
|
-
|
|
11552
|
+
let largest = [];
|
|
11553
|
+
for (const group of groups) if (group.length > largest.length) largest = group;
|
|
11554
|
+
return largest;
|
|
11541
11555
|
};
|
|
11542
11556
|
function findDocumentColumnCut(pages) {
|
|
11543
11557
|
const groups = groupNearbyCuts(pages.map(findColumnCut).filter((x) => x !== void 0));
|
|
@@ -13019,15 +13033,17 @@ function nestedBlockListsOf(block) {
|
|
|
13019
13033
|
return block.cells;
|
|
13020
13034
|
case "list":
|
|
13021
13035
|
return block.items;
|
|
13036
|
+
case "footnote":
|
|
13037
|
+
case "section":
|
|
13038
|
+
return [block.blocks];
|
|
13039
|
+
case "tabs":
|
|
13040
|
+
return block.tabs.map((tab) => tab.blocks);
|
|
13022
13041
|
case "heading":
|
|
13023
13042
|
case "paragraph":
|
|
13024
13043
|
case "image":
|
|
13025
13044
|
case "chart":
|
|
13026
|
-
case "footnote":
|
|
13027
13045
|
case "equation":
|
|
13028
13046
|
case "toc":
|
|
13029
|
-
case "section":
|
|
13030
|
-
case "tabs":
|
|
13031
13047
|
case "diagramText":
|
|
13032
13048
|
return [];
|
|
13033
13049
|
default: {
|
|
@@ -13150,16 +13166,38 @@ var sideWithoutBlankEnds = (group, isOnSide) => {
|
|
|
13150
13166
|
const last = items.length - 1 - [...items].reverse().findIndex(isInked);
|
|
13151
13167
|
return items.slice(first, last + 1);
|
|
13152
13168
|
};
|
|
13153
|
-
var
|
|
13154
|
-
const starts = segments.map((segment) => computeMinimum(segment, (item) => item.x));
|
|
13169
|
+
var mostFrequentStart = (starts) => {
|
|
13155
13170
|
const counts = /* @__PURE__ */ new Map();
|
|
13171
|
+
const buckets = /* @__PURE__ */ new Map();
|
|
13172
|
+
const metAt = /* @__PURE__ */ new Map();
|
|
13173
|
+
const bucketOf = (x) => Math.floor(x / SAME_LINE_PT);
|
|
13174
|
+
const firstKnownNear = (x) => {
|
|
13175
|
+
if (!Number.isFinite(x)) return void 0;
|
|
13176
|
+
let found;
|
|
13177
|
+
const low = bucketOf(x - SAME_LINE_PT) - 1;
|
|
13178
|
+
const high = bucketOf(x + SAME_LINE_PT) + 1;
|
|
13179
|
+
const seen = /* @__PURE__ */ new Set();
|
|
13180
|
+
for (let offset = 0; offset <= 4 && low + offset <= high; offset++) seen.add(low + offset);
|
|
13181
|
+
for (const bucket of seen)
|
|
13182
|
+
for (const known of buckets.get(bucket) ?? [])
|
|
13183
|
+
if (Math.abs(known - x) <= SAME_LINE_PT && (found === void 0 || metAt.get(known) < metAt.get(found)))
|
|
13184
|
+
found = known;
|
|
13185
|
+
return found;
|
|
13186
|
+
};
|
|
13156
13187
|
for (const x of starts) {
|
|
13157
|
-
const key =
|
|
13188
|
+
const key = firstKnownNear(x) ?? x;
|
|
13189
|
+
if (!counts.has(key) && Number.isFinite(key)) {
|
|
13190
|
+
metAt.set(key, metAt.size);
|
|
13191
|
+
const bucket = buckets.get(bucketOf(key));
|
|
13192
|
+
if (bucket === void 0) buckets.set(bucketOf(key), [key]);
|
|
13193
|
+
else bucket.push(key);
|
|
13194
|
+
}
|
|
13158
13195
|
counts.set(key, (counts.get(key) ?? 0) + 1);
|
|
13159
13196
|
}
|
|
13160
13197
|
const edge = findMode(counts, "smallest");
|
|
13161
13198
|
return edge !== void 0 && counts.get(edge) >= 2 ? edge : void 0;
|
|
13162
13199
|
};
|
|
13200
|
+
var columnEdge = (segments) => mostFrequentStart(segments.map((segment) => computeMinimum(segment, (item) => item.x)));
|
|
13163
13201
|
var crossesEdge = (group, edge) => edge !== void 0 && inked(group).some((item) => item.x < edge - SAME_LINE_PT && item.x + item.width > edge + SAME_LINE_PT);
|
|
13164
13202
|
var bandedSplit = (pageItems, cut, bound, {
|
|
13165
13203
|
standsAlone = false,
|
|
@@ -13395,24 +13433,87 @@ var tieBlocksOf = (entries) => {
|
|
|
13395
13433
|
}
|
|
13396
13434
|
return blocks;
|
|
13397
13435
|
};
|
|
13398
|
-
var
|
|
13399
|
-
|
|
13400
|
-
const parent =
|
|
13401
|
-
|
|
13402
|
-
|
|
13403
|
-
|
|
13404
|
-
|
|
13405
|
-
|
|
13406
|
-
|
|
13407
|
-
|
|
13436
|
+
var compareNumbers = (a, b) => a < b ? -1 : a > b ? 1 : 0;
|
|
13437
|
+
var tieOverlapComponents = (extents, threshold) => {
|
|
13438
|
+
const parent = extents.map((_, index) => index);
|
|
13439
|
+
const join = (a, b) => {
|
|
13440
|
+
parent[rootOf(parent, b)] = rootOf(parent, a);
|
|
13441
|
+
};
|
|
13442
|
+
const candidates = extents.flatMap((extent, index) => extent.right - extent.left > threshold ? [index] : []);
|
|
13443
|
+
const byLeft = [...candidates].sort((a, b) => compareNumbers(extents[a].left, extents[b].left));
|
|
13444
|
+
const byRight = [...candidates].sort((a, b) => compareNumbers(extents[a].right, extents[b].right));
|
|
13445
|
+
const inserted = new Uint8Array(extents.length);
|
|
13446
|
+
const merged = new Uint8Array(extents.length);
|
|
13447
|
+
const sweeps = /* @__PURE__ */ new Map();
|
|
13448
|
+
const activeSegments = /* @__PURE__ */ new Set();
|
|
13449
|
+
const sweepOf = (segment) => {
|
|
13450
|
+
let sweep = sweeps.get(segment);
|
|
13451
|
+
if (sweep === void 0) {
|
|
13452
|
+
sweep = { active: 0, loose: /* @__PURE__ */ new Set(), mergedActive: 0, mergedRep: -1 };
|
|
13453
|
+
sweeps.set(segment, sweep);
|
|
13454
|
+
}
|
|
13455
|
+
return sweep;
|
|
13456
|
+
};
|
|
13457
|
+
const expire = (gone) => {
|
|
13458
|
+
const sweep = sweepOf(extents[gone].segment);
|
|
13459
|
+
sweep.active--;
|
|
13460
|
+
if (merged[gone] === 1) sweep.mergedActive--;
|
|
13461
|
+
else sweep.loose.delete(gone);
|
|
13462
|
+
if (sweep.active === 0) activeSegments.delete(extents[gone].segment);
|
|
13463
|
+
};
|
|
13464
|
+
const anyActiveOf = (sweep) => {
|
|
13465
|
+
for (const line of sweep.loose) return line;
|
|
13466
|
+
return sweep.mergedRep;
|
|
13467
|
+
};
|
|
13468
|
+
let cursor = 0;
|
|
13469
|
+
for (const line of byLeft) {
|
|
13470
|
+
const { left, segment } = extents[line];
|
|
13471
|
+
while (cursor < byRight.length && !(extents[byRight[cursor]].right - left > threshold)) {
|
|
13472
|
+
const gone = byRight[cursor++];
|
|
13473
|
+
if (inserted[gone] === 1) expire(gone);
|
|
13474
|
+
}
|
|
13475
|
+
if (activeSegments.size >= 2) {
|
|
13476
|
+
for (const other of activeSegments)
|
|
13477
|
+
if (other !== segment) {
|
|
13478
|
+
join(anyActiveOf(sweepOf(other)), line);
|
|
13479
|
+
break;
|
|
13480
|
+
}
|
|
13481
|
+
} else {
|
|
13482
|
+
for (const other of activeSegments) {
|
|
13483
|
+
if (other === segment) continue;
|
|
13484
|
+
const sweep = sweepOf(other);
|
|
13485
|
+
if (sweep.mergedActive > 0) join(sweep.mergedRep, line);
|
|
13486
|
+
for (const looseLine of sweep.loose) {
|
|
13487
|
+
join(looseLine, line);
|
|
13488
|
+
merged[looseLine] = 1;
|
|
13489
|
+
sweep.mergedActive++;
|
|
13490
|
+
sweep.mergedRep = looseLine;
|
|
13491
|
+
}
|
|
13492
|
+
sweep.loose.clear();
|
|
13493
|
+
}
|
|
13408
13494
|
}
|
|
13495
|
+
const own = sweepOf(segment);
|
|
13496
|
+
own.active++;
|
|
13497
|
+
own.loose.add(line);
|
|
13498
|
+
activeSegments.add(segment);
|
|
13499
|
+
inserted[line] = 1;
|
|
13500
|
+
}
|
|
13409
13501
|
const byRoot = /* @__PURE__ */ new Map();
|
|
13410
|
-
|
|
13502
|
+
extents.forEach((_, index) => {
|
|
13411
13503
|
const root = rootOf(parent, index);
|
|
13412
|
-
|
|
13504
|
+
const component = byRoot.get(root);
|
|
13505
|
+
if (component === void 0) byRoot.set(root, [index]);
|
|
13506
|
+
else component.push(index);
|
|
13413
13507
|
});
|
|
13414
13508
|
return [...byRoot.values()];
|
|
13415
13509
|
};
|
|
13510
|
+
var tieComponents = (blocks) => {
|
|
13511
|
+
const members = blocks.flatMap((block, place) => block.ink === void 0 ? [] : [{ block, ink: block.ink, place }]);
|
|
13512
|
+
const extents = members.map((member) => ({ segment: member.block.segment, ...member.ink }));
|
|
13513
|
+
return tieOverlapComponents(extents, TIE_OVERLAP_THRESHOLD_PT).map(
|
|
13514
|
+
(component) => component.map((index) => members[index])
|
|
13515
|
+
);
|
|
13516
|
+
};
|
|
13416
13517
|
var reorderTie = (out, start, end) => {
|
|
13417
13518
|
const blocks = tieBlocksOf(out.slice(start, end));
|
|
13418
13519
|
const ordered = [...blocks];
|
|
@@ -13492,6 +13593,11 @@ var capsOfColumn = (lines, inks) => {
|
|
|
13492
13593
|
return found;
|
|
13493
13594
|
};
|
|
13494
13595
|
var seatDropCaps = (segments, slice) => {
|
|
13596
|
+
if (hasTooManyCandidates(segments))
|
|
13597
|
+
return {
|
|
13598
|
+
segments: segments.map((lines) => lines.filter((line) => line.length > 0).map((line) => [...line])),
|
|
13599
|
+
dropCaps: []
|
|
13600
|
+
};
|
|
13495
13601
|
const dropCaps = [];
|
|
13496
13602
|
const out = [];
|
|
13497
13603
|
segments.forEach((original, segment) => {
|
|
@@ -13518,6 +13624,16 @@ var seatDropCaps = (segments, slice) => {
|
|
|
13518
13624
|
});
|
|
13519
13625
|
return { segments: out, dropCaps };
|
|
13520
13626
|
};
|
|
13627
|
+
var hasTooManyCandidates = (segments) => {
|
|
13628
|
+
let candidates = 0;
|
|
13629
|
+
for (const lines of segments) {
|
|
13630
|
+
const smallestInk = smallestInkOf(lines.map(inkOfLine));
|
|
13631
|
+
for (const line of lines)
|
|
13632
|
+
for (const cap of line)
|
|
13633
|
+
if (isCapOfColumn(cap, line, smallestInk) && ++candidates > MAX_DROP_CAP_CANDIDATES) return true;
|
|
13634
|
+
}
|
|
13635
|
+
return false;
|
|
13636
|
+
};
|
|
13521
13637
|
var extentOf = (items) => items.length === 0 ? { top: void 0, bottom: void 0 } : { top: computeMaximum(items, (run) => run.y + run.height), bottom: computeMinimum(items, (run) => run.y) };
|
|
13522
13638
|
var segmentExtent = (lines) => {
|
|
13523
13639
|
const ink = lines.flat().filter((run) => run.text.trim() !== "");
|
|
@@ -14968,9 +15084,12 @@ function imagesPerCell(images, grid, merge) {
|
|
|
14968
15084
|
0,
|
|
14969
15085
|
Math.min(cols.length - 2, cols.findIndex((_, index) => index > 0 && x <= cols[index]) - 1)
|
|
14970
15086
|
);
|
|
14971
|
-
const
|
|
15087
|
+
const ownerIndex = merge?.ownerAt[row]?.[col];
|
|
15088
|
+
const owner = (ownerIndex === void 0 ? void 0 : merge?.cells[ownerIndex]) ?? { row, col };
|
|
14972
15089
|
const key = `${owner.row},${owner.col}`;
|
|
14973
|
-
|
|
15090
|
+
const cellImages = out.get(key);
|
|
15091
|
+
if (cellImages === void 0) out.set(key, [image]);
|
|
15092
|
+
else cellImages.push(image);
|
|
14974
15093
|
}
|
|
14975
15094
|
return out;
|
|
14976
15095
|
}
|
|
@@ -15605,7 +15724,7 @@ var toTextSourceRun = (run) => ({
|
|
|
15605
15724
|
var textUnreadReasons = (source, ocrLayerPageIndices) => {
|
|
15606
15725
|
const textUnreadByPage = /* @__PURE__ */ new Map();
|
|
15607
15726
|
source.pages.forEach((page, pageIndex) => {
|
|
15608
|
-
const reason = page.glyphsUnavailableReason ?? (page.hasUnreadFont === true && !ocrLayerPageIndices.includes(pageIndex) ? "font" : void 0);
|
|
15727
|
+
const reason = page.glyphsUnavailableReason ?? (page.hasCutText === true ? "limit" : void 0) ?? (page.hasUnreadFont === true && !ocrLayerPageIndices.includes(pageIndex) ? "font" : void 0);
|
|
15609
15728
|
if (reason !== void 0) textUnreadByPage.set(pageIndex, reason);
|
|
15610
15729
|
});
|
|
15611
15730
|
return textUnreadByPage;
|