|
| 1 | +const DEFAULT_LINE_HEIGHTS = [1, 1.05, 1.1] |
| 2 | +const DEFAULT_LETTER_SPACINGS = [0] |
| 3 | +const DEFAULT_FONT_SIZE_RADIUS = 1.5 |
| 4 | +const DEFAULT_FONT_SIZE_STEP = 0.25 |
| 5 | + |
| 6 | +function uniqueSortedNumbers(values) { |
| 7 | + return [ |
| 8 | + ...new Set(values.filter(value => Number.isFinite(value)).map(value => round(value, 3))), |
| 9 | + ].sort((left, right) => left - right) |
| 10 | +} |
| 11 | + |
| 12 | +function round(value, decimals = 2) { |
| 13 | + const factor = 10 ** decimals |
| 14 | + return Math.round(value * factor) / factor |
| 15 | +} |
| 16 | + |
| 17 | +function buildCenteredRange(center, radius, step) { |
| 18 | + if (!Number.isFinite(center) || center <= 0) { |
| 19 | + return [] |
| 20 | + } |
| 21 | + |
| 22 | + const safeRadius = Number.isFinite(radius) && radius >= 0 ? radius : DEFAULT_FONT_SIZE_RADIUS |
| 23 | + const safeStep = Number.isFinite(step) && step > 0 ? step : DEFAULT_FONT_SIZE_STEP |
| 24 | + const start = center - safeRadius |
| 25 | + const end = center + safeRadius |
| 26 | + const values = [] |
| 27 | + |
| 28 | + for (let value = start; value <= end + safeStep / 2; value += safeStep) { |
| 29 | + if (value > 0) { |
| 30 | + values.push(value) |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + return uniqueSortedNumbers(values) |
| 35 | +} |
| 36 | + |
| 37 | +function readPositiveNumber(value) { |
| 38 | + return typeof value === 'number' && Number.isFinite(value) && value > 0 ? value : null |
| 39 | +} |
| 40 | + |
| 41 | +function readFiniteNumber(value) { |
| 42 | + return typeof value === 'number' && Number.isFinite(value) ? value : null |
| 43 | +} |
| 44 | + |
| 45 | +function createDelta(left, right) { |
| 46 | + if (left === null || right === null) { |
| 47 | + return null |
| 48 | + } |
| 49 | + |
| 50 | + return round(left - right, 4) |
| 51 | +} |
| 52 | + |
| 53 | +function normalizeMeasurement(input, geometrySource) { |
| 54 | + const size = geometrySource === 'proposed' ? input.proposedGeometry : input.size |
| 55 | + const fallbackSize = geometrySource === 'proposed' ? input.size : input.proposedGeometry |
| 56 | + const resolvedSize = size ?? fallbackSize ?? null |
| 57 | + const renderMetrics = input.renderMetrics ?? {} |
| 58 | + |
| 59 | + return { |
| 60 | + cols: readPositiveNumber(resolvedSize?.cols), |
| 61 | + rows: readPositiveNumber(resolvedSize?.rows), |
| 62 | + cssCellWidth: readPositiveNumber(renderMetrics.cssCellWidth), |
| 63 | + cssCellHeight: readPositiveNumber(renderMetrics.cssCellHeight), |
| 64 | + effectiveDpr: readPositiveNumber(renderMetrics.effectiveDpr), |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +function scoreDeltas(deltas) { |
| 69 | + const missingPenalty = 1_000_000 |
| 70 | + const colsPenalty = deltas.cols === null ? missingPenalty : Math.abs(deltas.cols) * 1_000 |
| 71 | + const rowsPenalty = deltas.rows === null ? missingPenalty : Math.abs(deltas.rows) * 1_000 |
| 72 | + const cellWidthPenalty = |
| 73 | + deltas.cssCellWidth === null ? missingPenalty : Math.abs(deltas.cssCellWidth) * 100 |
| 74 | + const cellHeightPenalty = |
| 75 | + deltas.cssCellHeight === null ? missingPenalty : Math.abs(deltas.cssCellHeight) * 100 |
| 76 | + return round(colsPenalty + rowsPenalty + cellWidthPenalty + cellHeightPenalty, 4) |
| 77 | +} |
| 78 | + |
| 79 | +function createPreferenceDistance(candidate, preferredCandidate) { |
| 80 | + if (!preferredCandidate) { |
| 81 | + return 0 |
| 82 | + } |
| 83 | + |
| 84 | + const fontSizeDistance = Math.abs( |
| 85 | + (readFiniteNumber(candidate?.fontSize) ?? 0) - |
| 86 | + (readFiniteNumber(preferredCandidate.fontSize) ?? 0), |
| 87 | + ) |
| 88 | + const lineHeightDistance = Math.abs( |
| 89 | + (readFiniteNumber(candidate?.lineHeight) ?? 0) - |
| 90 | + (readFiniteNumber(preferredCandidate.lineHeight) ?? 0), |
| 91 | + ) |
| 92 | + const letterSpacingDistance = Math.abs( |
| 93 | + (readFiniteNumber(candidate?.letterSpacing) ?? 0) - |
| 94 | + (readFiniteNumber(preferredCandidate.letterSpacing) ?? 0), |
| 95 | + ) |
| 96 | + |
| 97 | + return round(fontSizeDistance + lineHeightDistance * 10 + letterSpacingDistance, 4) |
| 98 | +} |
| 99 | + |
| 100 | +function readPreferenceDistance(result) { |
| 101 | + return readFiniteNumber(result.preferenceDistance) ?? 0 |
| 102 | +} |
| 103 | + |
| 104 | +export function buildTerminalDisplayCalibrationCandidates({ |
| 105 | + baseFontSize, |
| 106 | + fontSizes, |
| 107 | + lineHeights, |
| 108 | + letterSpacings, |
| 109 | + fontSizeRadius = DEFAULT_FONT_SIZE_RADIUS, |
| 110 | + fontSizeStep = DEFAULT_FONT_SIZE_STEP, |
| 111 | +} = {}) { |
| 112 | + const resolvedFontSizes = |
| 113 | + Array.isArray(fontSizes) && fontSizes.length > 0 |
| 114 | + ? uniqueSortedNumbers(fontSizes) |
| 115 | + : buildCenteredRange(baseFontSize, fontSizeRadius, fontSizeStep) |
| 116 | + const resolvedLineHeights = |
| 117 | + Array.isArray(lineHeights) && lineHeights.length > 0 |
| 118 | + ? uniqueSortedNumbers(lineHeights) |
| 119 | + : DEFAULT_LINE_HEIGHTS |
| 120 | + const resolvedLetterSpacings = |
| 121 | + Array.isArray(letterSpacings) && letterSpacings.length > 0 |
| 122 | + ? uniqueSortedNumbers(letterSpacings) |
| 123 | + : DEFAULT_LETTER_SPACINGS |
| 124 | + const candidates = [] |
| 125 | + |
| 126 | + for (const fontSize of resolvedFontSizes) { |
| 127 | + for (const lineHeight of resolvedLineHeights) { |
| 128 | + for (const letterSpacing of resolvedLetterSpacings) { |
| 129 | + candidates.push({ fontSize, lineHeight, letterSpacing }) |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + return candidates |
| 135 | +} |
| 136 | + |
| 137 | +export function scoreTerminalDisplayCalibrationCandidate({ |
| 138 | + targetMetrics, |
| 139 | + candidateMetrics, |
| 140 | + candidate, |
| 141 | + preferredCandidate, |
| 142 | +}) { |
| 143 | + const target = normalizeMeasurement(targetMetrics, 'size') |
| 144 | + const measurement = normalizeMeasurement(candidateMetrics, 'proposed') |
| 145 | + const deltas = { |
| 146 | + cols: createDelta(measurement.cols, target.cols), |
| 147 | + rows: createDelta(measurement.rows, target.rows), |
| 148 | + cssCellWidth: createDelta(measurement.cssCellWidth, target.cssCellWidth), |
| 149 | + cssCellHeight: createDelta(measurement.cssCellHeight, target.cssCellHeight), |
| 150 | + effectiveDpr: createDelta(measurement.effectiveDpr, target.effectiveDpr), |
| 151 | + } |
| 152 | + const score = scoreDeltas(deltas) |
| 153 | + |
| 154 | + return { |
| 155 | + candidate, |
| 156 | + score, |
| 157 | + preferenceDistance: createPreferenceDistance(candidate, preferredCandidate), |
| 158 | + target, |
| 159 | + measurement, |
| 160 | + deltas, |
| 161 | + exactGeometry: deltas.cols === 0 && deltas.rows === 0, |
| 162 | + exactCellMetrics: deltas.cssCellWidth === 0 && deltas.cssCellHeight === 0, |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +export function rankTerminalDisplayCalibrationCandidates(results) { |
| 167 | + return [...results].sort((left, right) => { |
| 168 | + if (left.score !== right.score) { |
| 169 | + return left.score - right.score |
| 170 | + } |
| 171 | + |
| 172 | + if (readPreferenceDistance(left) !== readPreferenceDistance(right)) { |
| 173 | + return readPreferenceDistance(left) - readPreferenceDistance(right) |
| 174 | + } |
| 175 | + |
| 176 | + const leftFontSize = readFiniteNumber(left.candidate?.fontSize) ?? Number.POSITIVE_INFINITY |
| 177 | + const rightFontSize = readFiniteNumber(right.candidate?.fontSize) ?? Number.POSITIVE_INFINITY |
| 178 | + return leftFontSize - rightFontSize |
| 179 | + }) |
| 180 | +} |
| 181 | + |
| 182 | +export function summarizeTerminalDisplayCalibration(results, limit = 5) { |
| 183 | + const ranked = rankTerminalDisplayCalibrationCandidates(results) |
| 184 | + const best = ranked[0] ?? null |
| 185 | + return { |
| 186 | + best, |
| 187 | + topCandidates: ranked.slice(0, limit), |
| 188 | + candidateCount: results.length, |
| 189 | + } |
| 190 | +} |
| 191 | + |
| 192 | +export function compareTerminalDisplayMetrics(desktop, web) { |
| 193 | + const desktopRender = desktop.renderMetrics ?? {} |
| 194 | + const webRender = web.renderMetrics ?? {} |
| 195 | + return { |
| 196 | + colsDelta: (web.size?.cols ?? 0) - (desktop.size?.cols ?? 0), |
| 197 | + rowsDelta: (web.size?.rows ?? 0) - (desktop.size?.rows ?? 0), |
| 198 | + cssCellWidthDelta: (webRender.cssCellWidth ?? 0) - (desktopRender.cssCellWidth ?? 0), |
| 199 | + cssCellHeightDelta: (webRender.cssCellHeight ?? 0) - (desktopRender.cssCellHeight ?? 0), |
| 200 | + effectiveDprDelta: (webRender.effectiveDpr ?? 0) - (desktopRender.effectiveDpr ?? 0), |
| 201 | + } |
| 202 | +} |
| 203 | + |
| 204 | +export function isTerminalDisplayParity(comparison) { |
| 205 | + return ( |
| 206 | + comparison.colsDelta === 0 && |
| 207 | + comparison.rowsDelta === 0 && |
| 208 | + Math.abs(comparison.cssCellWidthDelta) <= 0.05 && |
| 209 | + Math.abs(comparison.cssCellHeightDelta) <= 0.05 |
| 210 | + ) |
| 211 | +} |
0 commit comments