Skip to content

Commit

Permalink
fixed "jumping" glyphs in font rendering
Browse files Browse the repository at this point in the history
- this fixes 95% of the cases, while some cases of large distance (1px) between small glyphs may still remain
- in general this makes font rendering much better looking
  • Loading branch information
mrDIMAS committed Dec 24, 2024
1 parent f39dded commit 8607b73
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 19 deletions.
34 changes: 21 additions & 13 deletions fyrox-ui/src/font/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,16 @@ use crate::core::{
visitor::prelude::*, TypeUuidProvider,
};
use fxhash::FxHashMap;
use fyrox_resource::manager::BuiltInResource;
use fyrox_resource::untyped::UntypedResource;
use fyrox_resource::{embedded_data_source, io::ResourceIo, Resource, ResourceData};
use fyrox_core::math::Rect;
use fyrox_resource::{
embedded_data_source, io::ResourceIo, manager::BuiltInResource, untyped::UntypedResource,
Resource, ResourceData,
};
use lazy_static::lazy_static;
use std::fmt::Formatter;
use std::{
any::Any,
error::Error,
fmt::Debug,
fmt::{Debug, Formatter},
hash::{Hash, Hasher},
ops::Deref,
path::Path,
Expand All @@ -43,13 +44,14 @@ pub mod loader;

#[derive(Debug)]
pub struct FontGlyph {
pub top: f32,
pub left: f32,
pub bitmap_top: f32,
pub bitmap_left: f32,
pub bitmap_width: f32,
pub bitmap_height: f32,
pub advance: f32,
pub tex_coords: [Vector2<f32>; 4],
pub bitmap_width: usize,
pub bitmap_height: usize,
pub page_index: usize,
pub bounds: Rect<f32>,
}

/// Page is a storage for rasterized glyphs.
Expand Down Expand Up @@ -147,12 +149,18 @@ impl Atlas {
page.modified = true;

let mut glyph = FontGlyph {
left: metrics.xmin as f32,
top: metrics.ymin as f32,
bitmap_left: metrics.xmin as f32,
bitmap_top: metrics.ymin as f32,
advance: metrics.advance_width,
tex_coords: Default::default(),
bitmap_width: metrics.width,
bitmap_height: metrics.height,
bitmap_width: metrics.width as f32,
bitmap_height: metrics.height as f32,
bounds: Rect::new(
metrics.bounds.xmin,
metrics.bounds.ymin,
metrics.bounds.width,
metrics.bounds.height,
),
page_index,
};

Expand Down
12 changes: 6 additions & 6 deletions fyrox-ui/src/formatted_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ fn build_glyph(
let ascender = metrics.ascender();
let font_size = metrics.size;

x = x.round();
x = x.floor();
y = y.floor();

// Request larger glyph with super sampling scaling.
Expand All @@ -178,10 +178,10 @@ fn build_glyph(
let k = 1.0 / super_sampling_scale;
// Insert glyph
let rect = Rect::new(
x + glyph.left.round() * k,
y + ascender.floor() - glyph.top.floor() * k - (glyph.bitmap_height as f32 * k),
glyph.bitmap_width as f32 * k,
glyph.bitmap_height as f32 * k,
x + glyph.bitmap_left * k,
y + ascender.floor() - glyph.bitmap_top * k - (glyph.bitmap_height * k),
glyph.bitmap_width * k,
glyph.bitmap_height * k,
);
let text_glyph = TextGlyph {
bounds: rect,
Expand Down Expand Up @@ -719,7 +719,7 @@ impl FormattedText {

let mut y: f32 = cursor_y_start.floor();
for line in self.lines.iter_mut() {
let mut x = line.x_offset;
let mut x = line.x_offset.floor();
if let Some(mask) = *self.mask_char {
let mut prev = None;
for c in std::iter::repeat::<char>(mask).take(line.len()) {
Expand Down

0 comments on commit 8607b73

Please sign in to comment.