Skip to content

Commit

Permalink
swrenderer: Fix rendering of SVG when SLINT_SCALE_FACTOR is defined a…
Browse files Browse the repository at this point in the history
…t compile time (slint-ui#5013)

We call fit_to with the source size (which is not scaled by the scale
factor) while the actual image is already scaled, so we need to adjust
for the difference between the texture size and the original size.

Also pass the scale_factor at compile time to the screenshot test
  • Loading branch information
ogoffart authored Apr 4, 2024
1 parent d1da3ba commit 02775f7
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 7 deletions.
14 changes: 13 additions & 1 deletion internal/core/software_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1402,7 +1402,19 @@ impl<'a, T: ProcessScene> SceneBuilder<'a, T> {

match image_inner {
ImageInner::None => (),
ImageInner::StaticTextures(StaticTextures { data, textures, .. }) => {
ImageInner::StaticTextures(StaticTextures {
data,
textures,
size,
original_size,
..
}) => {
let adjust_x = size.width as f32 / original_size.width as f32;
let adjust_y = size.height as f32 / original_size.height as f32;
let source_to_target_x = source_to_target_x / adjust_x;
let source_to_target_y = source_to_target_y / adjust_y;
let source_rect =
source_rect.cast::<f32>().scale(adjust_x, adjust_y).round().cast();
let dx = Fixed::from_f32(1. / source_to_target_x).unwrap();
let dy = Fixed::from_f32(1. / source_to_target_y).unwrap();

Expand Down
17 changes: 11 additions & 6 deletions tests/screenshots/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,16 @@ fn main() -> std::io::Result<()> {
let source = std::fs::read_to_string(&testcase.absolute_path)?;

let needle = "SLINT_SCALE_FACTOR=";
let scale_factor = if let Some(p) = source.find(needle) {
let scale_factor = source.find(needle).map(|p| {
let source = &source[p + needle.len()..];
let scale_factor: f32 = source
.find(char::is_whitespace)
.and_then(|end| source[..end].parse().ok())
.unwrap_or_else(|| {
panic!("Cannot parse {needle} for {}", testcase.relative_path.display())
});
format!("slint::platform::WindowAdapter::window(&*window).dispatch_event(slint::platform::WindowEvent::ScaleFactorChanged {{ scale_factor: {scale_factor}f32 }});")
} else {
String::new()
};
scale_factor
});

let needle = "ROTATION_THRESHOLD=";
let rotation_threshold = source.find(needle).map_or(0., |p| {
Expand All @@ -106,7 +104,12 @@ fn main() -> std::io::Result<()> {
Path::new(&std::env::var_os("OUT_DIR").unwrap()).join(format!("{}.rs", module_name)),
)?);

generate_source(source.as_str(), &mut output, testcase).unwrap();
generate_source(source.as_str(), &mut output, testcase, scale_factor.unwrap_or(1.))
.unwrap();

let scale_factor = scale_factor.map_or(String::new(), |scale_factor| {
format!("slint::platform::WindowAdapter::window(&*window).dispatch_event(slint::platform::WindowEvent::ScaleFactorChanged {{ scale_factor: {scale_factor}f32 }});")
});

write!(
output,
Expand Down Expand Up @@ -143,6 +146,7 @@ fn generate_source(
source: &str,
output: &mut impl Write,
testcase: test_driver_lib::TestCase,
scale_factor: f32,
) -> Result<(), std::io::Error> {
use i_slint_compiler::{diagnostics::BuildDiagnostics, *};

Expand All @@ -158,6 +162,7 @@ fn generate_source(
compiler_config.embed_resources = EmbedResourcesKind::EmbedTextures;
compiler_config.enable_component_containers = true;
compiler_config.style = Some("fluent".to_string());
compiler_config.scale_factor = scale_factor.into();
let (root_component, diag, _) =
spin_on::spin_on(compile_syntax_node(syntax_node, diag, compiler_config));

Expand Down
23 changes: 23 additions & 0 deletions tests/screenshots/cases/software/basic/images-scale_factor.slint
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright © SixtyFPS GmbH <info@slint.dev>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-1.1 OR LicenseRef-Slint-commercial

import { Slider } from "std-widgets.slint";

// SLINT_SCALE_FACTOR=3.2

export component TestCase inherits Window {
width: 64phx;
height: 64phx;
background: darkgray;

VerticalLayout {
Image {
source: @image-url("../../../../../logo/slint-logo-full-dark.svg");
Text { text: parent.source.width + "x" + parent.source.height; font-size: 4px; x: 0; y: 0; color: #123a;}
}
Image {
source: @image-url("../../../../../logo/slint-logo-full-dark.png");
Text { text: parent.source.width + "x" + parent.source.height; font-size: 4px; x: 0; y: 0; color: #321a;}
}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/screenshots/references/software/basic/text-clipped.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 02775f7

Please sign in to comment.