2
2
A binding for SDL2_ttf.
3
3
*/
4
4
5
+ #![ feature( macro_rules) ]
6
+
5
7
#![ crate_id="sdl2_ttf#sdl2_ttf:0.1" ]
6
8
#![ crate_type = "lib" ]
7
9
#![ desc = "SDL2_ttf bindings and wrappers" ]
@@ -12,14 +14,11 @@ extern crate libc;
12
14
extern crate sdl2;
13
15
14
16
use libc:: { c_int, c_long} ;
15
- use std:: ptr;
16
17
use std:: c_str:: CString ;
17
18
use std:: num:: FromPrimitive ;
18
19
use sdl2:: surface:: Surface ;
19
20
use sdl2:: get_error;
20
- use sdl2:: pixels;
21
21
use sdl2:: pixels:: Color ;
22
- use sdl2:: pixels:: ll:: SDL_Color ;
23
22
use sdl2:: rwops:: RWops ;
24
23
use sdl2:: version:: Version ;
25
24
@@ -45,16 +44,17 @@ mod others {
45
44
46
45
#[ allow( non_camel_case_types, dead_code) ]
47
46
mod ffi;
47
+ mod flag;
48
48
49
49
/// Font Style
50
50
#[ deriving( Show ) ]
51
- pub enum FontStyle {
52
- StyleNormal = ffi:: TTF_STYLE_NORMAL as int ,
53
- StyleBold = ffi:: TTF_STYLE_BOLD as int ,
54
- StyleItalic = ffi:: TTF_STYLE_ITALIC as int ,
55
- StyleUnderline = ffi:: TTF_STYLE_UNDERLINE as int ,
56
- StyleStrikeThrough = ffi:: TTF_STYLE_STRIKETHROUGH as int
57
- }
51
+ flag_type ! ( FontStyle : c_int {
52
+ StyleNormal = ffi:: TTF_STYLE_NORMAL ,
53
+ StyleBold = ffi:: TTF_STYLE_BOLD ,
54
+ StyleItalic = ffi:: TTF_STYLE_ITALIC ,
55
+ StyleUnderline = ffi:: TTF_STYLE_UNDERLINE ,
56
+ StyleStrikeThrough = ffi:: TTF_STYLE_STRIKETHROUGH
57
+ } )
58
58
59
59
#[ deriving( Show , Eq , FromPrimitive ) ]
60
60
pub enum Hinting {
@@ -74,14 +74,6 @@ pub struct GlyphMetrics {
74
74
pub advance : int
75
75
}
76
76
77
- #[ inline]
78
- fn color_to_c_color ( color : Color ) -> SDL_Color {
79
- match color {
80
- pixels:: RGB ( r, g, b) => SDL_Color { r : r, g : g, b : b, a : 255 } ,
81
- pixels:: RGBA ( r, g, b, a) => SDL_Color { r : r, g : g, b : b, a : a }
82
- }
83
- }
84
-
85
77
/// Returns the version of the dynamically linked SDL_ttf library
86
78
pub fn get_linked_version ( ) -> Version {
87
79
unsafe {
@@ -133,20 +125,12 @@ impl Drop for Font {
133
125
}
134
126
}
135
127
136
- fn wrap_font_styles ( bitflags : u32 ) -> Vec < FontStyle > {
137
- let styles = [ StyleBold , StyleItalic , StyleUnderline , StyleStrikeThrough ] ;
138
- styles. iter ( ) . filter_map ( |& flag| {
139
- if bitflags & ( flag as u32 ) != 0 { Some ( flag) }
140
- else { None }
141
- } ) . collect ( )
142
- }
143
-
144
128
impl Font {
145
129
pub fn from_file ( filename : & Path , ptsize : int ) -> Result < ~Font , ~str > {
146
130
//! Load file for use as a font, at ptsize size.
147
131
unsafe {
148
132
let raw = ffi:: TTF_OpenFont ( filename. to_c_str ( ) . unwrap ( ) , ptsize as c_int ) ;
149
- if raw == ptr :: null ( ) {
133
+ if raw. is_null ( ) {
150
134
Err ( get_error ( ) )
151
135
} else {
152
136
Ok ( ~Font { raw : raw, owned : true } )
@@ -158,25 +142,24 @@ impl Font {
158
142
//! Load file, face index, for use as a font, at ptsize size.
159
143
unsafe {
160
144
let raw = ffi:: TTF_OpenFontIndex ( filename. to_c_str ( ) . unwrap ( ) , ptsize as c_int , index as c_long ) ;
161
- if raw == ptr :: null ( ) {
145
+ if raw. is_null ( ) {
162
146
Err ( get_error ( ) )
163
147
} else {
164
148
Ok ( ~Font { raw : raw, owned : true } )
165
149
}
166
150
}
167
151
}
168
152
169
- pub fn get_style ( & self ) -> Vec < FontStyle > {
153
+ pub fn get_style ( & self ) -> FontStyle {
170
154
//! Get font render style
171
155
let raw = unsafe { ffi:: TTF_GetFontStyle ( self . raw ) } ;
172
- wrap_font_styles ( raw as u32 )
156
+ FontStyle :: new ( raw)
173
157
}
174
158
175
- pub fn set_style ( & mut self , styles : & [ FontStyle ] ) {
159
+ pub fn set_style ( & mut self , styles : FontStyle ) {
176
160
//! Set font render style.
177
- let flags = styles. iter ( ) . fold ( 0i32 , |flags, flag| { flags | * flag as i32 } ) ;
178
161
unsafe {
179
- ffi:: TTF_SetFontStyle ( self . raw , flags )
162
+ ffi:: TTF_SetFontStyle ( self . raw , styles . get ( ) )
180
163
}
181
164
}
182
165
@@ -269,7 +252,7 @@ impl Font {
269
252
unsafe {
270
253
// not owns buffer
271
254
let cname = ffi:: TTF_FontFaceFamilyName ( self . raw ) ;
272
- if cname == ptr :: null ( ) {
255
+ if cname. is_null ( ) {
273
256
None
274
257
} else {
275
258
Some ( CString :: new ( cname, false ) . as_str ( ) . unwrap ( ) . into_owned ( ) )
@@ -281,7 +264,7 @@ impl Font {
281
264
//! Get current font face style name string.
282
265
unsafe {
283
266
let cname = ffi:: TTF_FontFaceStyleName ( self . raw ) ;
284
- if cname == ptr :: null ( ) {
267
+ if cname. is_null ( ) {
285
268
None
286
269
} else {
287
270
Some ( CString :: new ( cname, false ) . as_str ( ) . unwrap ( ) . into_owned ( ) )
@@ -357,9 +340,9 @@ impl Font {
357
340
//! Draw LATIN1 text in solid mode.
358
341
unsafe {
359
342
let raw = text. with_c_str ( |ctext| {
360
- ffi:: TTF_RenderText_Solid ( self . raw , ctext, color_to_c_color ( fg ) )
343
+ ffi:: TTF_RenderText_Solid ( self . raw , ctext, fg )
361
344
} ) ;
362
- if raw == ptr :: null ( ) {
345
+ if raw. is_null ( ) {
363
346
Err ( get_error ( ) )
364
347
} else {
365
348
Ok ( ~Surface { raw : raw, owned : true } )
@@ -371,9 +354,9 @@ impl Font {
371
354
//! Draw UTF8 text in solid mode.
372
355
unsafe {
373
356
let raw = text. with_c_str ( |ctext| {
374
- ffi:: TTF_RenderUTF8_Solid ( self . raw , ctext, color_to_c_color ( fg ) )
357
+ ffi:: TTF_RenderUTF8_Solid ( self . raw , ctext, fg )
375
358
} ) ;
376
- if raw == ptr :: null ( ) {
359
+ if raw. is_null ( ) {
377
360
Err ( get_error ( ) )
378
361
} else {
379
362
Ok ( ~Surface { raw : raw, owned : true } )
@@ -384,8 +367,8 @@ impl Font {
384
367
pub fn render_char_solid ( & self , ch : char , fg : Color ) -> Result < ~Surface , ~str > {
385
368
//! Draw a UNICODE glyph in solid mode.
386
369
unsafe {
387
- let raw = ffi:: TTF_RenderGlyph_Solid ( self . raw , ch as u16 , color_to_c_color ( fg ) ) ;
388
- if raw == ptr :: null ( ) {
370
+ let raw = ffi:: TTF_RenderGlyph_Solid ( self . raw , ch as u16 , fg ) ;
371
+ if raw. is_null ( ) {
389
372
Err ( get_error ( ) )
390
373
} else {
391
374
Ok ( ~Surface { raw : raw, owned : true } )
@@ -397,9 +380,9 @@ impl Font {
397
380
//! Draw LATIN1 text in shaded mode.
398
381
unsafe {
399
382
let raw = text. with_c_str ( |ctext| {
400
- ffi:: TTF_RenderText_Shaded ( self . raw , ctext, color_to_c_color ( fg ) , color_to_c_color ( bg ) )
383
+ ffi:: TTF_RenderText_Shaded ( self . raw , ctext, fg , bg )
401
384
} ) ;
402
- if raw == ptr :: null ( ) {
385
+ if raw. is_null ( ) {
403
386
Err ( get_error ( ) )
404
387
} else {
405
388
Ok ( ~Surface { raw : raw, owned : true } )
@@ -411,9 +394,9 @@ impl Font {
411
394
//! Draw UTF8 text in shaded mode.
412
395
unsafe {
413
396
let raw = text. with_c_str ( |ctext| {
414
- ffi:: TTF_RenderUTF8_Shaded ( self . raw , ctext, color_to_c_color ( fg ) , color_to_c_color ( bg ) )
397
+ ffi:: TTF_RenderUTF8_Shaded ( self . raw , ctext, fg , bg )
415
398
} ) ;
416
- if raw == ptr :: null ( ) {
399
+ if raw. is_null ( ) {
417
400
Err ( get_error ( ) )
418
401
} else {
419
402
Ok ( ~Surface { raw : raw, owned : true } )
@@ -424,8 +407,8 @@ impl Font {
424
407
pub fn render_char_shaded ( & self , ch : char , fg : Color , bg : Color ) -> Result < ~Surface , ~str > {
425
408
//! Draw a UNICODE glyph in shaded mode.
426
409
unsafe {
427
- let raw = ffi:: TTF_RenderGlyph_Shaded ( self . raw , ch as u16 , color_to_c_color ( fg ) , color_to_c_color ( bg ) ) ;
428
- if raw == ptr :: null ( ) {
410
+ let raw = ffi:: TTF_RenderGlyph_Shaded ( self . raw , ch as u16 , fg , bg ) ;
411
+ if raw. is_null ( ) {
429
412
Err ( get_error ( ) )
430
413
} else {
431
414
Ok ( ~Surface { raw : raw, owned : true } )
@@ -437,9 +420,9 @@ impl Font {
437
420
//! Draw LATIN1 text in blended mode.
438
421
unsafe {
439
422
let raw = text. with_c_str ( |ctext| {
440
- ffi:: TTF_RenderText_Blended ( self . raw , ctext, color_to_c_color ( fg ) )
423
+ ffi:: TTF_RenderText_Blended ( self . raw , ctext, fg )
441
424
} ) ;
442
- if raw == ptr :: null ( ) {
425
+ if raw. is_null ( ) {
443
426
Err ( get_error ( ) )
444
427
} else {
445
428
Ok ( ~Surface { raw : raw, owned : true } )
@@ -451,9 +434,9 @@ impl Font {
451
434
//! Draw UTF8 text in blended mode.
452
435
unsafe {
453
436
let raw = text. with_c_str ( |ctext| {
454
- ffi:: TTF_RenderUTF8_Blended ( self . raw , ctext, color_to_c_color ( fg ) )
437
+ ffi:: TTF_RenderUTF8_Blended ( self . raw , ctext, fg )
455
438
} ) ;
456
- if raw == ptr :: null ( ) {
439
+ if raw. is_null ( ) {
457
440
Err ( get_error ( ) )
458
441
} else {
459
442
Ok ( ~Surface { raw : raw, owned : true } )
@@ -464,8 +447,8 @@ impl Font {
464
447
pub fn render_char_blended ( & self , ch : char , fg : Color ) -> Result < ~Surface , ~str > {
465
448
//! Draw a UNICODE glyph in blended mode.
466
449
unsafe {
467
- let raw = ffi:: TTF_RenderGlyph_Blended ( self . raw , ch as u16 , color_to_c_color ( fg ) ) ;
468
- if raw == ptr :: null ( ) {
450
+ let raw = ffi:: TTF_RenderGlyph_Blended ( self . raw , ch as u16 , fg ) ;
451
+ if raw. is_null ( ) {
469
452
Err ( get_error ( ) )
470
453
} else {
471
454
Ok ( ~Surface { raw : raw, owned : true } )
@@ -488,7 +471,7 @@ impl LoaderRWops for RWops {
488
471
let raw = unsafe {
489
472
ffi:: TTF_OpenFontRW ( self . raw , 0 , ptsize as c_int )
490
473
} ;
491
- if raw == ptr :: null ( ) {
474
+ if raw. is_null ( ) {
492
475
Err ( get_error ( ) )
493
476
} else {
494
477
Ok ( ~Font { raw : raw, owned : true } )
@@ -498,7 +481,7 @@ impl LoaderRWops for RWops {
498
481
let raw = unsafe {
499
482
ffi:: TTF_OpenFontIndexRW ( self . raw , 0 , ptsize as c_int , index as c_long )
500
483
} ;
501
- if raw == ptr :: null ( ) {
484
+ if raw. is_null ( ) {
502
485
Err ( get_error ( ) )
503
486
} else {
504
487
Ok ( ~Font { raw : raw, owned : true } )
0 commit comments