summaryrefslogtreecommitdiff
path: root/src/layout/text.rs
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2019-12-30 22:28:56 +0100
committerLaurenz <laurmaedje@gmail.com>2019-12-30 22:28:56 +0100
commit269f069a4d721a986807293ef71be1348bfae3d4 (patch)
tree31b737c4ff2aead3eb5e2673828595bb26622032 /src/layout/text.rs
parentb8620121a692df6313eeb5ccf7baf89c1e364116 (diff)
Simple line layouter 🧾
Diffstat (limited to 'src/layout/text.rs')
-rw-r--r--src/layout/text.rs44
1 files changed, 29 insertions, 15 deletions
diff --git a/src/layout/text.rs b/src/layout/text.rs
index 996c5139..e9721429 100644
--- a/src/layout/text.rs
+++ b/src/layout/text.rs
@@ -12,6 +12,7 @@ use super::*;
pub struct TextContext<'a, 'p> {
pub loader: &'a SharedFontLoader<'p>,
pub style: &'a TextStyle,
+ pub axes: LayoutAxes,
pub alignment: LayoutAlignment,
}
@@ -50,22 +51,14 @@ impl<'a, 'p> TextLayouter<'a, 'p> {
/// Layout the text
fn layout(mut self) -> LayoutResult<Layout> {
- for c in self.text.chars() {
- let (index, char_width) = self.select_font(c)?;
-
- self.width += char_width;
-
- if self.active_font != index {
- if !self.buffer.is_empty() {
- self.actions.add(LayoutAction::WriteText(self.buffer));
- self.buffer = String::new();
- }
-
- self.actions.add(LayoutAction::SetFont(index, self.ctx.style.font_size()));
- self.active_font = index;
+ if self.ctx.axes.primary.is_positive() {
+ for c in self.text.chars() {
+ self.layout_char(c)?;
+ }
+ } else {
+ for c in self.text.chars().rev() {
+ self.layout_char(c)?;
}
-
- self.buffer.push(c);
}
if !self.buffer.is_empty() {
@@ -79,6 +72,27 @@ impl<'a, 'p> TextLayouter<'a, 'p> {
})
}
+ /// Layout an individual character.
+ fn layout_char(&mut self, c: char) -> LayoutResult<()> {
+ let (index, char_width) = self.select_font(c)?;
+
+ self.width += char_width;
+
+ if self.active_font != index {
+ if !self.buffer.is_empty() {
+ let text = std::mem::replace(&mut self.buffer, String::new());
+ self.actions.add(LayoutAction::WriteText(text));
+ }
+
+ self.actions.add(LayoutAction::SetFont(index, self.ctx.style.font_size()));
+ self.active_font = index;
+ }
+
+ self.buffer.push(c);
+
+ Ok(())
+ }
+
/// Select the best font for a character and return its index along with
/// the width of the char in the font.
fn select_font(&mut self, c: char) -> LayoutResult<(usize, Size)> {