Skip to content

Build your personal knowledge base with Trilium Notes

fix(icons): draw a glyph where the stylesheet would, not where it measures

Elian Doran7019a53619f2
diff --git a/apps/client/src/services/icon_glyphs.spec.ts b/apps/client/src/services/icon_glyphs.spec.ts
index 81c8e1b1d9..182abf34fa 100644
--- a/apps/client/src/services/icon_glyphs.spec.ts
+++ b/apps/client/src/services/icon_glyphs.spec.ts
@@ -1,8 +1,7 @@
import { afterEach, describe, expect, it, vi } from "vitest";
import {
- drawIconGlyph, type IconGlyph, loadIconFonts, placeGlyph, renderIconImage, resolveIconGlyphs,
- warmIconFonts
+ drawIconGlyph, type IconGlyph, loadIconFonts, renderIconImage, resolveIconGlyphs, warmIconFonts
} from "./icon_glyphs";
const uncheckedWindow = window as unknown as { glob: { iconRegistry?: unknown } };
@@ -162,38 +161,6 @@ describe("warmIconFonts", () => {
});
});
-describe("placeGlyph", () => {
- it("centres the ink of the glyph rather than the line box it was written on", () => {
- const metrics = {
- actualBoundingBoxLeft: 0,
- actualBoundingBoxRight: 16,
- actualBoundingBoxAscent: 14,
- actualBoundingBoxDescent: 2
- } as TextMetrics;
-
- // 16 wide and 16 tall in a square of 20, so 2 of slack on each side; the baseline sits the
- // ascent below that.
- expect(placeGlyph(metrics, 20)).toEqual({
- x: 2, y: 16, textAlign: "start", textBaseline: "alphabetic"
- });
- });
-
- it("falls back to centring the square where the ink cannot be measured", () => {
- const unmeasured = [
- { actualBoundingBoxLeft: 0, actualBoundingBoxRight: 0, actualBoundingBoxAscent: 0, actualBoundingBoxDescent: 0 },
- // A browser that carries no such measurements at all, and one that carries nonsense.
- {},
- { actualBoundingBoxLeft: 0, actualBoundingBoxRight: Infinity, actualBoundingBoxAscent: 8, actualBoundingBoxDescent: 0 }
- ];
-
- for (const metrics of unmeasured) {
- expect(placeGlyph(metrics as TextMetrics, 20)).toEqual({
- x: 10, y: 10, textAlign: "center", textBaseline: "middle"
- });
- }
- });
-});
-
describe("drawIconGlyph", () => {
const glyph: IconGlyph = { fontFamily: "boxicons", content: FILE_GLYPH };
@@ -209,7 +176,13 @@ describe("drawIconGlyph", () => {
expect(context.scale).toHaveBeenCalledWith(3, 3);
expect(context.font).toBe("20px boxicons");
expect(context.fillStyle).toBe("red");
- expect(context.fillText).toHaveBeenCalledWith(FILE_GLYPH, 2, 16);
+ // The glyph's own box in the middle of the square, and nothing measured to get there:
+ // browsers do not answer `actualBoundingBox*` alike, and centring the ink on it drew the
+ // same icon pixels apart between them.
+ expect(context.measureText).not.toHaveBeenCalled();
+ expect(context.fillText).toHaveBeenCalledWith(FILE_GLYPH, 10, 10);
+ expect(context.textAlign).toBe("center");
+ expect(context.textBaseline).toBe("middle");
});
it("says so where there is no canvas to draw on", () => {
diff --git a/apps/client/src/services/icon_glyphs.ts b/apps/client/src/services/icon_glyphs.ts
index d49ead3969..8ac2338413 100644
--- a/apps/client/src/services/icon_glyphs.ts
+++ b/apps/client/src/services/icon_glyphs.ts
@@ -197,47 +197,16 @@ export function drawIconGlyph(glyph: IconGlyph, { size, color, scale }: Required
context.font = `${size}px ${glyph.fontFamily}`;
context.fillStyle = color;
- const placement = placeGlyph(context.measureText(glyph.content), size);
- context.textAlign = placement.textAlign;
- context.textBaseline = placement.textBaseline;
- context.fillText(glyph.content, placement.x, placement.y);
+ // The glyph's own box goes in the middle, which is where the stylesheet puts it everywhere else
+ // it is drawn. Measuring the ink and centring that instead is not portable: browsers do not
+ // answer `actualBoundingBox*` alike, and the same icon came out pixels apart between them.
+ context.textAlign = "center";
+ context.textBaseline = "middle";
+ context.fillText(glyph.content, size / 2, size / 2);
return canvas.toDataURL();
}
-/**
- * Where a glyph of the given measurements is written for its ink to sit in the middle of the square.
- *
- * An icon font fills its own line box as it pleases — the character is a drawing, not a letter, and
- * nothing holds it to the shape of one — so a glyph written on the middle line of the square lands
- * wherever the pack happened to draw it. Measuring the ink itself and centring that instead is what
- * keeps a row of icons from wandering up and down.
- *
- * Where the browser will not say how far the ink reaches, the glyph is centred on the square as a
- * letter would be, which is right often enough and is all there is to go on.
- */
-export function placeGlyph(metrics: TextMetrics, size: number) {
- const left = metrics.actualBoundingBoxLeft;
- const right = metrics.actualBoundingBoxRight;
- const ascent = metrics.actualBoundingBoxAscent;
- const descent = metrics.actualBoundingBoxDescent;
-
- const width = left + right;
- const height = ascent + descent;
- if (!(width > 0) || !(height > 0) || !Number.isFinite(width) || !Number.isFinite(height)) {
- return { x: size / 2, y: size / 2, textAlign: "center", textBaseline: "middle" } as const;
- }
-
- // The ink runs from `x - left` to `x + right`, and is centred when the first of those is at half
- // of what the square has left over. The same holds of the ascent going up from the baseline.
- return {
- x: (size - width) / 2 + left,
- y: (size - height) / 2 + ascent,
- textAlign: "start",
- textBaseline: "alphabetic"
- } as const;
-}
-
/** The display's density, as a number a drawing can be scaled by even where there is no window. */
function devicePixelRatio() {
return window.devicePixelRatio || 1;