Merge pull request #2247 from iced-rs/fix/tiny-skia-text-clipping
Fix clipping of text in `iced_tiny_skia`
This commit is contained in:
commit
891f29eea0
2 changed files with 49 additions and 42 deletions
|
|
@ -98,12 +98,7 @@ pub fn measure(buffer: &cosmic_text::Buffer) -> Size {
|
||||||
(run.line_w.max(width), total_lines + 1)
|
(run.line_w.max(width), total_lines + 1)
|
||||||
});
|
});
|
||||||
|
|
||||||
let (max_width, max_height) = buffer.size();
|
Size::new(width, total_lines as f32 * buffer.metrics().line_height)
|
||||||
|
|
||||||
Size::new(
|
|
||||||
width.min(max_width),
|
|
||||||
(total_lines as f32 * buffer.metrics().line_height).min(max_height),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the attributes of the given [`Font`].
|
/// Returns the attributes of the given [`Font`].
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,9 @@
|
||||||
use tiny_skia::Size;
|
|
||||||
|
|
||||||
use crate::core::{
|
use crate::core::{
|
||||||
Background, Color, Gradient, Rectangle, Transformation, Vector,
|
Background, Color, Gradient, Rectangle, Size, Transformation, Vector,
|
||||||
};
|
};
|
||||||
use crate::graphics::backend;
|
use crate::graphics::backend;
|
||||||
use crate::graphics::text;
|
use crate::graphics::text;
|
||||||
use crate::graphics::Viewport;
|
use crate::graphics::{Damage, Viewport};
|
||||||
use crate::primitive::{self, Primitive};
|
use crate::primitive::{self, Primitive};
|
||||||
|
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
|
|
@ -219,31 +217,34 @@ impl Backend {
|
||||||
(x..x + width).map(move |x| (x as f32, y as f32))
|
(x..x + width).map(move |x| (x as f32, y as f32))
|
||||||
})
|
})
|
||||||
.filter_map(|(x, y)| {
|
.filter_map(|(x, y)| {
|
||||||
Size::from_wh(half_width, half_height).map(|size| {
|
tiny_skia::Size::from_wh(half_width, half_height)
|
||||||
let shadow_distance = rounded_box_sdf(
|
.map(|size| {
|
||||||
Vector::new(
|
let shadow_distance = rounded_box_sdf(
|
||||||
x - physical_bounds.position().x
|
Vector::new(
|
||||||
- (shadow.offset.x * scale_factor)
|
x - physical_bounds.position().x
|
||||||
- half_width,
|
- (shadow.offset.x
|
||||||
y - physical_bounds.position().y
|
* scale_factor)
|
||||||
- (shadow.offset.y * scale_factor)
|
- half_width,
|
||||||
- half_height,
|
y - physical_bounds.position().y
|
||||||
),
|
- (shadow.offset.y
|
||||||
size,
|
* scale_factor)
|
||||||
&radii,
|
- half_height,
|
||||||
);
|
),
|
||||||
let shadow_alpha = 1.0
|
size,
|
||||||
- smoothstep(
|
&radii,
|
||||||
-shadow.blur_radius * scale_factor,
|
|
||||||
shadow.blur_radius * scale_factor,
|
|
||||||
shadow_distance,
|
|
||||||
);
|
);
|
||||||
|
let shadow_alpha = 1.0
|
||||||
|
- smoothstep(
|
||||||
|
-shadow.blur_radius * scale_factor,
|
||||||
|
shadow.blur_radius * scale_factor,
|
||||||
|
shadow_distance,
|
||||||
|
);
|
||||||
|
|
||||||
let mut color = into_color(shadow.color);
|
let mut color = into_color(shadow.color);
|
||||||
color.apply_opacity(shadow_alpha);
|
color.apply_opacity(shadow_alpha);
|
||||||
|
|
||||||
color.to_color_u8().premultiply()
|
color.to_color_u8().premultiply()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
|
|
@ -447,10 +448,12 @@ impl Backend {
|
||||||
paragraph,
|
paragraph,
|
||||||
position,
|
position,
|
||||||
color,
|
color,
|
||||||
clip_bounds: text_clip_bounds,
|
clip_bounds: _, // TODO: Support text clip bounds
|
||||||
} => {
|
} => {
|
||||||
let physical_bounds =
|
let physical_bounds =
|
||||||
*text_clip_bounds * transformation * scale_factor;
|
Rectangle::new(*position, paragraph.min_bounds)
|
||||||
|
* transformation
|
||||||
|
* scale_factor;
|
||||||
|
|
||||||
if !clip_bounds.intersects(&physical_bounds) {
|
if !clip_bounds.intersects(&physical_bounds) {
|
||||||
return;
|
return;
|
||||||
|
|
@ -473,10 +476,11 @@ impl Backend {
|
||||||
editor,
|
editor,
|
||||||
position,
|
position,
|
||||||
color,
|
color,
|
||||||
clip_bounds: text_clip_bounds,
|
clip_bounds: _, // TODO: Support text clip bounds
|
||||||
} => {
|
} => {
|
||||||
let physical_bounds =
|
let physical_bounds = Rectangle::new(*position, editor.bounds)
|
||||||
(*text_clip_bounds * transformation) * scale_factor;
|
* transformation
|
||||||
|
* scale_factor;
|
||||||
|
|
||||||
if !clip_bounds.intersects(&physical_bounds) {
|
if !clip_bounds.intersects(&physical_bounds) {
|
||||||
return;
|
return;
|
||||||
|
|
@ -505,10 +509,10 @@ impl Backend {
|
||||||
horizontal_alignment,
|
horizontal_alignment,
|
||||||
vertical_alignment,
|
vertical_alignment,
|
||||||
shaping,
|
shaping,
|
||||||
clip_bounds: text_clip_bounds,
|
clip_bounds: _, // TODO: Support text clip bounds
|
||||||
} => {
|
} => {
|
||||||
let physical_bounds =
|
let physical_bounds =
|
||||||
*text_clip_bounds * transformation * scale_factor;
|
primitive.bounds() * transformation * scale_factor;
|
||||||
|
|
||||||
if !clip_bounds.intersects(&physical_bounds) {
|
if !clip_bounds.intersects(&physical_bounds) {
|
||||||
return;
|
return;
|
||||||
|
|
@ -537,14 +541,18 @@ impl Backend {
|
||||||
buffer,
|
buffer,
|
||||||
position,
|
position,
|
||||||
color,
|
color,
|
||||||
clip_bounds: text_clip_bounds,
|
clip_bounds: _, // TODO: Support text clip bounds
|
||||||
}) => {
|
}) => {
|
||||||
let Some(buffer) = buffer.upgrade() else {
|
let Some(buffer) = buffer.upgrade() else {
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let (width, height) = buffer.size();
|
||||||
|
|
||||||
let physical_bounds =
|
let physical_bounds =
|
||||||
*text_clip_bounds * transformation * scale_factor;
|
Rectangle::new(*position, Size::new(width, height))
|
||||||
|
* transformation
|
||||||
|
* scale_factor;
|
||||||
|
|
||||||
if !clip_bounds.intersects(&physical_bounds) {
|
if !clip_bounds.intersects(&physical_bounds) {
|
||||||
return;
|
return;
|
||||||
|
|
@ -963,7 +971,11 @@ fn smoothstep(a: f32, b: f32, x: f32) -> f32 {
|
||||||
x * x * (3.0 - 2.0 * x)
|
x * x * (3.0 - 2.0 * x)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn rounded_box_sdf(to_center: Vector, size: Size, radii: &[f32]) -> f32 {
|
fn rounded_box_sdf(
|
||||||
|
to_center: Vector,
|
||||||
|
size: tiny_skia::Size,
|
||||||
|
radii: &[f32],
|
||||||
|
) -> f32 {
|
||||||
let radius = match (to_center.x > 0.0, to_center.y > 0.0) {
|
let radius = match (to_center.x > 0.0, to_center.y > 0.0) {
|
||||||
(true, true) => radii[2],
|
(true, true) => radii[2],
|
||||||
(true, false) => radii[1],
|
(true, false) => radii[1],
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue