Exercise 3: Scaling at a Non-Doubling Font-Size — Possible Solution ==================================================================== THE TEST ------------------------------ w16 = measure_word('Welcome', 16.0) w24 = measure_word('Welcome', 24.0) RESULT ------------------------------ w16 -> 61.12 w24 -> 91.68 91.68 is exactly 1.5 times 61.12 (61.12 * 1.5 = 91.68). WHY THIS IS EXACTLY WHAT THE EM-BASED MODEL PREDICTS ------------------------------ measure_word sums, for every character in the word, char_width_px(ch, font_size_px) = CHAR_WIDTHS_EM[ch] * font_size_px. Summing across all seven characters of 'Welcome': sum_of_em_values = W(0.94) + e(0.44) + l(0.28) + c(0.44) + o(0.50) + m(0.78) + e(0.44) = 3.82 (a fixed number, independent of font-size) measure_word('Welcome', font_size_px) is therefore always exactly 3.82 * font_size_px, for ANY font_size_px: at 16px: 3.82 * 16 = 61.12 at 24px: 3.82 * 24 = 91.68 Since 24 is exactly 1.5 times 16, and the whole expression is a straight multiplication by font_size_px with no other font-size- dependent term anywhere, 3.82*24 is necessarily exactly 1.5 times 3.82*16 -- this isn't a coincidence discovered by testing, it's a direct algebraic consequence of the formula's own shape. WHY THIS CONFIRMS GENUINE LINEARITY, NOT A 2X SPECIAL CASE ------------------------------ The chapter's own main demonstration used 16px vs. 32px -- a clean doubling, which could in principle be produced by a model that only handles font-size correctly for round-number multiples (e.g., a lookup table keyed by a small set of common sizes, or code with a special case for "2x" specifically). Testing at 24px -- a genuinely different, non-doubling multiple (1.5x) -- and finding the SAME proportional relationship holds rules that possibility out. Because sum_of_em_values is computed once, independent of font_size_px, and then multiplied by whatever font_size_px happens to be, the model is provably linear in font-size for ANY value, not just the specific ones already tested -- a font-size of, say, 20px or 13px would work exactly the same way, following directly from the same formula rather than needing to be separately verified case by case. WHY THIS WORKS AS AN ANSWER ------------------------------ This is the mathematical guarantee that makes the em-based approach genuinely superior to a naive lookup table keyed by absolute pixel sizes: defining widths as a RATIO of the font's own size, once, automatically produces correct measurements at every font-size a real page might use, without needing a separate table entry -- or a separate test -- for each one.