The fallback that reported itself as a measurement
TLDR
A line-spacing calibration module returned a hardcoded fallback value ($12\text{pt}$) when measurement failed, reporting the fallback through the exact same output channel as valid measurements (leadingPx). The fallback value was also in font points while downstream consumers expected viewport screen pixels ($24\text{px}$). Downstream threshold math miscalculated paragraph line gaps, splitting every sentence into an isolated paragraph. Returning null on calibration failure fixed the issue immediately.
| Calibration Strategy | Failed Measurement Return | Downstream Threshold Calculation | Extracted HTML Result |
|---|---|---|---|
| Hardcoded Fallback Value | 12 (Points, disguised as measurement) | $12 \times 0.45 = 21.6\text{px}$ gap cutoff | Paragraphs split into single lines |
Explicit null Return | null (Explicit failure signal) | Page-local font fallback ($39\text{px}$ cutoff) | Clean, correctly joined paragraphs |
Technical failure mode analysis
Extracted HTML output rendered every line of body copy as its own isolated <p> block:
<!-- DEFECTIVE OUTPUT: Lines split into separate paragraphs -->
<p>We introduce a new language representa-</p>
<p>tion model called BERT, which stands for</p>
The compounding defects
// DEFECTIVE IMPLEMENTATION: Returning hardcoded prior on failure
export function calibrateLineSpacing(deltas, prior = 12) {
if (deltas.length < MIN_SAMPLES) {
// BUG 1: Returns prior (12pt) through measurement property linePitchPx!
// BUG 2: Unit mismatch - prior is points (12), downstream expects viewport pixels (~24)
return { calibrated: true, linePitchPx: prior };
}
const mode = calculateHistogramMode(deltas);
// BUG 3: Plausibility guard compares point prior against pixel deltas if (mode < prior 0.3 || mode > prior 5) { return { calibrated: true, linePitchPx: prior }; // Disguised fallback! }
return { calibrated: true, linePitchPx: mode }; }
Because calibrateLineSpacing returned $12$, the paragraph gap threshold was calculated as:
$$\text{Threshold} = 12 \times 1.8 = 21.6\text{px}$$
The document's true line pitch was $24\text{px}$. Because $24\text{px} > 21.6\text{px}$, every standard line break exceeded the threshold and was split into a separate paragraph block.
Remediation: explicit null return signals
Remove hardcoded fallback values and return null when statistical calibration fails, allowing consumers to fall back to validated page-local defaults:
// REFACTORED: Explicit null returns on measurement failure
export function calibrateLineSpacing(deltas, priorPt = 12, viewportScale = 1.0) {
if (deltas.length < MIN_SAMPLES) {
return null; // Explicit failure: caller uses page defaults
}
const modePx = calculateHistogramMode(deltas); const expectedPriorPx = priorPt * viewportScale;
// Plausibility check in matching pixel units if (modePx < expectedPriorPx 0.5 || modePx > expectedPriorPx 2.5) { return null; // Explicit failure }
return refineByMedian(deltas, modePx); }
Downstream consumers check for null:
const calibratedPitch = calibrateLineSpacing(deltas, fontPt, scale);
// Use calibrated value if present; otherwise fall back to local page metrics
const finalLinePitch = calibratedPitch ?? computeLocalPageFallback(pageMeta);
Rule of thumb: Never return hardcoded fallback guesses through measurement channels. Return null on failure so callers handle fallbacks explicitly.