Support configurable LineHeight in text widgets

This commit is contained in:
Héctor Ramón Jiménez 2023-05-04 13:00:16 +02:00
parent 8e8808f0e1
commit 9499a8f9e6
No known key found for this signature in database
GPG key ID: 140CC052C94F138E
24 changed files with 337 additions and 42 deletions

View file

@ -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,

View file

@ -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,

View file

@ -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,