2022-01-10 22:13:10 +00:00
|
|
|
use std::collections::BTreeMap;
|
2019-01-12 23:55:56 +00:00
|
|
|
|
2021-01-10 14:42:46 +00:00
|
|
|
use crate::{
|
2022-01-24 13:32:36 +00:00
|
|
|
mutex::{Arc, Mutex, MutexGuard},
|
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
|
|
|
},
|
2022-01-22 10:23:12 +00:00
|
|
|
TextureAtlas,
|
2021-01-10 14:42:46 +00:00
|
|
|
};
|
2022-01-24 13:32:36 +00:00
|
|
|
use emath::NumExt as _;
|
2019-01-12 23:55:56 +00:00
|
|
|
|
2022-01-24 13:32:36 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/// How to select a sized font.
|
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
2021-09-29 06:45:13 +00:00
|
|
|
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
|
2022-01-24 13:32:36 +00:00
|
|
|
pub struct FontId {
|
|
|
|
/// Height in points.
|
|
|
|
pub size: f32,
|
|
|
|
|
|
|
|
/// What font family to use.
|
|
|
|
pub family: FontFamily,
|
|
|
|
// TODO: weight (bold), italics, …
|
2019-01-12 23:55:56 +00:00
|
|
|
}
|
|
|
|
|
2022-01-24 13:32:36 +00:00
|
|
|
impl Default for FontId {
|
|
|
|
#[inline]
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
size: 14.0,
|
|
|
|
family: FontFamily::Proportional,
|
|
|
|
}
|
2020-12-12 18:30:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-24 13:32:36 +00:00
|
|
|
impl FontId {
|
|
|
|
#[inline]
|
|
|
|
pub fn new(size: f32, family: FontFamily) -> Self {
|
|
|
|
Self { size, family }
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn proportional(size: f32) -> Self {
|
|
|
|
Self::new(size, FontFamily::Proportional)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn monospace(size: f32) -> Self {
|
|
|
|
Self::new(size, FontFamily::Monospace)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(clippy::derive_hash_xor_eq)]
|
|
|
|
impl std::hash::Hash for FontId {
|
|
|
|
#[inline(always)]
|
|
|
|
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
|
|
|
|
let Self { size, family } = self;
|
|
|
|
crate::f32_hash(state, *size);
|
|
|
|
family.hash(state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/// Font of unknown size.
|
|
|
|
///
|
|
|
|
/// Which style of font: [`Monospace`][`FontFamily::Monospace`], [`Proportional`][`FontFamily::Proportional`],
|
|
|
|
/// or by user-chosen name.
|
|
|
|
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
|
2021-09-29 06:45:13 +00:00
|
|
|
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
|
2019-12-02 19:08:49 +00:00
|
|
|
pub enum FontFamily {
|
2020-12-27 13:16:37 +00:00
|
|
|
/// A font where some characters are wider than other (e.g. 'w' is wider than 'i').
|
2022-01-24 13:32:36 +00:00
|
|
|
///
|
|
|
|
/// Proportional fonts are easier to read and should be the preferred choice in most situations.
|
2020-12-27 13:19:20 +00:00
|
|
|
Proportional,
|
2022-01-24 13:32:36 +00:00
|
|
|
|
|
|
|
/// A font where each character is the same width (`w` is the same width as `i`).
|
|
|
|
///
|
|
|
|
/// Useful for code snippets, or when you need to align numbers or text.
|
|
|
|
Monospace,
|
|
|
|
|
|
|
|
/// One of the names in [`FontDefinitions::families`].
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// # use epaint::FontFamily;
|
|
|
|
/// // User-chosen names:
|
|
|
|
/// FontFamily::Name("arial".into());
|
|
|
|
/// FontFamily::Name("serif".into());
|
|
|
|
/// ```
|
|
|
|
Name(Arc<str>),
|
2019-12-02 19:08:49 +00:00
|
|
|
}
|
|
|
|
|
2022-01-24 13:32:36 +00:00
|
|
|
impl Default for FontFamily {
|
|
|
|
#[inline]
|
|
|
|
fn default() -> Self {
|
|
|
|
FontFamily::Proportional
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::fmt::Display for FontFamily {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
match self {
|
|
|
|
Self::Monospace => "Monospace".fmt(f),
|
|
|
|
Self::Proportional => "Proportional".fmt(f),
|
|
|
|
Self::Name(name) => (*name).fmt(f),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2021-11-07 18:47:52 +00:00
|
|
|
/// A `.ttf` or `.otf` file and a font face index.
|
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
|
|
|
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
|
|
|
|
pub struct FontData {
|
2021-12-29 20:44:48 +00:00
|
|
|
/// The content of a `.ttf` or `.otf` file.
|
2021-11-07 18:47:52 +00:00
|
|
|
pub font: std::borrow::Cow<'static, [u8]>,
|
2022-01-24 13:32:36 +00:00
|
|
|
|
2021-11-07 18:47:52 +00:00
|
|
|
/// Which font face in the file to use.
|
|
|
|
/// When in doubt, use `0`.
|
|
|
|
pub index: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FontData {
|
|
|
|
pub fn from_static(font: &'static [u8]) -> Self {
|
|
|
|
Self {
|
|
|
|
font: std::borrow::Cow::Borrowed(font),
|
|
|
|
index: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn from_owned(font: Vec<u8>) -> Self {
|
|
|
|
Self {
|
|
|
|
font: std::borrow::Cow::Owned(font),
|
|
|
|
index: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-12-13 20:04:02 +00:00
|
|
|
|
2021-06-24 10:13:57 +00:00
|
|
|
fn ab_glyph_font_from_font_data(name: &str, data: &FontData) -> ab_glyph::FontArc {
|
2021-11-07 18:47:52 +00:00
|
|
|
match &data.font {
|
|
|
|
std::borrow::Cow::Borrowed(bytes) => {
|
|
|
|
ab_glyph::FontRef::try_from_slice_and_index(bytes, data.index)
|
|
|
|
.map(ab_glyph::FontArc::from)
|
|
|
|
}
|
|
|
|
std::borrow::Cow::Owned(bytes) => {
|
|
|
|
ab_glyph::FontVec::try_from_vec_and_index(bytes.clone(), data.index)
|
|
|
|
.map(ab_glyph::FontArc::from)
|
|
|
|
}
|
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.
|
|
|
|
///
|
2022-01-24 13:32:36 +00:00
|
|
|
/// This is how you install your own custom fonts:
|
2021-01-17 09:52:01 +00:00
|
|
|
/// ```
|
2022-01-24 13:32:36 +00:00
|
|
|
/// # use {epaint::text::{FontDefinitions, FontFamily, FontData}};
|
2021-05-11 12:56:27 +00:00
|
|
|
/// # struct FakeEguiCtx {};
|
|
|
|
/// # impl FakeEguiCtx { fn set_fonts(&self, _: FontDefinitions) {} }
|
2022-01-24 13:32:36 +00:00
|
|
|
/// # let egui_ctx = FakeEguiCtx {};
|
2021-05-11 12:56:27 +00:00
|
|
|
/// let mut fonts = FontDefinitions::default();
|
|
|
|
///
|
|
|
|
/// // Install my own font (maybe supporting non-latin characters):
|
|
|
|
/// fonts.font_data.insert("my_font".to_owned(),
|
2021-11-07 18:47:52 +00:00
|
|
|
/// FontData::from_static(include_bytes!("../../fonts/Ubuntu-Light.ttf"))); // .ttf and .otf supported
|
2021-05-11 12:56:27 +00:00
|
|
|
///
|
|
|
|
/// // Put my font first (highest priority):
|
2022-01-24 13:32:36 +00:00
|
|
|
/// fonts.families.get_mut(&FontFamily::Proportional).unwrap()
|
2021-05-11 12:56:27 +00:00
|
|
|
/// .insert(0, "my_font".to_owned());
|
|
|
|
///
|
|
|
|
/// // Put my font as last fallback for monospace:
|
2022-01-24 13:32:36 +00:00
|
|
|
/// fonts.families.get_mut(&FontFamily::Monospace).unwrap()
|
2021-05-11 12:56:27 +00:00
|
|
|
/// .push("my_font".to_owned());
|
|
|
|
///
|
2022-01-24 13:32:36 +00:00
|
|
|
/// egui_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.
|
|
|
|
///
|
2021-11-07 18:47:52 +00:00
|
|
|
/// `epaint` has built-in-default for these, 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.
|
2022-01-24 13:32:36 +00:00
|
|
|
// TODO: per font size-modifier.
|
|
|
|
pub families: BTreeMap<FontFamily, Vec<String>>,
|
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
|
|
|
|
2022-01-24 13:32:36 +00:00
|
|
|
let mut families = BTreeMap::new();
|
2020-12-26 20:20:55 +00:00
|
|
|
|
|
|
|
#[cfg(feature = "default_fonts")]
|
|
|
|
{
|
|
|
|
font_data.insert(
|
2021-10-17 18:49:28 +00:00
|
|
|
"Hack".to_owned(),
|
2021-11-07 18:47:52 +00:00
|
|
|
FontData::from_static(include_bytes!("../../fonts/Hack-Regular.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-11-07 18:47:52 +00:00
|
|
|
FontData::from_static(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-11-07 18:47:52 +00:00
|
|
|
FontData::from_static(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-11-07 18:47:52 +00:00
|
|
|
FontData::from_static(include_bytes!("../../fonts/emoji-icon-font.ttf")),
|
2020-12-26 20:20:55 +00:00
|
|
|
);
|
|
|
|
|
2022-01-24 13:32:36 +00:00
|
|
|
families.insert(
|
2020-12-26 20:20:55 +00:00
|
|
|
FontFamily::Monospace,
|
|
|
|
vec![
|
2021-10-17 18:49:28 +00:00
|
|
|
"Hack".to_owned(),
|
2020-12-26 20:20:55 +00:00
|
|
|
"Ubuntu-Light".to_owned(), // fallback for √ etc
|
|
|
|
"NotoEmoji-Regular".to_owned(),
|
|
|
|
"emoji-icon-font".to_owned(),
|
|
|
|
],
|
|
|
|
);
|
2022-01-24 13:32:36 +00:00
|
|
|
families.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"))]
|
|
|
|
{
|
2022-01-24 13:32:36 +00:00
|
|
|
families.insert(FontFamily::Monospace, vec![]);
|
|
|
|
families.insert(FontFamily::Proportional, vec![]);
|
2020-12-26 20:20:55 +00:00
|
|
|
}
|
2020-12-13 18:19:57 +00:00
|
|
|
|
2020-05-30 12:56:38 +00:00
|
|
|
Self {
|
2020-12-13 18:19:57 +00:00
|
|
|
font_data,
|
2022-01-24 13:32:36 +00:00
|
|
|
families,
|
2020-05-30 12:56:38 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-12 10:07:51 +00:00
|
|
|
}
|
|
|
|
|
2022-01-24 13:32:36 +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.
|
2022-01-24 13:32:36 +00:00
|
|
|
/// Create one and reuse. Cheap to clone.
|
|
|
|
///
|
|
|
|
/// You need to call [`Self::begin_frame`] and [`Self::font_image_delta`] once every frame.
|
|
|
|
///
|
|
|
|
/// Wrapper for `Arc<Mutex<FontsAndCache>>`.
|
|
|
|
pub struct Fonts(Arc<Mutex<FontsAndCache>>);
|
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.
|
2022-01-22 07:56:36 +00:00
|
|
|
/// This call is expensive, so only create one [`Fonts`] and then reuse it.
|
2022-01-24 13:32:36 +00:00
|
|
|
///
|
|
|
|
/// * `pixels_per_point`: how many physical pixels per logical "point".
|
|
|
|
/// * `max_texture_side`: largest supported texture size (one side).
|
|
|
|
pub fn new(
|
|
|
|
pixels_per_point: f32,
|
|
|
|
max_texture_side: usize,
|
|
|
|
definitions: FontDefinitions,
|
|
|
|
) -> Self {
|
|
|
|
let fonts_and_cache = FontsAndCache {
|
|
|
|
fonts: FontsImpl::new(pixels_per_point, max_texture_side, definitions),
|
2021-03-29 20:06:55 +00:00
|
|
|
galley_cache: Default::default(),
|
2022-01-24 13:32:36 +00:00
|
|
|
};
|
|
|
|
Self(Arc::new(Mutex::new(fonts_and_cache)))
|
2019-01-12 23:55:56 +00:00
|
|
|
}
|
|
|
|
|
2022-01-24 13:32:36 +00:00
|
|
|
/// Call at the start of each frame with the latest known
|
|
|
|
/// `pixels_per_point` and `max_texture_side`.
|
|
|
|
///
|
|
|
|
/// Call after painting the previous frame, but before using [`Fonts`] for the new frame.
|
|
|
|
///
|
|
|
|
/// This function will react to changes in `pixels_per_point` and `max_texture_side`,
|
|
|
|
/// as well as notice when the font atlas is getting full, and handle that.
|
|
|
|
pub fn begin_frame(&self, pixels_per_point: f32, max_texture_side: usize) {
|
|
|
|
let mut fonts_and_cache = self.0.lock();
|
|
|
|
|
|
|
|
let pixels_per_point_changed =
|
|
|
|
(fonts_and_cache.fonts.pixels_per_point - pixels_per_point).abs() > 1e-3;
|
|
|
|
let max_texture_side_changed = fonts_and_cache.fonts.max_texture_side != max_texture_side;
|
|
|
|
let font_atlas_almost_full = fonts_and_cache.fonts.atlas.lock().fill_ratio() > 0.8;
|
|
|
|
let needs_recreate =
|
|
|
|
pixels_per_point_changed || max_texture_side_changed || font_atlas_almost_full;
|
|
|
|
|
|
|
|
if needs_recreate {
|
|
|
|
let definitions = fonts_and_cache.fonts.definitions.clone();
|
|
|
|
|
|
|
|
*fonts_and_cache = FontsAndCache {
|
|
|
|
fonts: FontsImpl::new(pixels_per_point, max_texture_side, definitions),
|
|
|
|
galley_cache: Default::default(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
fonts_and_cache.galley_cache.flush_cache();
|
2020-12-27 22:09:51 +00:00
|
|
|
}
|
|
|
|
|
2022-01-24 13:32:36 +00:00
|
|
|
/// Call at the end of each frame (before painting) to get the change to the font texture since last call.
|
|
|
|
pub fn font_image_delta(&self) -> Option<crate::ImageDelta> {
|
|
|
|
self.lock().fonts.atlas.lock().take_delta()
|
2020-12-27 22:09:51 +00:00
|
|
|
}
|
|
|
|
|
2022-01-24 13:32:36 +00:00
|
|
|
/// Access the underlying [`FontsAndCache`].
|
|
|
|
#[doc(hidden)]
|
|
|
|
#[inline]
|
|
|
|
pub fn lock(&self) -> MutexGuard<'_, FontsAndCache> {
|
|
|
|
self.0.lock()
|
2021-03-30 19:07:19 +00:00
|
|
|
}
|
|
|
|
|
2022-01-24 13:32:36 +00:00
|
|
|
#[inline]
|
|
|
|
pub fn pixels_per_point(&self) -> f32 {
|
|
|
|
self.lock().fonts.pixels_per_point
|
2021-09-07 18:37:50 +00:00
|
|
|
}
|
|
|
|
|
2022-01-24 13:32:36 +00:00
|
|
|
#[inline]
|
|
|
|
pub fn max_texture_side(&self) -> usize {
|
|
|
|
self.lock().fonts.max_texture_side
|
2022-01-22 10:23:12 +00:00
|
|
|
}
|
2020-09-09 12:24:13 +00:00
|
|
|
|
2022-01-24 13:32:36 +00:00
|
|
|
/// Current size of the font image.
|
|
|
|
/// Pass this to [`crate::Tessellator`].
|
2022-01-22 10:23:12 +00:00
|
|
|
pub fn font_image_size(&self) -> [usize; 2] {
|
2022-01-24 13:32:36 +00:00
|
|
|
self.lock().fonts.atlas.lock().size()
|
2019-01-12 23:55:56 +00:00
|
|
|
}
|
2021-03-29 18:57:51 +00:00
|
|
|
|
|
|
|
/// Width of this character in points.
|
2022-01-24 13:32:36 +00:00
|
|
|
#[inline]
|
|
|
|
pub fn glyph_width(&self, font_id: &FontId, c: char) -> f32 {
|
|
|
|
self.lock().fonts.glyph_width(font_id, c)
|
2021-03-29 18:57:51 +00:00
|
|
|
}
|
|
|
|
|
2022-01-24 13:32:36 +00:00
|
|
|
/// Height of one row of text in points
|
|
|
|
#[inline]
|
|
|
|
pub fn row_height(&self, font_id: &FontId) -> f32 {
|
|
|
|
self.lock().fonts.row_height(font_id)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// List of all known font families.
|
|
|
|
pub fn families(&self) -> Vec<FontFamily> {
|
|
|
|
self.lock()
|
|
|
|
.fonts
|
|
|
|
.definitions
|
|
|
|
.families
|
|
|
|
.keys()
|
|
|
|
.cloned()
|
|
|
|
.collect()
|
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
|
|
|
/// Layout some text.
|
2022-01-24 13:32:36 +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
|
|
|
/// 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.
|
2022-01-24 13:32:36 +00:00
|
|
|
#[inline]
|
2021-09-04 08:19:58 +00:00
|
|
|
pub fn layout_job(&self, job: LayoutJob) -> Arc<Galley> {
|
2022-01-24 13:32:36 +00:00
|
|
|
self.lock().layout_job(job)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn num_galleys_in_cache(&self) -> usize {
|
|
|
|
self.lock().galley_cache.num_galleys_in_cache()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// How full is the font atlas?
|
|
|
|
///
|
|
|
|
/// This increases as new fonts and/or glyphs are used,
|
|
|
|
/// but can also decrease in a call to [`Self::begin_frame`].
|
|
|
|
pub fn font_atlas_fill_ratio(&self) -> f32 {
|
|
|
|
self.lock().fonts.atlas.lock().fill_ratio()
|
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,
|
2022-01-24 13:32:36 +00:00
|
|
|
font_id: FontId,
|
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
|
|
|
color: crate::Color32,
|
|
|
|
wrap_width: f32,
|
|
|
|
) -> Arc<Galley> {
|
2022-01-24 13:32:36 +00:00
|
|
|
let job = LayoutJob::simple(text, font_id, color, wrap_width);
|
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(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,
|
2022-01-24 13:32:36 +00:00
|
|
|
font_id: FontId,
|
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
|
|
|
color: crate::Color32,
|
2021-03-29 20:30:18 +00:00
|
|
|
) -> Arc<Galley> {
|
2022-01-24 13:32:36 +00:00
|
|
|
let job = LayoutJob::simple(text, font_id, color, f32::INFINITY);
|
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(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,
|
2022-01-24 13:32:36 +00:00
|
|
|
font_id: FontId,
|
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
|
|
|
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,
|
2022-01-24 13:32:36 +00:00
|
|
|
font_id,
|
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
|
|
|
crate::Color32::TEMPORARY_COLOR,
|
|
|
|
wrap_width,
|
|
|
|
))
|
2021-03-29 18:57:51 +00:00
|
|
|
}
|
2022-01-24 13:32:36 +00:00
|
|
|
}
|
2021-03-29 20:06:55 +00:00
|
|
|
|
2022-01-24 13:32:36 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
2021-03-29 20:06:55 +00:00
|
|
|
|
2022-01-24 13:32:36 +00:00
|
|
|
pub struct FontsAndCache {
|
|
|
|
pub fonts: FontsImpl,
|
|
|
|
galley_cache: GalleyCache,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FontsAndCache {
|
|
|
|
fn layout_job(&mut self, job: LayoutJob) -> Arc<Galley> {
|
|
|
|
self.galley_cache.layout(&mut self.fonts, job)
|
2021-03-29 20:06:55 +00:00
|
|
|
}
|
2019-01-12 23:55:56 +00:00
|
|
|
}
|
|
|
|
|
2022-01-24 13:32:36 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/// The collection of fonts used by `epaint`.
|
|
|
|
///
|
|
|
|
/// Required in order to paint text.
|
|
|
|
pub struct FontsImpl {
|
|
|
|
pixels_per_point: f32,
|
|
|
|
max_texture_side: usize,
|
|
|
|
definitions: FontDefinitions,
|
|
|
|
atlas: Arc<Mutex<TextureAtlas>>,
|
|
|
|
font_impl_cache: FontImplCache,
|
|
|
|
sized_family: ahash::AHashMap<(u32, FontFamily), Font>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FontsImpl {
|
|
|
|
/// Create a new [`FontsImpl`] for text layout.
|
|
|
|
/// This call is expensive, so only create one [`FontsImpl`] and then reuse it.
|
|
|
|
pub fn new(
|
|
|
|
pixels_per_point: f32,
|
|
|
|
max_texture_side: usize,
|
|
|
|
definitions: FontDefinitions,
|
|
|
|
) -> Self {
|
|
|
|
assert!(
|
|
|
|
0.0 < pixels_per_point && pixels_per_point < 100.0,
|
|
|
|
"pixels_per_point out of range: {}",
|
|
|
|
pixels_per_point
|
|
|
|
);
|
|
|
|
|
2022-01-25 00:04:28 +00:00
|
|
|
let texture_width = max_texture_side.at_most(8 * 1024);
|
|
|
|
let initial_height = 64;
|
2022-01-24 13:32:36 +00:00
|
|
|
let mut atlas = TextureAtlas::new([texture_width, initial_height]);
|
|
|
|
|
|
|
|
{
|
|
|
|
// Make the top left pixel fully white:
|
|
|
|
let (pos, image) = atlas.allocate((1, 1));
|
|
|
|
assert_eq!(pos, (0, 0));
|
|
|
|
image[pos] = 255;
|
|
|
|
}
|
|
|
|
|
|
|
|
let atlas = Arc::new(Mutex::new(atlas));
|
|
|
|
|
|
|
|
let font_impl_cache =
|
|
|
|
FontImplCache::new(atlas.clone(), pixels_per_point, &definitions.font_data);
|
|
|
|
|
|
|
|
Self {
|
|
|
|
pixels_per_point,
|
|
|
|
max_texture_side,
|
|
|
|
definitions,
|
|
|
|
atlas,
|
|
|
|
font_impl_cache,
|
|
|
|
sized_family: Default::default(),
|
|
|
|
}
|
|
|
|
}
|
2019-01-12 23:55:56 +00:00
|
|
|
|
2021-04-01 20:10:34 +00:00
|
|
|
#[inline(always)]
|
2022-01-24 13:32:36 +00:00
|
|
|
pub fn pixels_per_point(&self) -> f32 {
|
|
|
|
self.pixels_per_point
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn definitions(&self) -> &FontDefinitions {
|
|
|
|
&self.definitions
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the right font implementation from size and [`FontFamily`].
|
|
|
|
pub fn font(&mut self, font_id: &FontId) -> &mut Font {
|
|
|
|
let FontId { size, family } = font_id;
|
|
|
|
let scale_in_pixels = self.font_impl_cache.scale_as_pixels(*size);
|
|
|
|
|
|
|
|
self.sized_family
|
|
|
|
.entry((scale_in_pixels, family.clone()))
|
|
|
|
.or_insert_with(|| {
|
|
|
|
let fonts = &self.definitions.families.get(family);
|
|
|
|
let fonts = fonts.unwrap_or_else(|| {
|
|
|
|
panic!("FontFamily::{:?} is not bound to any fonts", family)
|
|
|
|
});
|
|
|
|
|
|
|
|
let fonts: Vec<Arc<FontImpl>> = fonts
|
|
|
|
.iter()
|
|
|
|
.map(|font_name| self.font_impl_cache.font_impl(scale_in_pixels, font_name))
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
Font::new(fonts)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Width of this character in points.
|
|
|
|
fn glyph_width(&mut self, font_id: &FontId, c: char) -> f32 {
|
|
|
|
self.font(font_id).glyph_width(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Height of one row of text. In points
|
|
|
|
fn row_height(&mut self, font_id: &FontId) -> f32 {
|
|
|
|
self.font(font_id).row_height()
|
2019-01-12 23:55:56 +00:00
|
|
|
}
|
|
|
|
}
|
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 {
|
2022-01-24 13:32:36 +00:00
|
|
|
fn layout(&mut self, fonts: &mut FontsImpl, 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.
|
2022-01-24 13:32:36 +00:00
|
|
|
pub fn flush_cache(&mut self) {
|
2021-03-29 20:06:55 +00:00
|
|
|
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
|
|
|
|
2022-01-24 13:32:36 +00:00
|
|
|
/// Map font pixel sizes and names to the cached `FontImpl`.
|
|
|
|
cache: ahash::AHashMap<(u32, String), 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,
|
2022-01-24 13:32:36 +00:00
|
|
|
font_data: &BTreeMap<String, FontData>,
|
2020-12-27 22:09:51 +00:00
|
|
|
) -> Self {
|
2022-01-24 13:32:36 +00:00
|
|
|
let ab_glyph_fonts = 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(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-24 13:32:36 +00:00
|
|
|
#[inline]
|
|
|
|
pub fn scale_as_pixels(&self, scale_in_points: f32) -> u32 {
|
|
|
|
let scale_in_pixels = self.pixels_per_point * scale_in_points;
|
|
|
|
|
|
|
|
// Round to an even number of physical pixels to get even kerning.
|
|
|
|
// See https://github.com/emilk/egui/issues/382
|
|
|
|
scale_in_pixels.round() as u32
|
2020-12-12 18:30:01 +00:00
|
|
|
}
|
|
|
|
|
2022-01-24 13:32:36 +00:00
|
|
|
pub fn font_impl(&mut self, scale_in_pixels: u32, font_name: &str) -> Arc<FontImpl> {
|
|
|
|
let scale_in_pixels = if font_name == "emoji-icon-font" {
|
|
|
|
(scale_in_pixels as f32 * 0.8).round() as u32 // TODO: remove font scale HACK!
|
|
|
|
} else {
|
|
|
|
scale_in_pixels
|
|
|
|
};
|
2020-12-12 18:30:01 +00:00
|
|
|
|
2021-01-10 09:20:50 +00:00
|
|
|
let y_offset = if font_name == "emoji-icon-font" {
|
2022-01-24 13:32:36 +00:00
|
|
|
let scale_in_points = scale_in_pixels as f32 / self.pixels_per_point;
|
|
|
|
scale_in_points * 0.29375 // 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
|
|
|
|
2020-12-12 18:30:01 +00:00
|
|
|
self.cache
|
2022-01-24 13:32:36 +00:00
|
|
|
.entry((scale_in_pixels, font_name.to_owned()))
|
|
|
|
.or_insert_with(|| {
|
|
|
|
let ab_glyph_font = self
|
|
|
|
.ab_glyph_fonts
|
|
|
|
.get(font_name)
|
|
|
|
.unwrap_or_else(|| panic!("No font data found for {:?}", font_name))
|
|
|
|
.clone();
|
|
|
|
|
|
|
|
Arc::new(FontImpl::new(
|
|
|
|
self.atlas.clone(),
|
|
|
|
self.pixels_per_point,
|
|
|
|
ab_glyph_font,
|
|
|
|
scale_in_pixels,
|
|
|
|
y_offset,
|
|
|
|
))
|
|
|
|
})
|
|
|
|
.clone()
|
2020-12-12 18:30:01 +00:00
|
|
|
}
|
|
|
|
}
|