imagescript 4.0.0-rc.5 → 4.0.0-rc.6

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.
@@ -13,6 +13,7 @@ import { loadFont } from "./text.js";
13
13
  import { parseColorLayers, parseBitmaps } from "./color.js";
14
14
  import { parsePaintGlyphs } from "./colr.js";
15
15
  import { multiply } from "../util/matrix.js";
16
+ import { LimitError } from "../util/limits.js";
16
17
  const faces = new WeakMap();
17
18
  function names(font, ids) {
18
19
  const table = font.tables.name;
@@ -40,6 +41,10 @@ function names(font, ids) {
40
41
  }
41
42
  return out;
42
43
  }
44
+ function append(target, items) {
45
+ for (const item of items)
46
+ target.push(item);
47
+ }
43
48
  export function fontFace(data, descriptors) {
44
49
  let font;
45
50
  try {
@@ -51,6 +56,37 @@ export function fontFace(data, descriptors) {
51
56
  const cached = !descriptors && faces.get(font);
52
57
  if (cached)
53
58
  return cached;
59
+ try {
60
+ return describeFace(font, descriptors);
61
+ }
62
+ catch (err) {
63
+ if (err instanceof RangeError && !(err instanceof LimitError))
64
+ return null;
65
+ throw err;
66
+ }
67
+ }
68
+ export function givenFontFace(data) {
69
+ let font;
70
+ try {
71
+ font = loadFont(data);
72
+ }
73
+ catch (err) {
74
+ throw damaged(err);
75
+ }
76
+ const cached = faces.get(font);
77
+ if (cached)
78
+ return cached;
79
+ try {
80
+ return describeFace(font);
81
+ }
82
+ catch (err) {
83
+ throw damaged(err);
84
+ }
85
+ }
86
+ export function damaged(err) {
87
+ return err instanceof RangeError && !(err instanceof LimitError) ? new Error('Font: damaged (a table lies outside of the font)', { cause: err }) : err;
88
+ }
89
+ function describeFace(font, descriptors) {
54
90
  const os2 = font.tables['OS/2'] && font.tables['OS/2'].length >= 78 ? font.tables['OS/2'].offset : -1;
55
91
  const post = font.tables.post && font.tables.post.length >= 16 ? font.tables.post.offset : -1;
56
92
  const version = os2 >= 0 ? font.u16(os2) : 0;
@@ -164,8 +200,8 @@ export function variedFace(face, query, fontSize) {
164
200
  byCoords.set(key, varied = { ...face, font, ascent: face.ascent + font.ascender - face.font.ascender, descent: face.descent + font.descender - face.font.descender });
165
201
  return varied;
166
202
  }
167
- export function matchFont(fonts, query) {
168
- if (!fonts.length)
203
+ export function matchFont(fonts, query, fallback = fonts[0]) {
204
+ if (!fonts.length || !fallback)
169
205
  return null;
170
206
  const family = (name) => fonts.filter(f => f.families.includes(name));
171
207
  for (const name of query.families) {
@@ -179,7 +215,7 @@ export function matchFont(fonts, query) {
179
215
  if (candidates.length)
180
216
  return bestMatch(candidates, query);
181
217
  }
182
- return bestMatch(family(fonts[0].families[0]).length ? family(fonts[0].families[0]) : [fonts[0]], query);
218
+ return bestMatch(family(fallback.families[0]).length ? family(fallback.families[0]) : [fallback], query);
183
219
  }
184
220
  function fallbackFont(fonts, base, grapheme, tried) {
185
221
  for (const f of fonts) {
@@ -280,7 +316,23 @@ function upright(u) {
280
316
  || (u >= 0xffe0 && u <= 0xffe7) || (u >= 0x1f000 && u <= 0x1faff) || (u >= 0x20000 && u <= 0x3fffd)
281
317
  || (u >= 0x2460 && u <= 0x24ff) || (u >= 0x2600 && u <= 0x27bf && u !== 0x2771 && u !== 0x2770);
282
318
  }
283
- const spanAt = (chunk, i) => chunk.spans.find(s => i >= s.start && i < s.end);
319
+ const sortedSpans = new WeakMap();
320
+ const spanAt = (chunk, i) => {
321
+ let spans = sortedSpans.get(chunk.spans);
322
+ if (!spans)
323
+ sortedSpans.set(chunk.spans, spans = chunk.spans.slice().sort((a, b) => a.start - b.start));
324
+ let low = 0, high = spans.length - 1, found = -1;
325
+ while (low <= high) {
326
+ const mid = (low + high) >>> 1;
327
+ if (spans[mid].start <= i) {
328
+ found = mid;
329
+ low = mid + 1;
330
+ }
331
+ else
332
+ high = mid - 1;
333
+ }
334
+ return found >= 0 && i < spans[found].end ? spans[found] : undefined;
335
+ };
284
336
  function shapeChunk(chunk, fonts, primary) {
285
337
  const cps = chunk.codepoints, n = cps.length;
286
338
  const faceOf = new Array(n).fill(null);
@@ -313,7 +365,7 @@ function shapeChunk(chunk, fonts, primary) {
313
365
  let j = i + 1;
314
366
  while (j < n && (levels[j] < 0 || levels[j] === level))
315
367
  j++;
316
- script.push(...scripts(cps.slice(i, j)));
368
+ append(script, scripts(cps.slice(i, j)));
317
369
  i = j;
318
370
  }
319
371
  last = 0;
@@ -367,7 +419,7 @@ function shapeChunk(chunk, fonts, primary) {
367
419
  const shaped = shape(base);
368
420
  const faces = new Set(faceOf.slice(rs, re));
369
421
  if (faces.size === 1) {
370
- glyphs.push(...shaped);
422
+ append(glyphs, shaped);
371
423
  continue;
372
424
  }
373
425
  const others = new Map([...faces].filter(f => f && f !== base).map(f => [f, shape(f)]));
@@ -379,14 +431,14 @@ function shapeChunk(chunk, fonts, primary) {
379
431
  const c = shaped[i].cluster, end = starts.find(v => v > c) ?? re;
380
432
  const other = faceOf[c] && others.get(faceOf[c]);
381
433
  if (!other)
382
- glyphs.push(...shaped.slice(i, j));
434
+ append(glyphs, shaped.slice(i, j));
383
435
  else {
384
436
  const own = other.filter(g => g.cluster >= c && g.cluster < end);
385
437
  const before = other.reduce((m, g) => g.cluster <= c ? Math.max(m, g.cluster) : m, -1);
386
438
  if (own.length)
387
- glyphs.push(...own);
439
+ append(glyphs, own);
388
440
  else if (!(before >= 0 && before < c))
389
- glyphs.push(...shaped.slice(i, j));
441
+ append(glyphs, shaped.slice(i, j));
390
442
  }
391
443
  i = j;
392
444
  }
@@ -789,13 +841,13 @@ export function placeOutline(out, outline, m) {
789
841
  function rectPath(m, x, y, w, h) {
790
842
  return [['M', ...apply(m, x, y)], ['L', ...apply(m, x + w, y)], ['L', ...apply(m, x + w, y + h)], ['L', ...apply(m, x, y + h)], ['Z']];
791
843
  }
792
- export function layOutText(text, fonts) {
844
+ export function layOutText(text, fonts, fallback = fonts[0]) {
793
845
  if (!fonts.length)
794
846
  return null;
795
847
  const primary = new Map();
796
848
  for (const chunk of text.chunks)
797
849
  for (const span of chunk.spans) {
798
- const face = matchFont(fonts, span.font);
850
+ const face = matchFont(fonts, span.font, fallback);
799
851
  if (face)
800
852
  primary.set(span, variedFace(face, span.font, span.fontSize));
801
853
  }
@@ -831,6 +883,27 @@ export function layOutText(text, fonts) {
831
883
  };
832
884
  const position = chunk.path ? positionOnPath(text, chunk, clusters, shiftOf) : positionLinear(text, chunk, clusters);
833
885
  const textMatrix = text.vertical && !chunk.path ? rotate(IDENTITY, 90, x, y) : IDENTITY;
886
+ const owned = new Map(), ordered = chunk.spans.slice().sort((a, b) => a.start - b.start);
887
+ clusters.forEach((c, i) => {
888
+ let low = 0, high = ordered.length - 1, found = -1;
889
+ while (low <= high) {
890
+ const mid = (low + high) >>> 1;
891
+ if (ordered[mid].start <= c.start) {
892
+ found = mid;
893
+ low = mid + 1;
894
+ }
895
+ else
896
+ high = mid - 1;
897
+ }
898
+ if (found < 0 || c.start >= ordered[found].end)
899
+ return;
900
+ const list = owned.get(ordered[found]);
901
+ if (list)
902
+ list.push(i);
903
+ else
904
+ owned.set(ordered[found], [i]);
905
+ });
906
+ const boxed = new Set();
834
907
  for (const span of chunk.spans) {
835
908
  const face = primary.get(span);
836
909
  if (!face)
@@ -841,18 +914,19 @@ export function layOutText(text, fonts) {
841
914
  const s = span.fontSize / face.font.unitsPerEm;
842
915
  const out = { paint: span.paint, visible: span.visible, glyphs: [], colors: [], before: [], after: [] };
843
916
  const runs = [];
844
- let current = null;
845
- for (const c of clusters) {
846
- if (c.start >= span.start && c.start < span.end) {
847
- if (current && c.hasRelativeShift)
848
- current = null;
849
- if (!current)
850
- runs.push(current = { width: c.advance, transform: c.transform });
851
- else
852
- current.width += c.advance;
853
- }
854
- else
917
+ let current = null, previous = -2;
918
+ const own = owned.get(span) ?? [];
919
+ for (const i of own) {
920
+ const c = clusters[i];
921
+ if (i !== previous + 1)
922
+ current = null;
923
+ previous = i;
924
+ if (current && c.hasRelativeShift)
855
925
  current = null;
926
+ if (!current)
927
+ runs.push(current = { width: c.advance, transform: c.transform });
928
+ else
929
+ current.width += c.advance;
856
930
  }
857
931
  const decoration = (paint, offset, list) => {
858
932
  if (!paint || !runs.length)
@@ -871,21 +945,31 @@ export function layOutText(text, fonts) {
871
945
  decoration(span.underline, text.vertical ? height / 2 : -face.underlinePosition * s, out.before);
872
946
  decoration(span.lineThrough, text.vertical ? 0 : -face.lineThroughPosition * s, out.after);
873
947
  let any = false;
874
- for (const c of clusters) {
948
+ const shift = chunk.path ? 0 : shiftOf(span);
949
+ if (!boxed.has(shift)) {
950
+ boxed.add(shift);
951
+ for (const c of clusters) {
952
+ if (!c.visible)
953
+ continue;
954
+ extend(rectPath(multiply(spanMatrix, multiply(c.transform, c.pathTransform)), 0, -c.ascent, c.advance > 0 ? c.advance : 1, c.ascent - c.descent));
955
+ }
956
+ }
957
+ for (const i of own) {
958
+ const c = clusters[i];
875
959
  if (!c.visible)
876
960
  continue;
877
961
  const m = multiply(spanMatrix, multiply(c.transform, c.pathTransform));
878
- extend(rectPath(m, 0, -c.ascent, c.advance > 0 ? c.advance : 1, c.ascent - c.descent));
879
- if (c.start < span.start || c.start >= span.end)
880
- continue;
881
962
  any = true;
882
963
  for (const g of c.glyphs) {
883
964
  const gs = c.fontSize / g.face.font.unitsPerEm, matrix = scale(translate(scale(m, gs, gs), g.x, -g.y), 1, -1);
884
965
  const kind = colorKind(g.face, g.glyph, c.fontSize);
885
966
  if (kind)
886
967
  out.colors.push({ face: g.face, glyph: g.glyph, kind, fontSize: c.fontSize, matrix });
887
- else
888
- placeOutline(out.glyphs, g.face.font.outline(g.glyph), matrix);
968
+ else {
969
+ const outline = g.face.font.outline(g.glyph);
970
+ text.charge?.(outline.length);
971
+ placeOutline(out.glyphs, outline, matrix);
972
+ }
889
973
  }
890
974
  }
891
975
  if (any || out.before.length || out.after.length)