Skip to content

Commit

Permalink
Warn if using an TextShape from before a change to pixels_per_point
Browse files Browse the repository at this point in the history
Closes #1915
  • Loading branch information
emilk committed Aug 16, 2022
1 parent 9c58f12 commit 39b63f6
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 0 deletions.
9 changes: 9 additions & 0 deletions epaint/src/shape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions epaint/src/tessellator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
1 change: 1 addition & 0 deletions epaint/src/text/text_layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand Down
8 changes: 8 additions & 0 deletions epaint/src/text/text_layout_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)]
Expand Down

0 comments on commit 39b63f6

Please sign in to comment.