imagescript 3.11.0 → 3.12.1
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 +18 -18
- package/THIRD_PARTY_NOTICES.md +1 -1
- package/dist/codecs/avif.js +2 -2
- package/dist/codecs/vp8l.js +220 -7
- 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 +2 -2
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.1';
|