Skip to content

Commit

Permalink
Fix DPI scaling when building display list
Browse files Browse the repository at this point in the history
  • Loading branch information
fschutt committed Aug 11, 2023
1 parent c994da3 commit 82ab522
Show file tree
Hide file tree
Showing 5 changed files with 361 additions and 9 deletions.
134 changes: 134 additions & 0 deletions azul-core/src/display_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,26 @@ pub struct GlyphInstance {
pub size: LogicalSize,
}

impl GlyphInstance {
pub fn scale_for_dpi(&mut self, scale_factor: f32) {
self.point.scale_for_dpi(scale_factor);
self.size.scale_for_dpi(scale_factor);
}
}

#[derive(Debug, Copy, Clone, PartialEq, PartialOrd)]
pub struct DisplayListImageMask {
pub image: ImageKey,
pub rect: LogicalRect,
pub repeat: bool,
}

impl DisplayListImageMask {
pub fn scale_for_dpi(&mut self, scale_factor: f32) {
self.rect.scale_for_dpi(scale_factor);
}
}

#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub struct CachedDisplayList {
pub root: DisplayListMsg,
Expand All @@ -59,6 +72,12 @@ impl CachedDisplayList {
root_size: LogicalSize::zero(),
}
}

pub fn scale_for_dpi(&mut self, scale_factor: f32) {
self.root_size.width *= scale_factor;
self.root_size.height *= scale_factor;
self.root.scale_for_dpi(scale_factor);
}
}

#[derive(Debug, Clone, PartialEq, PartialOrd)]
Expand All @@ -70,6 +89,23 @@ pub enum DisplayListMsg {
}

impl DisplayListMsg {

pub fn scale_for_dpi(&mut self, scale_factor: f32) {
match self {
DisplayListMsg::IFrame(_, s, _, dl) => {
s.width *= scale_factor;
s.height *= scale_factor;
dl.scale_for_dpi(scale_factor);
},
DisplayListMsg::Frame(f) => {
f.scale_for_dpi(scale_factor);
},
DisplayListMsg::ScrollFrame(sf) => {
sf.scale_for_dpi(scale_factor);
}
}
}

pub fn get_transform_key(&self) -> Option<&(TransformKey, ComputedTransform3D)> {
use self::DisplayListMsg::*;
match self {
Expand Down Expand Up @@ -212,6 +248,14 @@ pub struct DisplayListScrollFrame {
pub frame: DisplayListFrame,
}

impl DisplayListScrollFrame {
pub fn scale_for_dpi(&mut self, scale_factor: f32) {
self.parent_rect.scale_for_dpi(scale_factor);
self.content_rect.scale_for_dpi(scale_factor);
self.frame.scale_for_dpi(scale_factor);
}
}

impl fmt::Debug for DisplayListScrollFrame {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "DisplayListScrollFrame {{\r\n")?;
Expand Down Expand Up @@ -284,6 +328,22 @@ impl fmt::Debug for DisplayListFrame {
}

impl DisplayListFrame {
pub fn scale_for_dpi(&mut self, scale_factor: f32) {
self.size.width *= scale_factor;
self.size.height *= scale_factor;
self.position.scale_for_dpi(scale_factor);
self.clip_children.as_mut().map(|s| s.scale_for_dpi(scale_factor));
self.clip_mask.as_mut().map(|s| s.scale_for_dpi(scale_factor));
self.border_radius.scale_for_dpi(scale_factor);
self.transform.as_mut().map(|(k, v)| v.scale_for_dpi(scale_factor));
for c in self.content.iter_mut() {
c.scale_for_dpi(scale_factor);
}
for c in self.children.iter_mut() {
c.scale_for_dpi(scale_factor);
}
}

pub fn root(dimensions: LayoutSize, root_origin: LayoutPoint) -> Self {
use crate::ui_solver::PositionInfoInner;
DisplayListFrame {
Expand Down Expand Up @@ -343,7 +403,15 @@ impl StyleBorderRadius {
&& self.bottom_left.is_none()
&& self.bottom_right.is_none()
}

pub fn scale_for_dpi(&mut self, scale_factor: f32) {
self.top_left.as_mut().map(|s| s.scale_for_dpi(scale_factor));
self.top_right.as_mut().map(|s| s.scale_for_dpi(scale_factor));
self.bottom_left.as_mut().map(|s| s.scale_for_dpi(scale_factor));
self.bottom_right.as_mut().map(|s| s.scale_for_dpi(scale_factor));
}
}

impl fmt::Debug for StyleBorderRadius {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "StyleBorderRadius {{")?;
Expand Down Expand Up @@ -395,6 +463,14 @@ pub struct StyleBorderWidths {
}

impl StyleBorderWidths {

pub fn scale_for_dpi(&mut self, scale_factor: f32) {
self.top.as_mut().map(|s| s.scale_for_dpi(scale_factor));
self.right.as_mut().map(|s| s.scale_for_dpi(scale_factor));
self.bottom.as_mut().map(|s| s.scale_for_dpi(scale_factor));
self.left.as_mut().map(|s| s.scale_for_dpi(scale_factor));
}

#[inline]
pub fn left_width(&self) -> f32 {
self.left
Expand Down Expand Up @@ -510,6 +586,55 @@ pub enum LayoutRectContent {
},
}

impl LayoutRectContent {
pub fn scale_for_dpi(&mut self, scale_factor: f32) {
use self::LayoutRectContent::*;
match self {
Text {
glyphs,
font_instance_key,
color,
glyph_options,
overflow,
text_shadow,
} => {
for g in glyphs.iter_mut() {
g.scale_for_dpi(scale_factor);
}
text_shadow.as_mut().map(|s| s.scale_for_dpi(scale_factor));
},
Background {
content,
size,
offset,
repeat,
} => {
content.scale_for_dpi(scale_factor);
size.as_mut().map(|s| s.scale_for_dpi(scale_factor));
offset.as_mut().map(|s| s.scale_for_dpi(scale_factor));
},
Image {
size,
offset,
image_rendering,
alpha_type,
image_key,
background_color,
} => {
size.scale_for_dpi(scale_factor);
offset.scale_for_dpi(scale_factor);
},
Border {
widths,
colors,
styles,
} => {
widths.scale_for_dpi(scale_factor);
},
}
}
}

impl fmt::Debug for LayoutRectContent {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use self::LayoutRectContent::*;
Expand Down Expand Up @@ -616,6 +741,15 @@ impl fmt::Debug for RectBackground {
}

impl RectBackground {
pub fn scale_for_dpi(&mut self, scale_factor: f32) {
match self {
RectBackground::Image((_key, descriptor)) => {
descriptor.width = libm::round(descriptor.width as f64 * scale_factor as f64) as usize;
descriptor.height = libm::round(descriptor.height as f64 * scale_factor as f64) as usize;
}
_ => { },
}
}
pub fn get_content_size(&self) -> Option<(f32, f32)> {
match self {
RectBackground::Image((_key, descriptor)) => {
Expand Down
28 changes: 20 additions & 8 deletions azul-core/src/ui_solver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1596,18 +1596,18 @@ pub enum PositionInfo {
Relative(PositionInfoInner),
}

/*
impl ::core::fmt::Debug for PositionInfo {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {

impl PositionInfo {
pub fn scale_for_dpi(&mut self, scale_factor: f32) {
match self {
PositionInfo::Static(p) => write!(f, "static({}, {})", p.x_offset, p.y_offset),
PositionInfo::Fixed(p) => write!(f, "fixed({}, {})", p.x_offset, p.y_offset),
PositionInfo::Absolute(p) => write!(f, "absolute({}, {})", p.x_offset, p.y_offset),
PositionInfo::Relative(p) => write!(f, "relative({}, {})", p.x_offset, p.y_offset),
PositionInfo::Static(p) => p.scale_for_dpi(scale_factor),
PositionInfo::Fixed(p) => p.scale_for_dpi(scale_factor),
PositionInfo::Absolute(p) => p.scale_for_dpi(scale_factor),
PositionInfo::Relative(p) => p.scale_for_dpi(scale_factor),
}
}
}
*/

impl_option!(
PositionInfo,
OptionPositionInfo,
Expand All @@ -1633,6 +1633,13 @@ impl PositionInfoInner {
static_y_offset: 0.0,
}
}

pub fn scale_for_dpi(&mut self, scale_factor: f32) {
self.x_offset *= scale_factor;
self.y_offset *= scale_factor;
self.static_x_offset *= scale_factor;
self.static_y_offset *= scale_factor;
}
}

impl PositionInfo {
Expand Down Expand Up @@ -2201,6 +2208,11 @@ impl ComputedTransform3D {
Some(LogicalPosition { x: x / w, y: y / w })
}

pub fn scale_for_dpi(&mut self, scale_factor: f32) {
let scale_matrix = Self::new_scale(scale_factor, scale_factor, 1.0);
*self = self.then(&scale_matrix);
}

/// Computes the sum of two matrices while applying `other` AFTER the current matrix.
#[must_use]
#[inline]
Expand Down
23 changes: 23 additions & 0 deletions azul-core/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2688,6 +2688,14 @@ impl LogicalRect {
Self { origin, size }
}

#[inline]
pub fn scale_for_dpi(&mut self, scale_factor: f32) {
self.origin.x *= scale_factor;
self.origin.y *= scale_factor;
self.size.width *= scale_factor;
self.size.height *= scale_factor;
}

#[inline(always)]
pub fn max_x(&self) -> f32 {
self.origin.x + self.size.width
Expand Down Expand Up @@ -2786,6 +2794,13 @@ pub struct LogicalPosition {
pub y: f32,
}

impl LogicalPosition {
pub fn scale_for_dpi(&mut self, scale_factor: f32) {
self.x *= scale_factor;
self.y *= scale_factor;
}
}

impl SubAssign<LogicalPosition> for LogicalPosition {
fn sub_assign(&mut self, other: LogicalPosition) {
self.x -= other.x;
Expand Down Expand Up @@ -2875,6 +2890,14 @@ pub struct LogicalSize {
pub height: f32,
}

impl LogicalSize {
pub fn scale_for_dpi(&mut self, scale_factor: f32) -> Self {
self.width *= scale_factor;
self.height *= scale_factor;
*self
}
}

impl core::fmt::Debug for LogicalSize {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
write!(f, "{}x{}", self.width, self.height)
Expand Down
Loading

0 comments on commit 82ab522

Please sign in to comment.