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

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,24 @@ _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 = import("./LiberationSans-Regular-KIF3IRJY.js").then((module) => new Blob([module.default], { type: "font/ttf" })).then(async (sansBlob) => {
5347
+ const fontSans = new FontFace(
5348
+ "Liberation Sans",
5349
+ `url(${URL.createObjectURL(sansBlob)})`
5350
+ );
5351
+ document.fonts.add(await fontSans.load());
5352
+ });
5373
5353
  }
5354
+ return loadingFontPromise;
5374
5355
  }
5375
5356
 
5376
5357
  // src/lib/Path2D.ts
@@ -24546,7 +24527,8 @@ async function canvasToData(canvas) {
24546
24527
  }
24547
24528
  return new Uint8Array(await blob.arrayBuffer());
24548
24529
  }
24549
- return canvas.encode("png");
24530
+ const buffer = await canvas.encode("png");
24531
+ return new Uint8Array(buffer);
24550
24532
  }
24551
24533
  async function toDataUrl(data) {
24552
24534
  if (typeof FileReader !== "undefined") {
@@ -24627,8 +24609,9 @@ PDFPageProxy.prototype.getAnnotations = async function(params) {
24627
24609
  height * scale,
24628
24610
  0,
24629
24611
  0,
24630
- width * scale,
24631
- height * scale
24612
+ // https://github.com/Brooooooklyn/canvas/issues/1011
24613
+ isNodeJS ? viewport.width : width * scale,
24614
+ isNodeJS ? viewport.height : height * scale
24632
24615
  );
24633
24616
  annotation.graphics = await canvasToData(croppedCanvasContext.canvas);
24634
24617
  } catch (e) {
@@ -25728,7 +25711,7 @@ var SvgCanvasContext = class {
25728
25711
  this._groupStack = [];
25729
25712
  }
25730
25713
  };
25731
- async function createContext(width, height) {
25714
+ async function createSvgContext(width, height) {
25732
25715
  const canvas = await createCanvas(width, height);
25733
25716
  return new SvgCanvasContext(
25734
25717
  width,
@@ -25757,17 +25740,6 @@ var MAX_TEXT_DIVS_TO_RENDER2 = 1e5;
25757
25740
  var DEFAULT_FONT_SIZE3 = 30;
25758
25741
  var DEFAULT_FONT_ASCENT = 0.8;
25759
25742
  var HYPHEN_REGEX = /-\n+$/;
25760
- function getDefaultFontFamily(type) {
25761
- switch (type) {
25762
- case "serif":
25763
- return "Liberation Serif";
25764
- case "monospace":
25765
- return "Liberation Mono";
25766
- default:
25767
- return "Liberation Sans";
25768
- }
25769
- }
25770
- var loadingFontsPromise = null;
25771
25743
  async function createTextLayer(page, {
25772
25744
  canvasFactory,
25773
25745
  viewport = page.getViewport({ scale: 1 }),
@@ -25907,7 +25879,7 @@ async function createTextLayer(page, {
25907
25879
  }
25908
25880
  if (shouldScaleText) {
25909
25881
  if (canvasContext) {
25910
- canvasContext.font = `${fontHeight * outputScale}px ${getDefaultFontFamily(fontFamily)}`;
25882
+ canvasContext.font = `400 ${fontHeight * outputScale}px 'Liberation Sans'`;
25911
25883
  const { width } = canvasContext.measureText(geom.str);
25912
25884
  if (width > 0) {
25913
25885
  textDiv.scale = (style.vertical ? geom.height : geom.width) * outputScale / width;
@@ -25920,10 +25892,6 @@ async function createTextLayer(page, {
25920
25892
  const lang = inputLang || "";
25921
25893
  let canvasContext2 = canvasCache.get(lang);
25922
25894
  if (!canvasContext2) {
25923
- if (!loadingFontsPromise) {
25924
- loadingFontsPromise = loadDefaultFonts();
25925
- }
25926
- await loadingFontsPromise;
25927
25895
  const { context } = await canvasFactory.create(100, 100);
25928
25896
  context.canvas.lang = lang;
25929
25897
  canvasContext2 = context;
@@ -25941,7 +25909,7 @@ async function createTextLayer(page, {
25941
25909
  }
25942
25910
  const savedFont = canvasContext.font;
25943
25911
  canvasContext.canvas.width = canvasContext.canvas.height = DEFAULT_FONT_SIZE3;
25944
- canvasContext.font = `${DEFAULT_FONT_SIZE3}px ${getDefaultFontFamily(fontFamily)}`;
25912
+ canvasContext.font = `400 ${DEFAULT_FONT_SIZE3}px 'Liberation Sans'`;
25945
25913
  const metrics = canvasContext.measureText(" ");
25946
25914
  let ascent = metrics.fontBoundingBoxAscent;
25947
25915
  let descent = Math.abs(metrics.fontBoundingBoxDescent);
@@ -26213,6 +26181,7 @@ async function createTextLayer(page, {
26213
26181
  }
26214
26182
  return null;
26215
26183
  };
26184
+ await loadDefaultFonts();
26216
26185
  const reader = textContentSource.getReader();
26217
26186
  while (true) {
26218
26187
  const { value, done } = await reader.read();
@@ -26252,6 +26221,7 @@ export {
26252
26221
  PDFDateString,
26253
26222
  PDFWorker,
26254
26223
  PasswordResponses,
26224
+ Path2DConstructor as Path2D,
26255
26225
  PermissionFlag,
26256
26226
  PixelsPerInch,
26257
26227
  RenderingCancelledException,
@@ -26267,11 +26237,11 @@ export {
26267
26237
  XfaLayer,
26268
26238
  build,
26269
26239
  canvasToData,
26270
- createContext,
26240
+ createCanvas,
26241
+ createSvgContext,
26271
26242
  createTextLayer,
26272
26243
  createValidAbsoluteUrl,
26273
26244
  fetchData,
26274
- getDefaultFontFamily,
26275
26245
  getDocument,
26276
26246
  getFilenameFromUrl,
26277
26247
  getPdfFilenameFromUrl,
@@ -26313,6 +26283,7 @@ export {
26313
26283
  isUnderlineAnnotation,
26314
26284
  isWatermarkAnnotation,
26315
26285
  isWidgetAnnotation,
26286
+ loadDefaultFonts,
26316
26287
  noContextMenu,
26317
26288
  normalizeUnicode,
26318
26289
  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 {};
@@ -37,12 +37,6 @@ export interface TextLayerImage extends TextLayerNode {
37
37
  export declare function isTextNode(node: TextLayerNode): node is TextLayerText;
38
38
  export declare function isAnchorNode(node: TextLayerNode): node is TextLayerAnchor;
39
39
  export declare function isImageNode(node: TextLayerNode): node is TextLayerImage;
40
- /**
41
- * Get the default font family.
42
- * @param type The font type.
43
- * @returns The default font family.
44
- */
45
- export declare function getDefaultFontFamily(type: string): string;
46
40
  export declare function createTextLayer(page: PDFPageProxy, { canvasFactory, viewport, outputScale, annotations, }: {
47
41
  canvasFactory: BaseCanvasFactory;
48
42
  viewport?: PageViewport;
@@ -5332,7 +5332,10 @@ var PrintAnnotationStorage = class extends AnnotationStorage {
5332
5332
  _serializable = new WeakMap();
5333
5333
 
5334
5334
  // src/lib/NodeCanvas.ts
5335
+ import { writeFile } from "node:fs/promises";
5335
5336
  import { createRequire } from "node:module";
5337
+ import { dirname, join } from "node:path";
5338
+ import { fileURLToPath } from "node:url";
5336
5339
  var require2 = createRequire(import.meta.url);
5337
5340
  var {
5338
5341
  createCanvas: create,
@@ -5340,32 +5343,23 @@ var {
5340
5343
  Path2D
5341
5344
  } = require2("@napi-rs/canvas");
5342
5345
  async function createCanvas(width, height) {
5346
+ await loadDefaultFonts();
5343
5347
  return create(width, height);
5344
5348
  }
5349
+ var loadingFontPromise = null;
5345
5350
  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
- );
5351
+ if (loadingFontPromise === null) {
5352
+ loadingFontPromise = import("./LiberationSans-Regular-CDMMZL5S.js").then((module) => module.default).then(async (sansBuffer) => {
5353
+ const currentDir = dirname(fileURLToPath(import.meta.url));
5354
+ const fileName = join(currentDir, "LiberationSans-Regular.ttf");
5355
+ await writeFile(fileName, sansBuffer);
5356
+ GlobalFonts.registerFromPath(
5357
+ fileName,
5358
+ "Liberation Sans"
5359
+ );
5360
+ });
5361
+ }
5362
+ return loadingFontPromise;
5369
5363
  }
5370
5364
 
5371
5365
  // src/lib/Path2D.ts
@@ -24843,7 +24837,8 @@ async function canvasToData(canvas) {
24843
24837
  }
24844
24838
  return new Uint8Array(await blob.arrayBuffer());
24845
24839
  }
24846
- return canvas.encode("png");
24840
+ const buffer = await canvas.encode("png");
24841
+ return new Uint8Array(buffer);
24847
24842
  }
24848
24843
  async function toDataUrl(data) {
24849
24844
  if (typeof FileReader !== "undefined") {
@@ -24924,8 +24919,9 @@ PDFPageProxy.prototype.getAnnotations = async function(params) {
24924
24919
  height * scale,
24925
24920
  0,
24926
24921
  0,
24927
- width * scale,
24928
- height * scale
24922
+ // https://github.com/Brooooooklyn/canvas/issues/1011
24923
+ isNodeJS ? viewport.width : width * scale,
24924
+ isNodeJS ? viewport.height : height * scale
24929
24925
  );
24930
24926
  annotation.graphics = await canvasToData(croppedCanvasContext.canvas);
24931
24927
  } catch (e) {
@@ -26025,7 +26021,7 @@ var SvgCanvasContext = class {
26025
26021
  this._groupStack = [];
26026
26022
  }
26027
26023
  };
26028
- async function createContext(width, height) {
26024
+ async function createSvgContext(width, height) {
26029
26025
  const canvas = await createCanvas(width, height);
26030
26026
  return new SvgCanvasContext(
26031
26027
  width,
@@ -26054,17 +26050,6 @@ var MAX_TEXT_DIVS_TO_RENDER2 = 1e5;
26054
26050
  var DEFAULT_FONT_SIZE3 = 30;
26055
26051
  var DEFAULT_FONT_ASCENT = 0.8;
26056
26052
  var HYPHEN_REGEX = /-\n+$/;
26057
- function getDefaultFontFamily(type) {
26058
- switch (type) {
26059
- case "serif":
26060
- return "Liberation Serif";
26061
- case "monospace":
26062
- return "Liberation Mono";
26063
- default:
26064
- return "Liberation Sans";
26065
- }
26066
- }
26067
- var loadingFontsPromise = null;
26068
26053
  async function createTextLayer(page, {
26069
26054
  canvasFactory,
26070
26055
  viewport = page.getViewport({ scale: 1 }),
@@ -26204,7 +26189,7 @@ async function createTextLayer(page, {
26204
26189
  }
26205
26190
  if (shouldScaleText) {
26206
26191
  if (canvasContext) {
26207
- canvasContext.font = `${fontHeight * outputScale}px ${getDefaultFontFamily(fontFamily)}`;
26192
+ canvasContext.font = `400 ${fontHeight * outputScale}px 'Liberation Sans'`;
26208
26193
  const { width } = canvasContext.measureText(geom.str);
26209
26194
  if (width > 0) {
26210
26195
  textDiv.scale = (style.vertical ? geom.height : geom.width) * outputScale / width;
@@ -26217,10 +26202,6 @@ async function createTextLayer(page, {
26217
26202
  const lang = inputLang || "";
26218
26203
  let canvasContext2 = canvasCache.get(lang);
26219
26204
  if (!canvasContext2) {
26220
- if (!loadingFontsPromise) {
26221
- loadingFontsPromise = loadDefaultFonts();
26222
- }
26223
- await loadingFontsPromise;
26224
26205
  const { context } = await canvasFactory.create(100, 100);
26225
26206
  context.canvas.lang = lang;
26226
26207
  canvasContext2 = context;
@@ -26238,7 +26219,7 @@ async function createTextLayer(page, {
26238
26219
  }
26239
26220
  const savedFont = canvasContext.font;
26240
26221
  canvasContext.canvas.width = canvasContext.canvas.height = DEFAULT_FONT_SIZE3;
26241
- canvasContext.font = `${DEFAULT_FONT_SIZE3}px ${getDefaultFontFamily(fontFamily)}`;
26222
+ canvasContext.font = `400 ${DEFAULT_FONT_SIZE3}px 'Liberation Sans'`;
26242
26223
  const metrics = canvasContext.measureText(" ");
26243
26224
  let ascent = metrics.fontBoundingBoxAscent;
26244
26225
  let descent = Math.abs(metrics.fontBoundingBoxDescent);
@@ -26510,6 +26491,7 @@ async function createTextLayer(page, {
26510
26491
  }
26511
26492
  return null;
26512
26493
  };
26494
+ await loadDefaultFonts();
26513
26495
  const reader = textContentSource.getReader();
26514
26496
  while (true) {
26515
26497
  const { value, done } = await reader.read();
@@ -26549,6 +26531,7 @@ export {
26549
26531
  PDFDateString,
26550
26532
  PDFWorker,
26551
26533
  PasswordResponses,
26534
+ Path2D,
26552
26535
  PermissionFlag,
26553
26536
  PixelsPerInch,
26554
26537
  RenderingCancelledException,
@@ -26564,11 +26547,11 @@ export {
26564
26547
  XfaLayer,
26565
26548
  build,
26566
26549
  canvasToData,
26567
- createContext,
26550
+ createCanvas,
26551
+ createSvgContext,
26568
26552
  createTextLayer,
26569
26553
  createValidAbsoluteUrl,
26570
26554
  fetchData,
26571
- getDefaultFontFamily,
26572
26555
  getDocument,
26573
26556
  getFilenameFromUrl,
26574
26557
  getPdfFilenameFromUrl,
@@ -26610,6 +26593,7 @@ export {
26610
26593
  isUnderlineAnnotation,
26611
26594
  isWatermarkAnnotation,
26612
26595
  isWidgetAnnotation,
26596
+ loadDefaultFonts,
26613
26597
  noContextMenu,
26614
26598
  normalizeUnicode,
26615
26599
  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.2",
5
5
  "type": "module",
6
6
  "author": "Chialab <dev@chialab.it>",
7
7
  "license": "MIT",
@@ -27,8 +27,7 @@
27
27
  }
28
28
  },
29
29
  "dependencies": {
30
- "@napi-rs/canvas": "^0.1.67",
31
- "pdfjs-dist": "^4.10.38"
30
+ "@napi-rs/canvas": "^0.1.67"
32
31
  },
33
32
  "devDependencies": {
34
33
  "@biomejs/biome": "^1.9.4",