@chialab/pdfjs-lib 1.0.0-alpha.35 → 1.0.0-alpha.37

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,6 +42322,10 @@ var loadTextLayerFontsMap = /* @__PURE__ */ (() => {
42318
42322
  return promise;
42319
42323
  };
42320
42324
  })();
42325
+ var HYPHEN_REGEX2 = /-\n+$/;
42326
+ var isNameObject = (obj) => {
42327
+ return obj !== null && typeof obj === "object" && "name" in obj;
42328
+ };
42321
42329
  async function createTextLayerV2(page, {
42322
42330
  graphics,
42323
42331
  annotations: _annotations
@@ -42341,6 +42349,8 @@ async function createTextLayerV2(page, {
42341
42349
  const markedContentStack = [rootContainer];
42342
42350
  let markedContent = rootContainer;
42343
42351
  let currentTextItem = null;
42352
+ let lastTextItem = null;
42353
+ let transformMatrix = [1, 0, 0, 1, 0, 0];
42344
42354
  let textMatrix = [1, 0, 0, 1, 0, 0];
42345
42355
  let lineMatrix = [1, 0, 0, 1, 0, 0];
42346
42356
  let fontSize = 0;
@@ -42351,29 +42361,50 @@ async function createTextLayerV2(page, {
42351
42361
  let leading = 0;
42352
42362
  let textRise = 0;
42353
42363
  let textPosition = 0;
42354
- const createTextItem = (fontFamily2) => ({
42355
- glyphs: [],
42356
- font: page.commonObjs.get(fontFamily2)
42357
- });
42364
+ const createTextItem = (fontFamily2) => {
42365
+ const font = page.commonObjs.get(fontFamily2);
42366
+ const textFont = fonts[font.bold || font.black ? font.italic ? 3 : 2 : font.italic ? 1 : 0];
42367
+ const riseMatrix = [1, 0, 0, 1, 0, textRise];
42368
+ const finalMatrix = Util.transform(
42369
+ transformMatrix,
42370
+ Util.transform(textMatrix, riseMatrix)
42371
+ );
42372
+ const [, , , , x, y] = Util.transform(transform, finalMatrix);
42373
+ const finalFontSize = fontSize * Math.sqrt(finalMatrix[2] ** 2 + finalMatrix[3] ** 2);
42374
+ const shift = -finalFontSize - finalFontSize * textFont.descender / textFont.unitsPerEm;
42375
+ const angle = -Math.atan2(finalMatrix[1], finalMatrix[0]);
42376
+ const top = y + shift * Math.cos(angle);
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
+ };
42358
42387
  const resetTextItem = () => {
42359
42388
  currentTextItem = null;
42360
42389
  textPosition = 0;
42361
42390
  };
42362
42391
  const closeTextItem = () => {
42392
+ if (currentTextItem) {
42393
+ lastTextItem = currentTextItem;
42394
+ }
42363
42395
  if (!currentTextItem?.glyphs.length) {
42364
42396
  resetTextItem();
42365
42397
  return;
42366
42398
  }
42367
- const { font, glyphs: glyphsList } = currentTextItem;
42399
+ const {
42400
+ font,
42401
+ glyphs: glyphsList,
42402
+ fontSize: finalFontSize,
42403
+ top,
42404
+ left,
42405
+ angle
42406
+ } = currentTextItem;
42368
42407
  const textFont = fonts[font.bold || font.black ? font.italic ? 3 : 2 : font.italic ? 1 : 0];
42369
- const riseMatrix = [1, 0, 0, 1, 0, textRise];
42370
- const finalMatrix = Util.transform(textMatrix, riseMatrix);
42371
- const [, , , , x, y] = Util.transform(transform, finalMatrix);
42372
- const finalFontSize = fontSize * Math.sqrt(finalMatrix[2] ** 2 + finalMatrix[3] ** 2);
42373
- const angle = -Math.atan2(finalMatrix[1], finalMatrix[0]);
42374
- const shift = -finalFontSize - finalFontSize * textFont.descender / textFont.unitsPerEm;
42375
- const top = y + shift * Math.cos(angle);
42376
- const left = x - shift * Math.sin(angle);
42377
42408
  const glyphsBlocks = glyphsList.reduce(
42378
42409
  (acc, glyph) => {
42379
42410
  if (!glyph[0]) {
@@ -42391,12 +42422,14 @@ async function createTextLayerV2(page, {
42391
42422
  );
42392
42423
  const chunks = [];
42393
42424
  let currentLeft = 0;
42425
+ let computedText = "";
42394
42426
  for (let i = 0; i < glyphsBlocks.length; i++) {
42395
42427
  const glyphs = glyphsBlocks[i];
42396
42428
  if (glyphs.length === 0) {
42397
42429
  continue;
42398
42430
  }
42399
42431
  const text = glyphs.map((g) => g[0].unicode).join("");
42432
+ computedText += text;
42400
42433
  const textWidth = textFont.getAdvanceWidth(text, fontSize);
42401
42434
  if (!textWidth) {
42402
42435
  continue;
@@ -42429,30 +42462,60 @@ async function createTextLayerV2(page, {
42429
42462
  }
42430
42463
  }
42431
42464
  chunks.push({
42432
- text,
42465
+ text: bidi(text).str,
42433
42466
  width: textWidth,
42434
42467
  margin: margin * finalFontSize,
42435
42468
  scale: graphicWidth / textWidth
42436
42469
  });
42437
42470
  currentLeft += margin + textWidth / fontSize;
42438
42471
  }
42472
+ const dir = bidi(computedText).dir;
42439
42473
  markedContent.children.push({
42440
42474
  role: "text",
42441
- text: chunks,
42475
+ text: dir === "ltr" ? chunks : chunks.reverse(),
42442
42476
  fontFamily: font.loadedName,
42443
42477
  fontFallback: "sans-serif",
42444
- hyphen: false,
42478
+ hyphen: HYPHEN_REGEX2.test(computedText),
42445
42479
  top,
42446
42480
  left,
42447
42481
  fontSize: finalFontSize,
42448
42482
  fontWeight: font?.black ? 900 : font?.bold ? 700 : 400,
42449
42483
  fontStyle: font?.italic ? "italic" : "normal",
42450
- dir: "ltr",
42484
+ dir,
42451
42485
  scale: 1,
42452
42486
  angle: angle * (180 / Math.PI)
42453
42487
  });
42454
42488
  resetTextItem();
42455
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
+ };
42456
42519
  for (let i = 0; i < operatorsList.fnArray.length; i++) {
42457
42520
  const fnId = operatorsList.fnArray[i];
42458
42521
  const args = operatorsList.argsArray[i];
@@ -42472,6 +42535,7 @@ async function createTextLayerV2(page, {
42472
42535
  case OPS.showText:
42473
42536
  case OPS.showSpacedText: {
42474
42537
  currentTextItem ?? (currentTextItem = createTextItem(fontFamily));
42538
+ createParagraphIfNeeded(lastTextItem, currentTextItem);
42475
42539
  const [chars] = args;
42476
42540
  for (const char of chars) {
42477
42541
  if (typeof char === "number") {
@@ -42526,6 +42590,7 @@ async function createTextLayerV2(page, {
42526
42590
  case OPS.nextLineShowText: {
42527
42591
  closeTextItem();
42528
42592
  currentTextItem = createTextItem(fontFamily);
42593
+ createParagraphIfNeeded(lastTextItem, currentTextItem);
42529
42594
  textMatrix[4] = lineMatrix[4];
42530
42595
  textMatrix[5] = textMatrix[1] * 0 + textMatrix[3] * -leading + textMatrix[5];
42531
42596
  lineMatrix = [...textMatrix];
@@ -42555,6 +42620,7 @@ async function createTextLayerV2(page, {
42555
42620
  case OPS.nextLineSetSpacingShowText: {
42556
42621
  closeTextItem();
42557
42622
  currentTextItem = createTextItem(fontFamily);
42623
+ createParagraphIfNeeded(lastTextItem, currentTextItem);
42558
42624
  const [aw, ac, text] = args;
42559
42625
  wordSpacing = aw;
42560
42626
  charSpacing = ac;
@@ -42600,29 +42666,37 @@ async function createTextLayerV2(page, {
42600
42666
  case OPS.beginMarkedContentProps: {
42601
42667
  const [role, idRef, props] = args;
42602
42668
  const id2 = normalizeMarkedContentId(idRef);
42603
- markedContent.children.push({
42669
+ let normalizedRole = isNameObject(role) ? role.name.toLowerCase() : role?.toString().toLowerCase();
42670
+ switch (normalizedRole) {
42671
+ case "placedgraphic":
42672
+ normalizedRole = "figure";
42673
+ break;
42674
+ }
42675
+ const currentMarkedContent = markedContent || rootContainer;
42676
+ markedContent = {
42604
42677
  id: id2,
42605
- role: role?.toString().toLowerCase() || "span",
42678
+ role: normalizedRole || "span",
42606
42679
  attrs: props ?? void 0,
42607
42680
  children: []
42608
- });
42609
- markedContent = markedContent.children.at(-1) || rootContainer;
42681
+ };
42682
+ currentMarkedContent.children.push(markedContent);
42610
42683
  markedContentStack.push(markedContent);
42611
42684
  break;
42612
42685
  }
42613
42686
  case OPS.endMarkedContent: {
42614
- const oldMarkedContent = markedContentStack.pop();
42615
- markedContent = markedContentStack.at(-1) || rootContainer;
42616
- if (["artifact", "figure", "formula"].includes(oldMarkedContent.role)) {
42617
- markedContent.children = markedContent.children.filter(
42618
- (c) => c !== oldMarkedContent
42619
- );
42620
- }
42687
+ closeMarkedContent();
42688
+ break;
42689
+ }
42690
+ case OPS.transform: {
42691
+ closeTextItem();
42692
+ const [a, b, c, d, e, f] = args;
42693
+ transformMatrix = Util.transform(transformMatrix, [a, b, c, d, e, f]);
42621
42694
  break;
42622
42695
  }
42623
42696
  case OPS.restore: {
42624
42697
  const state = stateStack.pop();
42625
42698
  if (state) {
42699
+ transformMatrix = state.transformMatrix;
42626
42700
  textMatrix = state.textMatrix;
42627
42701
  lineMatrix = state.lineMatrix;
42628
42702
  fontSize = state.fontSize;
@@ -42637,6 +42711,7 @@ async function createTextLayerV2(page, {
42637
42711
  }
42638
42712
  case OPS.save: {
42639
42713
  stateStack.push({
42714
+ transformMatrix: [...transformMatrix],
42640
42715
  textMatrix: [...textMatrix],
42641
42716
  lineMatrix: [...lineMatrix],
42642
42717
  fontSize,
@@ -42688,6 +42763,7 @@ export {
42688
42763
  InvalidPDFException,
42689
42764
  MathClamp,
42690
42765
  OPS,
42766
+ OPS_DICT,
42691
42767
  OutputScale,
42692
42768
  PDFDataRangeTransport,
42693
42769
  PDFDateString,