Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 10 additions & 17 deletions src/core/text-rendering/SdfFontHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -550,22 +550,15 @@ export const measureText = (
fontFamily: string,
letterSpacing: number,
): number => {
if (text.length === 1) {
const char = text.charAt(0);
const codepoint = text.codePointAt(0);
if (codepoint === undefined) return 0;
if (hasZeroWidthSpace(char) === true) return 0;
if (text.length === 0) return 0;

const glyph = getGlyph(fontFamily, codepoint);
if (glyph === null) return 0;
return glyph.xadvance + letterSpacing;
}
let width = 0;
let prevCodepoint = 0;
for (let i = 0; i < text.length; i++) {
const char = text.charAt(i);
const codepoint = text.codePointAt(i);
if (codepoint === undefined) continue;
let prevGlyphId = 0;
for (const char of text) {
const codepoint = char.codePointAt(0);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!

if (codepoint === undefined) {
continue;
}

// Skip zero-width spaces in width calculations
if (hasZeroWidthSpace(char)) {
Expand All @@ -578,13 +571,13 @@ export const measureText = (
let advance = glyph.xadvance;

// Add kerning if there's a previous character
if (prevCodepoint !== 0) {
const kerning = getKerning(fontFamily, prevCodepoint, codepoint);
if (prevGlyphId !== 0) {
const kerning = getKerning(fontFamily, prevGlyphId, glyph.id);
advance += kerning;
}

width += advance + letterSpacing;
prevCodepoint = codepoint;
prevGlyphId = glyph.id;
}

return width;
Expand Down
27 changes: 11 additions & 16 deletions src/core/text-rendering/SdfTextRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,6 @@ const generateTextLayout = (
const fontFamily = props.fontFamily;
const lineHeight = props.lineHeight;
const metrics = SdfFontHandler.getFontMetrics(fontFamily, fontSize);
const verticalAlign = props.verticalAlign;

const fontData = fontCache.data;
const commonFontData = fontData.common;
Expand Down Expand Up @@ -323,14 +322,12 @@ const generateTextLayout = (
for (let i = 0; i < lineAmount; i++) {
const line = lines[i] as TextLineStruct;
const textLine = line[0];
const textLineLength = textLine.length;
let prevCodepoint = 0;
let prevGlyphId = 0;
currentX = line[3];
//convert Y coord to vertex value
currentY = line[4] / fontScale;

for (let j = 0; j < textLineLength; j++) {
const char = textLine.charAt(j);
for (const char of textLine) {
if (hasZeroWidthSpace(char) === true) {
continue;
}
Expand All @@ -343,19 +340,17 @@ const generateTextLayout = (
if (glyph === null) {
continue;
}
// Calculate advance with kerning (in design units)
let advance = glyph.xadvance;
// Kerning offsets the current glyph relative to the previous glyph.
let kerning = 0;

// Add kerning if there's a previous character
if (prevCodepoint !== 0) {
const kerning = SdfFontHandler.getKerning(
fontFamily,
prevCodepoint,
codepoint,
);
advance += kerning;
if (prevGlyphId !== 0) {
kerning = SdfFontHandler.getKerning(fontFamily, prevGlyphId, glyph.id);
}

// Apply pair kerning before placing this glyph.
currentX += kerning;

// Calculate glyph position and atlas coordinates (in design units)
const glyphLayout: GlyphLayout = {
codepoint,
Expand All @@ -375,8 +370,8 @@ const generateTextLayout = (
glyphs.push(glyphLayout);

// Advance position with letter spacing (in design units)
currentX += advance + letterSpacing;
prevCodepoint = codepoint;
currentX += glyph.xadvance + letterSpacing;
prevGlyphId = glyph.id;
}
currentY += lineHeightPx;
}
Expand Down
Binary file modified visual-regression/certified-snapshots/chromium-ci/alignment-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified visual-regression/certified-snapshots/chromium-ci/autosize-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified visual-regression/certified-snapshots/chromium-ci/autosize-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified visual-regression/certified-snapshots/chromium-ci/clipping-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified visual-regression/certified-snapshots/chromium-ci/clipping-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified visual-regression/certified-snapshots/chromium-ci/clipping-3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified visual-regression/certified-snapshots/chromium-ci/destroy-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified visual-regression/certified-snapshots/chromium-ci/resize-mode-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified visual-regression/certified-snapshots/chromium-ci/resize-mode-3.png
Binary file modified visual-regression/certified-snapshots/chromium-ci/resize-mode-5.png
Binary file modified visual-regression/certified-snapshots/chromium-ci/scaling-1.png
Binary file modified visual-regression/certified-snapshots/chromium-ci/scaling-2.png
Binary file modified visual-regression/certified-snapshots/chromium-ci/scaling-3.png
Binary file modified visual-regression/certified-snapshots/chromium-ci/text-align-1.png
Binary file modified visual-regression/certified-snapshots/chromium-ci/text-align-2.png
Binary file modified visual-regression/certified-snapshots/chromium-ci/text-align-3.png
Binary file modified visual-regression/certified-snapshots/chromium-ci/text-align-4.png
Binary file modified visual-regression/certified-snapshots/chromium-ci/text-alpha-1.png
Binary file modified visual-regression/certified-snapshots/chromium-ci/text-alpha-2.png
Binary file modified visual-regression/certified-snapshots/chromium-ci/text-zwsp-2.png
Binary file modified visual-regression/certified-snapshots/chromium-ci/zIndex-1.png
Loading