dear_imgui/fonts/
glyph_ranges.rs

1#![allow(deprecated)]
2//! Glyph ranges for font loading
3//!
4//! **⚠️ DEPRECATED with Dear ImGui 1.92+**: With Dear ImGui 1.92+, glyph ranges are no longer needed
5//! for most use cases. The new dynamic font system loads glyphs on-demand automatically.
6//!
7//! This module is kept for backward compatibility and special use cases where you need
8//! to explicitly control which glyphs are loaded (e.g., for memory-constrained environments).
9//!
10//! **Recommended approach with Dear ImGui 1.92+**: Use `FontSource` without specifying glyph ranges.
11//! The system will automatically load any Unicode character as needed.
12//!
13//! This module provides utilities for specifying which character ranges
14//! should be loaded when adding fonts to the atlas.
15
16use crate::sys;
17
18/// Builder for creating custom glyph ranges
19///
20/// **⚠️ DEPRECATED with Dear ImGui 1.92+**: Consider using the new dynamic font system instead.
21///
22/// This allows you to specify exactly which characters should be loaded
23/// from a font, which can save memory and improve performance.
24///
25/// **Note**: With Dear ImGui 1.92+ dynamic fonts, this is only needed for special cases
26/// where you want to explicitly control memory usage or exclude certain ranges.
27#[derive(Debug)]
28#[deprecated(
29    since = "0.1.0",
30    note = "Use dynamic font loading instead. Glyphs are now loaded on-demand automatically with Dear ImGui 1.92+."
31)]
32pub struct GlyphRangesBuilder {
33    raw: *mut sys::ImFontGlyphRangesBuilder,
34}
35
36impl GlyphRangesBuilder {
37    /// Creates a new glyph ranges builder
38    pub fn new() -> Self {
39        unsafe {
40            Self {
41                raw: sys::ImFontGlyphRangesBuilder_ImFontGlyphRangesBuilder(),
42            }
43        }
44    }
45
46    /// Add text to the builder (all characters in the text will be included)
47    #[doc(alias = "AddText")]
48    pub fn add_text(&mut self, text: &str) {
49        unsafe {
50            let text_start = text.as_ptr() as *const std::os::raw::c_char;
51            let text_end = text_start.add(text.len());
52            sys::ImFontGlyphRangesBuilder_AddText(self.raw, text_start, text_end);
53        }
54    }
55
56    /// Add a range of characters
57    #[doc(alias = "AddRanges")]
58    pub fn add_ranges(&mut self, ranges: &[u32]) {
59        unsafe {
60            sys::ImFontGlyphRangesBuilder_AddRanges(
61                self.raw,
62                ranges.as_ptr() as *const sys::ImWchar,
63            );
64        }
65    }
66
67    /// Build the final ranges array
68    #[doc(alias = "BuildRanges")]
69    pub fn build_ranges(&mut self) -> Vec<u32> {
70        unsafe {
71            let mut out_ranges = std::mem::zeroed::<sys::ImVector_ImWchar>();
72            sys::ImFontGlyphRangesBuilder_BuildRanges(self.raw, &mut out_ranges);
73
74            // Convert ImVector to Vec
75            let len = out_ranges.Size as usize;
76            let mut result = Vec::with_capacity(len);
77            for i in 0..len {
78                result.push(*out_ranges.Data.add(i) as u32);
79            }
80            result
81        }
82    }
83}
84
85impl Default for GlyphRangesBuilder {
86    fn default() -> Self {
87        Self::new()
88    }
89}
90
91/// Predefined glyph ranges for common character sets
92///
93/// **Note**: These ranges are still useful with Dear ImGui 1.92+ for:
94/// - Memory-constrained environments where you want to limit loaded glyphs
95/// - Font merging with `glyph_exclude_ranges()` to prevent conflicts
96/// - Explicit control over which characters are available
97///
98/// For most use cases, you can now omit glyph ranges and let the dynamic
99/// font system load glyphs on-demand.
100pub struct GlyphRanges;
101
102impl GlyphRanges {
103    /// Basic Latin + Latin Supplement (default)
104    pub const DEFAULT: &'static [u32] = &[
105        0x0020, 0x00FF, // Basic Latin + Latin Supplement
106        0,
107    ];
108
109    /// Korean characters
110    pub const KOREAN: &'static [u32] = &[
111        0x0020, 0x00FF, // Basic Latin + Latin Supplement
112        0x3131, 0x3163, // Korean alphabets
113        0xAC00, 0xD7A3, // Korean characters
114        0,
115    ];
116
117    /// Japanese Hiragana + Katakana + Half-Width characters
118    pub const JAPANESE: &'static [u32] = &[
119        0x0020, 0x00FF, // Basic Latin + Latin Supplement
120        0x3000, 0x30FF, // CJK Symbols and Punctuations, Hiragana, Katakana
121        0x31F0, 0x31FF, // Katakana Phonetic Extensions
122        0xFF00, 0xFFEF, // Half-width characters
123        0,
124    ];
125
126    /// Chinese Simplified common characters
127    pub const CHINESE_SIMPLIFIED_COMMON: &'static [u32] = &[
128        0x0020, 0x00FF, // Basic Latin + Latin Supplement
129        0x2000, 0x206F, // General Punctuation
130        0x3000, 0x30FF, // CJK Symbols and Punctuations, Hiragana, Katakana
131        0x31F0, 0x31FF, // Katakana Phonetic Extensions
132        0xFF00, 0xFFEF, // Half-width characters
133        0x4E00, 0x9FAF, // CJK Ideograms
134        0,
135    ];
136
137    /// Chinese Traditional common characters
138    pub const CHINESE_TRADITIONAL_COMMON: &'static [u32] = &[
139        0x0020, 0x00FF, // Basic Latin + Latin Supplement
140        0x2000, 0x206F, // General Punctuation
141        0x3000, 0x30FF, // CJK Symbols and Punctuations, Hiragana, Katakana
142        0x31F0, 0x31FF, // Katakana Phonetic Extensions
143        0xFF00, 0xFFEF, // Half-width characters
144        0x4E00, 0x9FAF, // CJK Ideograms
145        0,
146    ];
147
148    /// Cyrillic characters
149    pub const CYRILLIC: &'static [u32] = &[
150        0x0020, 0x00FF, // Basic Latin + Latin Supplement
151        0x0400, 0x052F, // Cyrillic + Cyrillic Supplement
152        0x2DE0, 0x2DFF, // Cyrillic Extended-A
153        0xA640, 0xA69F, // Cyrillic Extended-B
154        0,
155    ];
156
157    /// Thai characters
158    pub const THAI: &'static [u32] = &[
159        0x0020, 0x00FF, // Basic Latin + Latin Supplement
160        0x0E00, 0x0E7F, // Thai
161        0,
162    ];
163
164    /// Vietnamese characters
165    pub const VIETNAMESE: &'static [u32] = &[
166        0x0020, 0x00FF, // Basic Latin + Latin Supplement
167        0x0102, 0x0103, // Ă ă
168        0x0110, 0x0111, // Đ đ
169        0x0128, 0x0129, // Ĩ ĩ
170        0x0168, 0x0169, // Ũ ũ
171        0x01A0, 0x01A1, // Ơ ơ
172        0x01AF, 0x01B0, // Ư ư
173        0x1EA0, 0x1EF9, // Vietnamese Extended
174        0,
175    ];
176}