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.
@@ -59,9 +59,7 @@ class PDFTree {
59
59
  return out.join('\n');
60
60
  }
61
61
 
62
- _compareKeys()
63
- /*a, b*/
64
- {
62
+ _compareKeys() {
65
63
  throw new Error('Must be implemented by subclasses');
66
64
  }
67
65
 
@@ -69,9 +67,7 @@ class PDFTree {
69
67
  throw new Error('Must be implemented by subclasses');
70
68
  }
71
69
 
72
- _dataForKey()
73
- /*k*/
74
- {
70
+ _dataForKey() {
75
71
  throw new Error('Must be implemented by subclasses');
76
72
  }
77
73
 
@@ -405,6 +401,11 @@ class PDFPage {
405
401
  return data.Pattern != null ? data.Pattern : data.Pattern = {};
406
402
  }
407
403
 
404
+ get colorSpaces() {
405
+ var data = this.resources.data;
406
+ return data.ColorSpace || (data.ColorSpace = {});
407
+ }
408
+
408
409
  get annotations() {
409
410
  var data = this.dictionary.data;
410
411
  return data.Annots != null ? data.Annots : data.Annots = [];
@@ -1478,7 +1479,7 @@ class PDFGradient {
1478
1479
  return pattern;
1479
1480
  }
1480
1481
 
1481
- apply(op) {
1482
+ apply(stroke) {
1482
1483
  // apply gradient transform to existing document ctm
1483
1484
  var [m0, m1, m2, m3, m4, m5] = this.doc._ctm;
1484
1485
  var [m11, m12, m21, m22, dx, dy] = this.transform;
@@ -1488,6 +1489,9 @@ class PDFGradient {
1488
1489
  this.embed(m);
1489
1490
  }
1490
1491
 
1492
+ this.doc._setColorSpace('Pattern', stroke);
1493
+
1494
+ var op = stroke ? 'SCN' : 'scn';
1491
1495
  return this.doc.addContent("/".concat(this.id, " ").concat(op));
1492
1496
  }
1493
1497
 
@@ -1552,24 +1556,119 @@ var Gradient = {
1552
1556
  PDFRadialGradient
1553
1557
  };
1554
1558
 
1559
+ /*
1560
+ PDF tiling pattern support. Uncolored only.
1561
+ */
1562
+ var underlyingColorSpaces = ['DeviceCMYK', 'DeviceRGB'];
1563
+
1564
+ class PDFTilingPattern {
1565
+ constructor(doc, bBox, xStep, yStep, stream) {
1566
+ this.doc = doc;
1567
+ this.bBox = bBox;
1568
+ this.xStep = xStep;
1569
+ this.yStep = yStep;
1570
+ this.stream = stream;
1571
+ }
1572
+
1573
+ createPattern() {
1574
+ // no resources needed for our current usage
1575
+ // required entry
1576
+ var resources = this.doc.ref();
1577
+ resources.end(); // apply default transform matrix (flipped in the default doc._ctm)
1578
+ // see document.js & gradient.js
1579
+
1580
+ var [m0, m1, m2, m3, m4, m5] = this.doc._ctm;
1581
+ var [m11, m12, m21, m22, dx, dy] = [1, 0, 0, 1, 0, 0];
1582
+ var 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];
1583
+ var pattern = this.doc.ref({
1584
+ Type: 'Pattern',
1585
+ PatternType: 1,
1586
+ // tiling
1587
+ PaintType: 2,
1588
+ // 1-colored, 2-uncolored
1589
+ TilingType: 2,
1590
+ // 2-no distortion
1591
+ BBox: this.bBox,
1592
+ XStep: this.xStep,
1593
+ YStep: this.yStep,
1594
+ Matrix: m.map(v => +v.toFixed(5)),
1595
+ Resources: resources
1596
+ });
1597
+ pattern.end(this.stream);
1598
+ return pattern;
1599
+ }
1600
+
1601
+ embedPatternColorSpaces() {
1602
+ // map each pattern to an underlying color space
1603
+ // and embed on each page
1604
+ underlyingColorSpaces.forEach(csName => {
1605
+ var csId = this.getPatternColorSpaceId(csName);
1606
+ if (this.doc.page.colorSpaces[csId]) return;
1607
+ var cs = this.doc.ref(['Pattern', csName]);
1608
+ cs.end();
1609
+ this.doc.page.colorSpaces[csId] = cs;
1610
+ });
1611
+ }
1612
+
1613
+ getPatternColorSpaceId(underlyingColorspace) {
1614
+ return "CsP".concat(underlyingColorspace);
1615
+ }
1616
+
1617
+ embed() {
1618
+ if (!this.id) {
1619
+ this.doc._patternCount = this.doc._patternCount + 1;
1620
+ this.id = 'P' + this.doc._patternCount;
1621
+ this.pattern = this.createPattern();
1622
+ } // patterns are embedded in each page
1623
+
1624
+
1625
+ if (!this.doc.page.patterns[this.id]) {
1626
+ this.doc.page.patterns[this.id] = this.pattern;
1627
+ }
1628
+ }
1629
+
1630
+ apply(stroke, patternColor) {
1631
+ // do any embedding/creating that might be needed
1632
+ this.embedPatternColorSpaces();
1633
+ this.embed();
1634
+
1635
+ var normalizedColor = this.doc._normalizeColor(patternColor);
1636
+
1637
+ if (!normalizedColor) throw Error("invalid pattern color. (value: ".concat(patternColor, ")")); // select one of the pattern color spaces
1638
+
1639
+ var csId = this.getPatternColorSpaceId(this.doc._getColorSpace(normalizedColor));
1640
+
1641
+ this.doc._setColorSpace(csId, stroke); // stroke/fill using the pattern and color (in the above underlying color space)
1642
+
1643
+
1644
+ var op = stroke ? 'SCN' : 'scn';
1645
+ return this.doc.addContent("".concat(normalizedColor.join(' '), " /").concat(this.id, " ").concat(op));
1646
+ }
1647
+
1648
+ }
1649
+
1650
+ var pattern = {
1651
+ PDFTilingPattern
1652
+ };
1653
+
1555
1654
  var {
1556
1655
  PDFGradient: PDFGradient$1,
1557
1656
  PDFLinearGradient: PDFLinearGradient$1,
1558
1657
  PDFRadialGradient: PDFRadialGradient$1
1559
1658
  } = Gradient;
1659
+ var {
1660
+ PDFTilingPattern: PDFTilingPattern$1
1661
+ } = pattern;
1560
1662
  var ColorMixin = {
1561
1663
  initColor() {
1562
1664
  // The opacity dictionaries
1563
1665
  this._opacityRegistry = {};
1564
1666
  this._opacityCount = 0;
1667
+ this._patternCount = 0;
1565
1668
  return this._gradCount = 0;
1566
1669
  },
1567
1670
 
1568
1671
  _normalizeColor(color) {
1569
- if (color instanceof PDFGradient$1) {
1570
- return color;
1571
- }
1572
-
1573
1672
  if (typeof color === 'string') {
1574
1673
  if (color.charAt(0) === '#') {
1575
1674
  if (color.length === 4) {
@@ -1598,6 +1697,19 @@ var ColorMixin = {
1598
1697
  },
1599
1698
 
1600
1699
  _setColor(color, stroke) {
1700
+ if (color instanceof PDFGradient$1) {
1701
+ color.apply(stroke);
1702
+ return true; // see if tiling pattern, decode & apply it it
1703
+ } else if (Array.isArray(color) && color[0] instanceof PDFTilingPattern$1) {
1704
+ color[0].apply(stroke, color[1]);
1705
+ return true;
1706
+ } // any other case should be a normal color and not a pattern
1707
+
1708
+
1709
+ return this._setColorCore(color, stroke);
1710
+ },
1711
+
1712
+ _setColorCore(color, stroke) {
1601
1713
  color = this._normalizeColor(color);
1602
1714
 
1603
1715
  if (!color) {
@@ -1606,19 +1718,12 @@ var ColorMixin = {
1606
1718
 
1607
1719
  var op = stroke ? 'SCN' : 'scn';
1608
1720
 
1609
- if (color instanceof PDFGradient$1) {
1610
- this._setColorSpace('Pattern', stroke);
1721
+ var space = this._getColorSpace(color);
1611
1722
 
1612
- color.apply(op);
1613
- } else {
1614
- var space = color.length === 4 ? 'DeviceCMYK' : 'DeviceRGB';
1615
-
1616
- this._setColorSpace(space, stroke);
1617
-
1618
- color = color.join(' ');
1619
- this.addContent("".concat(color, " ").concat(op));
1620
- }
1723
+ this._setColorSpace(space, stroke);
1621
1724
 
1725
+ color = color.join(' ');
1726
+ this.addContent("".concat(color, " ").concat(op));
1622
1727
  return true;
1623
1728
  },
1624
1729
 
@@ -1627,6 +1732,10 @@ var ColorMixin = {
1627
1732
  return this.addContent("/".concat(space, " ").concat(op));
1628
1733
  },
1629
1734
 
1735
+ _getColorSpace(color) {
1736
+ return color.length === 4 ? 'DeviceCMYK' : 'DeviceRGB';
1737
+ },
1738
+
1630
1739
  fillColor(color, opacity) {
1631
1740
  var set = this._setColor(color, false);
1632
1741
 
@@ -1717,6 +1826,10 @@ var ColorMixin = {
1717
1826
 
1718
1827
  radialGradient(x1, y1, r1, x2, y2, r2) {
1719
1828
  return new PDFRadialGradient$1(this, x1, y1, r1, x2, y2, r2);
1829
+ },
1830
+
1831
+ pattern(bbox, xStep, yStep, stream) {
1832
+ return new PDFTilingPattern$1(this, bbox, xStep, yStep, stream);
1720
1833
  }
1721
1834
 
1722
1835
  };
@@ -4878,29 +4991,18 @@ var OutlineMixin = {
4878
4991
 
4879
4992
  };
4880
4993
 
4881
- function _defineProperty(obj, key, value) {
4882
- if (key in obj) {
4883
- Object.defineProperty(obj, key, {
4884
- value: value,
4885
- enumerable: true,
4886
- configurable: true,
4887
- writable: true
4888
- });
4889
- } else {
4890
- obj[key] = value;
4891
- }
4892
-
4893
- return obj;
4894
- }
4895
-
4896
4994
  function ownKeys(object, enumerableOnly) {
4897
4995
  var keys = Object.keys(object);
4898
4996
 
4899
4997
  if (Object.getOwnPropertySymbols) {
4900
4998
  var symbols = Object.getOwnPropertySymbols(object);
4901
- if (enumerableOnly) symbols = symbols.filter(function (sym) {
4902
- return Object.getOwnPropertyDescriptor(object, sym).enumerable;
4903
- });
4999
+
5000
+ if (enumerableOnly) {
5001
+ symbols = symbols.filter(function (sym) {
5002
+ return Object.getOwnPropertyDescriptor(object, sym).enumerable;
5003
+ });
5004
+ }
5005
+
4904
5006
  keys.push.apply(keys, symbols);
4905
5007
  }
4906
5008
 
@@ -4927,6 +5029,21 @@ function _objectSpread2(target) {
4927
5029
  return target;
4928
5030
  }
4929
5031
 
5032
+ function _defineProperty(obj, key, value) {
5033
+ if (key in obj) {
5034
+ Object.defineProperty(obj, key, {
5035
+ value: value,
5036
+ enumerable: true,
5037
+ configurable: true,
5038
+ writable: true
5039
+ });
5040
+ } else {
5041
+ obj[key] = value;
5042
+ }
5043
+
5044
+ return obj;
5045
+ }
5046
+
4930
5047
  /*
4931
5048
  PDFStructureContent - a reference to a marked structure content
4932
5049
  By Ben Schmidt
@@ -5033,7 +5150,7 @@ class PDFStructureElement {
5033
5150
  }
5034
5151
 
5035
5152
  _addContentToParentTree(content) {
5036
- content.refs.forEach((_ref) => {
5153
+ content.refs.forEach(_ref => {
5037
5154
  var {
5038
5155
  pageRef,
5039
5156
  mcid
@@ -5141,7 +5258,7 @@ class PDFStructureElement {
5141
5258
  }
5142
5259
 
5143
5260
  if (child instanceof PDFStructureContent) {
5144
- child.refs.forEach((_ref2) => {
5261
+ child.refs.forEach(_ref2 => {
5145
5262
  var {
5146
5263
  pageRef,
5147
5264
  mcid