@@ -21,12 +21,12 @@ struct fc_rect fc_text_bounds(struct fc_character_mapping const mapping[], size_
2121}
2222
2323struct fc_text_segment {
24- size_t begin ;
25- size_t end ;
24+ size_t first ;
25+ size_t last ;
2626};
2727
2828float fc_text_segment_width (struct fc_text_segment const * segment , struct fc_character_mapping const * mapping ) {
29- return mapping [segment -> end ].target .right - mapping [segment -> begin ].target .left ;
29+ return mapping [segment -> last ].target .right - mapping [segment -> first ].target .left ;
3030}
3131
3232void fc_wrap (struct fc_character_mapping mapping [], size_t glyph_count , float line_width , float line_height , float space_width , enum fc_alignment aligment ) {
@@ -40,44 +40,58 @@ void fc_wrap(struct fc_character_mapping mapping[], size_t glyph_count, float li
4040
4141 /* if this current word has no begin, it means it is starting at this glyph
4242 * and word_count got incremented below */
43- if (current_word -> begin == 0 ) {
44- current_word -> begin = i ;
43+ if (current_word -> first == 0 && word_count > 0 ) {
44+ current_word -> first = i ;
4545 }
4646
4747 /* if we found a space, the word ends here and we skip to next iteration */
48- if (current_glyph -> codepoint == 0x20 || current_glyph -> codepoint == '\0' || i + 1 == glyph_count ) {
48+ if (current_glyph -> codepoint == 0x20 || current_glyph -> codepoint == '\0' ) {
4949 /* some fonts don't properly set target width of spaces */
5050 if (fc_rect_width (& current_glyph -> target ) < 0.01f ) {
5151 current_glyph -> target .right = current_glyph -> target .left + space_width ;
5252 }
53- current_word -> end = i ;
53+ current_word -> last = i - 1 ;
54+ word_count += 1 ;
55+
56+ /* since our words already have target rectangles, we need to move them
57+ * right by the space_width amount for every extra space,
58+ * because the line positioning algorithm below expects words to
59+ * be apart by a single space */
60+ while (mapping [i + 1 ].codepoint == 0x20 ) {
61+ i ++ ;
62+ fc_move (mapping + current_word -> first , current_word -> last - current_word -> first + 1 , space_width , 0 );
63+ }
64+ }
65+
66+ if (i + 1 == glyph_count ) {
67+ current_word -> last = i ;
5468 word_count += 1 ;
55- continue ;
5669 }
70+
5771 }
5872
5973 /* identify lines */
6074 float space_left = line_width ;
6175 size_t line_count = 1 ;
6276 for (size_t word_index = 0 ; word_index < word_count ; word_index ++ ) {
6377 struct fc_text_segment * word = & words [word_index ];
64- struct fc_rect bounds = fc_text_bounds (mapping + word -> begin , word -> end - word -> begin + 1 );
78+ struct fc_rect bounds = fc_text_bounds (mapping + word -> first , word -> last - word -> first + 1 );
6579 float word_width = fc_rect_width (& bounds );
6680 if (((word_width + space_width ) > space_left ) && word_index > 0 ) {
6781 line_count ++ ;
68- lines [line_count - 1 ].begin = words [word_index ].begin ;
82+ lines [line_count - 1 ].first = words [word_index ].first ;
6983 space_left = line_width - word_width ;
7084 } else {
7185 space_left -= word_width + space_width ;
7286 }
73- lines [line_count - 1 ].end = words [word_index ].end ;
87+ lines [line_count - 1 ].last = words [word_index ].last ;
7488 }
7589
7690 /* ajust yadd and xadd for lines according to alignment */
7791 float xadd , yadd ;
7892 for (size_t line_i = 0 ; line_i < line_count ; line_i ++ ) {
7993 yadd = line_i * line_height ;
80- xadd = - mapping [lines [line_i ].begin ].target .left ;
94+ xadd = - mapping [lines [line_i ].first ].target .left ;
8195 switch (aligment ) {
8296 default :
8397 case fc_align_left :
@@ -89,7 +103,7 @@ void fc_wrap(struct fc_character_mapping mapping[], size_t glyph_count, float li
89103 xadd += line_width - fc_text_segment_width (& lines [line_i ], mapping );
90104 break ;
91105 }
92- for (size_t glyph_i = lines [line_i ].begin ; glyph_i <= lines [line_i ].end ; glyph_i ++ ) {
106+ for (size_t glyph_i = lines [line_i ].first ; glyph_i <= lines [line_i ].last ; glyph_i ++ ) {
93107 struct fc_rect * dst = & mapping [glyph_i ].target ;
94108 dst -> left += xadd ;
95109 dst -> right += xadd ;
0 commit comments