mirror of
https://github.com/emilk/egui.git
synced 2026-09-03 07:10:04 -04:00
Simplify kerning code
This commit is contained in:
@@ -409,7 +409,7 @@ impl FontDefinitions {
|
|||||||
pub(crate) struct FontFaceKey(u64);
|
pub(crate) struct FontFaceKey(u64);
|
||||||
|
|
||||||
impl FontFaceKey {
|
impl FontFaceKey {
|
||||||
const INVALID: Self = Self(0);
|
pub const INVALID: Self = Self(0);
|
||||||
|
|
||||||
fn new() -> Self {
|
fn new() -> Self {
|
||||||
static KEY_COUNTER: AtomicU64 = AtomicU64::new(1);
|
static KEY_COUNTER: AtomicU64 = AtomicU64::new(1);
|
||||||
|
|||||||
@@ -176,8 +176,9 @@ fn layout_section(
|
|||||||
|
|
||||||
let mut last_glyph_id = None;
|
let mut last_glyph_id = None;
|
||||||
|
|
||||||
// Optimization: only recompute `ScaledMetrics` when `FontFaceKey` changes.
|
// Optimization: only recompute `ScaledMetrics` when the concrete `FontImpl` changes.
|
||||||
let mut last_font: Option<(FontFaceKey, Option<ScaledMetrics>)> = None;
|
let mut current_font = FontFaceKey::INVALID;
|
||||||
|
let mut current_font_impl_metrics = ScaledMetrics::default();
|
||||||
|
|
||||||
for chr in job.text[byte_range.clone()].chars() {
|
for chr in job.text[byte_range.clone()].chars() {
|
||||||
if job.break_on_newline && chr == '\n' {
|
if job.break_on_newline && chr == '\n' {
|
||||||
@@ -186,26 +187,23 @@ fn layout_section(
|
|||||||
paragraph.empty_paragraph_height = line_height; // TODO(emilk): replace this hack with actually including `\n` in the glyphs?
|
paragraph.empty_paragraph_height = line_height; // TODO(emilk): replace this hack with actually including `\n` in the glyphs?
|
||||||
} else {
|
} else {
|
||||||
let (font_id, glyph_info) = font.glyph_info(chr);
|
let (font_id, glyph_info) = font.glyph_info(chr);
|
||||||
let (mut font_impl, font_impl_metrics) = match last_font {
|
let mut font_impl = font.fonts_by_id.get_mut(&font_id);
|
||||||
Some((last_font_id, last_font_metrics)) if last_font_id == font_id => (
|
if current_font != font_id {
|
||||||
font.fonts_by_id.get_mut(&font_id),
|
current_font = font_id;
|
||||||
last_font_metrics.unwrap_or_default(),
|
current_font_impl_metrics = font_impl
|
||||||
),
|
.as_ref()
|
||||||
_ => {
|
.map(|font_impl| font_impl.scaled_metrics(pixels_per_point, font_size))
|
||||||
let font_impl = font.fonts_by_id.get_mut(&font_id);
|
.unwrap_or_default();
|
||||||
let scaled_metrics = font_impl
|
}
|
||||||
.as_ref()
|
|
||||||
.map(|font_impl| font_impl.scaled_metrics(pixels_per_point, font_size));
|
|
||||||
last_font = Some((font_id, scaled_metrics));
|
|
||||||
(font_impl, scaled_metrics.unwrap_or_default())
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
if let (Some(font_impl), Some(last_glyph_id), Some(glyph_id)) =
|
if let (Some(font_impl), Some(last_glyph_id), Some(glyph_id)) =
|
||||||
(&font_impl, last_glyph_id, glyph_info.id)
|
(&font_impl, last_glyph_id, glyph_info.id)
|
||||||
{
|
{
|
||||||
paragraph.cursor_x_px +=
|
paragraph.cursor_x_px += font_impl.pair_kerning_pixels(
|
||||||
font_impl.pair_kerning_pixels(&font_impl_metrics, last_glyph_id, glyph_id);
|
¤t_font_impl_metrics,
|
||||||
|
last_glyph_id,
|
||||||
|
glyph_id,
|
||||||
|
);
|
||||||
|
|
||||||
// Only apply extra_letter_spacing to glyphs after the first one:
|
// Only apply extra_letter_spacing to glyphs after the first one:
|
||||||
paragraph.cursor_x_px += extra_letter_spacing * pixels_per_point;
|
paragraph.cursor_x_px += extra_letter_spacing * pixels_per_point;
|
||||||
@@ -214,7 +212,7 @@ fn layout_section(
|
|||||||
let (glyph_alloc, physical_x) = match font_impl.as_mut() {
|
let (glyph_alloc, physical_x) = match font_impl.as_mut() {
|
||||||
Some(font_impl) => font_impl.allocate_glyph(
|
Some(font_impl) => font_impl.allocate_glyph(
|
||||||
font.atlas,
|
font.atlas,
|
||||||
&font_impl_metrics,
|
¤t_font_impl_metrics,
|
||||||
glyph_info,
|
glyph_info,
|
||||||
chr,
|
chr,
|
||||||
paragraph.cursor_x_px,
|
paragraph.cursor_x_px,
|
||||||
@@ -227,8 +225,8 @@ fn layout_section(
|
|||||||
pos: pos2(physical_x as f32 / pixels_per_point, f32::NAN),
|
pos: pos2(physical_x as f32 / pixels_per_point, f32::NAN),
|
||||||
advance_width: glyph_alloc.advance_width_px / pixels_per_point,
|
advance_width: glyph_alloc.advance_width_px / pixels_per_point,
|
||||||
line_height,
|
line_height,
|
||||||
font_impl_height: font_impl_metrics.row_height,
|
font_impl_height: current_font_impl_metrics.row_height,
|
||||||
font_impl_ascent: font_impl_metrics.ascent,
|
font_impl_ascent: current_font_impl_metrics.ascent,
|
||||||
font_height: font_metrics.row_height,
|
font_height: font_metrics.row_height,
|
||||||
font_ascent: font_metrics.ascent,
|
font_ascent: font_metrics.ascent,
|
||||||
uv_rect: glyph_alloc.uv_rect,
|
uv_rect: glyph_alloc.uv_rect,
|
||||||
|
|||||||
Reference in New Issue
Block a user