Plot text can now contain rich text

This commit is contained in:
Emil Ernerfeldt 2022-01-23 12:01:34 +01:00
parent dba949240f
commit bb407e9b00
2 changed files with 18 additions and 17 deletions

View file

@ -267,6 +267,7 @@ impl RichText {
/// but it can be a [`RichText`] (text with color, style, etc), /// but it can be a [`RichText`] (text with color, style, etc),
/// a [`LayoutJob`] (for when you want full control of how the text looks) /// a [`LayoutJob`] (for when you want full control of how the text looks)
/// or text that has already been layed out in a [`Galley`]. /// or text that has already been layed out in a [`Galley`].
#[derive(Clone)]
pub enum WidgetText { pub enum WidgetText {
RichText(RichText), RichText(RichText),
/// Use this [`LayoutJob`] when laying out the text. /// Use this [`LayoutJob`] when laying out the text.

View file

@ -614,8 +614,7 @@ impl PlotItem for Polygon {
/// Text inside the plot. /// Text inside the plot.
pub struct Text { pub struct Text {
pub(super) text: String, pub(super) text: WidgetText,
pub(super) style: TextStyle,
pub(super) position: Value, pub(super) position: Value,
pub(super) name: String, pub(super) name: String,
pub(super) highlight: bool, pub(super) highlight: bool,
@ -624,11 +623,9 @@ pub struct Text {
} }
impl Text { impl Text {
#[allow(clippy::needless_pass_by_value)] pub fn new(position: Value, text: impl Into<WidgetText>) -> Self {
pub fn new(position: Value, text: impl ToString) -> Self {
Self { Self {
text: text.to_string(), text: text.into(),
style: TextStyle::Small,
position, position,
name: Default::default(), name: Default::default(),
highlight: false, highlight: false,
@ -643,13 +640,7 @@ impl Text {
self self
} }
/// Text style. Default is `TextStyle::Small`. /// Text color.
pub fn style(mut self, style: TextStyle) -> Self {
self.style = style;
self
}
/// Text color. Default is `Color32::TRANSPARENT` which means a color will be auto-assigned.
pub fn color(mut self, color: impl Into<Color32>) -> Self { pub fn color(mut self, color: impl Into<Color32>) -> Self {
self.color = color.into(); self.color = color.into();
self self
@ -681,14 +672,23 @@ impl PlotItem for Text {
} else { } else {
self.color self.color
}; };
let galley =
self.text
.clone()
.into_galley(ui, Some(false), f32::INFINITY, TextStyle::Small);
let pos = transform.position_from_value(&self.position); let pos = transform.position_from_value(&self.position);
let galley = ui
.fonts()
.layout_no_wrap(self.text.clone(), self.style, color);
let rect = self let rect = self
.anchor .anchor
.anchor_rect(Rect::from_min_size(pos, galley.size())); .anchor_rect(Rect::from_min_size(pos, galley.size()));
shapes.push(Shape::galley(rect.min, galley));
let mut text_shape = epaint::TextShape::new(rect.min, galley.galley);
if !galley.galley_has_color {
text_shape.override_text_color = Some(color);
}
shapes.push(text_shape.into());
if self.highlight { if self.highlight {
shapes.push(Shape::rect_stroke( shapes.push(Shape::rect_stroke(
rect.expand(2.0), rect.expand(2.0),