Warn if using an TextShape from before a change to pixels_per_point

Closes https://github.com/emilk/egui/issues/1915
This commit is contained in:
Emil Ernerfeldt 2022-08-16 22:47:23 +02:00
parent 9c58f12a6c
commit 39b63f6163
4 changed files with 23 additions and 0 deletions

View file

@ -13,6 +13,11 @@ pub use crate::{CubicBezierShape, QuadraticBezierShape};
/// A paint primitive such as a circle or a piece of text.
/// Coordinates are all screen space points (not physical pixels).
///
/// You should generally recreate your [`Shape`]s each frame,
/// but storing them should also be fine with one exception:
/// [`Shape::Text`] depends on the current `pixels_per_point` (dpi scale)
/// and so must be recreated every time `pixels_per_point` changes.
#[must_use = "Add a Shape to a Painter"]
#[derive(Clone, Debug, PartialEq)]
pub enum Shape {
@ -37,6 +42,8 @@ pub enum Shape {
Rect(RectShape),
/// Text.
///
/// This needs to be recreated if `pixels_per_point` (dpi scale) changes.
Text(TextShape),
/// A general triangle mesh.
@ -604,6 +611,8 @@ impl Rounding {
// ----------------------------------------------------------------------------
/// How to paint some text on screen.
///
/// This needs to be recreated if `pixels_per_point` (dpi scale) changes.
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct TextShape {

View file

@ -1334,6 +1334,11 @@ impl Tessellator {
return;
}
if galley.pixels_per_point != self.pixels_per_point {
eprintln!("epaint: WARNING: pixels_per_point (dpi scale) have changed between text layout and tessellation. \
You must recreate your text shapes if pixels_per_point changes.");
}
out.vertices.reserve(galley.num_vertices);
out.indices.reserve(galley.num_indices);

View file

@ -478,6 +478,7 @@ fn galley_from_rows(point_scale: PointScale, job: Arc<LayoutJob>, mut rows: Vec<
mesh_bounds,
num_vertices,
num_indices,
pixels_per_point: point_scale.pixels_per_point,
}
}

View file

@ -309,6 +309,8 @@ impl Default for TextWrapping {
/// Text that has been layed out, ready for painting.
///
/// You can create a [`Galley`] using [`crate::Fonts::layout_job`];
///
/// This needs to be recreated if `pixels_per_point` (dpi scale) changes.
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct Galley {
@ -341,6 +343,12 @@ pub struct Galley {
/// Total number of indices in all the row meshes.
pub num_indices: usize,
/// The number of physical pixels for each logical point.
/// Since this affects the layout, we keep track of it
/// so that we can warn if this has changed once we get to
/// tessellation.
pub pixels_per_point: f32,
}
#[derive(Clone, Debug, PartialEq)]