Skip to content

Commit 6998893

Browse files
committed
Add glyph baseine checker
1 parent 17f3cc6 commit 6998893

5 files changed

Lines changed: 23 additions & 5 deletions

File tree

src/symbol/shaping.js

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,6 @@ function shapeLines(shaping: Shaping,
456456

457457
let x = 0;
458458
let y = 0;
459-
// let y = shaping.yOffset;
460459

461460
let maxLineLength = 0;
462461
const positionedGlyphs = shaping.positionedGlyphs;
@@ -465,6 +464,21 @@ function shapeLines(shaping: Shaping,
465464
textJustify === 'right' ? 1 :
466465
textJustify === 'left' ? 0 : 0.5;
467466

467+
let hasBaseline = false;
468+
for (const line of lines) {
469+
line.trim();
470+
for (let i = 0; i < line.length(); i++) {
471+
const section = line.getSection(i);
472+
const codePoint = line.getCharCode(i);
473+
474+
const positions = glyphMap[section.fontStack];
475+
const glyph = positions && positions[codePoint];
476+
if (!glyph) continue;
477+
hasBaseline = glyph.metrics.ascender !== 0 && glyph.metrics.descender !== 0;
478+
if (!hasBaseline) break;
479+
}
480+
if (!hasBaseline) break;
481+
}
468482
for (const line of lines) {
469483
line.trim();
470484

@@ -483,15 +497,18 @@ function shapeLines(shaping: Shaping,
483497

484498
const positions = glyphMap[section.fontStack];
485499
const glyph = positions && positions[codePoint];
486-
487500
if (!glyph) continue;
501+
502+
// The rects have an addditional buffer that is not included in their size.
503+
const glyphPadding = 1.0;
504+
const rectBuffer = 3 + glyphPadding;
488505
// Each glyph's baseline is starting from its acsender, which is the vertical distance
489506
// from the horizontal baseline to the highest ‘character’ coordinate in a font face.
507+
// If ascender is applied, the shaping rect buffer needs to be counted.
508+
// If ascender is not applicable, fall back to use a default baseline yOffset.
490509
// Since we're laying out at 24 points, we need also calculate how much it will move
491510
// when we scale up or down.
492-
const hasBaselineOffset = glyph.metrics.ascender !== 0 && glyph.metrics.descender !== 0;
493-
const baselineOffset = (hasBaselineOffset ? (-glyph.metrics.ascender * section.scale) : shaping.yOffset) + (lineMaxScale - section.scale) * 24;
494-
// const baselineOffset = (lineMaxScale - section.scale) * 24;
511+
const baselineOffset = (hasBaseline ? ((-glyph.metrics.ascender + rectBuffer * 2) * section.scale) : shaping.yOffset) + (lineMaxScale - section.scale) * 24;
495512

496513
if (writingMode === WritingMode.horizontal ||
497514
// Don't verticalize glyphs that have no upright orientation if vertical placement is disabled.
2.73 MB
Binary file not shown.
5.12 MB
Binary file not shown.
55 Bytes
Loading

test/integration/render-tests/text-font/mixed-fonts/style.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"version": 8,
33
"metadata": {
44
"test": {
5+
"collisionDebug": true,
56
"height": 300,
67
"width": 300
78
}

0 commit comments

Comments
 (0)