@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,108 @@
1
+ import Glyph from './Glyph';
2
+ import BBox from './BBox';
3
+
4
+ class COLRLayer {
5
+ constructor(glyph, color) {
6
+ this.glyph = glyph;
7
+ this.color = color;
8
+ }
9
+ }
10
+
11
+ /**
12
+ * Represents a color (e.g. emoji) glyph in Microsoft's COLR format.
13
+ * Each glyph in this format contain a list of colored layers, each
14
+ * of which is another vector glyph.
15
+ */
16
+ export default class COLRGlyph extends Glyph {
17
+ type = 'COLR';
18
+
19
+ _getBBox() {
20
+ let layers = this.layers;
21
+ if (!layers) {
22
+ let g = this._font._getBaseGlyph(this.id);
23
+ return g ? g.bbox : new BBox(0, 0, 0, 0);
24
+ }
25
+
26
+ let bbox = new BBox;
27
+ for (let i = 0; i < layers.length; i++) {
28
+ let layer = layers[i];
29
+ let b = layer.glyph.bbox;
30
+ bbox.addPoint(b.minX, b.minY);
31
+ bbox.addPoint(b.maxX, b.maxY);
32
+ }
33
+
34
+ return bbox;
35
+ }
36
+
37
+ /**
38
+ * Returns an array of objects containing the glyph and color for
39
+ * each layer in the composite color glyph.
40
+ * @type {object[]}
41
+ */
42
+ get layers() {
43
+ let cpal = this._font.CPAL;
44
+ let colr = this._font.COLR;
45
+
46
+ // COLR v1 uses paint-based records instead of v0 baseGlyphRecord
47
+ if (!colr || !colr.baseGlyphRecord) {
48
+ return null;
49
+ }
50
+
51
+ let low = 0;
52
+ let high = colr.baseGlyphRecord.length - 1;
53
+ let baseLayer;
54
+
55
+ while (low <= high) {
56
+ let mid = (low + high) >> 1;
57
+ let rec = colr.baseGlyphRecord[mid];
58
+
59
+ if (this.id < rec.gid) {
60
+ high = mid - 1;
61
+ } else if (this.id > rec.gid) {
62
+ low = mid + 1;
63
+ } else {
64
+ baseLayer = rec;
65
+ break;
66
+ }
67
+ }
68
+
69
+ // if base glyph not found in COLR table,
70
+ // default to normal glyph from glyf or CFF
71
+ if (baseLayer == null) {
72
+ let g = this._font._getBaseGlyph(this.id);
73
+ let color = {
74
+ red: 0,
75
+ green: 0,
76
+ blue: 0,
77
+ alpha: 255
78
+ };
79
+
80
+ return [new COLRLayer(g, color)];
81
+ }
82
+
83
+ // otherwise, return an array of all the layers
84
+ let layers = [];
85
+ for (let i = baseLayer.firstLayerIndex; i < baseLayer.firstLayerIndex + baseLayer.numLayers; i++) {
86
+ let rec = colr.layerRecords[i];
87
+ let color = cpal.colorRecords[rec.paletteIndex];
88
+ let g = this._font._getBaseGlyph(rec.gid);
89
+ layers.push(new COLRLayer(g, color));
90
+ }
91
+
92
+ return layers;
93
+ }
94
+
95
+ render(ctx, size) {
96
+ let layers = this.layers;
97
+ if (!layers) {
98
+ return;
99
+ }
100
+
101
+ for (let {glyph, color} of layers) {
102
+ ctx.fillColor([color.red, color.green, color.blue], color.alpha / 255 * 100);
103
+ glyph.render(ctx, size);
104
+ }
105
+
106
+ return;
107
+ }
108
+ }
@@ -0,0 +1,270 @@
1
+ import { cache } from '../decorators';
2
+ import Path from './Path';
3
+ import {isMark} from '../packages/unicode-properties/index.js';
4
+ import StandardNames from './StandardNames';
5
+
6
+ /**
7
+ * Glyph objects represent a glyph in the font. They have various properties for accessing metrics and
8
+ * the actual vector path the glyph represents, and methods for rendering the glyph to a graphics context.
9
+ *
10
+ * You do not create glyph objects directly. They are created by various methods on the font object.
11
+ * There are several subclasses of the base Glyph class internally that may be returned depending
12
+ * on the font format, but they all inherit from this class.
13
+ */
14
+ export default class Glyph {
15
+ constructor(id, codePoints, font) {
16
+ /**
17
+ * The glyph id in the font
18
+ * @type {number}
19
+ */
20
+ this.id = id;
21
+
22
+ /**
23
+ * An array of unicode code points that are represented by this glyph.
24
+ * There can be multiple code points in the case of ligatures and other glyphs
25
+ * that represent multiple visual characters.
26
+ * @type {number[]}
27
+ */
28
+ this.codePoints = codePoints;
29
+ this._font = font;
30
+
31
+ // TODO: get this info from GDEF if available
32
+ this.isMark = this.codePoints.length > 0 && this.codePoints.every(isMark);
33
+ this.isLigature = this.codePoints.length > 1;
34
+ }
35
+
36
+ _getPath() {
37
+ return new Path();
38
+ }
39
+
40
+ _getCBox() {
41
+ return this.path.cbox;
42
+ }
43
+
44
+ _getBBox() {
45
+ return this.path.bbox;
46
+ }
47
+
48
+ _getTableMetrics(table) {
49
+ if (this.id < table.metrics.length) {
50
+ return table.metrics.get(this.id);
51
+ }
52
+
53
+ let metric = table.metrics.get(table.metrics.length - 1);
54
+ let res = {
55
+ advance: metric ? metric.advance : 0,
56
+ bearing: table.bearings.get(this.id - table.metrics.length) || 0
57
+ };
58
+
59
+ return res;
60
+ }
61
+
62
+ _getMetrics(cbox) {
63
+ if (this._metrics) { return this._metrics; }
64
+ if (cbox == null) { ({ cbox } = this); }
65
+
66
+ let {advance: advanceWidth, bearing: leftBearing} = this._getTableMetrics(this._font.hmtx);
67
+
68
+ // Vertical metrics: vmtx, else font ascent/descent (respects useTypoMetrics)
69
+ let advanceHeight, topBearing;
70
+ if (this._font.vmtx) {
71
+ ({advance: advanceHeight, bearing: topBearing} = this._getTableMetrics(this._font.vmtx));
72
+ } else {
73
+ advanceHeight = Math.abs(this._font.ascent - this._font.descent);
74
+ topBearing = this._font.ascent - cbox.maxY;
75
+ }
76
+
77
+ if (this._font._variationProcessor && this._font.HVAR) {
78
+ advanceWidth += this._font._variationProcessor.getAdvanceAdjustment(this.id, this._font.HVAR);
79
+ }
80
+
81
+ let {width, height} = cbox;
82
+ return this._metrics = {
83
+ width,
84
+ height,
85
+ advanceWidth,
86
+ advanceHeight,
87
+ leftBearing,
88
+ topBearing,
89
+ rightBearing: advanceWidth - leftBearing - width,
90
+ bottomBearing: advanceHeight - topBearing - height
91
+ };
92
+ }
93
+
94
+ /**
95
+ * The glyph’s control box.
96
+ * This is often the same as the bounding box, but is faster to compute.
97
+ * Because of the way bezier curves are defined, some of the control points
98
+ * can be outside of the bounding box. Where `bbox` takes this into account,
99
+ * `cbox` does not. Thus, cbox is less accurate, but faster to compute.
100
+ * See [here](http://www.freetype.org/freetype2/docs/glyphs/glyphs-6.html#section-2)
101
+ * for a more detailed description.
102
+ *
103
+ * @type {BBox}
104
+ */
105
+ @cache
106
+ get cbox() {
107
+ return this._getCBox();
108
+ }
109
+
110
+ /**
111
+ * The glyph’s bounding box, i.e. the rectangle that encloses the
112
+ * glyph outline as tightly as possible.
113
+ * @type {BBox}
114
+ */
115
+ @cache
116
+ get bbox() {
117
+ return this._getBBox();
118
+ }
119
+
120
+ /**
121
+ * A vector Path object representing the glyph outline.
122
+ * @type {Path}
123
+ */
124
+ @cache
125
+ get path() {
126
+ // Cache the path so we only decode it once
127
+ // Decoding is actually performed by subclasses
128
+ return this._getPath();
129
+ }
130
+
131
+ /**
132
+ * Returns a path scaled to the given font size.
133
+ * @param {number} size
134
+ * @return {Path}
135
+ */
136
+ getScaledPath(size) {
137
+ let scale = 1 / this._font.unitsPerEm * size;
138
+ return this.path.scale(scale);
139
+ }
140
+
141
+ /**
142
+ * The glyph's width.
143
+ * @type {number}
144
+ */
145
+ @cache
146
+ get width() {
147
+ return this._getMetrics().width;
148
+ }
149
+
150
+ /**
151
+ * The glyph's height.
152
+ * @type {number}
153
+ */
154
+ @cache
155
+ get height() {
156
+ return this._getMetrics().height;
157
+ }
158
+
159
+ /**
160
+ * The glyph's advance width.
161
+ * @type {number}
162
+ */
163
+ @cache
164
+ get advanceWidth() {
165
+ return this._getMetrics().advanceWidth;
166
+ }
167
+
168
+ /**
169
+ * The glyph's advance height.
170
+ * @type {number}
171
+ */
172
+ @cache
173
+ get advanceHeight() {
174
+ return this._getMetrics().advanceHeight;
175
+ }
176
+
177
+ /**
178
+ * The glyph's left side bearing.
179
+ * @type {number}
180
+ */
181
+ @cache
182
+ get leftBearing() {
183
+ return this._getMetrics().leftBearing;
184
+ }
185
+
186
+ /**
187
+ * The glyph's top side bearing.
188
+ * @type {number}
189
+ */
190
+ @cache
191
+ get topBearing() {
192
+ return this._getMetrics().topBearing;
193
+ }
194
+
195
+ /**
196
+ * The glyph's right side bearing.
197
+ * @type {number}
198
+ */
199
+ @cache
200
+ get rightBearing() {
201
+ return this._getMetrics().rightBearing;
202
+ }
203
+
204
+ /**
205
+ * The glyph's bottom side bearing.
206
+ * @type {number}
207
+ */
208
+ @cache
209
+ get bottomBearing() {
210
+ return this._getMetrics().bottomBearing;
211
+ }
212
+
213
+ get ligatureCaretPositions() {
214
+ return null;
215
+ }
216
+
217
+ _getName() {
218
+ let { post } = this._font;
219
+ if (!post) {
220
+ return null;
221
+ }
222
+
223
+ switch (post.version) {
224
+ case 1:
225
+ return StandardNames[this.id];
226
+
227
+ case 2: {
228
+ let id = post.glyphNameIndex[this.id];
229
+ if (id < StandardNames.length) {
230
+ return StandardNames[id];
231
+ }
232
+
233
+ return post.names[id - StandardNames.length];
234
+ }
235
+
236
+ case 2.5:
237
+ return StandardNames[this.id + post.offsets[this.id]];
238
+
239
+ case 4:
240
+ return String.fromCharCode(post.map[this.id]);
241
+ }
242
+ }
243
+
244
+ /**
245
+ * The glyph's name
246
+ * @type {string}
247
+ */
248
+ @cache
249
+ get name() {
250
+ return this._getName();
251
+ }
252
+
253
+ /**
254
+ * Renders the glyph to the given graphics context, at the specified font size.
255
+ * @param {CanvasRenderingContext2d} ctx
256
+ * @param {number} size
257
+ */
258
+ render(ctx, size) {
259
+ ctx.save();
260
+
261
+ let scale = 1 / this._font.head.unitsPerEm * size;
262
+ ctx.scale(scale, scale);
263
+
264
+ let fn = this.path.toFunction();
265
+ fn(ctx);
266
+ ctx.fill();
267
+
268
+ ctx.restore();
269
+ }
270
+ }