@@ -140,7 +140,6 @@ export default class FontRenderer {
140
140
return length ;
141
141
}
142
142
143
-
144
143
createBitMap ( img ) {
145
144
let canvas = document . createElement ( 'canvas' ) ;
146
145
canvas . width = img . width ;
@@ -178,6 +177,54 @@ export default class FontRenderer {
178
177
}
179
178
180
179
listFormattedStringToWidth ( text , wrapWidth ) {
181
- return text . split ( "\n" ) ; // TODO Implement wrap logic
180
+ let lines = [ ] ;
181
+ let currentColorCharacter = "r" ;
182
+ for ( let line of text . split ( '\n' ) ) {
183
+ let currentLine = '' ;
184
+ let currentLineWidth = 0 ;
185
+
186
+ // Split the text into words
187
+ for ( let word of line . split ( ' ' ) ) {
188
+ const wordWidth = this . getStringWidth ( word + " " ) ;
189
+
190
+ // If adding the word exceeds the wrap width, start a new line
191
+ if ( currentLineWidth + wordWidth > wrapWidth ) {
192
+ lines . push ( FontRenderer . COLOR_PREFIX + currentColorCharacter + currentLine . trim ( ) ) ;
193
+ currentColorCharacter = this . getLastColorCharacterOfText ( currentLine ) ;
194
+
195
+ currentLine = '' ;
196
+ currentLineWidth = 0 ;
197
+ }
198
+
199
+ // Add the word to the current line
200
+ currentLine += word + ' ' ;
201
+ currentLineWidth += wordWidth ;
202
+ }
203
+
204
+ // Push the last line
205
+ if ( currentLine . length > 0 ) {
206
+ lines . push ( FontRenderer . COLOR_PREFIX + currentColorCharacter + currentLine . trim ( ) ) ;
207
+ currentColorCharacter = this . getLastColorCharacterOfText ( currentLine ) ;
208
+ }
209
+ }
210
+ return lines ;
211
+ }
212
+
213
+ getLastColorCharacterOfText ( text ) {
214
+ let character = "r" ;
215
+ let isColorCode = false ;
216
+
217
+ // For each character
218
+ for ( let i = 0 ; i < text . length ; i ++ ) {
219
+ if ( text [ i ] === FontRenderer . COLOR_PREFIX ) {
220
+ isColorCode = true ;
221
+ } else {
222
+ if ( isColorCode ) {
223
+ character = text [ i ] ;
224
+ isColorCode = false ;
225
+ }
226
+ }
227
+ }
228
+ return character ;
182
229
}
183
230
}
0 commit comments