ntk 8.30.0 → 8.30.2
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 +44 -9
- package/lib/text/layout.js +10 -2
- package/package.json +1 -1
package/lib/text/font.js
CHANGED
|
@@ -584,8 +584,43 @@ export default class Font {
|
|
|
584
584
|
/** glyph bounding box in pixels relative to the baseline origin, y-down */
|
|
585
585
|
glyphExtents(glyphId, size) {
|
|
586
586
|
const s = this.scale(size);
|
|
587
|
+
// reaching as far as the raster, which is moved (`gridShift`), and as
|
|
588
|
+
// the outline, which is not
|
|
589
|
+
const k = this.gridShift(size);
|
|
587
590
|
const b = this.fk.getGlyph(glyphId).cbox;
|
|
588
|
-
return {
|
|
591
|
+
return {
|
|
592
|
+
minX: b.minX * s,
|
|
593
|
+
maxX: b.maxX * s,
|
|
594
|
+
minY: -b.maxY * s - Math.max(0, k),
|
|
595
|
+
maxY: -b.minY * s - Math.min(0, k)
|
|
596
|
+
};
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
/**
|
|
600
|
+
* How far up a glyph's raster is drawn at a size, in pixels, so that the
|
|
601
|
+
* face's ascender lies on a whole pixel above the baseline: the ascent
|
|
602
|
+
* rounded, less the ascent. A raster glyph's origin is a whole device pixel
|
|
603
|
+
* (see positionGlyphs), so an outline that reaches the ascender at 12.8px —
|
|
604
|
+
* Ahem's at 16px — covered a fifth of its top row and nothing of the row
|
|
605
|
+
* a box beside it fills whole. Hinting puts the ascender on the grid in
|
|
606
|
+
* the engines that hint (FreeType's blue zones), and a box's edge drawn
|
|
607
|
+
* against text then meets it; this is that for the one zone every glyph
|
|
608
|
+
* shares, and it moves no glyph more than half a pixel. Ahem's ascent and
|
|
609
|
+
* descent come to a whole number at every whole size, so both its edges
|
|
610
|
+
* land on the grid. The outline a vector glyph is drawn from is not moved:
|
|
611
|
+
* that path places a glyph at its origin unrounded, where the ascender
|
|
612
|
+
* lands wherever the layout put it.
|
|
613
|
+
*/
|
|
614
|
+
gridShift(size) {
|
|
615
|
+
const cache = (this._gridShifts ??= new Map());
|
|
616
|
+
let k = cache.get(size);
|
|
617
|
+
if (k === undefined) {
|
|
618
|
+
const ascent = this.fk.ascent * this.scale(size);
|
|
619
|
+
k = Number.isFinite(ascent) ? Math.round(ascent) - ascent : 0;
|
|
620
|
+
if (cache.size > 64) cache.clear();
|
|
621
|
+
cache.set(size, k);
|
|
622
|
+
}
|
|
623
|
+
return k;
|
|
589
624
|
}
|
|
590
625
|
|
|
591
626
|
/**
|
|
@@ -594,7 +629,7 @@ export default class Font {
|
|
|
594
629
|
* scanline rasterizer expects.
|
|
595
630
|
*/
|
|
596
631
|
rasterize(glyphId, size) {
|
|
597
|
-
return rasterizePath(this._pathCommands(glyphId, size));
|
|
632
|
+
return rasterizePath(this._pathCommands(glyphId, size, this.gridShift(size)));
|
|
598
633
|
}
|
|
599
634
|
|
|
600
635
|
/**
|
|
@@ -607,30 +642,30 @@ export default class Font {
|
|
|
607
642
|
}
|
|
608
643
|
|
|
609
644
|
/** fontkit y-up font-unit path -> y-down pixel-space M/L/Q/C/Z commands */
|
|
610
|
-
_pathCommands(glyphId, size) {
|
|
645
|
+
_pathCommands(glyphId, size, k = 0) {
|
|
611
646
|
const s = this.scale(size);
|
|
612
647
|
const commands = [];
|
|
613
648
|
for (const c of this.fk.getGlyph(glyphId).path.commands) {
|
|
614
649
|
const a = c.args;
|
|
615
650
|
switch (c.command) {
|
|
616
651
|
case 'moveTo':
|
|
617
|
-
commands.push({ type: 'M', x: a[0] * s, y: -a[1] * s });
|
|
652
|
+
commands.push({ type: 'M', x: a[0] * s, y: -a[1] * s - k });
|
|
618
653
|
break;
|
|
619
654
|
case 'lineTo':
|
|
620
|
-
commands.push({ type: 'L', x: a[0] * s, y: -a[1] * s });
|
|
655
|
+
commands.push({ type: 'L', x: a[0] * s, y: -a[1] * s - k });
|
|
621
656
|
break;
|
|
622
657
|
case 'quadraticCurveTo':
|
|
623
|
-
commands.push({ type: 'Q', x1: a[0] * s, y1: -a[1] * s, x: a[2] * s, y: -a[3] * s });
|
|
658
|
+
commands.push({ type: 'Q', x1: a[0] * s, y1: -a[1] * s - k, x: a[2] * s, y: -a[3] * s - k });
|
|
624
659
|
break;
|
|
625
660
|
case 'bezierCurveTo':
|
|
626
661
|
commands.push({
|
|
627
662
|
type: 'C',
|
|
628
663
|
x1: a[0] * s,
|
|
629
|
-
y1: -a[1] * s,
|
|
664
|
+
y1: -a[1] * s - k,
|
|
630
665
|
x2: a[2] * s,
|
|
631
|
-
y2: -a[3] * s,
|
|
666
|
+
y2: -a[3] * s - k,
|
|
632
667
|
x: a[4] * s,
|
|
633
|
-
y: -a[5] * s
|
|
668
|
+
y: -a[5] * s - k
|
|
634
669
|
});
|
|
635
670
|
break;
|
|
636
671
|
case 'closePath':
|
package/lib/text/layout.js
CHANGED
|
@@ -1636,9 +1636,16 @@ function stripTrailingWhitespace(entries, text) {
|
|
|
1636
1636
|
let wsWidth = 0;
|
|
1637
1637
|
// the code unit a glyph at the logical end stands for
|
|
1638
1638
|
let at = entries[i].end - 1;
|
|
1639
|
-
|
|
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) {
|
|
1640
1644
|
const g = run.glyphs[fromFront ? count : run.glyphs.length - 1 - count];
|
|
1641
|
-
if (!isWsGlyph(g)
|
|
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;
|
|
1642
1649
|
at -= cuLength(g.codePoints);
|
|
1643
1650
|
count++;
|
|
1644
1651
|
wsWidth += g.ax;
|
|
@@ -1658,6 +1665,7 @@ function stripTrailingWhitespace(entries, text) {
|
|
|
1658
1665
|
strippedStart = entries[i].end - cuStripped;
|
|
1659
1666
|
if (count === run.glyphs.length) {
|
|
1660
1667
|
entries.splice(i, 1);
|
|
1668
|
+
if (last) return result();
|
|
1661
1669
|
continue;
|
|
1662
1670
|
}
|
|
1663
1671
|
entries[i] = {
|