Raxol.Core.Performance.Caches.FontMetricsCache (Raxol v2.0.1)
View SourceHigh-performance cache for font metrics calculations.
This module caches font-related measurements to avoid repeated calculations of character widths, string widths, and font dimensions. These operations are frequently called during rendering and can be expensive, especially for complex Unicode characters and wide character sets.
Features
- Caches character width calculations
- Caches string width measurements
- Caches font dimension calculations
- Uses ETS cache manager for high-performance access
- Telemetry instrumentation for monitoring
Performance Impact
Expected improvements:
- 40-60% reduction in font metric calculation overhead
- Sub-microsecond access for cached values
- Minimal memory footprint with LRU eviction
Summary
Functions
Calculates the pixel height for a given number of lines using cached font metrics.
Calculates the pixel width for a given number of characters using cached font metrics.
Calculates the number of characters that fit in a given pixel width.
Gets the character width from cache or calculates and caches it.
Gets font dimensions from cache or calculates and caches them. Returns {char_width_px, char_height_px}.
Gets the font stack from cache or builds and caches it.
Gets the string width from cache or calculates and caches it.
Calculates the number of lines that fit in a given pixel height.
Warms up the cache with common values.
Functions
@spec calculate_pixel_height(Raxol.Terminal.Font.Manager.t(), non_neg_integer()) :: non_neg_integer()
Calculates the pixel height for a given number of lines using cached font metrics.
@spec calculate_pixel_width(Raxol.Terminal.Font.Manager.t(), non_neg_integer()) :: non_neg_integer()
Calculates the pixel width for a given number of characters using cached font metrics.
@spec chars_in_width(Raxol.Terminal.Font.Manager.t(), non_neg_integer()) :: non_neg_integer()
Calculates the number of characters that fit in a given pixel width.
Gets the character width from cache or calculates and caches it.
@spec get_font_dimensions(Raxol.Terminal.Font.Manager.t()) :: {non_neg_integer(), non_neg_integer()}
Gets font dimensions from cache or calculates and caches them. Returns {char_width_px, char_height_px}.
@spec get_font_stack(Raxol.Terminal.Font.Manager.t()) :: [String.t()]
Gets the font stack from cache or builds and caches it.
@spec get_string_width(String.t()) :: non_neg_integer()
Gets the string width from cache or calculates and caches it.
@spec lines_in_height(Raxol.Terminal.Font.Manager.t(), non_neg_integer()) :: non_neg_integer()
Calculates the number of lines that fit in a given pixel height.
@spec warmup() :: :ok
Warms up the cache with common values.