diff options
author | 2023-05-04 13:00:16 +0200 | |
---|---|---|
committer | 2023-05-04 18:39:31 +0200 | |
commit | 9499a8f9e6f9971dedfae563cb133232aa3cebc2 (patch) | |
tree | 54074dd8b1fc17d63ad92d84b6d2b4415ad29df6 /tiny_skia | |
parent | 8e8808f0e187ed6671441f5016f07bfcba426452 (diff) | |
download | iced-9499a8f9e6f9971dedfae563cb133232aa3cebc2.tar.gz iced-9499a8f9e6f9971dedfae563cb133232aa3cebc2.tar.bz2 iced-9499a8f9e6f9971dedfae563cb133232aa3cebc2.zip |
Support configurable `LineHeight` in text widgets
Diffstat (limited to 'tiny_skia')
-rw-r--r-- | tiny_skia/src/backend.rs | 20 | ||||
-rw-r--r-- | tiny_skia/src/geometry.rs | 1 | ||||
-rw-r--r-- | tiny_skia/src/text.rs | 28 |
3 files changed, 41 insertions, 8 deletions
diff --git a/tiny_skia/src/backend.rs b/tiny_skia/src/backend.rs index 3ef7e717..99230a2f 100644 --- a/tiny_skia/src/backend.rs +++ b/tiny_skia/src/backend.rs @@ -216,6 +216,7 @@ impl Backend { bounds, color, size, + line_height, font, horizontal_alignment, vertical_alignment, @@ -233,13 +234,15 @@ impl Backend { self.text_pipeline.draw( content, - (*bounds + translation) * scale_factor, + *bounds + translation, *color, - *size * scale_factor, + *size, + *line_height, *font, *horizontal_alignment, *vertical_alignment, *shaping, + scale_factor, pixels, clip_mask, ); @@ -626,18 +629,26 @@ impl backend::Text for Backend { &self, contents: &str, size: f32, + line_height: text::LineHeight, font: Font, bounds: Size, shaping: text::Shaping, ) -> (f32, f32) { - self.text_pipeline - .measure(contents, size, font, bounds, shaping) + self.text_pipeline.measure( + contents, + size, + line_height, + font, + bounds, + shaping, + ) } fn hit_test( &self, contents: &str, size: f32, + line_height: text::LineHeight, font: Font, bounds: Size, shaping: text::Shaping, @@ -647,6 +658,7 @@ impl backend::Text for Backend { self.text_pipeline.hit_test( contents, size, + line_height, font, bounds, shaping, diff --git a/tiny_skia/src/geometry.rs b/tiny_skia/src/geometry.rs index 7963fd89..a445b561 100644 --- a/tiny_skia/src/geometry.rs +++ b/tiny_skia/src/geometry.rs @@ -111,6 +111,7 @@ impl Frame { }, color: text.color, size: text.size, + line_height: text.line_height, font: text.font, horizontal_alignment: text.horizontal_alignment, vertical_alignment: text.vertical_alignment, diff --git a/tiny_skia/src/text.rs b/tiny_skia/src/text.rs index 58079cc0..ba8a4e4b 100644 --- a/tiny_skia/src/text.rs +++ b/tiny_skia/src/text.rs @@ -1,7 +1,7 @@ use crate::core::alignment; use crate::core::font::{self, Font}; -use crate::core::text::{Hit, Shaping}; -use crate::core::{Color, Point, Rectangle, Size}; +use crate::core::text::{Hit, LineHeight, Shaping}; +use crate::core::{Color, Pixels, Point, Rectangle, Size}; use rustc_hash::{FxHashMap, FxHashSet}; use std::borrow::Cow; @@ -46,13 +46,21 @@ impl Pipeline { bounds: Rectangle, color: Color, size: f32, + line_height: LineHeight, font: Font, horizontal_alignment: alignment::Horizontal, vertical_alignment: alignment::Vertical, shaping: Shaping, + scale_factor: f32, pixels: &mut tiny_skia::PixmapMut<'_>, clip_mask: Option<&tiny_skia::Mask>, ) { + let line_height = + f32::from(line_height.to_absolute(Pixels(size))) * scale_factor; + + let bounds = bounds * scale_factor; + let size = size * scale_factor; + let font_system = self.font_system.get_mut(); let key = Key { bounds: { @@ -64,6 +72,7 @@ impl Pipeline { content, font, size, + line_height, shaping, }; @@ -76,7 +85,7 @@ impl Pipeline { (i + 1, buffer.line_w.max(max)) }); - let total_height = total_lines as f32 * size * 1.2; + let total_height = total_lines as f32 * line_height; let x = match horizontal_alignment { alignment::Horizontal::Left => bounds.x, @@ -130,17 +139,21 @@ impl Pipeline { &self, content: &str, size: f32, + line_height: LineHeight, font: Font, bounds: Size, shaping: Shaping, ) -> (f32, f32) { let mut measurement_cache = self.measurement_cache.borrow_mut(); + let line_height = f32::from(line_height.to_absolute(Pixels(size))); + let (_, paragraph) = measurement_cache.allocate( &mut self.font_system.borrow_mut(), Key { content, size, + line_height, font, bounds, shaping, @@ -154,13 +167,14 @@ impl Pipeline { (i + 1, buffer.line_w.max(max)) }); - (max_width, size * 1.2 * total_lines as f32) + (max_width, line_height * total_lines as f32) } pub fn hit_test( &self, content: &str, size: f32, + line_height: LineHeight, font: Font, bounds: Size, shaping: Shaping, @@ -169,11 +183,14 @@ impl Pipeline { ) -> Option<Hit> { let mut measurement_cache = self.measurement_cache.borrow_mut(); + let line_height = f32::from(line_height.to_absolute(Pixels(size))); + let (_, paragraph) = measurement_cache.allocate( &mut self.font_system.borrow_mut(), Key { content, size, + line_height, font, bounds, shaping, @@ -380,9 +397,11 @@ impl Cache { key.content.hash(&mut hasher); key.size.to_bits().hash(&mut hasher); + key.line_height.to_bits().hash(&mut hasher); key.font.hash(&mut hasher); key.bounds.width.to_bits().hash(&mut hasher); key.bounds.height.to_bits().hash(&mut hasher); + key.shaping.hash(&mut hasher); hasher.finish() }; @@ -432,6 +451,7 @@ impl Cache { struct Key<'a> { content: &'a str, size: f32, + line_height: f32, font: Font, bounds: Size, shaping: Shaping, |