Engineering Journal
Pdf Processor
Pdf Processor

Four Fixes Between 'Tests Pass' and 'The Formula Is Right'

2026-07-19

TLDR

Unit tests for ported C++ PDFium math heuristics passed initially but failed on real-world documents due to hidden input assumptions. Oversets were consumed as superscripts, fixed $2\text{pt}$ tolerances rejected $4\text{pt}$ exponent overhangs, paragraph joiners merged math blocks into prose, and alignment detectors failed on single-line box limits. Remedies required scale-relative tolerances and explicit container reference frames.
Defect PatternFailure MechanismEngineering Fix
1. Overset Script LoopOverset annotations consumed as scriptsX-guard: Check if atom starts inside operand X-range
2. Fixed $2\text{pt}$ WindowRejected $4\text{pt}$ exponent overhangsScale slack dynamically: max(2, 0.8 * fontPt)
3. Paragraph FusionMath blocks merged into prose <p>Hard fence: mathEdge creates forced paragraph breaks
4. Tight Box AlignmentSingle-line box geometry identical for left/centerUnion frame: Measure alignment against column frame

Technical remediation patterns

// Fix 1: X-guard distinguishing trailing scripts from overlapping oversets
while (i < atoms.length && scriptLevel(atoms[i]) !== 0 &&
       atoms[i].left >= baseOperand.right - 0.5 * atoms[i].size) {
  // Process trailing script
}

// Fix 2 & 3: Dynamic font-scaled slack and paragraph math boundaries export function evaluateParagraphBreak(prevLine, currentLine, verticalGapPx, paraThresholdPx) { const isMathEdge = !!currentLine.isMath !== !!prevLine.isMath; return verticalGapPx > paraThresholdPx || prevLine.hasHardBreak || isMathEdge; }

Rule of thumb: Scale geometric tolerance windows dynamically by item font size (0.8 * fontSize), treating math blocks as hard boundaries for paragraph joiners.
Read this post in the full Engineering Journal →