| 1 | /* |
| 2 | Copyright (c) 2018 Poul Sander |
| 3 | |
| 4 | Permission is hereby granted, free of charge, to any person |
| 5 | obtaining a copy of this software and associated documentation files |
| 6 | (the "Software"), to deal in the Software without restriction, |
| 7 | including without limitation the rights to use, copy, modify, merge, |
| 8 | publish, distribute, sublicense, and/or sell copies of the Software, |
| 9 | and to permit persons to whom the Software is furnished to do so, |
| 10 | subject to the following conditions: |
| 11 | |
| 12 | The above copyright notice and this permission notice shall be |
| 13 | included in all copies or substantial portions of the Software. |
| 14 | |
| 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 16 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 17 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 18 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 19 | BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 20 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 21 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | SOFTWARE. |
| 23 | */ |
| 24 | |
| 25 | #include "SagoTextBox.hpp" |
| 26 | #include "SagoTextField.hpp" |
| 27 | #include <vector> |
| 28 | #include <iostream> |
| 29 | #include "utf8.h" |
| 30 | #include <algorithm> |
| 31 | |
| 32 | namespace sago { |
| 33 | |
| 34 | struct SagoTextBox::SagoTextBoxData { |
| 35 | const sago::SagoDataHolder* tex = nullptr; |
| 36 | std::string fontName = "freeserif"; |
| 37 | SDL_Color color = { 255, 255, 255, 0 }; |
| 38 | SDL_Color outlineColor = { 255, 255, 0, 0 }; |
| 39 | int fontSize = 16; |
| 40 | int outline = 0; |
| 41 | std::string text = ""; |
| 42 | std::string renderedText = ""; |
| 43 | std::vector<SagoTextField> lines; |
| 44 | int maxWidth = 0; |
| 45 | }; |
| 46 | |
| 47 | SagoTextBox::SagoTextBox() { |
| 48 | data = new SagoTextBoxData(); |
| 49 | } |
| 50 | |
| 51 | SagoTextBox::~SagoTextBox() { |
| 52 | delete data; |
| 53 | } |
| 54 | |
| 55 | void SagoTextBox::SetHolder(const SagoDataHolder* holder) { |
| 56 | data->tex = holder; |
| 57 | } |
| 58 | |
| 59 | void SagoTextBox::SetText(const char* text) { |
| 60 | data->text = text; |
| 61 | } |
| 62 | |
| 63 | void SagoTextBox::SetText(const std::string& text) { |
| 64 | data->text = text; |
| 65 | } |
| 66 | |
| 67 | void SagoTextBox::SetColor(const SDL_Color& color) { |
| 68 | data->color = color; |
| 69 | } |
| 70 | |
| 71 | void SagoTextBox::SetFont(const char* fontName) { |
| 72 | data->fontName = fontName; |
| 73 | } |
| 74 | |
| 75 | void SagoTextBox::SetFontSize(int fontSize) { |
| 76 | data->fontSize = fontSize; |
| 77 | } |
| 78 | |
| 79 | void SagoTextBox::SetOutline(int outlineSize, const SDL_Color& color) { |
| 80 | data->outline = outlineSize; |
| 81 | data->outlineColor = color; |
| 82 | } |
| 83 | |
| 84 | void SagoTextBox::SetMaxWidth(int width) { |
| 85 | data->maxWidth = width; |
| 86 | } |
| 87 | |
| 88 | const std::string& SagoTextBox::GetText() const { |
| 89 | return data->text; |
| 90 | } |
| 91 | |
| 92 | void SagoTextBox::AppendLineToCache(const std::string& text) { |
| 93 | data->lines.resize(data->lines.size()+1); |
| 94 | SagoTextField& tf = data->lines.back(); |
| 95 | tf.SetHolder(data->tex); |
| 96 | tf.SetFont(data->fontName.c_str()); |
| 97 | tf.SetFontSize(data->fontSize); |
| 98 | tf.SetColor(data->color); |
| 99 | tf.SetOutline(data->outline, data->outlineColor); |
| 100 | tf.SetText(text); |
| 101 | } |
| 102 | |
| 103 | |
| 104 | void SagoTextBox::SplitAndAppendLineToCache(TTF_Font* font, const std::string& text) { |
| 105 | int width = data->maxWidth; |
| 106 | TTF_SizeUTF8(font, text.c_str(),&width, nullptr); |
| 107 | if (data->maxWidth <= 0 || width <= data->maxWidth || text.length() == 1) { |
| 108 | AppendLineToCache(text); |
| 109 | return; |
| 110 | } |
| 111 | std::string::const_iterator splitLocation = text.begin()+1; |
| 112 | bool splitDone = false; |
| 113 | while (!splitDone) { |
| 114 | std::string::const_iterator nextSearchStart = splitLocation+1; |
| 115 | if (nextSearchStart == text.end()) { |
| 116 | splitDone = true; |
| 117 | continue; |
| 118 | } |
| 119 | std::string::const_iterator nextSpace = std::find(nextSearchStart, text.end(), ' '); |
| 120 | std::string attemptSubString(text.begin(), nextSpace); |
| 121 | TTF_SizeUTF8(font, attemptSubString.c_str(),&width, nullptr); |
| 122 | if (width <= data->maxWidth && nextSpace != text.end()) { |
| 123 | splitLocation = nextSpace; |
| 124 | } |
| 125 | else { |
| 126 | splitDone = true; |
| 127 | } |
| 128 | } |
| 129 | if (splitLocation == text.begin()+1) { |
| 130 | splitLocation = text.begin(); |
| 131 | utf8::advance(splitLocation, 1, text.end()); |
| 132 | splitDone = false; |
| 133 | while (!splitDone && splitLocation != text.end()) { |
| 134 | std::string::const_iterator nextSplit = splitLocation; |
| 135 | utf8::advance(nextSplit, 1, text.end()); |
| 136 | std::string attemptSubString(text.begin(), nextSplit); |
| 137 | TTF_SizeUTF8(font, attemptSubString.c_str(), &width, nullptr); |
| 138 | if (width <= data->maxWidth) { |
| 139 | splitLocation = nextSplit; |
| 140 | } |
| 141 | else { |
| 142 | splitDone = true; |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | std::string firstPart(text.begin(), splitLocation); |
| 147 | AppendLineToCache(firstPart); |
| 148 | while (splitLocation != text.end() && *splitLocation == ' ') { |
| 149 | //Trim spaces after an automatic line break. |
| 150 | ++splitLocation; |
| 151 | } |
| 152 | if (splitLocation == text.end()) { |
| 153 | return; |
| 154 | } |
| 155 | std::string secondPart(splitLocation, text.end()); |
| 156 | SplitAndAppendLineToCache(font, secondPart); |
| 157 | } |
| 158 | |
| 159 | void SagoTextBox::UpdateCache() { |
| 160 | if (!data->tex) { |
| 161 | std::cerr << "FATAL: SagoTextBox::UpdateCache - DataHolder not set!\n"; |
| 162 | abort(); |
| 163 | } |
| 164 | TTF_Font* font = data->tex->getFontPtr(data->fontName, data->fontSize); |
| 165 | const char delim = '\n'; |
| 166 | const std::string& s = data->text; |
| 167 | auto start = 0U; |
| 168 | auto end = s.find(delim); |
| 169 | data->lines.clear(); |
| 170 | while (end != std::string::npos) { |
| 171 | const std::string& theSubString = s.substr(start, end - start); |
| 172 | SplitAndAppendLineToCache(font, theSubString); |
| 173 | start = end + 1; |
| 174 | end = s.find(delim, start); |
| 175 | } |
| 176 | SplitAndAppendLineToCache(font, s.substr(start, end)); |
| 177 | data->renderedText = data->text; |
| 178 | } |
| 179 | |
| 180 | void SagoTextBox::Draw(SDL_Renderer* target, int x, int y, SagoTextField::Alignment alignment, SagoLogicalResize* resize ) { |
| 181 | if (data->text != data->renderedText) { |
| 182 | UpdateCache(); |
| 183 | } |
| 184 | TTF_Font* font = data->tex->getFontPtr(data->fontName, data->fontSize); |
| 185 | int lineSkip = TTF_FontLineSkip(font); |
| 186 | for (size_t i = 0; i < data->lines.size(); ++i) { |
| 187 | data->lines[i].Draw(target, x, y+i*lineSkip, alignment, sago::SagoTextField::VerticalAlignment::top, resize); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | } //namespace sago |