Skip to content

Commit 45976bc

Browse files
authored
Small polish of the appearence of small glyph with sdf
The trick is that we should have a constant delta, regardless of the size of the individual glyphs.
1 parent 17207b0 commit 45976bc

File tree

4 files changed

+22
-20
lines changed

4 files changed

+22
-20
lines changed

internal/core/software_renderer.rs

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1496,6 +1496,7 @@ impl<'a, T: ProcessScene> SceneBuilder<'a, T> {
14961496
.process_rectangle(geometry, selection.selection_background.into());
14971497
}
14981498
}
1499+
let scale_delta = paragraph.layout.font.scale_delta();
14991500
for positioned_glyph in glyphs {
15001501
let glyph = paragraph.layout.font.render_glyph(positioned_glyph.glyph_id);
15011502

@@ -1537,6 +1538,9 @@ impl<'a, T: ProcessScene> SceneBuilder<'a, T> {
15371538

15381539
match &glyph.alpha_map {
15391540
fonts::GlyphAlphaMap::Static(data) => {
1541+
if data.is_empty() {
1542+
continue;
1543+
}
15401544
let texture = if !glyph.sdf {
15411545
SceneTexture {
15421546
data,
@@ -1554,21 +1558,10 @@ impl<'a, T: ProcessScene> SceneBuilder<'a, T> {
15541558
},
15551559
}
15561560
} else {
1557-
let dx = if glyph.width.get() <= 1 {
1558-
Fixed::from_integer(0)
1559-
} else {
1560-
Fixed::from_integer(pixel_stride - 1)
1561-
/ (glyph.width.get() as u16 - 1)
1562-
};
1563-
let dy = if glyph.height.get() <= 1 {
1564-
Fixed::from_integer(0)
1565-
} else {
1566-
Fixed::from_integer(
1567-
(data.len() as u16 - 1) / pixel_stride - 1,
1568-
) / (glyph.height.get() as u16 - 1)
1569-
};
1570-
let off_x = Fixed::<i32, 8>::from_fixed(dx) * off_x as i32;
1571-
let off_y = Fixed::<i32, 8>::from_fixed(dy) * off_y as i32;
1561+
let off_x =
1562+
Fixed::<i32, 8>::from_fixed(scale_delta) * off_x as i32;
1563+
let off_y =
1564+
Fixed::<i32, 8>::from_fixed(scale_delta) * off_y as i32;
15721565
SceneTexture {
15731566
data,
15741567
pixel_stride,
@@ -1578,8 +1571,8 @@ impl<'a, T: ProcessScene> SceneBuilder<'a, T> {
15781571
// color already is mixed with global alpha
15791572
alpha: color.alpha(),
15801573
rotation: self.rotation.orientation,
1581-
dx,
1582-
dy,
1574+
dx: scale_delta,
1575+
dy: scale_delta,
15831576
off_x: Fixed::try_from_fixed(off_x).unwrap(),
15841577
off_y: Fixed::try_from_fixed(off_y).unwrap(),
15851578
},

internal/core/software_renderer/fonts.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use core::cell::RefCell;
99
#[cfg(all(not(feature = "std"), feature = "unsafe-single-threaded"))]
1010
use crate::thread_local_ as thread_local;
1111

12-
use super::{PhysicalLength, PhysicalSize};
12+
use super::{Fixed, PhysicalLength, PhysicalSize};
1313
use crate::graphics::{BitmapFont, FontRequest};
1414
use crate::items::TextWrap;
1515
use crate::lengths::{LogicalLength, LogicalSize, ScaleFactor};
@@ -45,6 +45,8 @@ impl RenderableGlyph {
4545

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

5052
pub(super) const DEFAULT_FONT_SIZE: LogicalLength = LogicalLength::new(12 as Coord);

internal/core/software_renderer/fonts/pixelfont.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,16 @@ impl GlyphRenderer for PixelFont {
4444
RenderableGlyph {
4545
x: self.scale_glyph_length(bitmap_glyph.x),
4646
y: self.scale_glyph_length(bitmap_glyph.y),
47-
width: self.scale_glyph_length(bitmap_glyph.width),
48-
height: self.scale_glyph_length(bitmap_glyph.height),
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),
4949
alpha_map: bitmap_glyph.data.as_slice().into(),
5050
pixel_stride: bitmap_glyph.width as u16,
5151
sdf: self.bitmap_font.sdf,
5252
}
5353
}
54+
fn scale_delta(&self) -> super::Fixed<u16, 8> {
55+
super::Fixed::from_integer(self.glyphs.pixel_size as u16) / self.pixel_size.get() as u16
56+
}
5457
}
5558

5659
impl TextShaper for PixelFont {

internal/core/software_renderer/fonts/vectorfont.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,4 +220,8 @@ impl super::GlyphRenderer for VectorFont {
220220
}
221221
})
222222
}
223+
224+
fn scale_delta(&self) -> super::Fixed<u16, 8> {
225+
super::Fixed::from_integer(1)
226+
}
223227
}

0 commit comments

Comments
 (0)