// A glyph's outline is moved up by less than half a pixel so that its // face's ascender lies on a whole pixel above the baseline (Font#gridShift). // A glyph's origin is a whole device pixel (positionGlyphs), so an outline // that reaches the ascender at a fraction — Ahem's at 16px reaches it at // 12.8 — covered part of its top row, where a box beside it, drawn to the // same edge, filled the row whole: react-x11-components' failed some // sixty of WPT's CSS 2.1 white-space tests by that one row. import assert from 'node:assert/strict'; import { readFileSync } from 'node:fs'; import { createRequire } from 'node:module'; import { dirname, join } from 'node:path'; import { test } from 'node:test'; import Font from '../lib/text/font.js'; const require = createRequire(import.meta.url); const fontDir = join(dirname(require.resolve('katex/package.json')), 'dist', 'fonts'); const font = Font.fromData(readFileSync(join(fontDir, 'KaTeX_Main-Regular.ttf'))); test('the ascender lands on a whole pixel, the outline moved by no more than half of one', () => { for (let size = 8; size <= 40; size += 0.25) { const ascent = font.metrics(size).ascent; const k = font.gridShift(size); assert.ok(Math.abs(k) <= 0.5, `${size}: ${k}`); assert.ok(Math.abs(ascent + k - Math.round(ascent + k)) < 1e-9, `${size}: ${ascent + k}`); } }); test("a glyph's raster is moved, its outline is not, and its extents hold both", () => { const id = font.fk.glyphForCodePoint(0x48).id; // H for (const size of [13, 16, 16.5, 21]) { const k = font.gridShift(size); const unmoved = -font.fk.getGlyph(id).cbox.maxY * font.scale(size); let top = Infinity; for (const poly of font.outline(id, size)) { for (let i = 1; i < poly.length; i += 2) top = Math.min(top, poly[i]); } assert.ok(Math.abs(top - unmoved) < 1e-9, `${size}: the outline, ${top} ${unmoved}`); const raster = font.rasterize(id, size); assert.equal(raster.top, Math.floor(unmoved - k), `${size}: the raster's top row`); const extents = font.glyphExtents(id, size); assert.ok(extents.minY <= Math.min(unmoved, unmoved - k) + 1e-9, `${size}: extents`); } });