Add fake italics (tilt text)
This commit is contained in:
parent
d249ed86ba
commit
c50190a7e8
6 changed files with 78 additions and 24 deletions
|
@ -12,9 +12,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
|
||||||
### Added ⭐
|
### Added ⭐
|
||||||
|
|
||||||
* Add support for secondary and middle mouse buttons.
|
* Add support for secondary and middle mouse buttons.
|
||||||
|
* Add `Label` methods for code, strong, strikethrough, underline and italics.
|
||||||
* `egui::popup::popup_below_widget`: show a popup area below another widget.
|
* `egui::popup::popup_below_widget`: show a popup area below another widget.
|
||||||
* Add `Slider::clamp_to_range(bool)`: if set, clamp the incoming and outgoing values to the slider range.
|
* Add `Slider::clamp_to_range(bool)`: if set, clamp the incoming and outgoing values to the slider range.
|
||||||
* Add `Label` methods for code, strong, strikethrough and underline.
|
|
||||||
|
|
||||||
### Changed 🔧
|
### Changed 🔧
|
||||||
|
|
||||||
|
|
|
@ -269,11 +269,23 @@ impl Painter {
|
||||||
|
|
||||||
/// Paint text that has already been layed out in a `Galley`.
|
/// Paint text that has already been layed out in a `Galley`.
|
||||||
pub fn galley(&self, pos: Pos2, galley: Galley, text_style: TextStyle, color: Color32) {
|
pub fn galley(&self, pos: Pos2, galley: Galley, text_style: TextStyle, color: Color32) {
|
||||||
|
self.galley_with_italics(pos, galley, text_style, color, false)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn galley_with_italics(
|
||||||
|
&self,
|
||||||
|
pos: Pos2,
|
||||||
|
galley: Galley,
|
||||||
|
text_style: TextStyle,
|
||||||
|
color: Color32,
|
||||||
|
fake_italics: bool,
|
||||||
|
) {
|
||||||
self.add(Shape::Text {
|
self.add(Shape::Text {
|
||||||
pos,
|
pos,
|
||||||
galley,
|
galley,
|
||||||
text_style,
|
text_style,
|
||||||
color,
|
color,
|
||||||
|
fake_italics,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,6 +13,7 @@ pub struct Label {
|
||||||
strong: bool,
|
strong: bool,
|
||||||
strikethrough: bool,
|
strikethrough: bool,
|
||||||
underline: bool,
|
underline: bool,
|
||||||
|
italics: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Label {
|
impl Label {
|
||||||
|
@ -27,6 +28,7 @@ impl Label {
|
||||||
strong: false,
|
strong: false,
|
||||||
strikethrough: false,
|
strikethrough: false,
|
||||||
underline: false,
|
underline: false,
|
||||||
|
italics: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -83,6 +85,12 @@ impl Label {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// tilt the characters to the right.
|
||||||
|
pub fn italics(mut self) -> Self {
|
||||||
|
self.italics = true;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
pub fn small(self) -> Self {
|
pub fn small(self) -> Self {
|
||||||
self.text_style(TextStyle::Small)
|
self.text_style(TextStyle::Small)
|
||||||
}
|
}
|
||||||
|
@ -133,6 +141,7 @@ impl Label {
|
||||||
strong,
|
strong,
|
||||||
strikethrough,
|
strikethrough,
|
||||||
underline,
|
underline,
|
||||||
|
italics,
|
||||||
..
|
..
|
||||||
} = *self;
|
} = *self;
|
||||||
|
|
||||||
|
@ -173,7 +182,8 @@ impl Label {
|
||||||
}
|
}
|
||||||
|
|
||||||
let text_style = self.text_style_or_default(ui.style());
|
let text_style = self.text_style_or_default(ui.style());
|
||||||
ui.painter().galley(pos, galley, text_style, text_color);
|
ui.painter()
|
||||||
|
.galley_with_italics(pos, galley, text_style, text_color, italics);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Read the text style, or get the default for the current style
|
/// Read the text style, or get the default for the current style
|
||||||
|
|
|
@ -114,37 +114,33 @@ impl Mesh {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Rectangle with a texture and color.
|
/// Rectangle with a texture and color.
|
||||||
pub fn add_rect_with_uv(&mut self, pos: Rect, uv: Rect, color: Color32) {
|
pub fn add_rect_with_uv(&mut self, rect: Rect, uv: Rect, color: Color32) {
|
||||||
#![allow(clippy::identity_op)]
|
#![allow(clippy::identity_op)]
|
||||||
|
|
||||||
let idx = self.vertices.len() as u32;
|
let idx = self.vertices.len() as u32;
|
||||||
self.add_triangle(idx + 0, idx + 1, idx + 2);
|
self.add_triangle(idx + 0, idx + 1, idx + 2);
|
||||||
self.add_triangle(idx + 2, idx + 1, idx + 3);
|
self.add_triangle(idx + 2, idx + 1, idx + 3);
|
||||||
|
|
||||||
let right_top = Vertex {
|
self.vertices.push(Vertex {
|
||||||
pos: pos.right_top(),
|
pos: rect.left_top(),
|
||||||
uv: uv.right_top(),
|
|
||||||
color,
|
|
||||||
};
|
|
||||||
let left_top = Vertex {
|
|
||||||
pos: pos.left_top(),
|
|
||||||
uv: uv.left_top(),
|
uv: uv.left_top(),
|
||||||
color,
|
color,
|
||||||
};
|
});
|
||||||
let left_bottom = Vertex {
|
self.vertices.push(Vertex {
|
||||||
pos: pos.left_bottom(),
|
pos: rect.right_top(),
|
||||||
|
uv: uv.right_top(),
|
||||||
|
color,
|
||||||
|
});
|
||||||
|
self.vertices.push(Vertex {
|
||||||
|
pos: rect.left_bottom(),
|
||||||
uv: uv.left_bottom(),
|
uv: uv.left_bottom(),
|
||||||
color,
|
color,
|
||||||
};
|
});
|
||||||
let right_bottom = Vertex {
|
self.vertices.push(Vertex {
|
||||||
pos: pos.right_bottom(),
|
pos: rect.right_bottom(),
|
||||||
uv: uv.right_bottom(),
|
uv: uv.right_bottom(),
|
||||||
color,
|
color,
|
||||||
};
|
});
|
||||||
self.vertices.push(left_top);
|
|
||||||
self.vertices.push(right_top);
|
|
||||||
self.vertices.push(left_bottom);
|
|
||||||
self.vertices.push(right_bottom);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Uniformly colored rectangle.
|
/// Uniformly colored rectangle.
|
||||||
|
|
|
@ -46,6 +46,8 @@ pub enum Shape {
|
||||||
galley: Galley,
|
galley: Galley,
|
||||||
text_style: TextStyle, // TODO: Font?
|
text_style: TextStyle, // TODO: Font?
|
||||||
color: Color32,
|
color: Color32,
|
||||||
|
/// If true, tilt the letters for an ugly italics effect
|
||||||
|
fake_italics: bool,
|
||||||
},
|
},
|
||||||
Mesh(Mesh),
|
Mesh(Mesh),
|
||||||
}
|
}
|
||||||
|
@ -138,6 +140,7 @@ impl Shape {
|
||||||
galley,
|
galley,
|
||||||
text_style,
|
text_style,
|
||||||
color,
|
color,
|
||||||
|
fake_italics: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -545,6 +545,7 @@ impl Tessellator {
|
||||||
galley,
|
galley,
|
||||||
text_style,
|
text_style,
|
||||||
color,
|
color,
|
||||||
|
fake_italics,
|
||||||
} => {
|
} => {
|
||||||
if options.debug_paint_text_rects {
|
if options.debug_paint_text_rects {
|
||||||
self.tessellate_rect(
|
self.tessellate_rect(
|
||||||
|
@ -557,7 +558,7 @@ impl Tessellator {
|
||||||
out,
|
out,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
self.tessellate_text(fonts, pos, &galley, text_style, color, out);
|
self.tessellate_text(fonts, pos, &galley, text_style, color, fake_italics, out);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -592,6 +593,7 @@ impl Tessellator {
|
||||||
stroke_path(&path.0, Closed, stroke, self.options, out);
|
stroke_path(&path.0, Closed, stroke, self.options, out);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(clippy::too_many_arguments)]
|
||||||
pub fn tessellate_text(
|
pub fn tessellate_text(
|
||||||
&mut self,
|
&mut self,
|
||||||
fonts: &Fonts,
|
fonts: &Fonts,
|
||||||
|
@ -599,6 +601,7 @@ impl Tessellator {
|
||||||
galley: &super::Galley,
|
galley: &super::Galley,
|
||||||
text_style: super::TextStyle,
|
text_style: super::TextStyle,
|
||||||
color: Color32,
|
color: Color32,
|
||||||
|
fake_italics: bool,
|
||||||
out: &mut Mesh,
|
out: &mut Mesh,
|
||||||
) {
|
) {
|
||||||
if color == Color32::TRANSPARENT {
|
if color == Color32::TRANSPARENT {
|
||||||
|
@ -636,12 +639,42 @@ impl Tessellator {
|
||||||
left_top.x = font.round_to_pixel(left_top.x); // Pixel-perfection.
|
left_top.x = font.round_to_pixel(left_top.x); // Pixel-perfection.
|
||||||
left_top.y = font.round_to_pixel(left_top.y); // Pixel-perfection.
|
left_top.y = font.round_to_pixel(left_top.y); // Pixel-perfection.
|
||||||
|
|
||||||
let pos = Rect::from_min_max(left_top, left_top + glyph.size);
|
let rect = Rect::from_min_max(left_top, left_top + glyph.size);
|
||||||
let uv = Rect::from_min_max(
|
let uv = Rect::from_min_max(
|
||||||
pos2(glyph.min.0 as f32 / tex_w, glyph.min.1 as f32 / tex_h),
|
pos2(glyph.min.0 as f32 / tex_w, glyph.min.1 as f32 / tex_h),
|
||||||
pos2(glyph.max.0 as f32 / tex_w, glyph.max.1 as f32 / tex_h),
|
pos2(glyph.max.0 as f32 / tex_w, glyph.max.1 as f32 / tex_h),
|
||||||
);
|
);
|
||||||
out.add_rect_with_uv(pos, uv, color);
|
|
||||||
|
if fake_italics {
|
||||||
|
let idx = out.vertices.len() as u32;
|
||||||
|
out.add_triangle(idx, idx + 1, idx + 2);
|
||||||
|
out.add_triangle(idx + 2, idx + 1, idx + 3);
|
||||||
|
|
||||||
|
let top_offset = rect.height() * 0.25 * Vec2::X;
|
||||||
|
|
||||||
|
out.vertices.push(Vertex {
|
||||||
|
pos: rect.left_top() + top_offset,
|
||||||
|
uv: uv.left_top(),
|
||||||
|
color,
|
||||||
|
});
|
||||||
|
out.vertices.push(Vertex {
|
||||||
|
pos: rect.right_top() + top_offset,
|
||||||
|
uv: uv.right_top(),
|
||||||
|
color,
|
||||||
|
});
|
||||||
|
out.vertices.push(Vertex {
|
||||||
|
pos: rect.left_bottom(),
|
||||||
|
uv: uv.left_bottom(),
|
||||||
|
color,
|
||||||
|
});
|
||||||
|
out.vertices.push(Vertex {
|
||||||
|
pos: rect.right_bottom(),
|
||||||
|
uv: uv.right_bottom(),
|
||||||
|
color,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
out.add_rect_with_uv(rect, uv, color);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if line.ends_with_newline {
|
if line.ends_with_newline {
|
||||||
|
|
Loading…
Reference in a new issue