Handle text overflow

Cull glyphs fully outside the bounds, and clip glyphs intersecting the
bounds. This is all done on the CPU for now.

Fixes #2
This commit is contained in:
grovesNL 2022-09-13 16:35:20 -02:30 committed by Josh Groves
parent 4e9e10061f
commit afab881559
3 changed files with 110 additions and 14 deletions

View file

@ -1,9 +1,10 @@
use fontdue::layout::{HorizontalAlign, VerticalAlign};
use glyphon::{
fontdue::{
layout::{CoordinateSystem, Layout, LayoutSettings, TextStyle},
Font, FontSettings,
},
Color, HasColor, Resolution, TextAtlas, TextRenderer,
Color, HasColor, Resolution, TextAtlas, TextOverflow, TextRenderer,
};
use wgpu::{
Backends, CommandEncoderDescriptor, DeviceDescriptor, Features, Instance, Limits, LoadOp,
@ -88,15 +89,15 @@ async fn run() {
window.request_redraw();
}
Event::RedrawRequested(_) => {
let mut layout = Layout::new(CoordinateSystem::PositiveYDown);
let mut layout1 = Layout::new(CoordinateSystem::PositiveYDown);
layout.reset(&LayoutSettings {
layout1.reset(&LayoutSettings {
x: 0.0,
y: 0.0,
..LayoutSettings::default()
});
layout.append(
layout1.append(
fonts.as_slice(),
&TextStyle::with_user_data(
"Hello world!\nI'm on a new line!",
@ -106,6 +107,28 @@ async fn run() {
),
);
let mut layout2 = Layout::new(CoordinateSystem::PositiveYDown);
layout2.reset(&LayoutSettings {
x: 0.0,
y: 200.0,
max_width: Some(200.0),
max_height: Some(190.0),
horizontal_align: HorizontalAlign::Center,
vertical_align: VerticalAlign::Middle,
..LayoutSettings::default()
});
layout2.append(
fonts.as_slice(),
&TextStyle::with_user_data(
"abcdefghijklmnopqrstuvwxyz\nThis should be partially clipped!\nabcdefghijklmnopqrstuvwxyz",
25.0,
0,
GlyphUserData,
),
);
text_renderer
.prepare(
&device,
@ -116,7 +139,7 @@ async fn run() {
height: config.height,
},
&fonts,
&[layout],
&[(layout1, TextOverflow::Hide), (layout2, TextOverflow::Hide)],
)
.unwrap();