Skip to content

Commit fc4d361

Browse files
committed
fix: font outline non-rendering bug
1 parent 9b65309 commit fc4d361

File tree

1 file changed

+26
-19
lines changed

1 file changed

+26
-19
lines changed

game/src/game/util/create_outlined_font_texture.rs

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use std::u8;
22

3-
43
use sdl2::{
54
pixels::{Color, PixelFormatEnum},
65
render::{Texture, TextureCreator, TextureValueError},
@@ -43,32 +42,29 @@ pub fn create_font_texture<'a, T>(
4342
let font = cairo::FontFace::create_from_ft(ft_font)
4443
.expect("Failed to create cairo font from freetype face");
4544
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+
);
4649
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")
5352
};
5453

5554
// Create cairo context
5655
let cairo_context =
5756
cairo::Context::new(&cairo_surface).expect("Failed to create cairo context");
5857

59-
// Draw string
58+
// Set text path
6059
cairo_context.set_font_size(font_size as f64);
6160
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,
6964
);
70-
cairo_context.fill().expect("Failed to render font");
65+
cairo_context.text_path(&text);
7166

67+
// Draw outline
7268
if outline_size != 0 {
7369
let outline_color = outline_color.expect("Outline color should be given");
7470
cairo_context.set_source_rgba(
@@ -78,14 +74,25 @@ pub fn create_font_texture<'a, T>(
7874
(outline_color.a as f64) / u8::MAX as f64,
7975
);
8076
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");
8280
cairo_context.set_line_width(0.0);
8381
}
8482

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+
8592
// Create surface\
8693
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,
8996
PixelFormatEnum::ARGB8888,
9097
)
9198
.expect("Failed to create surface");

0 commit comments

Comments
 (0)