@cantoo/fontkit 2.0.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.
Files changed (163) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +274 -0
  3. package/dist/browser-module.mjs +13660 -0
  4. package/dist/browser-module.mjs.map +1 -0
  5. package/dist/browser.cjs +13689 -0
  6. package/dist/browser.cjs.map +1 -0
  7. package/dist/main.cjs +13712 -0
  8. package/dist/main.cjs.map +1 -0
  9. package/dist/module.mjs +13673 -0
  10. package/dist/module.mjs.map +1 -0
  11. package/package.json +99 -0
  12. package/src/CmapProcessor.js +285 -0
  13. package/src/DFont.js +113 -0
  14. package/src/TTFFont.js +621 -0
  15. package/src/TrueTypeCollection.js +65 -0
  16. package/src/WOFF2Font.js +222 -0
  17. package/src/WOFFFont.js +56 -0
  18. package/src/aat/AATFeatureMap.js +545 -0
  19. package/src/aat/AATLayoutEngine.js +48 -0
  20. package/src/aat/AATLookupTable.js +125 -0
  21. package/src/aat/AATMorxProcessor.js +429 -0
  22. package/src/aat/AATStateMachine.js +96 -0
  23. package/src/base.js +29 -0
  24. package/src/cff/CFFCharsets.js +99 -0
  25. package/src/cff/CFFDict.js +166 -0
  26. package/src/cff/CFFEncodings.js +48 -0
  27. package/src/cff/CFFFont.js +158 -0
  28. package/src/cff/CFFIndex.js +150 -0
  29. package/src/cff/CFFOperand.js +135 -0
  30. package/src/cff/CFFPointer.js +50 -0
  31. package/src/cff/CFFPrivateDict.js +39 -0
  32. package/src/cff/CFFStandardStrings.js +71 -0
  33. package/src/cff/CFFTop.js +236 -0
  34. package/src/decorators.js +36 -0
  35. package/src/encodings.js +219 -0
  36. package/src/fs.js +12 -0
  37. package/src/glyph/BBox.js +72 -0
  38. package/src/glyph/CBDTGlyph.js +9 -0
  39. package/src/glyph/CFFGlyph.js +635 -0
  40. package/src/glyph/COLRGlyph.js +108 -0
  41. package/src/glyph/Glyph.js +270 -0
  42. package/src/glyph/GlyphVariationProcessor.js +487 -0
  43. package/src/glyph/Path.js +257 -0
  44. package/src/glyph/SBIXGlyph.js +54 -0
  45. package/src/glyph/StandardNames.js +27 -0
  46. package/src/glyph/TTFGlyph.js +408 -0
  47. package/src/glyph/TTFGlyphEncoder.js +158 -0
  48. package/src/glyph/WOFF2Glyph.js +70 -0
  49. package/src/index.js +15 -0
  50. package/src/layout/GlyphPosition.js +30 -0
  51. package/src/layout/GlyphRun.js +108 -0
  52. package/src/layout/KernProcessor.js +97 -0
  53. package/src/layout/LayoutEngine.js +190 -0
  54. package/src/layout/Script.js +231 -0
  55. package/src/layout/UnicodeLayoutEngine.js +250 -0
  56. package/src/node.js +16 -0
  57. package/src/opentype/GPOSProcessor.js +311 -0
  58. package/src/opentype/GSUBProcessor.js +208 -0
  59. package/src/opentype/GlyphInfo.js +57 -0
  60. package/src/opentype/GlyphIterator.js +82 -0
  61. package/src/opentype/OTLayoutEngine.js +117 -0
  62. package/src/opentype/OTProcessor.js +472 -0
  63. package/src/opentype/ShapingPlan.js +121 -0
  64. package/src/opentype/shapers/ArabicShaper.js +124 -0
  65. package/src/opentype/shapers/DefaultShaper.js +116 -0
  66. package/src/opentype/shapers/HangulShaper.js +285 -0
  67. package/src/opentype/shapers/HebrewShaper.js +105 -0
  68. package/src/opentype/shapers/IndicShaper.js +914 -0
  69. package/src/opentype/shapers/ThaiShaper.js +238 -0
  70. package/src/opentype/shapers/UniversalShaper.js +186 -0
  71. package/src/opentype/shapers/data.trie +0 -0
  72. package/src/opentype/shapers/gen-indic.js +208 -0
  73. package/src/opentype/shapers/gen-use.js +287 -0
  74. package/src/opentype/shapers/generate-data.js +33 -0
  75. package/src/opentype/shapers/index.js +109 -0
  76. package/src/opentype/shapers/indic-data.js +175 -0
  77. package/src/opentype/shapers/indic.json +1 -0
  78. package/src/opentype/shapers/indic.machine +31 -0
  79. package/src/opentype/shapers/indic.trie +0 -0
  80. package/src/opentype/shapers/use.json +1 -0
  81. package/src/opentype/shapers/use.machine +44 -0
  82. package/src/opentype/shapers/use.trie +0 -0
  83. package/src/packages/unicode-properties/LICENSE +8 -0
  84. package/src/packages/unicode-properties/README.md +12 -0
  85. package/src/packages/unicode-properties/data-trie.js +1 -0
  86. package/src/packages/unicode-properties/data.js +1 -0
  87. package/src/packages/unicode-properties/data.json +1 -0
  88. package/src/packages/unicode-properties/data.trie +0 -0
  89. package/src/packages/unicode-properties/dist/index.cjs +257 -0
  90. package/src/packages/unicode-properties/dist/index.cjs.map +7 -0
  91. package/src/packages/unicode-properties/dist/index.mjs +234 -0
  92. package/src/packages/unicode-properties/dist/index.mjs.map +7 -0
  93. package/src/packages/unicode-properties/generate.js +107 -0
  94. package/src/packages/unicode-properties/index.js +184 -0
  95. package/src/packages/unicode-properties/package.json +50 -0
  96. package/src/packages/unicode-trie/LICENSE +8 -0
  97. package/src/packages/unicode-trie/README.md +13 -0
  98. package/src/packages/unicode-trie/builder.js +965 -0
  99. package/src/packages/unicode-trie/dist/builder.cjs +711 -0
  100. package/src/packages/unicode-trie/dist/builder.cjs.map +7 -0
  101. package/src/packages/unicode-trie/dist/builder.mjs +690 -0
  102. package/src/packages/unicode-trie/dist/builder.mjs.map +7 -0
  103. package/src/packages/unicode-trie/dist/index.cjs +112 -0
  104. package/src/packages/unicode-trie/dist/index.cjs.map +7 -0
  105. package/src/packages/unicode-trie/dist/index.mjs +91 -0
  106. package/src/packages/unicode-trie/dist/index.mjs.map +7 -0
  107. package/src/packages/unicode-trie/index.js +135 -0
  108. package/src/packages/unicode-trie/package.json +49 -0
  109. package/src/packages/unicode-trie/swap.js +21 -0
  110. package/src/subset/CFFSubset.js +176 -0
  111. package/src/subset/Subset.js +63 -0
  112. package/src/subset/TTFSubset.js +130 -0
  113. package/src/tables/BASE.js +78 -0
  114. package/src/tables/COLR.js +25 -0
  115. package/src/tables/CPAL.js +24 -0
  116. package/src/tables/DSIG.js +21 -0
  117. package/src/tables/EBDT.js +91 -0
  118. package/src/tables/EBLC.js +80 -0
  119. package/src/tables/GDEF.js +57 -0
  120. package/src/tables/GPOS.js +209 -0
  121. package/src/tables/GSUB.js +85 -0
  122. package/src/tables/HVAR.js +44 -0
  123. package/src/tables/JSTF.js +43 -0
  124. package/src/tables/LTSH.js +10 -0
  125. package/src/tables/OS2.js +84 -0
  126. package/src/tables/PCLT.js +21 -0
  127. package/src/tables/STAT.js +99 -0
  128. package/src/tables/VDMX.js +33 -0
  129. package/src/tables/VORG.js +14 -0
  130. package/src/tables/WOFF2Directory.js +73 -0
  131. package/src/tables/WOFFDirectory.js +37 -0
  132. package/src/tables/aat.js +157 -0
  133. package/src/tables/avar.js +19 -0
  134. package/src/tables/bsln.js +31 -0
  135. package/src/tables/cmap.js +127 -0
  136. package/src/tables/cvt.js +6 -0
  137. package/src/tables/directory.js +58 -0
  138. package/src/tables/feat.js +28 -0
  139. package/src/tables/fpgm.js +8 -0
  140. package/src/tables/fvar.js +31 -0
  141. package/src/tables/gasp.js +15 -0
  142. package/src/tables/glyf.js +4 -0
  143. package/src/tables/gvar.js +27 -0
  144. package/src/tables/hdmx.js +15 -0
  145. package/src/tables/head.js +25 -0
  146. package/src/tables/hhea.js +19 -0
  147. package/src/tables/hmtx.js +11 -0
  148. package/src/tables/index.js +119 -0
  149. package/src/tables/just.js +81 -0
  150. package/src/tables/kern.js +91 -0
  151. package/src/tables/loca.js +30 -0
  152. package/src/tables/maxp.js +20 -0
  153. package/src/tables/morx.js +79 -0
  154. package/src/tables/name.js +157 -0
  155. package/src/tables/opbd.js +15 -0
  156. package/src/tables/opentype.js +217 -0
  157. package/src/tables/post.js +34 -0
  158. package/src/tables/prep.js +6 -0
  159. package/src/tables/sbix.js +17 -0
  160. package/src/tables/variations.js +80 -0
  161. package/src/tables/vhea.js +19 -0
  162. package/src/tables/vmtx.js +12 -0
  163. package/src/utils.js +62 -0
@@ -0,0 +1,257 @@
1
+ import BBox from './BBox';
2
+
3
+ const SVG_COMMANDS = {
4
+ moveTo: 'M',
5
+ lineTo: 'L',
6
+ quadraticCurveTo: 'Q',
7
+ bezierCurveTo: 'C',
8
+ closePath: 'Z'
9
+ };
10
+
11
+ /**
12
+ * Path objects are returned by glyphs and represent the actual
13
+ * vector outlines for each glyph in the font. Paths can be converted
14
+ * to SVG path data strings, or to functions that can be applied to
15
+ * render the path to a graphics context.
16
+ */
17
+ export default class Path {
18
+ constructor() {
19
+ this.commands = [];
20
+ this._bbox = null;
21
+ this._cbox = null;
22
+ }
23
+
24
+ /**
25
+ * Compiles the path to a JavaScript function that can be applied with
26
+ * a graphics context in order to render the path.
27
+ * @return {string}
28
+ */
29
+ toFunction() {
30
+ return ctx => {
31
+ this.commands.forEach(c => {
32
+ return ctx[c.command].apply(ctx, c.args)
33
+ })
34
+ };
35
+ }
36
+
37
+ /**
38
+ * Converts the path to an SVG path data string
39
+ * @return {string}
40
+ */
41
+ toSVG() {
42
+ let cmds = this.commands.map(c => {
43
+ let args = c.args.map(arg => Math.round(arg * 100) / 100);
44
+ return `${SVG_COMMANDS[c.command]}${args.join(' ')}`;
45
+ });
46
+
47
+ return cmds.join('');
48
+ }
49
+
50
+ /**
51
+ * Gets the "control box" of a path.
52
+ * This is like the bounding box, but it includes all points including
53
+ * control points of bezier segments and is much faster to compute than
54
+ * the real bounding box.
55
+ * @type {BBox}
56
+ */
57
+ get cbox() {
58
+ if (!this._cbox) {
59
+ if (this.commands.length === 0) {
60
+ this._cbox = Object.freeze(new BBox(0, 0, 0, 0));
61
+ } else {
62
+ let cbox = new BBox;
63
+ for (let command of this.commands) {
64
+ for (let i = 0; i < command.args.length; i += 2) {
65
+ cbox.addPoint(command.args[i], command.args[i + 1]);
66
+ }
67
+ }
68
+
69
+ this._cbox = Object.freeze(cbox);
70
+ }
71
+ }
72
+
73
+ return this._cbox;
74
+ }
75
+
76
+ /**
77
+ * Gets the exact bounding box of the path by evaluating curve segments.
78
+ * Slower to compute than the control box, but more accurate.
79
+ * @type {BBox}
80
+ */
81
+ get bbox() {
82
+ if (this._bbox) {
83
+ return this._bbox;
84
+ }
85
+
86
+ if (this.commands.length === 0) {
87
+ return this._bbox = Object.freeze(new BBox(0, 0, 0, 0));
88
+ }
89
+
90
+ let bbox = new BBox;
91
+ let cx = 0, cy = 0;
92
+
93
+ for (let c of this.commands) {
94
+ switch (c.command) {
95
+ case 'moveTo':
96
+ case 'lineTo': {
97
+ let [x, y] = c.args;
98
+ bbox.addPoint(x, y);
99
+ cx = x;
100
+ cy = y;
101
+ break;
102
+ }
103
+
104
+ case 'quadraticCurveTo':
105
+ case 'bezierCurveTo': {
106
+ let cp1x, cp1y, cp2x, cp2y, p3x, p3y;
107
+ if (c.command === 'quadraticCurveTo') {
108
+ // http://fontforge.org/bezier.html
109
+ let [qp1x, qp1y, qp3x, qp3y] = c.args;
110
+ p3x = qp3x;
111
+ p3y = qp3y;
112
+ cp1x = cx + 2 / 3 * (qp1x - cx); // CP1 = QP0 + 2/3 * (QP1-QP0)
113
+ cp1y = cy + 2 / 3 * (qp1y - cy);
114
+ cp2x = p3x + 2 / 3 * (qp1x - p3x); // CP2 = QP2 + 2/3 * (QP1-QP2)
115
+ cp2y = p3y + 2 / 3 * (qp1y - p3y);
116
+ } else {
117
+ [cp1x, cp1y, cp2x, cp2y, p3x, p3y] = c.args;
118
+ }
119
+
120
+ // http://blog.hackers-cafe.net/2009/06/how-to-calculate-bezier-curves-bounding.html
121
+ bbox.addPoint(p3x, p3y);
122
+
123
+ let p0 = [cx, cy];
124
+ let p1 = [cp1x, cp1y];
125
+ let p2 = [cp2x, cp2y];
126
+ let p3 = [p3x, p3y];
127
+
128
+ let f = (t, i) => (
129
+ Math.pow(1 - t, 3) * p0[i]
130
+ + 3 * Math.pow(1 - t, 2) * t * p1[i]
131
+ + 3 * (1 - t) * Math.pow(t, 2) * p2[i]
132
+ + Math.pow(t, 3) * p3[i]
133
+ );
134
+
135
+ for (let i = 0; i <= 1; i++) {
136
+ let b = 6 * p0[i] - 12 * p1[i] + 6 * p2[i];
137
+ let a = -3 * p0[i] + 9 * p1[i] - 9 * p2[i] + 3 * p3[i];
138
+ c = 3 * p1[i] - 3 * p0[i];
139
+
140
+ if (a === 0) {
141
+ if (b === 0) {
142
+ continue;
143
+ }
144
+
145
+ let t = -c / b;
146
+ if (0 < t && t < 1) {
147
+ if (i === 0) {
148
+ bbox.addPoint(f(t, i), bbox.maxY);
149
+ } else if (i === 1) {
150
+ bbox.addPoint(bbox.maxX, f(t, i));
151
+ }
152
+ }
153
+
154
+ continue;
155
+ }
156
+
157
+ let b2ac = Math.pow(b, 2) - 4 * c * a;
158
+ if (b2ac < 0) {
159
+ continue;
160
+ }
161
+
162
+ let t1 = (-b + Math.sqrt(b2ac)) / (2 * a);
163
+ if (0 < t1 && t1 < 1) {
164
+ if (i === 0) {
165
+ bbox.addPoint(f(t1, i), bbox.maxY);
166
+ } else if (i === 1) {
167
+ bbox.addPoint(bbox.maxX, f(t1, i));
168
+ }
169
+ }
170
+
171
+ let t2 = (-b - Math.sqrt(b2ac)) / (2 * a);
172
+ if (0 < t2 && t2 < 1) {
173
+ if (i === 0) {
174
+ bbox.addPoint(f(t2, i), bbox.maxY);
175
+ } else if (i === 1) {
176
+ bbox.addPoint(bbox.maxX, f(t2, i));
177
+ }
178
+ }
179
+ }
180
+
181
+ cx = p3x;
182
+ cy = p3y;
183
+ break;
184
+ }
185
+ }
186
+ }
187
+
188
+ return this._bbox = Object.freeze(bbox);
189
+ }
190
+
191
+ /**
192
+ * Applies a mapping function to each point in the path.
193
+ * @param {function} fn
194
+ * @return {Path}
195
+ */
196
+ mapPoints(fn) {
197
+ let path = new Path;
198
+
199
+ for (let c of this.commands) {
200
+ let args = [];
201
+ for (let i = 0; i < c.args.length; i += 2) {
202
+ let [x, y] = fn(c.args[i], c.args[i + 1]);
203
+ args.push(x, y);
204
+ }
205
+
206
+ path[c.command](...args);
207
+ }
208
+
209
+ return path;
210
+ }
211
+
212
+ /**
213
+ * Transforms the path by the given matrix.
214
+ */
215
+ transform(m0, m1, m2, m3, m4, m5) {
216
+ return this.mapPoints((x, y) => {
217
+ const tx = m0 * x + m2 * y + m4;
218
+ const ty = m1 * x + m3 * y + m5;
219
+ return [tx, ty];
220
+ });
221
+ }
222
+
223
+ /**
224
+ * Translates the path by the given offset.
225
+ */
226
+ translate(x, y) {
227
+ return this.transform(1, 0, 0, 1, x, y);
228
+ }
229
+
230
+ /**
231
+ * Rotates the path by the given angle (in radians).
232
+ */
233
+ rotate(angle) {
234
+ let cos = Math.cos(angle);
235
+ let sin = Math.sin(angle);
236
+ return this.transform(cos, sin, -sin, cos, 0, 0);
237
+ }
238
+
239
+ /**
240
+ * Scales the path.
241
+ */
242
+ scale(scaleX, scaleY = scaleX) {
243
+ return this.transform(scaleX, 0, 0, scaleY, 0, 0);
244
+ }
245
+ }
246
+
247
+ for (let command of ['moveTo', 'lineTo', 'quadraticCurveTo', 'bezierCurveTo', 'closePath']) {
248
+ Path.prototype[command] = function(...args) {
249
+ this._bbox = this._cbox = null;
250
+ this.commands.push({
251
+ command,
252
+ args
253
+ });
254
+
255
+ return this;
256
+ };
257
+ }
@@ -0,0 +1,54 @@
1
+ import TTFGlyph from './TTFGlyph';
2
+ import * as r from 'restructure';
3
+
4
+ let SBIXImage = new r.Struct({
5
+ originX: r.uint16,
6
+ originY: r.uint16,
7
+ type: new r.String(4),
8
+ data: new r.Buffer(t => t.parent.buflen - t._currentOffset)
9
+ });
10
+
11
+ /**
12
+ * Represents a color (e.g. emoji) glyph in Apple's SBIX format.
13
+ */
14
+ export default class SBIXGlyph extends TTFGlyph {
15
+ type = 'SBIX';
16
+
17
+ /**
18
+ * Returns an object representing a glyph image at the given point size.
19
+ * The object has a data property with a Buffer containing the actual image data,
20
+ * along with the image type, and origin.
21
+ *
22
+ * @param {number} size
23
+ * @return {object}
24
+ */
25
+ getImageForSize(size) {
26
+ for (let i = 0; i < this._font.sbix.imageTables.length; i++) {
27
+ var table = this._font.sbix.imageTables[i];
28
+ if (table.ppem >= size) { break; }
29
+ }
30
+
31
+ let offsets = table.imageOffsets;
32
+ let start = offsets[this.id];
33
+ let end = offsets[this.id + 1];
34
+
35
+ if (start === end) {
36
+ return null;
37
+ }
38
+
39
+ this._font.stream.pos = start;
40
+ return SBIXImage.decode(this._font.stream, {buflen: end - start});
41
+ }
42
+
43
+ render(ctx, size) {
44
+ let img = this.getImageForSize(size);
45
+ if (img != null) {
46
+ let scale = size / this._font.unitsPerEm;
47
+ ctx.image(img.data, {height: size, x: img.originX, y: (this.bbox.minY - img.originY) * scale});
48
+ }
49
+
50
+ if (this._font.sbix.flags.renderOutlines) {
51
+ super.render(ctx, size);
52
+ }
53
+ }
54
+ }
@@ -0,0 +1,27 @@
1
+ export default [
2
+ '.notdef', '.null', 'nonmarkingreturn', 'space', 'exclam', 'quotedbl', 'numbersign', 'dollar', 'percent',
3
+ 'ampersand', 'quotesingle', 'parenleft', 'parenright', 'asterisk', 'plus', 'comma', 'hyphen', 'period', 'slash',
4
+ 'zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'colon', 'semicolon', 'less',
5
+ 'equal', 'greater', 'question', 'at', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
6
+ 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'bracketleft', 'backslash', 'bracketright',
7
+ 'asciicircum', 'underscore', 'grave', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
8
+ 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'braceleft', 'bar', 'braceright', 'asciitilde',
9
+ 'Adieresis', 'Aring', 'Ccedilla', 'Eacute', 'Ntilde', 'Odieresis', 'Udieresis', 'aacute', 'agrave',
10
+ 'acircumflex', 'adieresis', 'atilde', 'aring', 'ccedilla', 'eacute', 'egrave', 'ecircumflex', 'edieresis',
11
+ 'iacute', 'igrave', 'icircumflex', 'idieresis', 'ntilde', 'oacute', 'ograve', 'ocircumflex', 'odieresis',
12
+ 'otilde', 'uacute', 'ugrave', 'ucircumflex', 'udieresis', 'dagger', 'degree', 'cent', 'sterling', 'section',
13
+ 'bullet', 'paragraph', 'germandbls', 'registered', 'copyright', 'trademark', 'acute', 'dieresis', 'notequal',
14
+ 'AE', 'Oslash', 'infinity', 'plusminus', 'lessequal', 'greaterequal', 'yen', 'mu', 'partialdiff', 'summation',
15
+ 'product', 'pi', 'integral', 'ordfeminine', 'ordmasculine', 'Omega', 'ae', 'oslash', 'questiondown',
16
+ 'exclamdown', 'logicalnot', 'radical', 'florin', 'approxequal', 'Delta', 'guillemotleft', 'guillemotright',
17
+ 'ellipsis', 'nonbreakingspace', 'Agrave', 'Atilde', 'Otilde', 'OE', 'oe', 'endash', 'emdash', 'quotedblleft',
18
+ 'quotedblright', 'quoteleft', 'quoteright', 'divide', 'lozenge', 'ydieresis', 'Ydieresis', 'fraction',
19
+ 'currency', 'guilsinglleft', 'guilsinglright', 'fi', 'fl', 'daggerdbl', 'periodcentered', 'quotesinglbase',
20
+ 'quotedblbase', 'perthousand', 'Acircumflex', 'Ecircumflex', 'Aacute', 'Edieresis', 'Egrave', 'Iacute',
21
+ 'Icircumflex', 'Idieresis', 'Igrave', 'Oacute', 'Ocircumflex', 'apple', 'Ograve', 'Uacute', 'Ucircumflex',
22
+ 'Ugrave', 'dotlessi', 'circumflex', 'tilde', 'macron', 'breve', 'dotaccent', 'ring', 'cedilla', 'hungarumlaut',
23
+ 'ogonek', 'caron', 'Lslash', 'lslash', 'Scaron', 'scaron', 'Zcaron', 'zcaron', 'brokenbar', 'Eth', 'eth',
24
+ 'Yacute', 'yacute', 'Thorn', 'thorn', 'minus', 'multiply', 'onesuperior', 'twosuperior', 'threesuperior',
25
+ 'onehalf', 'onequarter', 'threequarters', 'franc', 'Gbreve', 'gbreve', 'Idotaccent', 'Scedilla', 'scedilla',
26
+ 'Cacute', 'cacute', 'Ccaron', 'ccaron', 'dcroat'
27
+ ];