Skip to content

Commit d4e9857

Browse files
committed
Fix bounds calculation
1 parent 5618a71 commit d4e9857

1 file changed

Lines changed: 28 additions & 24 deletions

File tree

crates/lucie-text/src/rasterize.rs

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use lucie_common::geometry::{DevicePixels, Point, Size, point, size};
1+
use lucie_common::geometry::{DevicePixels, Point, ScaledPixels, Size, point, size};
22

33
use crate::{
44
SubpixelVariant,
@@ -13,7 +13,7 @@ pub struct RasterizedGlyph {
1313
/// If `true`, `data` has only alpha channel (1 byte/pixel); otherwise RGBA (4 bytes/pixel).
1414
pub is_monochromatic: bool,
1515
/// Offset to draw glyph at
16-
pub offset: Point<DevicePixels>
16+
pub offset: Point<ScaledPixels>
1717
}
1818

1919
pub(crate) fn rasterize_outline_glyph(
@@ -24,7 +24,7 @@ pub(crate) fn rasterize_outline_glyph(
2424
outlines: &skrifa::OutlineGlyphCollection<'_>,
2525
hint_cache: Option<&mut HintCache>
2626
) -> Option<RasterizedGlyph> {
27-
let mut pen = TinySkiaPen::default();
27+
let mut pen = TinySkiaPen::new(subpixel_variant.offset(), 0.0);
2828
outline
2929
.draw(
3030
if let Some(hint_cache) = hint_cache
@@ -38,52 +38,56 @@ pub(crate) fn rasterize_outline_glyph(
3838
)
3939
.unwrap();
4040

41-
let subpixel_offset = subpixel_variant.offset();
42-
4341
let path = pen.path.finish()?;
44-
let bounds = path.bounds().round_out().unwrap();
45-
let (mut w, h) = (bounds.width() as u32, bounds.height() as u32);
46-
if subpixel_offset > 0. {
47-
w += 1;
48-
}
42+
let bounds = path.bounds();
43+
44+
let (x, y) = (bounds.x(), bounds.y());
45+
let (w, h) = (bounds.width().ceil() as u32, bounds.height().ceil() as u32);
4946

5047
let mut pixmap = tiny_skia::Pixmap::new(w, h)?;
51-
pixmap.fill_path(
52-
&path,
53-
&tiny_skia::Paint::default(),
54-
tiny_skia::FillRule::Winding,
55-
tiny_skia::Transform::from_translate(-(bounds.left() as f32 - subpixel_offset), -bounds.top() as f32 - bounds.height() as f32).post_scale(1.0, -1.0),
56-
None
57-
);
48+
pixmap.fill_path(&path, &tiny_skia::Paint::default(), tiny_skia::FillRule::Winding, tiny_skia::Transform::from_translate(-x, -y), None);
5849

5950
Some(RasterizedGlyph {
6051
size: size(DevicePixels(pixmap.width() as _), DevicePixels(pixmap.height() as _)),
6152
data: pixmap.data().to_vec(),
6253
is_monochromatic: false,
63-
offset: point(DevicePixels(bounds.left()), DevicePixels(-(pixmap.height() as i32) - bounds.top()))
54+
offset: point(ScaledPixels(x.round()), ScaledPixels(y.round()))
6455
})
6556
}
6657

67-
#[derive(Default)]
6858
struct TinySkiaPen {
69-
pub(crate) path: tiny_skia::PathBuilder
59+
pub(crate) path: tiny_skia::PathBuilder,
60+
x_offset: f32,
61+
y_offset: f32
62+
}
63+
64+
impl TinySkiaPen {
65+
pub(crate) fn new(x_offset: f32, y_offset: f32) -> Self {
66+
Self {
67+
path: tiny_skia::PathBuilder::new(),
68+
x_offset,
69+
y_offset
70+
}
71+
}
7072
}
7173

7274
impl skrifa::outline::OutlinePen for TinySkiaPen {
7375
fn move_to(&mut self, x: f32, y: f32) {
74-
self.path.move_to(x, y);
76+
self.path.move_to(self.x_offset + x, self.y_offset - y);
7577
}
7678

7779
fn curve_to(&mut self, cx0: f32, cy0: f32, cx1: f32, cy1: f32, x: f32, y: f32) {
78-
self.path.cubic_to(cx0, cy0, cx1, cy1, x, y);
80+
self.path
81+
.cubic_to(self.x_offset + cx0, self.y_offset - cy0, self.x_offset + cx1, self.y_offset - cy1, self.x_offset + x, self.y_offset - y);
7982
}
8083

8184
fn line_to(&mut self, x: f32, y: f32) {
82-
self.path.line_to(x, y);
85+
self.path.line_to(self.x_offset + x, self.y_offset - y);
8386
}
8487

8588
fn quad_to(&mut self, cx0: f32, cy0: f32, x: f32, y: f32) {
86-
self.path.quad_to(cx0, cy0, x, y);
89+
self.path
90+
.quad_to(self.x_offset + cx0, self.y_offset - cy0, self.x_offset + x, self.y_offset - y);
8791
}
8892

8993
fn close(&mut self) {

0 commit comments

Comments
 (0)