imagescript 3.11.0 → 3.12.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/README.md +1 -1
- package/dist/codecs/avif.js +2 -2
- package/dist/image.d.ts +14 -5
- package/dist/image.js +6 -5
- package/dist/render/font.js +118 -13
- package/dist/render/hinting.js +4 -0
- package/dist/render/layout.js +42 -20
- package/dist/render/svg-text.js +61 -6
- package/dist/render/svg.js +7 -3
- package/dist/render/text.js +20 -6
- package/dist/render/variations.js +310 -0
- package/dist/util/icc.js +37 -1
- package/dist/version.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -56,7 +56,7 @@ and encoded as GIF, APNG or WebP, all at once or
|
|
|
56
56
|
P3, Adobe RGB, ...) are converted to sRGB when decoding, so images look the way browsers show them.
|
|
57
57
|
|
|
58
58
|
- [Text rendering](https://imagescript.matmen.dev/classes/Image.html#rendertext) with TrueType and OpenType fonts:
|
|
59
|
-
all scripts, color emoji
|
|
59
|
+
all scripts, color emoji, variable fonts
|
|
60
60
|
- [SVG rendering](https://imagescript.matmen.dev/classes/Image.html#rendersvg), including text (with fonts you pass
|
|
61
61
|
or fonts embedded in the SVG)
|
|
62
62
|
- [Resizing](https://imagescript.matmen.dev/classes/Image.html#resize),
|
package/dist/codecs/avif.js
CHANGED
|
@@ -540,7 +540,7 @@ async function decodeImage(d, it, items, idat, alphaItem, alphaOf, maxPixels, to
|
|
|
540
540
|
else if (box.type === 'imir')
|
|
541
541
|
pixels = mirror(pixels, width, height, d[box.start] & 1);
|
|
542
542
|
}
|
|
543
|
-
return { width, height, pixels, icc };
|
|
543
|
+
return { width, height, pixels, icc, cicp: icc ? undefined : [color.primaries, color.transfer] };
|
|
544
544
|
}
|
|
545
545
|
function iccOf(d, it) {
|
|
546
546
|
for (const { box } of it.props) {
|
|
@@ -662,5 +662,5 @@ async function decodeSequence(d, moov, maxPixels, toneMapping) {
|
|
|
662
662
|
return fromFrame(decodeAV1(data, 0, data.length, { maxPixels }));
|
|
663
663
|
};
|
|
664
664
|
const color = frame(colorTrack);
|
|
665
|
-
return { width: color.width, height: color.height, pixels: await toRgba(color, alphaTrack ? frame(alphaTrack) : null, toneMapping) };
|
|
665
|
+
return { width: color.width, height: color.height, pixels: await toRgba(color, alphaTrack ? frame(alphaTrack) : null, toneMapping), cicp: [color.primaries, color.transfer] };
|
|
666
666
|
}
|
package/dist/image.d.ts
CHANGED
|
@@ -148,11 +148,11 @@ export interface DecodeOptions {
|
|
|
148
148
|
/** Whether to rotate/mirror JPEGs, TIFFs, PNGs and WebPs as their orientation tag (Exif or XMP) says. Default: `true` */
|
|
149
149
|
applyOrientation?: boolean;
|
|
150
150
|
/**
|
|
151
|
-
* Whether to convert the colors of images with an embedded ICC profile (PNG, JPEG, WebP, AVIF, TIFF)
|
|
152
|
-
*
|
|
153
|
-
*
|
|
154
|
-
*
|
|
155
|
-
* Default: `true`
|
|
151
|
+
* Whether to convert the colors of images with an embedded ICC profile (PNG, JPEG, WebP, AVIF, JPEG XL, TIFF), and
|
|
152
|
+
* of AVIFs with other color primaries than sRGB's (Display P3, BT.2020), to sRGB, like browsers show them: wide
|
|
153
|
+
* gamut photos (Display P3 from phones, Adobe RGB, ProPhoto) look as intended instead of washed out. Matrix/TRC
|
|
154
|
+
* profiles (RGB and gray) are applied, relative colorimetric (colors outside sRGB are clipped); profiles with only
|
|
155
|
+
* lookup tables, CMYK ones and ones that are sRGB anyway leave the pixels as they are. Default: `true`
|
|
156
156
|
*/
|
|
157
157
|
applyColorProfile?: boolean;
|
|
158
158
|
/**
|
|
@@ -238,6 +238,15 @@ export interface TextStyle {
|
|
|
238
238
|
* `kern`), a number picks an alternate (`salt: 2`). Fonts without a feature ignore it. Default: `{}`
|
|
239
239
|
*/
|
|
240
240
|
features?: Record<string, boolean | number>;
|
|
241
|
+
/**
|
|
242
|
+
* The instance of variable fonts, by their axes: `weight` (1-1000, like CSS's `font-weight`: 400 regular, 700
|
|
243
|
+
* bold), `width` (percent of the normal width: 75 condensed, 125 expanded), `slant` (degrees, negative leans
|
|
244
|
+
* right), `italic` (`true`/`false`), `opticalSize` (the size the glyphs are designed for, in pixels; default: the
|
|
245
|
+
* font size, like browsers' `font-optical-sizing: auto`), and other axes by their 4-character tag (like Roboto
|
|
246
|
+
* Flex's `GRAD: 50`). Values outside an axis's range are clamped, axes a font doesn't have are ignored, fonts
|
|
247
|
+
* without axes aren't affected. Default: `{}` (the fonts' default instances)
|
|
248
|
+
*/
|
|
249
|
+
variations?: Record<string, number | boolean>;
|
|
241
250
|
}
|
|
242
251
|
/** Options of {@link Image.cropCircle} */
|
|
243
252
|
export interface CropCircleOptions {
|
package/dist/image.js
CHANGED
|
@@ -22,7 +22,7 @@ import * as quantize from "./codecs/quantize.js";
|
|
|
22
22
|
import * as textRenderer from "./render/text.js";
|
|
23
23
|
import * as svgRenderer from "./render/svg.js";
|
|
24
24
|
import { view } from "./util/mem.js";
|
|
25
|
-
import { profileTransform } from "./util/icc.js";
|
|
25
|
+
import { cicpTransform, profileTransform } from "./util/icc.js";
|
|
26
26
|
import { ImageType } from "./image-type.js";
|
|
27
27
|
import { TextLayout } from "./text-layout.js";
|
|
28
28
|
import { resolveMetadata, pngText, exifData } from "./metadata.js";
|
|
@@ -1114,8 +1114,8 @@ export class Image {
|
|
|
1114
1114
|
return tga.decode(data, maxPixels);
|
|
1115
1115
|
throw new Error('Unsupported image type');
|
|
1116
1116
|
});
|
|
1117
|
-
if (applyColorProfile
|
|
1118
|
-
profileTransform(decoded.icc)?.(decoded.pixels);
|
|
1117
|
+
if (applyColorProfile)
|
|
1118
|
+
(decoded.icc ? profileTransform(decoded.icc) : decoded.cicp ? cicpTransform(...decoded.cicp) : null)?.(decoded.pixels);
|
|
1119
1119
|
const image = new Image(decoded.width, decoded.height);
|
|
1120
1120
|
image.bitmap = decoded.pixels;
|
|
1121
1121
|
return applyOrientation && decoded.orientation ? image.__orient__(decoded.orientation) : image;
|
|
@@ -1208,7 +1208,8 @@ export class Image {
|
|
|
1208
1208
|
}
|
|
1209
1209
|
static async renderText(font, size, text, colorOrOptions = 0xffffffff, layoutArgument, styleArgument = {}) {
|
|
1210
1210
|
const options = isOptions(colorOrOptions) ? colorOrOptions : { ...styleArgument, color: colorOrOptions, layout: layoutArgument };
|
|
1211
|
-
const { color = 0xffffffff, layout = new TextLayout(), strokeWidth = 0, strokeColor = 0x000000ff, hinting = true, features = {} } = options;
|
|
1211
|
+
const { color = 0xffffffff, layout = new TextLayout(), strokeWidth = 0, strokeColor = 0x000000ff, hinting = true, features = {}, variations = {} } = options;
|
|
1212
|
+
const axes = textRenderer.axisValues(variations);
|
|
1212
1213
|
size = finite(size, 'font size');
|
|
1213
1214
|
if (finite(strokeWidth, 'stroke width') < 0)
|
|
1214
1215
|
throw new RangeError('Invalid stroke width');
|
|
@@ -1216,7 +1217,7 @@ export class Image {
|
|
|
1216
1217
|
if (!fonts.length)
|
|
1217
1218
|
throw new RangeError('No font given');
|
|
1218
1219
|
const [r, g, b, a] = Image.colorToRGBA(color);
|
|
1219
|
-
const rendered = await textRenderer.render(fonts.map(view), size, text, layout, hinting, (r << 24 | g << 16 | b << 8 | 255) >>> 0, featureList(features));
|
|
1220
|
+
const rendered = await textRenderer.render(fonts.map(view), size, text, layout, hinting, (r << 24 | g << 16 | b << 8 | 255) >>> 0, featureList(features), axes);
|
|
1220
1221
|
const pad = Math.ceil(strokeWidth);
|
|
1221
1222
|
const width = rendered.width + 2 * pad, height = rendered.height + 2 * pad;
|
|
1222
1223
|
const coverage = new Uint8Array(width * height);
|
package/dist/render/font.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Gvar, ItemVariationStore, cvarDeltas, deltaSetIndexMap, normalize, parseAvar, parseFvar } from "./variations.js";
|
|
1
2
|
const MAX_COMMANDS = 100000;
|
|
2
3
|
export class Font {
|
|
3
4
|
data;
|
|
@@ -16,6 +17,10 @@ export class Font {
|
|
|
16
17
|
outlineCache;
|
|
17
18
|
kernTable;
|
|
18
19
|
variation;
|
|
20
|
+
axes;
|
|
21
|
+
coords = [];
|
|
22
|
+
variationTables = null;
|
|
23
|
+
instances = null;
|
|
19
24
|
constructor(data) {
|
|
20
25
|
this.data = data;
|
|
21
26
|
this.view = new DataView(data.buffer, data.byteOffset, data.byteLength);
|
|
@@ -63,6 +68,62 @@ export class Font {
|
|
|
63
68
|
this.outlineCache = new Map();
|
|
64
69
|
this.kernTable = this.tables.kern && parseKern(this, this.tables.kern.offset) || null;
|
|
65
70
|
this.variation = parseVariations(this, this.tables.cmap.offset);
|
|
71
|
+
this.axes = this.tables.fvar ? parseFvar(this, this.tables.fvar.offset) : [];
|
|
72
|
+
}
|
|
73
|
+
get vary() {
|
|
74
|
+
if (this.variationTables)
|
|
75
|
+
return this.variationTables;
|
|
76
|
+
const t = this.tables, at = (tag) => t[tag].offset;
|
|
77
|
+
const hvar = t.HVAR ? (() => {
|
|
78
|
+
const o = at('HVAR'), map = this.u32(o + 8);
|
|
79
|
+
return { store: new ItemVariationStore(this, o + this.u32(o + 4)), advances: map ? deltaSetIndexMap(this, o + map) : null };
|
|
80
|
+
})() : null;
|
|
81
|
+
const mvar = t.MVAR && this.u16(at('MVAR') + 10) ? (() => {
|
|
82
|
+
const o = at('MVAR'), size = this.u16(o + 6), count = this.u16(o + 8), records = new Map();
|
|
83
|
+
for (let i = 0; i < count; i++) {
|
|
84
|
+
const r = o + 12 + i * size;
|
|
85
|
+
records.set(String.fromCharCode(...this.data.subarray(r, r + 4)), [this.u16(r + 4), this.u16(r + 6)]);
|
|
86
|
+
}
|
|
87
|
+
return { store: new ItemVariationStore(this, o + this.u16(o + 10)), records };
|
|
88
|
+
})() : null;
|
|
89
|
+
const gdef = t.GDEF && this.u16(at('GDEF') + 2) >= 3 && this.u32(at('GDEF') + 14) ? new ItemVariationStore(this, at('GDEF') + this.u32(at('GDEF') + 14)) : null;
|
|
90
|
+
const base = Object.getPrototypeOf(this) instanceof Font ? Object.getPrototypeOf(this) : this;
|
|
91
|
+
base.variationTables = {
|
|
92
|
+
avar: t.avar ? parseAvar(this, at('avar'), this.axes.length) : null,
|
|
93
|
+
gvar: t.gvar && this.outlines === 'glyf' ? new Gvar(this, at('gvar')) : null,
|
|
94
|
+
hvar, mvar, gdef,
|
|
95
|
+
};
|
|
96
|
+
return base.variationTables;
|
|
97
|
+
}
|
|
98
|
+
withVariations(values) {
|
|
99
|
+
if (!this.axes.length)
|
|
100
|
+
return this;
|
|
101
|
+
const coords = normalize(this.axes, this.vary.avar, values);
|
|
102
|
+
if (coords.every(c => c === 0))
|
|
103
|
+
return Object.getPrototypeOf(this) instanceof Font ? Object.getPrototypeOf(this) : this;
|
|
104
|
+
const base = Object.getPrototypeOf(this) instanceof Font ? Object.getPrototypeOf(this) : this;
|
|
105
|
+
const key = coords.join(',');
|
|
106
|
+
const cached = base.instances?.get(key);
|
|
107
|
+
if (cached)
|
|
108
|
+
return cached;
|
|
109
|
+
const instance = Object.create(base);
|
|
110
|
+
(base.instances ??= new Map()).set(key, instance);
|
|
111
|
+
instance.coords = coords;
|
|
112
|
+
instance.outlineCache = new Map();
|
|
113
|
+
const metric = (tag, value) => {
|
|
114
|
+
const item = this.vary.mvar?.records.get(tag);
|
|
115
|
+
return item ? value + this.vary.mvar.store.delta(coords, item[0], item[1]) : value;
|
|
116
|
+
};
|
|
117
|
+
instance.ascender = metric('hasc', base.ascender);
|
|
118
|
+
instance.descender = metric('hdsc', base.descender);
|
|
119
|
+
instance.lineGap = metric('hlgn', base.lineGap);
|
|
120
|
+
return instance;
|
|
121
|
+
}
|
|
122
|
+
variationDelta(outer, inner) {
|
|
123
|
+
return this.coords.length && this.vary.gdef ? this.vary.gdef.delta(this.coords, outer, inner) : 0;
|
|
124
|
+
}
|
|
125
|
+
cvtDeltas(count) {
|
|
126
|
+
return this.coords.length && this.tables.cvar ? cvarDeltas(this, this.tables.cvar.offset, this.axes.length, this.coords, count) : null;
|
|
66
127
|
}
|
|
67
128
|
u8(o) { return this.data[o]; }
|
|
68
129
|
i8(o) { return this.view.getInt8(o); }
|
|
@@ -76,14 +137,45 @@ export class Font {
|
|
|
76
137
|
advance(glyph) {
|
|
77
138
|
const hmtx = this.tables.hmtx.offset;
|
|
78
139
|
const index = Math.min(glyph, this.numberOfHMetrics - 1);
|
|
79
|
-
|
|
140
|
+
const advance = this.u16(hmtx + index * 4);
|
|
141
|
+
if (!this.coords.length)
|
|
142
|
+
return advance;
|
|
143
|
+
const hvar = this.vary.hvar;
|
|
144
|
+
if (hvar) {
|
|
145
|
+
const [outer, inner] = hvar.advances ? hvar.advances(glyph) : [0, glyph];
|
|
146
|
+
return advance + hvar.store.delta(this.coords, outer, inner);
|
|
147
|
+
}
|
|
148
|
+
const phantom = this.phantomDeltas(glyph);
|
|
149
|
+
return phantom ? advance + phantom[2] - phantom[0] : advance;
|
|
150
|
+
}
|
|
151
|
+
phantomDeltas(glyph) {
|
|
152
|
+
const varied = this.varied(glyph);
|
|
153
|
+
return varied && varied.deltas && varied.deltas.subarray(2 * varied.count);
|
|
154
|
+
}
|
|
155
|
+
varied(glyph) {
|
|
156
|
+
if (this.outlines !== 'glyf' || glyph >= this.numGlyphs)
|
|
157
|
+
return null;
|
|
158
|
+
const offset = this.glyfOffset(glyph);
|
|
159
|
+
const gvar = this.coords.length ? this.vary.gvar : null;
|
|
160
|
+
if (offset < 0)
|
|
161
|
+
return { count: 0, deltas: gvar ? gvar.deltas(glyph, this.coords, 4, null, [], []) : null, simple: null };
|
|
162
|
+
const contours = this.i16(offset);
|
|
163
|
+
if (contours < 0) {
|
|
164
|
+
const count = this.components(offset).components.length;
|
|
165
|
+
return { count, deltas: gvar ? gvar.deltas(glyph, this.coords, count + 4, null, [], []) : null, simple: null };
|
|
166
|
+
}
|
|
167
|
+
const simple = this.simplePoints(offset, contours);
|
|
168
|
+
if (!simple)
|
|
169
|
+
return null;
|
|
170
|
+
const count = simple.xs.length;
|
|
171
|
+
return { count, deltas: gvar ? gvar.deltas(glyph, this.coords, count + 4, simple.ends, simple.xs, simple.ys) : null, simple };
|
|
80
172
|
}
|
|
81
173
|
variationGlyph(codepoint, selector) {
|
|
82
174
|
const glyph = this.variation ? this.variation(codepoint, selector) : 0;
|
|
83
175
|
return glyph < 0 ? this.glyphIndex(codepoint) : glyph;
|
|
84
176
|
}
|
|
85
177
|
extents(glyph) {
|
|
86
|
-
if (this.outlines === 'glyf' && glyph < this.numGlyphs) {
|
|
178
|
+
if (this.outlines === 'glyf' && glyph < this.numGlyphs && !this.coords.length) {
|
|
87
179
|
const o = this.glyfOffset(glyph);
|
|
88
180
|
if (o < 0)
|
|
89
181
|
return { xMin: 0, yMin: 0, xMax: 0, yMax: 0 };
|
|
@@ -114,13 +206,16 @@ export class Font {
|
|
|
114
206
|
return null;
|
|
115
207
|
const contourCount = this.i16(offset);
|
|
116
208
|
const xMin = this.i16(offset + 2);
|
|
209
|
+
const varied = this.coords.length ? this.varied(glyph) : null, d = varied?.deltas;
|
|
210
|
+
const at = (i, axis) => d ? Math.round(d[2 * i + axis]) : 0;
|
|
211
|
+
const origin = d && varied ? at(varied.count, 0) : 0;
|
|
117
212
|
if (contourCount < 0) {
|
|
118
213
|
const { components, end } = this.components(offset);
|
|
119
214
|
const instructions = components[components.length - 1].flags & 0x100 ? this.data.subarray(end + 2, end + 2 + this.u16(end)) : new Uint8Array(0);
|
|
120
215
|
return {
|
|
121
|
-
xs: [], ys: [], on: [], ends: [], instructions, xMin,
|
|
122
|
-
components: components.map(({ glyph, flags, arg1, arg2, matrix }) => ({
|
|
123
|
-
glyph, dx: flags & 2 ? arg1 : 0, dy: flags & 2 ? arg2 : 0, matrix,
|
|
216
|
+
xs: [], ys: [], on: [], ends: [], instructions, xMin: xMin + origin,
|
|
217
|
+
components: components.map(({ glyph, flags, arg1, arg2, matrix }, i) => ({
|
|
218
|
+
glyph, dx: flags & 2 ? arg1 + at(i, 0) : 0, dy: flags & 2 ? arg2 + at(i, 1) : 0, matrix,
|
|
124
219
|
round: (flags & 4) !== 0, match: flags & 2 ? null : [arg1, arg2], useMetrics: (flags & 0x200) !== 0,
|
|
125
220
|
})),
|
|
126
221
|
};
|
|
@@ -129,7 +224,10 @@ export class Font {
|
|
|
129
224
|
if (!points)
|
|
130
225
|
return null;
|
|
131
226
|
const { xs, ys, flags, ends, instructions } = points;
|
|
132
|
-
return {
|
|
227
|
+
return {
|
|
228
|
+
xs: Array.from(xs, (x, i) => x + at(i, 0)), ys: Array.from(ys, (y, i) => y + at(i, 1)), on: Array.from(flags, f => (f & 1) === 1), ends,
|
|
229
|
+
instructions, xMin: xMin + origin, components: null,
|
|
230
|
+
};
|
|
133
231
|
}
|
|
134
232
|
glyfOffset(glyph) {
|
|
135
233
|
const loca = this.tables.loca.offset;
|
|
@@ -231,31 +329,38 @@ export class Font {
|
|
|
231
329
|
return [];
|
|
232
330
|
const contours = this.i16(offset);
|
|
233
331
|
if (contours >= 0) {
|
|
234
|
-
const path = this.simpleGlyph(offset, contours);
|
|
332
|
+
const path = this.simpleGlyph(offset, contours, glyph);
|
|
235
333
|
expanding.budget -= path.length;
|
|
236
334
|
return expanding.budget >= 0 ? path : [];
|
|
237
335
|
}
|
|
238
336
|
expanding.expanding.add(glyph);
|
|
239
|
-
const path = this.compositeGlyph(offset, expanding);
|
|
337
|
+
const path = this.compositeGlyph(offset, expanding, glyph);
|
|
240
338
|
expanding.expanding.delete(glyph);
|
|
241
339
|
return path;
|
|
242
340
|
}
|
|
243
|
-
simpleGlyph(offset, contourCount) {
|
|
341
|
+
simpleGlyph(offset, contourCount, glyph) {
|
|
244
342
|
const points = this.simplePoints(offset, contourCount);
|
|
245
343
|
if (!points)
|
|
246
344
|
return [];
|
|
345
|
+
let xs = points.xs, ys = points.ys;
|
|
346
|
+
const d = this.coords.length ? this.varied(glyph)?.deltas : null;
|
|
347
|
+
if (d) {
|
|
348
|
+
xs = Float64Array.from(points.xs, (x, i) => x + d[2 * i]);
|
|
349
|
+
ys = Float64Array.from(points.ys, (y, i) => y + d[2 * i + 1]);
|
|
350
|
+
}
|
|
247
351
|
const path = [];
|
|
248
352
|
let first = 0;
|
|
249
353
|
for (const last of points.ends) {
|
|
250
|
-
contourToPath(path,
|
|
354
|
+
contourToPath(path, xs, ys, points.flags, first, last);
|
|
251
355
|
first = last + 1;
|
|
252
356
|
}
|
|
253
357
|
return path;
|
|
254
358
|
}
|
|
255
|
-
compositeGlyph(offset, expanding) {
|
|
359
|
+
compositeGlyph(offset, expanding, composite) {
|
|
256
360
|
const path = [];
|
|
257
|
-
|
|
258
|
-
|
|
361
|
+
const deltas = this.coords.length ? this.varied(composite)?.deltas : null;
|
|
362
|
+
for (const [k, { glyph, flags, arg1, arg2, matrix: [a, b, c, d] }] of this.components(offset).components.entries()) {
|
|
363
|
+
const dx = flags & 2 ? arg1 + (deltas ? deltas[2 * k] : 0) : 0, dy = flags & 2 ? arg2 + (deltas ? deltas[2 * k + 1] : 0) : 0;
|
|
259
364
|
for (const command of this.glyfOutline(glyph, expanding)) {
|
|
260
365
|
const transformed = [command[0]];
|
|
261
366
|
for (let i = 1; i < command.length; i += 2) {
|
package/dist/render/hinting.js
CHANGED
|
@@ -111,6 +111,10 @@ export class Hinter {
|
|
|
111
111
|
this.cvt = new Int16Array(cvt.length >> 1);
|
|
112
112
|
for (let i = 0; i < this.cvt.length; i++)
|
|
113
113
|
this.cvt[i] = font.i16(font.tables['cvt '].offset + i * 2);
|
|
114
|
+
const deltas = font.cvtDeltas(this.cvt.length);
|
|
115
|
+
if (deltas)
|
|
116
|
+
for (let i = 0; i < this.cvt.length; i++)
|
|
117
|
+
this.cvt[i] = Math.round(this.cvt[i] + deltas[i]);
|
|
114
118
|
const maxp = font.tables.maxp.offset;
|
|
115
119
|
const version = font.u32(maxp);
|
|
116
120
|
this.maxTwilight = version >= 0x10000 ? font.u16(maxp + 16) : 0;
|
package/dist/render/layout.js
CHANGED
|
@@ -496,8 +496,10 @@ export class LayoutTable {
|
|
|
496
496
|
const condition = set + f.u32(set + 2 + j * 4);
|
|
497
497
|
if (f.u16(condition) !== 1)
|
|
498
498
|
matches = false;
|
|
499
|
-
else
|
|
500
|
-
|
|
499
|
+
else {
|
|
500
|
+
const v = f.coords[f.u16(condition + 2)] ?? 0;
|
|
501
|
+
matches = f.i16(condition + 4) / 16384 <= v && v <= f.i16(condition + 6) / 16384;
|
|
502
|
+
}
|
|
501
503
|
}
|
|
502
504
|
}
|
|
503
505
|
if (!matches)
|
|
@@ -886,7 +888,7 @@ export class LayoutTable {
|
|
|
886
888
|
return { coverage: has, apply: c => {
|
|
887
889
|
if (coverage(c.buffer.cur().codepoint) < 0)
|
|
888
890
|
return false;
|
|
889
|
-
applyValue(f, valueFormat, offset + 6, c.buffer.cur());
|
|
891
|
+
applyValue(f, valueFormat, offset + 6, c.buffer.cur(), offset);
|
|
890
892
|
c.buffer.idx++;
|
|
891
893
|
return true;
|
|
892
894
|
} };
|
|
@@ -896,7 +898,7 @@ export class LayoutTable {
|
|
|
896
898
|
const index = coverage(c.buffer.cur().codepoint);
|
|
897
899
|
if (index < 0 || index >= f.u16(offset + 6))
|
|
898
900
|
return false;
|
|
899
|
-
applyValue(f, valueFormat, offset + 8 + index * size, c.buffer.cur());
|
|
901
|
+
applyValue(f, valueFormat, offset + 8 + index * size, c.buffer.cur(), offset);
|
|
900
902
|
c.buffer.idx++;
|
|
901
903
|
return true;
|
|
902
904
|
} };
|
|
@@ -934,9 +936,9 @@ export class LayoutTable {
|
|
|
934
936
|
if (record < 0)
|
|
935
937
|
return false;
|
|
936
938
|
if (len1)
|
|
937
|
-
applyValue(f, format1, record + 2, buffer.cur());
|
|
939
|
+
applyValue(f, format1, record + 2, buffer.cur(), offset);
|
|
938
940
|
if (len2)
|
|
939
|
-
applyValue(f, format2, record + 2 + len1, buffer.info[pos]);
|
|
941
|
+
applyValue(f, format2, record + 2 + len1, buffer.info[pos], offset);
|
|
940
942
|
buffer.idx = len2 ? pos + 1 : pos;
|
|
941
943
|
return true;
|
|
942
944
|
} };
|
|
@@ -960,9 +962,9 @@ export class LayoutTable {
|
|
|
960
962
|
return false;
|
|
961
963
|
const v = records + (k1 * class2Count + k2) * (len1 + len2);
|
|
962
964
|
if (len1)
|
|
963
|
-
applyValue(f, format1, v, buffer.cur());
|
|
965
|
+
applyValue(f, format1, v, buffer.cur(), offset);
|
|
964
966
|
if (len2)
|
|
965
|
-
applyValue(f, format2, v + len1, buffer.info[pos]);
|
|
967
|
+
applyValue(f, format2, v + len1, buffer.info[pos], offset);
|
|
966
968
|
buffer.idx = len2 ? pos + 1 : pos;
|
|
967
969
|
return true;
|
|
968
970
|
} };
|
|
@@ -1156,20 +1158,40 @@ const valueSize = (format) => {
|
|
|
1156
1158
|
n += v & 1;
|
|
1157
1159
|
return n * 2;
|
|
1158
1160
|
};
|
|
1159
|
-
function
|
|
1160
|
-
if (
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1161
|
+
function deviceDelta(f, device) {
|
|
1162
|
+
if (!f.coords.length || f.u16(device + 4) !== 0x8000)
|
|
1163
|
+
return 0;
|
|
1164
|
+
return f.variationDelta(f.u16(device), f.u16(device + 2));
|
|
1165
|
+
}
|
|
1166
|
+
function applyValue(f, format, offset, info, base) {
|
|
1167
|
+
const fields = [0, 0, 0, 0];
|
|
1168
|
+
for (let k = 0; k < 4; k++)
|
|
1169
|
+
if (format & 1 << k) {
|
|
1170
|
+
fields[k] = f.i16(offset);
|
|
1171
|
+
offset += 2;
|
|
1172
|
+
}
|
|
1173
|
+
if (format & 0xf0 && f.coords.length)
|
|
1174
|
+
for (let k = 0; k < 4; k++)
|
|
1175
|
+
if (format & 0x10 << k) {
|
|
1176
|
+
const device = f.u16(offset);
|
|
1177
|
+
offset += 2;
|
|
1178
|
+
if (device)
|
|
1179
|
+
fields[k] += deviceDelta(f, base + device);
|
|
1180
|
+
}
|
|
1181
|
+
info.xOffset += fields[0];
|
|
1182
|
+
info.yOffset += fields[1];
|
|
1183
|
+
info.xAdvance += fields[2];
|
|
1170
1184
|
}
|
|
1171
1185
|
function anchor(f, offset) {
|
|
1172
|
-
|
|
1186
|
+
let x = f.i16(offset + 2), y = f.i16(offset + 4);
|
|
1187
|
+
if (f.u16(offset) === 3 && f.coords.length) {
|
|
1188
|
+
const dx = f.u16(offset + 6), dy = f.u16(offset + 8);
|
|
1189
|
+
if (dx)
|
|
1190
|
+
x += deviceDelta(f, offset + dx);
|
|
1191
|
+
if (dy)
|
|
1192
|
+
y += deviceDelta(f, offset + dy);
|
|
1193
|
+
}
|
|
1194
|
+
return [x, y];
|
|
1173
1195
|
}
|
|
1174
1196
|
function markAttach(f, c, markArray, markIndex, anchors, row, classCount, glyphPos) {
|
|
1175
1197
|
const buffer = c.buffer, info = buffer.info;
|
package/dist/render/svg-text.js
CHANGED
|
@@ -83,8 +83,18 @@ export function fontFace(data, descriptors) {
|
|
|
83
83
|
generic.add('serif');
|
|
84
84
|
else if ((familyType === 2 && serifStyle >= 11 && serifStyle <= 15) || familyClass === 8)
|
|
85
85
|
generic.add('sans-serif');
|
|
86
|
+
const axis = (tag) => font.axes.find(a => a.tag === tag);
|
|
87
|
+
const wght = axis('wght'), wdth = axis('wdth'), ital = axis('ital'), slnt = axis('slnt');
|
|
88
|
+
const classes = (min, max) => {
|
|
89
|
+
const lo = STRETCH_PERCENT.findIndex(p => p >= min), hi = STRETCH_PERCENT.filter(p => p <= max).length - 1;
|
|
90
|
+
return lo >= 0 && hi >= lo ? [lo + 1, hi + 1] : null;
|
|
91
|
+
};
|
|
92
|
+
const ranges = font.axes.length ? {
|
|
93
|
+
weight: wght ? [wght.min, wght.max] : null, stretch: wdth ? classes(wdth.min, wdth.max) : null,
|
|
94
|
+
italic: !!ital && ital.max >= 1, oblique: !!slnt && slnt.min < 0,
|
|
95
|
+
} : null;
|
|
86
96
|
const face = {
|
|
87
|
-
font, families: families.map(name => name.toLowerCase()), weight, style, stretch, generic,
|
|
97
|
+
font, families: families.map(name => name.toLowerCase()), weight, style, stretch, ranges, generic,
|
|
88
98
|
ascent, descent, xHeight,
|
|
89
99
|
underlinePosition: post >= 0 ? font.i16(post + 8) : -Math.trunc(units / 9),
|
|
90
100
|
underlineThickness,
|
|
@@ -96,8 +106,15 @@ export function fontFace(data, descriptors) {
|
|
|
96
106
|
faces.set(font, face);
|
|
97
107
|
return face;
|
|
98
108
|
}
|
|
109
|
+
const STRETCH_PERCENT = [50, 62.5, 75, 87.5, 100, 112.5, 125, 150, 200];
|
|
99
110
|
const GENERIC = new Set(['serif', 'sans-serif', 'monospace', 'cursive', 'fantasy']);
|
|
100
|
-
function bestMatch(
|
|
111
|
+
function bestMatch(faces, query) {
|
|
112
|
+
const clamp = (v, range, own) => range ? Math.min(Math.max(v, range[0]), range[1]) : own;
|
|
113
|
+
const candidates = faces.map(f => !f.ranges ? f : {
|
|
114
|
+
...f, face: f, weight: clamp(query.weight, f.ranges.weight, f.weight), stretch: clamp(query.stretch, f.ranges.stretch, f.stretch),
|
|
115
|
+
style: query.style === 'italic' && f.ranges.italic ? 'italic' : query.style === 'oblique' && f.ranges.oblique ? 'oblique' : f.style,
|
|
116
|
+
});
|
|
117
|
+
const original = (f) => f.face ?? f;
|
|
101
118
|
const pick = (values, wanted, below) => {
|
|
102
119
|
if (values.includes(wanted))
|
|
103
120
|
return wanted;
|
|
@@ -119,7 +136,32 @@ function bestMatch(candidates, query) {
|
|
|
119
136
|
}
|
|
120
137
|
else
|
|
121
138
|
weight = pick(weights, w, w < 400);
|
|
122
|
-
return list.find(f => f.weight === weight);
|
|
139
|
+
return original(list.find(f => f.weight === weight));
|
|
140
|
+
}
|
|
141
|
+
const variedFaces = new WeakMap();
|
|
142
|
+
export function variedFace(face, query, fontSize) {
|
|
143
|
+
if (!face.font.axes.length)
|
|
144
|
+
return face;
|
|
145
|
+
const values = { wght: query.weight };
|
|
146
|
+
if (query.stretch !== 5)
|
|
147
|
+
values.wdth = STRETCH_PERCENT[query.stretch - 1];
|
|
148
|
+
if (query.style === 'italic')
|
|
149
|
+
values.ital = 1;
|
|
150
|
+
if (query.style === 'oblique')
|
|
151
|
+
values.slnt = -12;
|
|
152
|
+
if (query.opticalSizing)
|
|
153
|
+
values.opsz = fontSize;
|
|
154
|
+
const font = face.font.withVariations({ ...values, ...query.variations });
|
|
155
|
+
if (font === face.font)
|
|
156
|
+
return face;
|
|
157
|
+
let byCoords = variedFaces.get(face);
|
|
158
|
+
if (!byCoords)
|
|
159
|
+
variedFaces.set(face, byCoords = new Map());
|
|
160
|
+
const key = font.coords.join(',');
|
|
161
|
+
let varied = byCoords.get(key);
|
|
162
|
+
if (!varied)
|
|
163
|
+
byCoords.set(key, varied = { ...face, font, ascent: face.ascent + font.ascender - face.font.ascender, descent: face.descent + font.descender - face.font.descender });
|
|
164
|
+
return varied;
|
|
123
165
|
}
|
|
124
166
|
export function matchFont(fonts, query) {
|
|
125
167
|
if (!fonts.length)
|
|
@@ -197,6 +239,17 @@ function colorKind(face, glyph, fontSize) {
|
|
|
197
239
|
return 'bitmap';
|
|
198
240
|
return null;
|
|
199
241
|
}
|
|
242
|
+
export function parseVariationSettings(value) {
|
|
243
|
+
const out = {};
|
|
244
|
+
if (value.trim().toLowerCase() === 'normal')
|
|
245
|
+
return out;
|
|
246
|
+
for (const part of value.split(',')) {
|
|
247
|
+
const m = /^\s*(["'])(.{4})\1\s+([-+]?(?:\d+\.?\d*|\.\d+)(?:e[-+]?\d+)?)\s*$/i.exec(part);
|
|
248
|
+
if (m)
|
|
249
|
+
out[m[2]] = Number(m[3]);
|
|
250
|
+
}
|
|
251
|
+
return out;
|
|
252
|
+
}
|
|
200
253
|
export function parseFontFamilies(value) {
|
|
201
254
|
const families = [];
|
|
202
255
|
for (const part of value.split(',')) {
|
|
@@ -247,8 +300,10 @@ function shapeChunk(chunk, fonts, primary) {
|
|
|
247
300
|
if (base) {
|
|
248
301
|
const grapheme = cps.slice(start, end);
|
|
249
302
|
let face = base;
|
|
250
|
-
if (!grapheme.every(c => isDefaultIgnorableCodepoint(c) || base.font.glyphIndex(c)))
|
|
251
|
-
|
|
303
|
+
if (!grapheme.every(c => isDefaultIgnorableCodepoint(c) || base.font.glyphIndex(c))) {
|
|
304
|
+
const other = fallbackFont(fonts, base, grapheme, new Set([base]));
|
|
305
|
+
face = other && span ? variedFace(other, span.font, span.fontSize) : base;
|
|
306
|
+
}
|
|
252
307
|
faceOf.fill(face, start, end);
|
|
253
308
|
if (face !== base)
|
|
254
309
|
fallback.fill(face, start, end);
|
|
@@ -748,7 +803,7 @@ export function layOutText(text, fonts) {
|
|
|
748
803
|
for (const span of chunk.spans) {
|
|
749
804
|
const face = matchFont(fonts, span.font);
|
|
750
805
|
if (face)
|
|
751
|
-
primary.set(span, face);
|
|
806
|
+
primary.set(span, variedFace(face, span.font, span.fontSize));
|
|
752
807
|
}
|
|
753
808
|
const spans = [];
|
|
754
809
|
let box = null;
|
package/dist/render/svg.js
CHANGED
|
@@ -15,7 +15,7 @@ import { base64 } from "../util/bytes.js";
|
|
|
15
15
|
import { gunzip } from "../codecs/zlib.js";
|
|
16
16
|
import { applyFilter } from "./filter.js";
|
|
17
17
|
import { BLEND_MODES, blendPixel } from "./blend.js";
|
|
18
|
-
import { fontFace, layOutText, parseFontFamilies, pathLength, placeOutline, colorTablesOf } from "./svg-text.js";
|
|
18
|
+
import { fontFace, layOutText, parseFontFamilies, parseVariationSettings, pathLength, placeOutline, colorTablesOf } from "./svg-text.js";
|
|
19
19
|
import { rasterizeGlyph } from "./text.js";
|
|
20
20
|
import { renderPaintGlyph } from "./colr.js";
|
|
21
21
|
import { decodeSync as decodePNG } from "../codecs/png.js";
|
|
@@ -26,7 +26,7 @@ const INHERITED = new Set(['fill', 'fill-opacity', 'fill-rule', 'stroke', 'strok
|
|
|
26
26
|
'stroke-miterlimit', 'stroke-dasharray', 'stroke-dashoffset', 'color', 'visibility', 'clip-rule', 'font-size', 'marker-start', 'marker-mid',
|
|
27
27
|
'marker-end', 'image-rendering', 'color-interpolation-filters', 'paint-order', 'shape-rendering',
|
|
28
28
|
'font-family', 'font-weight', 'font-style', 'font-stretch', 'font-variant', 'text-anchor', 'letter-spacing', 'word-spacing', 'dominant-baseline',
|
|
29
|
-
'writing-mode', 'kerning', 'font-kerning', 'text-rendering']);
|
|
29
|
+
'writing-mode', 'kerning', 'font-kerning', 'text-rendering', 'font-variation-settings', 'font-optical-sizing']);
|
|
30
30
|
const CSS_ONLY = new Set(['font-kerning']);
|
|
31
31
|
const SVG_ELEMENTS = new Set(['a', 'altGlyph', 'altGlyphDef', 'altGlyphItem', 'animate', 'animateColor', 'animateMotion', 'animateTransform', 'circle',
|
|
32
32
|
'clipPath', 'color-profile', 'cursor', 'defs', 'desc', 'ellipse', 'feBlend', 'feColorMatrix', 'feComponentTransfer', 'feComposite', 'feConvolveMatrix',
|
|
@@ -45,6 +45,7 @@ const DEFAULTS = {
|
|
|
45
45
|
'stop-color': 'black', 'stop-opacity': '1', 'marker-start': 'none', 'marker-mid': 'none', 'marker-end': 'none',
|
|
46
46
|
'image-rendering': 'auto', 'filter': 'none', 'flood-color': 'black', 'flood-opacity': '1', 'color-interpolation-filters': 'linearRGB',
|
|
47
47
|
'lighting-color': 'white', 'shape-rendering': 'auto', 'font-weight': '400', 'font-style': 'normal', 'font-stretch': 'normal',
|
|
48
|
+
'font-variation-settings': 'normal', 'font-optical-sizing': 'auto',
|
|
48
49
|
'font-variant': 'normal', 'text-anchor': 'start', 'letter-spacing': 'normal', 'word-spacing': 'normal', 'dominant-baseline': 'auto',
|
|
49
50
|
'writing-mode': 'lr-tb', 'text-rendering': 'auto'
|
|
50
51
|
};
|
|
@@ -1040,7 +1041,10 @@ class Document {
|
|
|
1040
1041
|
const fontStyle = style['font-style'] === 'italic' || style['font-style'] === 'oblique' ? style['font-style'] : 'normal';
|
|
1041
1042
|
return {
|
|
1042
1043
|
start, end, paint: style, visible: style.visibility === 'visible',
|
|
1043
|
-
font: {
|
|
1044
|
+
font: {
|
|
1045
|
+
families: parseFontFamilies(style['font-family'] ?? ''), weight: Number(style['font-weight']), style: fontStyle, stretch,
|
|
1046
|
+
variations: parseVariationSettings(style['font-variation-settings'] ?? ''), opticalSizing: style['font-optical-sizing'] !== 'none',
|
|
1047
|
+
},
|
|
1044
1048
|
fontSize, smallCaps: style['font-variant'] === 'small-caps',
|
|
1045
1049
|
kerning: !(style.kerning !== undefined && style.kerning !== 'auto' && parseLength(style.kerning, viewport, fontSize, 'x') === 0) && style['font-kerning'] !== 'none',
|
|
1046
1050
|
letterSpacing: spacing(style['letter-spacing']), wordSpacing: spacing(style['word-spacing']),
|
package/dist/render/text.js
CHANGED
|
@@ -30,8 +30,22 @@ export function loadFont(data) {
|
|
|
30
30
|
regions.set(key, font = new Font(data));
|
|
31
31
|
return font;
|
|
32
32
|
}
|
|
33
|
-
|
|
34
|
-
|
|
33
|
+
const AXES = { weight: 'wght', width: 'wdth', slant: 'slnt', italic: 'ital', opticalSize: 'opsz' };
|
|
34
|
+
export function axisValues(variations) {
|
|
35
|
+
const values = {};
|
|
36
|
+
for (const [key, value] of Object.entries(variations)) {
|
|
37
|
+
const tag = AXES[key] ?? (key.length === 4 ? key : null);
|
|
38
|
+
if (!tag)
|
|
39
|
+
throw new RangeError(`Unknown font variation ${key}: use weight, width, slant, italic, opticalSize or an axis tag`);
|
|
40
|
+
const v = typeof value === 'boolean' ? +value : value;
|
|
41
|
+
if (typeof v !== 'number' || !Number.isFinite(v))
|
|
42
|
+
throw new RangeError(`Invalid font variation ${key}: ${String(value)}`);
|
|
43
|
+
values[tag] = v;
|
|
44
|
+
}
|
|
45
|
+
return values;
|
|
46
|
+
}
|
|
47
|
+
function sizedFont(data, size, hinting, variations) {
|
|
48
|
+
const font = loadFont(data).withVariations({ opsz: size, ...variations });
|
|
35
49
|
let bySize = sizes.get(font);
|
|
36
50
|
if (!bySize)
|
|
37
51
|
sizes.set(font, bySize = new Map());
|
|
@@ -90,9 +104,9 @@ function glyphOf(f, index, size) {
|
|
|
90
104
|
f.glyphs.set(index, g);
|
|
91
105
|
return g;
|
|
92
106
|
}
|
|
93
|
-
export async function render(fonts, size, text, layout, hinting = false, foreground = 0xffffffff, features = []) {
|
|
107
|
+
export async function render(fonts, size, text, layout, hinting = false, foreground = 0xffffffff, features = [], variations = {}) {
|
|
94
108
|
try {
|
|
95
|
-
return await layOut(fonts, size, text, layout, hinting, foreground, features);
|
|
109
|
+
return await layOut(fonts, size, text, layout, hinting, foreground, features, variations);
|
|
96
110
|
}
|
|
97
111
|
catch (err) {
|
|
98
112
|
if (err instanceof RangeError && !(err instanceof TooLarge))
|
|
@@ -103,9 +117,9 @@ export async function render(fonts, size, text, layout, hinting = false, foregro
|
|
|
103
117
|
const MAX_PIXELS = 1 << 28;
|
|
104
118
|
class TooLarge extends RangeError {
|
|
105
119
|
}
|
|
106
|
-
async function layOut(fonts, size, text, layout, hinting, foreground, features) {
|
|
120
|
+
async function layOut(fonts, size, text, layout, hinting, foreground, features, variations) {
|
|
107
121
|
const sized = [];
|
|
108
|
-
const font = (i) => sized[i] ??= sizedFont(fonts[i], size, hinting);
|
|
122
|
+
const font = (i) => sized[i] ??= sizedFont(fonts[i], size, hinting, variations);
|
|
109
123
|
const primary = font(0);
|
|
110
124
|
const ascent = Math.ceil(primary.font.ascender * primary.scale);
|
|
111
125
|
const lineHeight = Math.max(1, Math.ceil((primary.font.ascender - primary.font.descender + primary.font.lineGap) * primary.scale));
|
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
const fixed = (r, o) => r.i32(o) / 65536;
|
|
2
|
+
const f2dot14 = (r, o) => r.i16(o) / 16384;
|
|
3
|
+
export function parseFvar(r, fvar) {
|
|
4
|
+
const axesOffset = r.u16(fvar + 4), count = r.u16(fvar + 8), size = r.u16(fvar + 10);
|
|
5
|
+
const axes = [];
|
|
6
|
+
for (let i = 0; i < count; i++) {
|
|
7
|
+
const o = fvar + axesOffset + i * size;
|
|
8
|
+
const tag = String.fromCharCode(r.u8(o), r.u8(o + 1), r.u8(o + 2), r.u8(o + 3));
|
|
9
|
+
const min = fixed(r, o + 4), def = fixed(r, o + 8), max = fixed(r, o + 12);
|
|
10
|
+
axes.push({ tag, min: Math.min(min, def), default: def, max: Math.max(max, def) });
|
|
11
|
+
}
|
|
12
|
+
return axes;
|
|
13
|
+
}
|
|
14
|
+
export function parseAvar(r, avar, axisCount) {
|
|
15
|
+
if (r.u16(avar) !== 1 || r.u16(avar + 6) !== axisCount)
|
|
16
|
+
return null;
|
|
17
|
+
const maps = [];
|
|
18
|
+
let o = avar + 8;
|
|
19
|
+
for (let a = 0; a < axisCount; a++) {
|
|
20
|
+
const count = r.u16(o), map = [];
|
|
21
|
+
for (let i = 0; i < count; i++)
|
|
22
|
+
map.push(f2dot14(r, o + 2 + i * 4), f2dot14(r, o + 4 + i * 4));
|
|
23
|
+
maps.push(map);
|
|
24
|
+
o += 2 + count * 4;
|
|
25
|
+
}
|
|
26
|
+
return maps;
|
|
27
|
+
}
|
|
28
|
+
export function normalize(axes, avar, values) {
|
|
29
|
+
return axes.map((axis, a) => {
|
|
30
|
+
const v = Math.min(Math.max(values[axis.tag] ?? axis.default, axis.min), axis.max);
|
|
31
|
+
let n = v < axis.default ? (v - axis.default) / (axis.default - axis.min) : v > axis.default ? (v - axis.default) / (axis.max - axis.default) : 0;
|
|
32
|
+
const map = avar?.[a];
|
|
33
|
+
if (map && map.length >= 6) {
|
|
34
|
+
for (let i = 2; i < map.length; i += 2) {
|
|
35
|
+
if (n > map[i])
|
|
36
|
+
continue;
|
|
37
|
+
const [x0, y0, x1, y1] = [map[i - 2], map[i - 1], map[i], map[i + 1]];
|
|
38
|
+
n = x1 === x0 ? y1 : y0 + (n - x0) * (y1 - y0) / (x1 - x0);
|
|
39
|
+
break;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return Math.round(n * 16384) / 16384;
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
export function scalar(coords, peak, start, end) {
|
|
46
|
+
let s = 1;
|
|
47
|
+
for (let a = 0; a < peak.length; a++) {
|
|
48
|
+
const p = peak[a];
|
|
49
|
+
if (p === 0)
|
|
50
|
+
continue;
|
|
51
|
+
const v = coords[a] ?? 0, lo = start ? start[a] : Math.min(p, 0), hi = end ? end[a] : Math.max(p, 0);
|
|
52
|
+
if (start && end && (lo > p || p > hi || lo < 0 && hi > 0))
|
|
53
|
+
continue;
|
|
54
|
+
if (v === p)
|
|
55
|
+
continue;
|
|
56
|
+
if (v <= lo || v >= hi)
|
|
57
|
+
return 0;
|
|
58
|
+
s *= v < p ? (v - lo) / (p - lo) : (hi - v) / (hi - p);
|
|
59
|
+
}
|
|
60
|
+
return s;
|
|
61
|
+
}
|
|
62
|
+
export class ItemVariationStore {
|
|
63
|
+
r;
|
|
64
|
+
regions = [];
|
|
65
|
+
data = [];
|
|
66
|
+
scalars = null;
|
|
67
|
+
scalarsFor = null;
|
|
68
|
+
constructor(r, store) {
|
|
69
|
+
this.r = r;
|
|
70
|
+
const regionList = store + r.u32(store + 2), count = r.u16(store + 6);
|
|
71
|
+
const axisCount = r.u16(regionList), regionCount = r.u16(regionList + 2);
|
|
72
|
+
for (let i = 0; i < regionCount; i++) {
|
|
73
|
+
const region = { peak: [], start: [], end: [] };
|
|
74
|
+
for (let a = 0; a < axisCount; a++) {
|
|
75
|
+
const o = regionList + 4 + (i * axisCount + a) * 6;
|
|
76
|
+
region.start.push(f2dot14(r, o));
|
|
77
|
+
region.peak.push(f2dot14(r, o + 2));
|
|
78
|
+
region.end.push(f2dot14(r, o + 4));
|
|
79
|
+
}
|
|
80
|
+
this.regions.push(region);
|
|
81
|
+
}
|
|
82
|
+
for (let i = 0; i < count; i++) {
|
|
83
|
+
const o = store + r.u32(store + 8 + i * 4);
|
|
84
|
+
const words = r.u16(o + 2), regionIndexCount = r.u16(o + 4), regions = [];
|
|
85
|
+
for (let k = 0; k < regionIndexCount; k++)
|
|
86
|
+
regions.push(r.u16(o + 6 + k * 2));
|
|
87
|
+
this.data.push({ itemCount: r.u16(o), wordCount: words & 0x7fff, long: (words & 0x8000) !== 0, regions, offset: o + 6 + regionIndexCount * 2 });
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
delta(coords, outer, inner) {
|
|
91
|
+
const d = this.data[outer];
|
|
92
|
+
if (!d || inner >= d.itemCount)
|
|
93
|
+
return 0;
|
|
94
|
+
if (this.scalarsFor !== coords) {
|
|
95
|
+
this.scalars = Float64Array.from(this.regions, region => scalar(coords, region.peak, region.start, region.end));
|
|
96
|
+
this.scalarsFor = coords;
|
|
97
|
+
}
|
|
98
|
+
const scalars = this.scalars, r = this.r;
|
|
99
|
+
const big = d.long ? 4 : 2, small = d.long ? 2 : 1;
|
|
100
|
+
let o = d.offset + inner * (d.wordCount * big + (d.regions.length - d.wordCount) * small), delta = 0;
|
|
101
|
+
for (let k = 0; k < d.regions.length; k++) {
|
|
102
|
+
const s = scalars[d.regions[k]] ?? 0;
|
|
103
|
+
let v;
|
|
104
|
+
if (k < d.wordCount) {
|
|
105
|
+
v = d.long ? r.i32(o) : r.i16(o);
|
|
106
|
+
o += big;
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
v = d.long ? r.i16(o) : r.i8(o);
|
|
110
|
+
o += small;
|
|
111
|
+
}
|
|
112
|
+
if (s)
|
|
113
|
+
delta += s * v;
|
|
114
|
+
}
|
|
115
|
+
return delta;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
export function deltaSetIndexMap(r, map) {
|
|
119
|
+
const format = r.u8(map), entryFormat = r.u8(map + 1);
|
|
120
|
+
const count = format === 0 ? r.u16(map + 2) : r.u32(map + 2), start = map + (format === 0 ? 4 : 6);
|
|
121
|
+
const size = (entryFormat >> 4 & 3) + 1, innerBits = (entryFormat & 15) + 1;
|
|
122
|
+
return index => {
|
|
123
|
+
if (!count)
|
|
124
|
+
return [0, index];
|
|
125
|
+
const i = Math.min(index, count - 1);
|
|
126
|
+
let v = 0;
|
|
127
|
+
for (let k = 0; k < size; k++)
|
|
128
|
+
v = v * 256 + r.u8(start + i * size + k);
|
|
129
|
+
return [Math.floor(v / 2 ** innerBits), v % 2 ** innerBits];
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
function readPoints(r, o) {
|
|
133
|
+
let count = r.u8(o++);
|
|
134
|
+
if (count & 0x80)
|
|
135
|
+
count = (count & 0x7f) << 8 | r.u8(o++);
|
|
136
|
+
if (!count)
|
|
137
|
+
return [null, o];
|
|
138
|
+
const points = [];
|
|
139
|
+
let point = 0;
|
|
140
|
+
while (points.length < count) {
|
|
141
|
+
const control = r.u8(o++), words = control & 0x80, run = (control & 0x7f) + 1;
|
|
142
|
+
for (let i = 0; i < run && points.length < count; i++) {
|
|
143
|
+
point += words ? r.u16(o) : r.u8(o);
|
|
144
|
+
o += words ? 2 : 1;
|
|
145
|
+
points.push(point);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
return [points, o];
|
|
149
|
+
}
|
|
150
|
+
function readDeltas(r, o, n) {
|
|
151
|
+
const deltas = new Int32Array(n);
|
|
152
|
+
for (let i = 0; i < n;) {
|
|
153
|
+
const control = r.u8(o++), run = (control & 0x3f) + 1;
|
|
154
|
+
for (let k = 0; k < run && i < n; k++, i++) {
|
|
155
|
+
if (control & 0x80)
|
|
156
|
+
deltas[i] = 0;
|
|
157
|
+
else if (control & 0x40) {
|
|
158
|
+
deltas[i] = r.i16(o);
|
|
159
|
+
o += 2;
|
|
160
|
+
}
|
|
161
|
+
else
|
|
162
|
+
deltas[i] = r.i8(o++);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
return [deltas, o];
|
|
166
|
+
}
|
|
167
|
+
function readTuples(r, base, header, axisCount, sharedTuples) {
|
|
168
|
+
const countField = r.u16(header), count = countField & 0xfff, dataOffset = r.u16(header + 2);
|
|
169
|
+
let h = header + 4, data = base + dataOffset;
|
|
170
|
+
let shared = null;
|
|
171
|
+
if (countField & 0x8000)
|
|
172
|
+
[shared, data] = readPoints(r, data);
|
|
173
|
+
const tuples = [];
|
|
174
|
+
for (let i = 0; i < count; i++) {
|
|
175
|
+
const size = r.u16(h), index = r.u16(h + 2);
|
|
176
|
+
h += 4;
|
|
177
|
+
let peak;
|
|
178
|
+
if (index & 0x8000) {
|
|
179
|
+
peak = Array.from({ length: axisCount }, (_, a) => f2dot14(r, h + a * 2));
|
|
180
|
+
h += axisCount * 2;
|
|
181
|
+
}
|
|
182
|
+
else
|
|
183
|
+
peak = sharedTuples[index & 0xfff] ?? new Array(axisCount).fill(0);
|
|
184
|
+
let start = null, end = null;
|
|
185
|
+
if (index & 0x4000) {
|
|
186
|
+
start = Array.from({ length: axisCount }, (_, a) => f2dot14(r, h + a * 2));
|
|
187
|
+
end = Array.from({ length: axisCount }, (_, a) => f2dot14(r, h + axisCount * 2 + a * 2));
|
|
188
|
+
h += axisCount * 4;
|
|
189
|
+
}
|
|
190
|
+
let points = shared, d = data;
|
|
191
|
+
if (index & 0x2000)
|
|
192
|
+
[points, d] = readPoints(r, data);
|
|
193
|
+
tuples.push({ peak, start, end, points, data: d, size: size - (d - data) });
|
|
194
|
+
data += size;
|
|
195
|
+
}
|
|
196
|
+
return tuples;
|
|
197
|
+
}
|
|
198
|
+
export class Gvar {
|
|
199
|
+
r;
|
|
200
|
+
axisCount;
|
|
201
|
+
sharedTuples = [];
|
|
202
|
+
glyphCount;
|
|
203
|
+
long;
|
|
204
|
+
offsets;
|
|
205
|
+
dataStart;
|
|
206
|
+
constructor(r, gvar) {
|
|
207
|
+
this.r = r;
|
|
208
|
+
this.axisCount = r.u16(gvar + 4);
|
|
209
|
+
const sharedCount = r.u16(gvar + 6), shared = gvar + r.u32(gvar + 8);
|
|
210
|
+
for (let i = 0; i < sharedCount; i++)
|
|
211
|
+
this.sharedTuples.push(Array.from({ length: this.axisCount }, (_, a) => f2dot14(r, shared + (i * this.axisCount + a) * 2)));
|
|
212
|
+
this.glyphCount = r.u16(gvar + 12);
|
|
213
|
+
this.long = (r.u16(gvar + 14) & 1) !== 0;
|
|
214
|
+
this.dataStart = gvar + r.u32(gvar + 16);
|
|
215
|
+
this.offsets = gvar + 20;
|
|
216
|
+
}
|
|
217
|
+
deltas(glyph, coords, n, ends, xs, ys) {
|
|
218
|
+
if (glyph >= this.glyphCount || coords.every(c => c === 0))
|
|
219
|
+
return null;
|
|
220
|
+
const r = this.r;
|
|
221
|
+
const offset = (i) => this.long ? r.u32(this.offsets + i * 4) : r.u16(this.offsets + i * 2) * 2;
|
|
222
|
+
const start = offset(glyph), end = offset(glyph + 1);
|
|
223
|
+
if (end <= start)
|
|
224
|
+
return null;
|
|
225
|
+
const base = this.dataStart + start;
|
|
226
|
+
const out = new Float64Array(2 * n);
|
|
227
|
+
let any = false;
|
|
228
|
+
for (const t of readTuples(r, base, base, this.axisCount, this.sharedTuples)) {
|
|
229
|
+
const s = scalar(coords, t.peak, t.start, t.end);
|
|
230
|
+
if (!s)
|
|
231
|
+
continue;
|
|
232
|
+
const count = t.points ? t.points.length : n;
|
|
233
|
+
const [dx, after] = readDeltas(r, t.data, count), [dy] = readDeltas(r, after, count);
|
|
234
|
+
any = true;
|
|
235
|
+
if (!t.points) {
|
|
236
|
+
for (let i = 0; i < n; i++) {
|
|
237
|
+
out[2 * i] += s * dx[i];
|
|
238
|
+
out[2 * i + 1] += s * dy[i];
|
|
239
|
+
}
|
|
240
|
+
continue;
|
|
241
|
+
}
|
|
242
|
+
const tx = new Float64Array(n), ty = new Float64Array(n), touched = new Uint8Array(n);
|
|
243
|
+
t.points.forEach((p, k) => {
|
|
244
|
+
if (p >= n)
|
|
245
|
+
return;
|
|
246
|
+
tx[p] = dx[k];
|
|
247
|
+
ty[p] = dy[k];
|
|
248
|
+
touched[p] = 1;
|
|
249
|
+
});
|
|
250
|
+
if (ends)
|
|
251
|
+
interpolateUntouched(tx, ty, touched, ends, xs, ys);
|
|
252
|
+
for (let i = 0; i < n; i++) {
|
|
253
|
+
out[2 * i] += s * tx[i];
|
|
254
|
+
out[2 * i + 1] += s * ty[i];
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
return any ? out : null;
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
function interpolateUntouched(dx, dy, touched, ends, xs, ys) {
|
|
261
|
+
let first = 0;
|
|
262
|
+
for (const last of ends) {
|
|
263
|
+
let firstTouched = -1;
|
|
264
|
+
for (let i = first; i <= last; i++)
|
|
265
|
+
if (touched[i]) {
|
|
266
|
+
firstTouched = i;
|
|
267
|
+
break;
|
|
268
|
+
}
|
|
269
|
+
if (firstTouched >= 0) {
|
|
270
|
+
for (const [d, c] of [[dx, xs], [dy, ys]]) {
|
|
271
|
+
let prev = firstTouched;
|
|
272
|
+
for (let k = 1; k <= last - first + 1; k++) {
|
|
273
|
+
const i = first + (firstTouched - first + k) % (last - first + 1);
|
|
274
|
+
if (!touched[i])
|
|
275
|
+
continue;
|
|
276
|
+
infer(d, c, prev, i, first, last);
|
|
277
|
+
prev = i;
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
first = last + 1;
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
function infer(d, c, a, b, first, last) {
|
|
285
|
+
const ca = c[a], cb = c[b], da = d[a], db = d[b];
|
|
286
|
+
const lo = Math.min(ca, cb), hi = Math.max(ca, cb), dlo = ca <= cb ? da : db, dhi = ca <= cb ? db : da;
|
|
287
|
+
for (let i = a === last ? first : a + 1; i !== b; i = i === last ? first : i + 1) {
|
|
288
|
+
const v = c[i];
|
|
289
|
+
d[i] = lo === hi ? (da === db ? da : 0) : v <= lo ? dlo : v >= hi ? dhi : dlo + (v - lo) * (dhi - dlo) / (hi - lo);
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
export function cvarDeltas(r, cvar, axisCount, coords, count) {
|
|
293
|
+
if (coords.every(c => c === 0))
|
|
294
|
+
return null;
|
|
295
|
+
const out = new Float64Array(count);
|
|
296
|
+
let any = false;
|
|
297
|
+
for (const t of readTuples(r, cvar, cvar + 4, axisCount, [])) {
|
|
298
|
+
const s = scalar(coords, t.peak, t.start, t.end);
|
|
299
|
+
if (!s)
|
|
300
|
+
continue;
|
|
301
|
+
const points = t.points ?? Array.from({ length: count }, (_, i) => i);
|
|
302
|
+
const [deltas] = readDeltas(r, t.data, points.length);
|
|
303
|
+
points.forEach((p, k) => {
|
|
304
|
+
if (p < count)
|
|
305
|
+
out[p] += s * deltas[k];
|
|
306
|
+
});
|
|
307
|
+
any = true;
|
|
308
|
+
}
|
|
309
|
+
return any ? out : null;
|
|
310
|
+
}
|
package/dist/util/icc.js
CHANGED
|
@@ -109,7 +109,43 @@ export function profileTransform(icc) {
|
|
|
109
109
|
catch {
|
|
110
110
|
return null;
|
|
111
111
|
}
|
|
112
|
-
|
|
112
|
+
return p && transform(p);
|
|
113
|
+
}
|
|
114
|
+
const CICP_PRIMARIES = {
|
|
115
|
+
1: [[0.64, 0.33], [0.30, 0.60], [0.15, 0.06], [0.3127, 0.3290]],
|
|
116
|
+
4: [[0.67, 0.33], [0.21, 0.71], [0.14, 0.08], [0.310, 0.316]],
|
|
117
|
+
5: [[0.64, 0.33], [0.29, 0.60], [0.15, 0.06], [0.3127, 0.3290]],
|
|
118
|
+
6: [[0.630, 0.340], [0.310, 0.595], [0.155, 0.070], [0.3127, 0.3290]],
|
|
119
|
+
7: [[0.630, 0.340], [0.310, 0.595], [0.155, 0.070], [0.3127, 0.3290]],
|
|
120
|
+
8: [[0.681, 0.319], [0.243, 0.692], [0.145, 0.049], [0.310, 0.316]],
|
|
121
|
+
9: [[0.708, 0.292], [0.170, 0.797], [0.131, 0.046], [0.3127, 0.3290]],
|
|
122
|
+
11: [[0.680, 0.320], [0.265, 0.690], [0.150, 0.060], [0.314, 0.351]],
|
|
123
|
+
12: [[0.680, 0.320], [0.265, 0.690], [0.150, 0.060], [0.3127, 0.3290]],
|
|
124
|
+
22: [[0.630, 0.340], [0.295, 0.605], [0.155, 0.077], [0.3127, 0.3290]],
|
|
125
|
+
};
|
|
126
|
+
const D50 = [0.9642, 1, 0.8249];
|
|
127
|
+
const BRADFORD = [0.8951, 0.2664, -0.1614, -0.7502, 1.7135, 0.0367, 0.0389, -0.0685, 1.0296];
|
|
128
|
+
export function cicpTransform(primaries, transfer) {
|
|
129
|
+
const xy = CICP_PRIMARIES[primaries];
|
|
130
|
+
if (!xy || transfer === 16 || transfer === 18)
|
|
131
|
+
return null;
|
|
132
|
+
const xyz = ([x, y]) => [x / y, 1, (1 - x - y) / y];
|
|
133
|
+
const p = [0, 1, 2].map(i => xyz(xy[i])), white = xyz(xy[3]);
|
|
134
|
+
const columns = [p[0][0], p[1][0], p[2][0], p[0][1], p[1][1], p[2][1], p[0][2], p[1][2], p[2][2]];
|
|
135
|
+
const inverse = invert3(columns);
|
|
136
|
+
if (!inverse)
|
|
137
|
+
return null;
|
|
138
|
+
const scale = [0, 1, 2].map(r => inverse[r * 3] * white[0] + inverse[r * 3 + 1] * white[1] + inverse[r * 3 + 2] * white[2]);
|
|
139
|
+
const matrix = columns.map((v, i) => v * scale[i % 3]);
|
|
140
|
+
const cone = (v) => [0, 1, 2].map(r => BRADFORD[r * 3] * v[0] + BRADFORD[r * 3 + 1] * v[1] + BRADFORD[r * 3 + 2] * v[2]);
|
|
141
|
+
const from = cone(white), to = cone(D50);
|
|
142
|
+
const adapt = multiply3(invert3(BRADFORD), multiply3([to[0] / from[0], 0, 0, 0, to[1] / from[1], 0, 0, 0, to[2] / from[2]], BRADFORD));
|
|
143
|
+
const curve = transfer === 4 ? v => v ** 2.2 : transfer === 5 ? v => v ** 2.8 : transfer === 8 ? v => v
|
|
144
|
+
: v => v <= 0.04045 ? v / 12.92 : ((v + 0.055) / 1.055) ** 2.4;
|
|
145
|
+
return transform({ gray: false, matrix: multiply3(adapt, matrix), curves: [curve, curve, curve] });
|
|
146
|
+
}
|
|
147
|
+
function transform(p) {
|
|
148
|
+
if (isSRGB(p))
|
|
113
149
|
return null;
|
|
114
150
|
const linear = p.curves.map(c => Float32Array.from({ length: 256 }, (_, i) => c(i / 255)));
|
|
115
151
|
const N = 4096, encode = new Uint8Array(N + 1);
|
package/dist/version.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const VERSION = '3.
|
|
1
|
+
export const VERSION = '3.12.0';
|