Skip to content

Commit

Permalink
add draw_multiline_text and draw_multiline_text_ex (#751)
Browse files Browse the repository at this point in the history
text: add `draw_multiline_text` and `draw_multiline_text_ex`
  • Loading branch information
cyrgani authored Jul 13, 2024
1 parent adc07da commit 6ceb56d
Showing 1 changed file with 53 additions and 1 deletion.
54 changes: 53 additions & 1 deletion src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,13 +359,65 @@ pub fn draw_text_ex(text: &str, x: f32, y: f32, params: TextParams) {
dest_size: Some(vec2(dest.w, dest.h)),
source: Some(source),
rotation: angle_rad,
pivot: Option::Some(vec2(dest.x, dest.y)),
pivot: Some(vec2(dest.x, dest.y)),
..Default::default()
},
);
}
}

/// Draw multiline text with the given font_size, line_distance_factor and color.
/// If no line distance but a custom font is given, the fonts line gap will be used as line distance factor if it exists.
pub fn draw_multiline_text(
text: &str,
x: f32,
y: f32,
font_size: f32,
line_distance_factor: Option<f32>,
color: Color,
) {
draw_multiline_text_ex(
text,
x,
y,
line_distance_factor,
TextParams {
font_size: font_size as u16,
font_scale: 1.0,
color,
..Default::default()
},
)
}

/// Draw multiline text with the given line distance and custom params such as font, font size and font scale.
/// If no line distance but a custom font is given, the fonts newline size will be used as line distance factor if it exists.
pub fn draw_multiline_text_ex(
text: &str,
x: f32,
mut y: f32,
line_distance_factor: Option<f32>,
params: TextParams,
) {
let line_distance = match line_distance_factor {
Some(distance) => distance,
None => {
let mut font_line_distance = 0.0;
if let Some(font) = params.font {
if let Some(metrics) = font.font.horizontal_line_metrics(1.0) {
font_line_distance = metrics.new_line_size;
}
}
font_line_distance
}
};

for line in text.lines() {
draw_text_ex(line, x, y, params.clone());
y += line_distance * params.font_size as f32 * params.font_scale;
}
}

/// Get the text center.
pub fn get_text_center(
text: &str,
Expand Down

0 comments on commit 6ceb56d

Please sign in to comment.