@chialab/pdfjs-lib 1.0.0-alpha.0 → 1.0.0-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5334,43 +5334,47 @@ _serializable = new WeakMap();
5334
5334
  // src/lib/Canvas.ts
5335
5335
  var Path2DConstructor = Path2D;
5336
5336
  async function createCanvas(width, height) {
5337
+ await loadDefaultFonts();
5337
5338
  const canvas = document.createElement("canvas");
5338
5339
  canvas.width = width;
5339
5340
  canvas.height = height;
5340
5341
  return canvas;
5341
5342
  }
5343
+ var loadingFontPromise = null;
5342
5344
  async function loadDefaultFonts() {
5343
- const [monoBlob, sansBlob, serifBlob] = await Promise.all([
5344
- import("./LiberationMono-Regular-UUOCTXY2.js").then(
5345
- (module) => new Blob([module.default], { type: "font/ttf" })
5346
- ),
5347
- import("./LiberationSans-Regular-KIF3IRJY.js").then(
5348
- (module) => new Blob([module.default], { type: "font/ttf" })
5349
- ),
5350
- import("./LiberationSerif-Regular-ASQ2LI3D.js").then(
5351
- (module) => new Blob([module.default], { type: "font/ttf" })
5352
- )
5353
- ]);
5354
- const fontMono = new FontFace(
5355
- "Liberation Mono",
5356
- `url(${URL.createObjectURL(monoBlob)})`
5357
- );
5358
- const fontSans = new FontFace(
5359
- "Liberation Sans",
5360
- `url(${URL.createObjectURL(sansBlob)})`
5361
- );
5362
- const fontSerif = new FontFace(
5363
- "Liberation Serif",
5364
- `url(${URL.createObjectURL(serifBlob)})`
5365
- );
5366
- const fonts = await Promise.all([
5367
- fontMono.load(),
5368
- fontSans.load(),
5369
- fontSerif.load()
5370
- ]);
5371
- for (const font of fonts) {
5372
- document.fonts.add(font);
5345
+ if (loadingFontPromise === null) {
5346
+ loadingFontPromise = Promise.all([
5347
+ import("./LiberationMono-Regular-UUOCTXY2.js").then(
5348
+ (module) => new Blob([module.default], { type: "font/ttf" })
5349
+ ),
5350
+ import("./LiberationSans-Regular-KIF3IRJY.js").then((module) => new Blob([module.default], { type: "font/ttf" })),
5351
+ import("./LiberationSerif-Regular-ASQ2LI3D.js").then(
5352
+ (module) => new Blob([module.default], { type: "font/ttf" })
5353
+ )
5354
+ ]).then(async ([monoBlob, sansBlob, serifBlob]) => {
5355
+ const fontMono = new FontFace(
5356
+ "Liberation Mono",
5357
+ `url(${URL.createObjectURL(monoBlob)})`
5358
+ );
5359
+ const fontSans = new FontFace(
5360
+ "Liberation Sans",
5361
+ `url(${URL.createObjectURL(sansBlob)})`
5362
+ );
5363
+ const fontSerif = new FontFace(
5364
+ "Liberation Serif",
5365
+ `url(${URL.createObjectURL(serifBlob)})`
5366
+ );
5367
+ const fonts = await Promise.all([
5368
+ fontMono.load(),
5369
+ fontSans.load(),
5370
+ fontSerif.load()
5371
+ ]);
5372
+ for (const font of fonts) {
5373
+ document.fonts.add(font);
5374
+ }
5375
+ });
5373
5376
  }
5377
+ return loadingFontPromise;
5374
5378
  }
5375
5379
 
5376
5380
  // src/lib/Path2D.ts
@@ -24546,7 +24550,8 @@ async function canvasToData(canvas) {
24546
24550
  }
24547
24551
  return new Uint8Array(await blob.arrayBuffer());
24548
24552
  }
24549
- return canvas.encode("png");
24553
+ const buffer = await canvas.encode("png");
24554
+ return new Uint8Array(buffer);
24550
24555
  }
24551
24556
  async function toDataUrl(data) {
24552
24557
  if (typeof FileReader !== "undefined") {
@@ -25728,7 +25733,7 @@ var SvgCanvasContext = class {
25728
25733
  this._groupStack = [];
25729
25734
  }
25730
25735
  };
25731
- async function createContext(width, height) {
25736
+ async function createSvgContext(width, height) {
25732
25737
  const canvas = await createCanvas(width, height);
25733
25738
  return new SvgCanvasContext(
25734
25739
  width,
@@ -25767,7 +25772,6 @@ function getDefaultFontFamily(type) {
25767
25772
  return "Liberation Sans";
25768
25773
  }
25769
25774
  }
25770
- var loadingFontsPromise = null;
25771
25775
  async function createTextLayer(page, {
25772
25776
  canvasFactory,
25773
25777
  viewport = page.getViewport({ scale: 1 }),
@@ -25920,10 +25924,6 @@ async function createTextLayer(page, {
25920
25924
  const lang = inputLang || "";
25921
25925
  let canvasContext2 = canvasCache.get(lang);
25922
25926
  if (!canvasContext2) {
25923
- if (!loadingFontsPromise) {
25924
- loadingFontsPromise = loadDefaultFonts();
25925
- }
25926
- await loadingFontsPromise;
25927
25927
  const { context } = await canvasFactory.create(100, 100);
25928
25928
  context.canvas.lang = lang;
25929
25929
  canvasContext2 = context;
@@ -26252,6 +26252,7 @@ export {
26252
26252
  PDFDateString,
26253
26253
  PDFWorker,
26254
26254
  PasswordResponses,
26255
+ Path2DConstructor as Path2D,
26255
26256
  PermissionFlag,
26256
26257
  PixelsPerInch,
26257
26258
  RenderingCancelledException,
@@ -26267,7 +26268,8 @@ export {
26267
26268
  XfaLayer,
26268
26269
  build,
26269
26270
  canvasToData,
26270
- createContext,
26271
+ createCanvas,
26272
+ createSvgContext,
26271
26273
  createTextLayer,
26272
26274
  createValidAbsoluteUrl,
26273
26275
  fetchData,
@@ -26313,6 +26315,7 @@ export {
26313
26315
  isUnderlineAnnotation,
26314
26316
  isWatermarkAnnotation,
26315
26317
  isWidgetAnnotation,
26318
+ loadDefaultFonts,
26316
26319
  noContextMenu,
26317
26320
  normalizeUnicode,
26318
26321
  setLayerDimensions,
package/dist/index.d.ts CHANGED
@@ -3,6 +3,7 @@ import './lib/PDFPageProxy';
3
3
  export type { DocumentInitParameters } from './pdf.js/src/display/api';
4
4
  export type { BaseCanvasFactory } from './pdf.js/src/display/canvas_factory';
5
5
  export * from './lib/utils';
6
+ export * from './lib/Canvas';
6
7
  export * from './lib/StandardFontDataFactory';
7
8
  export * from './lib/AnnotationData';
8
9
  export * from './lib/SvgCanvasContext';
@@ -151,6 +151,6 @@ export declare class SvgCanvasContext {
151
151
  protected _applyText(text: string, x: number, y: number, action: 'fill'): void;
152
152
  protected _clearCanvas(): void;
153
153
  }
154
- export declare function createContext(width: number, height: number): Promise<CanvasRenderingContext2D>;
154
+ export declare function createSvgContext(width: number, height: number): Promise<CanvasRenderingContext2D>;
155
155
  export declare function toSvgNode(ctx: CanvasRenderingContext2D): Promise<SvgRoot>;
156
156
  export {};
@@ -5340,32 +5340,36 @@ var {
5340
5340
  Path2D
5341
5341
  } = require2("@napi-rs/canvas");
5342
5342
  async function createCanvas(width, height) {
5343
+ await loadDefaultFonts();
5343
5344
  return create(width, height);
5344
5345
  }
5346
+ var loadingFontPromise = null;
5345
5347
  async function loadDefaultFonts() {
5346
- const [monoBuffer, sansBuffer, serifBuffer] = await Promise.all([
5347
- import("./LiberationMono-Regular-KMFXXO3B.js").then(
5348
- (module) => module.default
5349
- ),
5350
- import("./LiberationSans-Regular-CDMMZL5S.js").then(
5351
- (module) => module.default
5352
- ),
5353
- import("./LiberationSerif-Regular-WAOWR76G.js").then(
5354
- (module) => module.default
5355
- )
5356
- ]);
5357
- GlobalFonts.register(
5358
- Buffer.from(monoBuffer),
5359
- "Liberation Mono"
5360
- );
5361
- GlobalFonts.register(
5362
- Buffer.from(sansBuffer),
5363
- "Liberation Sans"
5364
- );
5365
- GlobalFonts.register(
5366
- Buffer.from(serifBuffer),
5367
- "Liberation Serif"
5368
- );
5348
+ if (loadingFontPromise === null) {
5349
+ loadingFontPromise = Promise.all([
5350
+ import("./LiberationMono-Regular-KMFXXO3B.js").then(
5351
+ (module) => module.default
5352
+ ),
5353
+ import("./LiberationSans-Regular-CDMMZL5S.js").then((module) => module.default),
5354
+ import("./LiberationSerif-Regular-WAOWR76G.js").then(
5355
+ (module) => module.default
5356
+ )
5357
+ ]).then(([monoBuffer, sansBuffer, serifBuffer]) => {
5358
+ GlobalFonts.register(
5359
+ Buffer.from(monoBuffer),
5360
+ "Liberation Mono"
5361
+ );
5362
+ GlobalFonts.register(
5363
+ Buffer.from(sansBuffer),
5364
+ "Liberation Sans"
5365
+ );
5366
+ GlobalFonts.register(
5367
+ Buffer.from(serifBuffer),
5368
+ "Liberation Serif"
5369
+ );
5370
+ });
5371
+ }
5372
+ return loadingFontPromise;
5369
5373
  }
5370
5374
 
5371
5375
  // src/lib/Path2D.ts
@@ -24843,7 +24847,8 @@ async function canvasToData(canvas) {
24843
24847
  }
24844
24848
  return new Uint8Array(await blob.arrayBuffer());
24845
24849
  }
24846
- return canvas.encode("png");
24850
+ const buffer = await canvas.encode("png");
24851
+ return new Uint8Array(buffer);
24847
24852
  }
24848
24853
  async function toDataUrl(data) {
24849
24854
  if (typeof FileReader !== "undefined") {
@@ -26025,7 +26030,7 @@ var SvgCanvasContext = class {
26025
26030
  this._groupStack = [];
26026
26031
  }
26027
26032
  };
26028
- async function createContext(width, height) {
26033
+ async function createSvgContext(width, height) {
26029
26034
  const canvas = await createCanvas(width, height);
26030
26035
  return new SvgCanvasContext(
26031
26036
  width,
@@ -26064,7 +26069,6 @@ function getDefaultFontFamily(type) {
26064
26069
  return "Liberation Sans";
26065
26070
  }
26066
26071
  }
26067
- var loadingFontsPromise = null;
26068
26072
  async function createTextLayer(page, {
26069
26073
  canvasFactory,
26070
26074
  viewport = page.getViewport({ scale: 1 }),
@@ -26217,10 +26221,6 @@ async function createTextLayer(page, {
26217
26221
  const lang = inputLang || "";
26218
26222
  let canvasContext2 = canvasCache.get(lang);
26219
26223
  if (!canvasContext2) {
26220
- if (!loadingFontsPromise) {
26221
- loadingFontsPromise = loadDefaultFonts();
26222
- }
26223
- await loadingFontsPromise;
26224
26224
  const { context } = await canvasFactory.create(100, 100);
26225
26225
  context.canvas.lang = lang;
26226
26226
  canvasContext2 = context;
@@ -26549,6 +26549,7 @@ export {
26549
26549
  PDFDateString,
26550
26550
  PDFWorker,
26551
26551
  PasswordResponses,
26552
+ Path2D,
26552
26553
  PermissionFlag,
26553
26554
  PixelsPerInch,
26554
26555
  RenderingCancelledException,
@@ -26564,7 +26565,8 @@ export {
26564
26565
  XfaLayer,
26565
26566
  build,
26566
26567
  canvasToData,
26567
- createContext,
26568
+ createCanvas,
26569
+ createSvgContext,
26568
26570
  createTextLayer,
26569
26571
  createValidAbsoluteUrl,
26570
26572
  fetchData,
@@ -26610,6 +26612,7 @@ export {
26610
26612
  isUnderlineAnnotation,
26611
26613
  isWatermarkAnnotation,
26612
26614
  isWidgetAnnotation,
26615
+ loadDefaultFonts,
26613
26616
  noContextMenu,
26614
26617
  normalizeUnicode,
26615
26618
  setLayerDimensions,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@chialab/pdfjs-lib",
3
3
  "description": "A custom Mozilla's PDF.js build with better Node support and extras.",
4
- "version": "1.0.0-alpha.0",
4
+ "version": "1.0.0-alpha.1",
5
5
  "type": "module",
6
6
  "author": "Chialab <dev@chialab.it>",
7
7
  "license": "MIT",