From e0c26b4f36cb9693d53b03695ef5fb6c084334bf Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Fri, 24 Mar 2023 07:55:07 -0600 Subject: [PATCH] Fix scaling issues --- Cargo.lock | 112 ++++++++++++++++++++++++------------------------ Cargo.toml | 1 + src/text_box.rs | 59 +++++++++++-------------- 3 files changed, 81 insertions(+), 91 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 83536fd..45f7ba3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -67,9 +67,9 @@ dependencies = [ [[package]] name = "anyhow" -version = "1.0.69" +version = "1.0.70" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "224afbd727c3d6e4b90103ece64b8d1b67fbb1973b1046c2281eed3f3803f800" +checksum = "7de8ce5e0f9f8d88245311066a578d72b7af3e7088f32783804676302df237e4" [[package]] name = "apply" @@ -88,9 +88,9 @@ dependencies = [ [[package]] name = "arrayref" -version = "0.3.6" +version = "0.3.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a4c527152e37cf757a3f78aae5a06fbeefdb07ccc535c980a3208ee3060dd544" +checksum = "6b4930d2cb77ce62f89ee5d5289b4ac049559b1c45539271f5ed4fdc7db34545" [[package]] name = "arrayvec" @@ -813,9 +813,9 @@ dependencies = [ [[package]] name = "euclid" -version = "0.22.7" +version = "0.22.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b52c2ef4a78da0ba68fbe1fd920627411096d2ac478f7f4c9f3a54ba6705bade" +checksum = "ff8b5875b64eb55f4b554f9d0e6413da078d96374974d8bb3bed5f02d55451ce" dependencies = [ "num-traits", ] @@ -1414,7 +1414,7 @@ checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" [[package]] name = "iced" version = "0.6.0" -source = "git+https://github.com/pop-os/libcosmic#24709e9c3b56c49a0af168d1e37ebef85f112c8a" +source = "git+https://github.com/pop-os/libcosmic?branch=scale-factor#6ce220e64cb82ecb2f65dfdc0a1304cdf9fcb958" dependencies = [ "iced_core", "iced_dyrend", @@ -1432,7 +1432,7 @@ dependencies = [ [[package]] name = "iced_core" version = "0.6.2" -source = "git+https://github.com/pop-os/libcosmic#24709e9c3b56c49a0af168d1e37ebef85f112c8a" +source = "git+https://github.com/pop-os/libcosmic?branch=scale-factor#6ce220e64cb82ecb2f65dfdc0a1304cdf9fcb958" dependencies = [ "bitflags", "palette", @@ -1442,7 +1442,7 @@ dependencies = [ [[package]] name = "iced_dyrend" version = "0.1.0" -source = "git+https://github.com/pop-os/libcosmic#24709e9c3b56c49a0af168d1e37ebef85f112c8a" +source = "git+https://github.com/pop-os/libcosmic?branch=scale-factor#6ce220e64cb82ecb2f65dfdc0a1304cdf9fcb958" dependencies = [ "iced_glow", "iced_graphics", @@ -1456,7 +1456,7 @@ dependencies = [ [[package]] name = "iced_futures" version = "0.5.1" -source = "git+https://github.com/pop-os/libcosmic#24709e9c3b56c49a0af168d1e37ebef85f112c8a" +source = "git+https://github.com/pop-os/libcosmic?branch=scale-factor#6ce220e64cb82ecb2f65dfdc0a1304cdf9fcb958" dependencies = [ "futures", "log", @@ -1467,7 +1467,7 @@ dependencies = [ [[package]] name = "iced_glow" version = "0.5.1" -source = "git+https://github.com/pop-os/libcosmic#24709e9c3b56c49a0af168d1e37ebef85f112c8a" +source = "git+https://github.com/pop-os/libcosmic?branch=scale-factor#6ce220e64cb82ecb2f65dfdc0a1304cdf9fcb958" dependencies = [ "bytemuck", "euclid", @@ -1482,7 +1482,7 @@ dependencies = [ [[package]] name = "iced_graphics" version = "0.5.0" -source = "git+https://github.com/pop-os/libcosmic#24709e9c3b56c49a0af168d1e37ebef85f112c8a" +source = "git+https://github.com/pop-os/libcosmic?branch=scale-factor#6ce220e64cb82ecb2f65dfdc0a1304cdf9fcb958" dependencies = [ "bitflags", "bytemuck", @@ -1502,7 +1502,7 @@ dependencies = [ [[package]] name = "iced_lazy" version = "0.3.0" -source = "git+https://github.com/pop-os/libcosmic#24709e9c3b56c49a0af168d1e37ebef85f112c8a" +source = "git+https://github.com/pop-os/libcosmic?branch=scale-factor#6ce220e64cb82ecb2f65dfdc0a1304cdf9fcb958" dependencies = [ "iced_native", "ouroboros 0.13.0", @@ -1511,7 +1511,7 @@ dependencies = [ [[package]] name = "iced_native" version = "0.7.0" -source = "git+https://github.com/pop-os/libcosmic#24709e9c3b56c49a0af168d1e37ebef85f112c8a" +source = "git+https://github.com/pop-os/libcosmic?branch=scale-factor#6ce220e64cb82ecb2f65dfdc0a1304cdf9fcb958" dependencies = [ "iced_core", "iced_futures", @@ -1524,7 +1524,7 @@ dependencies = [ [[package]] name = "iced_softbuffer" version = "0.1.0" -source = "git+https://github.com/pop-os/libcosmic#24709e9c3b56c49a0af168d1e37ebef85f112c8a" +source = "git+https://github.com/pop-os/libcosmic?branch=scale-factor#6ce220e64cb82ecb2f65dfdc0a1304cdf9fcb958" dependencies = [ "cosmic-text", "iced_graphics", @@ -1539,7 +1539,7 @@ dependencies = [ [[package]] name = "iced_style" version = "0.5.1" -source = "git+https://github.com/pop-os/libcosmic#24709e9c3b56c49a0af168d1e37ebef85f112c8a" +source = "git+https://github.com/pop-os/libcosmic?branch=scale-factor#6ce220e64cb82ecb2f65dfdc0a1304cdf9fcb958" dependencies = [ "iced_core", "once_cell", @@ -1549,7 +1549,7 @@ dependencies = [ [[package]] name = "iced_wgpu" version = "0.7.0" -source = "git+https://github.com/pop-os/libcosmic#24709e9c3b56c49a0af168d1e37ebef85f112c8a" +source = "git+https://github.com/pop-os/libcosmic?branch=scale-factor#6ce220e64cb82ecb2f65dfdc0a1304cdf9fcb958" dependencies = [ "bitflags", "bytemuck", @@ -1569,7 +1569,7 @@ dependencies = [ [[package]] name = "iced_winit" version = "0.6.0" -source = "git+https://github.com/pop-os/libcosmic#24709e9c3b56c49a0af168d1e37ebef85f112c8a" +source = "git+https://github.com/pop-os/libcosmic?branch=scale-factor#6ce220e64cb82ecb2f65dfdc0a1304cdf9fcb958" dependencies = [ "iced_futures", "iced_graphics", @@ -1631,9 +1631,9 @@ dependencies = [ [[package]] name = "io-lifetimes" -version = "1.0.7" +version = "1.0.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "76e86b86ae312accbf05ade23ce76b625e0e47a255712b7414037385a1c05380" +checksum = "09270fd4fa1111bc614ed2246c7ef56239a3063d5be0d1ec3b589c505d400aeb" dependencies = [ "hermit-abi 0.3.1", "libc", @@ -1726,7 +1726,7 @@ checksum = "99227334921fae1a979cf0bfdfcc6b3e5ce376ef57e16fb6fb3ea2ed6095f80c" [[package]] name = "libcosmic" version = "0.1.0" -source = "git+https://github.com/pop-os/libcosmic#24709e9c3b56c49a0af168d1e37ebef85f112c8a" +source = "git+https://github.com/pop-os/libcosmic?branch=scale-factor#6ce220e64cb82ecb2f65dfdc0a1304cdf9fcb958" dependencies = [ "apply", "cosmic-theme", @@ -2244,9 +2244,9 @@ dependencies = [ [[package]] name = "ordered-float" -version = "3.4.0" +version = "3.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d84eb1409416d254e4a9c8fa56cc24701755025b458f0fcd8e59e1f5f40c23bf" +checksum = "13a384337e997e6860ffbaa83708b2ef329fd8c54cb67a5f64d421e0f943254f" dependencies = [ "num-traits", ] @@ -2496,14 +2496,14 @@ checksum = "6ac9a59f73473f1b8d852421e59e64809f025994837ef743615c6d0c5b305160" [[package]] name = "plist" -version = "1.4.2" +version = "1.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ffac6a51110e97610dd3ac73e34a65b27e56a1e305df41bad1f616d8e1cb22f4" +checksum = "9bd9647b268a3d3e14ff09c23201133a62589c658db02bb7388c7246aafe0590" dependencies = [ "base64 0.21.0", "indexmap", "line-wrap", - "quick-xml 0.27.1", + "quick-xml 0.28.1", "serde", "time", ] @@ -2562,9 +2562,9 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.52" +version = "1.0.53" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1d0e1ae9e836cc3beddd63db0df682593d7e2d3d891ae8c9083d2113e1744224" +checksum = "ba466839c78239c09faf015484e5cc04860f88242cff4d03eb038f04b4699b73" dependencies = [ "unicode-ident", ] @@ -2586,9 +2586,9 @@ dependencies = [ [[package]] name = "quick-xml" -version = "0.27.1" +version = "0.28.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ffc053f057dd768a56f62cd7e434c42c831d296968997e9ac1f76ea7c2d14c41" +checksum = "e5c1a97b1bc42b1d550bfb48d4262153fe400a12bab1511821736f7eac76d7e2" dependencies = [ "memchr", ] @@ -2740,9 +2740,9 @@ dependencies = [ [[package]] name = "regex" -version = "1.7.1" +version = "1.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "48aaa5748ba571fb95cd2c85c09f629215d3a6ece942baa100950af03a34f733" +checksum = "cce168fea28d3e05f158bda4576cf0c844d5045bc2cc3620fa0292ed5bb5814c" dependencies = [ "aho-corasick", "memchr", @@ -2751,9 +2751,9 @@ dependencies = [ [[package]] name = "regex-syntax" -version = "0.6.28" +version = "0.6.29" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "456c603be3e8d448b072f410900c09faf164fbce2d480456f50eea6e25f9c848" +checksum = "f162c6dd7b008981e4d40210aca20b4bd0f9b60ca9271061b07f78537722f2e1" [[package]] name = "renderdoc-sys" @@ -2941,22 +2941,22 @@ dependencies = [ [[package]] name = "serde" -version = "1.0.157" +version = "1.0.158" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "707de5fcf5df2b5788fca98dd7eab490bc2fd9b7ef1404defc462833b83f25ca" +checksum = "771d4d9c4163ee138805e12c710dd365e4f44be8be0503cb1bb9eb989425d9c9" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.157" +version = "1.0.158" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "78997f4555c22a7971214540c4a661291970619afd56de19f77e0de86296e1e5" +checksum = "e801c1712f48475582b7696ac71e0ca34ebb30e09338425384269d9717c62cad" dependencies = [ "proc-macro2", "quote", - "syn 2.0.0", + "syn 2.0.8", ] [[package]] @@ -3168,9 +3168,9 @@ dependencies = [ [[package]] name = "sw-composite" -version = "0.7.15" +version = "0.7.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0b8d4f1dd38540e3f62c393ae78e874c94491c403025368183b018e3fb098b1f" +checksum = "9ac8fb7895b4afa060ad731a32860db8755da3449a47e796d5ecf758db2671d4" [[package]] name = "swash" @@ -3195,9 +3195,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.0" +version = "2.0.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4cff13bb1732bccfe3b246f3fdb09edfd51c01d6f5299b7ccd9457c2e4e37774" +checksum = "bcc02725fd69ab9f26eab07fad303e2497fad6fb9eba4f96c4d1687bdf704ad9" dependencies = [ "proc-macro2", "quote", @@ -3264,22 +3264,22 @@ dependencies = [ [[package]] name = "thiserror" -version = "1.0.39" +version = "1.0.40" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a5ab016db510546d856297882807df8da66a16fb8c4101cb8b30054b0d5b2d9c" +checksum = "978c9a314bd8dc99be594bc3c175faaa9794be04a5a5e153caba6915336cebac" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.39" +version = "1.0.40" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5420d42e90af0c38c3290abcca25b9b3bdf379fc9f55c528f53a269d9c9a267e" +checksum = "f9456a42c5b0d803c8cd86e73dd7cc9edd429499f37a3550d286d5e86720569f" dependencies = [ "proc-macro2", "quote", - "syn 1.0.109", + "syn 2.0.8", ] [[package]] @@ -3376,9 +3376,9 @@ checksum = "3ab8ed2edee10b50132aed5f331333428b011c99402b5a534154ed15746f9622" [[package]] name = "toml_edit" -version = "0.19.7" +version = "0.19.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dc18466501acd8ac6a3f615dd29a3438f8ca6bb3b19537138b3106e575621274" +checksum = "239410c8609e8125456927e6707163a3b1fdb40561e4b803bc041f466ccfdc13" dependencies = [ "indexmap", "toml_datetime", @@ -3422,9 +3422,9 @@ checksum = "6af6ae20167a9ece4bcb41af5b80f8a1f1df981f6391189ce00fd257af04126a" [[package]] name = "unicode-bidi" -version = "0.3.12" +version = "0.3.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7d502c968c6a838ead8e69b2ee18ec708802f99db92a0d156705ec9ef801993b" +checksum = "92888ba5573ff080736b3648696b70cafad7d250551175acbaa4e0385b3e1460" [[package]] name = "unicode-bidi-mirroring" @@ -4138,9 +4138,9 @@ dependencies = [ [[package]] name = "winnow" -version = "0.3.6" +version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "23d020b441f92996c80d94ae9166e8501e59c7bb56121189dc9eab3bd8216966" +checksum = "deac0939bd6e4f24ab5919fbf751c97a8cfc8543bb083a305ed5c0c10bb241d1" dependencies = [ "memchr", ] @@ -4267,9 +4267,9 @@ checksum = "c110ba09c9b3a43edd4803d570df0da2414fed6e822e22b976a4e3ef50860701" [[package]] name = "zune-inflate" -version = "0.2.51" +version = "0.2.52" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a01728b79fb9b7e28a8c11f715e1cd8dc2cda7416a007d66cac55cebb3a8ac6b" +checksum = "10ca8ee3897e213bf74d46641942575fb9b4bd9933cbce0b40eb320836da67e0" dependencies = [ "simd-adler32", ] diff --git a/Cargo.toml b/Cargo.toml index 70b3c65..3aed8bb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,6 +18,7 @@ features = ["syntect"] [dependencies.libcosmic] git = "https://github.com/pop-os/libcosmic" +branch = "scale-factor" default-features = false features = ["winit_softbuffer"] #path = "../libcosmic" diff --git a/src/text_box.rs b/src/text_box.rs index c9741a4..c0912d0 100644 --- a/src/text_box.rs +++ b/src/text_box.rs @@ -182,11 +182,13 @@ where tree: &widget::Tree, renderer: &mut Renderer, theme: &Renderer::Theme, - _style: &renderer::Style, + style: &renderer::Style, layout: Layout<'_>, _cursor_position: Point, viewport: &Rectangle, ) { + let instant = Instant::now(); + let state = tree.state.downcast_ref::(); let appearance = theme.appearance(); @@ -217,54 +219,41 @@ where let view_h = cmp::min(viewport.height as i32, layout.bounds().height as i32) - self.padding.vertical() as i32; + let image_w = (view_w as f64 * style.scale_factor) as i32; + let image_h = (view_h as f64 * style.scale_factor) as i32; + let mut font_system = FONT_SYSTEM.lock().unwrap(); let mut editor = editor.borrow_with(&mut font_system); - editor.buffer_mut().set_size(view_w as f32, view_h as f32); + // Scale metrics + let metrics = editor.buffer().metrics(); + editor.buffer_mut().set_metrics(metrics.scale(style.scale_factor as f32)); + // Set size + editor.buffer_mut().set_size(image_w as f32, image_h as f32); + + // Shape and layout editor.shape_as_needed(); - let instant = Instant::now(); - - let mut pixels = vec![0; view_w as usize * view_h as usize * 4]; - + // Draw to pixel buffer + let mut pixels = vec![0; image_w as usize * image_h as usize * 4]; editor.draw( &mut state.cache.lock().unwrap(), text_color, |x, y, w, h, color| { - if w <= 0 || h <= 0 { - // Do not draw invalid sized rectangles - return; - } - - if w > 1 || h > 1 { - // Draw rectangles with optimized quad renderer - renderer.fill_quad( - renderer::Quad { - bounds: Rectangle::new( - layout.position() - + [x as f32, y as f32].into() - + [self.padding.left as f32, self.padding.top as f32].into(), - Size::new(w as f32, h as f32), - ), - border_radius: 0.0.into(), - border_width: 0.0, - border_color: Color::TRANSPARENT, - }, - Color::from_rgba8( - color.r(), - color.g(), - color.b(), - (color.a() as f32) / 255.0, - ), - ); - } else { - draw_pixel(&mut pixels, view_w, view_h, x, y, color); + //TODO: improve performance + for row in 0..h as i32 { + for col in 0..w as i32 { + draw_pixel(&mut pixels, image_w, image_h, x + col, y + row, color); + } } }, ); - let handle = image::Handle::from_pixels(view_w as u32, view_h as u32, pixels); + // Restore original metrics + editor.buffer_mut().set_metrics(metrics); + + let handle = image::Handle::from_pixels(image_w as u32, image_h as u32, pixels); image::Renderer::draw( renderer, handle,