pdf-codec 0.0.0 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,484 @@
1
+ import { Color, LayoutDocument, LayoutFont } from "document-content-model";
2
+ import { z } from "zod";
3
+ //#region src/diagnostics.d.ts
4
+ type PdfDiagnosticSeverity = 'info' | 'warning';
5
+ interface PdfDiagnostic {
6
+ readonly code: string;
7
+ readonly severity: PdfDiagnosticSeverity;
8
+ readonly message: string;
9
+ readonly pageIndex?: number;
10
+ }
11
+ type PdfDiagnosticSink = (diagnostic: PdfDiagnostic) => void;
12
+ declare const NOOP_DIAGNOSTIC_SINK: PdfDiagnosticSink;
13
+ declare class PdfParseError extends Error {
14
+ readonly code: string;
15
+ constructor(code: string, message: string);
16
+ }
17
+ declare class PdfEncryptedError extends PdfParseError {
18
+ constructor(message?: string);
19
+ }
20
+ //#endregion
21
+ //#region src/matrix.d.ts
22
+ interface Point {
23
+ readonly x: number;
24
+ readonly y: number;
25
+ }
26
+ declare function rotatePointAboutCenter(point: Point, center: Point, degrees: number): Point;
27
+ //#endregion
28
+ //#region src/read.d.ts
29
+ interface ReadPdfOptions {
30
+ readonly sink?: PdfDiagnosticSink;
31
+ readonly signal?: AbortSignal;
32
+ }
33
+ declare function readPdf(bytes: Uint8Array<ArrayBuffer>, options?: ReadPdfOptions): LayoutDocument;
34
+ //#endregion
35
+ //#region src/math-types.d.ts
36
+ interface MathColor {
37
+ readonly r: number;
38
+ readonly g: number;
39
+ readonly b: number;
40
+ }
41
+ interface MathGlyphRun {
42
+ readonly kind: 'glyphs';
43
+ readonly xPt: number;
44
+ readonly yPt: number;
45
+ readonly text: string;
46
+ readonly sizePt: number;
47
+ readonly color: MathColor;
48
+ }
49
+ interface MathRule {
50
+ readonly kind: 'rule';
51
+ readonly xPt: number;
52
+ readonly yPt: number;
53
+ readonly widthPt: number;
54
+ readonly heightPt: number;
55
+ readonly color: MathColor;
56
+ }
57
+ interface MathStroke {
58
+ readonly kind: 'stroke';
59
+ readonly points: readonly {
60
+ readonly xPt: number;
61
+ readonly yPt: number;
62
+ }[];
63
+ readonly widthPt: number;
64
+ readonly color: MathColor;
65
+ }
66
+ type MathLayoutItem = MathGlyphRun | MathRule | MathStroke;
67
+ interface MathBox {
68
+ readonly widthPt: number;
69
+ readonly heightPt: number;
70
+ readonly ascentPt: number;
71
+ readonly descentPt: number;
72
+ readonly items: readonly MathLayoutItem[];
73
+ }
74
+ interface MathGlyphMetrics {
75
+ readonly advanceWidthPt: number;
76
+ readonly italicCorrectionPt: number;
77
+ readonly topAccentXPt?: number;
78
+ }
79
+ interface MathFontMetrics {
80
+ readonly ascentPerEm: number;
81
+ readonly descentPerEm: number;
82
+ readonly axisHeightPt: number;
83
+ readonly fractionRuleThicknessPt: number;
84
+ readonly fractionNumeratorShiftUpPt: number;
85
+ readonly fractionNumeratorDisplayShiftUpPt: number;
86
+ readonly fractionDenominatorShiftDownPt: number;
87
+ readonly fractionDenominatorDisplayShiftDownPt: number;
88
+ readonly fractionNumeratorGapMinPt: number;
89
+ readonly fractionDenominatorGapMinPt: number;
90
+ readonly radicalRuleThicknessPt: number;
91
+ readonly radicalExtraAscenderPt: number;
92
+ readonly radicalVerticalGapPt: number;
93
+ readonly radicalKernBeforeDegreePt: number;
94
+ readonly radicalKernAfterDegreePt: number;
95
+ readonly radicalDegreeBottomRaisePercent: number;
96
+ readonly subscriptShiftDownPt: number;
97
+ readonly superscriptShiftUpPt: number;
98
+ readonly superscriptShiftUpCrampedPt: number;
99
+ readonly subSuperscriptGapMinPt: number;
100
+ readonly superscriptBaselineDropMaxPt: number;
101
+ readonly subscriptBaselineDropMinPt: number;
102
+ readonly spaceAfterScriptPt: number;
103
+ readonly upperLimitGapMinPt: number;
104
+ readonly upperLimitBaselineRiseMinPt: number;
105
+ readonly lowerLimitGapMinPt: number;
106
+ readonly lowerLimitBaselineDropMinPt: number;
107
+ readonly stackTopShiftUpPt: number;
108
+ readonly stackBottomShiftDownPt: number;
109
+ readonly stackGapMinPt: number;
110
+ readonly scriptPercentScaleDown: number;
111
+ readonly scriptScriptPercentScaleDown: number;
112
+ readonly defaultRuleThicknessPt: number;
113
+ glyph(codePoint: number, sizePt: number): MathGlyphMetrics | undefined;
114
+ }
115
+ //#endregion
116
+ //#region src/formula.d.ts
117
+ interface PositionedFormula {
118
+ readonly pageIndex: number;
119
+ readonly xPt: number;
120
+ readonly yPt: number;
121
+ readonly box: MathBox;
122
+ }
123
+ //#endregion
124
+ //#region src/afm-widths.d.ts
125
+ type StandardFontName = 'Helvetica' | 'Helvetica-Bold' | 'Helvetica-Oblique' | 'Helvetica-BoldOblique' | 'Times-Roman' | 'Times-Bold' | 'Times-Italic' | 'Times-BoldItalic' | 'Courier' | 'Courier-Bold' | 'Courier-Oblique' | 'Courier-BoldOblique';
126
+ interface FontMetrics {
127
+ readonly ascender: number;
128
+ readonly descender: number;
129
+ readonly capHeight: number;
130
+ readonly xHeight: number;
131
+ readonly italicAngle: number;
132
+ readonly fontBBox: readonly [number, number, number, number];
133
+ readonly underlinePosition: number;
134
+ readonly underlineThickness: number;
135
+ readonly lineHeightEm: number;
136
+ readonly fixedWidth?: number;
137
+ readonly widths: ReadonlyMap<string, number>;
138
+ }
139
+ declare const STANDARD_METRICS: Readonly<Record<StandardFontName, FontMetrics>>;
140
+ //#endregion
141
+ //#region src/winansi.d.ts
142
+ interface WinAnsiSubstitution {
143
+ readonly from: string;
144
+ readonly to: string;
145
+ }
146
+ //#endregion
147
+ //#region src/write.d.ts
148
+ interface WritePdfOptions {
149
+ readonly compress?: boolean;
150
+ readonly signal?: AbortSignal;
151
+ readonly onSubstitution?: (substitution: WinAnsiSubstitution, context: {
152
+ readonly pageIndex: number;
153
+ }) => void;
154
+ readonly formulas?: readonly PositionedFormula[];
155
+ }
156
+ declare function writePdf(doc: LayoutDocument, options?: WritePdfOptions): Uint8Array<ArrayBuffer>;
157
+ //#endregion
158
+ //#region src/codec.d.ts
159
+ declare const PdfBytesSchema: z.ZodCustom<Uint8Array<ArrayBuffer>, Uint8Array<ArrayBuffer>>;
160
+ declare const pdfCodec: z.ZodCodec<z.ZodCustom<Uint8Array<ArrayBuffer>, Uint8Array<ArrayBuffer>>, z.ZodObject<{
161
+ formatVersion: z.ZodLiteral<1>;
162
+ metadata: z.ZodObject<{
163
+ title: z.ZodOptional<z.ZodString>;
164
+ author: z.ZodOptional<z.ZodString>;
165
+ subject: z.ZodOptional<z.ZodString>;
166
+ keywords: z.ZodOptional<z.ZodArray<z.ZodString>>;
167
+ creator: z.ZodOptional<z.ZodString>;
168
+ producer: z.ZodOptional<z.ZodString>;
169
+ createdIso: z.ZodOptional<z.ZodString>;
170
+ modifiedIso: z.ZodOptional<z.ZodString>;
171
+ }, z.core.$strip>;
172
+ pages: z.ZodArray<z.ZodObject<{
173
+ widthPt: z.ZodNumber;
174
+ heightPt: z.ZodNumber;
175
+ items: z.ZodArray<z.ZodDiscriminatedUnion<[z.ZodObject<{
176
+ kind: z.ZodLiteral<"text">;
177
+ text: z.ZodString;
178
+ xPt: z.ZodNumber;
179
+ yPt: z.ZodNumber;
180
+ font: z.ZodObject<{
181
+ family: z.ZodString;
182
+ weight: z.ZodEnum<{
183
+ normal: "normal";
184
+ bold: "bold";
185
+ }>;
186
+ style: z.ZodEnum<{
187
+ normal: "normal";
188
+ italic: "italic";
189
+ }>;
190
+ }, z.core.$strip>;
191
+ sizePt: z.ZodNumber;
192
+ color: z.ZodObject<{
193
+ r: z.ZodNumber;
194
+ g: z.ZodNumber;
195
+ b: z.ZodNumber;
196
+ }, z.core.$strip>;
197
+ widthPt: z.ZodOptional<z.ZodNumber>;
198
+ rotationDeg: z.ZodOptional<z.ZodNumber>;
199
+ underline: z.ZodOptional<z.ZodBoolean>;
200
+ sourcePath: z.ZodOptional<z.ZodString>;
201
+ }, z.core.$strip>, z.ZodObject<{
202
+ kind: z.ZodLiteral<"image">;
203
+ imageId: z.ZodString;
204
+ xPt: z.ZodNumber;
205
+ yPt: z.ZodNumber;
206
+ widthPt: z.ZodNumber;
207
+ heightPt: z.ZodNumber;
208
+ rotationDeg: z.ZodOptional<z.ZodNumber>;
209
+ sourcePath: z.ZodOptional<z.ZodString>;
210
+ }, z.core.$strip>, z.ZodObject<{
211
+ kind: z.ZodLiteral<"rect">;
212
+ xPt: z.ZodNumber;
213
+ yPt: z.ZodNumber;
214
+ widthPt: z.ZodNumber;
215
+ heightPt: z.ZodNumber;
216
+ fill: z.ZodOptional<z.ZodObject<{
217
+ r: z.ZodNumber;
218
+ g: z.ZodNumber;
219
+ b: z.ZodNumber;
220
+ }, z.core.$strip>>;
221
+ stroke: z.ZodOptional<z.ZodObject<{
222
+ color: z.ZodObject<{
223
+ r: z.ZodNumber;
224
+ g: z.ZodNumber;
225
+ b: z.ZodNumber;
226
+ }, z.core.$strip>;
227
+ widthPt: z.ZodNumber;
228
+ }, z.core.$strip>>;
229
+ sourcePath: z.ZodOptional<z.ZodString>;
230
+ }, z.core.$strip>, z.ZodObject<{
231
+ kind: z.ZodLiteral<"line">;
232
+ x1Pt: z.ZodNumber;
233
+ y1Pt: z.ZodNumber;
234
+ x2Pt: z.ZodNumber;
235
+ y2Pt: z.ZodNumber;
236
+ color: z.ZodObject<{
237
+ r: z.ZodNumber;
238
+ g: z.ZodNumber;
239
+ b: z.ZodNumber;
240
+ }, z.core.$strip>;
241
+ widthPt: z.ZodNumber;
242
+ sourcePath: z.ZodOptional<z.ZodString>;
243
+ }, z.core.$strip>, z.ZodObject<{
244
+ kind: z.ZodLiteral<"ellipse">;
245
+ xPt: z.ZodNumber;
246
+ yPt: z.ZodNumber;
247
+ widthPt: z.ZodNumber;
248
+ heightPt: z.ZodNumber;
249
+ fill: z.ZodOptional<z.ZodObject<{
250
+ r: z.ZodNumber;
251
+ g: z.ZodNumber;
252
+ b: z.ZodNumber;
253
+ }, z.core.$strip>>;
254
+ stroke: z.ZodOptional<z.ZodObject<{
255
+ color: z.ZodObject<{
256
+ r: z.ZodNumber;
257
+ g: z.ZodNumber;
258
+ b: z.ZodNumber;
259
+ }, z.core.$strip>;
260
+ widthPt: z.ZodNumber;
261
+ }, z.core.$strip>>;
262
+ sourcePath: z.ZodOptional<z.ZodString>;
263
+ }, z.core.$strip>, z.ZodObject<{
264
+ kind: z.ZodLiteral<"path">;
265
+ subpaths: z.ZodArray<z.ZodObject<{
266
+ startXPt: z.ZodNumber;
267
+ startYPt: z.ZodNumber;
268
+ segments: z.ZodArray<z.ZodDiscriminatedUnion<[z.ZodObject<{
269
+ kind: z.ZodLiteral<"line">;
270
+ xPt: z.ZodNumber;
271
+ yPt: z.ZodNumber;
272
+ }, z.core.$strip>, z.ZodObject<{
273
+ kind: z.ZodLiteral<"cubic">;
274
+ c1xPt: z.ZodNumber;
275
+ c1yPt: z.ZodNumber;
276
+ c2xPt: z.ZodNumber;
277
+ c2yPt: z.ZodNumber;
278
+ xPt: z.ZodNumber;
279
+ yPt: z.ZodNumber;
280
+ }, z.core.$strip>], "kind">>;
281
+ closed: z.ZodBoolean;
282
+ }, z.core.$strip>>;
283
+ fill: z.ZodOptional<z.ZodObject<{
284
+ r: z.ZodNumber;
285
+ g: z.ZodNumber;
286
+ b: z.ZodNumber;
287
+ }, z.core.$strip>>;
288
+ fillRule: z.ZodOptional<z.ZodEnum<{
289
+ nonzero: "nonzero";
290
+ evenodd: "evenodd";
291
+ }>>;
292
+ stroke: z.ZodOptional<z.ZodObject<{
293
+ color: z.ZodObject<{
294
+ r: z.ZodNumber;
295
+ g: z.ZodNumber;
296
+ b: z.ZodNumber;
297
+ }, z.core.$strip>;
298
+ widthPt: z.ZodNumber;
299
+ }, z.core.$strip>>;
300
+ sourcePath: z.ZodOptional<z.ZodString>;
301
+ }, z.core.$strip>, z.ZodObject<{
302
+ kind: z.ZodLiteral<"link">;
303
+ uri: z.ZodString;
304
+ xPt: z.ZodNumber;
305
+ yPt: z.ZodNumber;
306
+ widthPt: z.ZodNumber;
307
+ heightPt: z.ZodNumber;
308
+ sourcePath: z.ZodOptional<z.ZodString>;
309
+ }, z.core.$strip>], "kind">>;
310
+ notes: z.ZodOptional<z.ZodString>;
311
+ }, z.core.$strip>>;
312
+ images: z.ZodRecord<z.ZodString, z.ZodObject<{
313
+ format: z.ZodEnum<{
314
+ png: "png";
315
+ jpeg: "jpeg";
316
+ }>;
317
+ base64: z.ZodString;
318
+ widthPx: z.ZodNumber;
319
+ heightPx: z.ZodNumber;
320
+ }, z.core.$strip>>;
321
+ }, z.core.$strip>>;
322
+ //#endregion
323
+ //#region src/math-font.d.ts
324
+ interface MathFontDescriptorMetrics {
325
+ readonly unitsPerEm: number;
326
+ readonly ascent: number;
327
+ readonly descent: number;
328
+ readonly capHeight: number;
329
+ readonly bboxMin: readonly [number, number];
330
+ readonly bboxMax: readonly [number, number];
331
+ readonly italicAngle: number;
332
+ }
333
+ interface MathFont {
334
+ readonly metrics: MathFontMetrics;
335
+ readonly cffBytes: Uint8Array<ArrayBuffer>;
336
+ readonly descriptor: MathFontDescriptorMetrics;
337
+ glyphId(codePoint: number): number | undefined;
338
+ glyphSpaceWidth(glyphId: number): number;
339
+ }
340
+ interface LoadedMathFont {
341
+ readonly font: MathFont;
342
+ readonly metricsAt: (sizePt: number) => MathFontMetrics;
343
+ }
344
+ declare function loadMathFont(): LoadedMathFont;
345
+ //#endregion
346
+ //#region src/measure.d.ts
347
+ interface UnderlineMetrics {
348
+ readonly offsetPt: number;
349
+ readonly thicknessPt: number;
350
+ }
351
+ interface TextMeasurer {
352
+ widthOfTextAtSize(text: string, font: LayoutFont, sizePt: number): number;
353
+ lineHeightAtSize(font: LayoutFont, sizePt: number): number;
354
+ ascenderAtSize(font: LayoutFont, sizePt: number): number;
355
+ descenderAtSize(font: LayoutFont, sizePt: number): number;
356
+ underlineAtSize(font: LayoutFont, sizePt: number): UnderlineMetrics;
357
+ horizontalScaleFor(font: LayoutFont): number;
358
+ }
359
+ interface StandardFontMeasurerOptions {
360
+ readonly widthCorrectionByFamily?: Readonly<Record<string, number>>;
361
+ }
362
+ declare function createStandardFontMeasurer(options?: StandardFontMeasurerOptions): TextMeasurer;
363
+ //#endregion
364
+ //#region src/text-layout.d.ts
365
+ interface StyledRun {
366
+ readonly text: string;
367
+ readonly font: LayoutFont;
368
+ readonly sizePt: number;
369
+ readonly color: Color;
370
+ readonly underline?: boolean;
371
+ readonly hyperlink?: string;
372
+ readonly sourcePath?: string;
373
+ }
374
+ interface StyledFragment {
375
+ readonly text: string;
376
+ readonly font: LayoutFont;
377
+ readonly sizePt: number;
378
+ readonly color: Color;
379
+ readonly underline?: boolean;
380
+ readonly hyperlink?: string;
381
+ readonly sourcePath?: string;
382
+ }
383
+ interface WrappedLine {
384
+ readonly fragments: readonly (StyledFragment & {
385
+ readonly xOffsetPt: number;
386
+ })[];
387
+ readonly widthPt: number;
388
+ readonly maxSizePt: number;
389
+ readonly ascentPt: number;
390
+ readonly descentPt: number;
391
+ }
392
+ interface WrapOptions {
393
+ readonly breakLongWords?: boolean;
394
+ }
395
+ declare function wrapRunsToWidth(runs: readonly StyledRun[], measurer: TextMeasurer, maxWidthPt: number, options?: WrapOptions): WrappedLine[];
396
+ //#endregion
397
+ //#region src/fonts.d.ts
398
+ interface ResolvedFont {
399
+ readonly standardName: StandardFontName;
400
+ readonly matched: boolean;
401
+ }
402
+ declare function resolveStandardFont(family: string, bold: boolean, italic: boolean): ResolvedFont;
403
+ //#endregion
404
+ //#region src/bytes/crc32.d.ts
405
+ declare function crc32(bytes: Uint8Array<ArrayBuffer>): number;
406
+ //#endregion
407
+ //#region src/bytes/flate.d.ts
408
+ declare const MAX_INFLATE_OUTPUT_BYTES: number;
409
+ type DeflateLevel = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9;
410
+ declare function deflate(data: Uint8Array<ArrayBuffer>, level?: DeflateLevel): Uint8Array<ArrayBuffer>;
411
+ declare function inflate(data: Uint8Array<ArrayBuffer>): Uint8Array<ArrayBuffer>;
412
+ interface InflateResult {
413
+ readonly bytes: Uint8Array<ArrayBuffer>;
414
+ readonly recovered: boolean;
415
+ }
416
+ declare function inflateTolerant(data: Uint8Array<ArrayBuffer>): InflateResult;
417
+ //#endregion
418
+ //#region src/bytes/reader.d.ts
419
+ declare function isAsciiWhitespace(byte: number | undefined): boolean;
420
+ declare class ByteReader {
421
+ private readonly bytes;
422
+ private position;
423
+ constructor(bytes: Uint8Array<ArrayBuffer>);
424
+ get offset(): number;
425
+ get length(): number;
426
+ atEnd(): boolean;
427
+ peek(aheadBy?: number): number | undefined;
428
+ next(): number | undefined;
429
+ mark(): number;
430
+ reset(mark: number): void;
431
+ seek(offset: number): void;
432
+ slice(start: number, end: number): Uint8Array<ArrayBuffer>;
433
+ skipWhitespace(): void;
434
+ matchKeyword(keyword: string): boolean;
435
+ }
436
+ //#endregion
437
+ //#region src/bytes/writer.d.ts
438
+ declare class ByteWriter {
439
+ private readonly chunks;
440
+ private byteLength;
441
+ get length(): number;
442
+ writeBytes(bytes: Uint8Array<ArrayBuffer>): void;
443
+ writeByte(byte: number): void;
444
+ writeAscii(text: string): void;
445
+ toBytes(): Uint8Array<ArrayBuffer>;
446
+ }
447
+ declare function concatBytes(chunks: readonly Uint8Array<ArrayBuffer>[]): Uint8Array<ArrayBuffer>;
448
+ //#endregion
449
+ //#region src/image/jpeg-info.d.ts
450
+ interface JpegInfo {
451
+ readonly width: number;
452
+ readonly height: number;
453
+ readonly components: number;
454
+ readonly precision: number;
455
+ readonly progressive: boolean;
456
+ readonly adobeTransform: number | undefined;
457
+ }
458
+ declare function readJpegInfo(bytes: Uint8Array<ArrayBuffer>): JpegInfo;
459
+ //#endregion
460
+ //#region src/image/png-decode.d.ts
461
+ interface RawImage {
462
+ readonly width: number;
463
+ readonly height: number;
464
+ readonly channels: 1 | 3;
465
+ readonly data: Uint8Array<ArrayBuffer>;
466
+ readonly alpha?: Uint8Array<ArrayBuffer>;
467
+ }
468
+ interface PngDecodeOptions {
469
+ readonly onWarning?: (message: string) => void;
470
+ }
471
+ declare function decodePng(bytes: Uint8Array<ArrayBuffer>, options?: PngDecodeOptions): RawImage;
472
+ //#endregion
473
+ //#region src/image/png-encode.d.ts
474
+ interface PngEncodeOptions {
475
+ readonly filter?: 'none' | 'adaptive';
476
+ }
477
+ declare function encodePng(image: RawImage, options?: PngEncodeOptions): Uint8Array<ArrayBuffer>;
478
+ //#endregion
479
+ //#region src/image/png-filter.d.ts
480
+ type PngFilterType = 0 | 1 | 2 | 3 | 4;
481
+ declare function unfilterScanlines(data: Uint8Array<ArrayBuffer>, height: number, bytesPerRow: number, bpp: number): Uint8Array<ArrayBuffer>;
482
+ declare function filterScanlines(raw: Uint8Array<ArrayBuffer>, height: number, bytesPerRow: number, bpp: number, strategy?: 'none' | 'adaptive'): Uint8Array<ArrayBuffer>;
483
+ //#endregion
484
+ export { ByteReader, ByteWriter, type DeflateLevel, type FontMetrics, type InflateResult, type JpegInfo, type LoadedMathFont, MAX_INFLATE_OUTPUT_BYTES, type MathBox, type MathColor, type MathFont, type MathFontDescriptorMetrics, type MathFontMetrics, type MathGlyphMetrics, type MathGlyphRun, type MathLayoutItem, type MathRule, type MathStroke, NOOP_DIAGNOSTIC_SINK, PdfBytesSchema, type PdfDiagnostic, type PdfDiagnosticSeverity, type PdfDiagnosticSink, PdfEncryptedError, PdfParseError, type PngDecodeOptions, type PngEncodeOptions, type PngFilterType, type Point, type PositionedFormula, type RawImage, type ReadPdfOptions, type ResolvedFont, STANDARD_METRICS, type StandardFontMeasurerOptions, type StandardFontName, type StyledFragment, type StyledRun, type TextMeasurer, type UnderlineMetrics, type WinAnsiSubstitution, type WrapOptions, type WrappedLine, type WritePdfOptions, concatBytes, crc32, createStandardFontMeasurer, decodePng, deflate, encodePng, filterScanlines, inflate, inflateTolerant, isAsciiWhitespace, loadMathFont, pdfCodec, readJpegInfo, readPdf, resolveStandardFont, rotatePointAboutCenter, unfilterScanlines, wrapRunsToWidth, writePdf };