feat(schema): text autofit via slot metrics (options: ["autofit"]) - #100
Merged
Merged
Conversation
Closes #92 (phase 1: scale factor emission). Schema FieldSpec gains slot_width / slot_height / font_size / line_height. When a text field opts in via options ["autofit"] and declares slot metrics, the context gets <field>_font_scale (0.1..1.0): the factor to multiply the template's font-size so the wrapped text fits the slot. Width estimate = longest wrapped line x font-size x 0.52 (Inter-like average advance); height = lines x line_height. line_height defaults to font-size x 1.3. Templates opt in per field by multiplying their static font-size with {{ field_font_scale }} (rounding to int). Without the autofit option or slot metrics, nothing changes (backward compatible: default renders md5-identical, full suite passes). Autofit validation for social-quote (slot 920x616, font 52/68): a 280-char quote scales to 9 lines x reduced size and stays above the author line without overlap.
🔍 Cora AI Code Review✅ No issues found. Code looks good! Review powered by cora-code · BYOK · MIT |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Closes #92 — text autofit, phase 1 (scale factor emission).
FieldSpecgains optional slot metrics (slot_width,slot_height,font_size,line_height). A text field that opts in viaoptions: ["autofit"]and declares those metrics gets a<field>_font_scalecontext variable (0.1–1.0): the factor thetemplate multiplies its static font-size with so the wrapped text fits
the slot.
average advance width) vs
slot_widthslot_heightline_heightdefaults to font-size × 1.3; scale clamped to 0.1–1.0font-size="{{ (52.0 * quote_font_scale) | round | int }}"— or keepthe static size and ignore the variable entirely
No schema change is required for existing templates: without the
autofitoption + slot metrics, nothing is emitted and renders areunchanged.
Why
Shrink-to-fit is the single most-requested feature across commercial
image-generation APIs (Bannerbear makes it a per-layer setting; Robolly
and Placid ship equivalents). Cosy's char-based wrap + max-length
validation can't guarantee visual fit because glyph widths differ.
This gives template authors a deterministic, dependency-free escape
hatch while keeping full backward compatibility.
Testing
cargo test11/11 suites pass; clippy-D warningscleanpre-change baseline
quote triggers a scale < 1.0, reflows to 9 lines and clears the
author line (no overlap, vision-verified); short default quote stays
at scale 1.0
the bundled templates that benefit most (social-quote, quote-stack),
and a deterministic quality-gate fixture asserting max-length content
never overflows