Skip to content

Commit 553fb0d

Browse files
lukasx999not-fl3
authored andcommitted
replaced &str with impl AsRef<str> for more convenient api
1 parent adfe12f commit 553fb0d

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

src/text.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,13 @@ impl Font {
150150

151151
pub(crate) fn measure_text(
152152
&self,
153-
text: &str,
153+
text: impl AsRef<str>,
154154
font_size: u16,
155155
font_scale_x: f32,
156156
font_scale_y: f32,
157157
) -> TextDimensions {
158+
let text = text.as_ref();
159+
158160
let dpi_scaling = miniquad::window::dpi_scale();
159161
let font_size = (font_size as f32 * dpi_scaling).ceil() as u16;
160162

@@ -298,7 +300,7 @@ pub fn load_ttf_font_from_bytes(bytes: &[u8]) -> Result<Font, Error> {
298300

299301
/// Draw text with given font_size
300302
/// Returns text size
301-
pub fn draw_text(text: &str, x: f32, y: f32, font_size: f32, color: Color) -> TextDimensions {
303+
pub fn draw_text(text: impl AsRef<str>, x: f32, y: f32, font_size: f32, color: Color) -> TextDimensions {
302304
draw_text_ex(
303305
text,
304306
x,
@@ -314,7 +316,9 @@ pub fn draw_text(text: &str, x: f32, y: f32, font_size: f32, color: Color) -> Te
314316

315317
/// Draw text with custom params such as font, font size and font scale
316318
/// Returns text size
317-
pub fn draw_text_ex(text: &str, x: f32, y: f32, params: TextParams) -> TextDimensions {
319+
pub fn draw_text_ex(text: impl AsRef<str>, x: f32, y: f32, params: TextParams) -> TextDimensions {
320+
let text = text.as_ref();
321+
318322
if text.is_empty() {
319323
return TextDimensions::default();
320324
}
@@ -391,7 +395,7 @@ pub fn draw_text_ex(text: &str, x: f32, y: f32, params: TextParams) -> TextDimen
391395
/// Draw multiline text with the given font_size, line_distance_factor and color.
392396
/// If no line distance but a custom font is given, the fonts line gap will be used as line distance factor if it exists.
393397
pub fn draw_multiline_text(
394-
text: &str,
398+
text: impl AsRef<str>,
395399
x: f32,
396400
y: f32,
397401
font_size: f32,
@@ -415,12 +419,14 @@ pub fn draw_multiline_text(
415419
/// Draw multiline text with the given line distance and custom params such as font, font size and font scale.
416420
/// If no line distance but a custom font is given, the fonts newline size will be used as line distance factor if it exists, else default to font size.
417421
pub fn draw_multiline_text_ex(
418-
text: &str,
422+
text: impl AsRef<str>,
419423
mut x: f32,
420424
mut y: f32,
421425
line_distance_factor: Option<f32>,
422426
params: TextParams,
423427
) {
428+
let text = text.as_ref();
429+
424430
let line_distance = match line_distance_factor {
425431
Some(distance) => distance,
426432
None => {
@@ -447,7 +453,7 @@ pub fn draw_multiline_text_ex(
447453

448454
/// Get the text center.
449455
pub fn get_text_center(
450-
text: &str,
456+
text: impl AsRef<str>,
451457
font: Option<&Font>,
452458
font_size: u16,
453459
font_scale: f32,
@@ -462,7 +468,7 @@ pub fn get_text_center(
462468
}
463469

464470
pub fn measure_text(
465-
text: &str,
471+
text: impl AsRef<str>,
466472
font: Option<&Font>,
467473
font_size: u16,
468474
font_scale: f32,

0 commit comments

Comments
 (0)