Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Sep 27, 2023
1 parent 7b96711 commit 3516ad0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
13 changes: 8 additions & 5 deletions crates/egui/src/load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,20 @@
mod bytes_loader;
mod texture_loader;

use crate::Context;
use std::borrow::Cow;
use std::fmt::Debug;
use std::ops::Deref;
use std::{error::Error as StdError, fmt::Display, sync::Arc};

use ahash::HashMap;

use epaint::mutex::Mutex;
use epaint::util::FloatOrd;
use epaint::util::OrderedFloat;
use epaint::TextureHandle;
use epaint::{textures::TextureOptions, ColorImage, TextureId, Vec2};
use std::borrow::Cow;
use std::fmt::Debug;
use std::ops::Deref;
use std::{error::Error as StdError, fmt::Display, sync::Arc};

use crate::Context;

pub use self::bytes_loader::DefaultBytesLoader;
pub use self::texture_loader::DefaultTextureLoader;
Expand Down
14 changes: 8 additions & 6 deletions crates/epaint/src/text/font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ pub struct FontImpl {
height_in_points: f32,

// move each character by this much (hack)
y_offset: f32,
y_offset_in_points: f32,

ascent: f32,
pixels_per_point: f32,
Expand Down Expand Up @@ -111,22 +111,23 @@ impl FontImpl {
scale_in_points * tweak.y_offset_factor
} + tweak.y_offset;

// center scaled glyphs properly
let y_offset_points = y_offset_points + (tweak.scale - 1.0) * 0.5 * (ascent + descent);
// Center scaled glyphs properly:
let height = ascent + descent;
let y_offset_points = y_offset_points - (1.0 - tweak.scale) * 0.5 * height;

// Round to an even number of physical pixels to get even kerning.
// See https://github.com/emilk/egui/issues/382
let scale_in_pixels = scale_in_pixels.round() as u32;

// Round to closest pixel:
let y_offset = (y_offset_points * pixels_per_point).round() / pixels_per_point;
let y_offset_in_points = (y_offset_points * pixels_per_point).round() / pixels_per_point;

Self {
name,
ab_glyph_font,
scale_in_pixels,
height_in_points: ascent - descent + line_gap,
y_offset,
y_offset_in_points,
ascent: ascent + baseline_offset,
pixels_per_point,
glyph_info_cache: Default::default(),
Expand Down Expand Up @@ -283,7 +284,8 @@ impl FontImpl {
});

let offset_in_pixels = vec2(bb.min.x, bb.min.y);
let offset = offset_in_pixels / self.pixels_per_point + self.y_offset * Vec2::Y;
let offset =
offset_in_pixels / self.pixels_per_point + self.y_offset_in_points * Vec2::Y;
UvRect {
offset,
size: vec2(glyph_width as f32, glyph_height as f32) / self.pixels_per_point,
Expand Down

0 comments on commit 3516ad0

Please sign in to comment.