slint/internal/backends/mcu/lengths.rs
Simon Hausmann f912ec7e6b Fix scaling of glyphs and improve type safety in the MCU backend
The code was mixing logical and physical sizes, causing glyphs being
doubly scaled down. Instead, this patch introduces:

 * Physical* and Logical* euclid length/size/rect aliases
 * some extraction traits for getting the scalars in rects/sizes as lengths (until euclid has them
built-in)
 * wrapper traits/types for safely extracting the physical font metrics the
 compiler generates (i16)
 * Fix a bug in the text height calculation where we failed to take the
   descent into account
2022-02-17 15:07:57 +01:00

87 lines
2.4 KiB
Rust

// Copyright © SixtyFPS GmbH <info@slint-ui.com>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-commercial
pub struct PhysicalPx;
pub type PhysicalLength = euclid::Length<i16, PhysicalPx>;
pub type PhysicalRect = euclid::Rect<i16, PhysicalPx>;
pub type PhysicalSize = euclid::Size2D<i16, PhysicalPx>;
pub type PhysicalPoint = euclid::Point2D<i16, PhysicalPx>;
pub struct LogicalPx;
pub type LogicalLength = euclid::Length<f32, LogicalPx>;
pub type LogicalRect = euclid::Rect<f32, LogicalPx>;
pub type LogicalPoint = euclid::Point2D<f32, LogicalPx>;
pub type LogicalSize = euclid::Size2D<f32, LogicalPx>;
pub type ScaleFactor = euclid::Scale<f32, LogicalPx, PhysicalPx>;
pub trait SizeLengths<LengthType> {
fn width_length(&self) -> LengthType;
fn height_length(&self) -> LengthType;
}
impl SizeLengths<LogicalLength> for LogicalSize {
fn width_length(&self) -> LogicalLength {
LogicalLength::new(self.width)
}
fn height_length(&self) -> LogicalLength {
LogicalLength::new(self.height)
}
}
impl SizeLengths<PhysicalLength> for PhysicalSize {
fn width_length(&self) -> PhysicalLength {
PhysicalLength::new(self.width)
}
fn height_length(&self) -> PhysicalLength {
PhysicalLength::new(self.height)
}
}
pub trait PointLengths<LengthType> {
fn x_length(&self) -> LengthType;
fn y_length(&self) -> LengthType;
}
impl PointLengths<LogicalLength> for LogicalPoint {
fn x_length(&self) -> LogicalLength {
LogicalLength::new(self.x)
}
fn y_length(&self) -> LogicalLength {
LogicalLength::new(self.y)
}
}
impl PointLengths<PhysicalLength> for PhysicalPoint {
fn x_length(&self) -> PhysicalLength {
PhysicalLength::new(self.x)
}
fn y_length(&self) -> PhysicalLength {
PhysicalLength::new(self.y)
}
}
pub trait RectLengths<SizeType> {
fn size_length(&self) -> SizeType;
}
impl RectLengths<LogicalSize> for LogicalRect {
fn size_length(&self) -> LogicalSize {
let size = self.size;
LogicalSize::from_lengths(size.width_length(), size.height_length())
}
}
pub trait LogicalItemGeometry {
fn logical_geometry(self: core::pin::Pin<&Self>) -> LogicalRect;
}
impl<T: i_slint_core::items::Item> LogicalItemGeometry for T {
fn logical_geometry(self: core::pin::Pin<&Self>) -> LogicalRect {
LogicalRect::from_untyped(&self.geometry())
}
}