Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_gui_text_shorten.c
Go to the documentation of this file.
1
17
18#define ALLEGRO_UNSTABLE 1
19
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <unistd.h>
24
25#include "nilorea/n_common.h"
26#include "nilorea/n_log.h"
27#include "nilorea/n_gui.h"
28
29static int log_level = LOG_ERR;
30static int failures = 0;
31
33static void expect_true(const char* what, int ok) {
34 if (!ok) {
35 n_log(LOG_ERR, "FAIL: %s", what);
36 failures++;
37 } else {
38 n_log(LOG_INFO, "ok: %s", what);
39 }
40}
41
43static int process_args(int argc, char** argv) {
44 int opt = 0;
45
46 while ((opt = getopt(argc, argv, "V:")) != -1) {
47 if (opt == 'V') {
48 log_level = atoi(optarg);
49 } else {
50 return -1;
51 }
52 }
53 return 0;
54}
55
57static int utf8_whole(const char* s) {
58 const unsigned char* p = (const unsigned char*)s;
59
60 while (*p) {
61 int n = (*p & 0x80) == 0 ? 1 : (*p & 0xE0) == 0xC0 ? 2
62 : (*p & 0xF0) == 0xE0 ? 3
63 : (*p & 0xF8) == 0xF0 ? 4
64 : 0;
65 int k = 0;
66 if (n == 0) return 0;
67 for (k = 1; k < n; k++)
68 if ((p[k] & 0xC0) != 0x80) return 0;
69 p += n;
70 }
71 return 1;
72}
73
74int main(int argc, char** argv) {
75 char out[256];
76 float cw = 0.0f;
77
79 if (process_args(argc, argv) != 0) {
80 fprintf(stderr, "Usage: %s [-V LOG_LEVEL]\n", argv[0]);
81 return 2;
82 }
84 if (!al_init() || !al_init_font_addon()) {
85 n_log(LOG_ERR, "allegro init failed");
86 return 1;
87 }
88 ALLEGRO_FONT* font = al_create_builtin_font();
89 if (!font) return 1;
90 cw = (float)al_get_text_width(font, "a");
91 expect_true("the builtin font is monospaced", al_get_text_width(font, "abcdefghij") == (int)(cw * 10.0f));
92
93 expect_true("text that fits is left alone",
94 n_gui_text_shorten(font, "short", cw * 10.0f, N_GUI_TRUNCATE_END, out, sizeof(out)) == 0 &&
95 strcmp(out, "short") == 0);
96
97 n_gui_text_shorten(font, "abcdefghij", cw * 6.0f, N_GUI_TRUNCATE_END, out, sizeof(out));
98 expect_true("the end is cut: abc...", strcmp(out, "abc...") == 0);
99 n_gui_text_shorten(font, "abcdefghij", cw * 6.0f, N_GUI_TRUNCATE_START, out, sizeof(out));
100 expect_true("the start is cut: ...hij", strcmp(out, "...hij") == 0);
101
102 n_gui_text_shorten(font, "/home/user/photos/IMG_0001.CR3", cw * 22.0f, N_GUI_TRUNCATE_PATH, out,
103 sizeof(out));
104 expect_true("a path loses whole leading components and keeps its file name",
105 strcmp(out, ".../IMG_0001.CR3") == 0);
106 n_gui_text_shorten(font, "C:\\Users\\me\\Pictures\\IMG_0001.CR3", cw * 26.0f, N_GUI_TRUNCATE_PATH, out,
107 sizeof(out));
108 expect_true("either separator counts", strcmp(out, ".../Pictures\\IMG_0001.CR3") == 0);
109 n_gui_text_shorten(font, "dir/averyveryverylongname.CR3", cw * 10.0f, N_GUI_TRUNCATE_PATH, out,
110 sizeof(out));
111 expect_true("a file name wider than the box alone keeps its end",
112 strcmp(out, "...ame.CR3") == 0);
113
114 n_gui_text_shorten(font, "caf\xc3\xa9 cr\xc3\xa8me br\xc3\xbbl\xc3\xa9\x65", cw * 7.0f, N_GUI_TRUNCATE_START,
115 out, sizeof(out));
116 expect_true("a cut at the start never splits a UTF-8 sequence", utf8_whole(out));
117 n_gui_text_shorten(font, "caf\xc3\xa9 cr\xc3\xa8me br\xc3\xbbl\xc3\xa9\x65", cw * 7.0f, N_GUI_TRUNCATE_END,
118 out, sizeof(out));
119 expect_true("nor does a cut at the end", utf8_whole(out));
120
121 expect_true("NULL is refused", n_gui_text_shorten(NULL, "x", 10.0f, 0, out, sizeof(out)) == -1 &&
122 n_gui_text_shorten(font, "x", 10.0f, 0, NULL, 10) == -1);
123
124 /* The label's own setting. */
126 if (!gui) {
127 al_destroy_font(font);
128 return 1;
129 }
130 n_gui_set_display_size(gui, 800.0f, 600.0f);
131 int win = n_gui_add_window(gui, "W", 0.0f, 0.0f, 300.0f, 200.0f);
132 int label = n_gui_add_label(gui, win, "/a/b/c.txt", 10.0f, 10.0f, 80.0f, 20.0f, N_GUI_ALIGN_LEFT);
133 int button = n_gui_add_button(gui, win, "B", 10.0f, 40.0f, 40.0f, 20.0f, N_GUI_SHAPE_ROUNDED, NULL, NULL);
134 expect_true("a label cuts at the end until told otherwise",
137 expect_true("and cuts a path when told to", n_gui_label_get_truncate(gui, label) == N_GUI_TRUNCATE_PATH);
138 n_gui_label_set_truncate(gui, label, 42);
139 expect_true("an unknown mode is the end", n_gui_label_get_truncate(gui, label) == N_GUI_TRUNCATE_END);
141 expect_true("a button has no truncate mode", n_gui_label_get_truncate(gui, button) == N_GUI_TRUNCATE_END);
142
144 al_destroy_font(font);
145 if (failures) {
146 fprintf(stderr, "ex_gui_text_shorten: %d failure(s)\n", failures);
147 return 1;
148 }
149 printf("ex_gui_text_shorten: ok\n");
150 return 0;
151}
void process_args(int argc, char **argv)
static int failures
Definition ex_atlas.c:51
int main(void)
Definition ex_atlas.c:117
static void expect_true(const char *label, int cond)
Definition ex_atlas.c:53
static int log_level
static N_GUI_CTX * gui
static int utf8_whole(const char *s)
1 when s is whole UTF-8 sequences, none cut in half
FONT * font
the default 8x8 font, valid after allegro_init()
void n_gui_set_display_size(N_GUI_CTX *ctx, float w, float h)
set the display (viewport) size so global scrollbars can be computed.
Definition n_gui.c:12348
#define N_GUI_ALIGN_LEFT
left aligned text
Definition n_gui.h:212
void n_gui_label_set_truncate(N_GUI_CTX *ctx, int widget_id, int mode)
set the text of a label widget
Definition n_gui.c:6156
int n_gui_text_shorten(ALLEGRO_FONT *font, const char *text, float max_w, int mode, char *out, size_t out_len)
shorten text to fit max_w pixels in font, the way a label with truncate mode mode shows it: unchanged...
Definition n_gui.c:6890
int n_gui_add_label(N_GUI_CTX *ctx, int window_id, const char *text, float x, float y, float w, float h, int align)
Add a static text label.
Definition n_gui.c:3639
#define N_GUI_TRUNCATE_START
text too wide for its box is cut at the start, "...ong sentence": for anything whose end is what tell...
Definition n_gui.h:225
int n_gui_add_window(N_GUI_CTX *ctx, const char *title, float x, float y, float w, float h)
Add a new pseudo-window to the context.
Definition n_gui.c:1277
N_GUI_CTX * n_gui_new_ctx(ALLEGRO_FONT *default_font)
Create a new GUI context.
Definition n_gui.c:1103
#define N_GUI_TRUNCATE_END
text too wide for its box is cut at the end, "Long senten...": the default, right for a sentence
Definition n_gui.h:222
void n_gui_destroy_ctx(N_GUI_CTX **ctx)
Destroy a GUI context and all its windows/widgets.
Definition n_gui.c:1209
#define N_GUI_TRUNCATE_PATH
a path too wide for its box loses whole leading components, ".../photos/IMG_0001.CR3",...
Definition n_gui.h:229
int n_gui_label_get_truncate(N_GUI_CTX *ctx, int widget_id)
a label's truncate mode
Definition n_gui.c:6169
#define N_GUI_SHAPE_ROUNDED
rounded rectangle shape
Definition n_gui.h:172
int n_gui_add_button(N_GUI_CTX *ctx, int window_id, const char *label, float x, float y, float w, float h, int shape, void(*on_click)(int, void *), void *user_data)
Add a button widget to a window.
Definition n_gui.c:2720
The top-level GUI context that holds all windows.
Definition n_gui.h:1669
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:113
#define LOG_ERR
error conditions
Definition n_log.h:100
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL ).
Definition n_log.c:123
#define LOG_INFO
informational
Definition n_log.h:106
Common headers and low-level functions & define.
GUI system: buttons, sliders, text areas, checkboxes, scrollbars, dropdown menus, windows.
Generic log system.