Skip to content

Commit 0183bfd

Browse files
committed
[WIP] Add pixel ratio config, document Style/Layout as logical/physical pixels
Per #460 (comment) See details in doc-comment changes.
1 parent 10652b6 commit 0183bfd

File tree

4 files changed

+74
-29
lines changed

4 files changed

+74
-29
lines changed

RELEASES.md

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,32 @@
44

55
### Breaking
66

7-
Many APIs have been renamed to replace `points` or `Points` with `length` or `Length`.
8-
This new name better describes one-dimentional measure of space in some unspecified unit
9-
which is often unrelated to the PostScript point or the CSS `pt` unit.
7+
- Previously, absolute length values in `Style` and `Layout` were measured in some unspecified abstract unit whose meaning was chosen by Taffy users. Instead, they are now redefinied so that `Style` uses "logical pixel" which correspond to CSS `px`, while `Layout` contains "output pixels" which are recommended to use as device pixels. A new pixel ratio configuration controls the relationship between these two units. See `Taffy::set_pixel_ratio` documentation for details.
108

11-
This also removes a misleading similarity with the 2D `Point`,
12-
whose components can have any unit and are not even necessarily absolute lengths.
9+
- Many APIs have been renamed to replace `points` or `Points` with `length` or `Length`.
10+
The previous name suggested the PostScript point or the CSS `pt` unit,
11+
but per the above these values are now measured in CSS `px`.
1312

14-
Example usage change:
13+
This also removes a misleading similarity with the 2D `Point` struct,
14+
whose components can have any unit and are not even necessarily absolute lengths.
1515

16-
```diff
17-
use taffy::prelude::*;
16+
Example usage change:
1817

19-
// …
18+
```diff
19+
use taffy::prelude::*;
20+
21+
// …
22+
23+
let header_node = taffy
24+
.new_leaf(
25+
Style {
26+
- size: Size { width: points(800.0), height: points(100.0) },
27+
+ size: Size { width: length(800.0), height: length(100.0) },
28+
..Default::default()
29+
},
30+
).unwrap();
31+
```
2032

21-
let header_node = taffy
22-
.new_leaf(
23-
Style {
24-
- size: Size { width: points(800.0), height: points(100.0) },
25-
+ size: Size { width: length(800.0), height: length(100.0) },
26-
..Default::default()
27-
},
28-
).unwrap();
29-
```
3033

3134
### Removed
3235

src/style/dimension.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
//! Style types for representing lengths / sizes
22
33
use crate::geometry::{Rect, Size};
4+
#[cfg(doc)]
5+
use crate::prelude::Taffy;
46
use crate::style_helpers::{FromLength, FromPercent, TaffyAuto, TaffyMaxContent, TaffyMinContent, TaffyZero};
57
use crate::util::sys::abs;
68

@@ -10,8 +12,9 @@ use crate::util::sys::abs;
1012
#[derive(Copy, Clone, PartialEq, Debug)]
1113
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1214
pub enum LengthPercentage {
13-
/// An absolute length in some abstract units. Users of Taffy may define what they correspond
14-
/// to in their application (pixels, logical pixels, mm, etc) as they see fit.
15+
/// An absolute length in CSS `px` units a.k.a. logical pixels.
16+
///
17+
/// See [`Taffy::set_pixel_ratio`].
1518
Length(f32),
1619
/// The dimension is stored in percentage relative to the parent item.
1720
Percent(f32),
@@ -36,8 +39,9 @@ impl FromPercent for LengthPercentage {
3639
#[derive(Copy, Clone, PartialEq, Debug)]
3740
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3841
pub enum LengthPercentageAuto {
39-
/// An absolute length in some abstract units. Users of Taffy may define what they correspond
40-
/// to in their application (pixels, logical pixels, mm, etc) as they see fit.
42+
/// An absolute length in CSS `px` units a.k.a. logical pixels.
43+
///
44+
/// See [`Taffy::set_pixel_ratio`].
4145
Length(f32),
4246
/// The dimension is stored in percentage relative to the parent item.
4347
Percent(f32),
@@ -90,8 +94,9 @@ impl LengthPercentageAuto {
9094
#[derive(Copy, Clone, PartialEq, Debug)]
9195
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
9296
pub enum Dimension {
93-
/// An absolute length in some abstract units. Users of Taffy may define what they correspond
94-
/// to in their application (pixels, logical pixels, mm, etc) as they see fit.
97+
/// An absolute length in CSS `px` units a.k.a. logical pixels.
98+
///
99+
/// See [`Taffy::set_pixel_ratio`].
95100
Length(f32),
96101
/// The dimension is stored in percentage relative to the parent item.
97102
Percent(f32),

src/tree/layout.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
//! Final data structures that represent the high-level UI layout
22
33
use crate::geometry::{Point, Size};
4+
#[cfg(doc)]
5+
use crate::prelude::Taffy;
46

57
/// Whether we are performing a full layout, or we merely need to size the node
68
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
@@ -31,6 +33,7 @@ pub enum SizingMode {
3133
pub struct SizeAndBaselines {
3234
/// The size of the node
3335
pub size: Size<f32>,
36+
3437
/// The first baseline of the node in each dimension, if any
3538
pub first_baselines: Point<Option<f32>>,
3639
}
@@ -49,9 +52,15 @@ pub struct Layout {
4952
/// Nodes with a higher order should be rendered on top of those with a lower order.
5053
/// This is effectively a topological sort of each tree.
5154
pub order: u32,
52-
/// The width and height of the node
55+
56+
/// The width and height of the node, in output pixels.
57+
///
58+
/// See [`Taffy::set_pixel_ratio`].
5359
pub size: Size<f32>,
54-
/// The bottom-left corner of the node
60+
61+
/// The bottom-left corner of the node, in output pixels.
62+
///
63+
/// See [`Taffy::set_pixel_ratio`].
5564
pub location: Point<f32>,
5665
}
5766

src/tree/taffy_tree/tree.rs

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,15 @@ use super::{TaffyError, TaffyResult};
1616
pub(crate) struct TaffyConfig {
1717
/// Whether to round layout values
1818
pub(crate) use_rounding: bool,
19+
20+
/// Number of output length units per input length units.
21+
/// See `pixel_ratio` and `set_pixel_ratio` methods.
22+
pub(crate) pixel_ratio: f32,
1923
}
2024

2125
impl Default for TaffyConfig {
2226
fn default() -> Self {
23-
Self { use_rounding: true }
27+
Self { use_rounding: true, pixel_ratio: 1.0 }
2428
}
2529
}
2630

@@ -144,16 +148,40 @@ impl Taffy {
144148
}
145149
}
146150

147-
/// Enable rounding of layout values. Rounding is enabled by default.
151+
/// Enable rounding of sizes and locations in [`Layout`].
152+
/// Rounding is enabled by default.
148153
pub fn enable_rounding(&mut self) {
149154
self.config.use_rounding = true;
150155
}
151156

152-
/// Disable rounding of layout values. Rounding is enabled by default.
157+
/// Disable rounding of sizes and locations in [`Layout`].
158+
/// Rounding is enabled by default.
153159
pub fn disable_rounding(&mut self) {
154160
self.config.use_rounding = false;
155161
}
156162

163+
/// Returns the current pixel ratio. See [`set_pixel_ratio`][Self::set_pixel_ratio].
164+
pub fn pixel_ratio(&self) -> f32 {
165+
self.config.pixel_ratio
166+
}
167+
168+
/// Sets the pixel ratio: the number of output pixels per logical pixels.
169+
///
170+
/// Absolute length values in [`style`][crate::style] are measured in **logical pixels**
171+
/// and correspond to CSS the `px` unit.
172+
///
173+
/// Sizes and locations in [`Layout`] are measured in output pixels.
174+
/// When rendering to screen media, it is recommended to use them as **physical pixels**
175+
/// a.k.a. [device pixels](https://drafts.csswg.org/css-values/#device-pixel)
176+
/// and set the pixel ratio to the whole number that makes a logical pixel best approximate
177+
/// the [reference pixel](https://drafts.csswg.org/css-values/#reference-pixel),
178+
/// (the visual angle of one pixel on a 96dpi device held at arm’s length).
179+
///
180+
/// The default is 1.0.
181+
pub fn set_pixel_ratio(&mut self, new_pixel_ratio: f32) {
182+
self.config.pixel_ratio = new_pixel_ratio
183+
}
184+
157185
/// Creates and adds a new unattached leaf node to the tree, and returns the node of the new node
158186
pub fn new_leaf(&mut self, layout: Style) -> TaffyResult<NodeId> {
159187
let id = self.nodes.insert(NodeData::new(layout));

0 commit comments

Comments
 (0)