Skip to main content

dear_imgui_rs/fonts/atlas/
texture.rs

1use std::fmt;
2use std::ops::Deref;
3use std::ptr::NonNull;
4
5use crate::sys;
6use crate::texture::{TextureData, TextureRef};
7
8use super::FontAtlas;
9use super::state::{
10    acquire_font_atlas_texture_borrow, assert_no_open_font_atlas_frames,
11    release_font_atlas_texture_borrow,
12};
13
14/// Read-only lease for the current font atlas texture between frames.
15///
16/// The lease keeps safe atlas operations and frame advancement from invalidating the texture or
17/// its pixel buffer. Drop it before rebuilding, compacting, clearing, writing custom rectangles,
18/// or starting another frame that uses the atlas.
19#[must_use = "keep the texture lease alive while using texture data or pixel slices"]
20pub struct FontAtlasTexture<'atlas> {
21    atlas: &'atlas FontAtlas,
22    texture: NonNull<sys::ImTextureData>,
23}
24
25impl FontAtlas {
26    pub(crate) fn tex_data_internal(&self) -> Option<FontAtlasTexture<'_>> {
27        let raw = self.raw();
28        assert_no_open_font_atlas_frames(raw, "borrowing font atlas texture data");
29        let texture = NonNull::new(unsafe { (*raw).TexData })?;
30        acquire_font_atlas_texture_borrow(self);
31        Some(FontAtlasTexture {
32            atlas: self,
33            texture,
34        })
35    }
36}
37
38impl FontAtlasTexture<'_> {
39    /// Returns an owner-backed logical reference for image and plotting APIs.
40    pub fn texture_ref(&self) -> TextureRef<'_> {
41        let texture = unsafe { sys::ImTextureData_GetTexRef(self.texture.as_ptr()) };
42        TextureRef::from_font_atlas_raw(self.atlas.raw(), texture)
43    }
44}
45
46impl crate::Ui {
47    /// Return the current font atlas texture for image submission in this frame.
48    ///
49    /// Dear ImGui keeps the texture allocation alive through the frame. Drawing text may write
50    /// new glyphs into its pixels or grow the list of pending updates. For CPU pixel access
51    /// between frames, use [`LegacyFontAtlas::tex_data`](super::LegacyFontAtlas::tex_data).
52    ///
53    /// ```compile_fail,E0599
54    /// fn borrow_pixels(ui: &dear_imgui_rs::Ui) {
55    ///     let image = ui.font_atlas_texture().unwrap();
56    ///     let _ = image.pixels();
57    /// }
58    /// ```
59    pub fn font_atlas_texture(&self) -> Option<TextureRef<'_>> {
60        self.run_with_bound_context(|| unsafe {
61            let io = sys::igGetIO_ContextPtr(self.context_raw());
62            if io.is_null() || (*io).Fonts.is_null() {
63                return None;
64            }
65            let atlas = (*io).Fonts;
66            let texture = NonNull::new((*atlas).TexData)?;
67            Some(TextureRef::from_font_atlas_raw(
68                atlas,
69                sys::ImTextureData_GetTexRef(texture.as_ptr()),
70            ))
71        })
72    }
73}
74
75impl Deref for FontAtlasTexture<'_> {
76    type Target = TextureData;
77
78    fn deref(&self) -> &Self::Target {
79        unsafe { TextureData::from_raw_ref(self.texture.as_ptr()) }
80    }
81}
82
83impl fmt::Debug for FontAtlasTexture<'_> {
84    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
85        formatter
86            .debug_struct("FontAtlasTexture")
87            .field("native_unique_id", &self.native_unique_id())
88            .field("status", &self.status())
89            .field("format", &self.format())
90            .field("width", &self.width())
91            .field("height", &self.height())
92            .finish_non_exhaustive()
93    }
94}
95
96impl Drop for FontAtlasTexture<'_> {
97    fn drop(&mut self) {
98        release_font_atlas_texture_borrow(self.atlas);
99    }
100}