Prompt
How do I detect spacing and font rendering differences automatically?
Latest observation
A good way to detect spacing and font rendering differences automatically is to compare rendered output, not the raw HTML/CSS/text, because many differences only show up after layout and font shaping.
Best approaches
1. Pixel diff on screenshots
Render the same page/component in two environments and compare images.
Pipeline:
- Open page in a browser.
- Wait for fonts to load.
- Capture screenshot.
- Compare screenshots with an image diff tool.
Tools:
- Playwright / Puppeteer
- Percy / Chromatic / Applitools
pixelmatch,looks-same,jest-image-snapshot
Good for detecting:
- Font fallback differences
- Kerning/ligature changes
- Line-height/letter-spacing shifts
- Layout shifts caused by font metrics
Tips to reduce noise:
- Disable animations
- Use fixed viewport/device scale factor
- Set consistent rendering flags
- Use the same font files across machines if possible
- Wait for
document.fonts.ready
2. DOM/layout measurement diff
If you want to detect spacing changes more structurally, measure element geometry and compare values.
Examples:
getBoundingClientRect()- computed
line-height,letter-spacing,font-size - text width via canvas/text measurement
- element positions and sizes
Good for detecting:
- Changes in spacing between elements
- Text reflow differences
- Font metric changes that affect layout
Limitations:
- Won’t catch all glyph-shape differences
- Harder to detect subtle font rendering changes
3. Text rendering comparison with OCR/shape analysis
For cases where visual appearance matters but pixel diffs are too noisy, you can:
- render text to an image
- crop text regions
- compare with perceptual similarity metrics (SSIM, perceptual hash)
- optionally run OCR to verify text remains legible
This is more complex but useful if antialiasing or subpixel rendering causes small pixel changes.
Recommended practical setup
For most teams, the best solution is:
- Use Playwright
- Capture screenshots of key pages/components
- Compare with pixelmatch or a visual testing service
- Ensure fonts are loaded before capture
- Add a small diff threshold to ignore tiny antialiasing noise
Example in Playwright:
await page.goto('http://localhost:3000');
await page.evaluate(() => document.fonts.ready);
await expect(page).toHaveScreenshot('home.png', {
maxDiffPixelRatio: 0.01,
});
If you care specifically about font rendering
Include tests that:
- render the same sample text in multiple weights/sizes
- use characters that expose metric differences:
AVATAR,ffi,WMWM,il1,O0,gypq
- test across browsers/OSes if your app supports them
If you want a robust automated system
Use a layered approach:
- Layout assertions for spacing/position changes
- Screenshot diffs for visual differences
- Cross-browser runs to catch font fallback/rendering issues
If you want, I can give you:
- a Playwright + pixelmatch example
- a browser-agnostic layout diff script
- or a CI setup for automated font rendering checks.