@@ -11,6 +11,7 @@ use sdl2::get_error;
11
11
use sdl2:: pixels;
12
12
use sdl2:: pixels:: Color ;
13
13
use sdl2_sys:: pixels:: SDL_Color ;
14
+ use sdl2:: rwops:: RWops ;
14
15
use ffi;
15
16
16
17
/// Converts a rust-SDL2 color to its C ffi representation.
@@ -139,7 +140,7 @@ impl<'a> RenderableText<'a> {
139
140
#[ must_use]
140
141
pub struct PartialRendering < ' a > {
141
142
text : RenderableText < ' a > ,
142
- font : & ' a Font ,
143
+ font : & ' a Font < ' a > ,
143
144
}
144
145
145
146
/// Converts the given raw pointer to a surface.
@@ -246,20 +247,24 @@ impl<'a> PartialRendering<'a> {
246
247
}
247
248
248
249
/// A loaded TTF font.
249
- #[ derive( PartialEq ) ]
250
- pub struct Font {
250
+ pub struct Font < ' a > {
251
251
raw : * const ffi:: TTF_Font ,
252
- owned : bool
252
+ // RWops is only stored here because it must not outlive
253
+ // the Font struct, and this RWops should not be used by
254
+ // anything else
255
+ // None means that the RWops is handled by SDL itself,
256
+ // and Some(rwops) means that the RWops is handled by the Rust
257
+ // side
258
+ #[ allow( dead_code) ]
259
+ rwops : Option < RWops < ' a > >
253
260
}
254
261
255
- impl Drop for Font {
262
+ impl < ' a > Drop for Font < ' a > {
256
263
fn drop ( & mut self ) {
257
- if self . owned {
258
- unsafe {
259
- // avoid close font after quit()
260
- if ffi:: TTF_WasInit ( ) == 1 {
261
- ffi:: TTF_CloseFont ( self . raw ) ;
262
- }
264
+ unsafe {
265
+ // avoid close font after quit()
266
+ if ffi:: TTF_WasInit ( ) == 1 {
267
+ ffi:: TTF_CloseFont ( self . raw ) ;
263
268
}
264
269
}
265
270
}
@@ -273,15 +278,15 @@ pub fn internal_load_font(path: &Path, ptsize: u16) -> Result<Font, String> {
273
278
if raw. is_null ( ) {
274
279
Err ( get_error ( ) )
275
280
} else {
276
- Ok ( Font { raw : raw, owned : true } )
281
+ Ok ( Font { raw : raw, rwops : None } )
277
282
}
278
283
}
279
284
}
280
285
281
286
/// Internally used to load a font (for internal visibility).
282
- pub fn internal_load_font_from_ll ( raw : * const ffi:: TTF_Font , owned : bool )
283
- -> Font {
284
- Font { raw : raw, owned : owned }
287
+ pub fn internal_load_font_from_ll < ' a > ( raw : * const ffi:: TTF_Font , rwops : Option < RWops < ' a > > )
288
+ -> Font < ' a > {
289
+ Font { raw : raw, rwops : rwops }
285
290
}
286
291
287
292
/// Internally used to load a font (for internal visibility).
@@ -295,35 +300,35 @@ pub fn internal_load_font_at_index(path: &Path, index: u32, ptsize: u16)
295
300
if raw. is_null ( ) {
296
301
Err ( get_error ( ) )
297
302
} else {
298
- Ok ( Font { raw : raw, owned : true } )
303
+ Ok ( Font { raw : raw, rwops : None } )
299
304
}
300
305
}
301
306
}
302
307
303
- impl Font {
308
+ impl < ' a > Font < ' a > {
304
309
/// Returns the underlying C font object.
305
310
unsafe fn raw ( & self ) -> * const ffi:: TTF_Font {
306
311
self . raw
307
312
}
308
313
309
314
/// Starts specifying a rendering of the given UTF-8-encoded text.
310
- pub fn render < ' a > ( & ' a self , text : & ' a str ) -> PartialRendering < ' a > {
315
+ pub fn render ( & ' a self , text : & ' a str ) -> PartialRendering < ' a > {
311
316
PartialRendering {
312
317
text : RenderableText :: Utf8 ( text) ,
313
318
font : self ,
314
319
}
315
320
}
316
321
317
322
/// Starts specifying a rendering of the given Latin-1-encoded text.
318
- pub fn render_latin1 < ' a > ( & ' a self , text : & ' a [ u8 ] ) -> PartialRendering < ' a > {
323
+ pub fn render_latin1 ( & ' a self , text : & ' a [ u8 ] ) -> PartialRendering < ' a > {
319
324
PartialRendering {
320
325
text : RenderableText :: Latin1 ( text) ,
321
326
font : self ,
322
327
}
323
328
}
324
329
325
330
/// Starts specifying a rendering of the given UTF-8-encoded character.
326
- pub fn render_char ( & self , ch : char ) -> PartialRendering {
331
+ pub fn render_char ( & ' a self , ch : char ) -> PartialRendering < ' a > {
327
332
let mut s = String :: new ( ) ;
328
333
s. push ( ch) ;
329
334
PartialRendering {
0 commit comments