mirror of
https://github.com/emilk/egui.git
synced 2026-09-01 14:20:04 -04:00
Refactor allocate_glyph_by_id to use ShapedGlyph struct
Replace 5 loose parameters with a ShapedGlyph struct, removing the need for #[expect(clippy::too_many_arguments)].
This commit is contained in:
@@ -623,41 +623,34 @@ impl FontFace {
|
|||||||
shaper.shape(buffer, &[])
|
shaper.shape(buffer, &[])
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Allocate a glyph by its ID directly (from shaping output).
|
/// Allocate a glyph by its ID directly, using metrics from the shaper.
|
||||||
///
|
|
||||||
/// `shaper_y_offset_points` is the vertical offset from the shaper, already in points.
|
|
||||||
#[expect(clippy::too_many_arguments)]
|
|
||||||
pub fn allocate_glyph_by_id(
|
pub fn allocate_glyph_by_id(
|
||||||
&mut self,
|
&mut self,
|
||||||
atlas: &mut TextureAtlas,
|
atlas: &mut TextureAtlas,
|
||||||
metrics: &StyledMetrics,
|
metrics: &StyledMetrics,
|
||||||
glyph_id: skrifa::GlyphId,
|
shaped: &ShapedGlyph,
|
||||||
advance_width_px: f32,
|
|
||||||
h_pos: f32,
|
|
||||||
shaper_y_offset_points: f32,
|
|
||||||
is_cjk_glyph: bool,
|
|
||||||
) -> (GlyphAllocation, i32) {
|
) -> (GlyphAllocation, i32) {
|
||||||
if glyph_id == skrifa::GlyphId::NOTDEF {
|
if shaped.glyph_id == skrifa::GlyphId::NOTDEF {
|
||||||
return (GlyphAllocation::default(), h_pos as i32);
|
return (GlyphAllocation::default(), shaped.h_pos as i32);
|
||||||
}
|
}
|
||||||
|
|
||||||
let (h_pos_round, bin) = if is_cjk_glyph {
|
let (h_pos_round, bin) = if shaped.is_cjk {
|
||||||
(h_pos.round() as i32, SubpixelBin::Zero)
|
(shaped.h_pos.round() as i32, SubpixelBin::Zero)
|
||||||
} else {
|
} else {
|
||||||
SubpixelBin::new(h_pos)
|
SubpixelBin::new(shaped.h_pos)
|
||||||
};
|
};
|
||||||
|
|
||||||
let cache_key = GlyphCacheKey::new(glyph_id, metrics, bin);
|
let cache_key = GlyphCacheKey::new(shaped.glyph_id, metrics, bin);
|
||||||
if let Some(cached) = self.glyph_alloc_cache.get(&cache_key) {
|
if let Some(cached) = self.glyph_alloc_cache.get(&cache_key) {
|
||||||
let mut alloc = *cached;
|
let mut alloc = *cached;
|
||||||
alloc.advance_width_px = advance_width_px;
|
alloc.advance_width_px = shaped.advance_width_px;
|
||||||
alloc.uv_rect.offset.y += shaper_y_offset_points;
|
alloc.uv_rect.offset.y += shaped.y_offset_points;
|
||||||
return (alloc, h_pos_round);
|
return (alloc, h_pos_round);
|
||||||
}
|
}
|
||||||
|
|
||||||
let glyph_info = GlyphInfo {
|
let glyph_info = GlyphInfo {
|
||||||
id: Some(glyph_id),
|
id: Some(shaped.glyph_id),
|
||||||
advance_width_unscaled: OrderedFloat(advance_width_px / metrics.px_scale_factor),
|
advance_width_unscaled: OrderedFloat(shaped.advance_width_px / metrics.px_scale_factor),
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut allocation = self
|
let mut allocation = self
|
||||||
@@ -670,7 +663,7 @@ impl FontFace {
|
|||||||
|
|
||||||
// Apply shaper y_offset after caching — the offset varies per call site
|
// Apply shaper y_offset after caching — the offset varies per call site
|
||||||
// so we cache the base allocation without it.
|
// so we cache the base allocation without it.
|
||||||
allocation.uv_rect.offset.y += shaper_y_offset_points;
|
allocation.uv_rect.offset.y += shaped.y_offset_points;
|
||||||
|
|
||||||
(allocation, h_pos_round)
|
(allocation, h_pos_round)
|
||||||
}
|
}
|
||||||
@@ -722,6 +715,15 @@ impl FontFace {
|
|||||||
|
|
||||||
/// A contiguous run of text that maps to a single font face.
|
/// A contiguous run of text that maps to a single font face.
|
||||||
///
|
///
|
||||||
|
/// Glyph positioning info from the text shaper, ready for allocation.
|
||||||
|
pub(crate) struct ShapedGlyph {
|
||||||
|
pub glyph_id: skrifa::GlyphId,
|
||||||
|
pub advance_width_px: f32,
|
||||||
|
pub h_pos: f32,
|
||||||
|
pub y_offset_points: f32,
|
||||||
|
pub is_cjk: bool,
|
||||||
|
}
|
||||||
|
|
||||||
/// Produced by [`Font::segment_into_runs`] for text shaping.
|
/// Produced by [`Font::segment_into_runs`] for text shaping.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub(crate) struct TextRun {
|
pub(crate) struct TextRun {
|
||||||
|
|||||||
@@ -267,20 +267,17 @@ fn layout_shaped_run(
|
|||||||
});
|
});
|
||||||
paragraph.cursor_x_px += glyph_alloc.advance_width_px;
|
paragraph.cursor_x_px += glyph_alloc.advance_width_px;
|
||||||
} else {
|
} else {
|
||||||
let h_pos = paragraph.cursor_x_px + x_offset_px;
|
let shaped = super::font::ShapedGlyph {
|
||||||
let y_offset_points = y_offset_px / ctx.pixels_per_point;
|
glyph_id,
|
||||||
|
advance_width_px: x_advance_px,
|
||||||
|
h_pos: paragraph.cursor_x_px + x_offset_px,
|
||||||
|
y_offset_points: y_offset_px / ctx.pixels_per_point,
|
||||||
|
is_cjk: is_cjk(chr),
|
||||||
|
};
|
||||||
|
|
||||||
let (glyph_alloc, physical_x) =
|
let (glyph_alloc, physical_x) =
|
||||||
if let Some(ff) = font.fonts_by_id.get_mut(&run.font_key) {
|
if let Some(ff) = font.fonts_by_id.get_mut(&run.font_key) {
|
||||||
ff.allocate_glyph_by_id(
|
ff.allocate_glyph_by_id(font.atlas, face_metrics, &shaped)
|
||||||
font.atlas,
|
|
||||||
face_metrics,
|
|
||||||
glyph_id,
|
|
||||||
x_advance_px,
|
|
||||||
h_pos,
|
|
||||||
y_offset_points,
|
|
||||||
is_cjk(chr),
|
|
||||||
)
|
|
||||||
} else {
|
} else {
|
||||||
Default::default()
|
Default::default()
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user