ntk 8.29.2 → 8.30.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/lib/text/font.js CHANGED
@@ -489,16 +489,22 @@ export default class Font {
489
489
  shape(text, size, opts = {}) {
490
490
  const run = this._layout(text, opts);
491
491
  const s = this.scale(size);
492
- const glyphs = new Array(run.glyphs.length);
492
+ const count = run.glyphs.length;
493
+ const glyphs = new Array(count);
494
+ // fontkit puts a right-to-left run in visual order in its shaping
495
+ // engines, the OpenType one and the AAT one, and a face with no GSUB,
496
+ // no GPOS and no morx gets neither: its run comes back as it was read
497
+ const backwards = run.direction === 'rtl' && !this._shapedByEngine();
493
498
  let width = 0;
494
- for (let i = 0; i < run.glyphs.length; i++) {
495
- const pos = run.positions[i];
499
+ for (let i = 0; i < count; i++) {
500
+ const from = backwards ? count - 1 - i : i;
501
+ const pos = run.positions[from];
496
502
  const g = {
497
- id: run.glyphs[i].id,
503
+ id: run.glyphs[from].id,
498
504
  ax: pos.xAdvance * s,
499
505
  dx: pos.xOffset * s,
500
506
  dy: pos.yOffset * s,
501
- codePoints: run.glyphs[i].codePoints
507
+ codePoints: run.glyphs[from].codePoints
502
508
  };
503
509
  width += g.ax;
504
510
  glyphs[i] = g;
@@ -506,6 +512,23 @@ export default class Font {
506
512
  return { font: this, size, direction: run.direction, width, glyphs };
507
513
  }
508
514
 
515
+ /**
516
+ * Whether fontkit shapes this face's runs in an engine of its own: the
517
+ * AAT one for a face with `morx`, the OpenType one for a face with GSUB
518
+ * or GPOS (its `LayoutEngine`), which is where a right-to-left run is
519
+ * turned into visual order. A face with none of the three is laid out
520
+ * glyph for character, in the order of its text.
521
+ */
522
+ _shapedByEngine() {
523
+ if (this._engine === undefined) {
524
+ const tables = this.fk.directory?.tables;
525
+ this._engine = tables
526
+ ? !!(tables.morx || tables.GSUB || tables.GPOS)
527
+ : !!(this.fk.morx || this.fk.GSUB || this.fk.GPOS);
528
+ }
529
+ return this._engine;
530
+ }
531
+
509
532
  /**
510
533
  * fontkit's layout, with the face's mark lookups stood in for until a run
511
534
  * holds a glyph they act at.
@@ -181,10 +181,39 @@ function kernBefore(token) {
181
181
  return token.kernBefore ?? 0;
182
182
  }
183
183
 
184
+ const SOFT_HYPHEN = 0xad;
185
+
186
+ /** Whether a token ends on a soft hyphen: a place a line may wrap, with a
187
+ * hyphen shown where it does. */
188
+ function endsSoft(text, token) {
189
+ return token.fragments.length > 0 && text.charCodeAt(token.end - 1) === SOFT_HYPHEN;
190
+ }
191
+
192
+ /** Whether a line whose last token is `token` wrapped at a soft hyphen:
193
+ * the token ends on one, with text after it that no forced break parts it
194
+ * from. A soft hyphen a paragraph ends on, or its line, shows nothing. */
195
+ function wrapsSoft(text, token) {
196
+ return !token.required && token.end < text.length && endsSoft(text, token);
197
+ }
198
+
184
199
  function isWsGlyph(g) {
185
200
  return g.codePoints.length > 0 && g.codePoints.every((cp) => WS.has(cp));
186
201
  }
187
202
 
203
+ /** Whether a character a line ends on hangs past its edge with the white
204
+ * space around it: a space or a tab, or a bidi control (Bidi_Control), which
205
+ * is nothing to white space on either side of it. */
206
+ function hangs(cu) {
207
+ return (
208
+ WS.has(cu) ||
209
+ cu === 0x200e ||
210
+ cu === 0x200f ||
211
+ cu === 0x061c ||
212
+ (cu >= 0x202a && cu <= 0x202e) ||
213
+ (cu >= 0x2066 && cu <= 0x2069)
214
+ );
215
+ }
216
+
188
217
  /**
189
218
  * A line set to fill `width`: what it leaves of it shared equally among its
190
219
  * word separators (`SEPARATORS`), each glyph's advance widened by its share
@@ -234,7 +263,7 @@ function justifyLine(line, width) {
234
263
  *
235
264
  * Content is a plain string or an array of spans
236
265
  * `{ text, family?, size?, weight?, style?, features?, language?,
237
- * letterSpacing?, color?, nowrap?, shapeApart?, kernAcross? }`;
266
+ * letterSpacing?, color?, nowrap?, shapeApart?, kernAcross?, hyphen? }`;
238
267
  * span fields override the base style. Spans that share a truthy `nowrap` —
239
268
  * `true`, or any value a caller tells its groups apart by — have no break
240
269
  * opportunity inside them or between them, as the text of an element with
@@ -247,7 +276,11 @@ function justifyLine(line, width) {
247
276
  * browser shapes an element's `letter-spacing`, but one with a truthy
248
277
  * `kernAcross` keeps the kerning its letters make with its neighbours':
249
278
  * the spacing a justified line or CSS's `word-spacing` adds to a space is
250
- * in addition to kerning, and no element's (CSS Text 3, 7.2, 7.3).
279
+ * in addition to kerning, and no element's (CSS Text 3, 7.2, 7.3). A line
280
+ * that wraps at a soft hyphen, U+00AD, ends in a hyphen it has room for,
281
+ * in the style of the span the word ends in: that span's `hyphen` where it
282
+ * is a string — CSS's `hyphenate-character`, an empty one for none — and
283
+ * otherwise U+2010 or, in a font without it, a hyphen-minus (`_hyphen`).
251
284
  * Options:
252
285
  *
253
286
  * - `maxWidth` — target container width (default: unlimited)
@@ -404,7 +437,7 @@ export class TextLayout {
404
437
  // how far the line moves the pen where text goes on after it on the
405
438
  // same line: its trailing white space kept, in the item it ends
406
439
  let advance = fittedEntries(entries, round);
407
- let trailing = stripTrailingWhitespace(entries);
440
+ let trailing = stripTrailingWhitespace(entries, this._text);
408
441
  const contentEnd = trailing
409
442
  ? trailing.start
410
443
  : entries.length
@@ -435,6 +468,26 @@ export class TextLayout {
435
468
  trailing = null;
436
469
  advance = null;
437
470
  lineEnd = contentEnd;
471
+ } else if (toks.length && wrapsSoft(this._text, toks[toks.length - 1]) && entries.length) {
472
+ // A line that wraps at a soft hyphen ends in a hyphen (Unicode's
473
+ // SHY, CSS Text 3, 5.2), at the level of the word it ends, which
474
+ // puts it after that word whichever way the word runs. Its logical
475
+ // range is empty and at the line's end, as an ellipsis's is.
476
+ const last = entries[entries.length - 1];
477
+ if (last.kernAfter) entries[entries.length - 1] = { ...last, kernAfter: 0 };
478
+ const hyphen = this._hyphen(toks[toks.length - 1]);
479
+ for (const run of hyphen.shaped?.runs ?? []) {
480
+ entries.push({
481
+ run,
482
+ span: hyphen.span,
483
+ level: last.level,
484
+ start: contentEnd,
485
+ end: contentEnd,
486
+ hyphen: true
487
+ });
488
+ }
489
+ // and the pen goes on after it
490
+ if (advance !== null) advance += hyphen.width;
438
491
  }
439
492
  entries = reorderRuns(entries);
440
493
 
@@ -454,6 +507,7 @@ export class TextLayout {
454
507
  if (e.kern) x += e.kern;
455
508
  const positioned = { x, width: e.run.width, run: e.run, span: e.span, start: e.start, end: e.end };
456
509
  if (e.ellipsis) positioned.ellipsis = true;
510
+ if (e.hyphen) positioned.hyphen = true;
457
511
  runs.push(positioned);
458
512
  x += e.run.width;
459
513
  if (e.kernAfter) x += e.kernAfter;
@@ -568,13 +622,31 @@ export class TextLayout {
568
622
  cur = [];
569
623
  fit.reset();
570
624
  };
625
+ // A line that wraps after a soft hyphen shows a hyphen there
626
+ // (`_hyphen`), which takes room: where the line has none for it, it
627
+ // ends at the break before, and the tokens after that one start the
628
+ // next line. A line of one token keeps it, as a word too wide does.
629
+ const wrap = () => {
630
+ const carried = [];
631
+ while (cur.length > 1) {
632
+ const last = cur[cur.length - 1];
633
+ if (!endsSoft(text, last)) break;
634
+ if (fittedWidth(cur, round) + this._hyphen(last).width <= maxWidth + FIT_SLACK) break;
635
+ carried.unshift(cur.pop());
636
+ }
637
+ flush();
638
+ for (const token of carried) {
639
+ fit.add(token, cur.length ? kernBefore(token) : 0);
640
+ cur.push(token);
641
+ }
642
+ };
571
643
  for (let token of tokens) {
572
644
  while (
573
645
  wraps &&
574
646
  fit.with(token, cur.length ? kernBefore(token) : 0) > maxWidth + FIT_SLACK
575
647
  ) {
576
648
  if (cur.length > 0) {
577
- flush();
649
+ wrap();
578
650
  } else if (whole && !UNSPACED.test(text.slice(token.start, token.end))) {
579
651
  break;
580
652
  } else {
@@ -934,6 +1006,24 @@ export class TextLayout {
934
1006
  return { text, span, shaped, width: shaped.width };
935
1007
  }
936
1008
 
1009
+ /**
1010
+ * The hyphen a line shows where it wraps after `token`'s soft hyphen:
1011
+ * `{ text, span, shaped, width }`, in the style of the span the token
1012
+ * ends in. The span's own `hyphen` where it has one — CSS's
1013
+ * `hyphenate-character`, and nothing at all for an empty string — and
1014
+ * otherwise U+2010 where the span's font has it and a hyphen-minus where
1015
+ * it does not, as a browser picks one: a monospace face seldom has the
1016
+ * first, and a fallback's would be another width than its letters.
1017
+ */
1018
+ _hyphen(token) {
1019
+ const frags = token.fragments;
1020
+ const span = frags[frags.length - 1].span;
1021
+ const text = typeof span.hyphen === 'string' ? span.hyphen : span.font.hasGlyph(0x2010) ? '\u2010' : '-';
1022
+ if (!text) return { text, span, shaped: null, width: 0 };
1023
+ const shaped = this.fonts._shapeCached(text, span, String(this.baseLevel));
1024
+ return { text, span, shaped, width: shaped.width };
1025
+ }
1026
+
937
1027
  /**
938
1028
  * The longest prefix of a line's tokens whose width fits `budget` — whole
939
1029
  * tokens while they fit, then a grapheme-boundary cut into the first one
@@ -1312,8 +1402,9 @@ export class TextLayout {
1312
1402
  }
1313
1403
  }
1314
1404
  // the ellipsis stands for text that is not displayed, so anywhere on it
1315
- // is the end of what is: it has no indices of its own to land in
1316
- if (target.ellipsis) return this._cpOf(line._contentEnd);
1405
+ // is the end of what is: it has no indices of its own to land in. Nor
1406
+ // has the hyphen a line shows where it wraps at a soft one
1407
+ if (target.ellipsis || target.hyphen) return this._cpOf(line._contentEnd);
1317
1408
  return this._cpOf(this._runIndexAt(target, cx));
1318
1409
  }
1319
1410
 
@@ -1525,7 +1616,15 @@ function cuLength(codePoints) {
1525
1616
  // order (absolute code-unit start, per-glyph advance and code-unit length) —
1526
1617
  // so caret positioning can still walk through trailing spaces, or null when
1527
1618
  // nothing was stripped. Adjusts the surviving entries' logical `end`.
1528
- function stripTrailingWhitespace(entries) {
1619
+ //
1620
+ // A character with no glyph of its own is shaped as a space of no advance
1621
+ // (fontkit hides a default ignorable so), which is no white space: a
1622
+ // zero-width space after a space is where the line ends, and the space
1623
+ // before it is in the line, as every browser has it. So a glyph is stripped
1624
+ // only where `text` has a space or a tab for it, or a bidi control, which
1625
+ // white space is found through as though it were not there (CSS Text 3,
1626
+ // 4.1.1): an isolate's end after a space leaves the space hanging.
1627
+ function stripTrailingWhitespace(entries, text) {
1529
1628
  const stripped = [];
1530
1629
  let strippedStart = null;
1531
1630
  const result = () => (stripped.length ? { start: strippedStart, glyphs: stripped } : null);
@@ -1535,9 +1634,19 @@ function stripTrailingWhitespace(entries) {
1535
1634
  const fromFront = run.direction === 'rtl';
1536
1635
  let count = 0;
1537
1636
  let wsWidth = 0;
1538
- while (count < run.glyphs.length) {
1637
+ // the code unit a glyph at the logical end stands for
1638
+ let at = entries[i].end - 1;
1639
+ // the soft hyphen a line wraps at is no content of it, which its
1640
+ // hyphen stands for at the end, but it is no white space either: what
1641
+ // is before it stays in the line, as Chromium and WebKit keep it
1642
+ let last = false;
1643
+ while (count < run.glyphs.length && !last) {
1539
1644
  const g = run.glyphs[fromFront ? count : run.glyphs.length - 1 - count];
1540
1645
  if (!isWsGlyph(g)) break;
1646
+ const cu = text.charCodeAt(at);
1647
+ if (cu === SOFT_HYPHEN && stripped.length === 0 && count === 0) last = true;
1648
+ else if (!hangs(cu)) break;
1649
+ at -= cuLength(g.codePoints);
1541
1650
  count++;
1542
1651
  wsWidth += g.ax;
1543
1652
  }
@@ -1556,6 +1665,7 @@ function stripTrailingWhitespace(entries) {
1556
1665
  strippedStart = entries[i].end - cuStripped;
1557
1666
  if (count === run.glyphs.length) {
1558
1667
  entries.splice(i, 1);
1668
+ if (last) return result();
1559
1669
  continue;
1560
1670
  }
1561
1671
  entries[i] = {
package/lib/text/shape.js CHANGED
@@ -47,7 +47,51 @@ export function embeddingLevels(text, direction = 'auto') {
47
47
  paragraphs: [{ start: 0, end: text.length - 1, level: 0 }]
48
48
  };
49
49
  }
50
- return bidi.getEmbeddingLevels(text, explicit);
50
+ return bidi.getEmbeddingLevels(astralAsBmp(text), explicit);
51
+ }
52
+
53
+ // A character of the supplementary planes, as its two code units.
54
+ const ASTRAL = /[\uD800-\uDBFF][\uDC00-\uDFFF]/g;
55
+
56
+ // A character of each bidi class in the Basic Multilingual Plane, to stand
57
+ // in for one outside it (`astralAsBmp`).
58
+ const STAND_INS = {
59
+ R: '\u05D0',
60
+ AL: '\u0627',
61
+ AN: '\u0660',
62
+ EN: '0',
63
+ NSM: '\u0300',
64
+ BN: '\u00AD',
65
+ ET: '%',
66
+ ES: '+',
67
+ CS: ',',
68
+ ON: '!'
69
+ };
70
+
71
+ /**
72
+ * `text` for bidi-js to resolve the levels of: each character of the
73
+ * supplementary planes that is no left-to-right letter as two characters
74
+ * of its bidi class from the Basic Multilingual Plane, one for each of its
75
+ * code units.
76
+ *
77
+ * bidi-js reads a string a code unit at a time, and a surrogate is no
78
+ * character of any class: it takes each for a left-to-right letter. So
79
+ * Cypriot, Phoenician, Adlam, Hanifi Rohingya and their neighbours were
80
+ * left-to-right text, every letter at level 0 and set in the order it was
81
+ * written, and an emoji, which is neutral, was a strong left-to-right
82
+ * character: one that began a Hebrew paragraph made the paragraph
83
+ * left-to-right, and two between Hebrew words stood in the order they were
84
+ * written, where neutrals between right-to-left words run right to left.
85
+ * The stand-ins have the classes the characters have, the text is as long
86
+ * as it was, and a pair's two units come to one level, so the levels are
87
+ * the text's own. Asked only of text something can make right-to-left
88
+ * (`levelZeroThroughout`).
89
+ */
90
+ function astralAsBmp(text) {
91
+ return text.replace(ASTRAL, (pair) => {
92
+ const one = STAND_INS[bidi.getBidiCharTypeName(pair)];
93
+ return one ? one + one : pair;
94
+ });
51
95
  }
52
96
 
53
97
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ntk",
3
- "version": "8.29.2",
3
+ "version": "8.30.1",
4
4
  "description": "Desktop UI toolkit for X11 with canvas-like 2d and OpenGL rendering",
5
5
  "author": "Andrey Sidorov <sidorares@yandex.ru>",
6
6
  "license": "MIT",