postext-pdf 1.63.2 → 1.63.3
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/dist/__bench__/arabicShaping.bench.test.js +1 -1
- package/dist/__bench__/arabicShaping.bench.test.js.map +1 -1
- package/dist/__tests__/accessibility.test.js +1 -1
- package/dist/__tests__/accessibility.test.js.map +1 -1
- package/dist/__tests__/boxBorderInside.test.js +1 -1
- package/dist/__tests__/boxBorderInside.test.js.map +1 -1
- package/dist/__tests__/boxShape.test.js +2 -2
- package/dist/__tests__/boxShape.test.js.map +1 -1
- package/dist/__tests__/canvasShaping.test.d.ts +1 -1
- package/dist/__tests__/canvasShaping.test.d.ts.map +1 -1
- package/dist/__tests__/canvasShaping.test.js +1 -1
- package/dist/__tests__/canvasShaping.test.js.map +1 -1
- package/dist/__tests__/comic.test.js +1 -1
- package/dist/__tests__/comic.test.js.map +1 -1
- package/dist/__tests__/comicMarkPairs.test.js +1 -1
- package/dist/__tests__/comicMarkPairs.test.js.map +1 -1
- package/dist/__tests__/composedDecorationImages.test.js +1 -1
- package/dist/__tests__/composedDecorationImages.test.js.map +1 -1
- package/dist/__tests__/composedFrameBoxes.test.js +1 -1
- package/dist/__tests__/composedFrameBoxes.test.js.map +1 -1
- package/dist/__tests__/composedPage.test.js +2 -2
- package/dist/__tests__/composedPage.test.js.map +1 -1
- package/dist/__tests__/declaredFiles.test.js +1 -1
- package/dist/__tests__/declaredFiles.test.js.map +1 -1
- package/dist/__tests__/dottedText.test.js +3 -3
- package/dist/__tests__/dottedText.test.js.map +1 -1
- package/dist/__tests__/headerFooter.test.js +2 -2
- package/dist/__tests__/headerFooter.test.js.map +1 -1
- package/dist/__tests__/pathBlock.test.js +2 -2
- package/dist/__tests__/pathBlock.test.js.map +1 -1
- package/dist/__tests__/printMaster.test.js +1 -1
- package/dist/__tests__/printMaster.test.js.map +1 -1
- package/dist/__tests__/rtlPdf.test.js +1 -1
- package/dist/__tests__/rtlPdf.test.js.map +1 -1
- package/dist/__tests__/shapingLanguage.test.js +1 -1
- package/dist/__tests__/shapingLanguage.test.js.map +1 -1
- package/dist/__tests__/shapingLanguageRuns.test.js +1 -1
- package/dist/__tests__/shapingLanguageRuns.test.js.map +1 -1
- package/dist/__tests__/shapingLanguageRunsDocument.test.js +1 -1
- package/dist/__tests__/shapingLanguageRunsDocument.test.js.map +1 -1
- package/dist/__tests__/sharedGlyphReadback.test.d.ts +1 -1
- package/dist/__tests__/sharedGlyphReadback.test.d.ts.map +1 -1
- package/dist/__tests__/sharedGlyphReadback.test.js +1 -1
- package/dist/__tests__/sharedGlyphReadback.test.js.map +1 -1
- package/dist/__tests__/svgVector.test.js +2 -2
- package/dist/__tests__/svgVector.test.js.map +1 -1
- package/dist/faceFiles.js +1 -1
- package/dist/faceFiles.js.map +1 -1
- package/dist/fontCache/fontFiles.d.ts +172 -0
- package/dist/fontCache/fontFiles.d.ts.map +1 -0
- package/dist/fontCache/fontFiles.js +81 -0
- package/dist/fontCache/fontFiles.js.map +1 -0
- package/dist/fontCache/glyphTables.d.ts +38 -0
- package/dist/fontCache/glyphTables.d.ts.map +1 -0
- package/dist/fontCache/glyphTables.js +107 -0
- package/dist/fontCache/glyphTables.js.map +1 -0
- package/dist/fontCache.d.ts +5 -189
- package/dist/fontCache.d.ts.map +1 -1
- package/dist/fontCache.js +5 -184
- package/dist/fontCache.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/dist/pdf-backend/blocks/blockRender/lineText.d.ts +23 -0
- package/dist/pdf-backend/blocks/blockRender/lineText.d.ts.map +1 -0
- package/dist/pdf-backend/blocks/blockRender/lineText.js +200 -0
- package/dist/pdf-backend/blocks/blockRender/lineText.js.map +1 -0
- package/dist/pdf-backend/blocks/blockRender/markers.d.ts +23 -0
- package/dist/pdf-backend/blocks/blockRender/markers.d.ts.map +1 -0
- package/dist/pdf-backend/blocks/blockRender/markers.js +140 -0
- package/dist/pdf-backend/blocks/blockRender/markers.js.map +1 -0
- package/dist/pdf-backend/blocks/blockRender/segments.d.ts +74 -0
- package/dist/pdf-backend/blocks/blockRender/segments.d.ts.map +1 -0
- package/dist/pdf-backend/blocks/blockRender/segments.js +409 -0
- package/dist/pdf-backend/blocks/blockRender/segments.js.map +1 -0
- package/dist/pdf-backend/{blockRender.d.ts → blocks/blockRender.d.ts} +7 -10
- package/dist/pdf-backend/blocks/blockRender.d.ts.map +1 -0
- package/dist/pdf-backend/blocks/blockRender.js +146 -0
- package/dist/pdf-backend/blocks/blockRender.js.map +1 -0
- package/dist/pdf-backend/{chip.d.ts → blocks/chip.d.ts} +3 -3
- package/dist/pdf-backend/blocks/chip.d.ts.map +1 -0
- package/dist/pdf-backend/{chip.js → blocks/chip.js} +5 -5
- package/dist/pdf-backend/blocks/chip.js.map +1 -0
- package/dist/pdf-backend/{inlineIcon.d.ts → blocks/inlineIcon.d.ts} +3 -3
- package/dist/pdf-backend/blocks/inlineIcon.d.ts.map +1 -0
- package/dist/pdf-backend/{inlineIcon.js → blocks/inlineIcon.js} +3 -3
- package/dist/pdf-backend/blocks/inlineIcon.js.map +1 -0
- package/dist/pdf-backend/blocks/renderResourceBlock/lines.d.ts +37 -0
- package/dist/pdf-backend/blocks/renderResourceBlock/lines.d.ts.map +1 -0
- package/dist/pdf-backend/blocks/renderResourceBlock/lines.js +271 -0
- package/dist/pdf-backend/blocks/renderResourceBlock/lines.js.map +1 -0
- package/dist/pdf-backend/blocks/renderResourceBlock/overlays.d.ts +21 -0
- package/dist/pdf-backend/blocks/renderResourceBlock/overlays.d.ts.map +1 -0
- package/dist/pdf-backend/blocks/renderResourceBlock/overlays.js +164 -0
- package/dist/pdf-backend/blocks/renderResourceBlock/overlays.js.map +1 -0
- package/dist/pdf-backend/blocks/renderResourceBlock/preload.d.ts +27 -0
- package/dist/pdf-backend/blocks/renderResourceBlock/preload.d.ts.map +1 -0
- package/dist/pdf-backend/blocks/renderResourceBlock/preload.js +330 -0
- package/dist/pdf-backend/blocks/renderResourceBlock/preload.js.map +1 -0
- package/dist/pdf-backend/blocks/renderResourceBlock/rasterize.d.ts +38 -0
- package/dist/pdf-backend/blocks/renderResourceBlock/rasterize.d.ts.map +1 -0
- package/dist/pdf-backend/blocks/renderResourceBlock/rasterize.js +218 -0
- package/dist/pdf-backend/blocks/renderResourceBlock/rasterize.js.map +1 -0
- package/dist/pdf-backend/blocks/renderResourceBlock/table.d.ts +17 -0
- package/dist/pdf-backend/blocks/renderResourceBlock/table.d.ts.map +1 -0
- package/dist/pdf-backend/blocks/renderResourceBlock/table.js +203 -0
- package/dist/pdf-backend/blocks/renderResourceBlock/table.js.map +1 -0
- package/dist/pdf-backend/blocks/renderResourceBlock.d.ts +59 -0
- package/dist/pdf-backend/blocks/renderResourceBlock.d.ts.map +1 -0
- package/dist/pdf-backend/blocks/renderResourceBlock.js +201 -0
- package/dist/pdf-backend/blocks/renderResourceBlock.js.map +1 -0
- package/dist/pdf-backend/{scoreRender.d.ts → blocks/scoreRender.d.ts} +2 -2
- package/dist/pdf-backend/blocks/scoreRender.d.ts.map +1 -0
- package/dist/pdf-backend/{scoreRender.js → blocks/scoreRender.js} +2 -2
- package/dist/pdf-backend/blocks/scoreRender.js.map +1 -0
- package/dist/pdf-backend/{taskBox.d.ts → blocks/taskBox.d.ts} +1 -1
- package/dist/pdf-backend/blocks/taskBox.d.ts.map +1 -0
- package/dist/pdf-backend/{taskBox.js → blocks/taskBox.js} +1 -1
- package/dist/pdf-backend/blocks/taskBox.js.map +1 -0
- package/dist/pdf-backend/index.d.ts +8 -114
- package/dist/pdf-backend/index.d.ts.map +1 -1
- package/dist/pdf-backend/index.js +16 -614
- package/dist/pdf-backend/index.js.map +1 -1
- package/dist/pdf-backend/{comic.d.ts → page/comic.d.ts} +6 -6
- package/dist/pdf-backend/page/comic.d.ts.map +1 -0
- package/dist/pdf-backend/{comic.js → page/comic.js} +4 -4
- package/dist/pdf-backend/page/comic.js.map +1 -0
- package/dist/pdf-backend/{headerFooter.d.ts → page/headerFooter.d.ts} +4 -4
- package/dist/pdf-backend/page/headerFooter.d.ts.map +1 -0
- package/dist/pdf-backend/{headerFooter.js → page/headerFooter.js} +8 -8
- package/dist/pdf-backend/page/headerFooter.js.map +1 -0
- package/dist/pdf-backend/{pageDecorations.d.ts → page/pageDecorations.d.ts} +1 -1
- package/dist/pdf-backend/page/pageDecorations.d.ts.map +1 -0
- package/dist/pdf-backend/{pageDecorations.js → page/pageDecorations.js} +1 -1
- package/dist/pdf-backend/page/pageDecorations.js.map +1 -0
- package/dist/pdf-backend/page/pageLabels.d.ts.map +1 -0
- package/dist/pdf-backend/page/pageLabels.js.map +1 -0
- package/dist/pdf-backend/primitives/actualText.d.ts +91 -0
- package/dist/pdf-backend/primitives/actualText.d.ts.map +1 -0
- package/dist/pdf-backend/primitives/actualText.js +154 -0
- package/dist/pdf-backend/primitives/actualText.js.map +1 -0
- package/dist/pdf-backend/primitives/color.d.ts +22 -0
- package/dist/pdf-backend/primitives/color.d.ts.map +1 -0
- package/dist/pdf-backend/primitives/color.js +116 -0
- package/dist/pdf-backend/primitives/color.js.map +1 -0
- package/dist/pdf-backend/primitives/matrix.d.ts +55 -0
- package/dist/pdf-backend/primitives/matrix.d.ts.map +1 -0
- package/dist/pdf-backend/primitives/matrix.js +124 -0
- package/dist/pdf-backend/primitives/matrix.js.map +1 -0
- package/dist/pdf-backend/primitives/measuredText.d.ts +61 -0
- package/dist/pdf-backend/primitives/measuredText.d.ts.map +1 -0
- package/dist/pdf-backend/primitives/measuredText.js +142 -0
- package/dist/pdf-backend/primitives/measuredText.js.map +1 -0
- package/dist/pdf-backend/primitives/shapedRuns.d.ts +49 -0
- package/dist/pdf-backend/primitives/shapedRuns.d.ts.map +1 -0
- package/dist/pdf-backend/primitives/shapedRuns.js +367 -0
- package/dist/pdf-backend/primitives/shapedRuns.js.map +1 -0
- package/dist/pdf-backend/primitives/shapes.d.ts +35 -0
- package/dist/pdf-backend/primitives/shapes.d.ts.map +1 -0
- package/dist/pdf-backend/primitives/shapes.js +166 -0
- package/dist/pdf-backend/primitives/shapes.js.map +1 -0
- package/dist/pdf-backend/primitives/textShows.d.ts +90 -0
- package/dist/pdf-backend/primitives/textShows.d.ts.map +1 -0
- package/dist/pdf-backend/primitives/textShows.js +167 -0
- package/dist/pdf-backend/primitives/textShows.js.map +1 -0
- package/dist/pdf-backend/primitives.d.ts +16 -308
- package/dist/pdf-backend/primitives.d.ts.map +1 -1
- package/dist/pdf-backend/primitives.js +15 -1195
- package/dist/pdf-backend/primitives.js.map +1 -1
- package/dist/pdf-backend/renderToPdf/composedFrames.d.ts +19 -0
- package/dist/pdf-backend/renderToPdf/composedFrames.d.ts.map +1 -0
- package/dist/pdf-backend/renderToPdf/composedFrames.js +156 -0
- package/dist/pdf-backend/renderToPdf/composedFrames.js.map +1 -0
- package/dist/pdf-backend/renderToPdf/metadata.d.ts +31 -0
- package/dist/pdf-backend/renderToPdf/metadata.d.ts.map +1 -0
- package/dist/pdf-backend/renderToPdf/metadata.js +85 -0
- package/dist/pdf-backend/renderToPdf/metadata.js.map +1 -0
- package/dist/pdf-backend/renderToPdf/paintPage.d.ts +16 -0
- package/dist/pdf-backend/renderToPdf/paintPage.d.ts.map +1 -0
- package/dist/pdf-backend/renderToPdf/paintPage.js +348 -0
- package/dist/pdf-backend/renderToPdf/paintPage.js.map +1 -0
- package/dist/pdf-backend/renderToPdf/warnings.d.ts +114 -0
- package/dist/pdf-backend/renderToPdf/warnings.d.ts.map +1 -0
- package/dist/pdf-backend/renderToPdf/warnings.js +35 -0
- package/dist/pdf-backend/renderToPdf/warnings.js.map +1 -0
- package/dist/pdf-backend/structure/links.d.ts.map +1 -0
- package/dist/pdf-backend/structure/links.js.map +1 -0
- package/dist/pdf-backend/structure/outlines.d.ts.map +1 -0
- package/dist/pdf-backend/structure/outlines.js.map +1 -0
- package/dist/pdf-backend/structure/structureFlow.d.ts.map +1 -0
- package/dist/pdf-backend/structure/structureFlow.js.map +1 -0
- package/dist/pdf-backend/{tagging.d.ts → structure/tagging.d.ts} +1 -1
- package/dist/pdf-backend/structure/tagging.d.ts.map +1 -0
- package/dist/pdf-backend/structure/tagging.js.map +1 -0
- package/dist/pdf-backend/{annotations.d.ts → text/annotations.d.ts} +3 -3
- package/dist/pdf-backend/text/annotations.d.ts.map +1 -0
- package/dist/pdf-backend/{annotations.js → text/annotations.js} +3 -3
- package/dist/pdf-backend/text/annotations.js.map +1 -0
- package/dist/pdf-backend/{directedLine.d.ts → text/directedLine.d.ts} +1 -1
- package/dist/pdf-backend/text/directedLine.d.ts.map +1 -0
- package/dist/pdf-backend/{directedLine.js → text/directedLine.js} +1 -1
- package/dist/pdf-backend/text/directedLine.js.map +1 -0
- package/dist/pdf-backend/text/fallbackSpaces.d.ts.map +1 -0
- package/dist/pdf-backend/text/fallbackSpaces.js.map +1 -0
- package/dist/pdf-backend/text/fontHelpers.d.ts.map +1 -0
- package/dist/pdf-backend/text/fontHelpers.js.map +1 -0
- package/dist/pdf-backend/text/inkScale.d.ts.map +1 -0
- package/dist/pdf-backend/text/inkScale.js.map +1 -0
- package/dist/pdf-backend/{rtlRun.d.ts → text/rtlRun.d.ts} +1 -1
- package/dist/pdf-backend/text/rtlRun.d.ts.map +1 -0
- package/dist/pdf-backend/{rtlRun.js → text/rtlRun.js} +2 -2
- package/dist/pdf-backend/text/rtlRun.js.map +1 -0
- package/dist/pdf-backend/text/shapedText/shaping.d.ts +70 -0
- package/dist/pdf-backend/text/shapedText/shaping.d.ts.map +1 -0
- package/dist/pdf-backend/text/shapedText/shaping.js +380 -0
- package/dist/pdf-backend/text/shapedText/shaping.js.map +1 -0
- package/dist/pdf-backend/{shapedText.d.ts → text/shapedText.d.ts} +2 -2
- package/dist/pdf-backend/text/shapedText.d.ts.map +1 -0
- package/dist/pdf-backend/text/shapedText.js +311 -0
- package/dist/pdf-backend/text/shapedText.js.map +1 -0
- package/dist/pdf-backend/{verticalText.d.ts → text/verticalText.d.ts} +1 -1
- package/dist/pdf-backend/text/verticalText.d.ts.map +1 -0
- package/dist/pdf-backend/{verticalText.js → text/verticalText.js} +4 -4
- package/dist/pdf-backend/text/verticalText.js.map +1 -0
- package/dist/pdf-backend/text/wordPieces.d.ts.map +1 -0
- package/dist/pdf-backend/{wordPieces.js → text/wordPieces.js} +1 -1
- package/dist/pdf-backend/text/wordPieces.js.map +1 -0
- package/dist/pdf-backend/{pathBlock.d.ts → vector/pathBlock.d.ts} +1 -1
- package/dist/pdf-backend/vector/pathBlock.d.ts.map +1 -0
- package/dist/pdf-backend/{pathBlock.js → vector/pathBlock.js} +1 -1
- package/dist/pdf-backend/vector/pathBlock.js.map +1 -0
- package/dist/pdf-backend/vector/svgVector/emit.d.ts +11 -0
- package/dist/pdf-backend/vector/svgVector/emit.d.ts.map +1 -0
- package/dist/pdf-backend/vector/svgVector/emit.js +281 -0
- package/dist/pdf-backend/vector/svgVector/emit.js.map +1 -0
- package/dist/pdf-backend/vector/svgVector/geometry.d.ts +18 -0
- package/dist/pdf-backend/vector/svgVector/geometry.d.ts.map +1 -0
- package/dist/pdf-backend/vector/svgVector/geometry.js +382 -0
- package/dist/pdf-backend/vector/svgVector/geometry.js.map +1 -0
- package/dist/pdf-backend/vector/svgVector/style.d.ts +37 -0
- package/dist/pdf-backend/vector/svgVector/style.d.ts.map +1 -0
- package/dist/pdf-backend/vector/svgVector/style.js +314 -0
- package/dist/pdf-backend/vector/svgVector/style.js.map +1 -0
- package/dist/pdf-backend/vector/svgVector/walk.d.ts +15 -0
- package/dist/pdf-backend/vector/svgVector/walk.d.ts.map +1 -0
- package/dist/pdf-backend/vector/svgVector/walk.js +405 -0
- package/dist/pdf-backend/vector/svgVector/walk.js.map +1 -0
- package/dist/pdf-backend/vector/svgVector/xml.d.ts +20 -0
- package/dist/pdf-backend/vector/svgVector/xml.d.ts.map +1 -0
- package/dist/pdf-backend/vector/svgVector/xml.js +160 -0
- package/dist/pdf-backend/vector/svgVector/xml.js.map +1 -0
- package/dist/pdf-backend/{svgVector.d.ts → vector/svgVector.d.ts} +2 -12
- package/dist/pdf-backend/vector/svgVector.d.ts.map +1 -0
- package/dist/pdf-backend/vector/svgVector.js +95 -0
- package/dist/pdf-backend/vector/svgVector.js.map +1 -0
- package/dist/pdf-backend/vector/tint.d.ts.map +1 -0
- package/dist/pdf-backend/{tint.js → vector/tint.js} +1 -1
- package/dist/pdf-backend/vector/tint.js.map +1 -0
- package/dist/worker/client.d.ts +1 -1
- package/dist/worker/client.d.ts.map +1 -1
- package/dist/worker/client.js +1 -1
- package/dist/worker/client.js.map +1 -1
- package/package.json +3 -3
- package/dist/pdf-backend/annotations.d.ts.map +0 -1
- package/dist/pdf-backend/annotations.js.map +0 -1
- package/dist/pdf-backend/blockRender.d.ts.map +0 -1
- package/dist/pdf-backend/blockRender.js +0 -866
- package/dist/pdf-backend/blockRender.js.map +0 -1
- package/dist/pdf-backend/chip.d.ts.map +0 -1
- package/dist/pdf-backend/chip.js.map +0 -1
- package/dist/pdf-backend/comic.d.ts.map +0 -1
- package/dist/pdf-backend/comic.js.map +0 -1
- package/dist/pdf-backend/directedLine.d.ts.map +0 -1
- package/dist/pdf-backend/directedLine.js.map +0 -1
- package/dist/pdf-backend/fallbackSpaces.d.ts.map +0 -1
- package/dist/pdf-backend/fallbackSpaces.js.map +0 -1
- package/dist/pdf-backend/fontHelpers.d.ts.map +0 -1
- package/dist/pdf-backend/fontHelpers.js.map +0 -1
- package/dist/pdf-backend/headerFooter.d.ts.map +0 -1
- package/dist/pdf-backend/headerFooter.js.map +0 -1
- package/dist/pdf-backend/inkScale.d.ts.map +0 -1
- package/dist/pdf-backend/inkScale.js.map +0 -1
- package/dist/pdf-backend/inlineIcon.d.ts.map +0 -1
- package/dist/pdf-backend/inlineIcon.js.map +0 -1
- package/dist/pdf-backend/links.d.ts.map +0 -1
- package/dist/pdf-backend/links.js.map +0 -1
- package/dist/pdf-backend/outlines.d.ts.map +0 -1
- package/dist/pdf-backend/outlines.js.map +0 -1
- package/dist/pdf-backend/pageDecorations.d.ts.map +0 -1
- package/dist/pdf-backend/pageDecorations.js.map +0 -1
- package/dist/pdf-backend/pageLabels.d.ts.map +0 -1
- package/dist/pdf-backend/pageLabels.js.map +0 -1
- package/dist/pdf-backend/pathBlock.d.ts.map +0 -1
- package/dist/pdf-backend/pathBlock.js.map +0 -1
- package/dist/pdf-backend/renderResourceBlock.d.ts +0 -120
- package/dist/pdf-backend/renderResourceBlock.d.ts.map +0 -1
- package/dist/pdf-backend/renderResourceBlock.js +0 -1351
- package/dist/pdf-backend/renderResourceBlock.js.map +0 -1
- package/dist/pdf-backend/rtlRun.d.ts.map +0 -1
- package/dist/pdf-backend/rtlRun.js.map +0 -1
- package/dist/pdf-backend/scoreRender.d.ts.map +0 -1
- package/dist/pdf-backend/scoreRender.js.map +0 -1
- package/dist/pdf-backend/shapedText.d.ts.map +0 -1
- package/dist/pdf-backend/shapedText.js +0 -682
- package/dist/pdf-backend/shapedText.js.map +0 -1
- package/dist/pdf-backend/structureFlow.d.ts.map +0 -1
- package/dist/pdf-backend/structureFlow.js.map +0 -1
- package/dist/pdf-backend/svgVector.d.ts.map +0 -1
- package/dist/pdf-backend/svgVector.js +0 -1610
- package/dist/pdf-backend/svgVector.js.map +0 -1
- package/dist/pdf-backend/tagging.d.ts.map +0 -1
- package/dist/pdf-backend/tagging.js.map +0 -1
- package/dist/pdf-backend/taskBox.d.ts.map +0 -1
- package/dist/pdf-backend/taskBox.js.map +0 -1
- package/dist/pdf-backend/tint.d.ts.map +0 -1
- package/dist/pdf-backend/tint.js.map +0 -1
- package/dist/pdf-backend/verticalText.d.ts.map +0 -1
- package/dist/pdf-backend/verticalText.js.map +0 -1
- package/dist/pdf-backend/wordPieces.d.ts.map +0 -1
- package/dist/pdf-backend/wordPieces.js.map +0 -1
- /package/dist/pdf-backend/{pageLabels.d.ts → page/pageLabels.d.ts} +0 -0
- /package/dist/pdf-backend/{pageLabels.js → page/pageLabels.js} +0 -0
- /package/dist/pdf-backend/{links.d.ts → structure/links.d.ts} +0 -0
- /package/dist/pdf-backend/{links.js → structure/links.js} +0 -0
- /package/dist/pdf-backend/{outlines.d.ts → structure/outlines.d.ts} +0 -0
- /package/dist/pdf-backend/{outlines.js → structure/outlines.js} +0 -0
- /package/dist/pdf-backend/{structureFlow.d.ts → structure/structureFlow.d.ts} +0 -0
- /package/dist/pdf-backend/{structureFlow.js → structure/structureFlow.js} +0 -0
- /package/dist/pdf-backend/{tagging.js → structure/tagging.js} +0 -0
- /package/dist/pdf-backend/{fallbackSpaces.d.ts → text/fallbackSpaces.d.ts} +0 -0
- /package/dist/pdf-backend/{fallbackSpaces.js → text/fallbackSpaces.js} +0 -0
- /package/dist/pdf-backend/{fontHelpers.d.ts → text/fontHelpers.d.ts} +0 -0
- /package/dist/pdf-backend/{fontHelpers.js → text/fontHelpers.js} +0 -0
- /package/dist/pdf-backend/{inkScale.d.ts → text/inkScale.d.ts} +0 -0
- /package/dist/pdf-backend/{inkScale.js → text/inkScale.js} +0 -0
- /package/dist/pdf-backend/{wordPieces.d.ts → text/wordPieces.d.ts} +0 -0
- /package/dist/pdf-backend/{tint.d.ts → vector/tint.d.ts} +0 -0
|
@@ -1,1610 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Vector emission of SVG resources into the PDF.
|
|
3
|
-
*
|
|
4
|
-
* Converts a conservative subset of SVG into a flat list of filled / stroked
|
|
5
|
-
* paths in the SVG's root user space, which {@link drawVectorDrawing} then
|
|
6
|
-
* paints with native PDF path operators — so the figure stays vector (sharp at
|
|
7
|
-
* any zoom, tiny on disk) instead of going through a raster fallback. The
|
|
8
|
-
* paths of a drawing are written once per document, as a form XObject that
|
|
9
|
-
* every page drawing it paints.
|
|
10
|
-
*
|
|
11
|
-
* Supported: `svg` (root, `viewBox`, `preserveAspectRatio`), `g` / `a`,
|
|
12
|
-
* `defs`, `path`, `rect`, `circle`, `ellipse`, `line`, `polyline`, `polygon`,
|
|
13
|
-
* `use` (of any of the above), `clipPath` (`userSpaceOnUse`), `transform`
|
|
14
|
-
* lists, solid `fill` / `stroke` paints (hex, `rgb()`, named colours,
|
|
15
|
-
* `currentColor`), `fill-rule`, opacities, line caps / joins / miter limit,
|
|
16
|
-
* dash patterns, `display` / `visibility`, the same properties inside a
|
|
17
|
-
* `style` attribute, and — when the caller supplies a {@link VectorFontResolver}
|
|
18
|
-
* — `text` / `tspan` runs (`x`, `y`, single `dx` / `dy`, `font-family`,
|
|
19
|
-
* `font-size`, `font-weight`, `font-style`, `text-anchor`, `direction`),
|
|
20
|
-
* which are set as real, selectable text in the document's embedded fonts.
|
|
21
|
-
* Text in a right-to-left or joining script (Arabic) is shaped by HarfBuzz
|
|
22
|
-
* in its bidi runs (`shapedText.ts`), on the `direction` the browser lays
|
|
23
|
-
* it out with (left to right unless the SVG says otherwise); with HarfBuzz
|
|
24
|
-
* not loaded such text bails out to the raster embed.
|
|
25
|
-
*
|
|
26
|
-
* Anything else — images, gradients, patterns, masks, filters, markers,
|
|
27
|
-
* symbols, nested `svg`, style sheets (other than `@font-face` rules alone,
|
|
28
|
-
* which are skipped), `objectBoundingBox` clips, text on a
|
|
29
|
-
* path or with per-glyph positioning, text in a font the resolver cannot
|
|
30
|
-
* provide — makes {@link svgToVectorDrawing} return `null`, and the caller
|
|
31
|
-
* falls back to a raster embed. Bailing out (rather than drawing an
|
|
32
|
-
* approximation) keeps the printed figure faithful to what the screen
|
|
33
|
-
* backends show.
|
|
34
|
-
*
|
|
35
|
-
* Known approximations: group `opacity` is folded into each descendant's fill
|
|
36
|
-
* and stroke alpha (PDF group transparency would need a transparency group
|
|
37
|
-
* XObject); a `clipPath` with several children is the union of its shapes in
|
|
38
|
-
* SVG but is emitted as one nonzero path here; strokes under a non-uniform
|
|
39
|
-
* transform get a uniform width (the geometric mean of the scale factors).
|
|
40
|
-
*/
|
|
41
|
-
import { PDFName, PDFNumber, PDFOperator, PDFOperatorNames, LineCapStyle, LineJoinStyle, appendBezierCurve, drawObject, beginText, clip, clipEvenOdd, closePath, concatTransformationMatrix, endPath, endText, fill, fillAndStroke, lineTo, moveTo, popGraphicsState, pushGraphicsState, rectangle, setDashPattern, setFillingColor, setFontAndSize, setGraphicsState, setLineCap, setLineJoin, setLineWidth, setStrokingColor, setTextMatrix, stroke, } from 'pdf-lib';
|
|
42
|
-
import { colorFromHex, textShows } from './primitives.js';
|
|
43
|
-
import { isPrintMode, modeKey } from '../print/colorMode.js';
|
|
44
|
-
import { applyPrintPass, overprintStateDict } from '../print/printPass.js';
|
|
45
|
-
import { shapedSvgText } from './shapedText.js';
|
|
46
|
-
import { complexShaperReady, needsComplexShaping } from '../complexShaping.js';
|
|
47
|
-
import { isFontFaceOnlyStyleSheet } from 'postext';
|
|
48
|
-
const ENTITIES = { amp: '&', lt: '<', gt: '>', quot: '"', apos: "'" };
|
|
49
|
-
function decodeEntities(s) {
|
|
50
|
-
if (!s.includes('&'))
|
|
51
|
-
return s;
|
|
52
|
-
return s.replace(/&(#x[0-9a-fA-F]+|#\d+|[a-zA-Z]+);/g, (m, body) => {
|
|
53
|
-
if (body[0] === '#') {
|
|
54
|
-
const code = body[1] === 'x' || body[1] === 'X' ? parseInt(body.slice(2), 16) : parseInt(body.slice(1), 10);
|
|
55
|
-
return Number.isFinite(code) ? String.fromCodePoint(code) : m;
|
|
56
|
-
}
|
|
57
|
-
return ENTITIES[body] ?? m;
|
|
58
|
-
});
|
|
59
|
-
}
|
|
60
|
-
/** Local name of a qualified name (`svg:path` → `path`); `null` for names in
|
|
61
|
-
* foreign namespaces we can safely skip (`inkscape:*`, `sodipodi:*`, ...). */
|
|
62
|
-
function localName(qualified) {
|
|
63
|
-
const i = qualified.indexOf(':');
|
|
64
|
-
if (i < 0)
|
|
65
|
-
return { local: qualified, foreign: false };
|
|
66
|
-
const prefix = qualified.slice(0, i);
|
|
67
|
-
return { local: qualified.slice(i + 1), foreign: prefix !== 'svg' };
|
|
68
|
-
}
|
|
69
|
-
/** Minimal non-validating XML parser: elements, attributes, comments, CDATA,
|
|
70
|
-
* processing instructions and doctype. Character data is discarded (no
|
|
71
|
-
* supported element renders text). Returns `null` on malformed input. */
|
|
72
|
-
function parseXml(text) {
|
|
73
|
-
const root = { name: '#root', attrs: {}, children: [], content: [] };
|
|
74
|
-
const stack = [root];
|
|
75
|
-
let i = 0;
|
|
76
|
-
const n = text.length;
|
|
77
|
-
const markText = (from, to, raw = false) => {
|
|
78
|
-
if (to <= from)
|
|
79
|
-
return;
|
|
80
|
-
const top = stack[stack.length - 1];
|
|
81
|
-
const chunk = text.slice(from, to);
|
|
82
|
-
if (chunk.trim() !== '')
|
|
83
|
-
top.hasText = true;
|
|
84
|
-
top.content.push(raw ? chunk : decodeEntities(chunk));
|
|
85
|
-
};
|
|
86
|
-
while (i < n) {
|
|
87
|
-
const lt = text.indexOf('<', i);
|
|
88
|
-
if (lt < 0) {
|
|
89
|
-
markText(i, n);
|
|
90
|
-
break;
|
|
91
|
-
}
|
|
92
|
-
markText(i, lt);
|
|
93
|
-
i = lt;
|
|
94
|
-
if (text.startsWith('<!--', i)) {
|
|
95
|
-
const end = text.indexOf('-->', i + 4);
|
|
96
|
-
if (end < 0)
|
|
97
|
-
return null;
|
|
98
|
-
i = end + 3;
|
|
99
|
-
continue;
|
|
100
|
-
}
|
|
101
|
-
if (text.startsWith('<![CDATA[', i)) {
|
|
102
|
-
const end = text.indexOf(']]>', i + 9);
|
|
103
|
-
if (end < 0)
|
|
104
|
-
return null;
|
|
105
|
-
markText(i + 9, end, true);
|
|
106
|
-
i = end + 3;
|
|
107
|
-
continue;
|
|
108
|
-
}
|
|
109
|
-
if (text.startsWith('<?', i)) {
|
|
110
|
-
const end = text.indexOf('?>', i + 2);
|
|
111
|
-
if (end < 0)
|
|
112
|
-
return null;
|
|
113
|
-
i = end + 2;
|
|
114
|
-
continue;
|
|
115
|
-
}
|
|
116
|
-
if (text.startsWith('<!', i)) {
|
|
117
|
-
// <!DOCTYPE ...> possibly with an internal subset in [ ... ].
|
|
118
|
-
let depth = 0;
|
|
119
|
-
let j = i + 2;
|
|
120
|
-
for (; j < n; j++) {
|
|
121
|
-
const ch = text[j];
|
|
122
|
-
if (ch === '[')
|
|
123
|
-
depth++;
|
|
124
|
-
else if (ch === ']')
|
|
125
|
-
depth--;
|
|
126
|
-
else if (ch === '>' && depth === 0)
|
|
127
|
-
break;
|
|
128
|
-
}
|
|
129
|
-
if (j >= n)
|
|
130
|
-
return null;
|
|
131
|
-
i = j + 1;
|
|
132
|
-
continue;
|
|
133
|
-
}
|
|
134
|
-
if (text.startsWith('</', i)) {
|
|
135
|
-
const end = text.indexOf('>', i + 2);
|
|
136
|
-
if (end < 0)
|
|
137
|
-
return null;
|
|
138
|
-
const name = text.slice(i + 2, end).trim();
|
|
139
|
-
const open = stack.pop();
|
|
140
|
-
if (!open || open.name !== name || stack.length === 0)
|
|
141
|
-
return null;
|
|
142
|
-
i = end + 1;
|
|
143
|
-
continue;
|
|
144
|
-
}
|
|
145
|
-
// Start tag.
|
|
146
|
-
let j = i + 1;
|
|
147
|
-
while (j < n && !/[\s/>]/.test(text[j]))
|
|
148
|
-
j++;
|
|
149
|
-
const name = text.slice(i + 1, j);
|
|
150
|
-
if (!name)
|
|
151
|
-
return null;
|
|
152
|
-
const attrs = {};
|
|
153
|
-
let selfClosing = false;
|
|
154
|
-
for (;;) {
|
|
155
|
-
while (j < n && /\s/.test(text[j]))
|
|
156
|
-
j++;
|
|
157
|
-
if (j >= n)
|
|
158
|
-
return null;
|
|
159
|
-
if (text[j] === '/') {
|
|
160
|
-
selfClosing = true;
|
|
161
|
-
j++;
|
|
162
|
-
continue;
|
|
163
|
-
}
|
|
164
|
-
if (text[j] === '>') {
|
|
165
|
-
j++;
|
|
166
|
-
break;
|
|
167
|
-
}
|
|
168
|
-
let k = j;
|
|
169
|
-
while (k < n && !/[\s=/>]/.test(text[k]))
|
|
170
|
-
k++;
|
|
171
|
-
const attrName = text.slice(j, k);
|
|
172
|
-
if (!attrName)
|
|
173
|
-
return null;
|
|
174
|
-
j = k;
|
|
175
|
-
while (j < n && /\s/.test(text[j]))
|
|
176
|
-
j++;
|
|
177
|
-
if (text[j] !== '=') {
|
|
178
|
-
// Attribute without a value (not valid XML, but harmless).
|
|
179
|
-
attrs[attrName] = '';
|
|
180
|
-
continue;
|
|
181
|
-
}
|
|
182
|
-
j++;
|
|
183
|
-
while (j < n && /\s/.test(text[j]))
|
|
184
|
-
j++;
|
|
185
|
-
const quote = text[j];
|
|
186
|
-
if (quote !== '"' && quote !== "'")
|
|
187
|
-
return null;
|
|
188
|
-
const close = text.indexOf(quote, j + 1);
|
|
189
|
-
if (close < 0)
|
|
190
|
-
return null;
|
|
191
|
-
attrs[attrName] = decodeEntities(text.slice(j + 1, close));
|
|
192
|
-
j = close + 1;
|
|
193
|
-
}
|
|
194
|
-
const el = { name, attrs, children: [], content: [] };
|
|
195
|
-
stack[stack.length - 1].children.push(el);
|
|
196
|
-
stack[stack.length - 1].content.push(el);
|
|
197
|
-
if (!selfClosing)
|
|
198
|
-
stack.push(el);
|
|
199
|
-
i = j;
|
|
200
|
-
}
|
|
201
|
-
if (stack.length !== 1)
|
|
202
|
-
return null;
|
|
203
|
-
return root;
|
|
204
|
-
}
|
|
205
|
-
const IDENTITY = [1, 0, 0, 1, 0, 0];
|
|
206
|
-
function mul(a, b) {
|
|
207
|
-
return [
|
|
208
|
-
a[0] * b[0] + a[2] * b[1],
|
|
209
|
-
a[1] * b[0] + a[3] * b[1],
|
|
210
|
-
a[0] * b[2] + a[2] * b[3],
|
|
211
|
-
a[1] * b[2] + a[3] * b[3],
|
|
212
|
-
a[0] * b[4] + a[2] * b[5] + a[4],
|
|
213
|
-
a[1] * b[4] + a[3] * b[5] + a[5],
|
|
214
|
-
];
|
|
215
|
-
}
|
|
216
|
-
function apply(m, x, y) {
|
|
217
|
-
return [m[0] * x + m[2] * y + m[4], m[1] * x + m[3] * y + m[5]];
|
|
218
|
-
}
|
|
219
|
-
/** Uniform length scale of a matrix (geometric mean of its singular values). */
|
|
220
|
-
function lengthScale(m) {
|
|
221
|
-
return Math.sqrt(Math.abs(m[0] * m[3] - m[1] * m[2]));
|
|
222
|
-
}
|
|
223
|
-
function transformSegs(segs, m) {
|
|
224
|
-
return segs.map((s) => {
|
|
225
|
-
switch (s[0]) {
|
|
226
|
-
case 'M':
|
|
227
|
-
case 'L': {
|
|
228
|
-
const [x, y] = apply(m, s[1], s[2]);
|
|
229
|
-
return [s[0], x, y];
|
|
230
|
-
}
|
|
231
|
-
case 'C': {
|
|
232
|
-
const [x1, y1] = apply(m, s[1], s[2]);
|
|
233
|
-
const [x2, y2] = apply(m, s[3], s[4]);
|
|
234
|
-
const [x, y] = apply(m, s[5], s[6]);
|
|
235
|
-
return ['C', x1, y1, x2, y2, x, y];
|
|
236
|
-
}
|
|
237
|
-
default:
|
|
238
|
-
return s;
|
|
239
|
-
}
|
|
240
|
-
});
|
|
241
|
-
}
|
|
242
|
-
class Unsupported extends Error {
|
|
243
|
-
}
|
|
244
|
-
function unsupported(what) {
|
|
245
|
-
throw new Unsupported(what);
|
|
246
|
-
}
|
|
247
|
-
function parseTransform(value) {
|
|
248
|
-
if (!value)
|
|
249
|
-
return IDENTITY;
|
|
250
|
-
let m = IDENTITY;
|
|
251
|
-
const re = /([a-zA-Z]+)\s*\(([^)]*)\)/g;
|
|
252
|
-
let match;
|
|
253
|
-
let consumed = 0;
|
|
254
|
-
while ((match = re.exec(value)) !== null) {
|
|
255
|
-
if (value.slice(consumed, match.index).trim().replace(/,/g, '') !== '')
|
|
256
|
-
unsupported('transform syntax');
|
|
257
|
-
consumed = match.index + match[0].length;
|
|
258
|
-
const args = match[2].trim().split(/[\s,]+/).filter((s) => s !== '').map(Number);
|
|
259
|
-
if (args.some((v) => !Number.isFinite(v)))
|
|
260
|
-
unsupported('transform args');
|
|
261
|
-
let t;
|
|
262
|
-
switch (match[1]) {
|
|
263
|
-
case 'matrix':
|
|
264
|
-
if (args.length !== 6)
|
|
265
|
-
unsupported('matrix()');
|
|
266
|
-
t = [args[0], args[1], args[2], args[3], args[4], args[5]];
|
|
267
|
-
break;
|
|
268
|
-
case 'translate':
|
|
269
|
-
t = [1, 0, 0, 1, args[0] ?? 0, args[1] ?? 0];
|
|
270
|
-
break;
|
|
271
|
-
case 'scale':
|
|
272
|
-
t = [args[0] ?? 1, 0, 0, args[1] ?? args[0] ?? 1, 0, 0];
|
|
273
|
-
break;
|
|
274
|
-
case 'rotate': {
|
|
275
|
-
const a = ((args[0] ?? 0) * Math.PI) / 180;
|
|
276
|
-
const cos = Math.cos(a);
|
|
277
|
-
const sin = Math.sin(a);
|
|
278
|
-
const r = [cos, sin, -sin, cos, 0, 0];
|
|
279
|
-
if (args.length >= 3) {
|
|
280
|
-
const cx = args[1];
|
|
281
|
-
const cy = args[2];
|
|
282
|
-
t = mul(mul([1, 0, 0, 1, cx, cy], r), [1, 0, 0, 1, -cx, -cy]);
|
|
283
|
-
}
|
|
284
|
-
else {
|
|
285
|
-
t = r;
|
|
286
|
-
}
|
|
287
|
-
break;
|
|
288
|
-
}
|
|
289
|
-
case 'skewX':
|
|
290
|
-
t = [1, 0, Math.tan(((args[0] ?? 0) * Math.PI) / 180), 1, 0, 0];
|
|
291
|
-
break;
|
|
292
|
-
case 'skewY':
|
|
293
|
-
t = [1, Math.tan(((args[0] ?? 0) * Math.PI) / 180), 0, 1, 0, 0];
|
|
294
|
-
break;
|
|
295
|
-
default:
|
|
296
|
-
return unsupported(`transform ${match[1]}`);
|
|
297
|
-
}
|
|
298
|
-
m = mul(m, t);
|
|
299
|
-
}
|
|
300
|
-
if (value.slice(consumed).trim() !== '')
|
|
301
|
-
unsupported('transform syntax');
|
|
302
|
-
return m;
|
|
303
|
-
}
|
|
304
|
-
// ------------------------------------------------------------ path data
|
|
305
|
-
const KAPPA = 0.5522847498307936;
|
|
306
|
-
/** Endpoint-parameterised elliptical arc → cubic Béziers (SVG 1.1 §F.6). */
|
|
307
|
-
function arcToCubics(x1, y1, rx, ry, phiDeg, largeArc, sweep, x2, y2) {
|
|
308
|
-
if (x1 === x2 && y1 === y2)
|
|
309
|
-
return [];
|
|
310
|
-
rx = Math.abs(rx);
|
|
311
|
-
ry = Math.abs(ry);
|
|
312
|
-
if (rx === 0 || ry === 0)
|
|
313
|
-
return [['L', x2, y2]];
|
|
314
|
-
const phi = (phiDeg * Math.PI) / 180;
|
|
315
|
-
const cosPhi = Math.cos(phi);
|
|
316
|
-
const sinPhi = Math.sin(phi);
|
|
317
|
-
const dx = (x1 - x2) / 2;
|
|
318
|
-
const dy = (y1 - y2) / 2;
|
|
319
|
-
const x1p = cosPhi * dx + sinPhi * dy;
|
|
320
|
-
const y1p = -sinPhi * dx + cosPhi * dy;
|
|
321
|
-
const lambda = (x1p * x1p) / (rx * rx) + (y1p * y1p) / (ry * ry);
|
|
322
|
-
if (lambda > 1) {
|
|
323
|
-
const s = Math.sqrt(lambda);
|
|
324
|
-
rx *= s;
|
|
325
|
-
ry *= s;
|
|
326
|
-
}
|
|
327
|
-
const rx2 = rx * rx;
|
|
328
|
-
const ry2 = ry * ry;
|
|
329
|
-
const num = Math.max(0, rx2 * ry2 - rx2 * y1p * y1p - ry2 * x1p * x1p);
|
|
330
|
-
const den = rx2 * y1p * y1p + ry2 * x1p * x1p;
|
|
331
|
-
let coef = den === 0 ? 0 : Math.sqrt(num / den);
|
|
332
|
-
if (largeArc === sweep)
|
|
333
|
-
coef = -coef;
|
|
334
|
-
const cxp = (coef * rx * y1p) / ry;
|
|
335
|
-
const cyp = (-coef * ry * x1p) / rx;
|
|
336
|
-
const cx = cosPhi * cxp - sinPhi * cyp + (x1 + x2) / 2;
|
|
337
|
-
const cy = sinPhi * cxp + cosPhi * cyp + (y1 + y2) / 2;
|
|
338
|
-
const angle = (ux, uy, vx, vy) => {
|
|
339
|
-
const dot = ux * vx + uy * vy;
|
|
340
|
-
const len = Math.hypot(ux, uy) * Math.hypot(vx, vy);
|
|
341
|
-
let a = Math.acos(Math.max(-1, Math.min(1, dot / len)));
|
|
342
|
-
if (ux * vy - uy * vx < 0)
|
|
343
|
-
a = -a;
|
|
344
|
-
return a;
|
|
345
|
-
};
|
|
346
|
-
const theta1 = angle(1, 0, (x1p - cxp) / rx, (y1p - cyp) / ry);
|
|
347
|
-
let dTheta = angle((x1p - cxp) / rx, (y1p - cyp) / ry, (-x1p - cxp) / rx, (-y1p - cyp) / ry);
|
|
348
|
-
if (!sweep && dTheta > 0)
|
|
349
|
-
dTheta -= 2 * Math.PI;
|
|
350
|
-
else if (sweep && dTheta < 0)
|
|
351
|
-
dTheta += 2 * Math.PI;
|
|
352
|
-
const segments = Math.max(1, Math.ceil(Math.abs(dTheta) / (Math.PI / 2) - 1e-9));
|
|
353
|
-
const delta = dTheta / segments;
|
|
354
|
-
const t = (4 / 3) * Math.tan(delta / 4);
|
|
355
|
-
const out = [];
|
|
356
|
-
let theta = theta1;
|
|
357
|
-
for (let i = 0; i < segments; i++) {
|
|
358
|
-
const cos1 = Math.cos(theta);
|
|
359
|
-
const sin1 = Math.sin(theta);
|
|
360
|
-
const cos2 = Math.cos(theta + delta);
|
|
361
|
-
const sin2 = Math.sin(theta + delta);
|
|
362
|
-
// Derivative points on the unit circle, mapped through the ellipse.
|
|
363
|
-
const p = (c, s) => [
|
|
364
|
-
cx + rx * cosPhi * c - ry * sinPhi * s,
|
|
365
|
-
cy + rx * sinPhi * c + ry * cosPhi * s,
|
|
366
|
-
];
|
|
367
|
-
const [ex1, ey1] = p(cos1, sin1);
|
|
368
|
-
const [ex2, ey2] = p(cos2, sin2);
|
|
369
|
-
const d1x = -rx * cosPhi * sin1 - ry * sinPhi * cos1;
|
|
370
|
-
const d1y = -rx * sinPhi * sin1 + ry * cosPhi * cos1;
|
|
371
|
-
const d2x = -rx * cosPhi * sin2 - ry * sinPhi * cos2;
|
|
372
|
-
const d2y = -rx * sinPhi * sin2 + ry * cosPhi * cos2;
|
|
373
|
-
const last = i === segments - 1;
|
|
374
|
-
out.push(['C', ex1 + t * d1x, ey1 + t * d1y, ex2 - t * d2x, ey2 - t * d2y, last ? x2 : ex2, last ? y2 : ey2]);
|
|
375
|
-
theta += delta;
|
|
376
|
-
}
|
|
377
|
-
return out;
|
|
378
|
-
}
|
|
379
|
-
/** Parse SVG path data into absolute M / L / C / Z segments. Relative
|
|
380
|
-
* commands, H / V, smooth curves, quadratics and arcs are all normalised. */
|
|
381
|
-
export function parsePathData(d) {
|
|
382
|
-
const out = [];
|
|
383
|
-
let i = 0;
|
|
384
|
-
const n = d.length;
|
|
385
|
-
let cmd = '';
|
|
386
|
-
let cx = 0;
|
|
387
|
-
let cy = 0;
|
|
388
|
-
let sx = 0;
|
|
389
|
-
let sy = 0;
|
|
390
|
-
// Last cubic / quadratic control point, for S / T reflection.
|
|
391
|
-
let lastC = null;
|
|
392
|
-
let lastQ = null;
|
|
393
|
-
const skipSep = () => {
|
|
394
|
-
while (i < n && /[\s,]/.test(d[i]))
|
|
395
|
-
i++;
|
|
396
|
-
};
|
|
397
|
-
const number = () => {
|
|
398
|
-
skipSep();
|
|
399
|
-
const m = /^[+-]?(\d+\.?\d*|\.\d+)([eE][+-]?\d+)?/.exec(d.slice(i));
|
|
400
|
-
if (!m)
|
|
401
|
-
unsupported('path number');
|
|
402
|
-
i += m[0].length;
|
|
403
|
-
return Number(m[0]);
|
|
404
|
-
};
|
|
405
|
-
const flag = () => {
|
|
406
|
-
skipSep();
|
|
407
|
-
const ch = d[i];
|
|
408
|
-
if (ch !== '0' && ch !== '1')
|
|
409
|
-
unsupported('arc flag');
|
|
410
|
-
i++;
|
|
411
|
-
return ch === '1';
|
|
412
|
-
};
|
|
413
|
-
while (i < n) {
|
|
414
|
-
skipSep();
|
|
415
|
-
if (i >= n)
|
|
416
|
-
break;
|
|
417
|
-
const ch = d[i];
|
|
418
|
-
if (/[a-zA-Z]/.test(ch)) {
|
|
419
|
-
cmd = ch;
|
|
420
|
-
i++;
|
|
421
|
-
}
|
|
422
|
-
else if (!cmd) {
|
|
423
|
-
unsupported('path start');
|
|
424
|
-
}
|
|
425
|
-
else if (cmd === 'M')
|
|
426
|
-
cmd = 'L';
|
|
427
|
-
else if (cmd === 'm')
|
|
428
|
-
cmd = 'l';
|
|
429
|
-
const rel = cmd === cmd.toLowerCase();
|
|
430
|
-
const upper = cmd.toUpperCase();
|
|
431
|
-
const ox = rel ? cx : 0;
|
|
432
|
-
const oy = rel ? cy : 0;
|
|
433
|
-
switch (upper) {
|
|
434
|
-
case 'M': {
|
|
435
|
-
const x = number() + ox;
|
|
436
|
-
const y = number() + oy;
|
|
437
|
-
out.push(['M', x, y]);
|
|
438
|
-
cx = sx = x;
|
|
439
|
-
cy = sy = y;
|
|
440
|
-
lastC = lastQ = null;
|
|
441
|
-
break;
|
|
442
|
-
}
|
|
443
|
-
case 'L': {
|
|
444
|
-
const x = number() + ox;
|
|
445
|
-
const y = number() + oy;
|
|
446
|
-
out.push(['L', x, y]);
|
|
447
|
-
cx = x;
|
|
448
|
-
cy = y;
|
|
449
|
-
lastC = lastQ = null;
|
|
450
|
-
break;
|
|
451
|
-
}
|
|
452
|
-
case 'H': {
|
|
453
|
-
const x = number() + ox;
|
|
454
|
-
out.push(['L', x, cy]);
|
|
455
|
-
cx = x;
|
|
456
|
-
lastC = lastQ = null;
|
|
457
|
-
break;
|
|
458
|
-
}
|
|
459
|
-
case 'V': {
|
|
460
|
-
const y = number() + oy;
|
|
461
|
-
out.push(['L', cx, y]);
|
|
462
|
-
cy = y;
|
|
463
|
-
lastC = lastQ = null;
|
|
464
|
-
break;
|
|
465
|
-
}
|
|
466
|
-
case 'C': {
|
|
467
|
-
const x1 = number() + ox, y1 = number() + oy;
|
|
468
|
-
const x2 = number() + ox, y2 = number() + oy;
|
|
469
|
-
const x = number() + ox, y = number() + oy;
|
|
470
|
-
out.push(['C', x1, y1, x2, y2, x, y]);
|
|
471
|
-
lastC = [x2, y2];
|
|
472
|
-
lastQ = null;
|
|
473
|
-
cx = x;
|
|
474
|
-
cy = y;
|
|
475
|
-
break;
|
|
476
|
-
}
|
|
477
|
-
case 'S': {
|
|
478
|
-
const x2 = number() + ox, y2 = number() + oy;
|
|
479
|
-
const x = number() + ox, y = number() + oy;
|
|
480
|
-
const x1 = lastC ? 2 * cx - lastC[0] : cx;
|
|
481
|
-
const y1 = lastC ? 2 * cy - lastC[1] : cy;
|
|
482
|
-
out.push(['C', x1, y1, x2, y2, x, y]);
|
|
483
|
-
lastC = [x2, y2];
|
|
484
|
-
lastQ = null;
|
|
485
|
-
cx = x;
|
|
486
|
-
cy = y;
|
|
487
|
-
break;
|
|
488
|
-
}
|
|
489
|
-
case 'Q':
|
|
490
|
-
case 'T': {
|
|
491
|
-
let qx, qy;
|
|
492
|
-
if (upper === 'Q') {
|
|
493
|
-
qx = number() + ox;
|
|
494
|
-
qy = number() + oy;
|
|
495
|
-
}
|
|
496
|
-
else {
|
|
497
|
-
qx = lastQ ? 2 * cx - lastQ[0] : cx;
|
|
498
|
-
qy = lastQ ? 2 * cy - lastQ[1] : cy;
|
|
499
|
-
}
|
|
500
|
-
const x = number() + ox, y = number() + oy;
|
|
501
|
-
out.push(['C',
|
|
502
|
-
cx + (2 / 3) * (qx - cx), cy + (2 / 3) * (qy - cy),
|
|
503
|
-
x + (2 / 3) * (qx - x), y + (2 / 3) * (qy - y),
|
|
504
|
-
x, y]);
|
|
505
|
-
lastQ = [qx, qy];
|
|
506
|
-
lastC = null;
|
|
507
|
-
cx = x;
|
|
508
|
-
cy = y;
|
|
509
|
-
break;
|
|
510
|
-
}
|
|
511
|
-
case 'A': {
|
|
512
|
-
const rx = number(), ry = number(), rot = number();
|
|
513
|
-
const large = flag(), sweep = flag();
|
|
514
|
-
const x = number() + ox, y = number() + oy;
|
|
515
|
-
out.push(...arcToCubics(cx, cy, rx, ry, rot, large, sweep, x, y));
|
|
516
|
-
lastC = lastQ = null;
|
|
517
|
-
cx = x;
|
|
518
|
-
cy = y;
|
|
519
|
-
break;
|
|
520
|
-
}
|
|
521
|
-
case 'Z': {
|
|
522
|
-
out.push(['Z']);
|
|
523
|
-
cx = sx;
|
|
524
|
-
cy = sy;
|
|
525
|
-
lastC = lastQ = null;
|
|
526
|
-
break;
|
|
527
|
-
}
|
|
528
|
-
default:
|
|
529
|
-
unsupported(`path command ${cmd}`);
|
|
530
|
-
}
|
|
531
|
-
if (upper === 'Z') {
|
|
532
|
-
// A closepath takes no arguments; the next token must be a command.
|
|
533
|
-
skipSep();
|
|
534
|
-
if (i < n && !/[a-zA-Z]/.test(d[i]))
|
|
535
|
-
unsupported('path data after Z');
|
|
536
|
-
}
|
|
537
|
-
}
|
|
538
|
-
return out;
|
|
539
|
-
}
|
|
540
|
-
// ------------------------------------------------------------ shapes
|
|
541
|
-
function ellipseSegs(cx, cy, rx, ry) {
|
|
542
|
-
const kx = rx * KAPPA;
|
|
543
|
-
const ky = ry * KAPPA;
|
|
544
|
-
return [
|
|
545
|
-
['M', cx + rx, cy],
|
|
546
|
-
['C', cx + rx, cy + ky, cx + kx, cy + ry, cx, cy + ry],
|
|
547
|
-
['C', cx - kx, cy + ry, cx - rx, cy + ky, cx - rx, cy],
|
|
548
|
-
['C', cx - rx, cy - ky, cx - kx, cy - ry, cx, cy - ry],
|
|
549
|
-
['C', cx + kx, cy - ry, cx + rx, cy - ky, cx + rx, cy],
|
|
550
|
-
['Z'],
|
|
551
|
-
];
|
|
552
|
-
}
|
|
553
|
-
function rectSegs(x, y, w, h, rx, ry) {
|
|
554
|
-
if (rx <= 0 || ry <= 0) {
|
|
555
|
-
return [['M', x, y], ['L', x + w, y], ['L', x + w, y + h], ['L', x, y + h], ['Z']];
|
|
556
|
-
}
|
|
557
|
-
rx = Math.min(rx, w / 2);
|
|
558
|
-
ry = Math.min(ry, h / 2);
|
|
559
|
-
const kx = rx * KAPPA;
|
|
560
|
-
const ky = ry * KAPPA;
|
|
561
|
-
return [
|
|
562
|
-
['M', x + rx, y],
|
|
563
|
-
['L', x + w - rx, y],
|
|
564
|
-
['C', x + w - rx + kx, y, x + w, y + ry - ky, x + w, y + ry],
|
|
565
|
-
['L', x + w, y + h - ry],
|
|
566
|
-
['C', x + w, y + h - ry + ky, x + w - rx + kx, y + h, x + w - rx, y + h],
|
|
567
|
-
['L', x + rx, y + h],
|
|
568
|
-
['C', x + rx - kx, y + h, x, y + h - ry + ky, x, y + h - ry],
|
|
569
|
-
['L', x, y + ry],
|
|
570
|
-
['C', x, y + ry - ky, x + rx - kx, y, x + rx, y],
|
|
571
|
-
['Z'],
|
|
572
|
-
];
|
|
573
|
-
}
|
|
574
|
-
function pointsSegs(points, close) {
|
|
575
|
-
const nums = points.trim().split(/[\s,]+/).filter((s) => s !== '').map(Number);
|
|
576
|
-
if (nums.length < 2 || nums.length % 2 !== 0 || nums.some((v) => !Number.isFinite(v))) {
|
|
577
|
-
unsupported('points');
|
|
578
|
-
}
|
|
579
|
-
const segs = [['M', nums[0], nums[1]]];
|
|
580
|
-
for (let i = 2; i < nums.length; i += 2)
|
|
581
|
-
segs.push(['L', nums[i], nums[i + 1]]);
|
|
582
|
-
if (close)
|
|
583
|
-
segs.push(['Z']);
|
|
584
|
-
return segs;
|
|
585
|
-
}
|
|
586
|
-
// ------------------------------------------------------------ styling
|
|
587
|
-
/** Parse a length in user units. Absolute CSS units are converted at 96 dpi
|
|
588
|
-
* (the SVG user unit is a CSS px); relative units are unsupported. */
|
|
589
|
-
function parseLength(value, fallback) {
|
|
590
|
-
if (value === undefined || value.trim() === '')
|
|
591
|
-
return fallback;
|
|
592
|
-
const m = /^\s*([+-]?(?:\d+\.?\d*|\.\d+)(?:[eE][+-]?\d+)?)\s*([a-zA-Z%]*)\s*$/.exec(value);
|
|
593
|
-
if (!m)
|
|
594
|
-
return unsupported(`length ${value}`);
|
|
595
|
-
const num = Number(m[1]);
|
|
596
|
-
const unit = m[2].toLowerCase();
|
|
597
|
-
const factor = { '': 1, px: 1, pt: 96 / 72, pc: 16, in: 96, cm: 96 / 2.54, mm: 96 / 25.4 };
|
|
598
|
-
const f = factor[unit];
|
|
599
|
-
if (f === undefined)
|
|
600
|
-
unsupported(`length unit ${unit}`);
|
|
601
|
-
return num * f;
|
|
602
|
-
}
|
|
603
|
-
function parseNumberOrPercent(value, fallback) {
|
|
604
|
-
const v = value.trim();
|
|
605
|
-
if (v === '')
|
|
606
|
-
return fallback;
|
|
607
|
-
if (v.endsWith('%')) {
|
|
608
|
-
const p = Number(v.slice(0, -1));
|
|
609
|
-
return Number.isFinite(p) ? p / 100 : fallback;
|
|
610
|
-
}
|
|
611
|
-
const num = Number(v);
|
|
612
|
-
return Number.isFinite(num) ? num : fallback;
|
|
613
|
-
}
|
|
614
|
-
const NAMED_COLORS = {
|
|
615
|
-
aliceblue: '#f0f8ff', antiquewhite: '#faebd7', aqua: '#00ffff', aquamarine: '#7fffd4', azure: '#f0ffff',
|
|
616
|
-
beige: '#f5f5dc', bisque: '#ffe4c4', black: '#000000', blanchedalmond: '#ffebcd', blue: '#0000ff',
|
|
617
|
-
blueviolet: '#8a2be2', brown: '#a52a2a', burlywood: '#deb887', cadetblue: '#5f9ea0', chartreuse: '#7fff00',
|
|
618
|
-
chocolate: '#d2691e', coral: '#ff7f50', cornflowerblue: '#6495ed', cornsilk: '#fff8dc', crimson: '#dc143c',
|
|
619
|
-
cyan: '#00ffff', darkblue: '#00008b', darkcyan: '#008b8b', darkgoldenrod: '#b8860b', darkgray: '#a9a9a9',
|
|
620
|
-
darkgreen: '#006400', darkgrey: '#a9a9a9', darkkhaki: '#bdb76b', darkmagenta: '#8b008b', darkolivegreen: '#556b2f',
|
|
621
|
-
darkorange: '#ff8c00', darkorchid: '#9932cc', darkred: '#8b0000', darksalmon: '#e9967a', darkseagreen: '#8fbc8f',
|
|
622
|
-
darkslateblue: '#483d8b', darkslategray: '#2f4f4f', darkslategrey: '#2f4f4f', darkturquoise: '#00ced1',
|
|
623
|
-
darkviolet: '#9400d3', deeppink: '#ff1493', deepskyblue: '#00bfff', dimgray: '#696969', dimgrey: '#696969',
|
|
624
|
-
dodgerblue: '#1e90ff', firebrick: '#b22222', floralwhite: '#fffaf0', forestgreen: '#228b22', fuchsia: '#ff00ff',
|
|
625
|
-
gainsboro: '#dcdcdc', ghostwhite: '#f8f8ff', gold: '#ffd700', goldenrod: '#daa520', gray: '#808080',
|
|
626
|
-
green: '#008000', greenyellow: '#adff2f', grey: '#808080', honeydew: '#f0fff0', hotpink: '#ff69b4',
|
|
627
|
-
indianred: '#cd5c5c', indigo: '#4b0082', ivory: '#fffff0', khaki: '#f0e68c', lavender: '#e6e6fa',
|
|
628
|
-
lavenderblush: '#fff0f5', lawngreen: '#7cfc00', lemonchiffon: '#fffacd', lightblue: '#add8e6', lightcoral: '#f08080',
|
|
629
|
-
lightcyan: '#e0ffff', lightgoldenrodyellow: '#fafad2', lightgray: '#d3d3d3', lightgreen: '#90ee90', lightgrey: '#d3d3d3',
|
|
630
|
-
lightpink: '#ffb6c1', lightsalmon: '#ffa07a', lightseagreen: '#20b2aa', lightskyblue: '#87cefa', lightslategray: '#778899',
|
|
631
|
-
lightslategrey: '#778899', lightsteelblue: '#b0c4de', lightyellow: '#ffffe0', lime: '#00ff00', limegreen: '#32cd32',
|
|
632
|
-
linen: '#faf0e6', magenta: '#ff00ff', maroon: '#800000', mediumaquamarine: '#66cdaa', mediumblue: '#0000cd',
|
|
633
|
-
mediumorchid: '#ba55d3', mediumpurple: '#9370db', mediumseagreen: '#3cb371', mediumslateblue: '#7b68ee',
|
|
634
|
-
mediumspringgreen: '#00fa9a', mediumturquoise: '#48d1cc', mediumvioletred: '#c71585', midnightblue: '#191970',
|
|
635
|
-
mintcream: '#f5fffa', mistyrose: '#ffe4e1', moccasin: '#ffe4b5', navajowhite: '#ffdead', navy: '#000080',
|
|
636
|
-
oldlace: '#fdf5e6', olive: '#808000', olivedrab: '#6b8e23', orange: '#ffa500', orangered: '#ff4500',
|
|
637
|
-
orchid: '#da70d6', palegoldenrod: '#eee8aa', palegreen: '#98fb98', paleturquoise: '#afeeee', palevioletred: '#db7093',
|
|
638
|
-
papayawhip: '#ffefd5', peachpuff: '#ffdab9', peru: '#cd853f', pink: '#ffc0cb', plum: '#dda0dd',
|
|
639
|
-
powderblue: '#b0e0e6', purple: '#800080', rebeccapurple: '#663399', red: '#ff0000', rosybrown: '#bc8f8f',
|
|
640
|
-
royalblue: '#4169e1', saddlebrown: '#8b4513', salmon: '#fa8072', sandybrown: '#f4a460', seagreen: '#2e8b57',
|
|
641
|
-
seashell: '#fff5ee', sienna: '#a0522d', silver: '#c0c0c0', skyblue: '#87ceeb', slateblue: '#6a5acd',
|
|
642
|
-
slategray: '#708090', slategrey: '#708090', snow: '#fffafa', springgreen: '#00ff7f', steelblue: '#4682b4',
|
|
643
|
-
tan: '#d2b48c', teal: '#008080', thistle: '#d8bfd8', tomato: '#ff6347', turquoise: '#40e0d0',
|
|
644
|
-
violet: '#ee82ee', wheat: '#f5deb3', white: '#ffffff', whitesmoke: '#f5f5f5', yellow: '#ffff00',
|
|
645
|
-
yellowgreen: '#9acd32',
|
|
646
|
-
};
|
|
647
|
-
function parsePaint(value) {
|
|
648
|
-
const v = value.trim();
|
|
649
|
-
const lower = v.toLowerCase();
|
|
650
|
-
if (lower === 'none')
|
|
651
|
-
return 'none';
|
|
652
|
-
if (lower === 'transparent')
|
|
653
|
-
return { hex: '#000000', alpha: 0 };
|
|
654
|
-
if (lower === 'currentcolor')
|
|
655
|
-
return 'currentColor';
|
|
656
|
-
if (v.startsWith('#')) {
|
|
657
|
-
const h = v.slice(1);
|
|
658
|
-
if (!/^[0-9a-fA-F]+$/.test(h))
|
|
659
|
-
unsupported(`colour ${v}`);
|
|
660
|
-
const expand = (c) => c + c;
|
|
661
|
-
if (h.length === 3)
|
|
662
|
-
return { hex: `#${expand(h[0])}${expand(h[1])}${expand(h[2])}`.toLowerCase(), alpha: 1 };
|
|
663
|
-
if (h.length === 4) {
|
|
664
|
-
return {
|
|
665
|
-
hex: `#${expand(h[0])}${expand(h[1])}${expand(h[2])}`.toLowerCase(),
|
|
666
|
-
alpha: parseInt(expand(h[3]), 16) / 255,
|
|
667
|
-
};
|
|
668
|
-
}
|
|
669
|
-
if (h.length === 6)
|
|
670
|
-
return { hex: `#${h}`.toLowerCase(), alpha: 1 };
|
|
671
|
-
if (h.length === 8)
|
|
672
|
-
return { hex: `#${h.slice(0, 6)}`.toLowerCase(), alpha: parseInt(h.slice(6), 16) / 255 };
|
|
673
|
-
return unsupported(`colour ${v}`);
|
|
674
|
-
}
|
|
675
|
-
const fn = /^rgba?\(([^)]*)\)$/i.exec(v);
|
|
676
|
-
if (fn) {
|
|
677
|
-
const parts = fn[1].split(/[\s,/]+/).filter((s) => s !== '');
|
|
678
|
-
if (parts.length < 3 || parts.length > 4)
|
|
679
|
-
unsupported(`colour ${v}`);
|
|
680
|
-
const chan = (s) => {
|
|
681
|
-
const c = s.endsWith('%') ? (Number(s.slice(0, -1)) / 100) * 255 : Number(s);
|
|
682
|
-
if (!Number.isFinite(c))
|
|
683
|
-
unsupported(`colour ${v}`);
|
|
684
|
-
return Math.max(0, Math.min(255, Math.round(c)));
|
|
685
|
-
};
|
|
686
|
-
const hex = `#${[parts[0], parts[1], parts[2]].map((p) => chan(p).toString(16).padStart(2, '0')).join('')}`;
|
|
687
|
-
const alpha = parts.length === 4 ? Math.max(0, Math.min(1, parseNumberOrPercent(parts[3], 1))) : 1;
|
|
688
|
-
return { hex, alpha };
|
|
689
|
-
}
|
|
690
|
-
const named = NAMED_COLORS[lower];
|
|
691
|
-
if (named)
|
|
692
|
-
return { hex: named, alpha: 1 };
|
|
693
|
-
// url(#gradient), var(), hsl(), … — no vector equivalent here.
|
|
694
|
-
return unsupported(`paint ${v}`);
|
|
695
|
-
}
|
|
696
|
-
const ROOT_STYLE = {
|
|
697
|
-
fill: { hex: '#000000', alpha: 1 },
|
|
698
|
-
fillOpacity: 1,
|
|
699
|
-
evenOdd: false,
|
|
700
|
-
stroke: 'none',
|
|
701
|
-
strokeOpacity: 1,
|
|
702
|
-
strokeWidth: 1,
|
|
703
|
-
cap: LineCapStyle.Butt,
|
|
704
|
-
join: LineJoinStyle.Miter,
|
|
705
|
-
miterLimit: 4,
|
|
706
|
-
dash: null,
|
|
707
|
-
dashOffset: 0,
|
|
708
|
-
opacity: 1,
|
|
709
|
-
color: { hex: '#000000', alpha: 1 },
|
|
710
|
-
visible: true,
|
|
711
|
-
clipEvenOdd: false,
|
|
712
|
-
fontFamilies: [],
|
|
713
|
-
fontSize: 16,
|
|
714
|
-
fontWeight: 400,
|
|
715
|
-
italic: false,
|
|
716
|
-
textAnchor: 'start',
|
|
717
|
-
direction: 'ltr',
|
|
718
|
-
};
|
|
719
|
-
/** Presentation properties honoured from attributes and `style`. */
|
|
720
|
-
const STYLE_PROPS = new Set([
|
|
721
|
-
'fill', 'fill-opacity', 'fill-rule', 'stroke', 'stroke-opacity', 'stroke-width', 'stroke-linecap',
|
|
722
|
-
'stroke-linejoin', 'stroke-miterlimit', 'stroke-dasharray', 'stroke-dashoffset', 'opacity', 'color',
|
|
723
|
-
'visibility', 'display', 'clip-path', 'clip-rule',
|
|
724
|
-
'font-family', 'font-size', 'font-weight', 'font-style', 'text-anchor', 'direction',
|
|
725
|
-
// Text properties that change glyph placement: accepted only at their defaults.
|
|
726
|
-
'letter-spacing', 'word-spacing', 'writing-mode', 'text-decoration',
|
|
727
|
-
]);
|
|
728
|
-
/** Properties that may appear in `style` and are harmless to ignore
|
|
729
|
-
* (typography for text we never render, rendering hints, Inkscape extras). */
|
|
730
|
-
const IGNORED_STYLE_PROPS = new Set([
|
|
731
|
-
'font-variant', 'font-stretch', 'font-size-adjust',
|
|
732
|
-
'font-feature-settings', 'font-variant-ligatures', 'font-variant-caps', 'font-variant-numeric',
|
|
733
|
-
'font-variant-east-asian', 'line-height', 'text-align',
|
|
734
|
-
'text-rendering', 'unicode-bidi', 'dominant-baseline',
|
|
735
|
-
'baseline-shift', 'kerning', 'glyph-orientation-vertical', 'glyph-orientation-horizontal', 'white-space',
|
|
736
|
-
'inline-size', 'shape-padding', 'shape-margin', 'shape-rendering', 'color-rendering', 'image-rendering',
|
|
737
|
-
'color-interpolation', 'color-interpolation-filters', 'overflow', 'paint-order', 'vector-effect',
|
|
738
|
-
'mix-blend-mode', 'isolation', 'enable-background', 'stop-color', 'stop-opacity', 'cursor', 'pointer-events',
|
|
739
|
-
'transform-origin', 'transform-box',
|
|
740
|
-
]);
|
|
741
|
-
/** Merge presentation attributes and the `style` attribute (which wins) into
|
|
742
|
-
* a flat property map. Unknown properties in `style` bail out unless known
|
|
743
|
-
* to be harmless. */
|
|
744
|
-
function collectProps(attrs) {
|
|
745
|
-
const props = new Map();
|
|
746
|
-
for (const [k, v] of Object.entries(attrs))
|
|
747
|
-
if (STYLE_PROPS.has(k))
|
|
748
|
-
props.set(k, v);
|
|
749
|
-
const style = attrs['style'];
|
|
750
|
-
if (style) {
|
|
751
|
-
for (const decl of style.split(';')) {
|
|
752
|
-
const colon = decl.indexOf(':');
|
|
753
|
-
if (colon < 0) {
|
|
754
|
-
if (decl.trim() !== '')
|
|
755
|
-
unsupported('style syntax');
|
|
756
|
-
continue;
|
|
757
|
-
}
|
|
758
|
-
const name = decl.slice(0, colon).trim().toLowerCase();
|
|
759
|
-
const value = decl.slice(colon + 1).trim().replace(/\s*!important$/i, '');
|
|
760
|
-
if (name.startsWith('-'))
|
|
761
|
-
continue; // vendor / Inkscape prefixed
|
|
762
|
-
if (STYLE_PROPS.has(name))
|
|
763
|
-
props.set(name, value);
|
|
764
|
-
else if (!IGNORED_STYLE_PROPS.has(name))
|
|
765
|
-
unsupported(`style ${name}`);
|
|
766
|
-
}
|
|
767
|
-
}
|
|
768
|
-
return props;
|
|
769
|
-
}
|
|
770
|
-
function cascade(parent, attrs) {
|
|
771
|
-
const props = collectProps(attrs);
|
|
772
|
-
const next = { ...parent, opacity: parent.opacity };
|
|
773
|
-
const get = (name) => {
|
|
774
|
-
const v = props.get(name);
|
|
775
|
-
if (v === undefined || v.trim().toLowerCase() === 'inherit')
|
|
776
|
-
return undefined;
|
|
777
|
-
return v;
|
|
778
|
-
};
|
|
779
|
-
const color = get('color');
|
|
780
|
-
if (color !== undefined) {
|
|
781
|
-
const c = parsePaint(color);
|
|
782
|
-
if (c === 'none' || c === 'currentColor')
|
|
783
|
-
unsupported('color value');
|
|
784
|
-
next.color = c;
|
|
785
|
-
}
|
|
786
|
-
const fill = get('fill');
|
|
787
|
-
if (fill !== undefined)
|
|
788
|
-
next.fill = parsePaint(fill);
|
|
789
|
-
const fillOpacity = get('fill-opacity');
|
|
790
|
-
if (fillOpacity !== undefined)
|
|
791
|
-
next.fillOpacity = Math.max(0, Math.min(1, parseNumberOrPercent(fillOpacity, 1)));
|
|
792
|
-
const fillRule = get('fill-rule');
|
|
793
|
-
if (fillRule !== undefined)
|
|
794
|
-
next.evenOdd = fillRule.trim() === 'evenodd';
|
|
795
|
-
const clipRule = get('clip-rule');
|
|
796
|
-
if (clipRule !== undefined)
|
|
797
|
-
next.clipEvenOdd = clipRule.trim() === 'evenodd';
|
|
798
|
-
const stroke = get('stroke');
|
|
799
|
-
if (stroke !== undefined)
|
|
800
|
-
next.stroke = parsePaint(stroke);
|
|
801
|
-
const strokeOpacity = get('stroke-opacity');
|
|
802
|
-
if (strokeOpacity !== undefined)
|
|
803
|
-
next.strokeOpacity = Math.max(0, Math.min(1, parseNumberOrPercent(strokeOpacity, 1)));
|
|
804
|
-
const strokeWidth = get('stroke-width');
|
|
805
|
-
if (strokeWidth !== undefined) {
|
|
806
|
-
if (strokeWidth.trim().endsWith('%'))
|
|
807
|
-
unsupported('percent stroke width');
|
|
808
|
-
next.strokeWidth = Math.max(0, parseLength(strokeWidth, 1));
|
|
809
|
-
}
|
|
810
|
-
const cap = get('stroke-linecap');
|
|
811
|
-
if (cap !== undefined) {
|
|
812
|
-
const v = cap.trim();
|
|
813
|
-
next.cap = v === 'round' ? LineCapStyle.Round : v === 'square' ? LineCapStyle.Projecting : LineCapStyle.Butt;
|
|
814
|
-
}
|
|
815
|
-
const join = get('stroke-linejoin');
|
|
816
|
-
if (join !== undefined) {
|
|
817
|
-
const v = join.trim();
|
|
818
|
-
next.join = v === 'round' ? LineJoinStyle.Round : v === 'bevel' ? LineJoinStyle.Bevel : LineJoinStyle.Miter;
|
|
819
|
-
}
|
|
820
|
-
const miter = get('stroke-miterlimit');
|
|
821
|
-
if (miter !== undefined)
|
|
822
|
-
next.miterLimit = Math.max(1, Number(miter) || 4);
|
|
823
|
-
const dash = get('stroke-dasharray');
|
|
824
|
-
if (dash !== undefined) {
|
|
825
|
-
const v = dash.trim().toLowerCase();
|
|
826
|
-
if (v === 'none' || v === '')
|
|
827
|
-
next.dash = null;
|
|
828
|
-
else {
|
|
829
|
-
const parts = v.split(/[\s,]+/).filter((s) => s !== '').map((s) => parseLength(s, 0));
|
|
830
|
-
if (parts.some((p) => p < 0))
|
|
831
|
-
unsupported('negative dash');
|
|
832
|
-
next.dash = parts.every((p) => p === 0) ? null : parts;
|
|
833
|
-
}
|
|
834
|
-
}
|
|
835
|
-
const dashOffset = get('stroke-dashoffset');
|
|
836
|
-
if (dashOffset !== undefined)
|
|
837
|
-
next.dashOffset = parseLength(dashOffset, 0);
|
|
838
|
-
const opacity = get('opacity');
|
|
839
|
-
if (opacity !== undefined)
|
|
840
|
-
next.opacity = parent.opacity * Math.max(0, Math.min(1, parseNumberOrPercent(opacity, 1)));
|
|
841
|
-
const visibility = get('visibility');
|
|
842
|
-
if (visibility !== undefined)
|
|
843
|
-
next.visible = visibility.trim() !== 'hidden' && visibility.trim() !== 'collapse';
|
|
844
|
-
const fontFamily = get('font-family');
|
|
845
|
-
if (fontFamily !== undefined) {
|
|
846
|
-
next.fontFamilies = fontFamily.split(',').map((f) => f.trim().replace(/^['"]|['"]$/g, '')).filter((f) => f !== '');
|
|
847
|
-
}
|
|
848
|
-
const fontSize = get('font-size');
|
|
849
|
-
if (fontSize !== undefined) {
|
|
850
|
-
const v = fontSize.trim();
|
|
851
|
-
if (/[%]$|em$|rem$|ex$|ch$/.test(v) || /^[a-z-]+$/i.test(v))
|
|
852
|
-
unsupported('relative font-size');
|
|
853
|
-
next.fontSize = parseLength(v, 16);
|
|
854
|
-
}
|
|
855
|
-
const fontWeight = get('font-weight');
|
|
856
|
-
if (fontWeight !== undefined) {
|
|
857
|
-
const v = fontWeight.trim().toLowerCase();
|
|
858
|
-
if (v === 'normal')
|
|
859
|
-
next.fontWeight = 400;
|
|
860
|
-
else if (v === 'bold')
|
|
861
|
-
next.fontWeight = 700;
|
|
862
|
-
else if (v === 'bolder')
|
|
863
|
-
next.fontWeight = Math.min(900, parent.fontWeight + 300);
|
|
864
|
-
else if (v === 'lighter')
|
|
865
|
-
next.fontWeight = Math.max(100, parent.fontWeight - 300);
|
|
866
|
-
else if (/^\d+$/.test(v))
|
|
867
|
-
next.fontWeight = Number(v);
|
|
868
|
-
else
|
|
869
|
-
unsupported('font-weight');
|
|
870
|
-
}
|
|
871
|
-
const fontStyle = get('font-style');
|
|
872
|
-
if (fontStyle !== undefined)
|
|
873
|
-
next.italic = fontStyle.trim() !== 'normal';
|
|
874
|
-
const anchor = get('text-anchor');
|
|
875
|
-
if (anchor !== undefined) {
|
|
876
|
-
const v = anchor.trim();
|
|
877
|
-
next.textAnchor = v === 'middle' ? 'middle' : v === 'end' ? 'end' : 'start';
|
|
878
|
-
}
|
|
879
|
-
const direction = get('direction');
|
|
880
|
-
if (direction !== undefined && direction.trim() !== 'inherit')
|
|
881
|
-
next.direction = direction.trim() === 'rtl' ? 'rtl' : 'ltr';
|
|
882
|
-
for (const name of ['letter-spacing', 'word-spacing']) {
|
|
883
|
-
const v = get(name);
|
|
884
|
-
if (v !== undefined && v.trim() !== 'normal' && parseLength(v, 0) !== 0)
|
|
885
|
-
unsupported(name);
|
|
886
|
-
}
|
|
887
|
-
const writingMode = get('writing-mode');
|
|
888
|
-
if (writingMode !== undefined && !/^(horizontal-tb|lr|lr-tb|rl|rl-tb)$/.test(writingMode.trim()))
|
|
889
|
-
unsupported('writing-mode');
|
|
890
|
-
const decoration = get('text-decoration');
|
|
891
|
-
if (decoration !== undefined && decoration.trim() !== 'none')
|
|
892
|
-
unsupported('text-decoration');
|
|
893
|
-
return next;
|
|
894
|
-
}
|
|
895
|
-
// ------------------------------------------------------------ conversion
|
|
896
|
-
/** Elements that never render and can be skipped with their subtrees. */
|
|
897
|
-
const SKIPPED = new Set(['title', 'desc', 'metadata', 'defs', 'clipPath', 'style']);
|
|
898
|
-
/** Structural elements traversed like a group. */
|
|
899
|
-
const GROUPS = new Set(['g', 'a']);
|
|
900
|
-
/** Basic shapes. */
|
|
901
|
-
const SHAPES = new Set(['path', 'rect', 'circle', 'ellipse', 'line', 'polyline', 'polygon']);
|
|
902
|
-
/** Walk a child; in probe mode an unsupported subtree is skipped, not fatal. */
|
|
903
|
-
function walkChild(w, el, ctm, style, clips) {
|
|
904
|
-
if (!w.probe) {
|
|
905
|
-
walk(w, el, ctm, style, clips);
|
|
906
|
-
return;
|
|
907
|
-
}
|
|
908
|
-
try {
|
|
909
|
-
walk(w, el, ctm, style, clips);
|
|
910
|
-
}
|
|
911
|
-
catch (err) {
|
|
912
|
-
if (!(err instanceof Unsupported))
|
|
913
|
-
throw err;
|
|
914
|
-
}
|
|
915
|
-
}
|
|
916
|
-
/** Attributes that reposition glyphs individually; none are supported. */
|
|
917
|
-
const TEXT_UNSUPPORTED_ATTRS = ['rotate', 'textLength', 'lengthAdjust'];
|
|
918
|
-
/** Single numeric value of an `x` / `y` / `dx` / `dy` list, or undefined. */
|
|
919
|
-
function singleCoord(value) {
|
|
920
|
-
if (value === undefined)
|
|
921
|
-
return undefined;
|
|
922
|
-
const parts = value.trim().split(/[\s,]+/).filter((v) => v !== '');
|
|
923
|
-
if (parts.length === 0)
|
|
924
|
-
return undefined;
|
|
925
|
-
if (parts.length > 1)
|
|
926
|
-
unsupported('per-glyph positioning');
|
|
927
|
-
return parseLength(parts[0], 0);
|
|
928
|
-
}
|
|
929
|
-
/** Collapse whitespace the way `xml:space="default"` does for SVG text. */
|
|
930
|
-
function collapseWhitespace(raw) {
|
|
931
|
-
return raw.replace(/[\n\r\t]/g, ' ').replace(/ {2,}/g, ' ');
|
|
932
|
-
}
|
|
933
|
-
/** Lay out a `text` element: walks its runs and `tspan`s, positions each run
|
|
934
|
-
* on the current pen, then applies `text-anchor` per absolutely positioned
|
|
935
|
-
* chunk. Text in a font the resolver cannot provide bails out. */
|
|
936
|
-
function layoutText(w, el, m, style, clips) {
|
|
937
|
-
if (!w.fonts)
|
|
938
|
-
unsupported('text (no font resolver)');
|
|
939
|
-
const resolver = w.fonts;
|
|
940
|
-
const chunks = [];
|
|
941
|
-
let pen = { x: 0, y: 0 };
|
|
942
|
-
let chunk = null;
|
|
943
|
-
let pendingSpace = false;
|
|
944
|
-
let started = false;
|
|
945
|
-
// Each run's advance, as it was laid out.
|
|
946
|
-
const widths = new Map();
|
|
947
|
-
const visit = (node, nm, ns, isRoot) => {
|
|
948
|
-
for (const a of TEXT_UNSUPPORTED_ATTRS)
|
|
949
|
-
if (node.attrs[a] !== undefined)
|
|
950
|
-
unsupported(a);
|
|
951
|
-
if (node.attrs['transform'] !== undefined && !isRoot)
|
|
952
|
-
unsupported('tspan transform');
|
|
953
|
-
const x = singleCoord(node.attrs['x']);
|
|
954
|
-
const y = singleCoord(node.attrs['y']);
|
|
955
|
-
const dx = singleCoord(node.attrs['dx']) ?? 0;
|
|
956
|
-
const dy = singleCoord(node.attrs['dy']) ?? 0;
|
|
957
|
-
if (x !== undefined)
|
|
958
|
-
pen = { ...pen, x };
|
|
959
|
-
if (y !== undefined)
|
|
960
|
-
pen = { ...pen, y };
|
|
961
|
-
pen = { x: pen.x + dx, y: pen.y + dy };
|
|
962
|
-
if (isRoot || x !== undefined || y !== undefined) {
|
|
963
|
-
chunk = { runs: [], anchor: ns.textAnchor, direction: ns.direction, matrix: nm };
|
|
964
|
-
chunks.push(chunk);
|
|
965
|
-
}
|
|
966
|
-
for (const c of node.content) {
|
|
967
|
-
if (typeof c === 'string') {
|
|
968
|
-
let text = collapseWhitespace(c);
|
|
969
|
-
if (text === '')
|
|
970
|
-
continue;
|
|
971
|
-
if (!started) {
|
|
972
|
-
text = text.replace(/^ +/, '');
|
|
973
|
-
if (text === '')
|
|
974
|
-
continue;
|
|
975
|
-
}
|
|
976
|
-
if (pendingSpace && !text.startsWith(' '))
|
|
977
|
-
text = ' ' + text;
|
|
978
|
-
pendingSpace = text.endsWith(' ');
|
|
979
|
-
text = text.replace(/ +$/, '');
|
|
980
|
-
if (text === '')
|
|
981
|
-
continue;
|
|
982
|
-
started = true;
|
|
983
|
-
const font = resolver(ns.fontFamilies, ns.fontWeight, ns.italic);
|
|
984
|
-
if (!font)
|
|
985
|
-
unsupported(`font ${ns.fontFamilies.join(', ') || '(default)'}`);
|
|
986
|
-
const fillPaint = ns.fill === 'none' ? null : ns.fill === 'currentColor' ? ns.color : ns.fill;
|
|
987
|
-
const alpha = fillPaint ? fillPaint.alpha * ns.fillOpacity * ns.opacity : 0;
|
|
988
|
-
if (ns.stroke !== 'none')
|
|
989
|
-
unsupported('stroked text');
|
|
990
|
-
// Right-to-left and joining scripts are shaped by HarfBuzz, and
|
|
991
|
-
// measured by it; without it they go to the raster embed.
|
|
992
|
-
const complex = needsComplexShaping(text);
|
|
993
|
-
if (complex && !complexShaperReady())
|
|
994
|
-
unsupported('complex-script text (HarfBuzz not loaded)');
|
|
995
|
-
const shaped = complex && font.pdfFont ? shapedSvgText(font.pdfFont, text, ns.fontSize, ns.direction) : null;
|
|
996
|
-
const width = shaped ? shaped.advance : font.widthOf(text, ns.fontSize);
|
|
997
|
-
if (fillPaint && alpha > 0 && ns.visible) {
|
|
998
|
-
const run = {
|
|
999
|
-
text, font: font, size: ns.fontSize, x: pen.x, y: pen.y, matrix: nm,
|
|
1000
|
-
fill: { hex: fillPaint.hex, alpha },
|
|
1001
|
-
...(complex ? { base: ns.direction } : {}),
|
|
1002
|
-
};
|
|
1003
|
-
chunk.runs.push(run);
|
|
1004
|
-
widths.set(run, width);
|
|
1005
|
-
}
|
|
1006
|
-
pen = { x: pen.x + width, y: pen.y };
|
|
1007
|
-
continue;
|
|
1008
|
-
}
|
|
1009
|
-
const { local, foreign } = localName(c.name);
|
|
1010
|
-
if (foreign)
|
|
1011
|
-
continue;
|
|
1012
|
-
if (local !== 'tspan')
|
|
1013
|
-
unsupported(`text child ${local}`);
|
|
1014
|
-
if (c.attrs['display']?.trim() === 'none')
|
|
1015
|
-
continue;
|
|
1016
|
-
const cs = cascade(ns, c.attrs);
|
|
1017
|
-
visit(c, nm, cs, false);
|
|
1018
|
-
}
|
|
1019
|
-
};
|
|
1020
|
-
visit(el, m, style, true);
|
|
1021
|
-
const runs = [];
|
|
1022
|
-
for (const ch of chunks) {
|
|
1023
|
-
if (ch.runs.length === 0)
|
|
1024
|
-
continue;
|
|
1025
|
-
const first = ch.runs[0];
|
|
1026
|
-
const last = ch.runs[ch.runs.length - 1];
|
|
1027
|
-
const width = last.x + (widths.get(last) ?? 0) - first.x;
|
|
1028
|
-
// A right-to-left chunk runs leftwards from its start: its `tspan`s
|
|
1029
|
-
// take their places from the right, and `start` names its right end.
|
|
1030
|
-
const rtl = ch.direction === 'rtl';
|
|
1031
|
-
const placed = rtl ? ch.runs.map((r) => ({ ...r, x: first.x + width - (r.x - first.x) - (widths.get(r) ?? 0) })) : ch.runs;
|
|
1032
|
-
const anchor = rtl ? (ch.anchor === 'start' ? 'end' : ch.anchor === 'end' ? 'start' : 'middle') : ch.anchor;
|
|
1033
|
-
const shift = anchor === 'middle' ? -width / 2 : anchor === 'end' ? -width : 0;
|
|
1034
|
-
for (const r of placed)
|
|
1035
|
-
runs.push(shift === 0 ? r : { ...r, x: r.x + shift });
|
|
1036
|
-
}
|
|
1037
|
-
if (runs.length > 0)
|
|
1038
|
-
w.shapes.push({ kind: 'text', runs, clips });
|
|
1039
|
-
}
|
|
1040
|
-
function shapeSegs(el) {
|
|
1041
|
-
const a = el.attrs;
|
|
1042
|
-
const len = (name, fallback = 0) => parseLength(a[name], fallback);
|
|
1043
|
-
switch (el.name) {
|
|
1044
|
-
case 'path': {
|
|
1045
|
-
const d = a['d'];
|
|
1046
|
-
if (!d || d.trim() === '')
|
|
1047
|
-
return [];
|
|
1048
|
-
return parsePathData(d);
|
|
1049
|
-
}
|
|
1050
|
-
case 'rect': {
|
|
1051
|
-
const w = len('width');
|
|
1052
|
-
const h = len('height');
|
|
1053
|
-
if (w <= 0 || h <= 0)
|
|
1054
|
-
return [];
|
|
1055
|
-
let rx = a['rx'] !== undefined && a['rx'] !== 'auto' ? len('rx') : -1;
|
|
1056
|
-
let ry = a['ry'] !== undefined && a['ry'] !== 'auto' ? len('ry') : -1;
|
|
1057
|
-
if (rx < 0 && ry < 0)
|
|
1058
|
-
rx = ry = 0;
|
|
1059
|
-
else if (rx < 0)
|
|
1060
|
-
rx = ry;
|
|
1061
|
-
else if (ry < 0)
|
|
1062
|
-
ry = rx;
|
|
1063
|
-
return rectSegs(len('x'), len('y'), w, h, rx, ry);
|
|
1064
|
-
}
|
|
1065
|
-
case 'circle': {
|
|
1066
|
-
const r = len('r');
|
|
1067
|
-
return r > 0 ? ellipseSegs(len('cx'), len('cy'), r, r) : [];
|
|
1068
|
-
}
|
|
1069
|
-
case 'ellipse': {
|
|
1070
|
-
const rx = a['rx'] === 'auto' ? len('ry') : len('rx');
|
|
1071
|
-
const ry = a['ry'] === 'auto' ? len('rx') : len('ry');
|
|
1072
|
-
return rx > 0 && ry > 0 ? ellipseSegs(len('cx'), len('cy'), rx, ry) : [];
|
|
1073
|
-
}
|
|
1074
|
-
case 'line':
|
|
1075
|
-
return [['M', len('x1'), len('y1')], ['L', len('x2'), len('y2')]];
|
|
1076
|
-
case 'polyline':
|
|
1077
|
-
return a['points'] ? pointsSegs(a['points'], false) : [];
|
|
1078
|
-
case 'polygon':
|
|
1079
|
-
return a['points'] ? pointsSegs(a['points'], true) : [];
|
|
1080
|
-
default:
|
|
1081
|
-
return unsupported(el.name);
|
|
1082
|
-
}
|
|
1083
|
-
}
|
|
1084
|
-
function hrefOf(el) {
|
|
1085
|
-
return el.attrs['href'] ?? el.attrs['xlink:href'];
|
|
1086
|
-
}
|
|
1087
|
-
function resolveClip(w, attrs, ctm, style) {
|
|
1088
|
-
const raw = attrs['clip-path'];
|
|
1089
|
-
if (raw === undefined)
|
|
1090
|
-
return null;
|
|
1091
|
-
const v = raw.trim();
|
|
1092
|
-
if (v === 'none' || v === '')
|
|
1093
|
-
return null;
|
|
1094
|
-
const m = /^url\(\s*['"]?#([^'")\s]+)['"]?\s*\)$/.exec(v);
|
|
1095
|
-
if (!m)
|
|
1096
|
-
return unsupported(`clip-path ${v}`);
|
|
1097
|
-
const target = w.byId.get(m[1]);
|
|
1098
|
-
if (!target || target.name !== 'clipPath')
|
|
1099
|
-
return unsupported('clip-path target');
|
|
1100
|
-
const units = target.attrs['clipPathUnits'];
|
|
1101
|
-
if (units !== undefined && units !== 'userSpaceOnUse')
|
|
1102
|
-
unsupported('clipPathUnits');
|
|
1103
|
-
if (target.attrs['clip-path'] !== undefined)
|
|
1104
|
-
unsupported('nested clip-path');
|
|
1105
|
-
const clipCtm = mul(ctm, parseTransform(target.attrs['transform']));
|
|
1106
|
-
const clipStyle = cascade(style, target.attrs);
|
|
1107
|
-
const segs = [];
|
|
1108
|
-
let evenOdd = clipStyle.clipEvenOdd;
|
|
1109
|
-
const visit = (el, m2, s, depth) => {
|
|
1110
|
-
if (depth > 16)
|
|
1111
|
-
unsupported('clip recursion');
|
|
1112
|
-
const { local, foreign } = localName(el.name);
|
|
1113
|
-
if (foreign || local === 'title' || local === 'desc' || local === 'metadata')
|
|
1114
|
-
return;
|
|
1115
|
-
const s2 = cascade(s, el.attrs);
|
|
1116
|
-
if (el.attrs['display']?.trim() === 'none')
|
|
1117
|
-
return;
|
|
1118
|
-
const m3 = mul(m2, parseTransform(el.attrs['transform']));
|
|
1119
|
-
if (SHAPES.has(local)) {
|
|
1120
|
-
evenOdd = s2.clipEvenOdd;
|
|
1121
|
-
segs.push(...transformSegs(shapeSegs({ ...el, name: local }), m3));
|
|
1122
|
-
}
|
|
1123
|
-
else if (local === 'use') {
|
|
1124
|
-
const href = hrefOf(el);
|
|
1125
|
-
const id = href?.startsWith('#') ? href.slice(1) : undefined;
|
|
1126
|
-
const ref = id ? w.byId.get(id) : undefined;
|
|
1127
|
-
if (!ref)
|
|
1128
|
-
unsupported('use target');
|
|
1129
|
-
const m4 = mul(m3, [1, 0, 0, 1, parseLength(el.attrs['x'], 0), parseLength(el.attrs['y'], 0)]);
|
|
1130
|
-
visit(ref, m4, s2, depth + 1);
|
|
1131
|
-
}
|
|
1132
|
-
else {
|
|
1133
|
-
unsupported(`clipPath child ${local}`);
|
|
1134
|
-
}
|
|
1135
|
-
};
|
|
1136
|
-
for (const child of target.children)
|
|
1137
|
-
visit(child, clipCtm, clipStyle, 0);
|
|
1138
|
-
return { segs, evenOdd };
|
|
1139
|
-
}
|
|
1140
|
-
function walk(w, el, ctm, parentStyle, clips) {
|
|
1141
|
-
if (w.depth > 64)
|
|
1142
|
-
unsupported('nesting depth');
|
|
1143
|
-
const { local, foreign } = localName(el.name);
|
|
1144
|
-
if (foreign)
|
|
1145
|
-
return; // inkscape:*, sodipodi:*, rdf:* …
|
|
1146
|
-
if (SKIPPED.has(local)) {
|
|
1147
|
-
// A sheet of `@font-face` rules only (the faces an author embedded,
|
|
1148
|
-
// #630) changes no geometry: its text is set in the embedded fonts.
|
|
1149
|
-
if (local === 'style' && el.hasText && !isFontFaceOnlyStyleSheet(el.content.filter((c) => typeof c === 'string').join(''))) {
|
|
1150
|
-
unsupported('style sheet');
|
|
1151
|
-
}
|
|
1152
|
-
return;
|
|
1153
|
-
}
|
|
1154
|
-
const attrs = el.attrs;
|
|
1155
|
-
if (attrs['display']?.trim() === 'none')
|
|
1156
|
-
return;
|
|
1157
|
-
if (!w.probe) {
|
|
1158
|
-
// Painting-only concerns: a probe still wants the text underneath.
|
|
1159
|
-
if (attrs['mask'] !== undefined && attrs['mask'].trim() !== 'none')
|
|
1160
|
-
unsupported('mask');
|
|
1161
|
-
if (attrs['filter'] !== undefined && attrs['filter'].trim() !== 'none')
|
|
1162
|
-
unsupported('filter');
|
|
1163
|
-
if (attrs['marker-start'] || attrs['marker-mid'] || attrs['marker-end'])
|
|
1164
|
-
unsupported('marker');
|
|
1165
|
-
}
|
|
1166
|
-
const style = cascade(parentStyle, attrs);
|
|
1167
|
-
const m = mul(ctm, parseTransform(attrs['transform']));
|
|
1168
|
-
const clipHere = w.probe ? null : resolveClip(w, attrs, m, style);
|
|
1169
|
-
const nextClips = clipHere ? [...clips, clipHere] : clips;
|
|
1170
|
-
if (GROUPS.has(local) || (local === 'svg' && w.depth === 0)) {
|
|
1171
|
-
w.depth++;
|
|
1172
|
-
for (const child of el.children)
|
|
1173
|
-
walkChild(w, child, m, style, nextClips);
|
|
1174
|
-
w.depth--;
|
|
1175
|
-
return;
|
|
1176
|
-
}
|
|
1177
|
-
if (local === 'use') {
|
|
1178
|
-
const href = hrefOf(el);
|
|
1179
|
-
const id = href?.startsWith('#') ? href.slice(1) : undefined;
|
|
1180
|
-
const ref = id ? w.byId.get(id) : undefined;
|
|
1181
|
-
if (!ref)
|
|
1182
|
-
return unsupported('use target');
|
|
1183
|
-
const refLocal = localName(ref.name).local;
|
|
1184
|
-
if (refLocal === 'symbol' || refLocal === 'svg')
|
|
1185
|
-
unsupported('use of symbol');
|
|
1186
|
-
const m2 = mul(m, [1, 0, 0, 1, parseLength(attrs['x'], 0), parseLength(attrs['y'], 0)]);
|
|
1187
|
-
w.depth++;
|
|
1188
|
-
walk(w, ref, m2, style, nextClips);
|
|
1189
|
-
w.depth--;
|
|
1190
|
-
return;
|
|
1191
|
-
}
|
|
1192
|
-
if (SHAPES.has(local)) {
|
|
1193
|
-
if (!style.visible)
|
|
1194
|
-
return;
|
|
1195
|
-
const segs = shapeSegs({ ...el, name: local });
|
|
1196
|
-
if (segs.length === 0)
|
|
1197
|
-
return;
|
|
1198
|
-
const resolve = (p) => {
|
|
1199
|
-
if (p === 'none')
|
|
1200
|
-
return null;
|
|
1201
|
-
return p === 'currentColor' ? style.color : p;
|
|
1202
|
-
};
|
|
1203
|
-
const fillPaint = resolve(style.fill);
|
|
1204
|
-
const strokePaint = resolve(style.stroke);
|
|
1205
|
-
const fillAlpha = fillPaint ? fillPaint.alpha * style.fillOpacity * style.opacity : 0;
|
|
1206
|
-
const strokeAlpha = strokePaint ? strokePaint.alpha * style.strokeOpacity * style.opacity : 0;
|
|
1207
|
-
const scale = lengthScale(m);
|
|
1208
|
-
const strokeWidth = style.strokeWidth * scale;
|
|
1209
|
-
const fillOut = fillPaint && fillAlpha > 0 ? { hex: fillPaint.hex, alpha: fillAlpha } : null;
|
|
1210
|
-
const strokeOut = strokePaint && strokeAlpha > 0 && strokeWidth > 0
|
|
1211
|
-
? {
|
|
1212
|
-
hex: strokePaint.hex,
|
|
1213
|
-
alpha: strokeAlpha,
|
|
1214
|
-
width: strokeWidth,
|
|
1215
|
-
cap: style.cap,
|
|
1216
|
-
join: style.join,
|
|
1217
|
-
miterLimit: style.miterLimit,
|
|
1218
|
-
dash: style.dash ? style.dash.map((d) => d * scale) : null,
|
|
1219
|
-
dashOffset: style.dashOffset * scale,
|
|
1220
|
-
}
|
|
1221
|
-
: null;
|
|
1222
|
-
if (!fillOut && !strokeOut)
|
|
1223
|
-
return;
|
|
1224
|
-
w.shapes.push({ kind: 'path', segs: transformSegs(segs, m), fill: fillOut, evenOdd: style.evenOdd, stroke: strokeOut, clips: nextClips });
|
|
1225
|
-
return;
|
|
1226
|
-
}
|
|
1227
|
-
if (local === 'text') {
|
|
1228
|
-
layoutText(w, el, m, style, nextClips);
|
|
1229
|
-
return;
|
|
1230
|
-
}
|
|
1231
|
-
if (local === 'image') {
|
|
1232
|
-
// A data-URI PNG / JPEG stretched into its box (`preserveAspectRatio`
|
|
1233
|
-
// is not honoured: the box is taken as the picture's extent).
|
|
1234
|
-
if (!style.visible)
|
|
1235
|
-
return;
|
|
1236
|
-
const href = hrefOf(el) ?? '';
|
|
1237
|
-
const dm = /^data:image\/(png|jpeg|jpg);base64,([\s\S]*)$/i.exec(href.trim());
|
|
1238
|
-
if (!dm)
|
|
1239
|
-
return unsupported('image source');
|
|
1240
|
-
const width = parseLength(attrs['width'], 0);
|
|
1241
|
-
const height = parseLength(attrs['height'], 0);
|
|
1242
|
-
if (width <= 0 || height <= 0)
|
|
1243
|
-
return;
|
|
1244
|
-
if (w.probe)
|
|
1245
|
-
return;
|
|
1246
|
-
let data;
|
|
1247
|
-
try {
|
|
1248
|
-
const bin = atob(dm[2].replace(/\s+/g, ''));
|
|
1249
|
-
data = new Uint8Array(bin.length);
|
|
1250
|
-
for (let i = 0; i < bin.length; i++)
|
|
1251
|
-
data[i] = bin.charCodeAt(i);
|
|
1252
|
-
}
|
|
1253
|
-
catch {
|
|
1254
|
-
return unsupported('image data');
|
|
1255
|
-
}
|
|
1256
|
-
w.shapes.push({
|
|
1257
|
-
kind: 'image',
|
|
1258
|
-
data,
|
|
1259
|
-
format: dm[1].toLowerCase() === 'png' ? 'png' : 'jpeg',
|
|
1260
|
-
x: parseLength(attrs['x'], 0),
|
|
1261
|
-
y: parseLength(attrs['y'], 0),
|
|
1262
|
-
width,
|
|
1263
|
-
height,
|
|
1264
|
-
matrix: m,
|
|
1265
|
-
clips: nextClips,
|
|
1266
|
-
});
|
|
1267
|
-
return;
|
|
1268
|
-
}
|
|
1269
|
-
// tspan outside text, svg (nested), symbol, switch, foreignObject,
|
|
1270
|
-
// linearGradient outside defs, pattern, mask, filter, marker, …
|
|
1271
|
-
unsupported(local);
|
|
1272
|
-
}
|
|
1273
|
-
function indexIds(el, byId) {
|
|
1274
|
-
const id = el.attrs['id'];
|
|
1275
|
-
if (id && !byId.has(id))
|
|
1276
|
-
byId.set(id, el);
|
|
1277
|
-
for (const c of el.children)
|
|
1278
|
-
indexIds(c, byId);
|
|
1279
|
-
}
|
|
1280
|
-
function parseAspect(value) {
|
|
1281
|
-
const v = (value ?? 'xMidYMid meet').trim().split(/\s+/);
|
|
1282
|
-
if (v[0] === 'none')
|
|
1283
|
-
return null;
|
|
1284
|
-
const m = /^x(Min|Mid|Max)Y(Min|Mid|Max)$/.exec(v[0] ?? '');
|
|
1285
|
-
if (!m)
|
|
1286
|
-
return unsupported('preserveAspectRatio');
|
|
1287
|
-
const pos = (s) => (s === 'Min' ? 0 : s === 'Mid' ? 0.5 : 1);
|
|
1288
|
-
return { xAlign: pos(m[1]), yAlign: pos(m[2]), slice: v[1] === 'slice' };
|
|
1289
|
-
}
|
|
1290
|
-
/**
|
|
1291
|
-
* Convert SVG markup to a {@link VectorDrawing}, or `null` when the document
|
|
1292
|
-
* uses anything outside the supported subset (see the module comment). Never
|
|
1293
|
-
* throws.
|
|
1294
|
-
*/
|
|
1295
|
-
export function svgToVectorDrawing(svgText, options = {}) {
|
|
1296
|
-
try {
|
|
1297
|
-
const doc = parseXml(svgText);
|
|
1298
|
-
if (!doc)
|
|
1299
|
-
return null;
|
|
1300
|
-
const root = doc.children.find((c) => localName(c.name).local === 'svg' && !localName(c.name).foreign);
|
|
1301
|
-
if (!root || doc.children.some((c) => localName(c.name).local !== 'svg'))
|
|
1302
|
-
return null;
|
|
1303
|
-
const byId = new Map();
|
|
1304
|
-
indexIds(root, byId);
|
|
1305
|
-
let viewBox;
|
|
1306
|
-
const vb = root.attrs['viewBox'];
|
|
1307
|
-
if (vb) {
|
|
1308
|
-
const parts = vb.trim().split(/[\s,]+/).map(Number);
|
|
1309
|
-
if (parts.length !== 4 || parts.some((p) => !Number.isFinite(p)) || parts[2] <= 0 || parts[3] <= 0)
|
|
1310
|
-
return null;
|
|
1311
|
-
viewBox = [parts[0], parts[1], parts[2], parts[3]];
|
|
1312
|
-
}
|
|
1313
|
-
else {
|
|
1314
|
-
const w = root.attrs['width'];
|
|
1315
|
-
const h = root.attrs['height'];
|
|
1316
|
-
if (w?.trim().endsWith('%') || h?.trim().endsWith('%'))
|
|
1317
|
-
return null;
|
|
1318
|
-
const width = parseLength(w, 300);
|
|
1319
|
-
const height = parseLength(h, 150);
|
|
1320
|
-
if (width <= 0 || height <= 0)
|
|
1321
|
-
return null;
|
|
1322
|
-
viewBox = [0, 0, width, height];
|
|
1323
|
-
}
|
|
1324
|
-
const aspect = parseAspect(root.attrs['preserveAspectRatio']);
|
|
1325
|
-
const walker = { byId, shapes: [], depth: 0, fonts: options.fonts, probe: options.probe === true };
|
|
1326
|
-
// The root's own transform / clip / style apply like a group's.
|
|
1327
|
-
walk(walker, root, IDENTITY, ROOT_STYLE, []);
|
|
1328
|
-
return { viewBox, aspect, shapes: walker.shapes };
|
|
1329
|
-
}
|
|
1330
|
-
catch (err) {
|
|
1331
|
-
if (typeof process !== 'undefined' && process.env?.POSTEXT_SVG_DEBUG)
|
|
1332
|
-
console.warn('svgToVectorDrawing:', err.message);
|
|
1333
|
-
if (err instanceof Unsupported)
|
|
1334
|
-
return null;
|
|
1335
|
-
return null;
|
|
1336
|
-
}
|
|
1337
|
-
}
|
|
1338
|
-
// ------------------------------------------------------------ emission
|
|
1339
|
-
function round(v) {
|
|
1340
|
-
return Math.round(v * 10000) / 10000;
|
|
1341
|
-
}
|
|
1342
|
-
function pathOps(segs) {
|
|
1343
|
-
const ops = [];
|
|
1344
|
-
for (const s of segs) {
|
|
1345
|
-
switch (s[0]) {
|
|
1346
|
-
case 'M':
|
|
1347
|
-
ops.push(moveTo(round(s[1]), round(s[2])));
|
|
1348
|
-
break;
|
|
1349
|
-
case 'L':
|
|
1350
|
-
ops.push(lineTo(round(s[1]), round(s[2])));
|
|
1351
|
-
break;
|
|
1352
|
-
case 'C':
|
|
1353
|
-
ops.push(appendBezierCurve(round(s[1]), round(s[2]), round(s[3]), round(s[4]), round(s[5]), round(s[6])));
|
|
1354
|
-
break;
|
|
1355
|
-
case 'Z':
|
|
1356
|
-
ops.push(closePath());
|
|
1357
|
-
break;
|
|
1358
|
-
}
|
|
1359
|
-
}
|
|
1360
|
-
return ops;
|
|
1361
|
-
}
|
|
1362
|
-
/**
|
|
1363
|
-
* The resources of one form XObject: fonts, alpha states and pictures, each
|
|
1364
|
-
* named once. The form carries its own `/Resources`, so every page that
|
|
1365
|
-
* paints it finds them there.
|
|
1366
|
-
*/
|
|
1367
|
-
class FormResources {
|
|
1368
|
-
context;
|
|
1369
|
-
fonts = new Map();
|
|
1370
|
-
alphas = new Map();
|
|
1371
|
-
images = new Map();
|
|
1372
|
-
fontDict = {};
|
|
1373
|
-
gsDict = {};
|
|
1374
|
-
xobjectDict = {};
|
|
1375
|
-
constructor(context) {
|
|
1376
|
-
this.context = context;
|
|
1377
|
-
}
|
|
1378
|
-
font(font) {
|
|
1379
|
-
let name = this.fonts.get(font);
|
|
1380
|
-
if (!name) {
|
|
1381
|
-
const key = `F${this.fonts.size + 1}`;
|
|
1382
|
-
this.fontDict[key] = font.ref;
|
|
1383
|
-
name = PDFName.of(key);
|
|
1384
|
-
this.fonts.set(font, name);
|
|
1385
|
-
}
|
|
1386
|
-
return name;
|
|
1387
|
-
}
|
|
1388
|
-
/** An ExtGState of the form's own (the print pass's overprint states). */
|
|
1389
|
-
extGState(dict) {
|
|
1390
|
-
const key = `GSp${Object.keys(this.gsDict).length + 1}`;
|
|
1391
|
-
this.gsDict[key] = dict;
|
|
1392
|
-
this.alphas.set(key, PDFName.of(key));
|
|
1393
|
-
return PDFName.of(key);
|
|
1394
|
-
}
|
|
1395
|
-
alpha(ca, CA) {
|
|
1396
|
-
if (ca >= 1 && CA >= 1)
|
|
1397
|
-
return null;
|
|
1398
|
-
const id = `${round(ca)}|${round(CA)}`;
|
|
1399
|
-
let name = this.alphas.get(id);
|
|
1400
|
-
if (!name) {
|
|
1401
|
-
const key = `GSa${this.alphas.size + 1}`;
|
|
1402
|
-
this.gsDict[key] = this.context.obj({ Type: 'ExtGState', ca: round(ca), CA: round(CA) });
|
|
1403
|
-
name = PDFName.of(key);
|
|
1404
|
-
this.alphas.set(id, name);
|
|
1405
|
-
}
|
|
1406
|
-
return setGraphicsState(name);
|
|
1407
|
-
}
|
|
1408
|
-
image(ref) {
|
|
1409
|
-
let name = this.images.get(ref);
|
|
1410
|
-
if (!name) {
|
|
1411
|
-
const key = `Im${this.images.size + 1}`;
|
|
1412
|
-
this.xobjectDict[key] = ref;
|
|
1413
|
-
name = PDFName.of(key);
|
|
1414
|
-
this.images.set(ref, name);
|
|
1415
|
-
}
|
|
1416
|
-
return name;
|
|
1417
|
-
}
|
|
1418
|
-
dict() {
|
|
1419
|
-
const entries = {};
|
|
1420
|
-
if (this.fonts.size > 0)
|
|
1421
|
-
entries.Font = this.fontDict;
|
|
1422
|
-
if (this.alphas.size > 0)
|
|
1423
|
-
entries.ExtGState = this.gsDict;
|
|
1424
|
-
if (this.images.size > 0)
|
|
1425
|
-
entries.XObject = this.xobjectDict;
|
|
1426
|
-
return this.context.obj(entries);
|
|
1427
|
-
}
|
|
1428
|
-
}
|
|
1429
|
-
/** `#rrggbb` with an alpha byte appended (`#rrggbbaa`). */
|
|
1430
|
-
function withAlpha(hex, alpha) {
|
|
1431
|
-
const byte = Math.round(Math.max(0, Math.min(1, alpha)) * 255).toString(16).padStart(2, '0');
|
|
1432
|
-
return `${hex.slice(0, 7)}${byte}`;
|
|
1433
|
-
}
|
|
1434
|
-
/** The operators that paint `drawing`'s items in its root user space. */
|
|
1435
|
-
function drawingOps(drawing, res, colorSpace) {
|
|
1436
|
-
const ops = [];
|
|
1437
|
-
// PDF/X-1a has no transparency: a translucent paint is set as it would
|
|
1438
|
-
// print over the paper, and no alpha state is written.
|
|
1439
|
-
const flatten = isPrintMode(colorSpace) && colorSpace.flattenTransparency;
|
|
1440
|
-
const paint = (hex, alpha) => colorFromHex(flatten && alpha < 1 ? withAlpha(hex, alpha) : hex, colorSpace);
|
|
1441
|
-
const alphaState = (ca, CA) => (flatten ? null : res.alpha(ca, CA));
|
|
1442
|
-
for (const shape of drawing.shapes) {
|
|
1443
|
-
ops.push(pushGraphicsState());
|
|
1444
|
-
for (const c of shape.clips) {
|
|
1445
|
-
ops.push(...pathOps(c.segs), c.evenOdd ? clipEvenOdd() : clip(), endPath());
|
|
1446
|
-
}
|
|
1447
|
-
if (shape.kind === 'image') {
|
|
1448
|
-
if (shape.pdfImage) {
|
|
1449
|
-
// The unit square maps onto the picture's box; the image's rows run
|
|
1450
|
-
// bottom-up in PDF, so the box is flipped inside the y-down root
|
|
1451
|
-
// space to keep the picture upright.
|
|
1452
|
-
const [a, b, c, d, e, f] = mul(shape.matrix, [shape.width, 0, 0, -shape.height, shape.x, shape.y + shape.height]);
|
|
1453
|
-
ops.push(concatTransformationMatrix(round(a), round(b), round(c), round(d), round(e), round(f)), drawObject(res.image(shape.pdfImage.ref)));
|
|
1454
|
-
}
|
|
1455
|
-
ops.push(popGraphicsState());
|
|
1456
|
-
continue;
|
|
1457
|
-
}
|
|
1458
|
-
if (shape.kind === 'text') {
|
|
1459
|
-
for (const run of shape.runs) {
|
|
1460
|
-
const font = run.font.pdfFont;
|
|
1461
|
-
if (!font)
|
|
1462
|
-
continue;
|
|
1463
|
-
const gs = alphaState(run.fill.alpha, 1);
|
|
1464
|
-
ops.push(pushGraphicsState());
|
|
1465
|
-
if (gs)
|
|
1466
|
-
ops.push(gs);
|
|
1467
|
-
// Text matrix: local → root, then undo the root y-flip so glyphs
|
|
1468
|
-
// stand upright, with the pen at the run's baseline start.
|
|
1469
|
-
const [a, b, c, d, e, f] = mul(run.matrix, [1, 0, 0, -1, run.x, run.y]);
|
|
1470
|
-
// A face of several files switches font per stretch of characters;
|
|
1471
|
-
// right-to-left and joining scripts are shaped by HarfBuzz.
|
|
1472
|
-
const shows = (run.base !== undefined ? shapedSvgText(font, run.text, run.size, run.base)?.shows : undefined) ?? textShows(font, run.text, false, true);
|
|
1473
|
-
const body = [];
|
|
1474
|
-
let current = shows[0].font;
|
|
1475
|
-
for (const show of shows) {
|
|
1476
|
-
if (show.font !== current) {
|
|
1477
|
-
body.push(setFontAndSize(res.font(show.font), round(run.size)));
|
|
1478
|
-
current = show.font;
|
|
1479
|
-
}
|
|
1480
|
-
body.push(show.op);
|
|
1481
|
-
}
|
|
1482
|
-
ops.push(setFillingColor(paint(run.fill.hex, run.fill.alpha)), beginText(), setFontAndSize(res.font(shows[0].font), round(run.size)), setTextMatrix(round(a), round(b), round(c), round(d), round(e), round(f)), ...body, endText(), popGraphicsState());
|
|
1483
|
-
}
|
|
1484
|
-
ops.push(popGraphicsState());
|
|
1485
|
-
continue;
|
|
1486
|
-
}
|
|
1487
|
-
const gs = alphaState(shape.fill?.alpha ?? 1, shape.stroke?.alpha ?? 1);
|
|
1488
|
-
if (gs)
|
|
1489
|
-
ops.push(gs);
|
|
1490
|
-
if (shape.fill)
|
|
1491
|
-
ops.push(setFillingColor(paint(shape.fill.hex, shape.fill.alpha)));
|
|
1492
|
-
if (shape.stroke) {
|
|
1493
|
-
const st = shape.stroke;
|
|
1494
|
-
ops.push(setStrokingColor(paint(st.hex, st.alpha)), setLineWidth(round(st.width)), setLineCap(st.cap), setLineJoin(st.join), PDFOperator.of(PDFOperatorNames.SetLineMiterLimit, [PDFNumber.of(round(st.miterLimit))]));
|
|
1495
|
-
if (st.dash)
|
|
1496
|
-
ops.push(setDashPattern(st.dash.map(round), round(st.dashOffset)));
|
|
1497
|
-
}
|
|
1498
|
-
ops.push(...pathOps(shape.segs));
|
|
1499
|
-
if (shape.fill && shape.stroke) {
|
|
1500
|
-
ops.push(shape.evenOdd ? PDFOperator.of(PDFOperatorNames.FillEvenOddAndStroke) : fillAndStroke());
|
|
1501
|
-
}
|
|
1502
|
-
else if (shape.fill) {
|
|
1503
|
-
ops.push(shape.evenOdd ? PDFOperator.of(PDFOperatorNames.FillEvenOdd) : fill());
|
|
1504
|
-
}
|
|
1505
|
-
else {
|
|
1506
|
-
ops.push(stroke());
|
|
1507
|
-
}
|
|
1508
|
-
ops.push(popGraphicsState());
|
|
1509
|
-
}
|
|
1510
|
-
return ops;
|
|
1511
|
-
}
|
|
1512
|
-
/** The forms of each document, by drawing and colour space. */
|
|
1513
|
-
const formsByContext = new WeakMap();
|
|
1514
|
-
/** The form XObject that paints `drawing` in `ctx`'s document, written the
|
|
1515
|
-
* first time the drawing is drawn there (EF-183): an SVG drawn on many
|
|
1516
|
-
* pages (a frame in every page's design, a picture repeated) is stored
|
|
1517
|
-
* once, and each page paints it with `Do`. */
|
|
1518
|
-
function vectorForm(ctx, drawing) {
|
|
1519
|
-
const context = ctx.page.doc.context;
|
|
1520
|
-
let byDrawing = formsByContext.get(context);
|
|
1521
|
-
if (!byDrawing)
|
|
1522
|
-
formsByContext.set(context, (byDrawing = new WeakMap()));
|
|
1523
|
-
let bySpace = byDrawing.get(drawing);
|
|
1524
|
-
if (!bySpace)
|
|
1525
|
-
byDrawing.set(drawing, (bySpace = new Map()));
|
|
1526
|
-
const spaceKey = modeKey(ctx.colorSpace);
|
|
1527
|
-
let form = bySpace.get(spaceKey);
|
|
1528
|
-
if (!form) {
|
|
1529
|
-
const res = new FormResources(context);
|
|
1530
|
-
let ops = drawingOps(drawing, res, ctx.colorSpace);
|
|
1531
|
-
if (isPrintMode(ctx.colorSpace)) {
|
|
1532
|
-
const states = new Map();
|
|
1533
|
-
ops = applyPrintPass(ops, ctx.colorSpace, (flags) => {
|
|
1534
|
-
const key = `${flags.fill}|${flags.stroke}`;
|
|
1535
|
-
let name = states.get(key);
|
|
1536
|
-
if (!name)
|
|
1537
|
-
states.set(key, (name = res.extGState(context.obj(overprintStateDict(flags)))));
|
|
1538
|
-
return name;
|
|
1539
|
-
});
|
|
1540
|
-
}
|
|
1541
|
-
const [vx, vy, vw, vh] = drawing.viewBox;
|
|
1542
|
-
const bbox = [vx, vy, vx + vw, vy + vh];
|
|
1543
|
-
const stream = context.formXObject(ops, { BBox: bbox.map(round), Resources: res.dict() });
|
|
1544
|
-
form = { ref: context.register(stream), stream, bbox };
|
|
1545
|
-
bySpace.set(spaceKey, form);
|
|
1546
|
-
}
|
|
1547
|
-
return form;
|
|
1548
|
-
}
|
|
1549
|
-
/** Resource name of a form on a page, one per (page, form). */
|
|
1550
|
-
const formNames = new WeakMap();
|
|
1551
|
-
function formName(ctx, ref) {
|
|
1552
|
-
let names = formNames.get(ctx.page);
|
|
1553
|
-
if (!names)
|
|
1554
|
-
formNames.set(ctx.page, (names = new Map()));
|
|
1555
|
-
let name = names.get(ref);
|
|
1556
|
-
if (!name) {
|
|
1557
|
-
name = ctx.page.node.newXObject('Vec', ref);
|
|
1558
|
-
names.set(ref, name);
|
|
1559
|
-
}
|
|
1560
|
-
return name;
|
|
1561
|
-
}
|
|
1562
|
-
/**
|
|
1563
|
-
* Paint a {@link VectorDrawing} into the page, fitted to the given box in the
|
|
1564
|
-
* document's pixel space (top-down, like every other `PageCtx` primitive).
|
|
1565
|
-
* The drawing itself is a form XObject written once per document (see
|
|
1566
|
-
* {@link vectorForm}); the page clips to the viewport, maps the drawing's
|
|
1567
|
-
* user space onto it and paints the form.
|
|
1568
|
-
*/
|
|
1569
|
-
export function drawVectorDrawing(ctx, drawing, xPx, yPx, wPx, hPx) {
|
|
1570
|
-
const { scale, pageHeightPt } = ctx;
|
|
1571
|
-
const x0 = xPx * scale;
|
|
1572
|
-
const yTop = pageHeightPt - yPx * scale;
|
|
1573
|
-
const w = wPx * scale;
|
|
1574
|
-
const h = hPx * scale;
|
|
1575
|
-
if (w <= 0 || h <= 0)
|
|
1576
|
-
return;
|
|
1577
|
-
const [vx, vy, vw, vh] = drawing.viewBox;
|
|
1578
|
-
let sx = w / vw;
|
|
1579
|
-
let sy = h / vh;
|
|
1580
|
-
let ox = 0;
|
|
1581
|
-
let oy = 0;
|
|
1582
|
-
if (drawing.aspect) {
|
|
1583
|
-
const s = drawing.aspect.slice ? Math.max(sx, sy) : Math.min(sx, sy);
|
|
1584
|
-
ox = (w - vw * s) * drawing.aspect.xAlign;
|
|
1585
|
-
oy = (h - vh * s) * drawing.aspect.yAlign;
|
|
1586
|
-
sx = sy = s;
|
|
1587
|
-
}
|
|
1588
|
-
const form = vectorForm(ctx, drawing);
|
|
1589
|
-
// The form's box holds every viewport it is drawn in, mapped back into
|
|
1590
|
-
// its user space: inside the viewport SVG shows what a 'meet' drawing
|
|
1591
|
-
// paints past its view box, and a form is clipped to its box.
|
|
1592
|
-
const u0 = vx - ox / sx;
|
|
1593
|
-
const u1 = vx + (w - ox) / sx;
|
|
1594
|
-
const v0 = vy - oy / sy;
|
|
1595
|
-
const v1 = vy + (h - oy) / sy;
|
|
1596
|
-
const box = [
|
|
1597
|
-
Math.min(form.bbox[0], u0), Math.min(form.bbox[1], v0),
|
|
1598
|
-
Math.max(form.bbox[2], u1), Math.max(form.bbox[3], v1),
|
|
1599
|
-
];
|
|
1600
|
-
if (box.some((v, i) => v !== form.bbox[i])) {
|
|
1601
|
-
form.bbox = box;
|
|
1602
|
-
form.stream.dict.set(PDFName.of('BBox'), ctx.page.doc.context.obj(box.map(round)));
|
|
1603
|
-
}
|
|
1604
|
-
ctx.page.pushOperators(pushGraphicsState(),
|
|
1605
|
-
// Clip to the viewport (overflow: hidden on the root svg).
|
|
1606
|
-
rectangle(round(x0), round(yTop - h), round(w), round(h)), clip(), endPath(),
|
|
1607
|
-
// SVG user space → PDF points: scale, flip y, place the viewBox origin.
|
|
1608
|
-
concatTransformationMatrix(round(sx), 0, 0, round(-sy), round(x0 + ox - vx * sx), round(yTop - oy + vy * sy)), drawObject(formName(ctx, form.ref)), popGraphicsState());
|
|
1609
|
-
}
|
|
1610
|
-
//# sourceMappingURL=svgVector.js.map
|