1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
use std::marker::PhantomData;
use std::mem;
use std::os::raw::{c_int, c_void};
use std::ptr;
use sys;

#[derive(Clone, Eq, PartialEq, Hash, Debug)]
enum FontGlyphRangeData {
    Chinese,
    Cyrillic,
    Default,
    Japanese,
    Korean,
    Thai,
    Custom(*const sys::ImWchar),
}

/// A set of 16-bit Unicode codepoints
#[derive(Clone, Eq, PartialEq, Hash, Debug)]
pub struct FontGlyphRange(FontGlyphRangeData);
impl FontGlyphRange {
    /// The default set of glyph ranges used by imgui.
    pub fn default() -> FontGlyphRange {
        FontGlyphRange(FontGlyphRangeData::Default)
    }

    /// A set of glyph ranges appropriate for use with Chinese text.
    pub fn chinese() -> FontGlyphRange {
        FontGlyphRange(FontGlyphRangeData::Chinese)
    }
    /// A set of glyph ranges appropriate for use with Cyrillic text.
    pub fn cyrillic() -> FontGlyphRange {
        FontGlyphRange(FontGlyphRangeData::Cyrillic)
    }
    /// A set of glyph ranges appropriate for use with Japanese text.
    pub fn japanese() -> FontGlyphRange {
        FontGlyphRange(FontGlyphRangeData::Japanese)
    }
    /// A set of glyph ranges appropriate for use with Korean text.
    pub fn korean() -> FontGlyphRange {
        FontGlyphRange(FontGlyphRangeData::Korean)
    }
    /// A set of glyph ranges appropriate for use with Thai text.
    pub fn thai() -> FontGlyphRange {
        FontGlyphRange(FontGlyphRangeData::Thai)
    }

    /// Creates a glyph range from a static slice. The expected format is a series of pairs of
    /// non-zero shorts, each representing an inclusive range of codepoints, followed by a single
    /// zero terminating the range. The ranges must not overlap.
    ///
    /// As the slice is expected to last as long as a font is used, and is written into global
    /// state, it must be `'static`.
    ///
    /// Panics
    /// ======
    ///
    /// This function will panic if the given slice is not a valid font range.
    pub fn from_slice(slice: &'static [sys::ImWchar]) -> FontGlyphRange {
        assert_eq!(
            slice.len() % 2,
            1,
            "The length of a glyph range must be odd."
        );
        assert_eq!(
            slice.last(),
            Some(&0),
            "A glyph range must be zero-terminated."
        );

        for (i, &glyph) in slice.iter().enumerate().take(slice.len() - 1) {
            assert_ne!(
                glyph, 0,
                "A glyph in a range cannot be zero. \
                 (Glyph is zero at index {})",
                i
            )
        }

        let mut ranges = Vec::new();
        for i in 0..slice.len() / 2 {
            let (start, end) = (slice[i * 2], slice[i * 2 + 1]);
            assert!(
                start <= end,
                "The start of a range cannot be larger than its end. \
                 (At index {}, {} > {})",
                i * 2,
                start,
                end
            );
            ranges.push((start, end));
        }
        ranges.sort_unstable_by_key(|x| x.0);
        for i in 0..ranges.len() - 1 {
            let (range_a, range_b) = (ranges[i], ranges[i + 1]);
            if range_a.1 >= range_b.0 {
                panic!(
                    "The glyph ranges {:?} and {:?} overlap between {:?}.",
                    range_a,
                    range_b,
                    (range_a.1, range_b.0)
                );
            }
        }

        unsafe { FontGlyphRange::from_slice_unchecked(slice) }
    }

    /// Creates a glyph range from a static slice without checking its validity.
    ///
    /// See [`FontRangeGlyph::from_slice`] for more information.
    pub unsafe fn from_slice_unchecked(slice: &'static [sys::ImWchar]) -> FontGlyphRange {
        FontGlyphRange::from_ptr(slice.as_ptr())
    }

    /// Creates a glyph range from a pointer, without checking its validity or enforcing its
    /// lifetime. The memory the pointer points to must be valid for as long as the font is
    /// in use.
    pub unsafe fn from_ptr(ptr: *const sys::ImWchar) -> FontGlyphRange {
        FontGlyphRange(FontGlyphRangeData::Custom(ptr))
    }

    unsafe fn to_ptr(&self, atlas: *mut sys::ImFontAtlas) -> *const sys::ImWchar {
        match &self.0 {
            &FontGlyphRangeData::Chinese => sys::ImFontAtlas_GetGlyphRangesChinese(atlas),
            &FontGlyphRangeData::Cyrillic => sys::ImFontAtlas_GetGlyphRangesCyrillic(atlas),
            &FontGlyphRangeData::Default => sys::ImFontAtlas_GetGlyphRangesDefault(atlas),
            &FontGlyphRangeData::Japanese => sys::ImFontAtlas_GetGlyphRangesJapanese(atlas),
            &FontGlyphRangeData::Korean => sys::ImFontAtlas_GetGlyphRangesKorean(atlas),
            &FontGlyphRangeData::Thai => sys::ImFontAtlas_GetGlyphRangesThai(atlas),

            &FontGlyphRangeData::Custom(ptr) => ptr,
        }
    }
}

/// A builder for the configuration for a font.
#[derive(Copy, Clone, PartialEq, Debug)]
pub struct ImFontConfig {
    size_pixels: f32,
    oversample_h: u32,
    oversample_v: u32,
    pixel_snap_h: bool,
    glyph_extra_spacing: sys::ImVec2,
    glyph_offset: sys::ImVec2,
    merge_mode: bool,
    rasterizer_multiply: f32,
}
impl ImFontConfig {
    pub fn new() -> ImFontConfig {
        ImFontConfig {
            size_pixels: 0.0,
            oversample_h: 3,
            oversample_v: 1,
            pixel_snap_h: false,
            glyph_extra_spacing: sys::ImVec2::zero(),
            glyph_offset: sys::ImVec2::zero(),
            merge_mode: false,
            rasterizer_multiply: 1.0,
        }
    }

    pub fn size_pixels(mut self, size_pixels: f32) -> ImFontConfig {
        self.size_pixels = size_pixels;
        self
    }
    pub fn oversample_h(mut self, oversample_h: u32) -> ImFontConfig {
        self.oversample_h = oversample_h;
        self
    }
    pub fn oversample_v(mut self, oversample_v: u32) -> ImFontConfig {
        self.oversample_v = oversample_v;
        self
    }
    pub fn pixel_snap_h(mut self, pixel_snap_h: bool) -> ImFontConfig {
        self.pixel_snap_h = pixel_snap_h;
        self
    }
    pub fn glyph_extra_spacing<I: Into<sys::ImVec2>>(mut self, extra_spacing: I) -> ImFontConfig {
        self.glyph_extra_spacing = extra_spacing.into();
        self
    }
    pub fn glyph_offset<I: Into<sys::ImVec2>>(mut self, glyph_offset: I) -> ImFontConfig {
        self.glyph_offset = glyph_offset.into();
        self
    }
    pub fn merge_mode(mut self, merge_mode: bool) -> ImFontConfig {
        self.merge_mode = merge_mode;
        self
    }
    pub fn rasterizer_multiply(mut self, rasterizer_multiply: f32) -> ImFontConfig {
        self.rasterizer_multiply = rasterizer_multiply;
        self
    }

    fn make_config(self) -> sys::ImFontConfig {
        let mut config = unsafe {
            let mut config = mem::uninitialized();
            sys::ImFontConfig_DefaultConstructor(&mut config);
            config
        };
        config.size_pixels = self.size_pixels;
        config.oversample_h = self.oversample_h as c_int;
        config.oversample_v = self.oversample_v as c_int;
        config.pixel_snap_h = self.pixel_snap_h;
        config.glyph_extra_spacing = self.glyph_extra_spacing;
        config.glyph_offset = self.glyph_offset;
        config.merge_mode = self.merge_mode;
        config.rasterizer_multiply = self.rasterizer_multiply;
        config
    }

    /// Adds a custom font to the font set with the given configuration. A font size must be set
    /// in the configuration.
    ///
    /// Panics
    /// ======
    ///
    /// If no font size is set for the configuration.
    pub fn add_font<'a>(
        self,
        atlas: &'a mut ImFontAtlas<'a>,
        data: &[u8],
        range: &FontGlyphRange,
    ) -> ImFont<'a> {
        atlas.add_font_with_config(data, self, range)
    }

    /// Adds the default font to a given atlas using this configuration.
    pub fn add_default_font<'a>(self, atlas: &'a mut ImFontAtlas<'a>) -> ImFont<'a> {
        atlas.add_default_font_with_config(self)
    }
}
impl Default for ImFontConfig {
    fn default() -> Self {
        ImFontConfig::new()
    }
}

/// A handle to an imgui font.
pub struct ImFont<'a> {
    font: *mut sys::ImFont,
    _phantom: PhantomData<&'a mut sys::ImFont>,
}
impl<'a> ImFont<'a> {
    unsafe fn from_ptr(font: *mut sys::ImFont) -> ImFont<'a> {
        ImFont {
            font,
            _phantom: PhantomData,
        }
    }

    fn chain(&mut self) -> ImFont {
        ImFont {
            font: self.font,
            _phantom: PhantomData,
        }
    }

    pub fn font_size(&self) -> f32 {
        unsafe { sys::ImFont_GetFontSize(self.font) }
    }
    pub fn set_font_size(&mut self, size: f32) -> ImFont {
        unsafe { sys::ImFont_SetFontSize(self.font, size) }
        self.chain()
    }

    pub fn scale(&self) -> f32 {
        unsafe { sys::ImFont_GetScale(self.font) }
    }
    pub fn set_scale(&mut self, size: f32) -> ImFont {
        unsafe { sys::ImFont_SetScale(self.font, size) }
        self.chain()
    }

    pub fn display_offset(&self) -> (f32, f32) {
        let mut display_offset = unsafe { mem::uninitialized() };
        unsafe { sys::ImFont_GetDisplayOffset(self.font, &mut display_offset) }
        display_offset.into()
    }
}

/// A handle to imgui's font manager.
#[repr(C)]
pub struct ImFontAtlas<'a> {
    atlas: *mut sys::ImFontAtlas,
    _phantom: PhantomData<&'a mut sys::ImFontAtlas>,
}
impl<'a> ImFontAtlas<'a> {
    pub(crate) unsafe fn from_ptr(atlas: *mut sys::ImFontAtlas) -> ImFontAtlas<'a> {
        ImFontAtlas {
            atlas,
            _phantom: PhantomData,
        }
    }

    /// Adds the default font to the font set.
    pub fn add_default_font(&mut self) -> ImFont {
        unsafe { ImFont::from_ptr(sys::ImFontAtlas_AddFontDefault(self.atlas, ptr::null_mut())) }
    }

    /// Adds the default fnt to the font set with the given configuration.
    pub fn add_default_font_with_config(&mut self, config: ImFontConfig) -> ImFont {
        let config = config.make_config();
        unsafe { ImFont::from_ptr(sys::ImFontAtlas_AddFontDefault(self.atlas, &config)) }
    }

    fn raw_add_font(
        &mut self,
        data: &[u8],
        config: ImFontConfig,
        range: &FontGlyphRange,
    ) -> ImFont {
        assert!(
            (data.len() as u64) < (c_int::max_value() as u64),
            "Font data is too long."
        );
        unsafe {
            let mut config = config.make_config();
            assert!(config.size_pixels > 0.0, "Font size cannot be zero.");
            config.font_data = data.as_ptr() as *mut c_void;
            config.font_data_size = data.len() as c_int;
            config.glyph_ranges = range.to_ptr(self.atlas);
            config.font_data_owned_by_atlas = false;

            ImFont::from_ptr(sys::ImFontAtlas_AddFont(self.atlas, &config))
        }
    }

    /// Adds a custom font to the font set.
    pub fn add_font(&mut self, data: &[u8], size: f32, range: &FontGlyphRange) -> ImFont {
        self.raw_add_font(data, ImFontConfig::new().size_pixels(size), range)
    }

    /// Adds a custom font to the font set with the given configuration. A font size must be set
    /// in the configuration.
    ///
    /// Panics
    /// ======
    ///
    /// If no font size is set for the configuration.
    pub fn add_font_with_config(
        &mut self,
        data: &[u8],
        config: ImFontConfig,
        range: &FontGlyphRange,
    ) -> ImFont {
        self.raw_add_font(data, config, range)
    }

    /// The number of fonts currently registered in the atlas.
    pub fn font_count(&self) -> usize {
        unsafe { sys::ImFontAtlas_Fonts_size(self.atlas) as usize }
    }

    /// Gets a font from the atlas.
    ///
    /// Panics
    /// ======
    ///
    /// Panics if the index is out of range.
    pub fn index_font(&mut self, index: usize) -> ImFont {
        assert!(index < self.font_count(), "Font index is out of range.");
        unsafe { ImFont::from_ptr(sys::ImFontAtlas_Fonts_index(self.atlas, index as c_int)) }
    }

    /// Clears all fonts associated with this texture atlas.
    pub fn clear(&mut self) {
        unsafe { sys::ImFontAtlas_Clear(self.atlas) }
    }

    pub fn texture_id(&self) -> usize {
        unsafe { (*self.atlas).tex_id as usize }
    }
    pub fn set_texture_id(&mut self, value: usize) {
        unsafe {
            (*self.atlas).tex_id = value as *mut c_void;
        }
    }
}