Files
apparel-designer/src/constants/textMetrics.js
2026-05-23 03:28:58 -05:00

55 lines
2.8 KiB
JavaScript

/**
* Text-rendering constants shared across measurement, bounds, and render code.
*
* Before this module: `1.2`, `12`, and similar magic numbers were repeated
* across `textGeometry.js`, `elementBounds.js`, and `TextElement.jsx`. If
* Konva's defaults ever changed (or the design system picked a different
* line-height ratio), every site had to be updated in lockstep — and there
* was no way to grep for "the line-height ratio" since it was just `1.2`.
*
* Centralizing here keeps those callers honest. New text-related constants
* that show up in 2+ places should land here.
*
* Note on per-component font-family defaults
* ──────────────────────────────────────────
* `TextElement.jsx` defaults `fontFamily = 'DM Sans'` (the body face) for
* its prop. `textGeometry.js` defaulted to `'Pacifico'` (the display face)
* for its measurement-fallback path. Those are intentionally different —
* an element rendered without a fontFamily uses DM Sans, but the
* measurement helpers fall back to the display face that matches new-text
* placement defaults. We keep that distinction by exposing both names
* here and letting each call site pick the one that matches its semantics.
*/
// Konva's default line-height ratio. Multiply by fontSize to get the
// rendered height of a single text line.
export const LINE_HEIGHT_RATIO = 1.2;
// Floor for fontSize during scale-down operations. Below this, text becomes
// hard to read and the rounded display in the Text tab starts losing
// precision (12 → 11 → 10 each cost ~10% of legibility).
export const MIN_FONT_SIZE = 12;
// Ceiling for auto-computed fontSize values (e.g. width-preservation
// when swapping to a narrower font). The Text tab's slider tops out at
// 120 for typical fine-tuning, but width-preservation can legitimately
// land above that when going from a wide display face to a very
// condensed one. 200 gives plenty of headroom (about 2/3 of the
// 300-design-unit canvas height) while preventing pathological growth
// on a font whose measureText returns near-zero for a particular glyph.
// The user can still corner-drag the text on canvas past this value;
// the cap only governs the auto-preserve math.
export const MAX_FONT_SIZE = 200;
// Default fontSize when an element doesn't specify one. Matches the
// historical TEXT default in textGeometry.placeTextCentered.
export const DEFAULT_FONT_SIZE = 32;
// Display face used by the freeform "Add text" button and as the
// measurement fallback when an element has no explicit fontFamily.
export const DEFAULT_DISPLAY_FONT = 'Pacifico';
// Body face used by TextElement's prop default (so Konva renders something
// reasonable when the prop is omitted).
export const DEFAULT_BODY_FONT = 'DM Sans';