Engineering Journal
Pdf Processor
Pdf Processor

Port the Invariant, Not the Code

2026-07-19

TLDR

Cross-language library ports (such as moving C++/PDFium layout algorithms to JavaScript/PDF.js Web Workers) fail when engineers transliterate syntax line-by-line across incompatible data structures. Successful ports extract invariant typographic thresholds ($\Delta Y > 0.34 \times \text{fontSize}$, font scaling ratios, stretch delimiter thresholds) while completely rewriting iteration mechanics to match the target runtime's data model.
Porting StrategyCode StructureInvariant FidelityCross-Library Viability
Line-by-Line TransliterationMirrored loops & variablesFragile (Breaks on data granularity)Low
Invariant TransplantationNative JS implementation100% Retained domain mathHigh

Core invariant specifications

Extract domain invariants into clean, framework-independent constants:

// Portable Typographic Math Invariants
export const MATH_INVARIANTS = {
  NUMERATOR_Y_OFFSET_RATIO: 0.34,   // dy > +0.34 * baseFontSize
  DENOMINATOR_Y_OFFSET_RATIO: 0.30, // dy < -0.30 * baseFontSize
  SCRIPT_FONT_SIZE_RATIO: 0.85,    // fontSize < 0.85 * baseFontSize
  STRETCH_DELIMITER_RATIO: 1.50     // delimiterSize >= 1.50 * medianFontSize
};
Rule of thumb: Document core mathematical and domain invariants separately from language runtime mechanics when porting algorithms between libraries.
Read this post in the full Engineering Journal →