Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_gui_label_lines.c
Go to the documentation of this file.
1
18
19#define ALLEGRO_UNSTABLE 1
20
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <unistd.h>
25
26#include <allegro5/allegro_primitives.h>
27
28#include "nilorea/n_common.h"
29#include "nilorea/n_log.h"
30#include "nilorea/n_gui.h"
31
32static int log_level = LOG_ERR;
33static int failures = 0;
34
36static void expect_true(const char* what, int ok) {
37 if (!ok) {
38 n_log(LOG_ERR, "FAIL: %s", what);
39 failures++;
40 } else {
41 n_log(LOG_INFO, "ok: %s", what);
42 }
43}
44
46static int process_args(int argc, char** argv) {
47 int opt = 0;
48
49 while ((opt = getopt(argc, argv, "V:")) != -1) {
50 if (opt == 'V') {
51 log_level = atoi(optarg);
52 } else {
53 return -1;
54 }
55 }
56 return 0;
57}
58
60static int colour_distance(ALLEGRO_COLOR a, ALLEGRO_COLOR b) {
61 unsigned char ar, ag, ab, br, bg, bb;
62 al_unmap_rgb(a, &ar, &ag, &ab);
63 al_unmap_rgb(b, &br, &bg, &bb);
64 return abs(ar - br) + abs(ag - bg) + abs(ab - bb);
65}
66
70static void ink(N_GUI_CTX* gui, int x0, int y0, int w, int h, int* bands, int* widest_gap, int* first_right) {
71 ALLEGRO_BITMAP* target = NULL;
72 int old = al_get_new_bitmap_flags();
73 int x, y, in_band = 0, gap = 0, band_no = 0;
74
75 al_set_new_bitmap_flags(ALLEGRO_MEMORY_BITMAP);
76 target = al_create_bitmap(400, 300);
77 al_set_new_bitmap_flags(old);
78 al_set_target_bitmap(target);
79 al_clear_to_color(al_map_rgb(0, 0, 0));
81 /* The background is what the label's box holds in its bottom right
82 * corner, which no text reaches. */
83 ALLEGRO_COLOR bg = al_get_pixel(target, x0 + w - 2, y0 + h - 2);
84 *bands = 0;
85 *widest_gap = 0;
86 *first_right = -1;
87 for (y = y0; y < y0 + h; y++) {
88 int row_ink = 0;
89 for (x = x0; x < x0 + w - 3; x++) {
90 if (colour_distance(al_get_pixel(target, x, y), bg) > 60) {
91 row_ink = 1;
92 if (band_no <= 1 && x > *first_right && (in_band || band_no == 0)) *first_right = x;
93 }
94 }
95 if (row_ink && !in_band) {
96 (*bands)++;
97 band_no = *bands;
98 if (*bands > 1 && gap > *widest_gap) *widest_gap = gap;
99 in_band = 1;
100 } else if (!row_ink && in_band) {
101 in_band = 0;
102 gap = 0;
103 }
104 if (!row_ink) gap++;
105 }
106 al_set_target_bitmap(NULL);
107 al_destroy_bitmap(target);
108}
109
110int main(int argc, char** argv) {
111 int bands = 0, gap = 0, right = 0;
112 float nw = 0.0f, nh = 0.0f;
113
115 if (process_args(argc, argv) != 0) {
116 fprintf(stderr, "Usage: %s [-V LOG_LEVEL]\n", argv[0]);
117 return 2;
118 }
120 if (!al_init() || !al_init_font_addon() || !al_init_primitives_addon()) {
121 n_log(LOG_ERR, "allegro init failed");
122 return 1;
123 }
124 ALLEGRO_FONT* font = al_create_builtin_font();
125 if (!font) return 1;
127 if (!gui) {
128 al_destroy_font(font);
129 return 1;
130 }
131 n_gui_set_display_size(gui, 400.0f, 300.0f);
132 float fh = (float)al_get_font_line_height(font);
133 float pad = gui->style.label_padding;
134 int win = n_gui_add_window(gui, "W", 0.0f, 0.0f, 400.0f, 300.0f);
137
138 /* Plain labels. */
139 int left = n_gui_add_label(gui, win, "one\ntwo\nthree", 10.0f, 10.0f, 150.0f, 60.0f, N_GUI_ALIGN_LEFT);
140 expect_true("fits counts every line",
141 n_gui_label_fits(gui, left, &nw, &nh) == 1 && nh == 3.0f * fh &&
142 nw == (float)al_get_text_width(font, "three") + 2.0f * pad);
143 ink(gui, 10, 10, 150, 60, &bands, &gap, &right);
144 expect_true("a LEFT label draws each line under the one before", bands == 3);
145
146 n_gui_label_set_text(gui, left, "above\n\nbelow");
147 ink(gui, 10, 10, 150, 60, &bands, &gap, &right);
148 expect_true("a blank line is a line", bands == 2 && gap >= (int)fh);
149
150 n_gui_label_set_text(gui, left, "no break here");
151 expect_true("a label with no break measures as it always did",
152 n_gui_label_fits(gui, left, &nw, &nh) == 1 && nh == fh);
153
154 int centre = n_gui_add_label(gui, win, "a\nwide line", 10.0f, 80.0f, 150.0f, 40.0f, N_GUI_ALIGN_CENTER);
155 ink(gui, 10, 80, 150, 40, &bands, &gap, &right);
156 expect_true("a CENTER label draws its lines", bands == 2);
157 (void)centre;
158
159 /* Not fitting, and cut at its box rather than drawn over what is below:
160 nothing of its fourth line may reach the label under it. */
161 expect_true("a box too short for the lines does not fit",
162 n_gui_label_fits(gui, n_gui_add_label(gui, win, "1\n2\n3\n4", 10.0f, 130.0f, 100.0f, 20.0f, N_GUI_ALIGN_LEFT), &nw, &nh) == 0 &&
163 nh == 4.0f * fh);
164
165 /* Justified: a break, and the line before it is not stretched. */
166 n_gui_label_set_text(gui, left, "");
167 int just = n_gui_add_label(gui, win, "aa bb\ncc", 10.0f, 160.0f, 300.0f, 60.0f, N_GUI_ALIGN_JUSTIFIED);
168 ink(gui, 10, 160, 300, 60, &bands, &gap, &right);
169 expect_true("a JUSTIFIED label breaks at a line break", bands == 2);
170 expect_true("and does not stretch the line before it across the box",
171 right >= 0 && right < 10 + (int)pad + al_get_text_width(font, "aa bb") + 4);
172 expect_true("its content height counts the break",
173 n_gui_label_fits(gui, just, &nw, &nh) == 1 && nh == 2.0f * fh + pad);
174
176 al_destroy_font(font);
177 if (failures) {
178 fprintf(stderr, "ex_gui_label_lines: %d failure(s)\n", failures);
179 return 1;
180 }
181 printf("ex_gui_label_lines: ok\n");
182 return 0;
183}
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 void right(N_GUI_CTX *gui, float cx, float cy)
right press
static N_GUI_CTX * gui
static void ink(N_GUI_CTX *gui, int x0, int y0, int w, int h, int *bands, int *widest_gap, int *first_right)
draw the context into a memory bitmap and describe the ink inside a rectangle: how many separate band...
static int colour_distance(ALLEGRO_COLOR a, ALLEGRO_COLOR b)
how far a pixel is from another, summed over the three channels
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_text(N_GUI_CTX *ctx, int widget_id, const char *text)
set label text content
Definition n_gui.c:6184
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_WIN_FRAMELESS
frameless window: no title bar drawn, drag via window body unless N_GUI_WIN_FIXED_POSITION is also se...
Definition n_gui.h:276
void n_gui_draw(N_GUI_CTX *ctx)
draw all visible GUI windows and widgets
Definition n_gui.c:11735
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_ALIGN_CENTER
center aligned text
Definition n_gui.h:214
void n_gui_destroy_ctx(N_GUI_CTX **ctx)
Destroy a GUI context and all its windows/widgets.
Definition n_gui.c:1209
void n_gui_open_window(N_GUI_CTX *ctx, int window_id)
Open (show) a window.
Definition n_gui.c:1436
int n_gui_label_fits(N_GUI_CTX *ctx, int widget_id, float *needed_w, float *needed_h)
Report whether a label's text is drawn in full inside its own widget box.
Definition n_gui.c:6215
void n_gui_window_set_flags(N_GUI_CTX *ctx, int window_id, int flags)
Set feature flags on a window.
Definition n_gui.c:2515
#define N_GUI_ALIGN_JUSTIFIED
justified text (spread words to fill width)
Definition n_gui.h:218
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.