@chialab/pdfjs-lib 1.0.0-alpha.1 → 1.0.0-alpha.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/browser/index.js +10 -45
- package/dist/lib/TextLayer.d.ts +0 -6
- package/dist/node/index.js +17 -39
- package/package.json +2 -3
- package/dist/browser/LiberationMono-Regular-UUOCTXY2.js +0 -9
- package/dist/browser/LiberationSerif-Regular-ASQ2LI3D.js +0 -9
- package/dist/node/LiberationMono-Regular-KMFXXO3B.js +0 -9
- package/dist/node/LiberationSerif-Regular-WAOWR76G.js +0 -9
package/dist/browser/index.js
CHANGED
|
@@ -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 =
|
|
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
|
-
|
|
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
|
-
|
|
24636
|
-
|
|
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) {
|
|
@@ -24657,7 +24635,7 @@ PDFPageProxy.prototype.getAnnotations = async function(params) {
|
|
|
24657
24635
|
firstFill = false;
|
|
24658
24636
|
continue;
|
|
24659
24637
|
}
|
|
24660
|
-
if (fn === OPS.stroke) {
|
|
24638
|
+
if (fn === OPS.stroke || fn === OPS.closeStroke) {
|
|
24661
24639
|
if (currentLineWidth === null) {
|
|
24662
24640
|
annotation.borderStyle.width = 1;
|
|
24663
24641
|
}
|
|
@@ -24665,7 +24643,7 @@ PDFPageProxy.prototype.getAnnotations = async function(params) {
|
|
|
24665
24643
|
currentLineWidth = null;
|
|
24666
24644
|
continue;
|
|
24667
24645
|
}
|
|
24668
|
-
if (fn === OPS.fillStroke) {
|
|
24646
|
+
if (fn === OPS.fillStroke || fn === OPS.closeFillStroke) {
|
|
24669
24647
|
if (currentFillColor) {
|
|
24670
24648
|
annotation.interiorColor = currentFillColor;
|
|
24671
24649
|
currentFillColor = null;
|
|
@@ -24685,9 +24663,6 @@ PDFPageProxy.prototype.getAnnotations = async function(params) {
|
|
|
24685
24663
|
annotation.borderStyle.width = currentLineWidth;
|
|
24686
24664
|
}
|
|
24687
24665
|
if (fn === OPS.setStrokeRGBColor && firstStroke) {
|
|
24688
|
-
if (!annotation.borderStyle.width) {
|
|
24689
|
-
annotation.borderStyle.width = 1;
|
|
24690
|
-
}
|
|
24691
24666
|
annotation.color = args;
|
|
24692
24667
|
}
|
|
24693
24668
|
if (fn === OPS.setFillRGBColor && firstFill) {
|
|
@@ -25762,16 +25737,6 @@ var MAX_TEXT_DIVS_TO_RENDER2 = 1e5;
|
|
|
25762
25737
|
var DEFAULT_FONT_SIZE3 = 30;
|
|
25763
25738
|
var DEFAULT_FONT_ASCENT = 0.8;
|
|
25764
25739
|
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
25740
|
async function createTextLayer(page, {
|
|
25776
25741
|
canvasFactory,
|
|
25777
25742
|
viewport = page.getViewport({ scale: 1 }),
|
|
@@ -25911,7 +25876,7 @@ async function createTextLayer(page, {
|
|
|
25911
25876
|
}
|
|
25912
25877
|
if (shouldScaleText) {
|
|
25913
25878
|
if (canvasContext) {
|
|
25914
|
-
canvasContext.font =
|
|
25879
|
+
canvasContext.font = `400 ${fontHeight * outputScale}px 'Liberation Sans'`;
|
|
25915
25880
|
const { width } = canvasContext.measureText(geom.str);
|
|
25916
25881
|
if (width > 0) {
|
|
25917
25882
|
textDiv.scale = (style.vertical ? geom.height : geom.width) * outputScale / width;
|
|
@@ -25941,7 +25906,7 @@ async function createTextLayer(page, {
|
|
|
25941
25906
|
}
|
|
25942
25907
|
const savedFont = canvasContext.font;
|
|
25943
25908
|
canvasContext.canvas.width = canvasContext.canvas.height = DEFAULT_FONT_SIZE3;
|
|
25944
|
-
canvasContext.font =
|
|
25909
|
+
canvasContext.font = `400 ${DEFAULT_FONT_SIZE3}px 'Liberation Sans'`;
|
|
25945
25910
|
const metrics = canvasContext.measureText(" ");
|
|
25946
25911
|
let ascent = metrics.fontBoundingBoxAscent;
|
|
25947
25912
|
let descent = Math.abs(metrics.fontBoundingBoxDescent);
|
|
@@ -26213,6 +26178,7 @@ async function createTextLayer(page, {
|
|
|
26213
26178
|
}
|
|
26214
26179
|
return null;
|
|
26215
26180
|
};
|
|
26181
|
+
await loadDefaultFonts();
|
|
26216
26182
|
const reader = textContentSource.getReader();
|
|
26217
26183
|
while (true) {
|
|
26218
26184
|
const { value, done } = await reader.read();
|
|
@@ -26273,7 +26239,6 @@ export {
|
|
|
26273
26239
|
createTextLayer,
|
|
26274
26240
|
createValidAbsoluteUrl,
|
|
26275
26241
|
fetchData,
|
|
26276
|
-
getDefaultFontFamily,
|
|
26277
26242
|
getDocument,
|
|
26278
26243
|
getFilenameFromUrl,
|
|
26279
26244
|
getPdfFilenameFromUrl,
|
package/dist/lib/TextLayer.d.ts
CHANGED
|
@@ -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;
|
package/dist/node/index.js
CHANGED
|
@@ -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 =
|
|
5350
|
-
import
|
|
5351
|
-
|
|
5352
|
-
)
|
|
5353
|
-
|
|
5354
|
-
|
|
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
|
-
|
|
24933
|
-
|
|
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) {
|
|
@@ -24954,7 +24945,7 @@ PDFPageProxy.prototype.getAnnotations = async function(params) {
|
|
|
24954
24945
|
firstFill = false;
|
|
24955
24946
|
continue;
|
|
24956
24947
|
}
|
|
24957
|
-
if (fn === OPS.stroke) {
|
|
24948
|
+
if (fn === OPS.stroke || fn === OPS.closeStroke) {
|
|
24958
24949
|
if (currentLineWidth === null) {
|
|
24959
24950
|
annotation.borderStyle.width = 1;
|
|
24960
24951
|
}
|
|
@@ -24962,7 +24953,7 @@ PDFPageProxy.prototype.getAnnotations = async function(params) {
|
|
|
24962
24953
|
currentLineWidth = null;
|
|
24963
24954
|
continue;
|
|
24964
24955
|
}
|
|
24965
|
-
if (fn === OPS.fillStroke) {
|
|
24956
|
+
if (fn === OPS.fillStroke || fn === OPS.closeFillStroke) {
|
|
24966
24957
|
if (currentFillColor) {
|
|
24967
24958
|
annotation.interiorColor = currentFillColor;
|
|
24968
24959
|
currentFillColor = null;
|
|
@@ -24982,9 +24973,6 @@ PDFPageProxy.prototype.getAnnotations = async function(params) {
|
|
|
24982
24973
|
annotation.borderStyle.width = currentLineWidth;
|
|
24983
24974
|
}
|
|
24984
24975
|
if (fn === OPS.setStrokeRGBColor && firstStroke) {
|
|
24985
|
-
if (!annotation.borderStyle.width) {
|
|
24986
|
-
annotation.borderStyle.width = 1;
|
|
24987
|
-
}
|
|
24988
24976
|
annotation.color = args;
|
|
24989
24977
|
}
|
|
24990
24978
|
if (fn === OPS.setFillRGBColor && firstFill) {
|
|
@@ -26059,16 +26047,6 @@ var MAX_TEXT_DIVS_TO_RENDER2 = 1e5;
|
|
|
26059
26047
|
var DEFAULT_FONT_SIZE3 = 30;
|
|
26060
26048
|
var DEFAULT_FONT_ASCENT = 0.8;
|
|
26061
26049
|
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
26050
|
async function createTextLayer(page, {
|
|
26073
26051
|
canvasFactory,
|
|
26074
26052
|
viewport = page.getViewport({ scale: 1 }),
|
|
@@ -26208,7 +26186,7 @@ async function createTextLayer(page, {
|
|
|
26208
26186
|
}
|
|
26209
26187
|
if (shouldScaleText) {
|
|
26210
26188
|
if (canvasContext) {
|
|
26211
|
-
canvasContext.font =
|
|
26189
|
+
canvasContext.font = `400 ${fontHeight * outputScale}px 'Liberation Sans'`;
|
|
26212
26190
|
const { width } = canvasContext.measureText(geom.str);
|
|
26213
26191
|
if (width > 0) {
|
|
26214
26192
|
textDiv.scale = (style.vertical ? geom.height : geom.width) * outputScale / width;
|
|
@@ -26238,7 +26216,7 @@ async function createTextLayer(page, {
|
|
|
26238
26216
|
}
|
|
26239
26217
|
const savedFont = canvasContext.font;
|
|
26240
26218
|
canvasContext.canvas.width = canvasContext.canvas.height = DEFAULT_FONT_SIZE3;
|
|
26241
|
-
canvasContext.font =
|
|
26219
|
+
canvasContext.font = `400 ${DEFAULT_FONT_SIZE3}px 'Liberation Sans'`;
|
|
26242
26220
|
const metrics = canvasContext.measureText(" ");
|
|
26243
26221
|
let ascent = metrics.fontBoundingBoxAscent;
|
|
26244
26222
|
let descent = Math.abs(metrics.fontBoundingBoxDescent);
|
|
@@ -26510,6 +26488,7 @@ async function createTextLayer(page, {
|
|
|
26510
26488
|
}
|
|
26511
26489
|
return null;
|
|
26512
26490
|
};
|
|
26491
|
+
await loadDefaultFonts();
|
|
26513
26492
|
const reader = textContentSource.getReader();
|
|
26514
26493
|
while (true) {
|
|
26515
26494
|
const { value, done } = await reader.read();
|
|
@@ -26570,7 +26549,6 @@ export {
|
|
|
26570
26549
|
createTextLayer,
|
|
26571
26550
|
createValidAbsoluteUrl,
|
|
26572
26551
|
fetchData,
|
|
26573
|
-
getDefaultFontFamily,
|
|
26574
26552
|
getDocument,
|
|
26575
26553
|
getFilenameFromUrl,
|
|
26576
26554
|
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.
|
|
4
|
+
"version": "1.0.0-alpha.3",
|
|
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",
|