Introduce text::Alignment with Justified support
This commit is contained in:
parent
e45d4b5cb6
commit
0e4a392731
30 changed files with 227 additions and 195 deletions
|
|
@ -87,11 +87,11 @@ impl text::Paragraph for () {
|
|||
text::Difference::None
|
||||
}
|
||||
|
||||
fn horizontal_alignment(&self) -> Option<alignment::Horizontal> {
|
||||
None
|
||||
fn align_x(&self) -> text::Alignment {
|
||||
text::Alignment::Default
|
||||
}
|
||||
|
||||
fn vertical_alignment(&self) -> alignment::Vertical {
|
||||
fn align_y(&self) -> alignment::Vertical {
|
||||
alignment::Vertical::Top
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -34,10 +34,10 @@ pub struct Text<Content = String, Font = crate::Font> {
|
|||
pub font: Font,
|
||||
|
||||
/// The horizontal alignment of the [`Text`].
|
||||
pub horizontal_alignment: Option<alignment::Horizontal>,
|
||||
pub align_x: Alignment,
|
||||
|
||||
/// The vertical alignment of the [`Text`].
|
||||
pub vertical_alignment: alignment::Vertical,
|
||||
pub align_y: alignment::Vertical,
|
||||
|
||||
/// The [`Shaping`] strategy of the [`Text`].
|
||||
pub shaping: Shaping,
|
||||
|
|
@ -46,6 +46,45 @@ pub struct Text<Content = String, Font = crate::Font> {
|
|||
pub wrapping: Wrapping,
|
||||
}
|
||||
|
||||
/// The alignment of some text.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
|
||||
pub enum Alignment {
|
||||
/// No specific alignment.
|
||||
///
|
||||
/// Left-to-right text will be aligned to the left, while
|
||||
/// right-to-left text will be aligned to the right.
|
||||
#[default]
|
||||
Default,
|
||||
/// Align text to the left.
|
||||
Left,
|
||||
/// Center text.
|
||||
Center,
|
||||
/// Align text to the right.
|
||||
Right,
|
||||
/// Justify text.
|
||||
Justified,
|
||||
}
|
||||
|
||||
impl From<alignment::Horizontal> for Alignment {
|
||||
fn from(alignment: alignment::Horizontal) -> Self {
|
||||
match alignment {
|
||||
alignment::Horizontal::Left => Self::Left,
|
||||
alignment::Horizontal::Center => Self::Center,
|
||||
alignment::Horizontal::Right => Self::Right,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<crate::Alignment> for Alignment {
|
||||
fn from(alignment: crate::Alignment) -> Self {
|
||||
match alignment {
|
||||
crate::Alignment::Start => Self::Left,
|
||||
crate::Alignment::Center => Self::Center,
|
||||
crate::Alignment::End => Self::Right,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The shaping strategy of some text.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
|
||||
pub enum Shaping {
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
//! Draw paragraphs.
|
||||
use crate::alignment;
|
||||
use crate::text::{Difference, Hit, Span, Text};
|
||||
use crate::text::{Alignment, Difference, Hit, Span, Text};
|
||||
use crate::{Point, Rectangle, Size};
|
||||
|
||||
/// A text paragraph.
|
||||
|
|
@ -24,10 +24,10 @@ pub trait Paragraph: Sized + Default {
|
|||
fn compare(&self, text: Text<(), Self::Font>) -> Difference;
|
||||
|
||||
/// Returns the horizontal alignment of the [`Paragraph`].
|
||||
fn horizontal_alignment(&self) -> Option<alignment::Horizontal>;
|
||||
fn align_x(&self) -> Alignment;
|
||||
|
||||
/// Returns the vertical alignment of the [`Paragraph`].
|
||||
fn vertical_alignment(&self) -> alignment::Vertical;
|
||||
fn align_y(&self) -> alignment::Vertical;
|
||||
|
||||
/// Returns the minimum boundaries that can fit the contents of the
|
||||
/// [`Paragraph`].
|
||||
|
|
@ -92,8 +92,8 @@ impl<P: Paragraph> Plain<P> {
|
|||
size: text.size,
|
||||
line_height: text.line_height,
|
||||
font: text.font,
|
||||
horizontal_alignment: text.horizontal_alignment,
|
||||
vertical_alignment: text.vertical_alignment,
|
||||
align_x: text.align_x,
|
||||
align_y: text.align_y,
|
||||
shaping: text.shaping,
|
||||
wrapping: text.wrapping,
|
||||
}) {
|
||||
|
|
@ -108,13 +108,13 @@ impl<P: Paragraph> Plain<P> {
|
|||
}
|
||||
|
||||
/// Returns the horizontal alignment of the [`Paragraph`].
|
||||
pub fn horizontal_alignment(&self) -> Option<alignment::Horizontal> {
|
||||
self.raw.horizontal_alignment()
|
||||
pub fn align_x(&self) -> Alignment {
|
||||
self.raw.align_x()
|
||||
}
|
||||
|
||||
/// Returns the vertical alignment of the [`Paragraph`].
|
||||
pub fn vertical_alignment(&self) -> alignment::Vertical {
|
||||
self.raw.vertical_alignment()
|
||||
pub fn align_y(&self) -> alignment::Vertical {
|
||||
self.raw.align_y()
|
||||
}
|
||||
|
||||
/// Returns the minimum boundaries that can fit the contents of the
|
||||
|
|
|
|||
|
|
@ -32,7 +32,7 @@ use crate::{
|
|||
Widget,
|
||||
};
|
||||
|
||||
pub use text::{LineHeight, Shaping, Wrapping};
|
||||
pub use text::{Alignment, LineHeight, Shaping, Wrapping};
|
||||
|
||||
/// A bunch of text.
|
||||
///
|
||||
|
|
@ -67,8 +67,8 @@ where
|
|||
line_height: LineHeight,
|
||||
width: Length,
|
||||
height: Length,
|
||||
horizontal_alignment: Option<alignment::Horizontal>,
|
||||
vertical_alignment: alignment::Vertical,
|
||||
align_x: text::Alignment,
|
||||
align_y: alignment::Vertical,
|
||||
font: Option<Renderer::Font>,
|
||||
shaping: Shaping,
|
||||
wrapping: Wrapping,
|
||||
|
|
@ -89,8 +89,8 @@ where
|
|||
font: None,
|
||||
width: Length::Shrink,
|
||||
height: Length::Shrink,
|
||||
horizontal_alignment: None,
|
||||
vertical_alignment: alignment::Vertical::Top,
|
||||
align_x: text::Alignment::Default,
|
||||
align_y: alignment::Vertical::Top,
|
||||
shaping: Shaping::default(),
|
||||
wrapping: Wrapping::default(),
|
||||
class: Theme::default(),
|
||||
|
|
@ -136,11 +136,8 @@ where
|
|||
}
|
||||
|
||||
/// Sets the [`alignment::Horizontal`] of the [`Text`].
|
||||
pub fn align_x(
|
||||
mut self,
|
||||
alignment: impl Into<alignment::Horizontal>,
|
||||
) -> Self {
|
||||
self.horizontal_alignment = Some(alignment.into());
|
||||
pub fn align_x(mut self, alignment: impl Into<text::Alignment>) -> Self {
|
||||
self.align_x = alignment.into();
|
||||
self
|
||||
}
|
||||
|
||||
|
|
@ -149,7 +146,7 @@ where
|
|||
mut self,
|
||||
alignment: impl Into<alignment::Vertical>,
|
||||
) -> Self {
|
||||
self.vertical_alignment = alignment.into();
|
||||
self.align_y = alignment.into();
|
||||
self
|
||||
}
|
||||
|
||||
|
|
@ -245,8 +242,8 @@ where
|
|||
self.line_height,
|
||||
self.size,
|
||||
self.font,
|
||||
self.horizontal_alignment,
|
||||
self.vertical_alignment,
|
||||
self.align_x,
|
||||
self.align_y,
|
||||
self.shaping,
|
||||
self.wrapping,
|
||||
)
|
||||
|
|
@ -290,8 +287,8 @@ pub fn layout<Renderer>(
|
|||
line_height: LineHeight,
|
||||
size: Option<Pixels>,
|
||||
font: Option<Renderer::Font>,
|
||||
horizontal_alignment: Option<alignment::Horizontal>,
|
||||
vertical_alignment: alignment::Vertical,
|
||||
align_x: text::Alignment,
|
||||
align_y: alignment::Vertical,
|
||||
shaping: Shaping,
|
||||
wrapping: Wrapping,
|
||||
) -> layout::Node
|
||||
|
|
@ -312,8 +309,8 @@ where
|
|||
size,
|
||||
line_height,
|
||||
font,
|
||||
horizontal_alignment,
|
||||
vertical_alignment,
|
||||
align_x,
|
||||
align_y,
|
||||
shaping,
|
||||
wrapping,
|
||||
});
|
||||
|
|
@ -344,13 +341,13 @@ pub fn draw<Renderer>(
|
|||
{
|
||||
let bounds = layout.bounds();
|
||||
|
||||
let x = match paragraph.horizontal_alignment() {
|
||||
None | Some(alignment::Horizontal::Left) => bounds.x,
|
||||
Some(alignment::Horizontal::Center) => bounds.center_x(),
|
||||
Some(alignment::Horizontal::Right) => bounds.x + bounds.width,
|
||||
let x = match paragraph.align_x() {
|
||||
Alignment::Default | Alignment::Left | Alignment::Justified => bounds.x,
|
||||
Alignment::Center => bounds.center_x(),
|
||||
Alignment::Right => bounds.x + bounds.width,
|
||||
};
|
||||
|
||||
let y = match paragraph.vertical_alignment() {
|
||||
let y = match paragraph.align_y() {
|
||||
alignment::Vertical::Top => bounds.y,
|
||||
alignment::Vertical::Center => bounds.center_y(),
|
||||
alignment::Vertical::Bottom => bounds.y + bounds.height,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue