pdfkit 0.12.3 → 0.13.0

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.
package/js/pdfkit.js CHANGED
@@ -60,9 +60,7 @@ class PDFTree {
60
60
  return out.join('\n');
61
61
  }
62
62
 
63
- _compareKeys()
64
- /*a, b*/
65
- {
63
+ _compareKeys() {
66
64
  throw new Error('Must be implemented by subclasses');
67
65
  }
68
66
 
@@ -70,9 +68,7 @@ class PDFTree {
70
68
  throw new Error('Must be implemented by subclasses');
71
69
  }
72
70
 
73
- _dataForKey()
74
- /*k*/
75
- {
71
+ _dataForKey() {
76
72
  throw new Error('Must be implemented by subclasses');
77
73
  }
78
74
 
@@ -401,6 +397,11 @@ class PDFPage {
401
397
  return data.Pattern != null ? data.Pattern : data.Pattern = {};
402
398
  }
403
399
 
400
+ get colorSpaces() {
401
+ const data = this.resources.data;
402
+ return data.ColorSpace || (data.ColorSpace = {});
403
+ }
404
+
404
405
  get annotations() {
405
406
  const data = this.dictionary.data;
406
407
  return data.Annots != null ? data.Annots : data.Annots = [];
@@ -1462,7 +1463,7 @@ class PDFGradient {
1462
1463
  return pattern;
1463
1464
  }
1464
1465
 
1465
- apply(op) {
1466
+ apply(stroke) {
1466
1467
  // apply gradient transform to existing document ctm
1467
1468
  const [m0, m1, m2, m3, m4, m5] = this.doc._ctm;
1468
1469
  const [m11, m12, m21, m22, dx, dy] = this.transform;
@@ -1472,6 +1473,9 @@ class PDFGradient {
1472
1473
  this.embed(m);
1473
1474
  }
1474
1475
 
1476
+ this.doc._setColorSpace('Pattern', stroke);
1477
+
1478
+ const op = stroke ? 'SCN' : 'scn';
1475
1479
  return this.doc.addContent(`/${this.id} ${op}`);
1476
1480
  }
1477
1481
 
@@ -1536,24 +1540,119 @@ var Gradient = {
1536
1540
  PDFRadialGradient
1537
1541
  };
1538
1542
 
1543
+ /*
1544
+ PDF tiling pattern support. Uncolored only.
1545
+ */
1546
+ const underlyingColorSpaces = ['DeviceCMYK', 'DeviceRGB'];
1547
+
1548
+ class PDFTilingPattern {
1549
+ constructor(doc, bBox, xStep, yStep, stream) {
1550
+ this.doc = doc;
1551
+ this.bBox = bBox;
1552
+ this.xStep = xStep;
1553
+ this.yStep = yStep;
1554
+ this.stream = stream;
1555
+ }
1556
+
1557
+ createPattern() {
1558
+ // no resources needed for our current usage
1559
+ // required entry
1560
+ const resources = this.doc.ref();
1561
+ resources.end(); // apply default transform matrix (flipped in the default doc._ctm)
1562
+ // see document.js & gradient.js
1563
+
1564
+ const [m0, m1, m2, m3, m4, m5] = this.doc._ctm;
1565
+ const [m11, m12, m21, m22, dx, dy] = [1, 0, 0, 1, 0, 0];
1566
+ const m = [m0 * m11 + m2 * m12, m1 * m11 + m3 * m12, m0 * m21 + m2 * m22, m1 * m21 + m3 * m22, m0 * dx + m2 * dy + m4, m1 * dx + m3 * dy + m5];
1567
+ const pattern = this.doc.ref({
1568
+ Type: 'Pattern',
1569
+ PatternType: 1,
1570
+ // tiling
1571
+ PaintType: 2,
1572
+ // 1-colored, 2-uncolored
1573
+ TilingType: 2,
1574
+ // 2-no distortion
1575
+ BBox: this.bBox,
1576
+ XStep: this.xStep,
1577
+ YStep: this.yStep,
1578
+ Matrix: m.map(v => +v.toFixed(5)),
1579
+ Resources: resources
1580
+ });
1581
+ pattern.end(this.stream);
1582
+ return pattern;
1583
+ }
1584
+
1585
+ embedPatternColorSpaces() {
1586
+ // map each pattern to an underlying color space
1587
+ // and embed on each page
1588
+ underlyingColorSpaces.forEach(csName => {
1589
+ const csId = this.getPatternColorSpaceId(csName);
1590
+ if (this.doc.page.colorSpaces[csId]) return;
1591
+ const cs = this.doc.ref(['Pattern', csName]);
1592
+ cs.end();
1593
+ this.doc.page.colorSpaces[csId] = cs;
1594
+ });
1595
+ }
1596
+
1597
+ getPatternColorSpaceId(underlyingColorspace) {
1598
+ return `CsP${underlyingColorspace}`;
1599
+ }
1600
+
1601
+ embed() {
1602
+ if (!this.id) {
1603
+ this.doc._patternCount = this.doc._patternCount + 1;
1604
+ this.id = 'P' + this.doc._patternCount;
1605
+ this.pattern = this.createPattern();
1606
+ } // patterns are embedded in each page
1607
+
1608
+
1609
+ if (!this.doc.page.patterns[this.id]) {
1610
+ this.doc.page.patterns[this.id] = this.pattern;
1611
+ }
1612
+ }
1613
+
1614
+ apply(stroke, patternColor) {
1615
+ // do any embedding/creating that might be needed
1616
+ this.embedPatternColorSpaces();
1617
+ this.embed();
1618
+
1619
+ const normalizedColor = this.doc._normalizeColor(patternColor);
1620
+
1621
+ if (!normalizedColor) throw Error(`invalid pattern color. (value: ${patternColor})`); // select one of the pattern color spaces
1622
+
1623
+ const csId = this.getPatternColorSpaceId(this.doc._getColorSpace(normalizedColor));
1624
+
1625
+ this.doc._setColorSpace(csId, stroke); // stroke/fill using the pattern and color (in the above underlying color space)
1626
+
1627
+
1628
+ const op = stroke ? 'SCN' : 'scn';
1629
+ return this.doc.addContent(`${normalizedColor.join(' ')} /${this.id} ${op}`);
1630
+ }
1631
+
1632
+ }
1633
+
1634
+ var pattern = {
1635
+ PDFTilingPattern
1636
+ };
1637
+
1539
1638
  const {
1540
1639
  PDFGradient: PDFGradient$1,
1541
1640
  PDFLinearGradient: PDFLinearGradient$1,
1542
1641
  PDFRadialGradient: PDFRadialGradient$1
1543
1642
  } = Gradient;
1643
+ const {
1644
+ PDFTilingPattern: PDFTilingPattern$1
1645
+ } = pattern;
1544
1646
  var ColorMixin = {
1545
1647
  initColor() {
1546
1648
  // The opacity dictionaries
1547
1649
  this._opacityRegistry = {};
1548
1650
  this._opacityCount = 0;
1651
+ this._patternCount = 0;
1549
1652
  return this._gradCount = 0;
1550
1653
  },
1551
1654
 
1552
1655
  _normalizeColor(color) {
1553
- if (color instanceof PDFGradient$1) {
1554
- return color;
1555
- }
1556
-
1557
1656
  if (typeof color === 'string') {
1558
1657
  if (color.charAt(0) === '#') {
1559
1658
  if (color.length === 4) {
@@ -1582,6 +1681,19 @@ var ColorMixin = {
1582
1681
  },
1583
1682
 
1584
1683
  _setColor(color, stroke) {
1684
+ if (color instanceof PDFGradient$1) {
1685
+ color.apply(stroke);
1686
+ return true; // see if tiling pattern, decode & apply it it
1687
+ } else if (Array.isArray(color) && color[0] instanceof PDFTilingPattern$1) {
1688
+ color[0].apply(stroke, color[1]);
1689
+ return true;
1690
+ } // any other case should be a normal color and not a pattern
1691
+
1692
+
1693
+ return this._setColorCore(color, stroke);
1694
+ },
1695
+
1696
+ _setColorCore(color, stroke) {
1585
1697
  color = this._normalizeColor(color);
1586
1698
 
1587
1699
  if (!color) {
@@ -1590,19 +1702,12 @@ var ColorMixin = {
1590
1702
 
1591
1703
  const op = stroke ? 'SCN' : 'scn';
1592
1704
 
1593
- if (color instanceof PDFGradient$1) {
1594
- this._setColorSpace('Pattern', stroke);
1705
+ const space = this._getColorSpace(color);
1595
1706
 
1596
- color.apply(op);
1597
- } else {
1598
- const space = color.length === 4 ? 'DeviceCMYK' : 'DeviceRGB';
1599
-
1600
- this._setColorSpace(space, stroke);
1601
-
1602
- color = color.join(' ');
1603
- this.addContent(`${color} ${op}`);
1604
- }
1707
+ this._setColorSpace(space, stroke);
1605
1708
 
1709
+ color = color.join(' ');
1710
+ this.addContent(`${color} ${op}`);
1606
1711
  return true;
1607
1712
  },
1608
1713
 
@@ -1611,6 +1716,10 @@ var ColorMixin = {
1611
1716
  return this.addContent(`/${space} ${op}`);
1612
1717
  },
1613
1718
 
1719
+ _getColorSpace(color) {
1720
+ return color.length === 4 ? 'DeviceCMYK' : 'DeviceRGB';
1721
+ },
1722
+
1614
1723
  fillColor(color, opacity) {
1615
1724
  const set = this._setColor(color, false);
1616
1725
 
@@ -1701,6 +1810,10 @@ var ColorMixin = {
1701
1810
 
1702
1811
  radialGradient(x1, y1, r1, x2, y2, r2) {
1703
1812
  return new PDFRadialGradient$1(this, x1, y1, r1, x2, y2, r2);
1813
+ },
1814
+
1815
+ pattern(bbox, xStep, yStep, stream) {
1816
+ return new PDFTilingPattern$1(this, bbox, xStep, yStep, stream);
1704
1817
  }
1705
1818
 
1706
1819
  };
@@ -4919,29 +5032,18 @@ var OutlineMixin = {
4919
5032
 
4920
5033
  };
4921
5034
 
4922
- function _defineProperty(obj, key, value) {
4923
- if (key in obj) {
4924
- Object.defineProperty(obj, key, {
4925
- value: value,
4926
- enumerable: true,
4927
- configurable: true,
4928
- writable: true
4929
- });
4930
- } else {
4931
- obj[key] = value;
4932
- }
4933
-
4934
- return obj;
4935
- }
4936
-
4937
5035
  function ownKeys(object, enumerableOnly) {
4938
5036
  var keys = Object.keys(object);
4939
5037
 
4940
5038
  if (Object.getOwnPropertySymbols) {
4941
5039
  var symbols = Object.getOwnPropertySymbols(object);
4942
- if (enumerableOnly) symbols = symbols.filter(function (sym) {
4943
- return Object.getOwnPropertyDescriptor(object, sym).enumerable;
4944
- });
5040
+
5041
+ if (enumerableOnly) {
5042
+ symbols = symbols.filter(function (sym) {
5043
+ return Object.getOwnPropertyDescriptor(object, sym).enumerable;
5044
+ });
5045
+ }
5046
+
4945
5047
  keys.push.apply(keys, symbols);
4946
5048
  }
4947
5049
 
@@ -4968,6 +5070,21 @@ function _objectSpread2(target) {
4968
5070
  return target;
4969
5071
  }
4970
5072
 
5073
+ function _defineProperty(obj, key, value) {
5074
+ if (key in obj) {
5075
+ Object.defineProperty(obj, key, {
5076
+ value: value,
5077
+ enumerable: true,
5078
+ configurable: true,
5079
+ writable: true
5080
+ });
5081
+ } else {
5082
+ obj[key] = value;
5083
+ }
5084
+
5085
+ return obj;
5086
+ }
5087
+
4971
5088
  /*
4972
5089
  PDFStructureContent - a reference to a marked structure content
4973
5090
  By Ben Schmidt