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

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.
@@ -42318,6 +42318,24 @@ var loadTextLayerFontsMap = /* @__PURE__ */ (() => {
42318
42318
  return promise;
42319
42319
  };
42320
42320
  })();
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
+ };
42336
+ var isNameObject = (obj) => {
42337
+ return obj !== null && typeof obj === "object" && "name" in obj;
42338
+ };
42321
42339
  async function createTextLayerV2(page, {
42322
42340
  graphics,
42323
42341
  annotations: _annotations
@@ -42341,6 +42359,7 @@ async function createTextLayerV2(page, {
42341
42359
  const markedContentStack = [rootContainer];
42342
42360
  let markedContent = rootContainer;
42343
42361
  let currentTextItem = null;
42362
+ let transformMatrix = [1, 0, 0, 1, 0, 0];
42344
42363
  let textMatrix = [1, 0, 0, 1, 0, 0];
42345
42364
  let lineMatrix = [1, 0, 0, 1, 0, 0];
42346
42365
  let fontSize = 0;
@@ -42367,7 +42386,10 @@ async function createTextLayerV2(page, {
42367
42386
  const { font, glyphs: glyphsList } = currentTextItem;
42368
42387
  const textFont = fonts[font.bold || font.black ? font.italic ? 3 : 2 : font.italic ? 1 : 0];
42369
42388
  const riseMatrix = [1, 0, 0, 1, 0, textRise];
42370
- const finalMatrix = Util.transform(textMatrix, riseMatrix);
42389
+ const finalMatrix = Util.transform(
42390
+ transformMatrix,
42391
+ Util.transform(textMatrix, riseMatrix)
42392
+ );
42371
42393
  const [, , , , x, y] = Util.transform(transform, finalMatrix);
42372
42394
  const finalFontSize = fontSize * Math.sqrt(finalMatrix[2] ** 2 + finalMatrix[3] ** 2);
42373
42395
  const angle = -Math.atan2(finalMatrix[1], finalMatrix[0]);
@@ -42391,12 +42413,14 @@ async function createTextLayerV2(page, {
42391
42413
  );
42392
42414
  const chunks = [];
42393
42415
  let currentLeft = 0;
42416
+ let computedText = "";
42394
42417
  for (let i = 0; i < glyphsBlocks.length; i++) {
42395
42418
  const glyphs = glyphsBlocks[i];
42396
42419
  if (glyphs.length === 0) {
42397
42420
  continue;
42398
42421
  }
42399
42422
  const text = glyphs.map((g) => g[0].unicode).join("");
42423
+ computedText += text;
42400
42424
  const textWidth = textFont.getAdvanceWidth(text, fontSize);
42401
42425
  if (!textWidth) {
42402
42426
  continue;
@@ -42430,6 +42454,7 @@ async function createTextLayerV2(page, {
42430
42454
  }
42431
42455
  chunks.push({
42432
42456
  text,
42457
+ width: textWidth,
42433
42458
  margin: margin * finalFontSize,
42434
42459
  scale: graphicWidth / textWidth
42435
42460
  });
@@ -42446,7 +42471,7 @@ async function createTextLayerV2(page, {
42446
42471
  fontSize: finalFontSize,
42447
42472
  fontWeight: font?.black ? 900 : font?.bold ? 700 : 400,
42448
42473
  fontStyle: font?.italic ? "italic" : "normal",
42449
- dir: "ltr",
42474
+ dir: getTextDirection(computedText),
42450
42475
  scale: 1,
42451
42476
  angle: angle * (180 / Math.PI)
42452
42477
  });
@@ -42599,9 +42624,15 @@ async function createTextLayerV2(page, {
42599
42624
  case OPS.beginMarkedContentProps: {
42600
42625
  const [role, idRef, props] = args;
42601
42626
  const id2 = normalizeMarkedContentId(idRef);
42627
+ let normalizedRole = isNameObject(role) ? role.name.toLowerCase() : role?.toString().toLowerCase();
42628
+ switch (normalizedRole) {
42629
+ case "placedgraphic":
42630
+ normalizedRole = "figure";
42631
+ break;
42632
+ }
42602
42633
  markedContent.children.push({
42603
42634
  id: id2,
42604
- role: role?.toString().toLowerCase() || "span",
42635
+ role: normalizedRole || "span",
42605
42636
  attrs: props ?? void 0,
42606
42637
  children: []
42607
42638
  });
@@ -42619,9 +42650,16 @@ async function createTextLayerV2(page, {
42619
42650
  }
42620
42651
  break;
42621
42652
  }
42653
+ case OPS.transform: {
42654
+ closeTextItem();
42655
+ const [a, b, c, d, e, f] = args;
42656
+ transformMatrix = Util.transform(transformMatrix, [a, b, c, d, e, f]);
42657
+ break;
42658
+ }
42622
42659
  case OPS.restore: {
42623
42660
  const state = stateStack.pop();
42624
42661
  if (state) {
42662
+ transformMatrix = state.transformMatrix;
42625
42663
  textMatrix = state.textMatrix;
42626
42664
  lineMatrix = state.lineMatrix;
42627
42665
  fontSize = state.fontSize;
@@ -42636,6 +42674,7 @@ async function createTextLayerV2(page, {
42636
42674
  }
42637
42675
  case OPS.save: {
42638
42676
  stateStack.push({
42677
+ transformMatrix: [...transformMatrix],
42639
42678
  textMatrix: [...textMatrix],
42640
42679
  lineMatrix: [...lineMatrix],
42641
42680
  fontSize,
@@ -42,6 +42,10 @@ export interface AnnotationData {
42
42
  str: string;
43
43
  dir: Dir;
44
44
  };
45
+ contentsObj?: {
46
+ str: string;
47
+ dir: Dir;
48
+ };
45
49
  popupRef?: string | null;
46
50
  [key: string]: unknown;
47
51
  }
@@ -22,6 +22,7 @@ export interface TextLayerAnchor extends TextLayerElement {
22
22
  }
23
23
  export interface TextLayerChunk {
24
24
  text: string;
25
+ width: number;
25
26
  margin: number;
26
27
  scale?: number;
27
28
  }
@@ -40960,6 +40960,24 @@ var loadTextLayerFontsMap = /* @__PURE__ */ (() => {
40960
40960
  return promise;
40961
40961
  };
40962
40962
  })();
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
+ };
40978
+ var isNameObject = (obj) => {
40979
+ return obj !== null && typeof obj === "object" && "name" in obj;
40980
+ };
40963
40981
  async function createTextLayerV2(page, {
40964
40982
  graphics,
40965
40983
  annotations: _annotations
@@ -40983,6 +41001,7 @@ async function createTextLayerV2(page, {
40983
41001
  const markedContentStack = [rootContainer];
40984
41002
  let markedContent = rootContainer;
40985
41003
  let currentTextItem = null;
41004
+ let transformMatrix = [1, 0, 0, 1, 0, 0];
40986
41005
  let textMatrix = [1, 0, 0, 1, 0, 0];
40987
41006
  let lineMatrix = [1, 0, 0, 1, 0, 0];
40988
41007
  let fontSize = 0;
@@ -41009,7 +41028,10 @@ async function createTextLayerV2(page, {
41009
41028
  const { font, glyphs: glyphsList } = currentTextItem;
41010
41029
  const textFont = fonts[font.bold || font.black ? font.italic ? 3 : 2 : font.italic ? 1 : 0];
41011
41030
  const riseMatrix = [1, 0, 0, 1, 0, textRise];
41012
- const finalMatrix = Util.transform(textMatrix, riseMatrix);
41031
+ const finalMatrix = Util.transform(
41032
+ transformMatrix,
41033
+ Util.transform(textMatrix, riseMatrix)
41034
+ );
41013
41035
  const [, , , , x, y] = Util.transform(transform, finalMatrix);
41014
41036
  const finalFontSize = fontSize * Math.sqrt(finalMatrix[2] ** 2 + finalMatrix[3] ** 2);
41015
41037
  const angle = -Math.atan2(finalMatrix[1], finalMatrix[0]);
@@ -41033,12 +41055,14 @@ async function createTextLayerV2(page, {
41033
41055
  );
41034
41056
  const chunks = [];
41035
41057
  let currentLeft = 0;
41058
+ let computedText = "";
41036
41059
  for (let i = 0; i < glyphsBlocks.length; i++) {
41037
41060
  const glyphs = glyphsBlocks[i];
41038
41061
  if (glyphs.length === 0) {
41039
41062
  continue;
41040
41063
  }
41041
41064
  const text = glyphs.map((g) => g[0].unicode).join("");
41065
+ computedText += text;
41042
41066
  const textWidth = textFont.getAdvanceWidth(text, fontSize);
41043
41067
  if (!textWidth) {
41044
41068
  continue;
@@ -41072,6 +41096,7 @@ async function createTextLayerV2(page, {
41072
41096
  }
41073
41097
  chunks.push({
41074
41098
  text,
41099
+ width: textWidth,
41075
41100
  margin: margin * finalFontSize,
41076
41101
  scale: graphicWidth / textWidth
41077
41102
  });
@@ -41088,7 +41113,7 @@ async function createTextLayerV2(page, {
41088
41113
  fontSize: finalFontSize,
41089
41114
  fontWeight: font?.black ? 900 : font?.bold ? 700 : 400,
41090
41115
  fontStyle: font?.italic ? "italic" : "normal",
41091
- dir: "ltr",
41116
+ dir: getTextDirection(computedText),
41092
41117
  scale: 1,
41093
41118
  angle: angle * (180 / Math.PI)
41094
41119
  });
@@ -41241,9 +41266,15 @@ async function createTextLayerV2(page, {
41241
41266
  case OPS.beginMarkedContentProps: {
41242
41267
  const [role, idRef, props] = args;
41243
41268
  const id2 = normalizeMarkedContentId(idRef);
41269
+ let normalizedRole = isNameObject(role) ? role.name.toLowerCase() : role?.toString().toLowerCase();
41270
+ switch (normalizedRole) {
41271
+ case "placedgraphic":
41272
+ normalizedRole = "figure";
41273
+ break;
41274
+ }
41244
41275
  markedContent.children.push({
41245
41276
  id: id2,
41246
- role: role?.toString().toLowerCase() || "span",
41277
+ role: normalizedRole || "span",
41247
41278
  attrs: props ?? void 0,
41248
41279
  children: []
41249
41280
  });
@@ -41261,9 +41292,16 @@ async function createTextLayerV2(page, {
41261
41292
  }
41262
41293
  break;
41263
41294
  }
41295
+ case OPS.transform: {
41296
+ closeTextItem();
41297
+ const [a, b, c, d, e, f] = args;
41298
+ transformMatrix = Util.transform(transformMatrix, [a, b, c, d, e, f]);
41299
+ break;
41300
+ }
41264
41301
  case OPS.restore: {
41265
41302
  const state = stateStack.pop();
41266
41303
  if (state) {
41304
+ transformMatrix = state.transformMatrix;
41267
41305
  textMatrix = state.textMatrix;
41268
41306
  lineMatrix = state.lineMatrix;
41269
41307
  fontSize = state.fontSize;
@@ -41278,6 +41316,7 @@ async function createTextLayerV2(page, {
41278
41316
  }
41279
41317
  case OPS.save: {
41280
41318
  stateStack.push({
41319
+ transformMatrix: [...transformMatrix],
41281
41320
  textMatrix: [...textMatrix],
41282
41321
  lineMatrix: [...lineMatrix],
41283
41322
  fontSize,
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.34",
4
+ "version": "1.0.0-alpha.36",
5
5
  "type": "module",
6
6
  "author": "Chialab <dev@chialab.it>",
7
7
  "license": "MIT",