Skip to content

Commit 446bb4a

Browse files
authored
swrenderer: signed distance field: properly round the x and y glyph coordinate (#6687)
1 parent 0f8e3b8 commit 446bb4a

File tree

5 files changed

+23
-16
lines changed

5 files changed

+23
-16
lines changed

internal/compiler/passes/embed_glyphs.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -557,8 +557,8 @@ fn generate_sdf_for_glyph(
557557
glyph_data.push(0);
558558

559559
let metrics = fontdue::Metrics {
560-
xmin: -(1. - bbox.x_min as f64 * scale).floor() as i32,
561-
ymin: -(1. - bbox.y_min as f64 * scale).floor() as i32,
560+
xmin: -(1. - bbox.x_min as f64 * scale).round() as i32,
561+
ymin: -(1. - bbox.y_min as f64 * scale).round() as i32,
562562
width: width as usize,
563563
height: height as usize,
564564
advance_width: (face.glyph_hor_advance(glyph_id).unwrap() as f64 * scale) as f32,

internal/core/software_renderer.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1498,7 +1498,11 @@ impl<'a, T: ProcessScene> SceneBuilder<'a, T> {
14981498
}
14991499
let scale_delta = paragraph.layout.font.scale_delta();
15001500
for positioned_glyph in glyphs {
1501-
let glyph = paragraph.layout.font.render_glyph(positioned_glyph.glyph_id);
1501+
let Some(glyph) =
1502+
paragraph.layout.font.render_glyph(positioned_glyph.glyph_id)
1503+
else {
1504+
continue;
1505+
};
15021506

15031507
let target_rect = PhysicalRect::new(
15041508
PhysicalPoint::from_lengths(
@@ -1538,9 +1542,6 @@ impl<'a, T: ProcessScene> SceneBuilder<'a, T> {
15381542

15391543
match &glyph.alpha_map {
15401544
fonts::GlyphAlphaMap::Static(data) => {
1541-
if data.is_empty() {
1542-
continue;
1543-
}
15441545
let texture = if !glyph.sdf {
15451546
SceneTexture {
15461547
data,

internal/core/software_renderer/fonts.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ impl RenderableGlyph {
4444
}
4545

4646
pub trait GlyphRenderer {
47-
fn render_glyph(&self, glyph_id: core::num::NonZeroU16) -> RenderableGlyph;
47+
fn render_glyph(&self, glyph_id: core::num::NonZeroU16) -> Option<RenderableGlyph>;
4848
/// The amount of pixel in the original image that correspond to one pixel in the rendered image
4949
fn scale_delta(&self) -> Fixed<u16, 8>;
5050
}

internal/core/software_renderer/fonts/pixelfont.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,24 @@ impl PixelFont {
3838
}
3939

4040
impl GlyphRenderer for PixelFont {
41-
fn render_glyph(&self, glyph_id: core::num::NonZeroU16) -> RenderableGlyph {
41+
fn render_glyph(&self, glyph_id: core::num::NonZeroU16) -> Option<RenderableGlyph> {
4242
let glyph_index = Self::glyph_id_to_glyph_index(glyph_id);
4343
let bitmap_glyph = &self.glyphs.glyph_data[glyph_index];
44-
RenderableGlyph {
44+
if bitmap_glyph.data.len() == 0 {
45+
// For example, ' ' has no glyph data
46+
return None;
47+
}
48+
let width = self.scale_glyph_length(bitmap_glyph.width - 1) + PhysicalLength::new(1);
49+
let height = self.scale_glyph_length(bitmap_glyph.height - 1) + PhysicalLength::new(1);
50+
Some(RenderableGlyph {
4551
x: self.scale_glyph_length(bitmap_glyph.x),
46-
y: self.scale_glyph_length(bitmap_glyph.y),
47-
width: self.scale_glyph_length(bitmap_glyph.width - 1) + PhysicalLength::new(1),
48-
height: self.scale_glyph_length(bitmap_glyph.height - 1) + PhysicalLength::new(1),
52+
y: self.scale_glyph_length(bitmap_glyph.y + bitmap_glyph.height) - height,
53+
width,
54+
height,
4955
alpha_map: bitmap_glyph.data.as_slice().into(),
5056
pixel_stride: bitmap_glyph.width as u16,
5157
sdf: self.bitmap_font.sdf,
52-
}
58+
})
5359
}
5460
fn scale_delta(&self) -> super::Fixed<u16, 8> {
5561
super::Fixed::from_integer(self.glyphs.pixel_size as u16) / self.pixel_size.get() as u16

internal/core/software_renderer/fonts/vectorfont.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,14 +191,14 @@ impl crate::textlayout::FontMetrics<PhysicalLength> for VectorFont {
191191
}
192192

193193
impl super::GlyphRenderer for VectorFont {
194-
fn render_glyph(&self, glyph_id: core::num::NonZeroU16) -> super::RenderableGlyph {
194+
fn render_glyph(&self, glyph_id: core::num::NonZeroU16) -> Option<super::RenderableGlyph> {
195195
GLYPH_CACHE.with(|cache| {
196196
let mut cache = cache.borrow_mut();
197197

198198
let cache_key = (self.id, self.pixel_size, glyph_id);
199199

200200
if let Some(entry) = cache.get(&cache_key) {
201-
entry.clone()
201+
Some(entry.clone())
202202
} else {
203203
let (metrics, alpha_map) =
204204
self.fontdue_font.rasterize_indexed(glyph_id.get(), self.pixel_size.get() as _);
@@ -216,7 +216,7 @@ impl super::GlyphRenderer for VectorFont {
216216
};
217217

218218
cache.put_with_weight(cache_key, glyph.clone()).ok();
219-
glyph
219+
Some(glyph)
220220
}
221221
})
222222
}

0 commit comments

Comments
 (0)