Support max_width and text::Alignment for canvas::Text
This commit is contained in:
parent
6ebf386249
commit
97b4ed0d84
13 changed files with 139 additions and 127 deletions
|
|
@ -1,5 +1,6 @@
|
|||
use crate::core;
|
||||
use crate::core::alignment;
|
||||
use crate::core::text::{LineHeight, Shaping};
|
||||
use crate::core::text::{Alignment, LineHeight, Paragraph, Shaping, Wrapping};
|
||||
use crate::core::{Color, Font, Pixels, Point, Size, Vector};
|
||||
use crate::geometry::Path;
|
||||
use crate::text;
|
||||
|
|
@ -21,6 +22,10 @@ pub struct Text {
|
|||
/// For example, when the horizontal_alignment and vertical_alignment are set to Center, the
|
||||
/// center of the text will be placed at the given position NOT the top-left coordinate.
|
||||
pub position: Point,
|
||||
/// The maximum horizontal space available for this [`Text`].
|
||||
///
|
||||
/// Text will break into new lines when the width is reached.
|
||||
pub max_width: f32,
|
||||
/// The color of the text
|
||||
pub color: Color,
|
||||
/// The size of the text
|
||||
|
|
@ -30,7 +35,7 @@ pub struct Text {
|
|||
/// The font of the text
|
||||
pub font: Font,
|
||||
/// The horizontal alignment of the text
|
||||
pub align_x: alignment::Horizontal,
|
||||
pub align_x: Alignment,
|
||||
/// The vertical alignment of the text
|
||||
pub align_y: alignment::Vertical,
|
||||
/// The shaping strategy of the text.
|
||||
|
|
@ -41,62 +46,50 @@ impl Text {
|
|||
/// Computes the [`Path`]s of the [`Text`] and draws them using
|
||||
/// the given closure.
|
||||
pub fn draw_with(&self, mut f: impl FnMut(Path, Color)) {
|
||||
let mut font_system =
|
||||
text::font_system().write().expect("Write font system");
|
||||
|
||||
let mut buffer = cosmic_text::BufferLine::new(
|
||||
&self.content,
|
||||
cosmic_text::LineEnding::default(),
|
||||
cosmic_text::AttrsList::new(&text::to_attributes(self.font)),
|
||||
text::to_shaping(self.shaping),
|
||||
);
|
||||
|
||||
let layout = buffer.layout(
|
||||
font_system.raw(),
|
||||
self.size.0,
|
||||
None,
|
||||
cosmic_text::Wrap::None,
|
||||
None,
|
||||
4,
|
||||
);
|
||||
let paragraph = text::Paragraph::with_text(core::text::Text {
|
||||
content: &self.content,
|
||||
bounds: Size::new(self.max_width, f32::INFINITY),
|
||||
size: self.size,
|
||||
line_height: self.line_height,
|
||||
font: self.font,
|
||||
align_x: self.align_x,
|
||||
align_y: self.align_y,
|
||||
shaping: self.shaping,
|
||||
wrapping: Wrapping::default(),
|
||||
});
|
||||
|
||||
let translation_x = match self.align_x {
|
||||
alignment::Horizontal::Left => self.position.x,
|
||||
alignment::Horizontal::Center | alignment::Horizontal::Right => {
|
||||
let mut line_width = 0.0f32;
|
||||
|
||||
for line in layout.iter() {
|
||||
line_width = line_width.max(line.w);
|
||||
}
|
||||
|
||||
if self.align_x == alignment::Horizontal::Center {
|
||||
self.position.x - line_width / 2.0
|
||||
} else {
|
||||
self.position.x - line_width
|
||||
}
|
||||
Alignment::Default | Alignment::Left | Alignment::Justified => {
|
||||
self.position.x
|
||||
}
|
||||
Alignment::Center => self.position.x - paragraph.min_width() / 2.0,
|
||||
Alignment::Right => self.position.x - paragraph.min_width(),
|
||||
};
|
||||
|
||||
let translation_y = {
|
||||
let line_height = self.line_height.to_absolute(self.size);
|
||||
|
||||
match self.align_y {
|
||||
alignment::Vertical::Top => self.position.y,
|
||||
alignment::Vertical::Center => {
|
||||
self.position.y - line_height.0 / 2.0
|
||||
self.position.y - paragraph.min_height() / 2.0
|
||||
}
|
||||
alignment::Vertical::Bottom => {
|
||||
self.position.y - paragraph.min_height()
|
||||
}
|
||||
alignment::Vertical::Bottom => self.position.y - line_height.0,
|
||||
}
|
||||
};
|
||||
|
||||
let buffer = paragraph.buffer();
|
||||
let mut swash_cache = cosmic_text::SwashCache::new();
|
||||
|
||||
for run in layout.iter() {
|
||||
let mut font_system =
|
||||
text::font_system().write().expect("Write font system");
|
||||
|
||||
for run in buffer.layout_runs() {
|
||||
for glyph in run.glyphs.iter() {
|
||||
let physical_glyph = glyph.physical((0.0, 0.0), 1.0);
|
||||
|
||||
let start_x = translation_x + glyph.x + glyph.x_offset;
|
||||
let start_y = translation_y + glyph.y_offset + self.size.0;
|
||||
let start_y = translation_y + glyph.y_offset + run.line_y;
|
||||
let offset = Vector::new(start_x, start_y);
|
||||
|
||||
if let Some(commands) = swash_cache.get_outline_commands(
|
||||
|
|
@ -176,11 +169,12 @@ impl Default for Text {
|
|||
Text {
|
||||
content: String::new(),
|
||||
position: Point::ORIGIN,
|
||||
max_width: f32::INFINITY,
|
||||
color: Color::BLACK,
|
||||
size: Pixels(16.0),
|
||||
line_height: LineHeight::Relative(1.2),
|
||||
font: Font::default(),
|
||||
align_x: alignment::Horizontal::Left,
|
||||
align_x: Alignment::Default,
|
||||
align_y: alignment::Vertical::Top,
|
||||
shaping: Shaping::Basic,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -257,6 +257,47 @@ pub fn measure(buffer: &cosmic_text::Buffer) -> (Size, bool) {
|
|||
(Size::new(width, height), has_rtl)
|
||||
}
|
||||
|
||||
/// Aligns the given [`cosmic_text::Buffer`] with the given [`Alignment`]
|
||||
/// and returns its minimum [`Size`].
|
||||
pub fn align(
|
||||
buffer: &mut cosmic_text::Buffer,
|
||||
font_system: &mut cosmic_text::FontSystem,
|
||||
alignment: Alignment,
|
||||
) -> Size {
|
||||
let (min_bounds, has_rtl) = measure(buffer);
|
||||
let mut needs_relayout = has_rtl;
|
||||
|
||||
if let Some(align) = to_align(alignment) {
|
||||
let has_multiple_lines = buffer.lines.len() > 1
|
||||
|| buffer.lines.first().is_some_and(|line| {
|
||||
line.layout_opt().is_some_and(|layout| layout.len() > 1)
|
||||
});
|
||||
|
||||
if has_multiple_lines {
|
||||
for line in &mut buffer.lines {
|
||||
let _ = line.set_align(Some(align));
|
||||
}
|
||||
|
||||
needs_relayout = true;
|
||||
} else if let Some(line) = buffer.lines.first_mut() {
|
||||
needs_relayout = line.set_align(None);
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Avoid relayout with some changes to `cosmic-text` (?)
|
||||
if needs_relayout {
|
||||
log::trace!("Relayouting paragraph...");
|
||||
|
||||
buffer.set_size(
|
||||
font_system,
|
||||
Some(min_bounds.width),
|
||||
Some(min_bounds.height),
|
||||
);
|
||||
}
|
||||
|
||||
min_bounds
|
||||
}
|
||||
|
||||
/// Returns the attributes of the given [`Font`].
|
||||
pub fn to_attributes(font: Font) -> cosmic_text::Attrs<'static> {
|
||||
cosmic_text::Attrs::new()
|
||||
|
|
|
|||
|
|
@ -58,15 +58,7 @@ impl Cache {
|
|||
text::to_shaping(key.shaping),
|
||||
);
|
||||
|
||||
let (bounds, has_rtl) = text::measure(&buffer);
|
||||
|
||||
if has_rtl {
|
||||
buffer.set_size(
|
||||
font_system,
|
||||
Some(bounds.width),
|
||||
Some(bounds.height),
|
||||
);
|
||||
}
|
||||
let bounds = text::align(&mut buffer, font_system, key.align_x);
|
||||
|
||||
let _ = entry.insert(Entry {
|
||||
buffer,
|
||||
|
|
@ -123,6 +115,8 @@ pub struct Key<'a> {
|
|||
pub bounds: Size,
|
||||
/// The shaping strategy of the text.
|
||||
pub shaping: text::Shaping,
|
||||
/// The alignment of the text.
|
||||
pub align_x: text::Alignment,
|
||||
}
|
||||
|
||||
impl Key<'_> {
|
||||
|
|
@ -134,6 +128,7 @@ impl Key<'_> {
|
|||
self.bounds.width.to_bits().hash(&mut hasher);
|
||||
self.bounds.height.to_bits().hash(&mut hasher);
|
||||
self.shaping.hash(&mut hasher);
|
||||
self.align_x.hash(&mut hasher);
|
||||
|
||||
hasher.finish()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -89,7 +89,8 @@ impl core::text::Paragraph for Paragraph {
|
|||
text::to_shaping(text.shaping),
|
||||
);
|
||||
|
||||
let min_bounds = align(&mut buffer, &mut font_system, text.align_x);
|
||||
let min_bounds =
|
||||
text::align(&mut buffer, font_system.raw(), text.align_x);
|
||||
|
||||
Self(Arc::new(Internal {
|
||||
buffer,
|
||||
|
|
@ -159,7 +160,8 @@ impl core::text::Paragraph for Paragraph {
|
|||
None,
|
||||
);
|
||||
|
||||
let min_bounds = align(&mut buffer, &mut font_system, text.align_x);
|
||||
let min_bounds =
|
||||
text::align(&mut buffer, font_system.raw(), text.align_x);
|
||||
|
||||
Self(Arc::new(Internal {
|
||||
buffer,
|
||||
|
|
@ -186,8 +188,11 @@ impl core::text::Paragraph for Paragraph {
|
|||
Some(new_bounds.height),
|
||||
);
|
||||
|
||||
let min_bounds =
|
||||
align(&mut paragraph.buffer, &mut font_system, paragraph.align_x);
|
||||
let min_bounds = text::align(
|
||||
&mut paragraph.buffer,
|
||||
font_system.raw(),
|
||||
paragraph.align_x,
|
||||
);
|
||||
|
||||
paragraph.bounds = new_bounds;
|
||||
paragraph.min_bounds = min_bounds;
|
||||
|
|
@ -357,45 +362,6 @@ impl core::text::Paragraph for Paragraph {
|
|||
}
|
||||
}
|
||||
|
||||
fn align(
|
||||
buffer: &mut cosmic_text::Buffer,
|
||||
font_system: &mut text::FontSystem,
|
||||
alignment: Alignment,
|
||||
) -> Size {
|
||||
let (min_bounds, has_rtl) = text::measure(buffer);
|
||||
let mut needs_relayout = has_rtl;
|
||||
|
||||
if let Some(align) = text::to_align(alignment) {
|
||||
let has_multiple_lines = buffer.lines.len() > 1
|
||||
|| buffer.lines.first().is_some_and(|line| {
|
||||
line.layout_opt().is_some_and(|layout| layout.len() > 1)
|
||||
});
|
||||
|
||||
if has_multiple_lines {
|
||||
for line in &mut buffer.lines {
|
||||
let _ = line.set_align(Some(align));
|
||||
}
|
||||
|
||||
needs_relayout = true;
|
||||
} else if let Some(line) = buffer.lines.first_mut() {
|
||||
needs_relayout = line.set_align(None);
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Avoid relayout with some changes to `cosmic-text` (?)
|
||||
if needs_relayout {
|
||||
log::trace!("Relayouting paragraph...");
|
||||
|
||||
buffer.set_size(
|
||||
font_system.raw(),
|
||||
Some(min_bounds.width),
|
||||
Some(min_bounds.height),
|
||||
);
|
||||
}
|
||||
|
||||
min_bounds
|
||||
}
|
||||
|
||||
impl Default for Paragraph {
|
||||
fn default() -> Self {
|
||||
Self(Arc::new(Internal::default()))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue