Skip to content

Commit

Permalink
Merge pull request #31 from bugadani/try-opt
Browse files Browse the repository at this point in the history
Optimizations
  • Loading branch information
bugadani committed Aug 15, 2020
2 parents 4d38cf3 + b4675ab commit afa7a71
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 92 deletions.
34 changes: 18 additions & 16 deletions src/alignment/center.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Horizontal and vertical center aligned text.
use crate::{
alignment::{HorizontalTextAlignment, VerticalTextAlignment},
parser::Token,
parser::{Parser, Token},
rendering::{
cursor::Cursor,
line::{StyledLineIterator, UniformSpaceConfig},
Expand Down Expand Up @@ -30,13 +30,13 @@ where
F: Font + Copy,
{
/// Starts processing a line.
NextLine(Option<Token<'a>>, Cursor<F>),
NextLine(Option<Token<'a>>, Cursor<F>, Parser<'a>),

/// Renders the processed line.
DrawLine(StyledLineIterator<'a, C, F, UniformSpaceConfig, CenterAligned>),
}

impl<'a, C, F, V> StateFactory<F> for StyledTextBox<'a, C, F, CenterAligned, V>
impl<'a, C, F, V> StateFactory<'a, F> for StyledTextBox<'a, C, F, CenterAligned, V>
where
C: PixelColor,
F: Font + Copy,
Expand All @@ -46,8 +46,8 @@ where

#[inline]
#[must_use]
fn create_state(&self, cursor: Cursor<F>) -> Self::PixelIteratorState {
State::NextLine(None, cursor)
fn create_state(&self, cursor: Cursor<F>, parser: Parser<'a>) -> Self::PixelIteratorState {
State::NextLine(None, cursor, parser)
}
}

Expand All @@ -63,26 +63,25 @@ where
fn next(&mut self) -> Option<Self::Item> {
loop {
match self.state {
State::NextLine(ref carried_token, ref mut cursor) => {
State::NextLine(ref carried_token, mut cursor, ref mut parser) => {
if !cursor.in_display_area() {
break None;
}

if carried_token.is_none() && self.parser.is_empty() {
if carried_token.is_none() && parser.is_empty() {
break None;
}

let parser_clone = parser.clone();
let max_line_width = cursor.line_width();
let (width, _, _) = self.style.measure_line(
&mut self.parser.clone(),
carried_token.clone(),
max_line_width,
);
let (width, _, _) =
self.style
.measure_line(parser, carried_token.clone(), max_line_width);
cursor.advance_unchecked((max_line_width - width + 1) / 2);

self.state = State::DrawLine(StyledLineIterator::new(
self.parser.clone(),
*cursor,
parser_clone,
cursor,
UniformSpaceConfig {
space_width: F::total_char_width(' '),
},
Expand All @@ -96,10 +95,13 @@ where
break pixel;
}

self.parser = line_iterator.parser.clone();
let carried_token = line_iterator.remaining_token();

self.state = State::NextLine(carried_token, line_iterator.cursor);
self.state = State::NextLine(
carried_token,
line_iterator.cursor,
line_iterator.parser.clone(),
);
}
}
}
Expand Down
36 changes: 18 additions & 18 deletions src/alignment/justified.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Fully justified text.
use crate::{
alignment::{HorizontalTextAlignment, VerticalTextAlignment},
parser::Token,
parser::{Parser, Token},
rendering::{
cursor::Cursor,
line::{SpaceConfig, StyledLineIterator},
Expand Down Expand Up @@ -76,13 +76,13 @@ where
F: Font + Copy,
{
/// Starts processing a line.
NextLine(Option<Token<'a>>, Cursor<F>),
NextLine(Option<Token<'a>>, Cursor<F>, Parser<'a>),

/// Renders the processed line.
DrawLine(StyledLineIterator<'a, C, F, JustifiedSpaceConfig, Justified>),
}

impl<'a, C, F, V> StateFactory<F> for StyledTextBox<'a, C, F, Justified, V>
impl<'a, C, F, V> StateFactory<'a, F> for StyledTextBox<'a, C, F, Justified, V>
where
C: PixelColor,
F: Font + Copy,
Expand All @@ -92,8 +92,8 @@ where

#[inline]
#[must_use]
fn create_state(&self, cursor: Cursor<F>) -> Self::PixelIteratorState {
State::NextLine(None, cursor)
fn create_state(&self, cursor: Cursor<F>, parser: Parser<'a>) -> Self::PixelIteratorState {
State::NextLine(None, cursor, parser)
}
}

Expand All @@ -109,21 +109,20 @@ where
fn next(&mut self) -> Option<Self::Item> {
loop {
match self.state {
State::NextLine(ref carried_token, ref cursor) => {
State::NextLine(ref carried_token, cursor, ref mut parser) => {
if !cursor.in_display_area() {
break None;
}

if carried_token.is_none() && self.parser.is_empty() {
if carried_token.is_none() && parser.is_empty() {
break None;
}

let parser_clone = parser.clone();
let max_line_width = cursor.line_width();
let (width, total_whitespace_count, t) = self.style.measure_line(
&mut self.parser.clone(),
carried_token.clone(),
max_line_width,
);
let (width, total_whitespace_count, t) =
self.style
.measure_line(parser, carried_token.clone(), max_line_width);

let space = max_line_width
- (width - total_whitespace_count * F::total_char_width(' '));
Expand All @@ -138,8 +137,8 @@ where
};

self.state = State::DrawLine(StyledLineIterator::new(
self.parser.clone(),
*cursor,
parser_clone,
cursor,
space_info,
self.style.text_style,
carried_token.clone(),
Expand All @@ -151,10 +150,11 @@ where
break pixel;
}

self.parser = line_iterator.parser.clone();
let carried_token = line_iterator.remaining_token();

self.state = State::NextLine(carried_token, line_iterator.cursor);
self.state = State::NextLine(
line_iterator.remaining_token(),
line_iterator.cursor,
line_iterator.parser.clone(),
);
}
}
}
Expand Down
27 changes: 14 additions & 13 deletions src/alignment/left.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Left aligned text.
use crate::{
alignment::{HorizontalTextAlignment, VerticalTextAlignment},
parser::Token,
parser::{Parser, Token},
rendering::{
cursor::Cursor,
line::{StyledLineIterator, UniformSpaceConfig},
Expand All @@ -28,13 +28,13 @@ where
F: Font + Copy,
{
/// Starts processing a line.
NextLine(Option<Token<'a>>, Cursor<F>),
NextLine(Option<Token<'a>>, Cursor<F>, Parser<'a>),

/// Renders the processed line.
DrawLine(StyledLineIterator<'a, C, F, UniformSpaceConfig, LeftAligned>),
}

impl<'a, C, F, V> StateFactory<F> for StyledTextBox<'a, C, F, LeftAligned, V>
impl<'a, C, F, V> StateFactory<'a, F> for StyledTextBox<'a, C, F, LeftAligned, V>
where
C: PixelColor,
F: Font + Copy,
Expand All @@ -44,8 +44,8 @@ where

#[inline]
#[must_use]
fn create_state(&self, cursor: Cursor<F>) -> Self::PixelIteratorState {
State::NextLine(None, cursor)
fn create_state(&self, cursor: Cursor<F>, parser: Parser<'a>) -> Self::PixelIteratorState {
State::NextLine(None, cursor, parser)
}
}

Expand All @@ -61,18 +61,18 @@ where
fn next(&mut self) -> Option<Self::Item> {
loop {
match self.state {
State::NextLine(ref carried_token, ref cursor) => {
State::NextLine(ref carried_token, cursor, ref parser) => {
if !cursor.in_display_area() {
break None;
}

if carried_token.is_none() && self.parser.is_empty() {
if carried_token.is_none() && parser.is_empty() {
break None;
}

self.state = State::DrawLine(StyledLineIterator::new(
self.parser.clone(),
*cursor,
parser.clone(),
cursor,
UniformSpaceConfig {
space_width: F::total_char_width(' '),
},
Expand All @@ -86,10 +86,11 @@ where
break pixel;
}

self.parser = line_iterator.parser.clone();
let carried_token = line_iterator.remaining_token();

self.state = State::NextLine(carried_token, line_iterator.cursor);
self.state = State::NextLine(
line_iterator.remaining_token(),
line_iterator.cursor,
line_iterator.parser.clone(),
);
}
};
}
Expand Down
36 changes: 18 additions & 18 deletions src/alignment/right.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Right aligned text.
use crate::{
alignment::{HorizontalTextAlignment, VerticalTextAlignment},
parser::Token,
parser::{Parser, Token},
rendering::{
cursor::Cursor,
line::{StyledLineIterator, UniformSpaceConfig},
Expand All @@ -28,13 +28,13 @@ where
F: Font + Copy,
{
/// Starts processing a line.
NextLine(Option<Token<'a>>, Cursor<F>),
NextLine(Option<Token<'a>>, Cursor<F>, Parser<'a>),

/// Renders the processed line.
DrawLine(StyledLineIterator<'a, C, F, UniformSpaceConfig, RightAligned>),
}

impl<'a, C, F, V> StateFactory<F> for StyledTextBox<'a, C, F, RightAligned, V>
impl<'a, C, F, V> StateFactory<'a, F> for StyledTextBox<'a, C, F, RightAligned, V>
where
C: PixelColor,
F: Font + Copy,
Expand All @@ -44,8 +44,8 @@ where

#[inline]
#[must_use]
fn create_state(&self, cursor: Cursor<F>) -> Self::PixelIteratorState {
State::NextLine(None, cursor)
fn create_state(&self, cursor: Cursor<F>, parser: Parser<'a>) -> Self::PixelIteratorState {
State::NextLine(None, cursor, parser)
}
}

Expand All @@ -61,26 +61,25 @@ where
fn next(&mut self) -> Option<Self::Item> {
loop {
match self.state {
State::NextLine(ref carried_token, ref mut cursor) => {
State::NextLine(ref carried_token, mut cursor, ref mut parser) => {
if !cursor.in_display_area() {
break None;
}

if carried_token.is_none() && self.parser.is_empty() {
if carried_token.is_none() && parser.is_empty() {
break None;
}

let parser_clone = parser.clone();
let max_line_width = cursor.line_width();
let (width, _, _) = self.style.measure_line(
&mut self.parser.clone(),
carried_token.clone(),
max_line_width,
);
let (width, _, _) =
self.style
.measure_line(parser, carried_token.clone(), max_line_width);
cursor.advance_unchecked(max_line_width - width);

self.state = State::DrawLine(StyledLineIterator::new(
self.parser.clone(),
*cursor,
parser_clone,
cursor,
UniformSpaceConfig {
space_width: F::total_char_width(' '),
},
Expand All @@ -94,10 +93,11 @@ where
break pixel;
}

self.parser = line_iterator.parser.clone();
let carried_token = line_iterator.remaining_token();

self.state = State::NextLine(carried_token, line_iterator.cursor);
self.state = State::NextLine(
line_iterator.remaining_token(),
line_iterator.cursor,
line_iterator.parser.clone(),
);
}
}
}
Expand Down

0 comments on commit afa7a71

Please sign in to comment.