1
1
use std:: u8;
2
2
3
-
4
3
use sdl2:: {
5
4
pixels:: { Color , PixelFormatEnum } ,
6
5
render:: { Texture , TextureCreator , TextureValueError } ,
@@ -43,32 +42,29 @@ pub fn create_font_texture<'a, T>(
43
42
let font = cairo:: FontFace :: create_from_ft ( ft_font)
44
43
. expect ( "Failed to create cairo font from freetype face" ) ;
45
44
let text_extent = get_text_extent ( & font, font_size as f64 , text) ;
45
+ let ( surface_width, surface_height) = (
46
+ text_extent. width ( ) as i32 + ( outline_size as i32 ) * 2 ,
47
+ text_extent. height ( ) as i32 + ( outline_size as i32 ) * 2 ,
48
+ ) ;
46
49
let cairo_surface = {
47
- cairo:: ImageSurface :: create (
48
- cairo:: Format :: ARgb32 ,
49
- text_extent. width ( ) as i32 ,
50
- text_extent. height ( ) as i32 ,
51
- )
52
- . expect ( "Couldn't create a surface" )
50
+ cairo:: ImageSurface :: create ( cairo:: Format :: ARgb32 , surface_width, surface_height)
51
+ . expect ( "Couldn't create a surface" )
53
52
} ;
54
53
55
54
// Create cairo context
56
55
let cairo_context =
57
56
cairo:: Context :: new ( & cairo_surface) . expect ( "Failed to create cairo context" ) ;
58
57
59
- // Draw string
58
+ // Set text path
60
59
cairo_context. set_font_size ( font_size as f64 ) ;
61
60
cairo_context. set_font_face ( & font) ;
62
- cairo_context. move_to ( -text_extent. x_bearing ( ) , -text_extent. y_bearing ( ) ) ;
63
- cairo_context. text_path ( & text) ;
64
- cairo_context. set_source_rgba (
65
- ( font_color. r as f64 ) / u8:: MAX as f64 ,
66
- ( font_color. g as f64 ) / u8:: MAX as f64 ,
67
- ( font_color. b as f64 ) / u8:: MAX as f64 ,
68
- ( font_color. a as f64 ) / u8:: MAX as f64 ,
61
+ cairo_context. move_to (
62
+ -text_extent. x_bearing ( ) + outline_size as f64 ,
63
+ -text_extent. y_bearing ( ) + outline_size as f64 ,
69
64
) ;
70
- cairo_context. fill ( ) . expect ( "Failed to render font" ) ;
65
+ cairo_context. text_path ( & text ) ;
71
66
67
+ // Draw outline
72
68
if outline_size != 0 {
73
69
let outline_color = outline_color. expect ( "Outline color should be given" ) ;
74
70
cairo_context. set_source_rgba (
@@ -78,14 +74,25 @@ pub fn create_font_texture<'a, T>(
78
74
( outline_color. a as f64 ) / u8:: MAX as f64 ,
79
75
) ;
80
76
cairo_context. set_line_width ( outline_size as f64 ) ;
81
- cairo_context. stroke ( ) . expect ( "Failed to draw font-stroke" ) ;
77
+ cairo_context
78
+ . stroke_preserve ( )
79
+ . expect ( "Failed to draw font-stroke" ) ;
82
80
cairo_context. set_line_width ( 0.0 ) ;
83
81
}
84
82
83
+ // Fill inside
84
+ cairo_context. set_source_rgba (
85
+ ( font_color. r as f64 ) / u8:: MAX as f64 ,
86
+ ( font_color. g as f64 ) / u8:: MAX as f64 ,
87
+ ( font_color. b as f64 ) / u8:: MAX as f64 ,
88
+ ( font_color. a as f64 ) / u8:: MAX as f64 ,
89
+ ) ;
90
+ cairo_context. fill ( ) . expect ( "Failed to render font" ) ;
91
+
85
92
// Create surface\
86
93
let mut surface: sdl2:: surface:: Surface = sdl2:: surface:: Surface :: new (
87
- text_extent . width ( ) as u32 ,
88
- text_extent . height ( ) as u32 ,
94
+ surface_width as u32 ,
95
+ surface_height as u32 ,
89
96
PixelFormatEnum :: ARGB8888 ,
90
97
)
91
98
. expect ( "Failed to create surface" ) ;
0 commit comments