| 789 | } |
| 790 | |
| 791 | void Field::reflowTextToFit(const int characterOffset, bool check) { |
| 792 | if ( text == nullptr || textlen <= 1 ) { |
| 793 | return; |
| 794 | } |
| 795 | |
| 796 | if (check) { |
| 797 | if (auto getText = Text::get(text, font.c_str(), textColor, outlineColor)) { |
| 798 | if (getText->getWidth() <= (getSize().w)) { |
| 799 | // no work to do |
| 800 | return; |
| 801 | } |
| 802 | } |
| 803 | } |
| 804 | std::string reflowText = ""; |
| 805 | |
| 806 | #ifndef EDITOR |
| 807 | bool usePreciseStringWidth = bUsePreciseFieldTextReflow; |
| 808 | #else |
| 809 | bool usePreciseStringWidth = true; |
| 810 | #endif |
| 811 | if ( usePreciseStringWidth ) |
| 812 | { |
| 813 | // more expensive, but accurate text reflow. |
| 814 | std::vector<std::string> allLines; |
| 815 | char* nexttoken; |
| 816 | char* token = text; |
| 817 | do { |
| 818 | nexttoken = tokenize(token, "\n"); |
| 819 | std::string tokenStr(token); |
| 820 | std::vector<std::string> result; |
| 821 | reflowTextLine(tokenStr, (getSize().w), font.c_str(), result); |
| 822 | for ( size_t i = 0; i < result.size(); ++i ) |
| 823 | { |
| 824 | allLines.push_back(result[i]); |
| 825 | } |
| 826 | } while ( (token = nexttoken) != NULL ); |
| 827 | |
| 828 | for ( auto it = allLines.begin(); it != allLines.end(); ++it ) |
| 829 | { |
| 830 | reflowText += (*it); |
| 831 | auto nextStr = std::next(it); |
| 832 | if ( nextStr != allLines.end() ) |
| 833 | { |
| 834 | reflowText += '\n'; |
| 835 | } |
| 836 | } |
| 837 | setText(reflowText.c_str()); |
| 838 | return; |
| 839 | } |
| 840 | |
| 841 | Font* actualFont = Font::get(font.c_str()); |
| 842 | if ( !actualFont ) |
| 843 | { |
| 844 | return; |
| 845 | } |
| 846 | |
| 847 | int charWidth = 0; |
| 848 | actualFont->sizeText("_", &charWidth, nullptr); |
no test coverage detected