@chialab/pdfjs-lib 1.0.0-alpha.36 → 1.0.0-alpha.38

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.
@@ -1,6 +1,7 @@
1
1
  import {
2
2
  BaseException,
3
3
  FeatureTest,
4
+ OPS,
4
5
  Util,
5
6
  shadow,
6
7
  stringToBytes,
@@ -1688,6 +1689,9 @@ function randomUUID() {
1688
1689
  hex[8] = hex[13] = hex[18] = hex[23] = "-";
1689
1690
  return hex.join("");
1690
1691
  }
1692
+ var OPS_DICT = Object.fromEntries(
1693
+ Object.entries(OPS).map(([key, value]) => [value, key])
1694
+ );
1691
1695
 
1692
1696
  // src/lib/NodeFilterFactory.ts
1693
1697
  var filtersRegistry = /* @__PURE__ */ new Map();
@@ -2075,6 +2079,7 @@ export {
2075
2079
  rgbToHex,
2076
2080
  parseRgbaColor,
2077
2081
  randomUUID,
2082
+ OPS_DICT,
2078
2083
  filtersRegistry,
2079
2084
  NodeFilterFactory,
2080
2085
  NodeCanvasFactory,
@@ -3,9 +3,10 @@ import {
3
3
  FontInfo,
4
4
  MessageHandler,
5
5
  MurmurHash3_64,
6
+ bidi,
6
7
  convertBlackAndWhiteToRGBA,
7
8
  wrapReason
8
- } from "./chunk-4Y7OZIIX.js";
9
+ } from "./chunk-FGBCMLPZ.js";
9
10
  import {
10
11
  BaseStandardFontDataFactory,
11
12
  CSSConstants,
@@ -20,6 +21,7 @@ import {
20
21
  NodeFilterFactory,
21
22
  NodeStandardFontDataFactory,
22
23
  NodeWasmFactory,
24
+ OPS_DICT,
23
25
  OutputScale,
24
26
  PDFDateString,
25
27
  PageViewport,
@@ -54,7 +56,7 @@ import {
54
56
  setLayerDimensions,
55
57
  stopEvent,
56
58
  toDataUrl
57
- } from "./chunk-FUWEGVHM.js";
59
+ } from "./chunk-XAT2FF6L.js";
58
60
  import {
59
61
  AbortException,
60
62
  AnnotationBorderStyleType,
@@ -11488,14 +11490,14 @@ var require_opentype = __commonJS({
11488
11490
  };
11489
11491
  Font.prototype.stringToGlyphs = function(s, options) {
11490
11492
  var this$1 = this;
11491
- var bidi = new Bidi();
11493
+ var bidi2 = new Bidi();
11492
11494
  var charToGlyphIndexMod = function(token) {
11493
11495
  return this$1.charToGlyphIndex(token.char);
11494
11496
  };
11495
- bidi.registerModifier("glyphIndex", null, charToGlyphIndexMod);
11497
+ bidi2.registerModifier("glyphIndex", null, charToGlyphIndexMod);
11496
11498
  var features = options ? this.updateFeatures(options.features) : this.defaultRenderOptions.features;
11497
- bidi.applyFeatures(this, features);
11498
- var indexes = bidi.getTextGlyphs(s);
11499
+ bidi2.applyFeatures(this, features);
11500
+ var indexes = bidi2.getTextGlyphs(s);
11499
11501
  var length = indexes.length;
11500
11502
  var glyphs = new Array(length);
11501
11503
  var notdef = this.glyphs.get(0);
@@ -39834,7 +39836,7 @@ function destroySvgContext(ctx) {
39834
39836
 
39835
39837
  // src/lib/PDFPageProxy.ts
39836
39838
  async function loadNodeCanvasFactory() {
39837
- const { NodeCanvasFactory: NodeCanvasFactory2 } = await import("./NodeUtils-WMSRRHQR.js");
39839
+ const { NodeCanvasFactory: NodeCanvasFactory2 } = await import("./NodeUtils-U752HDWU.js");
39838
39840
  return new NodeCanvasFactory2({});
39839
39841
  }
39840
39842
  var getAnnotations = PDFPageProxy.prototype.getAnnotations;
@@ -40960,21 +40962,7 @@ var loadTextLayerFontsMap = /* @__PURE__ */ (() => {
40960
40962
  return promise;
40961
40963
  };
40962
40964
  })();
40963
- var getTextDirection = (text) => {
40964
- const rtlChars = /[\u0591-\u07FF\uFB1D-\uFDFD\uFE70-\uFEFC]/;
40965
- const ltrChars = /[A-Za-z]/;
40966
- let rtlCount = 0;
40967
- let ltrCount = 0;
40968
- for (const char of text) {
40969
- if (rtlChars.test(char)) {
40970
- rtlCount++;
40971
- }
40972
- if (ltrChars.test(char)) {
40973
- ltrCount++;
40974
- }
40975
- }
40976
- return rtlCount > ltrCount ? "rtl" : "ltr";
40977
- };
40965
+ var HYPHEN_REGEX2 = /-\n+$/;
40978
40966
  var isNameObject = (obj) => {
40979
40967
  return obj !== null && typeof obj === "object" && "name" in obj;
40980
40968
  };
@@ -41001,6 +40989,7 @@ async function createTextLayerV2(page, {
41001
40989
  const markedContentStack = [rootContainer];
41002
40990
  let markedContent = rootContainer;
41003
40991
  let currentTextItem = null;
40992
+ let lastTextItem = null;
41004
40993
  let transformMatrix = [1, 0, 0, 1, 0, 0];
41005
40994
  let textMatrix = [1, 0, 0, 1, 0, 0];
41006
40995
  let lineMatrix = [1, 0, 0, 1, 0, 0];
@@ -41012,20 +41001,8 @@ async function createTextLayerV2(page, {
41012
41001
  let leading = 0;
41013
41002
  let textRise = 0;
41014
41003
  let textPosition = 0;
41015
- const createTextItem = (fontFamily2) => ({
41016
- glyphs: [],
41017
- font: page.commonObjs.get(fontFamily2)
41018
- });
41019
- const resetTextItem = () => {
41020
- currentTextItem = null;
41021
- textPosition = 0;
41022
- };
41023
- const closeTextItem = () => {
41024
- if (!currentTextItem?.glyphs.length) {
41025
- resetTextItem();
41026
- return;
41027
- }
41028
- const { font, glyphs: glyphsList } = currentTextItem;
41004
+ const createTextItem = (fontFamily2) => {
41005
+ const font = page.commonObjs.get(fontFamily2);
41029
41006
  const textFont = fonts[font.bold || font.black ? font.italic ? 3 : 2 : font.italic ? 1 : 0];
41030
41007
  const riseMatrix = [1, 0, 0, 1, 0, textRise];
41031
41008
  const finalMatrix = Util.transform(
@@ -41034,10 +41011,40 @@ async function createTextLayerV2(page, {
41034
41011
  );
41035
41012
  const [, , , , x, y] = Util.transform(transform, finalMatrix);
41036
41013
  const finalFontSize = fontSize * Math.sqrt(finalMatrix[2] ** 2 + finalMatrix[3] ** 2);
41037
- const angle = -Math.atan2(finalMatrix[1], finalMatrix[0]);
41038
41014
  const shift = -finalFontSize - finalFontSize * textFont.descender / textFont.unitsPerEm;
41015
+ const angle = -Math.atan2(finalMatrix[1], finalMatrix[0]);
41039
41016
  const top = y + shift * Math.cos(angle);
41040
41017
  const left = x - shift * Math.sin(angle);
41018
+ return {
41019
+ glyphs: [],
41020
+ font,
41021
+ fontSize: finalFontSize,
41022
+ top,
41023
+ left,
41024
+ angle
41025
+ };
41026
+ };
41027
+ const resetTextItem = () => {
41028
+ currentTextItem = null;
41029
+ textPosition = 0;
41030
+ };
41031
+ const closeTextItem = () => {
41032
+ if (currentTextItem) {
41033
+ lastTextItem = currentTextItem;
41034
+ }
41035
+ if (!currentTextItem?.glyphs.length) {
41036
+ resetTextItem();
41037
+ return;
41038
+ }
41039
+ const {
41040
+ font,
41041
+ glyphs: glyphsList,
41042
+ fontSize: finalFontSize,
41043
+ top,
41044
+ left,
41045
+ angle
41046
+ } = currentTextItem;
41047
+ const textFont = fonts[font.bold || font.black ? font.italic ? 3 : 2 : font.italic ? 1 : 0];
41041
41048
  const glyphsBlocks = glyphsList.reduce(
41042
41049
  (acc, glyph) => {
41043
41050
  if (!glyph[0]) {
@@ -41095,30 +41102,60 @@ async function createTextLayerV2(page, {
41095
41102
  }
41096
41103
  }
41097
41104
  chunks.push({
41098
- text,
41105
+ text: bidi(text).str,
41099
41106
  width: textWidth,
41100
41107
  margin: margin * finalFontSize,
41101
41108
  scale: graphicWidth / textWidth
41102
41109
  });
41103
41110
  currentLeft += margin + textWidth / fontSize;
41104
41111
  }
41112
+ const dir = bidi(computedText).dir;
41105
41113
  markedContent.children.push({
41106
41114
  role: "text",
41107
- text: chunks,
41115
+ text: dir === "ltr" ? chunks : chunks.reverse(),
41108
41116
  fontFamily: font.loadedName,
41109
41117
  fontFallback: "sans-serif",
41110
- hyphen: false,
41118
+ hyphen: HYPHEN_REGEX2.test(computedText),
41111
41119
  top,
41112
41120
  left,
41113
41121
  fontSize: finalFontSize,
41114
41122
  fontWeight: font?.black ? 900 : font?.bold ? 700 : 400,
41115
41123
  fontStyle: font?.italic ? "italic" : "normal",
41116
- dir: getTextDirection(computedText),
41124
+ dir,
41117
41125
  scale: 1,
41118
41126
  angle: angle * (180 / Math.PI)
41119
41127
  });
41120
41128
  resetTextItem();
41121
41129
  };
41130
+ const createParagraphIfNeeded = (oldTextItem, newTextItem) => {
41131
+ if (structTree) {
41132
+ return;
41133
+ }
41134
+ if (oldTextItem && newTextItem && oldTextItem.fontSize === newTextItem.fontSize) {
41135
+ return;
41136
+ }
41137
+ if (oldTextItem) {
41138
+ closeMarkedContent();
41139
+ }
41140
+ const currentMarkedContent = markedContent || rootContainer;
41141
+ markedContent = {
41142
+ role: "p",
41143
+ children: []
41144
+ };
41145
+ currentMarkedContent.children.push(markedContent);
41146
+ markedContentStack.push(markedContent);
41147
+ };
41148
+ const closeMarkedContent = () => {
41149
+ const oldMarkedContent = markedContentStack.pop();
41150
+ markedContent = markedContentStack.at(-1) || rootContainer;
41151
+ if (oldMarkedContent) {
41152
+ if (["artifact", "figure", "formula"].includes(oldMarkedContent.role)) {
41153
+ markedContent.children = markedContent.children.filter(
41154
+ (c) => c !== oldMarkedContent
41155
+ );
41156
+ }
41157
+ }
41158
+ };
41122
41159
  for (let i = 0; i < operatorsList.fnArray.length; i++) {
41123
41160
  const fnId = operatorsList.fnArray[i];
41124
41161
  const args = operatorsList.argsArray[i];
@@ -41138,6 +41175,7 @@ async function createTextLayerV2(page, {
41138
41175
  case OPS.showText:
41139
41176
  case OPS.showSpacedText: {
41140
41177
  currentTextItem ?? (currentTextItem = createTextItem(fontFamily));
41178
+ createParagraphIfNeeded(lastTextItem, currentTextItem);
41141
41179
  const [chars] = args;
41142
41180
  for (const char of chars) {
41143
41181
  if (typeof char === "number") {
@@ -41192,6 +41230,7 @@ async function createTextLayerV2(page, {
41192
41230
  case OPS.nextLineShowText: {
41193
41231
  closeTextItem();
41194
41232
  currentTextItem = createTextItem(fontFamily);
41233
+ createParagraphIfNeeded(lastTextItem, currentTextItem);
41195
41234
  textMatrix[4] = lineMatrix[4];
41196
41235
  textMatrix[5] = textMatrix[1] * 0 + textMatrix[3] * -leading + textMatrix[5];
41197
41236
  lineMatrix = [...textMatrix];
@@ -41221,6 +41260,7 @@ async function createTextLayerV2(page, {
41221
41260
  case OPS.nextLineSetSpacingShowText: {
41222
41261
  closeTextItem();
41223
41262
  currentTextItem = createTextItem(fontFamily);
41263
+ createParagraphIfNeeded(lastTextItem, currentTextItem);
41224
41264
  const [aw, ac, text] = args;
41225
41265
  wordSpacing = aw;
41226
41266
  charSpacing = ac;
@@ -41272,24 +41312,19 @@ async function createTextLayerV2(page, {
41272
41312
  normalizedRole = "figure";
41273
41313
  break;
41274
41314
  }
41275
- markedContent.children.push({
41315
+ const currentMarkedContent = markedContent || rootContainer;
41316
+ markedContent = {
41276
41317
  id: id2,
41277
41318
  role: normalizedRole || "span",
41278
41319
  attrs: props ?? void 0,
41279
41320
  children: []
41280
- });
41281
- markedContent = markedContent.children.at(-1) || rootContainer;
41321
+ };
41322
+ currentMarkedContent.children.push(markedContent);
41282
41323
  markedContentStack.push(markedContent);
41283
41324
  break;
41284
41325
  }
41285
41326
  case OPS.endMarkedContent: {
41286
- const oldMarkedContent = markedContentStack.pop();
41287
- markedContent = markedContentStack.at(-1) || rootContainer;
41288
- if (["artifact", "figure", "formula"].includes(oldMarkedContent.role)) {
41289
- markedContent.children = markedContent.children.filter(
41290
- (c) => c !== oldMarkedContent
41291
- );
41292
- }
41327
+ closeMarkedContent();
41293
41328
  break;
41294
41329
  }
41295
41330
  case OPS.transform: {
@@ -41368,6 +41403,7 @@ export {
41368
41403
  InvalidPDFException,
41369
41404
  MathClamp,
41370
41405
  OPS,
41406
+ OPS_DICT,
41371
41407
  OutputScale,
41372
41408
  PDFDataRangeTransport,
41373
41409
  PDFDateString,