| 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 "SagoTextField.hpp" |
| 26 | #include <iostream> |
| 27 | #include <SDL_ttf.h> |
| 28 | |
| 29 | namespace sago { |
| 30 | |
| 31 | class OutlineHandler { |
| 32 | TTF_Font* font; |
| 33 | int originalOutline = 0; |
| 34 | int targetOutline; |
| 35 | bool doChange = false; |
| 36 | public: |
| 37 | OutlineHandler(TTF_Font* font, int outline) : font{font}, targetOutline{outline} { |
| 38 | originalOutline = TTF_GetFontOutline(font); |
| 39 | if (originalOutline == targetOutline) { |
| 40 | return; |
| 41 | } |
| 42 | doChange = true; |
| 43 | TTF_SetFontOutline(font, targetOutline); |
| 44 | }; |
| 45 | |
| 46 | void reset() { |
| 47 | if (doChange) { |
| 48 | TTF_SetFontOutline(font,originalOutline); |
| 49 | doChange = false; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | ~OutlineHandler() { |
| 54 | reset(); |
| 55 | } |
| 56 | private: |
| 57 | OutlineHandler(const OutlineHandler& orig) = delete; |
| 58 | OutlineHandler& operator=(const OutlineHandler& base) = delete; |
| 59 | }; |
| 60 | |
| 61 | struct SagoTextField::SagoTextFieldData { |
| 62 | const sago::SagoDataHolder* tex = nullptr; |
| 63 | SDL_Surface* textSurface = nullptr; |
| 64 | SDL_Texture* texture = nullptr; |
| 65 | SDL_Surface* outlineTextSurface = nullptr; |
| 66 | SDL_Texture* outlineTexture = nullptr; |
| 67 | std::string fontName = "freeserif"; |
| 68 | SDL_Color color = { 255, 255, 255, 0 }; |
| 69 | SDL_Color outlineColor = { 255, 255, 0, 0 }; |
| 70 | int fontSize = 16; |
| 71 | int outline = 0; |
| 72 | std::string text = ""; |
| 73 | std::string renderedText = ""; |
| 74 | Uint64 renderedVersion = 0; |
| 75 | }; |
| 76 | |
| 77 | SagoTextField::SagoTextField() { |
| 78 | data = new SagoTextFieldData(); |
| 79 | } |
| 80 | |
| 81 | SagoTextField::SagoTextField(SagoTextField&& o) noexcept { |
| 82 | data = o.data; |
| 83 | o.data = nullptr; |
| 84 | } |
| 85 | |
| 86 | SagoTextField& SagoTextField::CopyFrom(const SagoTextField& base) { |
| 87 | ClearCache(); |
| 88 | try { |
| 89 | *data = *(base.data); |
| 90 | //Copy all data but do not reuse the cache as it would result in a double free |
| 91 | data->outlineTextSurface = nullptr; |
| 92 | data->outlineTexture = nullptr; |
| 93 | data->textSurface = nullptr; |
| 94 | data->texture = nullptr; |
| 95 | return *this; |
| 96 | } |
| 97 | catch (...) { |
| 98 | delete data; |
| 99 | throw; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | SagoTextField::~SagoTextField() { |
| 104 | if (!data) { |
| 105 | return; |
| 106 | } |
| 107 | ClearCache(); |
| 108 | delete data; |
| 109 | } |
| 110 | |
| 111 | void SagoTextField::SetHolder(const SagoDataHolder* holder) { |
| 112 | data->tex = holder; |
| 113 | } |
| 114 | |
| 115 | void SagoTextField::SetText(const char* text) { |
| 116 | data->text = text; |
| 117 | } |
| 118 | |
| 119 | void SagoTextField::SetText(const std::string& text) { |
| 120 | data->text = text; |
| 121 | } |
| 122 | |
| 123 | void SagoTextField::SetColor(const SDL_Color& color) { |
| 124 | data->color = color; |
| 125 | } |
| 126 | |
| 127 | void SagoTextField::SetFont(const char* fontName) { |
| 128 | data->fontName = fontName; |
| 129 | } |
| 130 | |
| 131 | void SagoTextField::SetFontSize(int fontSize) { |
| 132 | data->fontSize = fontSize; |
| 133 | } |
| 134 | |
| 135 | void SagoTextField::SetOutline(int outlineSize, const SDL_Color& color) { |
| 136 | data->outline = outlineSize; |
| 137 | data->outlineColor = color; |
| 138 | } |
| 139 | |
| 140 | const std::string& SagoTextField::GetText() const { |
| 141 | return data->text; |
| 142 | } |
| 143 | |
| 144 | void SagoTextField::ClearCache() { |
| 145 | if (data->texture) { |
| 146 | SDL_DestroyTexture(data->texture); |
| 147 | data->texture = nullptr; |
| 148 | } |
| 149 | if (data->textSurface) { |
| 150 | SDL_FreeSurface(data->textSurface); |
| 151 | data->textSurface = nullptr; |
| 152 | } |
| 153 | if (data->outlineTexture) { |
| 154 | SDL_DestroyTexture(data->outlineTexture); |
| 155 | data->outlineTexture = nullptr; |
| 156 | } |
| 157 | if (data->outlineTextSurface) { |
| 158 | SDL_FreeSurface(data->outlineTextSurface); |
| 159 | data->outlineTextSurface = nullptr; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | void SagoTextField::UpdateCache(SDL_Renderer* target) { |
| 164 | if (!data->tex) { |
| 165 | std::cerr << "FATAL: DataHolder not set!\n"; |
| 166 | abort(); |
| 167 | } |
| 168 | ClearCache(); |
| 169 | TTF_Font* font = data->tex->getFontPtr(data->fontName, data->fontSize); |
| 170 | data->textSurface = TTF_RenderUTF8_Blended (font, data->text.c_str(), data->color); |
| 171 | data->texture = SDL_CreateTextureFromSurface(target, data->textSurface); |
| 172 | if (data->outline > 0) { |
| 173 | OutlineHandler oh(font, data->outline); |
| 174 | data->outlineTextSurface = TTF_RenderUTF8_Blended (font, data->text.c_str(), data->outlineColor); |
| 175 | data->outlineTexture = SDL_CreateTextureFromSurface(target, data->outlineTextSurface); |
| 176 | oh.reset(); |
| 177 | } |
| 178 | data->renderedText = data->text; |
| 179 | data->renderedVersion = data->tex->getVersion(); |
| 180 | } |
| 181 | |
| 182 | void SagoTextField::GetRenderedSize(const char* text, int* w, int* h) { |
| 183 | TTF_Font* font = data->tex->getFontPtr(data->fontName, data->fontSize); |
| 184 | int ret = TTF_SizeUTF8(font, text, w, h); |
| 185 | if (ret) { |
| 186 | if (w) { |
| 187 | *w = 0; |
| 188 | } |
| 189 | if (h) { |
| 190 | *h = 0; |
| 191 | } |
| 192 | std::cerr << "GetRenderedSize failed to find size of " << text << ". Error code: " << ret << "\n"; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | void SagoTextField::Draw(SDL_Renderer* target, int x, int y, Alignment alignment, VerticalAlignment verticalAlignment, SagoLogicalResize* resize) { |
| 197 | if (data->text.empty()) { |
| 198 | return; |
| 199 | } |
| 200 | if (data->text != data->renderedText || data->renderedVersion != data->tex->getVersion()) { |
| 201 | UpdateCache(target); |
| 202 | } |
| 203 | if (!data->texture) { |
| 204 | return; |
| 205 | } |
| 206 | int texW = 0; |
| 207 | int texH = 0; |
| 208 | SDL_QueryTexture(data->texture, NULL, NULL, &texW, &texH); |
| 209 | if (alignment == Alignment::center) { |
| 210 | x -= texW/2; |
| 211 | } |
| 212 | if (alignment == Alignment::right) { |
| 213 | x -= texW; |
| 214 | } |
| 215 | if (verticalAlignment == VerticalAlignment::center) { |
| 216 | y -= texH/2; |
| 217 | } |
| 218 | if (verticalAlignment == VerticalAlignment::bottom) { |
| 219 | y -= texH; |
| 220 | } |
| 221 | SDL_Rect dstrect = { x, y, texW, texH }; |
| 222 | if (data->outlineTexture) { |
| 223 | int outlineTexW = 0; |
| 224 | int outlineTexH = 0; |
| 225 | SDL_QueryTexture(data->outlineTexture, NULL, NULL, &outlineTexW, &outlineTexH); |
| 226 | SDL_Rect dstrectOutline = { x-(data->outline), y-(data->outline), outlineTexW, outlineTexH }; |
| 227 | if (resize) { |
| 228 | resize->LogicalToPhysical(dstrectOutline); |
| 229 | } |
| 230 | SDL_RenderCopy(target, data->outlineTexture, NULL, &dstrectOutline); |
| 231 | } |
| 232 | if (resize) { |
| 233 | resize->LogicalToPhysical(dstrect); |
| 234 | } |
| 235 | SDL_RenderCopy(target, data->texture, NULL, &dstrect); |
| 236 | } |
| 237 | |
| 238 | } //namespace sago |