@awacloud/pdf 0.0.0-stage → 1.0.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/CHANGELOG.md +609 -0
- package/LICENSE +661 -0
- package/NOTICE +77 -0
- package/README.md +363 -2
- package/dist/build/index.js +21 -0
- package/dist/build/pdf-full-rw.js +10972 -0
- package/dist/build/pdf-full-rw.meta.json +105 -0
- package/dist/build/pdf-full-rw.min.js +53 -0
- package/dist/build/pdf-full.js +6078 -0
- package/dist/build/pdf-full.meta.json +90 -0
- package/dist/build/pdf-full.min.js +32 -0
- package/dist/build/pdf-large-rw.js +10367 -0
- package/dist/build/pdf-large-rw.meta.json +99 -0
- package/dist/build/pdf-large-rw.min.js +53 -0
- package/dist/build/pdf-large.js +5473 -0
- package/dist/build/pdf-large.meta.json +84 -0
- package/dist/build/pdf-large.min.js +32 -0
- package/dist/build/pdf-legacy-rw.js +12402 -0
- package/dist/build/pdf-legacy-rw.meta.json +110 -0
- package/dist/build/pdf-legacy-rw.min.js +53 -0
- package/dist/build/pdf-legacy.js +7508 -0
- package/dist/build/pdf-legacy.meta.json +95 -0
- package/dist/build/pdf-legacy.min.js +32 -0
- package/dist/build/pdf-rw.js +7578 -0
- package/dist/build/pdf-rw.meta.json +77 -0
- package/dist/build/pdf-rw.min.js +53 -0
- package/dist/build/pdf.js +2684 -0
- package/dist/build/pdf.meta.json +62 -0
- package/dist/build/pdf.min.js +32 -0
- package/dist/standalone/pdf-full-rw.js +16798 -0
- package/dist/standalone/pdf-full-rw.meta.json +78 -0
- package/dist/standalone/pdf-full-rw.min.js +56 -0
- package/dist/standalone/pdf-full.js +11904 -0
- package/dist/standalone/pdf-full.meta.json +63 -0
- package/dist/standalone/pdf-full.min.js +35 -0
- package/dist/standalone/pdf-large-rw.js +16193 -0
- package/dist/standalone/pdf-large-rw.meta.json +72 -0
- package/dist/standalone/pdf-large-rw.min.js +56 -0
- package/dist/standalone/pdf-large.js +11299 -0
- package/dist/standalone/pdf-large.meta.json +57 -0
- package/dist/standalone/pdf-large.min.js +35 -0
- package/dist/standalone/pdf-legacy-rw.js +18228 -0
- package/dist/standalone/pdf-legacy-rw.meta.json +83 -0
- package/dist/standalone/pdf-legacy-rw.min.js +56 -0
- package/dist/standalone/pdf-legacy.js +13334 -0
- package/dist/standalone/pdf-legacy.meta.json +68 -0
- package/dist/standalone/pdf-legacy.min.js +35 -0
- package/dist/standalone/pdf-rw.js +13404 -0
- package/dist/standalone/pdf-rw.meta.json +50 -0
- package/dist/standalone/pdf-rw.min.js +56 -0
- package/dist/standalone/pdf.js +8510 -0
- package/dist/standalone/pdf.meta.json +35 -0
- package/dist/standalone/pdf.min.js +35 -0
- package/docs/README.md +53 -0
- package/docs/api/README.md +38 -0
- package/docs/api/_shared/README.md +91 -0
- package/docs/api/action/README.md +29 -0
- package/docs/api/action/action.md +81 -0
- package/docs/api/action/goTo.md +66 -0
- package/docs/api/action/launch.md +58 -0
- package/docs/api/action/named.md +55 -0
- package/docs/api/action/uri.md +54 -0
- package/docs/api/annot/README.md +53 -0
- package/docs/api/annot/annot.md +114 -0
- package/docs/api/annot/fileAttach.md +53 -0
- package/docs/api/annot/freeText.md +68 -0
- package/docs/api/annot/ink.md +69 -0
- package/docs/api/annot/link.md +74 -0
- package/docs/api/annot/markup.md +83 -0
- package/docs/api/annot/popup.md +52 -0
- package/docs/api/annot/projection.md +56 -0
- package/docs/api/annot/redact.md +67 -0
- package/docs/api/annot/square.md +87 -0
- package/docs/api/annot/stamp.md +54 -0
- package/docs/api/annot/text.md +69 -0
- package/docs/api/annot/widget.md +69 -0
- package/docs/api/associatedFiles/README.md +9 -0
- package/docs/api/associatedFiles/associatedFiles.md +78 -0
- package/docs/api/bundles/README.md +68 -0
- package/docs/api/bundles/dist-matrix.md +165 -0
- package/docs/api/bundles/pdf-full.md +148 -0
- package/docs/api/bundles/pdf-large.md +144 -0
- package/docs/api/bundles/pdf-legacy.md +169 -0
- package/docs/api/content/README.md +29 -0
- package/docs/api/content/color.md +99 -0
- package/docs/api/content/graphics.md +114 -0
- package/docs/api/content/images.md +124 -0
- package/docs/api/content/ops.md +100 -0
- package/docs/api/content/stream.md +107 -0
- package/docs/api/content/text.md +98 -0
- package/docs/api/crypto/README.md +29 -0
- package/docs/api/crypto/aesGcm.md +72 -0
- package/docs/api/crypto/permissions.md +79 -0
- package/docs/api/crypto/security.md +98 -0
- package/docs/api/crypto/standardV4.md +104 -0
- package/docs/api/crypto/standardV5.md +84 -0
- package/docs/api/crypto/standardV6.md +93 -0
- package/docs/api/destination/README.md +9 -0
- package/docs/api/destination/destination.md +79 -0
- package/docs/api/document/README.md +29 -0
- package/docs/api/document/builder.md +281 -0
- package/docs/api/document/catalog.md +98 -0
- package/docs/api/document/document.md +187 -0
- package/docs/api/document/encryptedWriter.md +149 -0
- package/docs/api/document/incrementalWriter.md +148 -0
- package/docs/api/document/page.md +99 -0
- package/docs/api/document/pages.md +82 -0
- package/docs/api/document/resources.md +102 -0
- package/docs/api/document/writer.md +157 -0
- package/docs/api/document/xrefStreamWriter.md +122 -0
- package/docs/api/embedded/README.md +13 -0
- package/docs/api/embedded/collection.md +80 -0
- package/docs/api/embedded/embeddedFile.md +86 -0
- package/docs/api/embedded/fileSpec.md +87 -0
- package/docs/api/errors.md +110 -0
- package/docs/api/extra/3d-richmedia.md +76 -0
- package/docs/api/extra/README.md +99 -0
- package/docs/api/extra/annot-extended.md +71 -0
- package/docs/api/extra/associated-files.md +70 -0
- package/docs/api/extra/ccitt-fax-decoder.md +74 -0
- package/docs/api/extra/color-spaces-extended.md +72 -0
- package/docs/api/extra/content-ops-extended.md +82 -0
- package/docs/api/extra/document-parts.md +69 -0
- package/docs/api/extra/embedded-files-portfolio.md +87 -0
- package/docs/api/extra/font-cid-typed.md +77 -0
- package/docs/api/extra/font-color-tagging.md +76 -0
- package/docs/api/extra/form-actions-extended.md +75 -0
- package/docs/api/extra/info-dict-deprecated.md +72 -0
- package/docs/api/extra/jbig2-read.md +80 -0
- package/docs/api/extra/legacy-deprecated-annots.md +89 -0
- package/docs/api/extra/legacy-deprecated-filters.md +78 -0
- package/docs/api/extra/legacy-rc4-read.md +74 -0
- package/docs/api/extra/legacy-xfa-read.md +65 -0
- package/docs/api/extra/linearization-write.md +71 -0
- package/docs/api/extra/misc.md +93 -0
- package/docs/api/extra/optional-content-extended.md +83 -0
- package/docs/api/extra/pdf-a-output-intent.md +65 -0
- package/docs/api/extra/pdf-sandbox.md +76 -0
- package/docs/api/extra/pdf-ua-tagged.md +63 -0
- package/docs/api/extra/pdf-x-prepress.md +65 -0
- package/docs/api/extra/redaction-iso32005.md +65 -0
- package/docs/api/extra/shading-typed.md +73 -0
- package/docs/api/extra/sig-aes-gcm.md +69 -0
- package/docs/api/extra/sig-pades.md +103 -0
- package/docs/api/extra/tagged-pdf-typed.md +78 -0
- package/docs/api/extra/transparency-typed.md +74 -0
- package/docs/api/extra/well-tagged-pdf.md +61 -0
- package/docs/api/extra/xmp-extended.md +65 -0
- package/docs/api/font/README.md +25 -0
- package/docs/api/font/embed.md +157 -0
- package/docs/api/font/encoding.md +95 -0
- package/docs/api/font/font.md +97 -0
- package/docs/api/font/type3.md +89 -0
- package/docs/api/form/README.md +35 -0
- package/docs/api/form/acroform.md +88 -0
- package/docs/api/form/appearance.md +87 -0
- package/docs/api/form/button.md +97 -0
- package/docs/api/form/choice.md +96 -0
- package/docs/api/form/fieldTree.md +93 -0
- package/docs/api/form/signature.md +90 -0
- package/docs/api/form/text.md +88 -0
- package/docs/api/linearization/README.md +11 -0
- package/docs/api/linearization/linearization.md +81 -0
- package/docs/api/main.md +116 -0
- package/docs/api/metadata/README.md +10 -0
- package/docs/api/metadata/info.md +70 -0
- package/docs/api/metadata/xmp.md +62 -0
- package/docs/api/ocg/README.md +23 -0
- package/docs/api/ocg/config.md +95 -0
- package/docs/api/ocg/ocg.md +77 -0
- package/docs/api/outline/README.md +11 -0
- package/docs/api/outline/outline.md +107 -0
- package/docs/api/pdf.md +152 -0
- package/docs/api/prepress/README.md +10 -0
- package/docs/api/prepress/outputIntent.md +79 -0
- package/docs/api/prepress/pageBoundary.md +75 -0
- package/docs/api/sig/README.md +32 -0
- package/docs/api/sig/byteRange.md +120 -0
- package/docs/api/sig/certChain.md +84 -0
- package/docs/api/sig/dss.md +111 -0
- package/docs/api/sig/oids.md +76 -0
- package/docs/api/sig/sha1.md +72 -0
- package/docs/api/sig/sign.md +317 -0
- package/docs/api/sig/signature.md +178 -0
- package/docs/api/sig/timestamp.md +84 -0
- package/docs/api/syntax/README.md +29 -0
- package/docs/api/syntax/crossRefStream.md +115 -0
- package/docs/api/syntax/filters/README.md +50 -0
- package/docs/api/syntax/filters/ascii85.md +76 -0
- package/docs/api/syntax/filters/asciiHex.md +73 -0
- package/docs/api/syntax/filters/dispatch.md +125 -0
- package/docs/api/syntax/filters/flate.md +134 -0
- package/docs/api/syntax/filters/runLength.md +78 -0
- package/docs/api/syntax/objStream.md +88 -0
- package/docs/api/syntax/parser-obj.md +97 -0
- package/docs/api/syntax/parser.md +151 -0
- package/docs/api/syntax/serializer.md +109 -0
- package/docs/api/syntax/tokenizer.md +104 -0
- package/docs/api/syntax/trailer.md +85 -0
- package/docs/api/syntax/xref.md +139 -0
- package/docs/api/tagged/README.md +25 -0
- package/docs/api/tagged/classMap.md +67 -0
- package/docs/api/tagged/markedContent.md +62 -0
- package/docs/api/tagged/parentTree.md +67 -0
- package/docs/api/tagged/roleMap.md +67 -0
- package/docs/api/tagged/structElement.md +76 -0
- package/docs/api/tagged/structTree.md +75 -0
- package/docs/guide/coverage.md +113 -0
- package/docs/guide/crypto.md +121 -0
- package/docs/guide/extending.md +76 -0
- package/docs/guide/getting-started.md +75 -0
- package/docs/guide/legacy-1.7.md +42 -0
- package/docs/guide/pades-integration.md +579 -0
- package/docs/guide/read-pdf.md +89 -0
- package/package.json +97 -4
- package/src/_shared/index.js +179 -0
- package/src/action/action.js +119 -0
- package/src/action/goTo.js +89 -0
- package/src/action/launch.js +61 -0
- package/src/action/named.js +54 -0
- package/src/action/uri.js +51 -0
- package/src/annot/annot.js +212 -0
- package/src/annot/fileAttach.js +55 -0
- package/src/annot/freeText.js +82 -0
- package/src/annot/ink.js +77 -0
- package/src/annot/link.js +77 -0
- package/src/annot/markup.js +91 -0
- package/src/annot/popup.js +53 -0
- package/src/annot/projection.js +52 -0
- package/src/annot/redact.js +87 -0
- package/src/annot/square.js +132 -0
- package/src/annot/stamp.js +48 -0
- package/src/annot/text.js +54 -0
- package/src/annot/widget.js +61 -0
- package/src/associatedFiles/associatedFiles.js +86 -0
- package/src/bundles/pdf-full.js +91 -0
- package/src/bundles/pdf-large.js +81 -0
- package/src/bundles/pdf-legacy.js +107 -0
- package/src/content/color.js +114 -0
- package/src/content/graphics.js +192 -0
- package/src/content/images.js +160 -0
- package/src/content/ops.js +137 -0
- package/src/content/stream.js +154 -0
- package/src/content/text.js +125 -0
- package/src/crypto/aesGcm.js +123 -0
- package/src/crypto/permissions.js +112 -0
- package/src/crypto/security.js +327 -0
- package/src/crypto/standardV4.js +443 -0
- package/src/crypto/standardV5.js +306 -0
- package/src/crypto/standardV6.js +334 -0
- package/src/destination/destination.js +183 -0
- package/src/document/builder.js +618 -0
- package/src/document/catalog.js +100 -0
- package/src/document/document.js +472 -0
- package/src/document/encryptedWriter.js +554 -0
- package/src/document/incrementalWriter.js +514 -0
- package/src/document/page.js +131 -0
- package/src/document/pages.js +103 -0
- package/src/document/resources.js +146 -0
- package/src/document/writer.js +211 -0
- package/src/document/xrefStreamWriter.js +353 -0
- package/src/embedded/collection.js +102 -0
- package/src/embedded/embeddedFile.js +99 -0
- package/src/embedded/fileSpec.js +137 -0
- package/src/errors.js +78 -0
- package/src/extra/3d-richmedia.js +171 -0
- package/src/extra/annot-extended.js +200 -0
- package/src/extra/associated-files.js +131 -0
- package/src/extra/ccitt-fax-decoder.js +776 -0
- package/src/extra/color-spaces-extended.js +196 -0
- package/src/extra/content-ops-extended.js +153 -0
- package/src/extra/document-parts.js +149 -0
- package/src/extra/embedded-files-portfolio.js +234 -0
- package/src/extra/font-cid-typed.js +185 -0
- package/src/extra/font-color-tagging.js +144 -0
- package/src/extra/form-actions-extended.js +196 -0
- package/src/extra/info-dict-deprecated.js +137 -0
- package/src/extra/jbig2-read.js +169 -0
- package/src/extra/legacy-deprecated-annots.js +198 -0
- package/src/extra/legacy-deprecated-filters.js +167 -0
- package/src/extra/legacy-rc4-read.js +235 -0
- package/src/extra/legacy-xfa-read.js +104 -0
- package/src/extra/linearization-write.js +97 -0
- package/src/extra/misc.js +217 -0
- package/src/extra/optional-content-extended.js +142 -0
- package/src/extra/pdf-a-output-intent.js +112 -0
- package/src/extra/pdf-sandbox.js +88 -0
- package/src/extra/pdf-ua-tagged.js +116 -0
- package/src/extra/pdf-x-prepress.js +114 -0
- package/src/extra/redaction-iso32005.js +136 -0
- package/src/extra/shading-typed.js +222 -0
- package/src/extra/sig-aes-gcm.js +135 -0
- package/src/extra/sig-pades.js +242 -0
- package/src/extra/tagged-pdf-typed.js +203 -0
- package/src/extra/transparency-typed.js +135 -0
- package/src/extra/well-tagged-pdf.js +138 -0
- package/src/extra/xmp-extended.js +190 -0
- package/src/font/embed.js +480 -0
- package/src/font/encoding.js +92 -0
- package/src/font/font.js +101 -0
- package/src/font/type3.js +75 -0
- package/src/form/acroform.js +94 -0
- package/src/form/appearance.js +90 -0
- package/src/form/button.js +105 -0
- package/src/form/choice.js +152 -0
- package/src/form/fieldTree.js +120 -0
- package/src/form/signature.js +100 -0
- package/src/form/text.js +101 -0
- package/src/linearization/linearization.js +107 -0
- package/src/main.js +411 -0
- package/src/metadata/info.js +87 -0
- package/src/metadata/xmp.js +62 -0
- package/src/ocg/config.js +156 -0
- package/src/ocg/ocg.js +124 -0
- package/src/outline/outline.js +157 -0
- package/src/pdf.js +133 -0
- package/src/prepress/outputIntent.js +118 -0
- package/src/prepress/pageBoundary.js +108 -0
- package/src/sig/byteRange.js +306 -0
- package/src/sig/certChain.js +247 -0
- package/src/sig/dss.js +317 -0
- package/src/sig/oids.js +157 -0
- package/src/sig/sha1.js +142 -0
- package/src/sig/sign.js +1899 -0
- package/src/sig/signature.js +1441 -0
- package/src/sig/timestamp.js +236 -0
- package/src/syntax/crossRefStream.js +133 -0
- package/src/syntax/filters/ascii85.js +122 -0
- package/src/syntax/filters/asciiHex.js +83 -0
- package/src/syntax/filters/dispatch.js +176 -0
- package/src/syntax/filters/flate.js +316 -0
- package/src/syntax/filters/runLength.js +96 -0
- package/src/syntax/objStream.js +99 -0
- package/src/syntax/parser-obj.js +52 -0
- package/src/syntax/parser.js +321 -0
- package/src/syntax/serializer.js +221 -0
- package/src/syntax/tokenizer.js +290 -0
- package/src/syntax/trailer.js +76 -0
- package/src/syntax/xref.js +341 -0
- package/src/tagged/classMap.js +81 -0
- package/src/tagged/markedContent.js +123 -0
- package/src/tagged/parentTree.js +126 -0
- package/src/tagged/roleMap.js +107 -0
- package/src/tagged/structElement.js +138 -0
- package/src/tagged/structTree.js +94 -0
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
// Copyright (c) 2026 AwaCloud SAS
|
|
2
|
+
// Author: Matthieu Bouilloux
|
|
3
|
+
// SPDX-License-Identifier: AGPL-3.0-only
|
|
4
|
+
// Dual-licensed; see the NOTICE file for licensing and any additional terms.
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @fileoverview RoleMap typing per ISO 32000-2:2020 §14.7.5 + namespace
|
|
8
|
+
* handling per §14.7.6 (PDF 2.0).
|
|
9
|
+
*
|
|
10
|
+
* `/RoleMap` maps custom structure-type names to standard ones. A chain
|
|
11
|
+
* lookup may be required if a custom role maps to another custom role.
|
|
12
|
+
* Cycles are detected and rejected.
|
|
13
|
+
*
|
|
14
|
+
* The PDF 2.0 namespace model adds `/Namespaces` and per-element `/NS`.
|
|
15
|
+
* A namespace dict may carry its own `/RoleMapNS` that takes precedence
|
|
16
|
+
* for elements within that namespace.
|
|
17
|
+
*
|
|
18
|
+
* @module pdf/tagged/roleMap
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Module factory.
|
|
23
|
+
*/
|
|
24
|
+
import { pdfErrors } from '../errors.js';
|
|
25
|
+
import { pdfParser } from '../syntax/parser.js';
|
|
26
|
+
|
|
27
|
+
export const pdfRoleMap = {
|
|
28
|
+
name: 'pdfRoleMap',
|
|
29
|
+
dependencies: ['pdfErrors', 'pdfParser'],
|
|
30
|
+
deps: [pdfErrors, pdfParser],
|
|
31
|
+
factory(errors, parser) {
|
|
32
|
+
const { ParseError } = errors;
|
|
33
|
+
const { isType } = parser;
|
|
34
|
+
|
|
35
|
+
const STANDARD_TYPES = new Set([
|
|
36
|
+
// Grouping
|
|
37
|
+
'Document', 'DocumentFragment', 'Part', 'Art', 'Sect', 'Div',
|
|
38
|
+
'BlockQuote', 'Caption', 'TOC', 'TOCI', 'Index', 'NonStruct',
|
|
39
|
+
'Private', 'Aside', 'Title', 'FENote',
|
|
40
|
+
// Paragraph-like
|
|
41
|
+
'P', 'H', 'H1', 'H2', 'H3', 'H4', 'H5', 'H6', 'H7',
|
|
42
|
+
// Lists
|
|
43
|
+
'L', 'LI', 'Lbl', 'LBody',
|
|
44
|
+
// Tables
|
|
45
|
+
'Table', 'TR', 'TH', 'TD', 'THead', 'TBody', 'TFoot',
|
|
46
|
+
// Inline
|
|
47
|
+
'Span', 'Quote', 'Note', 'Reference', 'BibEntry', 'Code',
|
|
48
|
+
'Link', 'Annot', 'Em', 'Strong',
|
|
49
|
+
// Ruby / Warichu
|
|
50
|
+
'Ruby', 'RB', 'RT', 'RP', 'Warichu', 'WT', 'WP',
|
|
51
|
+
// Illustration
|
|
52
|
+
'Figure', 'Formula', 'Form', 'Artifact'
|
|
53
|
+
]);
|
|
54
|
+
|
|
55
|
+
function typeRoleMap(dict) {
|
|
56
|
+
if (!isType(dict, 'dict')) {
|
|
57
|
+
throw new ParseError('pdf/tagged/role-map/not-dict',
|
|
58
|
+
'RoleMap must be a dictionary',
|
|
59
|
+
{ context: { type: dict && dict.type } });
|
|
60
|
+
}
|
|
61
|
+
const map = {};
|
|
62
|
+
for (const [k, v] of Object.entries(dict.entries)) {
|
|
63
|
+
if (!v || v.type !== 'name') {
|
|
64
|
+
throw new ParseError('pdf/tagged/role-map/bad-value',
|
|
65
|
+
'RoleMap values must be name objects',
|
|
66
|
+
{ context: { key: k, kind: v && v.type } });
|
|
67
|
+
}
|
|
68
|
+
map[k] = v.value;
|
|
69
|
+
}
|
|
70
|
+
return { map, raw: dict };
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function resolveStandardType(name, map, namespaceMap) {
|
|
74
|
+
const chain = [];
|
|
75
|
+
const seen = new Set();
|
|
76
|
+
let cur = name;
|
|
77
|
+
while (cur) {
|
|
78
|
+
if (seen.has(cur)) {
|
|
79
|
+
throw new ParseError('pdf/tagged/role-map/cycle',
|
|
80
|
+
'cycle detected resolving role-map alias',
|
|
81
|
+
{ context: { chain: chain.slice() } });
|
|
82
|
+
}
|
|
83
|
+
seen.add(cur);
|
|
84
|
+
chain.push(cur);
|
|
85
|
+
if (STANDARD_TYPES.has(cur)) return { standard: cur, chain };
|
|
86
|
+
const nsHit = namespaceMap && namespaceMap[cur];
|
|
87
|
+
const next = nsHit || (map && map[cur]);
|
|
88
|
+
if (!next || next === cur) {
|
|
89
|
+
return { standard: null, chain };
|
|
90
|
+
}
|
|
91
|
+
cur = next;
|
|
92
|
+
}
|
|
93
|
+
return { standard: null, chain };
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function isStandardType(name) {
|
|
97
|
+
return STANDARD_TYPES.has(name);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return {
|
|
101
|
+
typeRoleMap,
|
|
102
|
+
resolveStandardType,
|
|
103
|
+
isStandardType,
|
|
104
|
+
STANDARD_TYPES
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
};
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
// Copyright (c) 2026 AwaCloud SAS
|
|
2
|
+
// Author: Matthieu Bouilloux
|
|
3
|
+
// SPDX-License-Identifier: AGPL-3.0-only
|
|
4
|
+
// Dual-licensed; see the NOTICE file for licensing and any additional terms.
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @fileoverview Structure Element typing per ISO 32000-2:2020 §14.7.3.
|
|
8
|
+
*
|
|
9
|
+
* A `/Type /StructElem` dict represents a node in the logical structure
|
|
10
|
+
* tree. Heterogeneous `/K` is normalized to
|
|
11
|
+
* `[{ kind: 'elem'|'mcid'|'mcr'|'objr', ... }]`.
|
|
12
|
+
*
|
|
13
|
+
* @module pdf/tagged/structElement
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Module factory.
|
|
18
|
+
*/
|
|
19
|
+
import { pdfErrors } from '../errors.js';
|
|
20
|
+
import { pdfParser } from '../syntax/parser.js';
|
|
21
|
+
|
|
22
|
+
export const pdfStructElement = {
|
|
23
|
+
name: 'pdfStructElement',
|
|
24
|
+
dependencies: ['pdfErrors', 'pdfParser'],
|
|
25
|
+
deps: [pdfErrors, pdfParser],
|
|
26
|
+
factory(errors, parser) {
|
|
27
|
+
const { ParseError } = errors;
|
|
28
|
+
const { isType } = parser;
|
|
29
|
+
|
|
30
|
+
const KNOWN = new Set([
|
|
31
|
+
'Type', 'S', 'P', 'ID', 'Pg', 'K', 'A', 'C', 'R', 'T', 'Lang',
|
|
32
|
+
'Alt', 'E', 'ActualText', 'AF', 'NS', 'PhoneticAlphabet', 'Phoneme'
|
|
33
|
+
]);
|
|
34
|
+
|
|
35
|
+
function typeStructElement(dict) {
|
|
36
|
+
if (!isType(dict, 'dict')) {
|
|
37
|
+
throw new ParseError('pdf/tagged/struct-elem/not-dict',
|
|
38
|
+
'StructElem must be a dictionary',
|
|
39
|
+
{ context: { type: dict && dict.type } });
|
|
40
|
+
}
|
|
41
|
+
const e = dict.entries;
|
|
42
|
+
|
|
43
|
+
if (e.Type && (e.Type.type !== 'name' || e.Type.value !== 'StructElem')) {
|
|
44
|
+
throw new ParseError('pdf/tagged/struct-elem/bad-type',
|
|
45
|
+
'/Type entry must be /StructElem',
|
|
46
|
+
{ context: { actual: e.Type.value } });
|
|
47
|
+
}
|
|
48
|
+
if (!e.S || e.S.type !== 'name') {
|
|
49
|
+
throw new ParseError('pdf/tagged/struct-elem/missing-s',
|
|
50
|
+
'StructElem missing required /S (structure type) name',
|
|
51
|
+
{ context: { type: e.S && e.S.type } });
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const out = {
|
|
55
|
+
s: e.S.value,
|
|
56
|
+
raw: dict,
|
|
57
|
+
_extras: {}
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
if (e.P && e.P.type === 'ref') out.p = e.P;
|
|
61
|
+
if (e.Pg && e.Pg.type === 'ref') out.pg = e.Pg;
|
|
62
|
+
if (e.ID && e.ID.type === 'string') out.id = e.ID.value;
|
|
63
|
+
if (e.A) out.a = e.A;
|
|
64
|
+
if (e.C && (e.C.type === 'name' || e.C.type === 'array')) out.c = e.C;
|
|
65
|
+
if (e.R && (e.R.type === 'int' || e.R.type === 'real')) out.r = e.R.value | 0;
|
|
66
|
+
if (e.T && e.T.type === 'string') out.t = e.T.value;
|
|
67
|
+
if (e.Lang && e.Lang.type === 'string') out.lang = e.Lang.value;
|
|
68
|
+
if (e.Alt && e.Alt.type === 'string') out.alt = e.Alt.value;
|
|
69
|
+
if (e.E && e.E.type === 'string') out.e = e.E.value;
|
|
70
|
+
if (e.ActualText && e.ActualText.type === 'string') out.actualText = e.ActualText.value;
|
|
71
|
+
if (e.AF) out.af = e.AF;
|
|
72
|
+
if (e.NS && e.NS.type === 'ref') out.ns = e.NS;
|
|
73
|
+
if (e.PhoneticAlphabet && e.PhoneticAlphabet.type === 'name') {
|
|
74
|
+
out.phoneticAlphabet = e.PhoneticAlphabet.value;
|
|
75
|
+
}
|
|
76
|
+
if (e.Phoneme && e.Phoneme.type === 'string') out.phoneme = e.Phoneme.value;
|
|
77
|
+
|
|
78
|
+
if (e.K !== undefined) out.k = readKids(e.K);
|
|
79
|
+
|
|
80
|
+
for (const k of Object.keys(e)) {
|
|
81
|
+
if (!KNOWN.has(k)) out._extras[k] = e[k];
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return out;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function readKids(k) {
|
|
88
|
+
if (!k) return [];
|
|
89
|
+
if (k.type === 'array') {
|
|
90
|
+
const out = [];
|
|
91
|
+
for (const item of k.items) out.push(classifyKid(item));
|
|
92
|
+
return out;
|
|
93
|
+
}
|
|
94
|
+
return [classifyKid(k)];
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function classifyKid(item) {
|
|
98
|
+
if (!item || !item.type) {
|
|
99
|
+
throw new ParseError('pdf/tagged/struct-elem/bad-kid',
|
|
100
|
+
'StructElem /K entry is malformed',
|
|
101
|
+
{ context: { item } });
|
|
102
|
+
}
|
|
103
|
+
if (item.type === 'int') return { kind: 'mcid', mcid: item.value | 0 };
|
|
104
|
+
if (item.type === 'ref') return { kind: 'elem', ref: item };
|
|
105
|
+
if (item.type === 'dict') {
|
|
106
|
+
const t = item.entries.Type;
|
|
107
|
+
const tn = t && t.type === 'name' ? t.value : null;
|
|
108
|
+
if (tn === 'MCR') {
|
|
109
|
+
return {
|
|
110
|
+
kind: 'mcr',
|
|
111
|
+
pg: item.entries.Pg,
|
|
112
|
+
stm: item.entries.Stm,
|
|
113
|
+
stmOwn: item.entries.StmOwn,
|
|
114
|
+
mcid: (item.entries.MCID && item.entries.MCID.type === 'int')
|
|
115
|
+
? item.entries.MCID.value | 0 : null,
|
|
116
|
+
raw: item
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
if (tn === 'OBJR') {
|
|
120
|
+
return {
|
|
121
|
+
kind: 'objr',
|
|
122
|
+
pg: item.entries.Pg,
|
|
123
|
+
obj: item.entries.Obj,
|
|
124
|
+
raw: item
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
if (item.entries.MCID && item.entries.MCID.type === 'int') {
|
|
128
|
+
return { kind: 'mcr', mcid: item.entries.MCID.value | 0, raw: item };
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
throw new ParseError('pdf/tagged/struct-elem/bad-kid',
|
|
132
|
+
'StructElem /K entry must be ref, int, MCR-dict, or OBJR-dict',
|
|
133
|
+
{ context: { kind: item.type } });
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
return { typeStructElement };
|
|
137
|
+
}
|
|
138
|
+
};
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
// Copyright (c) 2026 AwaCloud SAS
|
|
2
|
+
// Author: Matthieu Bouilloux
|
|
3
|
+
// SPDX-License-Identifier: AGPL-3.0-only
|
|
4
|
+
// Dual-licensed; see the NOTICE file for licensing and any additional terms.
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @fileoverview Structure Tree Root typing per ISO 32000-2:2020 §14.7.2.
|
|
8
|
+
*
|
|
9
|
+
* The `/Type /StructTreeRoot` dict is referenced by the Catalog's
|
|
10
|
+
* `/StructTreeRoot` entry and anchors the logical structure tree used by
|
|
11
|
+
* Tagged PDF (§14.8) and PDF/UA-2 (ISO 14289-2). Unknown entries land in
|
|
12
|
+
* `_extras`.
|
|
13
|
+
*
|
|
14
|
+
* @module pdf/tagged/structTree
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Module factory.
|
|
19
|
+
*/
|
|
20
|
+
import { pdfErrors } from '../errors.js';
|
|
21
|
+
import { pdfParser } from '../syntax/parser.js';
|
|
22
|
+
|
|
23
|
+
export const pdfStructTree = {
|
|
24
|
+
name: 'pdfStructTree',
|
|
25
|
+
dependencies: ['pdfErrors', 'pdfParser'],
|
|
26
|
+
deps: [pdfErrors, pdfParser],
|
|
27
|
+
factory(errors, parser) {
|
|
28
|
+
const { ParseError } = errors;
|
|
29
|
+
const { isType } = parser;
|
|
30
|
+
|
|
31
|
+
const KNOWN = new Set([
|
|
32
|
+
'Type', 'K', 'IDTree', 'ParentTree', 'ParentTreeNextKey',
|
|
33
|
+
'RoleMap', 'ClassMap', 'Namespaces', 'AF', 'PronunciationLexicon'
|
|
34
|
+
]);
|
|
35
|
+
|
|
36
|
+
function typeStructTreeRoot(dict) {
|
|
37
|
+
if (!isType(dict, 'dict')) {
|
|
38
|
+
throw new ParseError('pdf/tagged/struct-tree/not-dict',
|
|
39
|
+
'StructTreeRoot must be a dictionary',
|
|
40
|
+
{ context: { type: dict && dict.type } });
|
|
41
|
+
}
|
|
42
|
+
const e = dict.entries;
|
|
43
|
+
|
|
44
|
+
if (e.Type && (e.Type.type !== 'name' || e.Type.value !== 'StructTreeRoot')) {
|
|
45
|
+
throw new ParseError('pdf/tagged/struct-tree/bad-type',
|
|
46
|
+
'/Type entry must be /StructTreeRoot',
|
|
47
|
+
{ context: { actual: e.Type.value } });
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const out = { raw: dict, _extras: {} };
|
|
51
|
+
|
|
52
|
+
if (e.K !== undefined) out.kids = readKids(e.K);
|
|
53
|
+
if (e.IDTree) out.idTree = e.IDTree;
|
|
54
|
+
if (e.ParentTree) out.parentTree = e.ParentTree;
|
|
55
|
+
if (e.ParentTreeNextKey && (e.ParentTreeNextKey.type === 'int'
|
|
56
|
+
|| e.ParentTreeNextKey.type === 'real')) {
|
|
57
|
+
out.parentTreeNextKey = e.ParentTreeNextKey.value | 0;
|
|
58
|
+
}
|
|
59
|
+
if (e.RoleMap && e.RoleMap.type === 'dict') out.roleMap = e.RoleMap;
|
|
60
|
+
if (e.ClassMap && e.ClassMap.type === 'dict') out.classMap = e.ClassMap;
|
|
61
|
+
if (e.Namespaces && e.Namespaces.type === 'array') out.namespaces = e.Namespaces;
|
|
62
|
+
if (e.AF) out.af = e.AF;
|
|
63
|
+
if (e.PronunciationLexicon) out.pronunciationLexicon = e.PronunciationLexicon;
|
|
64
|
+
|
|
65
|
+
for (const k of Object.keys(e)) {
|
|
66
|
+
if (!KNOWN.has(k)) out._extras[k] = e[k];
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return out;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function readKids(k) {
|
|
73
|
+
if (!k) return [];
|
|
74
|
+
if (k.type === 'array') {
|
|
75
|
+
const out = [];
|
|
76
|
+
for (const item of k.items) {
|
|
77
|
+
if (item.type !== 'ref' && item.type !== 'dict') {
|
|
78
|
+
throw new ParseError('pdf/tagged/struct-tree/bad-kid',
|
|
79
|
+
'StructTreeRoot /K array entries must be refs or dicts',
|
|
80
|
+
{ context: { kind: item.type } });
|
|
81
|
+
}
|
|
82
|
+
out.push(item);
|
|
83
|
+
}
|
|
84
|
+
return out;
|
|
85
|
+
}
|
|
86
|
+
if (k.type === 'ref' || k.type === 'dict') return [k];
|
|
87
|
+
throw new ParseError('pdf/tagged/struct-tree/bad-kid',
|
|
88
|
+
'StructTreeRoot /K must be a ref, dict, or array',
|
|
89
|
+
{ context: { kind: k.type } });
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
return { typeStructTreeRoot };
|
|
93
|
+
}
|
|
94
|
+
};
|