Issue
A Python-based text rendering system for video content applied a fixed line height (90px) to all rendered text, regardless of content type. This created two primary problems:
- Excessive spacing: Regular text (43px font) used a line height of 90px, resulting in unnaturally large gaps between lines.
- Lack of visual hierarchy: Code blocks and explanatory text shared identical spacing, eliminating any visual distinction between different content types.
User feedback highlighted the unnatural reading experience, particularly noting that text sections appeared overly spaced and transitions between text and code lacked clear separation.
Culprit
The root cause was a single hardcoded line height calculation that ignored the context of the content being rendered:
- The line height was calculated as
code_font_size * 1.4, producing a fixed 90px line height regardless of whether the content was code or regular text. - This approach forced all content types to adhere to a spacing ratio derived from code, resulting in inconsistent and visually unbalanced output.
- The system did not differentiate between code and text spacing ratios or account for transitions between different content types.
Solution
A three-tier adaptive spacing system was implemented to dynamically adjust line heights based on content type and transitions:
1. Content-Type Specific Ratios
- Code content was assigned a line height ratio of 1.5x the font size (e.g., 64px font × 1.5 = 96px line height).
- Regular text was assigned a line height ratio of 1.25x the font size (e.g., 43px font × 1.25 = 54px line height).
This reduced text spacing by 40% (from 90px to 54px) and improved code spacing slightly (from 90px to 96px), enhancing readability for both content types.
2. Transition Detection
A transition detection system was introduced to apply additional spacing when switching between content types:
- Text-to-Code and Code-to-Text transitions were assigned a 1.5x multiplier to the line height, providing clear visual separation between different content sections.
- This ensured that transitions between paragraphs, code blocks, and other content types were visually distinct and improved the overall reading experience.
3. Smart Wrapping Integration
The transition logic was extended to handle word-wrapped lines, ensuring consistent spacing adjustments for both regular and wrapped text.
The solution maintains backward compatibility while automatically adapting to changes in font size, providing a more visually balanced and intuitive rendering experience.