Based on SDL 2.0.12 TTF_RenderUTF8_Blended_Wrapped
| 20 | |
| 21 | // Based on SDL 2.0.12 TTF_RenderUTF8_Blended_Wrapped |
| 22 | SDL_Surface *RenderUTF8_Solid_Wrapped(TTF_Font *font, const char *text, SDL_Color fg, Uint32 wrapLength, TextAlignment x_align) |
| 23 | { |
| 24 | int width, height; |
| 25 | SDL_Surface *textbuf; |
| 26 | const int lineSpace = 2; |
| 27 | char *str, **strLines; |
| 28 | |
| 29 | /* Get the dimensions of the text surface */ |
| 30 | if ((TTF_SizeUTF8(font, text, &width, &height) < 0) || !width) { |
| 31 | TTF_SetError("Text has zero width"); |
| 32 | return nullptr; |
| 33 | } |
| 34 | |
| 35 | std::size_t numLines = 1; |
| 36 | str = nullptr; |
| 37 | strLines = nullptr; |
| 38 | if (wrapLength > 0 && *text) { |
| 39 | const char *wrapDelims = " \t\r\n"; |
| 40 | int w, h; |
| 41 | char *spot, *tok, *next_tok, *end; |
| 42 | char delim; |
| 43 | size_t str_len = SDL_strlen(text); |
| 44 | |
| 45 | numLines = 0; |
| 46 | |
| 47 | str = SDL_stack_alloc(char, str_len + 1); |
| 48 | if (str == nullptr) { |
| 49 | TTF_SetError("Out of memory"); |
| 50 | return nullptr; |
| 51 | } |
| 52 | |
| 53 | SDL_strlcpy(str, text, str_len + 1); |
| 54 | tok = str; |
| 55 | end = str + str_len; |
| 56 | do { |
| 57 | strLines = (char **)SDL_realloc(strLines, (numLines + 1) * sizeof(*strLines)); |
| 58 | if (!strLines) { |
| 59 | TTF_SetError("Out of memory"); |
| 60 | return nullptr; |
| 61 | } |
| 62 | strLines[numLines++] = tok; |
| 63 | |
| 64 | /* Look for the end of the line */ |
| 65 | if ((spot = SDL_strchr(tok, '\r')) != nullptr || (spot = SDL_strchr(tok, '\n')) != nullptr) { |
| 66 | if (*spot == '\r') { |
| 67 | ++spot; |
| 68 | } |
| 69 | if (*spot == '\n') { |
| 70 | ++spot; |
| 71 | } |
| 72 | } else { |
| 73 | spot = end; |
| 74 | } |
| 75 | next_tok = spot; |
| 76 | |
| 77 | /* Get the longest string that will fit in the desired space */ |
| 78 | for (;;) { |
| 79 | /* Strip trailing whitespace */ |
no test coverage detected