Make FontSystem global and simplify Paragraph API
This commit is contained in:
parent
9245423c5d
commit
346af3f8b0
15 changed files with 165 additions and 328 deletions
|
|
@ -58,16 +58,6 @@ impl text::Renderer for Null {
|
|||
|
||||
fn load_font(&mut self, _font: Cow<'static, [u8]>) {}
|
||||
|
||||
fn create_paragraph(&self, _text: Text<'_, Self::Font>) -> Self::Paragraph {
|
||||
}
|
||||
|
||||
fn resize_paragraph(
|
||||
&self,
|
||||
_paragraph: &mut Self::Paragraph,
|
||||
_new_bounds: Size,
|
||||
) {
|
||||
}
|
||||
|
||||
fn fill_paragraph(
|
||||
&mut self,
|
||||
_paragraph: &Self::Paragraph,
|
||||
|
|
@ -88,24 +78,12 @@ impl text::Renderer for Null {
|
|||
impl text::Paragraph for () {
|
||||
type Font = Font;
|
||||
|
||||
fn content(&self) -> &str {
|
||||
""
|
||||
}
|
||||
fn with_text(_text: Text<'_, Self::Font>) -> Self {}
|
||||
|
||||
fn text_size(&self) -> Pixels {
|
||||
Pixels(16.0)
|
||||
}
|
||||
fn resize(&mut self, _new_bounds: Size) {}
|
||||
|
||||
fn font(&self) -> Self::Font {
|
||||
Font::default()
|
||||
}
|
||||
|
||||
fn line_height(&self) -> text::LineHeight {
|
||||
text::LineHeight::default()
|
||||
}
|
||||
|
||||
fn shaping(&self) -> text::Shaping {
|
||||
text::Shaping::default()
|
||||
fn compare(&self, _text: Text<'_, Self::Font>) -> text::Difference {
|
||||
text::Difference::None
|
||||
}
|
||||
|
||||
fn horizontal_alignment(&self) -> alignment::Horizontal {
|
||||
|
|
@ -120,10 +98,6 @@ impl text::Paragraph for () {
|
|||
None
|
||||
}
|
||||
|
||||
fn bounds(&self) -> Size {
|
||||
Size::ZERO
|
||||
}
|
||||
|
||||
fn min_bounds(&self) -> Size {
|
||||
Size::ZERO
|
||||
}
|
||||
|
|
|
|||
|
|
@ -156,33 +156,6 @@ pub trait Renderer: crate::Renderer {
|
|||
/// Loads a [`Self::Font`] from its bytes.
|
||||
fn load_font(&mut self, font: Cow<'static, [u8]>);
|
||||
|
||||
/// Creates a new [`Paragraph`] laid out with the given [`Text`].
|
||||
fn create_paragraph(&self, text: Text<'_, Self::Font>) -> Self::Paragraph;
|
||||
|
||||
/// Lays out the given [`Paragraph`] with some new boundaries.
|
||||
fn resize_paragraph(
|
||||
&self,
|
||||
paragraph: &mut Self::Paragraph,
|
||||
new_bounds: Size,
|
||||
);
|
||||
|
||||
/// Updates a [`Paragraph`] to match the given [`Text`], if needed.
|
||||
fn update_paragraph(
|
||||
&self,
|
||||
paragraph: &mut Self::Paragraph,
|
||||
text: Text<'_, Self::Font>,
|
||||
) {
|
||||
match compare(paragraph, text) {
|
||||
Difference::None => {}
|
||||
Difference::Bounds => {
|
||||
self.resize_paragraph(paragraph, text.bounds);
|
||||
}
|
||||
Difference::Shape => {
|
||||
*paragraph = self.create_paragraph(text);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Draws the given [`Paragraph`] at the given position and with the given
|
||||
/// [`Color`].
|
||||
fn fill_paragraph(
|
||||
|
|
@ -201,25 +174,21 @@ pub trait Renderer: crate::Renderer {
|
|||
color: Color,
|
||||
);
|
||||
}
|
||||
|
||||
/// A text paragraph.
|
||||
pub trait Paragraph: Default {
|
||||
pub trait Paragraph: Sized + Default {
|
||||
/// The font of this [`Paragraph`].
|
||||
type Font;
|
||||
type Font: Copy + PartialEq;
|
||||
|
||||
/// Returns the content of the [`Paragraph`].
|
||||
fn content(&self) -> &str;
|
||||
/// Creates a new [`Paragraph`] laid out with the given [`Text`].
|
||||
fn with_text(text: Text<'_, Self::Font>) -> Self;
|
||||
|
||||
/// Returns the text size of the [`Paragraph`].
|
||||
fn text_size(&self) -> Pixels;
|
||||
/// Lays out the [`Paragraph`] with some new boundaries.
|
||||
fn resize(&mut self, new_bounds: Size);
|
||||
|
||||
/// Returns the [`LineHeight`] of the [`Paragraph`].
|
||||
fn line_height(&self) -> LineHeight;
|
||||
|
||||
/// Returns the [`Self::Font`] of the [`Paragraph`].
|
||||
fn font(&self) -> Self::Font;
|
||||
|
||||
/// Returns the [`Shaping`] strategy of the [`Paragraph`].
|
||||
fn shaping(&self) -> Shaping;
|
||||
/// Compares the [`Paragraph`] with some desired [`Text`] and returns the
|
||||
/// [`Difference`].
|
||||
fn compare(&self, text: Text<'_, Self::Font>) -> Difference;
|
||||
|
||||
/// Returns the horizontal alignment of the [`Paragraph`].
|
||||
fn horizontal_alignment(&self) -> alignment::Horizontal;
|
||||
|
|
@ -227,9 +196,6 @@ pub trait Paragraph: Default {
|
|||
/// Returns the vertical alignment of the [`Paragraph`].
|
||||
fn vertical_alignment(&self) -> alignment::Vertical;
|
||||
|
||||
/// Returns the boundaries of the [`Paragraph`].
|
||||
fn bounds(&self) -> Size;
|
||||
|
||||
/// Returns the minimum boundaries that can fit the contents of the
|
||||
/// [`Paragraph`].
|
||||
fn min_bounds(&self) -> Size;
|
||||
|
|
@ -241,6 +207,19 @@ pub trait Paragraph: Default {
|
|||
/// Returns the distance to the given grapheme index in the [`Paragraph`].
|
||||
fn grapheme_position(&self, line: usize, index: usize) -> Option<Point>;
|
||||
|
||||
/// Updates the [`Paragraph`] to match the given [`Text`], if needed.
|
||||
fn update(&mut self, text: Text<'_, Self::Font>) {
|
||||
match self.compare(text) {
|
||||
Difference::None => {}
|
||||
Difference::Bounds => {
|
||||
self.resize(text.bounds);
|
||||
}
|
||||
Difference::Shape => {
|
||||
*self = Self::with_text(text);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the minimum width that can fit the contents of the [`Paragraph`].
|
||||
fn min_width(&self) -> f32 {
|
||||
self.min_bounds().width
|
||||
|
|
@ -276,26 +255,3 @@ pub enum Difference {
|
|||
/// the text is necessary.
|
||||
Shape,
|
||||
}
|
||||
|
||||
/// Compares a [`Paragraph`] with some desired [`Text`] and returns the
|
||||
/// [`Difference`].
|
||||
pub fn compare<Font: PartialEq>(
|
||||
paragraph: &impl Paragraph<Font = Font>,
|
||||
text: Text<'_, Font>,
|
||||
) -> Difference {
|
||||
if paragraph.content() != text.content
|
||||
|| paragraph.text_size() != text.size
|
||||
|| paragraph.line_height().to_absolute(text.size)
|
||||
!= text.line_height.to_absolute(text.size)
|
||||
|| paragraph.font() != text.font
|
||||
|| paragraph.shaping() != text.shaping
|
||||
|| paragraph.horizontal_alignment() != text.horizontal_alignment
|
||||
|| paragraph.vertical_alignment() != text.vertical_alignment
|
||||
{
|
||||
Difference::Shape
|
||||
} else if paragraph.bounds() != text.bounds {
|
||||
Difference::Bounds
|
||||
} else {
|
||||
Difference::None
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -212,19 +212,16 @@ where
|
|||
|
||||
let State(ref mut paragraph) = state;
|
||||
|
||||
renderer.update_paragraph(
|
||||
paragraph,
|
||||
text::Text {
|
||||
content,
|
||||
bounds,
|
||||
size,
|
||||
line_height,
|
||||
font,
|
||||
shaping,
|
||||
horizontal_alignment,
|
||||
vertical_alignment,
|
||||
},
|
||||
);
|
||||
paragraph.update(text::Text {
|
||||
content,
|
||||
bounds,
|
||||
size,
|
||||
line_height,
|
||||
font,
|
||||
shaping,
|
||||
horizontal_alignment,
|
||||
vertical_alignment,
|
||||
});
|
||||
|
||||
let size = limits.resolve(paragraph.min_bounds());
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue