@chialab/pdfjs-lib 1.0.0-alpha.1 → 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.
@@ -5343,35 +5343,12 @@ async function createCanvas(width, height) {
5343
5343
  var loadingFontPromise = null;
5344
5344
  async function loadDefaultFonts() {
5345
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
- );
5346
+ loadingFontPromise = import("./LiberationSans-Regular-KIF3IRJY.js").then((module) => new Blob([module.default], { type: "font/ttf" })).then(async (sansBlob) => {
5359
5347
  const fontSans = new FontFace(
5360
5348
  "Liberation Sans",
5361
5349
  `url(${URL.createObjectURL(sansBlob)})`
5362
5350
  );
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
- }
5351
+ document.fonts.add(await fontSans.load());
5375
5352
  });
5376
5353
  }
5377
5354
  return loadingFontPromise;
@@ -24632,8 +24609,9 @@ PDFPageProxy.prototype.getAnnotations = async function(params) {
24632
24609
  height * scale,
24633
24610
  0,
24634
24611
  0,
24635
- width * scale,
24636
- height * scale
24612
+ // https://github.com/Brooooooklyn/canvas/issues/1011
24613
+ isNodeJS ? viewport.width : width * scale,
24614
+ isNodeJS ? viewport.height : height * scale
24637
24615
  );
24638
24616
  annotation.graphics = await canvasToData(croppedCanvasContext.canvas);
24639
24617
  } catch (e) {
@@ -25762,16 +25740,6 @@ var MAX_TEXT_DIVS_TO_RENDER2 = 1e5;
25762
25740
  var DEFAULT_FONT_SIZE3 = 30;
25763
25741
  var DEFAULT_FONT_ASCENT = 0.8;
25764
25742
  var HYPHEN_REGEX = /-\n+$/;
25765
- function getDefaultFontFamily(type) {
25766
- switch (type) {
25767
- case "serif":
25768
- return "Liberation Serif";
25769
- case "monospace":
25770
- return "Liberation Mono";
25771
- default:
25772
- return "Liberation Sans";
25773
- }
25774
- }
25775
25743
  async function createTextLayer(page, {
25776
25744
  canvasFactory,
25777
25745
  viewport = page.getViewport({ scale: 1 }),
@@ -25911,7 +25879,7 @@ async function createTextLayer(page, {
25911
25879
  }
25912
25880
  if (shouldScaleText) {
25913
25881
  if (canvasContext) {
25914
- canvasContext.font = `${fontHeight * outputScale}px ${getDefaultFontFamily(fontFamily)}`;
25882
+ canvasContext.font = `400 ${fontHeight * outputScale}px 'Liberation Sans'`;
25915
25883
  const { width } = canvasContext.measureText(geom.str);
25916
25884
  if (width > 0) {
25917
25885
  textDiv.scale = (style.vertical ? geom.height : geom.width) * outputScale / width;
@@ -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();
@@ -26273,7 +26242,6 @@ export {
26273
26242
  createTextLayer,
26274
26243
  createValidAbsoluteUrl,
26275
26244
  fetchData,
26276
- getDefaultFontFamily,
26277
26245
  getDocument,
26278
26246
  getFilenameFromUrl,
26279
26247
  getPdfFilenameFromUrl,
@@ -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,
@@ -5346,27 +5349,14 @@ async function createCanvas(width, height) {
5346
5349
  var loadingFontPromise = null;
5347
5350
  async function loadDefaultFonts() {
5348
5351
  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),
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,
5364
5358
  "Liberation Sans"
5365
5359
  );
5366
- GlobalFonts.register(
5367
- Buffer.from(serifBuffer),
5368
- "Liberation Serif"
5369
- );
5370
5360
  });
5371
5361
  }
5372
5362
  return loadingFontPromise;
@@ -24929,8 +24919,9 @@ PDFPageProxy.prototype.getAnnotations = async function(params) {
24929
24919
  height * scale,
24930
24920
  0,
24931
24921
  0,
24932
- width * scale,
24933
- height * scale
24922
+ // https://github.com/Brooooooklyn/canvas/issues/1011
24923
+ isNodeJS ? viewport.width : width * scale,
24924
+ isNodeJS ? viewport.height : height * scale
24934
24925
  );
24935
24926
  annotation.graphics = await canvasToData(croppedCanvasContext.canvas);
24936
24927
  } catch (e) {
@@ -26059,16 +26050,6 @@ var MAX_TEXT_DIVS_TO_RENDER2 = 1e5;
26059
26050
  var DEFAULT_FONT_SIZE3 = 30;
26060
26051
  var DEFAULT_FONT_ASCENT = 0.8;
26061
26052
  var HYPHEN_REGEX = /-\n+$/;
26062
- function getDefaultFontFamily(type) {
26063
- switch (type) {
26064
- case "serif":
26065
- return "Liberation Serif";
26066
- case "monospace":
26067
- return "Liberation Mono";
26068
- default:
26069
- return "Liberation Sans";
26070
- }
26071
- }
26072
26053
  async function createTextLayer(page, {
26073
26054
  canvasFactory,
26074
26055
  viewport = page.getViewport({ scale: 1 }),
@@ -26208,7 +26189,7 @@ async function createTextLayer(page, {
26208
26189
  }
26209
26190
  if (shouldScaleText) {
26210
26191
  if (canvasContext) {
26211
- canvasContext.font = `${fontHeight * outputScale}px ${getDefaultFontFamily(fontFamily)}`;
26192
+ canvasContext.font = `400 ${fontHeight * outputScale}px 'Liberation Sans'`;
26212
26193
  const { width } = canvasContext.measureText(geom.str);
26213
26194
  if (width > 0) {
26214
26195
  textDiv.scale = (style.vertical ? geom.height : geom.width) * outputScale / width;
@@ -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();
@@ -26570,7 +26552,6 @@ export {
26570
26552
  createTextLayer,
26571
26553
  createValidAbsoluteUrl,
26572
26554
  fetchData,
26573
- getDefaultFontFamily,
26574
26555
  getDocument,
26575
26556
  getFilenameFromUrl,
26576
26557
  getPdfFilenameFromUrl,
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.1",
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",