MCPcopy Create free account
hub / github.com/diasurgical/devilution / RenderUTF8_Solid_Wrapped

Function RenderUTF8_Solid_Wrapped

SourceX/DiabloUI/ttf_render_wrapped.cpp:22–173  ·  view source on GitHub ↗

Based on SDL 2.0.12 TTF_RenderUTF8_Blended_Wrapped

Source from the content-addressed store, hash-verified

20
21// Based on SDL 2.0.12 TTF_RenderUTF8_Blended_Wrapped
22SDL_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 */

Callers 1

DrawTTFFunction · 0.85

Calls 3

CharacterIsDelimiterFunction · 0.85
SDLC_SetColorKeyFunction · 0.85
SDL_LogFunction · 0.85

Tested by

no test coverage detected