@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.
@@ -30,6 +30,7 @@ import {
30
30
  VerbosityLevel,
31
31
  _isValidExplicitDest,
32
32
  assert,
33
+ bidi,
33
34
  convertBlackAndWhiteToRGBA,
34
35
  createValidAbsoluteUrl,
35
36
  fromBase64Util,
@@ -47,7 +48,7 @@ import {
47
48
  updateUrlHash,
48
49
  warn,
49
50
  wrapReason
50
- } from "./chunk-R7G44NYV.js";
51
+ } from "./chunk-WUR3YYPJ.js";
51
52
  import {
52
53
  NodeCMapReaderFactory,
53
54
  NodeCanvasFactory,
@@ -27581,6 +27582,9 @@ function randomUUID() {
27581
27582
  hex[8] = hex[13] = hex[18] = hex[23] = "-";
27582
27583
  return hex.join("");
27583
27584
  }
27585
+ var OPS_DICT = Object.fromEntries(
27586
+ Object.entries(OPS).map(([key, value]) => [value, key])
27587
+ );
27584
27588
 
27585
27589
  // src/lib/Svg.ts
27586
27590
  function isSvgElement(node) {
@@ -41427,14 +41431,14 @@ Font.prototype.updateFeatures = function(options) {
41427
41431
  };
41428
41432
  Font.prototype.stringToGlyphs = function(s, options) {
41429
41433
  var this$1 = this;
41430
- var bidi = new Bidi();
41434
+ var bidi2 = new Bidi();
41431
41435
  var charToGlyphIndexMod = function(token) {
41432
41436
  return this$1.charToGlyphIndex(token.char);
41433
41437
  };
41434
- bidi.registerModifier("glyphIndex", null, charToGlyphIndexMod);
41438
+ bidi2.registerModifier("glyphIndex", null, charToGlyphIndexMod);
41435
41439
  var features = options ? this.updateFeatures(options.features) : this.defaultRenderOptions.features;
41436
- bidi.applyFeatures(this, features);
41437
- var indexes = bidi.getTextGlyphs(s);
41440
+ bidi2.applyFeatures(this, features);
41441
+ var indexes = bidi2.getTextGlyphs(s);
41438
41442
  var length = indexes.length;
41439
41443
  var glyphs = new Array(length);
41440
41444
  var notdef = this.glyphs.get(0);
@@ -42318,21 +42322,7 @@ var loadTextLayerFontsMap = /* @__PURE__ */ (() => {
42318
42322
  return promise;
42319
42323
  };
42320
42324
  })();
42321
- var getTextDirection = (text) => {
42322
- const rtlChars = /[\u0591-\u07FF\uFB1D-\uFDFD\uFE70-\uFEFC]/;
42323
- const ltrChars = /[A-Za-z]/;
42324
- let rtlCount = 0;
42325
- let ltrCount = 0;
42326
- for (const char of text) {
42327
- if (rtlChars.test(char)) {
42328
- rtlCount++;
42329
- }
42330
- if (ltrChars.test(char)) {
42331
- ltrCount++;
42332
- }
42333
- }
42334
- return rtlCount > ltrCount ? "rtl" : "ltr";
42335
- };
42325
+ var HYPHEN_REGEX2 = /-\n+$/;
42336
42326
  var isNameObject = (obj) => {
42337
42327
  return obj !== null && typeof obj === "object" && "name" in obj;
42338
42328
  };
@@ -42359,6 +42349,7 @@ async function createTextLayerV2(page, {
42359
42349
  const markedContentStack = [rootContainer];
42360
42350
  let markedContent = rootContainer;
42361
42351
  let currentTextItem = null;
42352
+ let lastTextItem = null;
42362
42353
  let transformMatrix = [1, 0, 0, 1, 0, 0];
42363
42354
  let textMatrix = [1, 0, 0, 1, 0, 0];
42364
42355
  let lineMatrix = [1, 0, 0, 1, 0, 0];
@@ -42370,20 +42361,8 @@ async function createTextLayerV2(page, {
42370
42361
  let leading = 0;
42371
42362
  let textRise = 0;
42372
42363
  let textPosition = 0;
42373
- const createTextItem = (fontFamily2) => ({
42374
- glyphs: [],
42375
- font: page.commonObjs.get(fontFamily2)
42376
- });
42377
- const resetTextItem = () => {
42378
- currentTextItem = null;
42379
- textPosition = 0;
42380
- };
42381
- const closeTextItem = () => {
42382
- if (!currentTextItem?.glyphs.length) {
42383
- resetTextItem();
42384
- return;
42385
- }
42386
- const { font, glyphs: glyphsList } = currentTextItem;
42364
+ const createTextItem = (fontFamily2) => {
42365
+ const font = page.commonObjs.get(fontFamily2);
42387
42366
  const textFont = fonts[font.bold || font.black ? font.italic ? 3 : 2 : font.italic ? 1 : 0];
42388
42367
  const riseMatrix = [1, 0, 0, 1, 0, textRise];
42389
42368
  const finalMatrix = Util.transform(
@@ -42392,10 +42371,40 @@ async function createTextLayerV2(page, {
42392
42371
  );
42393
42372
  const [, , , , x, y] = Util.transform(transform, finalMatrix);
42394
42373
  const finalFontSize = fontSize * Math.sqrt(finalMatrix[2] ** 2 + finalMatrix[3] ** 2);
42395
- const angle = -Math.atan2(finalMatrix[1], finalMatrix[0]);
42396
42374
  const shift = -finalFontSize - finalFontSize * textFont.descender / textFont.unitsPerEm;
42375
+ const angle = -Math.atan2(finalMatrix[1], finalMatrix[0]);
42397
42376
  const top = y + shift * Math.cos(angle);
42398
42377
  const left = x - shift * Math.sin(angle);
42378
+ return {
42379
+ glyphs: [],
42380
+ font,
42381
+ fontSize: finalFontSize,
42382
+ top,
42383
+ left,
42384
+ angle
42385
+ };
42386
+ };
42387
+ const resetTextItem = () => {
42388
+ currentTextItem = null;
42389
+ textPosition = 0;
42390
+ };
42391
+ const closeTextItem = () => {
42392
+ if (currentTextItem) {
42393
+ lastTextItem = currentTextItem;
42394
+ }
42395
+ if (!currentTextItem?.glyphs.length) {
42396
+ resetTextItem();
42397
+ return;
42398
+ }
42399
+ const {
42400
+ font,
42401
+ glyphs: glyphsList,
42402
+ fontSize: finalFontSize,
42403
+ top,
42404
+ left,
42405
+ angle
42406
+ } = currentTextItem;
42407
+ const textFont = fonts[font.bold || font.black ? font.italic ? 3 : 2 : font.italic ? 1 : 0];
42399
42408
  const glyphsBlocks = glyphsList.reduce(
42400
42409
  (acc, glyph) => {
42401
42410
  if (!glyph[0]) {
@@ -42453,30 +42462,60 @@ async function createTextLayerV2(page, {
42453
42462
  }
42454
42463
  }
42455
42464
  chunks.push({
42456
- text,
42465
+ text: bidi(text).str,
42457
42466
  width: textWidth,
42458
42467
  margin: margin * finalFontSize,
42459
42468
  scale: graphicWidth / textWidth
42460
42469
  });
42461
42470
  currentLeft += margin + textWidth / fontSize;
42462
42471
  }
42472
+ const dir = bidi(computedText).dir;
42463
42473
  markedContent.children.push({
42464
42474
  role: "text",
42465
- text: chunks,
42475
+ text: dir === "ltr" ? chunks : chunks.reverse(),
42466
42476
  fontFamily: font.loadedName,
42467
42477
  fontFallback: "sans-serif",
42468
- hyphen: false,
42478
+ hyphen: HYPHEN_REGEX2.test(computedText),
42469
42479
  top,
42470
42480
  left,
42471
42481
  fontSize: finalFontSize,
42472
42482
  fontWeight: font?.black ? 900 : font?.bold ? 700 : 400,
42473
42483
  fontStyle: font?.italic ? "italic" : "normal",
42474
- dir: getTextDirection(computedText),
42484
+ dir,
42475
42485
  scale: 1,
42476
42486
  angle: angle * (180 / Math.PI)
42477
42487
  });
42478
42488
  resetTextItem();
42479
42489
  };
42490
+ const createParagraphIfNeeded = (oldTextItem, newTextItem) => {
42491
+ if (structTree) {
42492
+ return;
42493
+ }
42494
+ if (oldTextItem && newTextItem && oldTextItem.fontSize === newTextItem.fontSize) {
42495
+ return;
42496
+ }
42497
+ if (oldTextItem) {
42498
+ closeMarkedContent();
42499
+ }
42500
+ const currentMarkedContent = markedContent || rootContainer;
42501
+ markedContent = {
42502
+ role: "p",
42503
+ children: []
42504
+ };
42505
+ currentMarkedContent.children.push(markedContent);
42506
+ markedContentStack.push(markedContent);
42507
+ };
42508
+ const closeMarkedContent = () => {
42509
+ const oldMarkedContent = markedContentStack.pop();
42510
+ markedContent = markedContentStack.at(-1) || rootContainer;
42511
+ if (oldMarkedContent) {
42512
+ if (["artifact", "figure", "formula"].includes(oldMarkedContent.role)) {
42513
+ markedContent.children = markedContent.children.filter(
42514
+ (c) => c !== oldMarkedContent
42515
+ );
42516
+ }
42517
+ }
42518
+ };
42480
42519
  for (let i = 0; i < operatorsList.fnArray.length; i++) {
42481
42520
  const fnId = operatorsList.fnArray[i];
42482
42521
  const args = operatorsList.argsArray[i];
@@ -42496,6 +42535,7 @@ async function createTextLayerV2(page, {
42496
42535
  case OPS.showText:
42497
42536
  case OPS.showSpacedText: {
42498
42537
  currentTextItem ?? (currentTextItem = createTextItem(fontFamily));
42538
+ createParagraphIfNeeded(lastTextItem, currentTextItem);
42499
42539
  const [chars] = args;
42500
42540
  for (const char of chars) {
42501
42541
  if (typeof char === "number") {
@@ -42550,6 +42590,7 @@ async function createTextLayerV2(page, {
42550
42590
  case OPS.nextLineShowText: {
42551
42591
  closeTextItem();
42552
42592
  currentTextItem = createTextItem(fontFamily);
42593
+ createParagraphIfNeeded(lastTextItem, currentTextItem);
42553
42594
  textMatrix[4] = lineMatrix[4];
42554
42595
  textMatrix[5] = textMatrix[1] * 0 + textMatrix[3] * -leading + textMatrix[5];
42555
42596
  lineMatrix = [...textMatrix];
@@ -42579,6 +42620,7 @@ async function createTextLayerV2(page, {
42579
42620
  case OPS.nextLineSetSpacingShowText: {
42580
42621
  closeTextItem();
42581
42622
  currentTextItem = createTextItem(fontFamily);
42623
+ createParagraphIfNeeded(lastTextItem, currentTextItem);
42582
42624
  const [aw, ac, text] = args;
42583
42625
  wordSpacing = aw;
42584
42626
  charSpacing = ac;
@@ -42630,24 +42672,19 @@ async function createTextLayerV2(page, {
42630
42672
  normalizedRole = "figure";
42631
42673
  break;
42632
42674
  }
42633
- markedContent.children.push({
42675
+ const currentMarkedContent = markedContent || rootContainer;
42676
+ markedContent = {
42634
42677
  id: id2,
42635
42678
  role: normalizedRole || "span",
42636
42679
  attrs: props ?? void 0,
42637
42680
  children: []
42638
- });
42639
- markedContent = markedContent.children.at(-1) || rootContainer;
42681
+ };
42682
+ currentMarkedContent.children.push(markedContent);
42640
42683
  markedContentStack.push(markedContent);
42641
42684
  break;
42642
42685
  }
42643
42686
  case OPS.endMarkedContent: {
42644
- const oldMarkedContent = markedContentStack.pop();
42645
- markedContent = markedContentStack.at(-1) || rootContainer;
42646
- if (["artifact", "figure", "formula"].includes(oldMarkedContent.role)) {
42647
- markedContent.children = markedContent.children.filter(
42648
- (c) => c !== oldMarkedContent
42649
- );
42650
- }
42687
+ closeMarkedContent();
42651
42688
  break;
42652
42689
  }
42653
42690
  case OPS.transform: {
@@ -42726,6 +42763,7 @@ export {
42726
42763
  InvalidPDFException,
42727
42764
  MathClamp,
42728
42765
  OPS,
42766
+ OPS_DICT,
42729
42767
  OutputScale,
42730
42768
  PDFDataRangeTransport,
42731
42769
  PDFDateString,