2021-09-25 09:22:34 +00:00
|
|
|
use std::{collections::BTreeMap, sync::Arc};
|
2019-01-12 23:55:56 +00:00
|
|
|
|
2021-01-10 14:42:46 +00:00
|
|
|
use crate::{
|
|
|
|
mutex::Mutex,
|
2021-03-29 18:57:51 +00:00
|
|
|
text::{
|
|
|
|
font::{Font, FontImpl},
|
New text layout (#682)
This PR introduces a completely rewritten text layout engine which is simpler and more powerful. It allows mixing different text styles (heading, body, etc) and formats (color, underlining, strikethrough, …) in the same layout pass, and baked into the same `Galley`.
This opens up the door to having a syntax-highlighed code editor, or a WYSIWYG markdown editor.
One major change is the color is now baked in at layout time. However, many widgets changes text color on hovered. But we need to do the text layout before we know if it is hovered. Therefor the painter has an option to override the text color of a galley.
## Performance
Text layout alone is about 20% slower, but a lot of that is because more tessellation is done upfront. Text tessellation is now a lot faster, but text layout + tessellation still lands at a net loss of 5-10% in performance. There are however a few tricks to speed it up (like using `smallvec`) which I am saving for later. Text layout is also cached, meaning that in most cases (when all text isn't changing each frame) text tessellation is actually more important (and that's more than 2x faster!).
Sadly, the actual text cache lookup is significantly slower (300ns -> 600ns). That's because the `TextLayoutJob` is a lot bigger (it has more options, like underlining, fonts etc), so it is slower to hash and compare. I have an idea how to speed this up, but I need to do some other work before I can implement that.
All in all, the performance impact on `demo_with_tesselate__realistic` is about 5-6% in the red. Not great; not terrible. The benefits are worth it, but I also think with some work I can get that down significantly, hopefully down to the old levels.
2021-09-03 16:18:00 +00:00
|
|
|
Galley, LayoutJob,
|
2021-03-29 18:57:51 +00:00
|
|
|
},
|
2021-01-10 14:42:46 +00:00
|
|
|
Texture, TextureAtlas,
|
|
|
|
};
|
2019-01-12 23:55:56 +00:00
|
|
|
|
2020-07-23 12:35:12 +00:00
|
|
|
// TODO: rename
|
2020-12-27 13:16:37 +00:00
|
|
|
/// One of a few categories of styles of text, e.g. body, button or heading.
|
2020-05-30 09:04:40 +00:00
|
|
|
#[derive(Copy, Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
|
2021-09-29 06:45:13 +00:00
|
|
|
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
|
|
|
|
#[cfg_attr(feature = "serde", serde(rename_all = "snake_case"))]
|
2019-01-12 23:55:56 +00:00
|
|
|
pub enum TextStyle {
|
2020-12-27 13:16:37 +00:00
|
|
|
/// Used when small text is needed.
|
2020-10-25 07:56:48 +00:00
|
|
|
Small,
|
2020-12-27 13:16:37 +00:00
|
|
|
/// Normal labels. Easily readable, doesn't take up too much space.
|
2019-01-12 23:55:56 +00:00
|
|
|
Body,
|
2020-12-27 13:16:37 +00:00
|
|
|
/// Buttons. Maybe slightly bigger than `Body`.
|
2019-01-12 23:55:56 +00:00
|
|
|
Button,
|
2020-12-27 13:16:37 +00:00
|
|
|
/// Heading. Probably larger than `Body`.
|
2019-01-12 23:55:56 +00:00
|
|
|
Heading,
|
2020-12-27 13:16:37 +00:00
|
|
|
/// Same size as `Body`, but used when monospace is important (for aligning number, code snippets, etc).
|
2019-12-02 19:08:49 +00:00
|
|
|
Monospace,
|
2019-01-12 23:55:56 +00:00
|
|
|
}
|
|
|
|
|
2020-12-12 18:30:01 +00:00
|
|
|
impl TextStyle {
|
|
|
|
pub fn all() -> impl Iterator<Item = TextStyle> {
|
|
|
|
[
|
|
|
|
TextStyle::Small,
|
|
|
|
TextStyle::Body,
|
|
|
|
TextStyle::Button,
|
|
|
|
TextStyle::Heading,
|
|
|
|
TextStyle::Monospace,
|
|
|
|
]
|
|
|
|
.iter()
|
|
|
|
.copied()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-27 13:19:20 +00:00
|
|
|
/// Which style of font: [`Monospace`][`FontFamily::Monospace`] or [`Proportional`][`FontFamily::Proportional`].
|
2020-05-30 09:04:40 +00:00
|
|
|
#[derive(Copy, Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
|
2021-09-29 06:45:13 +00:00
|
|
|
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
|
|
|
|
#[cfg_attr(feature = "serde", serde(rename_all = "snake_case"))]
|
2019-12-02 19:08:49 +00:00
|
|
|
pub enum FontFamily {
|
2020-12-27 13:16:37 +00:00
|
|
|
/// A font where each character is the same width (`w` is the same width as `i`).
|
2019-12-02 19:08:49 +00:00
|
|
|
Monospace,
|
2020-12-27 13:16:37 +00:00
|
|
|
/// A font where some characters are wider than other (e.g. 'w' is wider than 'i').
|
2020-12-27 13:19:20 +00:00
|
|
|
Proportional,
|
2019-12-02 19:08:49 +00:00
|
|
|
}
|
|
|
|
|
2020-12-13 20:04:02 +00:00
|
|
|
/// The data of a `.ttf` or `.otf` file.
|
|
|
|
pub type FontData = std::borrow::Cow<'static, [u8]>;
|
|
|
|
|
2021-06-24 10:13:57 +00:00
|
|
|
fn ab_glyph_font_from_font_data(name: &str, data: &FontData) -> ab_glyph::FontArc {
|
2020-12-13 20:04:02 +00:00
|
|
|
match data {
|
2021-06-24 10:13:57 +00:00
|
|
|
std::borrow::Cow::Borrowed(bytes) => ab_glyph::FontArc::try_from_slice(bytes),
|
|
|
|
std::borrow::Cow::Owned(bytes) => ab_glyph::FontArc::try_from_vec(bytes.clone()),
|
2020-12-13 20:04:02 +00:00
|
|
|
}
|
2021-06-24 10:13:57 +00:00
|
|
|
.unwrap_or_else(|err| panic!("Error parsing {:?} TTF/OTF font file: {}", name, err))
|
2020-12-13 20:04:02 +00:00
|
|
|
}
|
|
|
|
|
2020-12-27 13:16:37 +00:00
|
|
|
/// Describes the font data and the sizes to use.
|
|
|
|
///
|
|
|
|
/// Often you would start with [`FontDefinitions::default()`] and then add/change the contents.
|
|
|
|
///
|
2021-01-17 09:52:01 +00:00
|
|
|
/// ```
|
2021-05-11 12:56:27 +00:00
|
|
|
/// # use {epaint::text::{FontDefinitions, TextStyle, FontFamily}};
|
|
|
|
/// # struct FakeEguiCtx {};
|
|
|
|
/// # impl FakeEguiCtx { fn set_fonts(&self, _: FontDefinitions) {} }
|
|
|
|
/// # let ctx = FakeEguiCtx {};
|
|
|
|
/// let mut fonts = FontDefinitions::default();
|
|
|
|
///
|
2020-12-27 13:16:37 +00:00
|
|
|
/// // Large button text:
|
|
|
|
/// fonts.family_and_size.insert(
|
2021-05-11 12:56:27 +00:00
|
|
|
/// TextStyle::Button,
|
|
|
|
/// (FontFamily::Proportional, 32.0)
|
|
|
|
/// );
|
|
|
|
///
|
|
|
|
/// ctx.set_fonts(fonts);
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// You can also install your own custom fonts:
|
|
|
|
/// ```
|
|
|
|
/// # use {epaint::text::{FontDefinitions, TextStyle, FontFamily}};
|
|
|
|
/// # struct FakeEguiCtx {};
|
|
|
|
/// # impl FakeEguiCtx { fn set_fonts(&self, _: FontDefinitions) {} }
|
|
|
|
/// # let ctx = FakeEguiCtx {};
|
|
|
|
/// let mut fonts = FontDefinitions::default();
|
|
|
|
///
|
|
|
|
/// // Install my own font (maybe supporting non-latin characters):
|
|
|
|
/// fonts.font_data.insert("my_font".to_owned(),
|
|
|
|
/// std::borrow::Cow::Borrowed(include_bytes!("../../fonts/Ubuntu-Light.ttf"))); // .ttf and .otf supported
|
|
|
|
///
|
|
|
|
/// // Put my font first (highest priority):
|
|
|
|
/// fonts.fonts_for_family.get_mut(&FontFamily::Proportional).unwrap()
|
|
|
|
/// .insert(0, "my_font".to_owned());
|
|
|
|
///
|
|
|
|
/// // Put my font as last fallback for monospace:
|
|
|
|
/// fonts.fonts_for_family.get_mut(&FontFamily::Monospace).unwrap()
|
|
|
|
/// .push("my_font".to_owned());
|
|
|
|
///
|
|
|
|
/// ctx.set_fonts(fonts);
|
2020-12-27 13:16:37 +00:00
|
|
|
/// ```
|
2020-05-30 12:56:38 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
2021-09-29 06:45:13 +00:00
|
|
|
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
|
|
|
|
#[cfg_attr(feature = "serde", serde(default))]
|
2020-05-30 12:56:38 +00:00
|
|
|
pub struct FontDefinitions {
|
2020-12-13 18:19:57 +00:00
|
|
|
/// List of font names and their definitions.
|
|
|
|
/// The definition must be the contents of either a `.ttf` or `.otf` font file.
|
|
|
|
///
|
2021-01-17 09:52:01 +00:00
|
|
|
/// `epaint` has built-in-default for these,
|
2020-10-31 17:03:13 +00:00
|
|
|
/// but you can override them if you like.
|
2020-12-13 20:04:02 +00:00
|
|
|
pub font_data: BTreeMap<String, FontData>,
|
2020-12-13 18:19:57 +00:00
|
|
|
|
2020-12-27 13:16:37 +00:00
|
|
|
/// Which fonts (names) to use for each [`FontFamily`].
|
2020-12-13 18:19:57 +00:00
|
|
|
///
|
2020-12-27 13:16:37 +00:00
|
|
|
/// The list should be a list of keys into [`Self::font_data`].
|
2021-01-17 09:52:01 +00:00
|
|
|
/// When looking for a character glyph `epaint` will start with
|
2020-12-13 19:37:44 +00:00
|
|
|
/// the first font and then move to the second, and so on.
|
2020-12-13 18:19:57 +00:00
|
|
|
/// So the first font is the primary, and then comes a list of fallbacks in order of priority.
|
|
|
|
pub fonts_for_family: BTreeMap<FontFamily, Vec<String>>,
|
|
|
|
|
2020-12-27 13:16:37 +00:00
|
|
|
/// The [`FontFamily`] and size you want to use for a specific [`TextStyle`].
|
2020-12-13 18:19:57 +00:00
|
|
|
pub family_and_size: BTreeMap<TextStyle, (FontFamily, f32)>,
|
2020-05-30 12:56:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for FontDefinitions {
|
|
|
|
fn default() -> Self {
|
2020-12-26 20:20:55 +00:00
|
|
|
#[allow(unused)]
|
2020-12-13 20:04:02 +00:00
|
|
|
let mut font_data: BTreeMap<String, FontData> = BTreeMap::new();
|
2020-12-13 18:19:57 +00:00
|
|
|
|
|
|
|
let mut fonts_for_family = BTreeMap::new();
|
2020-12-26 20:20:55 +00:00
|
|
|
|
|
|
|
#[cfg(feature = "default_fonts")]
|
|
|
|
{
|
|
|
|
// TODO: figure out a way to make the WASM smaller despite including fonts. Zip them?
|
|
|
|
|
|
|
|
// Use size 13 for this. NOTHING ELSE:
|
|
|
|
font_data.insert(
|
2020-12-13 18:19:57 +00:00
|
|
|
"ProggyClean".to_owned(),
|
2021-01-10 14:42:46 +00:00
|
|
|
std::borrow::Cow::Borrowed(include_bytes!("../../fonts/ProggyClean.ttf")),
|
2020-12-26 20:20:55 +00:00
|
|
|
);
|
|
|
|
font_data.insert(
|
2020-12-13 18:19:57 +00:00
|
|
|
"Ubuntu-Light".to_owned(),
|
2021-01-10 14:42:46 +00:00
|
|
|
std::borrow::Cow::Borrowed(include_bytes!("../../fonts/Ubuntu-Light.ttf")),
|
2020-12-26 20:20:55 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// Some good looking emojis. Use as first priority:
|
|
|
|
font_data.insert(
|
2020-12-13 18:19:57 +00:00
|
|
|
"NotoEmoji-Regular".to_owned(),
|
2021-01-10 14:42:46 +00:00
|
|
|
std::borrow::Cow::Borrowed(include_bytes!("../../fonts/NotoEmoji-Regular.ttf")),
|
2020-12-26 20:20:55 +00:00
|
|
|
);
|
|
|
|
// Bigger emojis, and more. <http://jslegers.github.io/emoji-icon-font/>:
|
|
|
|
font_data.insert(
|
2020-12-13 18:19:57 +00:00
|
|
|
"emoji-icon-font".to_owned(),
|
2021-01-10 14:42:46 +00:00
|
|
|
std::borrow::Cow::Borrowed(include_bytes!("../../fonts/emoji-icon-font.ttf")),
|
2020-12-26 20:20:55 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
fonts_for_family.insert(
|
|
|
|
FontFamily::Monospace,
|
|
|
|
vec![
|
|
|
|
"ProggyClean".to_owned(),
|
|
|
|
"Ubuntu-Light".to_owned(), // fallback for √ etc
|
|
|
|
"NotoEmoji-Regular".to_owned(),
|
|
|
|
"emoji-icon-font".to_owned(),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
fonts_for_family.insert(
|
2020-12-27 13:19:20 +00:00
|
|
|
FontFamily::Proportional,
|
2020-12-26 20:20:55 +00:00
|
|
|
vec![
|
|
|
|
"Ubuntu-Light".to_owned(),
|
|
|
|
"NotoEmoji-Regular".to_owned(),
|
|
|
|
"emoji-icon-font".to_owned(),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(feature = "default_fonts"))]
|
|
|
|
{
|
|
|
|
fonts_for_family.insert(FontFamily::Monospace, vec![]);
|
2020-12-27 13:19:20 +00:00
|
|
|
fonts_for_family.insert(FontFamily::Proportional, vec![]);
|
2020-12-26 20:20:55 +00:00
|
|
|
}
|
2020-12-13 18:19:57 +00:00
|
|
|
|
|
|
|
let mut family_and_size = BTreeMap::new();
|
2020-12-27 13:19:20 +00:00
|
|
|
family_and_size.insert(TextStyle::Small, (FontFamily::Proportional, 10.0));
|
|
|
|
family_and_size.insert(TextStyle::Body, (FontFamily::Proportional, 14.0));
|
2021-06-12 13:53:56 +00:00
|
|
|
family_and_size.insert(TextStyle::Button, (FontFamily::Proportional, 14.0));
|
2020-12-27 13:19:20 +00:00
|
|
|
family_and_size.insert(TextStyle::Heading, (FontFamily::Proportional, 20.0));
|
2020-12-13 18:19:57 +00:00
|
|
|
family_and_size.insert(TextStyle::Monospace, (FontFamily::Monospace, 13.0)); // 13 for `ProggyClean`
|
2020-05-30 12:56:38 +00:00
|
|
|
|
|
|
|
Self {
|
2020-12-13 18:19:57 +00:00
|
|
|
font_data,
|
|
|
|
fonts_for_family,
|
|
|
|
family_and_size,
|
2020-05-30 12:56:38 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-12 10:07:51 +00:00
|
|
|
}
|
|
|
|
|
2021-01-17 09:52:01 +00:00
|
|
|
/// The collection of fonts used by `epaint`.
|
2021-10-10 13:35:13 +00:00
|
|
|
///
|
|
|
|
/// Required in order to paint text.
|
2019-01-12 23:55:56 +00:00
|
|
|
pub struct Fonts {
|
2020-12-27 22:09:51 +00:00
|
|
|
pixels_per_point: f32,
|
2019-12-02 19:08:49 +00:00
|
|
|
definitions: FontDefinitions,
|
2019-01-12 23:55:56 +00:00
|
|
|
fonts: BTreeMap<TextStyle, Font>,
|
2020-09-09 12:24:13 +00:00
|
|
|
atlas: Arc<Mutex<TextureAtlas>>,
|
|
|
|
/// Copy of the texture in the texture atlas.
|
|
|
|
/// This is so we can return a reference to it (the texture atlas is behind a lock).
|
|
|
|
buffered_texture: Mutex<Arc<Texture>>,
|
2021-03-29 20:06:55 +00:00
|
|
|
|
|
|
|
galley_cache: Mutex<GalleyCache>,
|
2019-01-12 23:55:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Fonts {
|
2021-10-10 13:35:13 +00:00
|
|
|
/// Create a new [`Fonts`] for text layout.
|
|
|
|
/// This call is expensive, so only create on [`Fonts`] and then reuse it.
|
2021-09-20 19:36:56 +00:00
|
|
|
pub fn new(pixels_per_point: f32, definitions: FontDefinitions) -> Self {
|
2021-02-23 11:03:20 +00:00
|
|
|
assert!(
|
|
|
|
0.0 < pixels_per_point && pixels_per_point < 100.0,
|
|
|
|
"pixels_per_point out of range: {}",
|
|
|
|
pixels_per_point
|
|
|
|
);
|
|
|
|
|
2020-12-12 18:30:01 +00:00
|
|
|
// We want an atlas big enough to be able to include all the Emojis in the `TextStyle::Heading`,
|
|
|
|
// so we can show the Emoji picker demo window.
|
|
|
|
let mut atlas = TextureAtlas::new(2048, 64);
|
2019-01-12 23:55:56 +00:00
|
|
|
|
2020-09-09 12:24:13 +00:00
|
|
|
{
|
2020-09-09 13:24:09 +00:00
|
|
|
// Make the top left pixel fully white:
|
|
|
|
let pos = atlas.allocate((1, 1));
|
2020-09-09 12:24:13 +00:00
|
|
|
assert_eq!(pos, (0, 0));
|
2020-09-09 13:24:09 +00:00
|
|
|
atlas.texture_mut()[pos] = 255;
|
2020-09-09 12:24:13 +00:00
|
|
|
}
|
2019-01-12 23:55:56 +00:00
|
|
|
|
|
|
|
let atlas = Arc::new(Mutex::new(atlas));
|
|
|
|
|
2020-12-27 22:09:51 +00:00
|
|
|
let mut font_impl_cache = FontImplCache::new(atlas.clone(), pixels_per_point, &definitions);
|
2020-12-12 18:30:01 +00:00
|
|
|
|
2020-12-27 22:09:51 +00:00
|
|
|
let fonts = definitions
|
2020-12-13 18:19:57 +00:00
|
|
|
.family_and_size
|
2020-12-12 18:30:01 +00:00
|
|
|
.iter()
|
2020-12-13 18:19:57 +00:00
|
|
|
.map(|(&text_style, &(family, scale_in_points))| {
|
2020-12-27 22:09:51 +00:00
|
|
|
let fonts = &definitions.fonts_for_family.get(&family);
|
2020-12-26 20:20:55 +00:00
|
|
|
let fonts = fonts.unwrap_or_else(|| {
|
|
|
|
panic!("FontFamily::{:?} is not bound to any fonts", family)
|
|
|
|
});
|
|
|
|
let fonts: Vec<Arc<FontImpl>> = fonts
|
2020-12-13 18:19:57 +00:00
|
|
|
.iter()
|
|
|
|
.map(|font_name| font_impl_cache.font_impl(font_name, scale_in_points))
|
|
|
|
.collect();
|
2020-12-11 22:39:32 +00:00
|
|
|
|
2021-03-29 19:24:09 +00:00
|
|
|
(text_style, Font::new(text_style, fonts))
|
2019-01-19 16:10:28 +00:00
|
|
|
})
|
2019-01-17 17:03:39 +00:00
|
|
|
.collect();
|
2019-01-12 23:55:56 +00:00
|
|
|
|
2020-09-09 12:24:13 +00:00
|
|
|
{
|
|
|
|
let mut atlas = atlas.lock();
|
|
|
|
let texture = atlas.texture_mut();
|
|
|
|
// Make sure we seed the texture version with something unique based on the default characters:
|
2021-09-25 09:22:34 +00:00
|
|
|
texture.version = crate::util::hash(&texture.pixels);
|
2020-09-09 12:24:13 +00:00
|
|
|
}
|
|
|
|
|
2020-12-27 22:09:51 +00:00
|
|
|
Self {
|
|
|
|
pixels_per_point,
|
|
|
|
definitions,
|
|
|
|
fonts,
|
|
|
|
atlas,
|
|
|
|
buffered_texture: Default::default(), //atlas.lock().texture().clone();
|
2021-03-29 20:06:55 +00:00
|
|
|
galley_cache: Default::default(),
|
2020-12-27 22:09:51 +00:00
|
|
|
}
|
2019-01-12 23:55:56 +00:00
|
|
|
}
|
|
|
|
|
2021-09-20 19:36:56 +00:00
|
|
|
#[deprecated = "Renamed to Fonts::new"]
|
|
|
|
pub fn from_definitions(pixels_per_point: f32, definitions: FontDefinitions) -> Self {
|
|
|
|
Self::new(pixels_per_point, definitions)
|
|
|
|
}
|
|
|
|
|
2021-03-30 19:07:19 +00:00
|
|
|
#[inline(always)]
|
2020-12-27 22:09:51 +00:00
|
|
|
pub fn pixels_per_point(&self) -> f32 {
|
|
|
|
self.pixels_per_point
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn definitions(&self) -> &FontDefinitions {
|
|
|
|
&self.definitions
|
|
|
|
}
|
|
|
|
|
2021-03-30 19:07:19 +00:00
|
|
|
#[inline(always)]
|
|
|
|
pub fn round_to_pixel(&self, point: f32) -> f32 {
|
|
|
|
(point * self.pixels_per_point).round() / self.pixels_per_point
|
|
|
|
}
|
|
|
|
|
2021-09-07 18:37:50 +00:00
|
|
|
#[inline(always)]
|
|
|
|
pub fn floor_to_pixel(&self, point: f32) -> f32 {
|
|
|
|
(point * self.pixels_per_point).floor() / self.pixels_per_point
|
|
|
|
}
|
|
|
|
|
2020-12-27 22:09:51 +00:00
|
|
|
/// Call each frame to get the latest available font texture data.
|
2020-09-09 12:24:13 +00:00
|
|
|
pub fn texture(&self) -> Arc<Texture> {
|
|
|
|
let atlas = self.atlas.lock();
|
|
|
|
let mut buffered_texture = self.buffered_texture.lock();
|
|
|
|
if buffered_texture.version != atlas.texture().version {
|
|
|
|
*buffered_texture = Arc::new(atlas.texture().clone());
|
|
|
|
}
|
|
|
|
|
|
|
|
buffered_texture.clone()
|
2019-01-12 23:55:56 +00:00
|
|
|
}
|
2021-03-29 18:57:51 +00:00
|
|
|
|
|
|
|
/// Width of this character in points.
|
|
|
|
pub fn glyph_width(&self, text_style: TextStyle, c: char) -> f32 {
|
|
|
|
self.fonts[&text_style].glyph_width(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Height of one row of text. In points
|
|
|
|
pub fn row_height(&self, text_style: TextStyle) -> f32 {
|
|
|
|
self.fonts[&text_style].row_height()
|
|
|
|
}
|
|
|
|
|
New text layout (#682)
This PR introduces a completely rewritten text layout engine which is simpler and more powerful. It allows mixing different text styles (heading, body, etc) and formats (color, underlining, strikethrough, …) in the same layout pass, and baked into the same `Galley`.
This opens up the door to having a syntax-highlighed code editor, or a WYSIWYG markdown editor.
One major change is the color is now baked in at layout time. However, many widgets changes text color on hovered. But we need to do the text layout before we know if it is hovered. Therefor the painter has an option to override the text color of a galley.
## Performance
Text layout alone is about 20% slower, but a lot of that is because more tessellation is done upfront. Text tessellation is now a lot faster, but text layout + tessellation still lands at a net loss of 5-10% in performance. There are however a few tricks to speed it up (like using `smallvec`) which I am saving for later. Text layout is also cached, meaning that in most cases (when all text isn't changing each frame) text tessellation is actually more important (and that's more than 2x faster!).
Sadly, the actual text cache lookup is significantly slower (300ns -> 600ns). That's because the `TextLayoutJob` is a lot bigger (it has more options, like underlining, fonts etc), so it is slower to hash and compare. I have an idea how to speed this up, but I need to do some other work before I can implement that.
All in all, the performance impact on `demo_with_tesselate__realistic` is about 5-6% in the red. Not great; not terrible. The benefits are worth it, but I also think with some work I can get that down significantly, hopefully down to the old levels.
2021-09-03 16:18:00 +00:00
|
|
|
/// Layout some text.
|
|
|
|
/// This is the most advanced layout function.
|
|
|
|
/// See also [`Self::layout`], [`Self::layout_no_wrap`] and
|
|
|
|
/// [`Self::layout_delayed_color`].
|
2021-05-20 20:09:35 +00:00
|
|
|
///
|
New text layout (#682)
This PR introduces a completely rewritten text layout engine which is simpler and more powerful. It allows mixing different text styles (heading, body, etc) and formats (color, underlining, strikethrough, …) in the same layout pass, and baked into the same `Galley`.
This opens up the door to having a syntax-highlighed code editor, or a WYSIWYG markdown editor.
One major change is the color is now baked in at layout time. However, many widgets changes text color on hovered. But we need to do the text layout before we know if it is hovered. Therefor the painter has an option to override the text color of a galley.
## Performance
Text layout alone is about 20% slower, but a lot of that is because more tessellation is done upfront. Text tessellation is now a lot faster, but text layout + tessellation still lands at a net loss of 5-10% in performance. There are however a few tricks to speed it up (like using `smallvec`) which I am saving for later. Text layout is also cached, meaning that in most cases (when all text isn't changing each frame) text tessellation is actually more important (and that's more than 2x faster!).
Sadly, the actual text cache lookup is significantly slower (300ns -> 600ns). That's because the `TextLayoutJob` is a lot bigger (it has more options, like underlining, fonts etc), so it is slower to hash and compare. I have an idea how to speed this up, but I need to do some other work before I can implement that.
All in all, the performance impact on `demo_with_tesselate__realistic` is about 5-6% in the red. Not great; not terrible. The benefits are worth it, but I also think with some work I can get that down significantly, hopefully down to the old levels.
2021-09-03 16:18:00 +00:00
|
|
|
/// The implementation uses memoization so repeated calls are cheap.
|
2021-09-04 08:19:58 +00:00
|
|
|
pub fn layout_job(&self, job: LayoutJob) -> Arc<Galley> {
|
|
|
|
self.galley_cache.lock().layout(self, job)
|
2021-03-29 18:57:51 +00:00
|
|
|
}
|
|
|
|
|
New text layout (#682)
This PR introduces a completely rewritten text layout engine which is simpler and more powerful. It allows mixing different text styles (heading, body, etc) and formats (color, underlining, strikethrough, …) in the same layout pass, and baked into the same `Galley`.
This opens up the door to having a syntax-highlighed code editor, or a WYSIWYG markdown editor.
One major change is the color is now baked in at layout time. However, many widgets changes text color on hovered. But we need to do the text layout before we know if it is hovered. Therefor the painter has an option to override the text color of a galley.
## Performance
Text layout alone is about 20% slower, but a lot of that is because more tessellation is done upfront. Text tessellation is now a lot faster, but text layout + tessellation still lands at a net loss of 5-10% in performance. There are however a few tricks to speed it up (like using `smallvec`) which I am saving for later. Text layout is also cached, meaning that in most cases (when all text isn't changing each frame) text tessellation is actually more important (and that's more than 2x faster!).
Sadly, the actual text cache lookup is significantly slower (300ns -> 600ns). That's because the `TextLayoutJob` is a lot bigger (it has more options, like underlining, fonts etc), so it is slower to hash and compare. I have an idea how to speed this up, but I need to do some other work before I can implement that.
All in all, the performance impact on `demo_with_tesselate__realistic` is about 5-6% in the red. Not great; not terrible. The benefits are worth it, but I also think with some work I can get that down significantly, hopefully down to the old levels.
2021-09-03 16:18:00 +00:00
|
|
|
/// Will wrap text at the given width and line break at `\n`.
|
2021-03-29 18:57:51 +00:00
|
|
|
///
|
New text layout (#682)
This PR introduces a completely rewritten text layout engine which is simpler and more powerful. It allows mixing different text styles (heading, body, etc) and formats (color, underlining, strikethrough, …) in the same layout pass, and baked into the same `Galley`.
This opens up the door to having a syntax-highlighed code editor, or a WYSIWYG markdown editor.
One major change is the color is now baked in at layout time. However, many widgets changes text color on hovered. But we need to do the text layout before we know if it is hovered. Therefor the painter has an option to override the text color of a galley.
## Performance
Text layout alone is about 20% slower, but a lot of that is because more tessellation is done upfront. Text tessellation is now a lot faster, but text layout + tessellation still lands at a net loss of 5-10% in performance. There are however a few tricks to speed it up (like using `smallvec`) which I am saving for later. Text layout is also cached, meaning that in most cases (when all text isn't changing each frame) text tessellation is actually more important (and that's more than 2x faster!).
Sadly, the actual text cache lookup is significantly slower (300ns -> 600ns). That's because the `TextLayoutJob` is a lot bigger (it has more options, like underlining, fonts etc), so it is slower to hash and compare. I have an idea how to speed this up, but I need to do some other work before I can implement that.
All in all, the performance impact on `demo_with_tesselate__realistic` is about 5-6% in the red. Not great; not terrible. The benefits are worth it, but I also think with some work I can get that down significantly, hopefully down to the old levels.
2021-09-03 16:18:00 +00:00
|
|
|
/// The implementation uses memoization so repeated calls are cheap.
|
|
|
|
pub fn layout(
|
|
|
|
&self,
|
|
|
|
text: String,
|
|
|
|
text_style: TextStyle,
|
|
|
|
color: crate::Color32,
|
|
|
|
wrap_width: f32,
|
|
|
|
) -> Arc<Galley> {
|
|
|
|
let job = LayoutJob::simple(text, text_style, color, wrap_width);
|
|
|
|
self.layout_job(job)
|
2021-03-29 18:57:51 +00:00
|
|
|
}
|
|
|
|
|
New text layout (#682)
This PR introduces a completely rewritten text layout engine which is simpler and more powerful. It allows mixing different text styles (heading, body, etc) and formats (color, underlining, strikethrough, …) in the same layout pass, and baked into the same `Galley`.
This opens up the door to having a syntax-highlighed code editor, or a WYSIWYG markdown editor.
One major change is the color is now baked in at layout time. However, many widgets changes text color on hovered. But we need to do the text layout before we know if it is hovered. Therefor the painter has an option to override the text color of a galley.
## Performance
Text layout alone is about 20% slower, but a lot of that is because more tessellation is done upfront. Text tessellation is now a lot faster, but text layout + tessellation still lands at a net loss of 5-10% in performance. There are however a few tricks to speed it up (like using `smallvec`) which I am saving for later. Text layout is also cached, meaning that in most cases (when all text isn't changing each frame) text tessellation is actually more important (and that's more than 2x faster!).
Sadly, the actual text cache lookup is significantly slower (300ns -> 600ns). That's because the `TextLayoutJob` is a lot bigger (it has more options, like underlining, fonts etc), so it is slower to hash and compare. I have an idea how to speed this up, but I need to do some other work before I can implement that.
All in all, the performance impact on `demo_with_tesselate__realistic` is about 5-6% in the red. Not great; not terrible. The benefits are worth it, but I also think with some work I can get that down significantly, hopefully down to the old levels.
2021-09-03 16:18:00 +00:00
|
|
|
/// Will line break at `\n`.
|
2021-05-20 20:09:35 +00:00
|
|
|
///
|
New text layout (#682)
This PR introduces a completely rewritten text layout engine which is simpler and more powerful. It allows mixing different text styles (heading, body, etc) and formats (color, underlining, strikethrough, …) in the same layout pass, and baked into the same `Galley`.
This opens up the door to having a syntax-highlighed code editor, or a WYSIWYG markdown editor.
One major change is the color is now baked in at layout time. However, many widgets changes text color on hovered. But we need to do the text layout before we know if it is hovered. Therefor the painter has an option to override the text color of a galley.
## Performance
Text layout alone is about 20% slower, but a lot of that is because more tessellation is done upfront. Text tessellation is now a lot faster, but text layout + tessellation still lands at a net loss of 5-10% in performance. There are however a few tricks to speed it up (like using `smallvec`) which I am saving for later. Text layout is also cached, meaning that in most cases (when all text isn't changing each frame) text tessellation is actually more important (and that's more than 2x faster!).
Sadly, the actual text cache lookup is significantly slower (300ns -> 600ns). That's because the `TextLayoutJob` is a lot bigger (it has more options, like underlining, fonts etc), so it is slower to hash and compare. I have an idea how to speed this up, but I need to do some other work before I can implement that.
All in all, the performance impact on `demo_with_tesselate__realistic` is about 5-6% in the red. Not great; not terrible. The benefits are worth it, but I also think with some work I can get that down significantly, hopefully down to the old levels.
2021-09-03 16:18:00 +00:00
|
|
|
/// The implementation uses memoization so repeated calls are cheap.
|
|
|
|
pub fn layout_no_wrap(
|
2021-03-29 18:57:51 +00:00
|
|
|
&self,
|
|
|
|
text: String,
|
New text layout (#682)
This PR introduces a completely rewritten text layout engine which is simpler and more powerful. It allows mixing different text styles (heading, body, etc) and formats (color, underlining, strikethrough, …) in the same layout pass, and baked into the same `Galley`.
This opens up the door to having a syntax-highlighed code editor, or a WYSIWYG markdown editor.
One major change is the color is now baked in at layout time. However, many widgets changes text color on hovered. But we need to do the text layout before we know if it is hovered. Therefor the painter has an option to override the text color of a galley.
## Performance
Text layout alone is about 20% slower, but a lot of that is because more tessellation is done upfront. Text tessellation is now a lot faster, but text layout + tessellation still lands at a net loss of 5-10% in performance. There are however a few tricks to speed it up (like using `smallvec`) which I am saving for later. Text layout is also cached, meaning that in most cases (when all text isn't changing each frame) text tessellation is actually more important (and that's more than 2x faster!).
Sadly, the actual text cache lookup is significantly slower (300ns -> 600ns). That's because the `TextLayoutJob` is a lot bigger (it has more options, like underlining, fonts etc), so it is slower to hash and compare. I have an idea how to speed this up, but I need to do some other work before I can implement that.
All in all, the performance impact on `demo_with_tesselate__realistic` is about 5-6% in the red. Not great; not terrible. The benefits are worth it, but I also think with some work I can get that down significantly, hopefully down to the old levels.
2021-09-03 16:18:00 +00:00
|
|
|
text_style: TextStyle,
|
|
|
|
color: crate::Color32,
|
2021-03-29 20:30:18 +00:00
|
|
|
) -> Arc<Galley> {
|
New text layout (#682)
This PR introduces a completely rewritten text layout engine which is simpler and more powerful. It allows mixing different text styles (heading, body, etc) and formats (color, underlining, strikethrough, …) in the same layout pass, and baked into the same `Galley`.
This opens up the door to having a syntax-highlighed code editor, or a WYSIWYG markdown editor.
One major change is the color is now baked in at layout time. However, many widgets changes text color on hovered. But we need to do the text layout before we know if it is hovered. Therefor the painter has an option to override the text color of a galley.
## Performance
Text layout alone is about 20% slower, but a lot of that is because more tessellation is done upfront. Text tessellation is now a lot faster, but text layout + tessellation still lands at a net loss of 5-10% in performance. There are however a few tricks to speed it up (like using `smallvec`) which I am saving for later. Text layout is also cached, meaning that in most cases (when all text isn't changing each frame) text tessellation is actually more important (and that's more than 2x faster!).
Sadly, the actual text cache lookup is significantly slower (300ns -> 600ns). That's because the `TextLayoutJob` is a lot bigger (it has more options, like underlining, fonts etc), so it is slower to hash and compare. I have an idea how to speed this up, but I need to do some other work before I can implement that.
All in all, the performance impact on `demo_with_tesselate__realistic` is about 5-6% in the red. Not great; not terrible. The benefits are worth it, but I also think with some work I can get that down significantly, hopefully down to the old levels.
2021-09-03 16:18:00 +00:00
|
|
|
let job = LayoutJob::simple(text, text_style, color, f32::INFINITY);
|
|
|
|
self.layout_job(job)
|
2021-03-29 18:57:51 +00:00
|
|
|
}
|
|
|
|
|
New text layout (#682)
This PR introduces a completely rewritten text layout engine which is simpler and more powerful. It allows mixing different text styles (heading, body, etc) and formats (color, underlining, strikethrough, …) in the same layout pass, and baked into the same `Galley`.
This opens up the door to having a syntax-highlighed code editor, or a WYSIWYG markdown editor.
One major change is the color is now baked in at layout time. However, many widgets changes text color on hovered. But we need to do the text layout before we know if it is hovered. Therefor the painter has an option to override the text color of a galley.
## Performance
Text layout alone is about 20% slower, but a lot of that is because more tessellation is done upfront. Text tessellation is now a lot faster, but text layout + tessellation still lands at a net loss of 5-10% in performance. There are however a few tricks to speed it up (like using `smallvec`) which I am saving for later. Text layout is also cached, meaning that in most cases (when all text isn't changing each frame) text tessellation is actually more important (and that's more than 2x faster!).
Sadly, the actual text cache lookup is significantly slower (300ns -> 600ns). That's because the `TextLayoutJob` is a lot bigger (it has more options, like underlining, fonts etc), so it is slower to hash and compare. I have an idea how to speed this up, but I need to do some other work before I can implement that.
All in all, the performance impact on `demo_with_tesselate__realistic` is about 5-6% in the red. Not great; not terrible. The benefits are worth it, but I also think with some work I can get that down significantly, hopefully down to the old levels.
2021-09-03 16:18:00 +00:00
|
|
|
/// Like [`Self::layout`], made for when you want to pick a color for the text later.
|
2021-05-20 20:09:35 +00:00
|
|
|
///
|
New text layout (#682)
This PR introduces a completely rewritten text layout engine which is simpler and more powerful. It allows mixing different text styles (heading, body, etc) and formats (color, underlining, strikethrough, …) in the same layout pass, and baked into the same `Galley`.
This opens up the door to having a syntax-highlighed code editor, or a WYSIWYG markdown editor.
One major change is the color is now baked in at layout time. However, many widgets changes text color on hovered. But we need to do the text layout before we know if it is hovered. Therefor the painter has an option to override the text color of a galley.
## Performance
Text layout alone is about 20% slower, but a lot of that is because more tessellation is done upfront. Text tessellation is now a lot faster, but text layout + tessellation still lands at a net loss of 5-10% in performance. There are however a few tricks to speed it up (like using `smallvec`) which I am saving for later. Text layout is also cached, meaning that in most cases (when all text isn't changing each frame) text tessellation is actually more important (and that's more than 2x faster!).
Sadly, the actual text cache lookup is significantly slower (300ns -> 600ns). That's because the `TextLayoutJob` is a lot bigger (it has more options, like underlining, fonts etc), so it is slower to hash and compare. I have an idea how to speed this up, but I need to do some other work before I can implement that.
All in all, the performance impact on `demo_with_tesselate__realistic` is about 5-6% in the red. Not great; not terrible. The benefits are worth it, but I also think with some work I can get that down significantly, hopefully down to the old levels.
2021-09-03 16:18:00 +00:00
|
|
|
/// The implementation uses memoization so repeated calls are cheap.
|
|
|
|
pub fn layout_delayed_color(
|
2021-03-29 18:57:51 +00:00
|
|
|
&self,
|
|
|
|
text: String,
|
New text layout (#682)
This PR introduces a completely rewritten text layout engine which is simpler and more powerful. It allows mixing different text styles (heading, body, etc) and formats (color, underlining, strikethrough, …) in the same layout pass, and baked into the same `Galley`.
This opens up the door to having a syntax-highlighed code editor, or a WYSIWYG markdown editor.
One major change is the color is now baked in at layout time. However, many widgets changes text color on hovered. But we need to do the text layout before we know if it is hovered. Therefor the painter has an option to override the text color of a galley.
## Performance
Text layout alone is about 20% slower, but a lot of that is because more tessellation is done upfront. Text tessellation is now a lot faster, but text layout + tessellation still lands at a net loss of 5-10% in performance. There are however a few tricks to speed it up (like using `smallvec`) which I am saving for later. Text layout is also cached, meaning that in most cases (when all text isn't changing each frame) text tessellation is actually more important (and that's more than 2x faster!).
Sadly, the actual text cache lookup is significantly slower (300ns -> 600ns). That's because the `TextLayoutJob` is a lot bigger (it has more options, like underlining, fonts etc), so it is slower to hash and compare. I have an idea how to speed this up, but I need to do some other work before I can implement that.
All in all, the performance impact on `demo_with_tesselate__realistic` is about 5-6% in the red. Not great; not terrible. The benefits are worth it, but I also think with some work I can get that down significantly, hopefully down to the old levels.
2021-09-03 16:18:00 +00:00
|
|
|
text_style: TextStyle,
|
|
|
|
wrap_width: f32,
|
2021-03-29 20:30:18 +00:00
|
|
|
) -> Arc<Galley> {
|
New text layout (#682)
This PR introduces a completely rewritten text layout engine which is simpler and more powerful. It allows mixing different text styles (heading, body, etc) and formats (color, underlining, strikethrough, …) in the same layout pass, and baked into the same `Galley`.
This opens up the door to having a syntax-highlighed code editor, or a WYSIWYG markdown editor.
One major change is the color is now baked in at layout time. However, many widgets changes text color on hovered. But we need to do the text layout before we know if it is hovered. Therefor the painter has an option to override the text color of a galley.
## Performance
Text layout alone is about 20% slower, but a lot of that is because more tessellation is done upfront. Text tessellation is now a lot faster, but text layout + tessellation still lands at a net loss of 5-10% in performance. There are however a few tricks to speed it up (like using `smallvec`) which I am saving for later. Text layout is also cached, meaning that in most cases (when all text isn't changing each frame) text tessellation is actually more important (and that's more than 2x faster!).
Sadly, the actual text cache lookup is significantly slower (300ns -> 600ns). That's because the `TextLayoutJob` is a lot bigger (it has more options, like underlining, fonts etc), so it is slower to hash and compare. I have an idea how to speed this up, but I need to do some other work before I can implement that.
All in all, the performance impact on `demo_with_tesselate__realistic` is about 5-6% in the red. Not great; not terrible. The benefits are worth it, but I also think with some work I can get that down significantly, hopefully down to the old levels.
2021-09-03 16:18:00 +00:00
|
|
|
self.layout_job(LayoutJob::simple(
|
|
|
|
text,
|
|
|
|
text_style,
|
|
|
|
crate::Color32::TEMPORARY_COLOR,
|
|
|
|
wrap_width,
|
|
|
|
))
|
2021-03-29 18:57:51 +00:00
|
|
|
}
|
2021-03-29 20:06:55 +00:00
|
|
|
|
|
|
|
pub fn num_galleys_in_cache(&self) -> usize {
|
|
|
|
self.galley_cache.lock().num_galleys_in_cache()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Must be called once per frame to clear the [`Galley`] cache.
|
|
|
|
pub fn end_frame(&self) {
|
New text layout (#682)
This PR introduces a completely rewritten text layout engine which is simpler and more powerful. It allows mixing different text styles (heading, body, etc) and formats (color, underlining, strikethrough, …) in the same layout pass, and baked into the same `Galley`.
This opens up the door to having a syntax-highlighed code editor, or a WYSIWYG markdown editor.
One major change is the color is now baked in at layout time. However, many widgets changes text color on hovered. But we need to do the text layout before we know if it is hovered. Therefor the painter has an option to override the text color of a galley.
## Performance
Text layout alone is about 20% slower, but a lot of that is because more tessellation is done upfront. Text tessellation is now a lot faster, but text layout + tessellation still lands at a net loss of 5-10% in performance. There are however a few tricks to speed it up (like using `smallvec`) which I am saving for later. Text layout is also cached, meaning that in most cases (when all text isn't changing each frame) text tessellation is actually more important (and that's more than 2x faster!).
Sadly, the actual text cache lookup is significantly slower (300ns -> 600ns). That's because the `TextLayoutJob` is a lot bigger (it has more options, like underlining, fonts etc), so it is slower to hash and compare. I have an idea how to speed this up, but I need to do some other work before I can implement that.
All in all, the performance impact on `demo_with_tesselate__realistic` is about 5-6% in the red. Not great; not terrible. The benefits are worth it, but I also think with some work I can get that down significantly, hopefully down to the old levels.
2021-09-03 16:18:00 +00:00
|
|
|
self.galley_cache.lock().end_frame();
|
2021-03-29 20:06:55 +00:00
|
|
|
}
|
2019-01-12 23:55:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl std::ops::Index<TextStyle> for Fonts {
|
|
|
|
type Output = Font;
|
|
|
|
|
2021-04-01 20:10:34 +00:00
|
|
|
#[inline(always)]
|
2019-01-12 23:55:56 +00:00
|
|
|
fn index(&self, text_style: TextStyle) -> &Font {
|
|
|
|
&self.fonts[&text_style]
|
|
|
|
}
|
|
|
|
}
|
2020-12-12 18:30:01 +00:00
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2021-03-29 20:06:55 +00:00
|
|
|
struct CachedGalley {
|
|
|
|
/// When it was last used
|
|
|
|
last_used: u32,
|
2021-03-29 20:30:18 +00:00
|
|
|
galley: Arc<Galley>,
|
2021-03-29 20:06:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
struct GalleyCache {
|
|
|
|
/// Frame counter used to do garbage collection on the cache
|
|
|
|
generation: u32,
|
2021-09-04 08:19:58 +00:00
|
|
|
cache: nohash_hasher::IntMap<u64, CachedGalley>,
|
2021-03-29 20:06:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl GalleyCache {
|
2021-09-04 08:19:58 +00:00
|
|
|
fn layout(&mut self, fonts: &Fonts, job: LayoutJob) -> Arc<Galley> {
|
2021-10-06 15:44:51 +00:00
|
|
|
let hash = crate::util::hash(&job); // TODO: even faster hasher?
|
2021-09-04 08:19:58 +00:00
|
|
|
|
|
|
|
match self.cache.entry(hash) {
|
New text layout (#682)
This PR introduces a completely rewritten text layout engine which is simpler and more powerful. It allows mixing different text styles (heading, body, etc) and formats (color, underlining, strikethrough, …) in the same layout pass, and baked into the same `Galley`.
This opens up the door to having a syntax-highlighed code editor, or a WYSIWYG markdown editor.
One major change is the color is now baked in at layout time. However, many widgets changes text color on hovered. But we need to do the text layout before we know if it is hovered. Therefor the painter has an option to override the text color of a galley.
## Performance
Text layout alone is about 20% slower, but a lot of that is because more tessellation is done upfront. Text tessellation is now a lot faster, but text layout + tessellation still lands at a net loss of 5-10% in performance. There are however a few tricks to speed it up (like using `smallvec`) which I am saving for later. Text layout is also cached, meaning that in most cases (when all text isn't changing each frame) text tessellation is actually more important (and that's more than 2x faster!).
Sadly, the actual text cache lookup is significantly slower (300ns -> 600ns). That's because the `TextLayoutJob` is a lot bigger (it has more options, like underlining, fonts etc), so it is slower to hash and compare. I have an idea how to speed this up, but I need to do some other work before I can implement that.
All in all, the performance impact on `demo_with_tesselate__realistic` is about 5-6% in the red. Not great; not terrible. The benefits are worth it, but I also think with some work I can get that down significantly, hopefully down to the old levels.
2021-09-03 16:18:00 +00:00
|
|
|
std::collections::hash_map::Entry::Occupied(entry) => {
|
|
|
|
let cached = entry.into_mut();
|
|
|
|
cached.last_used = self.generation;
|
|
|
|
cached.galley.clone()
|
|
|
|
}
|
|
|
|
std::collections::hash_map::Entry::Vacant(entry) => {
|
2021-09-04 08:19:58 +00:00
|
|
|
let galley = super::layout(fonts, job.into());
|
New text layout (#682)
This PR introduces a completely rewritten text layout engine which is simpler and more powerful. It allows mixing different text styles (heading, body, etc) and formats (color, underlining, strikethrough, …) in the same layout pass, and baked into the same `Galley`.
This opens up the door to having a syntax-highlighed code editor, or a WYSIWYG markdown editor.
One major change is the color is now baked in at layout time. However, many widgets changes text color on hovered. But we need to do the text layout before we know if it is hovered. Therefor the painter has an option to override the text color of a galley.
## Performance
Text layout alone is about 20% slower, but a lot of that is because more tessellation is done upfront. Text tessellation is now a lot faster, but text layout + tessellation still lands at a net loss of 5-10% in performance. There are however a few tricks to speed it up (like using `smallvec`) which I am saving for later. Text layout is also cached, meaning that in most cases (when all text isn't changing each frame) text tessellation is actually more important (and that's more than 2x faster!).
Sadly, the actual text cache lookup is significantly slower (300ns -> 600ns). That's because the `TextLayoutJob` is a lot bigger (it has more options, like underlining, fonts etc), so it is slower to hash and compare. I have an idea how to speed this up, but I need to do some other work before I can implement that.
All in all, the performance impact on `demo_with_tesselate__realistic` is about 5-6% in the red. Not great; not terrible. The benefits are worth it, but I also think with some work I can get that down significantly, hopefully down to the old levels.
2021-09-03 16:18:00 +00:00
|
|
|
let galley = Arc::new(galley);
|
|
|
|
entry.insert(CachedGalley {
|
2021-03-29 20:06:55 +00:00
|
|
|
last_used: self.generation,
|
|
|
|
galley: galley.clone(),
|
New text layout (#682)
This PR introduces a completely rewritten text layout engine which is simpler and more powerful. It allows mixing different text styles (heading, body, etc) and formats (color, underlining, strikethrough, …) in the same layout pass, and baked into the same `Galley`.
This opens up the door to having a syntax-highlighed code editor, or a WYSIWYG markdown editor.
One major change is the color is now baked in at layout time. However, many widgets changes text color on hovered. But we need to do the text layout before we know if it is hovered. Therefor the painter has an option to override the text color of a galley.
## Performance
Text layout alone is about 20% slower, but a lot of that is because more tessellation is done upfront. Text tessellation is now a lot faster, but text layout + tessellation still lands at a net loss of 5-10% in performance. There are however a few tricks to speed it up (like using `smallvec`) which I am saving for later. Text layout is also cached, meaning that in most cases (when all text isn't changing each frame) text tessellation is actually more important (and that's more than 2x faster!).
Sadly, the actual text cache lookup is significantly slower (300ns -> 600ns). That's because the `TextLayoutJob` is a lot bigger (it has more options, like underlining, fonts etc), so it is slower to hash and compare. I have an idea how to speed this up, but I need to do some other work before I can implement that.
All in all, the performance impact on `demo_with_tesselate__realistic` is about 5-6% in the red. Not great; not terrible. The benefits are worth it, but I also think with some work I can get that down significantly, hopefully down to the old levels.
2021-09-03 16:18:00 +00:00
|
|
|
});
|
|
|
|
galley
|
|
|
|
}
|
2021-03-29 20:06:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn num_galleys_in_cache(&self) -> usize {
|
|
|
|
self.cache.len()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Must be called once per frame to clear the [`Galley`] cache.
|
|
|
|
pub fn end_frame(&mut self) {
|
|
|
|
let current_generation = self.generation;
|
|
|
|
self.cache.retain(|_key, cached| {
|
|
|
|
cached.last_used == current_generation // only keep those that were used this frame
|
|
|
|
});
|
|
|
|
self.generation = self.generation.wrapping_add(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2020-12-27 11:56:16 +00:00
|
|
|
struct FontImplCache {
|
2020-12-12 18:30:01 +00:00
|
|
|
atlas: Arc<Mutex<TextureAtlas>>,
|
|
|
|
pixels_per_point: f32,
|
2021-06-24 10:13:57 +00:00
|
|
|
ab_glyph_fonts: BTreeMap<String, ab_glyph::FontArc>,
|
2020-12-12 18:30:01 +00:00
|
|
|
|
2020-12-13 18:19:57 +00:00
|
|
|
/// Map font names and size to the cached `FontImpl`.
|
|
|
|
/// Can't have f32 in a HashMap or BTreeMap, so let's do a linear search
|
|
|
|
cache: Vec<(String, f32, Arc<FontImpl>)>,
|
2020-12-12 18:30:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl FontImplCache {
|
2020-12-27 22:09:51 +00:00
|
|
|
pub fn new(
|
|
|
|
atlas: Arc<Mutex<TextureAtlas>>,
|
|
|
|
pixels_per_point: f32,
|
|
|
|
definitions: &super::FontDefinitions,
|
|
|
|
) -> Self {
|
2021-06-24 10:13:57 +00:00
|
|
|
let ab_glyph_fonts = definitions
|
2020-12-13 18:19:57 +00:00
|
|
|
.font_data
|
2020-12-12 18:30:01 +00:00
|
|
|
.iter()
|
2021-06-24 10:13:57 +00:00
|
|
|
.map(|(name, font_data)| (name.clone(), ab_glyph_font_from_font_data(name, font_data)))
|
2020-12-12 18:30:01 +00:00
|
|
|
.collect();
|
|
|
|
|
|
|
|
Self {
|
|
|
|
atlas,
|
2020-12-27 22:09:51 +00:00
|
|
|
pixels_per_point,
|
2021-06-24 10:13:57 +00:00
|
|
|
ab_glyph_fonts,
|
2020-12-12 18:30:01 +00:00
|
|
|
cache: Default::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-24 10:13:57 +00:00
|
|
|
pub fn ab_glyph_font(&self, font_name: &str) -> ab_glyph::FontArc {
|
|
|
|
self.ab_glyph_fonts
|
2020-12-13 18:19:57 +00:00
|
|
|
.get(font_name)
|
|
|
|
.unwrap_or_else(|| panic!("No font data found for {:?}", font_name))
|
|
|
|
.clone()
|
2020-12-12 18:30:01 +00:00
|
|
|
}
|
|
|
|
|
2020-12-13 18:19:57 +00:00
|
|
|
pub fn font_impl(&mut self, font_name: &str, scale_in_points: f32) -> Arc<FontImpl> {
|
2020-12-12 18:30:01 +00:00
|
|
|
for entry in &self.cache {
|
2020-12-13 18:19:57 +00:00
|
|
|
if (entry.0.as_str(), entry.1) == (font_name, scale_in_points) {
|
2020-12-12 18:30:01 +00:00
|
|
|
return entry.2.clone();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-10 09:20:50 +00:00
|
|
|
let y_offset = if font_name == "emoji-icon-font" {
|
2021-01-12 20:01:03 +00:00
|
|
|
scale_in_points * 0.235 // TODO: remove font alignment hack
|
2021-01-10 09:20:50 +00:00
|
|
|
} else {
|
2021-01-12 20:01:03 +00:00
|
|
|
0.0
|
2021-01-10 09:20:50 +00:00
|
|
|
};
|
2021-01-12 20:01:03 +00:00
|
|
|
let y_offset = y_offset - 3.0; // Tweaked to make text look centered in buttons and text edit fields
|
2021-01-10 09:20:50 +00:00
|
|
|
|
|
|
|
let scale_in_points = if font_name == "emoji-icon-font" {
|
2021-01-12 20:01:03 +00:00
|
|
|
scale_in_points * 0.8 // TODO: remove HACK!
|
2021-01-10 09:20:50 +00:00
|
|
|
} else {
|
|
|
|
scale_in_points
|
|
|
|
};
|
|
|
|
|
2020-12-12 18:30:01 +00:00
|
|
|
let font_impl = Arc::new(FontImpl::new(
|
|
|
|
self.atlas.clone(),
|
|
|
|
self.pixels_per_point,
|
2021-06-24 10:13:57 +00:00
|
|
|
self.ab_glyph_font(font_name),
|
2020-12-12 18:30:01 +00:00
|
|
|
scale_in_points,
|
2021-01-10 09:20:50 +00:00
|
|
|
y_offset,
|
2020-12-12 18:30:01 +00:00
|
|
|
));
|
|
|
|
self.cache
|
2020-12-13 18:19:57 +00:00
|
|
|
.push((font_name.to_owned(), scale_in_points, font_impl.clone()));
|
2020-12-12 18:30:01 +00:00
|
|
|
font_impl
|
|
|
|
}
|
|
|
|
}
|