dear_imgui_rs/fonts/atlas/
texture.rs1use 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#[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 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 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}