11//! Fully justified text.
22use crate :: {
33 alignment:: { HorizontalTextAlignment , VerticalTextAlignment } ,
4- parser:: Token ,
4+ parser:: { Parser , Token } ,
55 rendering:: {
66 cursor:: Cursor ,
77 line:: { SpaceConfig , StyledLineIterator } ,
@@ -76,13 +76,13 @@ where
7676 F : Font + Copy ,
7777{
7878 /// Starts processing a line.
79- NextLine ( Option < Token < ' a > > , Cursor < F > ) ,
79+ NextLine ( Option < Token < ' a > > , Cursor < F > , Parser < ' a > ) ,
8080
8181 /// Renders the processed line.
8282 DrawLine ( StyledLineIterator < ' a , C , F , JustifiedSpaceConfig , Justified > ) ,
8383}
8484
85- impl < ' a , C , F , V > StateFactory < F > for StyledTextBox < ' a , C , F , Justified , V >
85+ impl < ' a , C , F , V > StateFactory < ' a , F > for StyledTextBox < ' a , C , F , Justified , V >
8686where
8787 C : PixelColor ,
8888 F : Font + Copy ,
9292
9393 #[ inline]
9494 #[ must_use]
95- fn create_state ( & self , cursor : Cursor < F > ) -> Self :: PixelIteratorState {
96- State :: NextLine ( None , cursor)
95+ fn create_state ( & self , cursor : Cursor < F > , parser : Parser < ' a > ) -> Self :: PixelIteratorState {
96+ State :: NextLine ( None , cursor, parser )
9797 }
9898}
9999
@@ -109,21 +109,20 @@ where
109109 fn next ( & mut self ) -> Option < Self :: Item > {
110110 loop {
111111 match self . state {
112- State :: NextLine ( ref carried_token, ref cursor ) => {
112+ State :: NextLine ( ref carried_token, cursor , ref mut parser ) => {
113113 if !cursor. in_display_area ( ) {
114114 break None ;
115115 }
116116
117- if carried_token. is_none ( ) && self . parser . is_empty ( ) {
117+ if carried_token. is_none ( ) && parser. is_empty ( ) {
118118 break None ;
119119 }
120120
121+ let parser_clone = parser. clone ( ) ;
121122 let max_line_width = cursor. line_width ( ) ;
122- let ( width, total_whitespace_count, t) = self . style . measure_line (
123- & mut self . parser . clone ( ) ,
124- carried_token. clone ( ) ,
125- max_line_width,
126- ) ;
123+ let ( width, total_whitespace_count, t) =
124+ self . style
125+ . measure_line ( parser, carried_token. clone ( ) , max_line_width) ;
127126
128127 let space = max_line_width
129128 - ( width - total_whitespace_count * F :: total_char_width ( ' ' ) ) ;
@@ -138,8 +137,8 @@ where
138137 } ;
139138
140139 self . state = State :: DrawLine ( StyledLineIterator :: new (
141- self . parser . clone ( ) ,
142- * cursor,
140+ parser_clone ,
141+ cursor,
143142 space_info,
144143 self . style . text_style ,
145144 carried_token. clone ( ) ,
@@ -151,10 +150,11 @@ where
151150 break pixel;
152151 }
153152
154- self . parser = line_iterator. parser . clone ( ) ;
155- let carried_token = line_iterator. remaining_token ( ) ;
156-
157- self . state = State :: NextLine ( carried_token, line_iterator. cursor ) ;
153+ self . state = State :: NextLine (
154+ line_iterator. remaining_token ( ) ,
155+ line_iterator. cursor ,
156+ line_iterator. parser . clone ( ) ,
157+ ) ;
158158 }
159159 }
160160 }
0 commit comments