Table of Contents
ex_gui_text_shorten.c
shortening text to a width, at the end, at the start, or a path A label too narrow for its text cut it at the end, which is right for a sentence and wrong for a path: it kept the leading directories every file in the folder shares and threw away the file name. n_gui_label_set_truncate() chooses the cut, and n_gui_text_shorten() is the same shortening for a caller that draws its own text.
Headless: the builtin font is monospaced, so widths are counted in characters. ```c
#define ALLEGRO_UNSTABLE 1
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h>
#include "nilorea/n_common.h" #include "nilorea/n_log.h" #include "nilorea/n_gui.h"
static int log_level = LOG_ERR; static int failures = 0;
static void expect_true(const char* what, int ok) { if (!ok) { n_log(LOG_ERR, "FAIL: %s", what); failures++; } else { n_log(LOG_INFO, "ok: %s", what); } }
static int process_args(int argc, char** argv) { int opt = 0;
while ((opt = getopt(argc, argv, "V:")) != -1) {
if (opt == 'V') {
log_level = atoi(optarg);
} else {
return -1;
}
}
return 0;
}
static int utf8_whole(const char* s) { const unsigned char* p = (const unsigned char*)s;
while (*p) {
int n = (*p & 0x80) == 0 ? 1 : (*p & 0xE0) == 0xC0 ? 2
: (*p & 0xF0) == 0xE0 ? 3
: (*p & 0xF8) == 0xF0 ? 4
: 0;
int k = 0;
if (n == 0) return 0;
for (k = 1; k < n; k++)
if ((p[k] & 0xC0) != 0x80) return 0;
p += n;
}
return 1;
}
int main(int argc, char** argv) { char out[256]; float cw = 0.0f;
set_log_level(LOG_ERR);
if (process_args(argc, argv) != 0) {
fprintf(stderr, "Usage: %s [-V LOG_LEVEL]\n", argv[0]);
return 2;
}
set_log_level(log_level);
if (!al_init() || !al_init_font_addon()) {
n_log(LOG_ERR, "allegro init failed");
return 1;
}
ALLEGRO_FONT* font = al_create_builtin_font();
if (!font) return 1;
cw = (float)al_get_text_width(font, "a");
expect_true("the builtin font is monospaced", al_get_text_width(font, "abcdefghij") == (int)(cw * 10.0f));
expect_true("text that fits is left alone",
n_gui_text_shorten(font, "short", cw * 10.0f, N_GUI_TRUNCATE_END, out, sizeof(out)) == 0 &&
strcmp(out, "short") == 0);
n_gui_text_shorten(font, "abcdefghij", cw * 6.0f, N_GUI_TRUNCATE_END, out, sizeof(out));
expect_true("the end is cut: abc...", strcmp(out, "abc...") == 0);
n_gui_text_shorten(font, "abcdefghij", cw * 6.0f, N_GUI_TRUNCATE_START, out, sizeof(out));
expect_true("the start is cut: ...hij", strcmp(out, "...hij") == 0);
n_gui_text_shorten(font, "/home/user/photos/IMG_0001.CR3", cw * 22.0f, N_GUI_TRUNCATE_PATH, out,
sizeof(out));
expect_true("a path loses whole leading components and keeps its file name",
strcmp(out, ".../IMG_0001.CR3") == 0);
n_gui_text_shorten(font, "C:\\Users\\me\\Pictures\\IMG_0001.CR3", cw * 26.0f, N_GUI_TRUNCATE_PATH, out,
sizeof(out));
expect_true("either separator counts", strcmp(out, ".../Pictures\\IMG_0001.CR3") == 0);
n_gui_text_shorten(font, "dir/averyveryverylongname.CR3", cw * 10.0f, N_GUI_TRUNCATE_PATH, out,
sizeof(out));
expect_true("a file name wider than the box alone keeps its end",
strcmp(out, "...ame.CR3") == 0);
n_gui_text_shorten(font, "caf\xc3\xa9 cr\xc3\xa8me br\xc3\xbbl\xc3\xa9\x65", cw * 7.0f, N_GUI_TRUNCATE_START,
out, sizeof(out));
expect_true("a cut at the start never splits a UTF-8 sequence", utf8_whole(out));
n_gui_text_shorten(font, "caf\xc3\xa9 cr\xc3\xa8me br\xc3\xbbl\xc3\xa9\x65", cw * 7.0f, N_GUI_TRUNCATE_END,
out, sizeof(out));
expect_true("nor does a cut at the end", utf8_whole(out));
expect_true("NULL is refused", n_gui_text_shorten(NULL, "x", 10.0f, 0, out, sizeof(out)) == -1 &&
n_gui_text_shorten(font, "x", 10.0f, 0, NULL, 10) == -1);
/* The label's own setting. */
N_GUI_CTX* gui = n_gui_new_ctx(font);
if (!gui) {
al_destroy_font(font);
return 1;
}
n_gui_set_display_size(gui, 800.0f, 600.0f);
int win = n_gui_add_window(gui, "W", 0.0f, 0.0f, 300.0f, 200.0f);
int label = n_gui_add_label(gui, win, "/a/b/c.txt", 10.0f, 10.0f, 80.0f, 20.0f, N_GUI_ALIGN_LEFT);
int button = n_gui_add_button(gui, win, "B", 10.0f, 40.0f, 40.0f, 20.0f, N_GUI_SHAPE_ROUNDED, NULL, NULL);
expect_true("a label cuts at the end until told otherwise",
n_gui_label_get_truncate(gui, label) == N_GUI_TRUNCATE_END);
n_gui_label_set_truncate(gui, label, N_GUI_TRUNCATE_PATH);
expect_true("and cuts a path when told to", n_gui_label_get_truncate(gui, label) == N_GUI_TRUNCATE_PATH);
n_gui_label_set_truncate(gui, label, 42);
expect_true("an unknown mode is the end", n_gui_label_get_truncate(gui, label) == N_GUI_TRUNCATE_END);
n_gui_label_set_truncate(gui, button, N_GUI_TRUNCATE_START);
expect_true("a button has no truncate mode", n_gui_label_get_truncate(gui, button) == N_GUI_TRUNCATE_END);
n_gui_destroy_ctx(&gui);
al_destroy_font(font);
if (failures) {
fprintf(stderr, "ex_gui_text_shorten: %d failure(s)\n", failures);
return 1;
}
printf("ex_gui_text_shorten: ok\n");
return 0;
}
_Filename: ex_gui_text_shorten.c_
Modules
- AABB3D
- ACCEPT_POOL
- ALLEGRO4
- ALLEGRO5
- ANIM_LIB
- ASTAR
- ATLAS_LIB
- AVRO
- CLOCK_SYNC
- COMMONS
- CONFIGFILE
- CYPHER_BASE64
- CYPHER_VIGENERE
- DEAD_RECKONING
- ENUMS
- EXCEPTIONS
- GAME_ENV
- GIT
- GUI
- HASH_TABLE
- ISOMETRIC_ENGINE
- LIST
- LOG
- LOGNODUP
- LZ4
- NETWORKING
- NETWORK_MSG
- N_COOKIES
- N_DIFF
- N_DIGEST
- N_DISK_CACHE
- N_DNS
- N_ECC
- N_ENTROPY
- N_FILES
- N_FLUIDS
- N_GUI_COLOR
- N_GUI_COLORWHEEL
- N_GUI_CURVE
- N_GUI_GRID
- N_GUI_POPUP
- N_HEX
- N_HTTP3
- N_HTTP_COOKIE
- N_HTTP_FORM
- N_HTTP_MIME
- N_HTTP_REQ
- N_HTTP_RESP
- N_HTTP_ROUTER
- N_HTTP_SERVER
- N_ICC
- N_JSON
- N_KAFKA
- N_LINALG
- N_MODULE
- N_OPENSSL_COMPAT
- N_PRETTY
- N_PTH
- N_QUERY
- N_RANDOM
- N_SHA256
- N_STR
- N_TIFF
- N_TIME
- N_USER
- N_X509
- N_ZIP
- PARTICLES
- PCRE
- PHYSICS
- SIGNALS
- STACK
- THREADS
- TRAJECTORY
- TREE
- ZLIB