| 624 | } |
| 625 | |
| 626 | void reflowTextLine(std::string& input, int width, const char* font, std::vector<std::string>& result) |
| 627 | { |
| 628 | Font* actualFont = Font::get(font); |
| 629 | if ( !actualFont ) |
| 630 | { |
| 631 | return; |
| 632 | } |
| 633 | int charWidth = 0; |
| 634 | actualFont->sizeText("_", &charWidth, nullptr); |
| 635 | if ( charWidth == 0 ) |
| 636 | { |
| 637 | return; |
| 638 | } |
| 639 | ++charWidth; |
| 640 | |
| 641 | const int charactersPerLine = (width) / charWidth; |
| 642 | |
| 643 | size_t offset = 0; |
| 644 | size_t findChar = 0; |
| 645 | std::vector<std::string> tokens; |
| 646 | /*switch ( local_rng.rand() % 3 ) |
| 647 | { |
| 648 | case 0: |
| 649 | input = " this is a test "; |
| 650 | break; |
| 651 | case 1: |
| 652 | input = " this is a test "; |
| 653 | break; |
| 654 | case 2: |
| 655 | input = " this is a test !"; |
| 656 | break; |
| 657 | }*/ |
| 658 | while ( (findChar = input.find(' ', offset)) != std::string::npos ) { |
| 659 | tokens.push_back(input.substr(offset, findChar - offset)); |
| 660 | offset = findChar + 1; |
| 661 | } |
| 662 | tokens.push_back(input.substr(offset)); |
| 663 | |
| 664 | size_t currentLine = 0; |
| 665 | Text* getText = nullptr; |
| 666 | bool lastInsertedManualSpace = false; |
| 667 | result.push_back(""); |
| 668 | for ( auto& token : tokens ) |
| 669 | { |
| 670 | size_t currentLength = result[currentLine].size(); |
| 671 | if ( (currentLength + 1 + token.size() < charactersPerLine / 2) ) |
| 672 | { |
| 673 | // this is probably OK |
| 674 | } |
| 675 | else if ( getText = Text::get(std::string(result[currentLine] + " " + token).c_str(), font, |
| 676 | makeColor(255, 255, 255, 255), makeColor(0, 0, 0, 255)) ) |
| 677 | { |
| 678 | if ( getText->getWidth() > width ) |
| 679 | { |
| 680 | ++currentLine; |
| 681 | result.push_back(""); |
| 682 | } |
| 683 | } |