A markdown-to-video rendering system processed mixed content (code and text) with different font metrics, leading to inconsistent vertical spacing between code and text segments. The system used distinct font sizes for code (smaller) and text (larger), causing visual asymmetry in transitions.
Issue
The rendering system exhibited inconsistent vertical spacing between code-to-text and text-to-code transitions. Specifically:
- Code-to-text transitions had 9.6px less spacing than text-to-code transitions.
- This resulted in tighter spacing after code blocks (86.4px total) compared to roomier spacing before code blocks (96px total).
The inconsistency was visually noticeable and affected the overall quality of the rendered video.
Culprit
The spacing calculation relied on the preceding line’s font metrics:
- Text-to-code transitions: Used text font metrics (larger font size) for spacing, resulting in 96px total spacing.
- Code-to-text transitions: Used code font metrics (smaller font size) for spacing, resulting in 86.4px total spacing.
The system did not account for font size differences between content types during transition detection, leading to the asymmetry.
Solution
Rendering-Time Spacing Adjustment
A fix was implemented to dynamically adjust spacing during the rendering process:
- Transition detection: Tracked segment boundaries to identify code-to-text transitions.
- Text font baseline: Used text font metrics (38.4px × 1.25 line height) to ensure consistent spacing for all transitions.
- Segment-aware processing: Only applied spacing adjustments when transitioning from code to text, based on language type mismatches.
Design Choices
- Rendering-time solution: Avoids modifying the input markdown files, ensuring backward compatibility.
- Consistency: Ensured all transitions used text font metrics for spacing, eliminating visual asymmetry.
- Segment-based logic: Leveraged segment boundaries to accurately detect transitions between content types.
Result: Symmetric spacing (134.4px total) across all transitions, improving the visual consistency of the rendered output. The fix was verified through user confirmation and visual inspection of the rendered video.