| 400 | } |
| 401 | |
| 402 | Eigen::Vector2f Font::sizeWrappedText(std::string text, float xLen) const |
| 403 | { |
| 404 | Eigen::Vector2f out(0, 0); |
| 405 | |
| 406 | float y = 0; |
| 407 | |
| 408 | std::string line, word, temp; |
| 409 | Eigen::Vector2f textSize; |
| 410 | size_t space, newline; |
| 411 | |
| 412 | while(text.length() > 0 || !line.empty()) //while there's text or we still have text to render |
| 413 | { |
| 414 | space = text.find(' ', 0); |
| 415 | if(space == std::string::npos) |
| 416 | space = text.length() - 1; |
| 417 | |
| 418 | word = text.substr(0, space + 1); |
| 419 | |
| 420 | //check if the next word contains a newline |
| 421 | newline = word.find('\n', 0); |
| 422 | if(newline != std::string::npos) |
| 423 | { |
| 424 | word = word.substr(0, newline); |
| 425 | text.erase(0, newline + 1); |
| 426 | }else{ |
| 427 | text.erase(0, space + 1); |
| 428 | } |
| 429 | |
| 430 | temp = line + word; |
| 431 | |
| 432 | textSize = sizeText(temp); |
| 433 | |
| 434 | //if we're on the last word and it'll fit on the line, just add it to the line |
| 435 | if((textSize.x() <= xLen && text.length() == 0) || newline != std::string::npos) |
| 436 | { |
| 437 | line = temp; |
| 438 | word = ""; |
| 439 | } |
| 440 | |
| 441 | //if the next line will be too long or we're on the last of the text, render it |
| 442 | if(textSize.x() > xLen || text.length() == 0 || newline != std::string::npos) |
| 443 | { |
| 444 | //increment y by height and some extra padding for the next line |
| 445 | y += textSize.y() + 4; |
| 446 | |
| 447 | //move the word we skipped to the next line |
| 448 | line = word; |
| 449 | |
| 450 | //update our maximum known line width |
| 451 | if(textSize.x() > out.x()) |
| 452 | out[0] = textSize.x(); |
| 453 | }else{ |
| 454 | //there's still space, continue building the line |
| 455 | line = temp; |
| 456 | } |
| 457 | |
| 458 | } |
| 459 |
no test coverage detected