2023-04-19 02:00:45 +02:00
|
|
|
use crate::core::alignment;
|
2023-05-04 13:00:16 +02:00
|
|
|
use crate::core::text::{LineHeight, Shaping};
|
2023-08-30 04:31:21 +02:00
|
|
|
use crate::core::{Color, Font, Pixels, Point};
|
2020-03-05 22:05:05 -07:00
|
|
|
|
2020-03-07 22:27:02 +01:00
|
|
|
/// A bunch of text that can be drawn to a canvas
|
2020-03-05 22:05:05 -07:00
|
|
|
#[derive(Debug, Clone)]
|
2020-03-07 22:27:02 +01:00
|
|
|
pub struct Text {
|
2020-03-05 22:05:05 -07:00
|
|
|
/// The contents of the text
|
|
|
|
|
pub content: String,
|
2022-06-22 17:32:55 -04:00
|
|
|
/// The position of the text relative to the alignment properties.
|
|
|
|
|
/// By default, this position will be relative to the top-left corner coordinate meaning that
|
|
|
|
|
/// if the horizontal and vertical alignments are unchanged, this property will tell where the
|
|
|
|
|
/// top-left corner of the text should be placed.
|
|
|
|
|
/// By changing the horizontal_alignment and vertical_alignment properties, you are are able to
|
|
|
|
|
/// change what part of text is placed at this positions.
|
|
|
|
|
/// For example, when the horizontal_alignment and vertical_alignment are set to Center, the
|
|
|
|
|
/// center of the text will be placed at the given position NOT the top-left coordinate.
|
2020-03-07 23:45:54 +01:00
|
|
|
pub position: Point,
|
2020-03-05 22:05:05 -07:00
|
|
|
/// The color of the text
|
|
|
|
|
pub color: Color,
|
|
|
|
|
/// The size of the text
|
2023-08-30 04:31:21 +02:00
|
|
|
pub size: Pixels,
|
2023-05-04 13:00:16 +02:00
|
|
|
/// The line height of the text.
|
|
|
|
|
pub line_height: LineHeight,
|
2020-03-05 22:05:05 -07:00
|
|
|
/// The font of the text
|
|
|
|
|
pub font: Font,
|
|
|
|
|
/// The horizontal alignment of the text
|
2021-09-20 15:09:55 +07:00
|
|
|
pub horizontal_alignment: alignment::Horizontal,
|
2020-03-05 22:05:05 -07:00
|
|
|
/// The vertical alignment of the text
|
2021-09-20 15:09:55 +07:00
|
|
|
pub vertical_alignment: alignment::Vertical,
|
2023-04-19 02:00:45 +02:00
|
|
|
/// The shaping strategy of the text.
|
|
|
|
|
pub shaping: Shaping,
|
2020-03-05 22:05:05 -07:00
|
|
|
}
|
2020-03-08 00:06:48 +01:00
|
|
|
|
|
|
|
|
impl Default for Text {
|
|
|
|
|
fn default() -> Text {
|
|
|
|
|
Text {
|
|
|
|
|
content: String::new(),
|
|
|
|
|
position: Point::ORIGIN,
|
|
|
|
|
color: Color::BLACK,
|
2023-08-30 04:31:21 +02:00
|
|
|
size: Pixels(16.0),
|
2023-05-04 13:00:16 +02:00
|
|
|
line_height: LineHeight::Relative(1.2),
|
2023-03-30 00:56:00 +02:00
|
|
|
font: Font::default(),
|
2021-09-20 15:09:55 +07:00
|
|
|
horizontal_alignment: alignment::Horizontal::Left,
|
|
|
|
|
vertical_alignment: alignment::Vertical::Top,
|
2023-04-19 02:00:45 +02:00
|
|
|
shaping: Shaping::Basic,
|
2020-03-08 00:06:48 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-04-14 06:43:43 +02:00
|
|
|
|
|
|
|
|
impl From<String> for Text {
|
|
|
|
|
fn from(content: String) -> Text {
|
|
|
|
|
Text {
|
|
|
|
|
content,
|
|
|
|
|
..Default::default()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-04-14 06:43:58 +02:00
|
|
|
|
|
|
|
|
impl From<&str> for Text {
|
|
|
|
|
fn from(content: &str) -> Text {
|
|
|
|
|
String::from(content).into()
|
|
|
|
|
}
|
|
|
|
|
}
|