pdf-lite 1.7.4-alpha.2 → 1.7.4-alpha.4

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.
@@ -26,6 +26,11 @@ export class PdfGraphics {
26
26
  }
27
27
  beginText() {
28
28
  this.lines.push('BT');
29
+ // Re-emit font inside text object — some viewers (e.g. pdfjs)
30
+ // require Tf inside BT..ET before any text rendering operator.
31
+ if (this.defaultAppearance) {
32
+ this.lines.push(this.defaultAppearance.toString());
33
+ }
29
34
  return this;
30
35
  }
31
36
  endText() {
@@ -1,4 +1,5 @@
1
1
  import { PdfString } from '../../core/objects/pdf-string.js';
2
+ import { PdfName } from '../../core/objects/pdf-name.js';
2
3
  import { encodeToPDFDocEncoding } from '../../utils/encodeToPDFDocEncoding.js';
3
4
  /**
4
5
  * Value object that parses and builds DA (Default Appearance) strings.
@@ -12,7 +13,7 @@ export class PdfDefaultAppearance extends PdfString {
12
13
  _fontSize;
13
14
  _colorOp;
14
15
  constructor(fontName, fontSize, colorOp) {
15
- super(`/${fontName} ${fontSize} Tf ${colorOp}`);
16
+ super(`/${PdfName.escapeName(fontName)} ${fontSize} Tf ${colorOp}`);
16
17
  this._fontName = fontName;
17
18
  this._fontSize = fontSize;
18
19
  this._colorOp = colorOp;
@@ -43,13 +44,13 @@ export class PdfDefaultAppearance extends PdfString {
43
44
  this.raw = encodeToPDFDocEncoding(this.toString());
44
45
  }
45
46
  toString() {
46
- return `/${this._fontName} ${this._fontSize} Tf ${this._colorOp}`;
47
+ return `/${PdfName.escapeName(this._fontName)} ${this._fontSize} Tf ${this._colorOp}`;
47
48
  }
48
49
  static parse(da) {
49
50
  const fontMatch = da.match(/\/([\S]+)\s+([\d.]+)\s+Tf/);
50
51
  if (!fontMatch)
51
52
  return null;
52
- const fontName = fontMatch[1];
53
+ const fontName = PdfName.unescapeName(fontMatch[1]);
53
54
  const fontSize = parseFloat(fontMatch[2]) || 0;
54
55
  let colorOp = '0 g';
55
56
  const rgMatch = da.match(/([\d.]+\s+[\d.]+\s+[\d.]+)\s+rg/);
@@ -390,6 +390,7 @@ export class PdfFormField extends PdfWidgetAnnotation {
390
390
  parsed.fontSize = size;
391
391
  this.content.set('DA', parsed);
392
392
  }
393
+ this.updateAppearance();
393
394
  }
394
395
  get fontName() {
395
396
  const da = this.defaultAppearance || '';
@@ -427,6 +428,7 @@ export class PdfFormField extends PdfWidgetAnnotation {
427
428
  parsed.fontName = resourceName;
428
429
  this.content.set('DA', parsed);
429
430
  }
431
+ this.updateAppearance();
430
432
  }
431
433
  _embedFontInDR(font) {
432
434
  const resourceName = font.resourceName;
@@ -652,7 +652,14 @@ export class PdfFont extends PdfIndirectObject {
652
652
  font.content.set('DescendantFonts', new PdfArray([cidFontObject.reference]));
653
653
  // Create ToUnicode CMap if mappings provided
654
654
  if (unicodeMappings && unicodeMappings.size > 0) {
655
- const cmapContent = PdfFont.generateToUnicodeCMap(unicodeMappings);
655
+ // unicodeMappings is Map<Unicode, GID> but ToUnicode needs
656
+ // Map<CID, Unicode>. Since CID = Unicode codepoint in our
657
+ // Identity-H scheme, the ToUnicode is an identity mapping.
658
+ const toUnicodeMap = new Map();
659
+ for (const unicode of unicodeMappings.keys()) {
660
+ toUnicodeMap.set(unicode, unicode);
661
+ }
662
+ const cmapContent = PdfFont.generateToUnicodeCMap(toUnicodeMap);
656
663
  const cmapStream = new PdfStream({
657
664
  header: new PdfDictionary(),
658
665
  original: new TextEncoder().encode(cmapContent),
@@ -1061,6 +1061,7 @@ export class PdfDocument extends PdfObject {
1061
1061
  // Third pass: reuse tokens from second pass since xref should now be stable
1062
1062
  // (xref binary size should not change because W widths and entry count are same).
1063
1063
  this.calculateOffsets(tokens);
1064
+ this.updateRevisions();
1064
1065
  }
1065
1066
  finally {
1066
1067
  this._updating = false;
@@ -1111,6 +1112,7 @@ export class PdfDocument extends PdfObject {
1111
1112
  * @returns A cloned PdfDocument instance
1112
1113
  */
1113
1114
  cloneImpl() {
1115
+ this.update();
1114
1116
  const clonedRevisions = this.revisions.map((rev) => rev.clone());
1115
1117
  const cloned = new PdfDocument({
1116
1118
  revisions: clonedRevisions,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pdf-lite",
3
- "version": "1.7.4-alpha.2",
3
+ "version": "1.7.4-alpha.4",
4
4
  "main": "dist/index.js",
5
5
  "type": "module",
6
6
  "exports": {