1
0
mirror of https://github.com/emilk/egui.git synced 2026-06-28 07:23:13 -04:00

refactor fonts: put TextStyle in Galley instead of in Shape::Text

This commit is contained in:
Emil Ernerfeldt
2021-03-29 21:24:09 +02:00
parent ade41403b5
commit d4e5133da2
14 changed files with 61 additions and 50 deletions

View File

@@ -40,13 +40,13 @@ pub enum Shape {
stroke: Stroke,
},
Text {
/// Top left corner of the first character.
/// Top left corner of the first character..
pos: Pos2,
/// The layed out text
/// The layed out text.
galley: Galley,
text_style: TextStyle, // TODO: Font?
/// Text color (foreground).
color: Color32,
/// If true, tilt the letters for an ugly italics effect
/// If true, tilt the letters for a hacky italics effect.
fake_italics: bool,
},
Mesh(Mesh),
@@ -137,7 +137,6 @@ impl Shape {
Self::Text {
pos: rect.min,
galley,
text_style,
color,
fake_italics: false,
}

View File

@@ -566,7 +566,6 @@ impl Tessellator {
Shape::Text {
pos,
galley,
text_style,
color,
fake_italics,
} => {
@@ -581,7 +580,7 @@ impl Tessellator {
out,
);
}
self.tessellate_text(fonts, pos, &galley, text_style, color, fake_italics, out);
self.tessellate_text(fonts, pos, &galley, color, fake_italics, out);
}
}
}
@@ -622,7 +621,6 @@ impl Tessellator {
fonts: &Fonts,
pos: Pos2,
galley: &super::Galley,
text_style: super::TextStyle,
color: Color32,
fake_italics: bool,
out: &mut Mesh,
@@ -641,7 +639,7 @@ impl Tessellator {
let clip_rect = self.clip_rect.expand(2.0); // Some fudge to handle letters that are slightly larger than expected.
let font = &fonts[text_style];
let font = &fonts[galley.text_style];
let mut chars = galley.text.chars();
for line in &galley.rows {
let line_min_y = pos.y + line.y_min;

View File

@@ -7,7 +7,10 @@ use {
use crate::{
mutex::{Mutex, RwLock},
text::galley::{Galley, Row},
text::{
galley::{Galley, Row},
TextStyle,
},
TextureAtlas,
};
use emath::{vec2, Vec2};
@@ -153,8 +156,8 @@ type FontIndex = usize;
// TODO: rename?
/// Wrapper over multiple `FontImpl` (e.g. a primary + fallbacks for emojis)
#[derive(Default)]
pub struct Font {
text_style: TextStyle,
fonts: Vec<Arc<FontImpl>>,
replacement_glyph: (FontIndex, GlyphInfo),
pixels_per_point: f32,
@@ -163,15 +166,23 @@ pub struct Font {
}
impl Font {
pub fn new(fonts: Vec<Arc<FontImpl>>) -> Self {
pub fn new(text_style: TextStyle, fonts: Vec<Arc<FontImpl>>) -> Self {
if fonts.is_empty() {
return Default::default();
return Self {
text_style,
fonts,
replacement_glyph: Default::default(),
pixels_per_point: 0.0,
row_height: 0.0,
glyph_info_cache: Default::default(),
};
}
let pixels_per_point = fonts[0].pixels_per_point();
let row_height = fonts[0].row_height();
let mut slf = Self {
text_style,
fonts,
replacement_glyph: Default::default(),
pixels_per_point,
@@ -204,6 +215,11 @@ impl Font {
slf
}
#[inline(always)]
pub fn text_style(&self) -> TextStyle {
self.text_style
}
#[inline]
pub fn round_to_pixel(&self, point: f32) -> f32 {
(point * self.pixels_per_point).round() / self.pixels_per_point
@@ -301,6 +317,7 @@ impl Font {
let width = row.max_x();
let size = vec2(width, self.row_height());
let galley = Galley {
text_style: self.text_style,
text,
rows: vec![row],
size,
@@ -391,7 +408,13 @@ impl Font {
}
let size = vec2(widest_row, rows.last().unwrap().y_max);
let galley = Galley { text, rows, size };
let text_style = self.text_style;
let galley = Galley {
text_style,
text,
rows,
size,
};
galley.sanity_check();
galley
}

View File

@@ -221,7 +221,7 @@ impl Fonts {
.map(|font_name| font_impl_cache.font_impl(font_name, scale_in_points))
.collect();
(text_style, Font::new(fonts))
(text_style, Font::new(text_style, fonts))
})
.collect();

View File

@@ -23,8 +23,11 @@ use super::cursor::*;
use emath::{pos2, NumExt, Rect, Vec2};
/// A collection of text locked into place.
#[derive(Clone, Debug, Default, PartialEq)]
#[derive(Clone, Debug, PartialEq)]
pub struct Galley {
/// The [`crate::TextStyle`] (font) used.
pub text_style: crate::TextStyle,
/// The full text, including any an all `\n`.
pub text: String,