use flo_draw::*;
use flo_draw::canvas::*;
use std::sync::*;
pub fn main() {
with_2d_graphics(|| {
let lato = CanvasFontFace::from_slice(include_bytes!("Lato-Regular.ttf"));
let lato_bold = CanvasFontFace::from_slice(include_bytes!("Lato-Bold.ttf"));
let canvas = create_drawing_window("Text layout example");
canvas.draw(|gc| {
gc.canvas_height(1000.0);
gc.center_region(0.0, 0.0, 1000.0, 1000.0);
gc.define_font_data(FontId(1), Arc::clone(&lato));
gc.define_font_data(FontId(2), Arc::clone(&lato_bold));
gc.set_font_size(FontId(1), 18.0);
gc.set_font_size(FontId(2), 18.0);
});
canvas.draw(|gc| {
gc.fill_color(Color::Rgba(0.0, 0.0, 0.6, 1.0));
gc.begin_line_layout(18.0, 900.0, TextAlignment::Left);
gc.layout_text(FontId(1), "Simple text layout".to_string());
gc.draw_text_layout();
});
canvas.draw(|gc| {
gc.begin_line_layout(18.0, 900.0 - 30.0, TextAlignment::Left);
gc.layout_text(FontId(1), "We can change ".to_string());
gc.layout_text(FontId(2), "fonts".to_string());
gc.layout_text(FontId(1), " during layout ".to_string());
gc.draw_text_layout();
});
canvas.draw(|gc| {
gc.begin_line_layout(18.0, 900.0 - 60.0, TextAlignment::Left);
gc.layout_text(FontId(1), "Or we could change ".to_string());
gc.fill_color(Color::Rgba(0.8, 0.6, 0.0, 1.0));
gc.layout_text(FontId(1), "colours".to_string());
gc.fill_color(Color::Rgba(0.0, 0.0, 0.6, 1.0));
gc.layout_text(FontId(1), " during layout ".to_string());
gc.draw_text_layout();
});
canvas.draw(|gc| {
gc.begin_line_layout(18.0, 900.0 - 100.0, TextAlignment::Left);
gc.layout_text(FontId(1), "It's also possible to alter ".to_string());
gc.set_font_size(FontId(1), 36.0);
gc.layout_text(FontId(1), "sizes".to_string());
gc.set_font_size(FontId(1), 18.0);
gc.layout_text(FontId(1), " during layout ".to_string());
gc.draw_text_layout();
});
canvas.draw(|gc| {
gc.begin_line_layout(500.0, 500.0, TextAlignment::Center);
gc.layout_text(FontId(1), "Text layout demonstration, with changing ".to_string());
gc.set_font_size(FontId(1), 36.0);
gc.layout_text(FontId(1), "sizes,".to_string());
gc.set_font_size(FontId(1), 18.0);
gc.fill_color(Color::Rgba(0.8, 0.6, 0.0, 1.0));
gc.layout_text(FontId(1), " colours,".to_string());
gc.fill_color(Color::Rgba(0.0, 0.0, 0.6, 1.0));
gc.layout_text(FontId(2), " fonts,".to_string());
gc.layout_text(FontId(1), " and center alignment ".to_string());
gc.draw_text_layout();
gc.begin_line_layout(1000.0-18.0, 80.0, TextAlignment::Right);
gc.layout_text(FontId(1), "Right alignment is supported too".to_string());
gc.draw_text_layout();
});
canvas.draw(|gc| {
let mut text_layout = CanvasFontLineLayout::new(&lato, 18.0);
text_layout.add_text("Performing layout manually is also possible");
text_layout.align_transform(500.0, 400.0, TextAlignment::Center);
gc.draw_list(text_layout.to_drawing(FontId(1)));
});
canvas.draw(|gc| {
let lato_metrics = lato.font_metrics(18.0).unwrap();
let mut text_layout = CanvasFontLineLayout::new(&lato, 18.0);
text_layout.add_text("Manual layout allows ");
let start_pos = text_layout.measure();
text_layout.add_text("custom");
let end_pos = text_layout.measure();
text_layout.new_path();
text_layout.move_to(start_pos.pos.x() as _, start_pos.pos.y() as f32 + lato_metrics.underline_position.unwrap().offset);
text_layout.line_to(end_pos.pos.x() as _, end_pos.pos.y() as f32 + lato_metrics.underline_position.unwrap().offset);
text_layout.stroke_color(Color::Rgba(0.8, 0.6, 0.0, 1.0));
text_layout.line_width(lato_metrics.underline_position.unwrap().thickness);
text_layout.stroke();
text_layout.new_path();
text_layout.move_to(start_pos.pos.x() as _, start_pos.pos.y() as f32 + lato_metrics.ascender);
text_layout.line_to(end_pos.pos.x() as _, end_pos.pos.y() as f32 + lato_metrics.ascender);
text_layout.stroke_color(Color::Rgba(0.8, 0.6, 0.0, 1.0));
text_layout.line_width(lato_metrics.underline_position.unwrap().thickness);
text_layout.stroke();
let mid_point = (start_pos.pos + end_pos.pos) * 0.5;
text_layout.move_to(mid_point.x() as _, mid_point.y() as f32 + lato_metrics.underline_position.unwrap().offset);
text_layout.line_to(mid_point.x() as _, mid_point.y() as f32 + lato_metrics.underline_position.unwrap().offset - 8.0);
text_layout.stroke_color(Color::Rgba(0.8, 0.6, 0.0, 1.0));
text_layout.line_width(2.0);
text_layout.stroke();
text_layout.begin_line_layout(mid_point.x() as _, mid_point.y() as f32-lato_metrics.underline_position.unwrap().offset - 30.0, TextAlignment::Center);
text_layout.layout_text(FontId(1), "here".to_string());
text_layout.draw_text_layout();
text_layout.add_text(" drawing effects, such as this underline");
text_layout.align_transform(500.0, 370.0, TextAlignment::Center);
gc.draw_list(text_layout.to_drawing(FontId(1)));
});
canvas.draw(|gc| {
let mut text_layout = CanvasFontLineLayout::new(&lato, 18.0);
text_layout.add_text("Changing ");
text_layout.fill_color(Color::Rgba(0.8, 0.6, 0.0, 1.0));
text_layout.add_text("colour");
text_layout.fill_color(Color::Rgba(0.0, 0.0, 0.6, 1.0));
text_layout.add_text(" and ");
let mut text_layout = text_layout.continue_with_new_font(FontId(1), &lato_bold, 18.0);
text_layout.add_text("font");
let mut text_layout = text_layout.continue_with_new_font(FontId(2), &lato, 18.0);
text_layout.add_text(" is still possible with manual layouts");
text_layout.align_transform(500.0, 310.0, TextAlignment::Center);
gc.draw_list(text_layout.to_drawing(FontId(1)));
});
});
}