1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-31 05:40:03 -04: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.
This commit is contained in:
Emil Ernerfeldt
2021-09-03 18:18:00 +02:00
committed by GitHub
parent 36cffd7b84
commit de1a1ba9b2
43 changed files with 2204 additions and 1295 deletions

View File

@@ -56,6 +56,7 @@ impl Rect {
Rect { min, max }
}
#[inline(always)]
pub fn from_min_size(min: Pos2, size: Vec2) -> Self {
Rect {
min,
@@ -63,6 +64,7 @@ impl Rect {
}
}
#[inline(always)]
pub fn from_center_size(center: Pos2, size: Vec2) -> Self {
Rect {
min: center - size * 0.5,
@@ -70,6 +72,7 @@ impl Rect {
}
}
#[inline(always)]
pub fn from_x_y_ranges(x_range: RangeInclusive<f32>, y_range: RangeInclusive<f32>) -> Self {
Rect {
min: pos2(*x_range.start(), *y_range.start()),
@@ -77,6 +80,7 @@ impl Rect {
}
}
#[inline]
pub fn from_two_pos(a: Pos2, b: Pos2) -> Self {
Rect {
min: pos2(a.x.min(b.x), a.y.min(b.y)),
@@ -85,6 +89,7 @@ impl Rect {
}
/// A `Rect` that contains every point to the right of the given X coordinate.
#[inline]
pub fn everything_right_of(left_x: f32) -> Self {
let mut rect = Self::EVERYTHING;
rect.set_left(left_x);
@@ -92,6 +97,7 @@ impl Rect {
}
/// A `Rect` that contains every point to the left of the given X coordinate.
#[inline]
pub fn everything_left_of(right_x: f32) -> Self {
let mut rect = Self::EVERYTHING;
rect.set_right(right_x);
@@ -99,6 +105,7 @@ impl Rect {
}
/// A `Rect` that contains every point below a certain y coordinate
#[inline]
pub fn everything_below(top_y: f32) -> Self {
let mut rect = Self::EVERYTHING;
rect.set_top(top_y);
@@ -106,6 +113,7 @@ impl Rect {
}
/// A `Rect` that contains every point above a certain y coordinate
#[inline]
pub fn everything_above(bottom_y: f32) -> Self {
let mut rect = Self::EVERYTHING;
rect.set_bottom(bottom_y);
@@ -137,6 +145,7 @@ impl Rect {
}
#[must_use]
#[inline]
pub fn translate(self, amnt: Vec2) -> Self {
Rect::from_min_size(self.min + amnt, self.size())
}
@@ -151,6 +160,7 @@ impl Rect {
}
#[must_use]
#[inline]
pub fn intersects(self, other: Rect) -> bool {
self.min.x <= other.max.x
&& other.min.x <= self.max.x
@@ -191,23 +201,27 @@ impl Rect {
p.clamp(self.min, self.max)
}
#[inline(always)]
pub fn extend_with(&mut self, p: Pos2) {
self.min = self.min.min(p);
self.max = self.max.max(p);
}
#[inline(always)]
/// Expand to include the given x coordinate
pub fn extend_with_x(&mut self, x: f32) {
self.min.x = self.min.x.min(x);
self.max.x = self.max.x.max(x);
}
#[inline(always)]
/// Expand to include the given y coordinate
pub fn extend_with_y(&mut self, y: f32) {
self.min.y = self.min.y.min(y);
self.max.y = self.max.y.max(y);
}
#[inline(always)]
pub fn union(self, other: Rect) -> Rect {
Rect {
min: self.min.min(other.min),
@@ -260,16 +274,22 @@ impl Rect {
}
}
#[inline(always)]
pub fn area(&self) -> f32 {
self.width() * self.height()
}
#[inline(always)]
pub fn x_range(&self) -> RangeInclusive<f32> {
self.min.x..=self.max.x
}
#[inline(always)]
pub fn y_range(&self) -> RangeInclusive<f32> {
self.min.y..=self.max.y
}
#[inline(always)]
pub fn bottom_up_range(&self) -> RangeInclusive<f32> {
self.max.y..=self.min.y
}