@@ -132,14 +132,17 @@ impl std::ops::DerefMut for Collection {
132132}
133133
134134/// Handle to a registered font that can be used for future operations.
135+ ///
136+ /// Returned by [`register_font_from_memory()`].
135137#[ derive( Debug , Clone ) ]
136- pub struct FontHandle {
137- /// Family IDs of the registered fonts
138- pub family_ids : Vec < fontique:: FamilyId > ,
139- }
138+ #[ non_exhaustive]
139+ pub struct FontHandle { }
140140
141141/// Error type for font registration failures.
142+ ///
143+ /// Returned by [`register_font_from_memory()`].
142144#[ derive( Debug , Clone ) ]
145+ #[ non_exhaustive]
143146pub enum RegisterFontError {
144147 /// The provided font data was empty
145148 EmptyData ,
@@ -165,22 +168,18 @@ impl std::error::Error for RegisterFontError {}
165168
166169/// Register a font from byte data dynamically.
167170///
168- /// # Arguments
169- ///
170- /// * `font_data` - Font data as bytes (supports any type that can be converted to a byte slice)
171+ /// The argument is the owner of a slice of byte that contains the data of the font.
172+ /// It can for example be a `&'static [u8]` obtained from [`include_bytes!()`] or a `Vec<u8>`
173+ /// obtained from [`std::fs::read()`].
171174///
172- /// # Returns
173- ///
174- /// Returns a `FontHandle` on success, or a `RegisterFontError` on failure.
175+ /// The data must be in the [TrueType (TTF) format](https://en.wikipedia.org/wiki/TrueType).
175176///
176177/// # Example
177178///
178- /// ```ignore
179- /// let font_bytes = include_bytes!("my_font.ttf");
180- /// match register_font_from_memory(font_bytes.to_vec()) {
181- /// Ok(handle) => println!("Registered {} font families", handle.family_ids.len()),
182- /// Err(e) => eprintln!("Failed to register font: {}", e),
183- /// }
179+ /// ```
180+ /// # use i_slint_common::sharedfontique as slint;
181+ /// let font_bytes = include_bytes!("sharedfontique/DejaVuSans.ttf");
182+ /// slint::register_font_from_memory(font_bytes).expect("Failed to register font");
184183/// ```
185184pub fn register_font_from_memory (
186185 font_data : impl AsRef < [ u8 ] > + Send + Sync + ' static ,
@@ -191,9 +190,7 @@ pub fn register_font_from_memory(
191190 return Err ( RegisterFontError :: EmptyData ) ;
192191 }
193192
194- // Convert to owned data for Arc
195- let owned_data: Vec < u8 > = data. to_vec ( ) ;
196- let blob = fontique:: Blob :: new ( Arc :: new ( owned_data) ) ;
193+ let blob = fontique:: Blob :: new ( Arc :: new ( font_data) ) ;
197194
198195 let mut collection = get_collection ( ) ;
199196 let fonts = collection. register_fonts ( blob, None ) ;
@@ -202,15 +199,13 @@ pub fn register_font_from_memory(
202199 return Err ( RegisterFontError :: NoFontsFound ) ;
203200 }
204201
205- let family_ids: Vec < _ > = fonts. iter ( ) . map ( |( family_id, _) | * family_id) . collect ( ) ;
206-
207202 // Set up fallbacks for all scripts
208203 for script in fontique:: Script :: all_samples ( ) . iter ( ) . map ( |( script, _) | * script) {
209204 collection
210- . append_fallbacks ( fontique:: FallbackKey :: new ( script, None ) , family_ids . iter ( ) . copied ( ) ) ;
205+ . append_fallbacks ( fontique:: FallbackKey :: new ( script, None ) , fonts . iter ( ) . map ( |x| x . 0 ) ) ;
211206 }
212207
213- Ok ( FontHandle { family_ids } )
208+ Ok ( FontHandle { } )
214209}
215210
216211/// Font metrics in design space. Scale with desired pixel size and divided by units_per_em
0 commit comments