1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
4
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
8
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
12
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
20*/
21#include <SDL3/SDL_test.h>
22
23#define UTF8_IsTrailingByte(c) ((c) >= 0x80 && (c) <= 0xBF)
24
25int FONT_CHARACTER_SIZE = SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE;
26
27bool SDLTest_DrawCharacter(SDL_Renderer *renderer, float x, float y, Uint32 c)
28{
29 char str[5];
30 char *ptr = SDL_UCS4ToUTF8(c, str);
31 *ptr = '\0';
32 return SDL_RenderDebugText(renderer, x, y, str);
33}
34
35bool SDLTest_DrawString(SDL_Renderer *renderer, float x, float y, const char *s)
36{
37 return SDL_RenderDebugText(renderer, x, y, s);
38}
39
40SDLTest_TextWindow *SDLTest_TextWindowCreate(float x, float y, float w, float h)
41{
42 SDLTest_TextWindow *textwin = (SDLTest_TextWindow *)SDL_malloc(sizeof(*textwin));
43
44 if (!textwin) {
45 return NULL;
46 }
47
48 textwin->rect.x = x;
49 textwin->rect.y = y;
50 textwin->rect.w = w;
51 textwin->rect.h = h;
52 textwin->current = 0;
53 textwin->numlines = (int)SDL_ceilf(h / FONT_LINE_HEIGHT);
54 textwin->lines = (char **)SDL_calloc(textwin->numlines, sizeof(*textwin->lines));
55 if (!textwin->lines) {
56 SDL_free(textwin);
57 return NULL;
58 }
59 return textwin;
60}
61
62void SDLTest_TextWindowDisplay(SDLTest_TextWindow *textwin, SDL_Renderer *renderer)
63{
64 int i;
65 float y;
66
67 for (y = textwin->rect.y, i = 0; i < textwin->numlines; ++i, y += FONT_LINE_HEIGHT) {
68 if (textwin->lines[i]) {
69 SDLTest_DrawString(renderer, textwin->rect.x, y, textwin->lines[i]);
70 }
71 }
72}
73
74void SDLTest_TextWindowAddText(SDLTest_TextWindow *textwin, const char *fmt, ...)
75{
76 char text[1024];
77 va_list ap;
78
79 va_start(ap, fmt);
80 (void)SDL_vsnprintf(text, sizeof(text), fmt, ap);
81 va_end(ap);
82
83 SDLTest_TextWindowAddTextWithLength(textwin, text, SDL_strlen(text));
84}
85
86void SDLTest_TextWindowAddTextWithLength(SDLTest_TextWindow *textwin, const char *text, size_t len)
87{
88 size_t existing;
89 bool newline = false;
90 char *line;
91
92 if (len > 0 && text[len - 1] == '\n') {
93 --len;
94 newline = true;
95 }
96
97 if (textwin->lines[textwin->current]) {
98 existing = SDL_strlen(textwin->lines[textwin->current]);
99 } else {
100 existing = 0;
101 }
102
103 if (*text == '\b') {
104 if (existing) {
105 while (existing > 1 && UTF8_IsTrailingByte((Uint8)textwin->lines[textwin->current][existing - 1])) {
106 --existing;
107 }
108 --existing;
109 textwin->lines[textwin->current][existing] = '\0';
110 } else if (textwin->current > 0) {
111 SDL_free(textwin->lines[textwin->current]);
112 textwin->lines[textwin->current] = NULL;
113 --textwin->current;
114 }
115 return;
116 }
117
118 line = (char *)SDL_realloc(textwin->lines[textwin->current], existing + len + 1);
119 if (line) {
120 SDL_memcpy(&line[existing], text, len);
121 line[existing + len] = '\0';
122 textwin->lines[textwin->current] = line;
123 if (newline) {
124 if (textwin->current == textwin->numlines - 1) {
125 SDL_free(textwin->lines[0]);
126 SDL_memmove(&textwin->lines[0], &textwin->lines[1], (textwin->numlines - 1) * sizeof(textwin->lines[1]));
127 textwin->lines[textwin->current] = NULL;
128 } else {
129 ++textwin->current;
130 }
131 }
132 }
133}
134
135void SDLTest_TextWindowClear(SDLTest_TextWindow *textwin)
136{
137 int i;
138
139 for (i = 0; i < textwin->numlines; ++i) {
140 if (textwin->lines[i]) {
141 SDL_free(textwin->lines[i]);
142 textwin->lines[i] = NULL;
143 }
144 }
145 textwin->current = 0;
146}
147
148void SDLTest_TextWindowDestroy(SDLTest_TextWindow *textwin)
149{
150 if (textwin) {
151 SDLTest_TextWindowClear(textwin);
152 SDL_free(textwin->lines);
153 SDL_free(textwin);
154 }
155}
156
157void SDLTest_CleanupTextDrawing(void)
158{
159}
160