ascii-chat
Binary v0.16.50-198-g78f51665f
Library lib/v0.3.2-2240-g78f51665f
Video chat in your terminal
Loading...
Searching...
No Matches
palette.c
Go to the documentation of this file.
1
7
8/* Storage capacities in elements; text buffers include the terminating NUL. */
9#define VIDEO_ASCII_PALETTE_OLD_LOCALE_BUFFER_SIZE 256
10
12#include <recast/defer.h>
14#include <stdbool.h>
15#include <stddef.h>
16#include <string.h>
17#include <locale.h>
18#include <stdlib.h>
19#include <wchar.h>
20#ifndef _WIN32
21#include <langinfo.h>
22#include <unistd.h>
23#endif
24
26#include <ascii-chat/common.h>
27#include <libplatform/terminal.h>
28#include <ascii-chat/util/utf8.h> // For UTF-8 character width detection
29
30/* Default palette constants for legacy functions */
32const size_t DEFAULT_ASCII_PALETTE_LEN = 23; // strlen(PALETTE_CHARS_STANDARD)
33
34// Built-in palette definitions
35static const palette_def_t builtin_palettes[PALETTE_COUNT] = {
36 [PALETTE_STANDARD] = {.name = "standard", .chars = PALETTE_CHARS_STANDARD, .length = 23, .requires_utf8 = false, .is_validated = true},
37 [PALETTE_BLOCKS] = {.name = "blocks", .chars = PALETTE_CHARS_BLOCKS, .length = 11, .requires_utf8 = true, .is_validated = true},
38 [PALETTE_DIGITAL] = {.name = "digital", .chars = PALETTE_CHARS_DIGITAL, .length = 10, .requires_utf8 = true, .is_validated = true},
39 [PALETTE_MINIMAL] = {.name = "minimal", .chars = PALETTE_CHARS_MINIMAL, .length = 8, .requires_utf8 = false, .is_validated = true},
40 [PALETTE_COOL] = {.name = "cool", .chars = PALETTE_CHARS_COOL, .length = 11, .requires_utf8 = true, .is_validated = true},
41 // PALETTE_CUSTOM is handled specially - no predefined entry
42};
43
44// Get a built-in palette definition
46 if (type >= PALETTE_COUNT || type == PALETTE_CUSTOM || type == PALETTE_UNSET) {
47 return NULL;
48 }
49 return &builtin_palettes[type];
50}
51
52// Check if a palette string contains UTF-8 characters
53bool palette_requires_utf8_encoding(const char *chars, size_t len) {
54 // Handle NULL or empty string
55 if (!chars || len == 0) {
56 return false;
57 }
58
59 for (size_t i = 0; i < len; i++) {
60 // Any byte with high bit set indicates UTF-8
61 if ((unsigned char)chars[i] >= 128) {
62 return true;
63 }
64 }
65 return false;
66}
67
68// Validate UTF-8 character sequences and terminal width
69bool validate_palette_chars(const char *chars, size_t len) {
70 if (!chars || len == 0) {
71 SET_ERRNO(ERROR_INVALID_PARAM, "Palette validation failed: empty or NULL palette");
72 return false;
73 }
74
75 if (len > 256) {
76 SET_ERRNO(ERROR_INVALID_PARAM, "Palette validation failed: palette too long (%zu chars, max 256)", len);
77 return false;
78 }
79
80 // Decode UTF-8 string into codepoints
81 uint32_t codepoints[256];
82 size_t codepoint_count = utf8_to_codepoints(chars, codepoints, 256);
83
84 if (codepoint_count == SIZE_MAX) {
85 SET_ERRNO(ERROR_INVALID_PARAM, "Palette validation failed: invalid UTF-8 sequence");
86 return false;
87 }
88
89 // Validate each codepoint
90 for (size_t i = 0; i < codepoint_count; i++) {
91 uint32_t cp = codepoints[i];
92
93 // Check for control characters (except tab)
94 if (cp < 32 && cp != '\t') {
95 SET_ERRNO(ERROR_INVALID_PARAM, "Palette validation failed: control character at position %zu", i);
96 return false;
97 }
98
99 // Convert codepoint to UTF-8 and check display width
100 // For ASCII characters, this is straightforward
101 // For multi-byte UTF-8, we need to check the display width
102 if (cp < 128) {
103 // ASCII character - always width 1
104 continue;
105 } else {
106 // Non-ASCII: use utf8_display_width to check character width
107 // Create a temporary buffer with the UTF-8 encoded character
108 uint8_t utf8_buf[ASCII_CHAT_UTF8_BUFFER_SIZE] = {0};
109
110 // Encode codepoint to UTF-8 (simple for demo, should use proper encoder)
111 if (cp <= 0x7F) {
112 utf8_buf[0] = (uint8_t)cp;
113 } else if (cp <= 0x7FF) {
114 utf8_buf[0] = (uint8_t)(0xC0 | (cp >> 6));
115 utf8_buf[1] = (uint8_t)(0x80 | (cp & 0x3F));
116 } else if (cp <= 0xFFFF) {
117 utf8_buf[0] = (uint8_t)(0xE0 | (cp >> 12));
118 utf8_buf[1] = (uint8_t)(0x80 | ((cp >> 6) & 0x3F));
119 utf8_buf[2] = (uint8_t)(0x80 | (cp & 0x3F));
120 } else if (cp <= 0x10FFFF) {
121 utf8_buf[0] = (uint8_t)(0xF0 | (cp >> 18));
122 utf8_buf[1] = (uint8_t)(0x80 | ((cp >> 12) & 0x3F));
123 utf8_buf[2] = (uint8_t)(0x80 | ((cp >> 6) & 0x3F));
124 utf8_buf[3] = (uint8_t)(0x80 | (cp & 0x3F));
125 } else {
126 SET_ERRNO(ERROR_INVALID_PARAM, "Palette validation failed: invalid codepoint at position %zu", i);
127 return false;
128 }
129
130 // Check character width - allow width 1 and 2 (for emoji and wide characters)
131 int width = utf8_display_width((const char *)utf8_buf);
132 if (width < 0 || width > 2) {
133 SET_ERRNO(ERROR_INVALID_PARAM, "Palette validation failed: character at position %zu has invalid width %d (must be 1 or 2)", i, width);
134 return false;
135 }
136 }
137 }
138
139 log_debug("Palette validation successful: %zu characters validated", codepoint_count);
140 return true;
141}
142
143// Detect client UTF-8 support from environment
145 if (!caps) {
146 return false;
147 }
148
149 // Initialize structure
150 SAFE_MEMSET(caps, sizeof(utf8_capabilities_t), 0, sizeof(utf8_capabilities_t));
151
152 // Check environment variables
153 const char *term = SAFE_GETENV("TERM");
154
155 // Store terminal type
156 if (term) {
157 SAFE_STRNCPY(caps->terminal_type, term, sizeof(caps->terminal_type));
158 }
159
160 // Use platform-specific UTF-8 detection from platform abstraction layer
162
163 if (caps->utf8_support) {
164 SAFE_STRNCPY(caps->locale_encoding, "UTF-8", sizeof(caps->locale_encoding));
165 } else {
166 // Try to detect encoding via locale
167 char *current_locale = setlocale(LC_CTYPE, NULL);
168 char *old_locale = NULL;
169 if (current_locale) {
170 SAFE_STRDUP(old_locale, current_locale);
171 }
172 if (setlocale(LC_CTYPE, "")) {
173#ifndef _WIN32
174 const char *codeset = nl_langinfo(CODESET);
175 if (codeset) {
176 SAFE_STRNCPY(caps->locale_encoding, codeset, sizeof(caps->locale_encoding));
177 }
178#else
179 // Windows may not have locale set but still support UTF-8
180 SAFE_STRNCPY(caps->locale_encoding, "CP1252", sizeof(caps->locale_encoding));
181#endif
182 // Restore old locale
183 if (old_locale) {
184 (void)setlocale(LC_CTYPE, old_locale);
185 }
186 }
187 // Always free old_locale regardless of setlocale success
188 if (old_locale) {
189 SAFE_FREE(old_locale);
190 }
191 }
192
193 // Check for known UTF-8 supporting terminals
194 if (term) {
195 const char *utf8_terminals[] = {"xterm-256color", "screen-256color", "tmux-256color", "alacritty", "kitty", "iterm", "iterm2", "gnome-terminal", "konsole", "terminology", NULL};
196
197 for (int i = 0; utf8_terminals[i]; i++) {
198 if (strstr(term, utf8_terminals[i])) {
199 caps->utf8_support = true;
200 break;
201 }
202 }
203 }
204
205 log_debug("UTF-8 support detection: %s (term=%s, encoding=%s)", caps->utf8_support ? "supported" : "not supported", caps->terminal_type[0] ? caps->terminal_type : "unknown",
206 caps->locale_encoding[0] ? caps->locale_encoding : "unknown");
207
208 return caps->utf8_support;
209}
210
211// Select a compatible palette based on client capabilities
213 const palette_def_t *palette = get_builtin_palette(requested);
214
215 // Custom palettes are validated separately
216 if (requested == PALETTE_CUSTOM) {
217 return PALETTE_CUSTOM;
218 }
219
220 if (!palette) {
221 log_warn("Invalid palette type %d, falling back to standard", requested);
222 return PALETTE_STANDARD;
223 }
224
225 // If palette requires UTF-8 but client doesn't support it, find fallback
226 if (palette->requires_utf8 && !client_utf8) {
227 log_warn("Client doesn't support UTF-8, falling back from %s", palette->name);
228
229 // Fallback hierarchy
230 switch (requested) {
231 case PALETTE_BLOCKS:
232 case PALETTE_DIGITAL:
233 case PALETTE_COOL:
234 default:
235 return PALETTE_STANDARD; // ASCII equivalent
236 }
237 }
238
239 return requested; // Compatible, use as requested
240}
241
242// Apply palette configuration for client-side initialization only (no global cache)
243int apply_palette_config(palette_type_t type, const char *custom_chars) {
244 // This function is now used only for client-side initialization
245 // Server uses initialize_client_palette() for per-client palettes
246
247 log_debug("Client palette config: type=%d, custom_chars=%s", type, custom_chars ? custom_chars : "(none)");
248
249 // Just validate the palette - no global state changes
250 if (type == PALETTE_CUSTOM) {
251 if (!custom_chars || strlen(custom_chars) == 0) {
252 SET_ERRNO(ERROR_INVALID_PARAM, "Custom palette requested but no characters provided");
253 return -1;
254 }
255
256 if (!validate_palette_chars(custom_chars, strlen(custom_chars))) {
257 SET_ERRNO(ERROR_INVALID_PARAM, "Custom palette validation failed");
258 return -1;
259 }
260 } else {
261 const palette_def_t *palette = get_builtin_palette(type);
262 if (!palette) {
263 SET_ERRNO(ERROR_INVALID_PARAM, "Invalid palette type: %d", type);
264 return -1;
265 }
266 }
267
268 return 0; // Validation successful, no global state to update
269}
270
271// Build a per-client luminance palette without affecting global cache
272int build_client_luminance_palette(const char *palette_chars, size_t palette_len, char luminance_mapping[ASCII_LUMINANCE_LEVELS]) {
273 if (!palette_chars || palette_len == 0 || !luminance_mapping) {
274 SET_ERRNO(ERROR_INVALID_PARAM, "Invalid parameters for client luminance palette");
275 return -1;
276 }
277
278 // Map 256 luminance values to palette indices for this specific client
279 for (int i = 0; i < 256; i++) {
280 // Linear mapping with proper rounding
281 size_t palette_index = (i * (palette_len - 1) + 127) / 255;
282 if (palette_index >= palette_len) {
283 palette_index = palette_len - 1;
284 }
285 luminance_mapping[i] = palette_chars[palette_index];
286 }
287
288 return 0;
289}
290
291// Initialize a client's palette cache from their capabilities
292int initialize_client_palette(palette_type_t palette_type, const char *custom_chars, char client_palette_chars[ASCII_PALETTE_BUFFER_SIZE], size_t *client_palette_len,
293 char client_luminance_palette[ASCII_LUMINANCE_LEVELS]) {
294 const palette_def_t *palette = NULL;
295 const char *chars_to_use = NULL;
296 size_t len_to_use = 0;
297
298 if (palette_type == PALETTE_CUSTOM) {
299 if (!custom_chars) {
300 SET_ERRNO(ERROR_INVALID_PARAM, "Client requested custom palette but custom_chars is NULL");
301 return -1;
302 }
303
304 len_to_use = strlen(custom_chars);
305 if (len_to_use == 0) {
306 SET_ERRNO(ERROR_INVALID_PARAM, "Client requested custom palette but custom_chars is empty");
307 return -1;
308 }
309 if (len_to_use >= 256) {
310 SET_ERRNO(ERROR_INVALID_PARAM, "Client custom palette too long: %zu chars", len_to_use);
311 return -1;
312 }
313
314 // Validate custom palette
315 if (!validate_palette_chars(custom_chars, len_to_use)) {
316 SET_ERRNO(ERROR_INVALID_PARAM, "Client custom palette validation failed");
317 return -1;
318 }
319
320 chars_to_use = custom_chars;
321 } else {
322 palette = get_builtin_palette(palette_type);
323 if (!palette) {
324 SET_ERRNO(ERROR_INVALID_PARAM, "Invalid client palette type: %d", palette_type);
325 return -1;
326 }
327
328 chars_to_use = palette->chars;
329 len_to_use = strlen(palette->chars); // Use actual byte count for UTF-8, not character count
330
331 // Skip validation for built-in palettes since they're already validated
332 log_dev("Using built-in palette: %s, chars='%s', char_count=%zu, byte_len=%zu", palette->name, chars_to_use, palette->length, len_to_use);
333 }
334
335 // Copy palette to client cache
336 SAFE_MEMCPY(client_palette_chars, len_to_use, chars_to_use, len_to_use);
337 client_palette_chars[len_to_use] = '\0';
338 *client_palette_len = len_to_use;
339
340 // Build client-specific luminance mapping
341 if (build_client_luminance_palette(chars_to_use, len_to_use, client_luminance_palette) != 0) {
342 SET_ERRNO(ERROR_INVALID_STATE, "Failed to build client luminance palette");
343 return -1;
344 }
345
346 log_dev("Initialized client palette: type=%d, %zu chars, first_char='%c', last_char='%c'", palette_type, len_to_use, chars_to_use[0], chars_to_use[len_to_use - 1]);
347
348 return 0;
349}
350
351/* UTF-8 Palette Functions Implementation */
352
353// Create a UTF-8 palette structure from a string
354utf8_palette_t *utf8_palette_create(const char *palette_string) {
355 if (!palette_string || *palette_string == '\0') {
356 return NULL;
357 }
358
359 utf8_palette_t *palette;
360 palette = SAFE_MALLOC(sizeof(utf8_palette_t), utf8_palette_t *);
361 defer_named(release_palette, SAFE_FREE(palette));
362
363 // Count UTF-8 characters (not bytes)
364 size_t char_count = 0;
365 const char *p = palette_string;
366 size_t total_bytes = strlen(palette_string);
367
368 // First pass: count characters
369 size_t bytes_processed = 0;
370 while (bytes_processed < total_bytes) {
371 int bytes = 1;
372 unsigned char c = (unsigned char)p[0];
373
374 if ((c & 0x80) == 0) {
375 bytes = 1; // ASCII
376 } else if ((c & 0xE0) == 0xC0) {
377 bytes = 2; // 2-byte UTF-8
378 } else if ((c & 0xF0) == 0xE0) {
379 bytes = 3; // 3-byte UTF-8
380 } else if ((c & 0xF8) == 0xF0) {
381 bytes = 4; // 4-byte UTF-8
382 }
383
384 // Verify we have enough bytes left
385 if (bytes_processed + bytes > total_bytes) {
386 break;
387 }
388
389 p += bytes;
390 bytes_processed += bytes;
391 char_count++;
392 }
393
394 // Validate we got at least one character
395 if (char_count == 0) {
396 SET_ERRNO(ERROR_INVALID_PARAM, "Palette string contains no valid UTF-8 characters");
397 return NULL;
398 }
399
400 // Allocate character array
401 palette->chars = SAFE_MALLOC(char_count * sizeof(utf8_char_info_t), utf8_char_info_t *);
402 palette->raw_string = SAFE_MALLOC(total_bytes + 1, char *);
403 defer_named(release_chars, SAFE_FREE(palette->chars));
404 defer_named(release_raw, SAFE_FREE(palette->raw_string));
405 // Explicit NULL checks to satisfy static analyzer (SAFE_MALLOC calls FATAL on failure)
406 if (palette->chars == NULL || palette->raw_string == NULL) {
407 SET_ERRNO(ERROR_MEMORY, "Failed to allocate palette character array");
408 return NULL;
409 }
410
411 SAFE_MEMCPY(palette->raw_string, total_bytes + 1, palette_string, total_bytes + 1);
412 palette->char_count = char_count;
413 palette->total_bytes = total_bytes; // Use strlen() value
414
415 // Second pass: parse characters
416 p = palette_string;
417 size_t char_idx = 0;
418 bytes_processed = 0;
419
420 // Set locale for wcwidth - save a copy of the old locale
421 char old_locale[VIDEO_ASCII_PALETTE_OLD_LOCALE_BUFFER_SIZE] = {0};
422 char *current_locale = setlocale(LC_CTYPE, NULL);
423 if (current_locale) {
424 SAFE_STRNCPY(old_locale, current_locale, sizeof(old_locale));
425 }
426 (void)setlocale(LC_CTYPE, "");
427
428 while (char_idx < char_count && bytes_processed < total_bytes) {
429 utf8_char_info_t *char_info = &palette->chars[char_idx];
430
431 // Determine UTF-8 byte length
432 unsigned char c = (unsigned char)*p;
433 int bytes = 1;
434
435 if ((c & 0x80) == 0) {
436 bytes = 1;
437 } else if ((c & 0xE0) == 0xC0) {
438 bytes = 2;
439 } else if ((c & 0xF0) == 0xE0) {
440 bytes = 3;
441 } else if ((c & 0xF8) == 0xF0) {
442 bytes = 4;
443 }
444
445 // Verify we have enough bytes left
446 if (bytes_processed + bytes > total_bytes) {
447 break;
448 }
449
450 // Copy bytes and null-terminate
451 SAFE_MEMCPY(char_info->bytes, bytes, p, bytes);
452 if (bytes < 4) {
453 SAFE_MEMSET(char_info->bytes + bytes, 4 - bytes, 0, 4 - bytes);
454 }
455 char_info->byte_len = bytes;
456
457 // Get display width using utf8_display_width
458 int width = utf8_display_width_n(p, bytes);
459 char_info->display_width = (width > 0 && width <= 2) ? width : 1;
460
461 p += bytes;
462 bytes_processed += bytes;
463 char_idx++;
464 }
465
466 // Restore locale
467 if (old_locale[0] != '\0') {
468 (void)setlocale(LC_CTYPE, old_locale);
469 }
470
471 defer_cancel(release_raw);
472 defer_cancel(release_chars);
473 defer_cancel(release_palette);
474 return palette;
475}
476
477// Destroy a UTF-8 palette structure
479 if (palette) {
480 SAFE_FREE(palette->chars);
481 SAFE_FREE(palette->raw_string);
482 SAFE_FREE(palette);
483 }
484}
485
486// Get the nth character from the palette
488 if (!palette || index >= palette->char_count) {
489 return NULL;
490 }
491 return &palette->chars[index];
492}
493
494// Get the number of characters in the palette
496 if (!palette) {
497 return 0;
498 }
499 return palette->char_count;
500}
501
502// Check if palette contains a specific UTF-8 character
503bool utf8_palette_contains_char(const utf8_palette_t *palette, const char *utf8_char, size_t char_bytes) {
504 if (!palette || !utf8_char || char_bytes == 0 || char_bytes > 4) {
505 return false;
506 }
507
508 for (size_t i = 0; i < palette->char_count; i++) {
509 const utf8_char_info_t *char_info = &palette->chars[i];
510 if (char_info->byte_len == char_bytes && memcmp(char_info->bytes, utf8_char, char_bytes) == 0) {
511 return true;
512 }
513 }
514
515 return false;
516}
517
518// Find the index of a UTF-8 character in the palette
519size_t utf8_palette_find_char_index(const utf8_palette_t *palette, const char *utf8_char, size_t char_bytes) {
520 if (!palette || !utf8_char || char_bytes == 0 || char_bytes > 4) {
521 return (size_t)-1;
522 }
523
524 for (size_t i = 0; i < palette->char_count; i++) {
525 const utf8_char_info_t *char_info = &palette->chars[i];
526 if (char_info->byte_len == char_bytes && memcmp(char_info->bytes, utf8_char, char_bytes) == 0) {
527 return i;
528 }
529 }
530
531 return (size_t)-1;
532}
533
534// Find all indices of a UTF-8 character in the palette (handles duplicates)
535// Returns the number of indices found, fills indices array up to max_indices
536size_t utf8_palette_find_all_char_indices(const utf8_palette_t *palette, const char *utf8_char, size_t char_bytes, size_t *indices, size_t max_indices) {
537 if (!palette || !utf8_char || char_bytes == 0 || char_bytes > 4 || !indices || max_indices == 0) {
538 return 0;
539 }
540
541 size_t found_count = 0;
542
543 for (size_t i = 0; i < palette->char_count && found_count < max_indices; i++) {
544 const utf8_char_info_t *char_info = &palette->chars[i];
545 if (char_info->byte_len == char_bytes && memcmp(char_info->bytes, utf8_char, char_bytes) == 0) {
546 indices[found_count++] = i;
547 }
548 }
549
550 return found_count;
551}
#define SET_ERRNO(code,...)
Common buffer size definitions.
unsigned int width
Definition fft.c:24
#define ASCII_CHAT_UTF8_BUFFER_SIZE
unsigned int uint32_t
#define SAFE_STRNCPY(dst, src, size)
#define SAFE_FREE(ptr)
#define SAFE_MEMSET(dest, dest_size, ch, count)
#define SAFE_MALLOC(size, cast)
#define SAFE_GETENV(name)
unsigned char uint8_t
#define SAFE_MEMCPY(dest, dest_size, src, count)
#define SAFE_STRDUP(dst, src)
@ ERROR_INVALID_STATE
@ ERROR_MEMORY
Definition error_codes.h:55
@ ERROR_INVALID_PARAM
#define log_warn(...)
Log a WARN message.
Definition log/log.h:615
#define log_dev(...)
Log a DEV message (most verbose, development only).
Definition log/log.h:575
#define log_debug(...)
Log a DEBUG message.
Definition log/log.h:589
uint8_t byte_len
Number of bytes in UTF-8 encoding (1-4).
Definition palette.h:235
void utf8_palette_destroy(utf8_palette_t *palette)
Destroy a UTF-8 palette and free resources.
Definition palette.c:478
size_t utf8_palette_find_all_char_indices(const utf8_palette_t *palette, const char *utf8_char, size_t char_bytes, size_t *indices, size_t max_indices)
Find all indices of UTF-8 character in palette.
Definition palette.c:536
size_t char_count
Number of characters (not bytes!).
Definition palette.h:263
bool palette_requires_utf8_encoding(const char *chars, size_t len)
Check if palette requires UTF-8 encoding.
Definition palette.c:53
size_t utf8_palette_get_char_count(const utf8_palette_t *palette)
Get number of characters in UTF-8 palette.
Definition palette.c:495
#define PALETTE_CHARS_MINIMAL
Minimal ASCII palette character string.
Definition palette.h:206
const char * name
Human-readable palette name.
Definition palette.h:132
bool detect_client_utf8_support(utf8_capabilities_t *caps)
Detect client UTF-8 support capabilities.
Definition palette.c:144
palette_type_t select_compatible_palette(palette_type_t requested, bool client_utf8)
Select compatible palette based on client capabilities.
Definition palette.c:212
bool utf8_support
True if terminal supports UTF-8 (detected automatically).
Definition palette.h:154
const size_t DEFAULT_ASCII_PALETTE_LEN
Length of default ASCII palette.
Definition palette.c:32
char terminal_type[32]
$TERM environment variable value (for detection)
Definition palette.h:158
utf8_char_info_t * chars
Array of UTF-8 character information.
Definition palette.h:261
const char DEFAULT_ASCII_PALETTE[]
Default ASCII palette for legacy functions.
Definition palette.c:31
uint8_t display_width
Terminal display width in character cells (1-2).
Definition palette.h:237
int initialize_client_palette(palette_type_t palette_type, const char *custom_chars, char client_palette_chars[ASCII_PALETTE_BUFFER_SIZE], size_t *client_palette_len, char client_luminance_palette[ASCII_LUMINANCE_LEVELS])
Initialize client palette with full configuration.
Definition palette.c:292
const palette_def_t * get_builtin_palette(palette_type_t type)
Get built-in palette definition.
Definition palette.c:45
#define PALETTE_CHARS_DIGITAL
Digital/glitch palette character string.
Definition palette.h:197
palette_type_t
Built-in palette type enumeration.
Definition palette.h:96
#define PALETTE_CHARS_STANDARD
Standard ASCII palette character string.
Definition palette.h:179
int apply_palette_config(palette_type_t type, const char *custom_chars)
Apply palette configuration (set global palette).
Definition palette.c:243
bool requires_utf8
True if palette contains UTF-8 multi-byte characters.
Definition palette.h:138
size_t total_bytes
Total byte length of palette string.
Definition palette.h:265
size_t utf8_palette_find_char_index(const utf8_palette_t *palette, const char *utf8_char, size_t char_bytes)
Find index of UTF-8 character in palette.
Definition palette.c:519
bool validate_palette_chars(const char *chars, size_t len)
Validate palette character sequence.
Definition palette.c:69
const char * chars
Character sequence (ordered from dark to light).
Definition palette.h:134
char bytes[UTF8_MAX_BYTES]
UTF-8 byte sequence (max 4 bytes per character).
Definition palette.h:233
const utf8_char_info_t * utf8_palette_get_char(const utf8_palette_t *palette, size_t index)
Get UTF-8 character information at index.
Definition palette.c:487
char * raw_string
Original palette string (for reference/debugging).
Definition palette.h:267
char locale_encoding[16]
Current locale encoding (e.g., "UTF-8").
Definition palette.h:160
size_t length
Number of characters in sequence.
Definition palette.h:136
#define PALETTE_CHARS_BLOCKS
Unicode block palette character string.
Definition palette.h:188
bool utf8_palette_contains_char(const utf8_palette_t *palette, const char *utf8_char, size_t char_bytes)
Check if UTF-8 palette contains a specific character.
Definition palette.c:503
int build_client_luminance_palette(const char *palette_chars, size_t palette_len, char luminance_mapping[ASCII_LUMINANCE_LEVELS])
Build luminance mapping table from palette characters.
Definition palette.c:272
#define PALETTE_CHARS_COOL
Cool ascending blocks palette character string.
Definition palette.h:215
utf8_palette_t * utf8_palette_create(const char *palette_string)
Create a UTF-8 palette from string.
Definition palette.c:354
@ PALETTE_COUNT
Number of palette types (not a valid palette).
Definition palette.h:112
@ PALETTE_BLOCKS
Unicode block characters: " ░░▒▒▓▓██".
Definition palette.h:102
@ PALETTE_CUSTOM
User-defined via –palette-chars.
Definition palette.h:110
@ PALETTE_COOL
Ascending blocks: " ▁▂▃▄▅▆▇█".
Definition palette.h:108
@ PALETTE_STANDARD
Standard ASCII palette: " ...',;:clodxkO0KXNWM".
Definition palette.h:100
@ PALETTE_UNSET
Unset palette type (will be resolved to default or custom).
Definition palette.h:98
@ PALETTE_DIGITAL
Digital/glitch aesthetic: " -=≡≣▰▱◼".
Definition palette.h:104
@ PALETTE_MINIMAL
Simple ASCII: " .-+*#".
Definition palette.h:106
#define ASCIICHAT_API
Export symbols on Unix platforms (Linux, macOS).
Definition api.h:86
int utf8_display_width_n(const char *str, size_t max_bytes)
Calculate terminal display width of a UTF-8 string up to byte limit.
Definition utf8.c:102
size_t utf8_to_codepoints(const char *str, uint32_t *out_codepoints, size_t max_codepoints)
Convert UTF-8 string to array of Unicode codepoints.
Definition utf8.c:157
int utf8_display_width(const char *str)
Calculate terminal display width of a UTF-8 string.
Definition utf8.c:98
#define ASCII_LUMINANCE_LEVELS
Number of luminance levels supported (256).
Definition ascii.h:438
⚙️ Common definitions, error codes, macros, and types shared throughout the application
SIMD-optimized ASCII conversion interface.
#define VIDEO_ASCII_PALETTE_OLD_LOCALE_BUFFER_SIZE
Definition palette.c:9
ASCII Palette Management for Video-to-ASCII Conversion.
static bool size_t index
Definition queries.h:8
bool terminal_supports_utf8(void)
🔤 String Manipulation and Shell Escaping Utilities
Validated luminance palette used to turn brightness into characters.
Definition palette.h:130
Validated luminance palette used to turn brightness into characters.
Definition palette.h:152
UTF-8 character information structure.
Definition palette.h:231
When code needs to work with utf8 char info, it keeps byte_len, display_width here instead of scatter...
Definition palette.h:259
🔤 UTF-8 Encoding and Decoding Utilities