A label drew one line for every alignment but JUSTIFIED, which wrapped to its width and treated a line break as a space: two lines meant two widgets and a computed line height. Now "\n" is a hard break everywhere: a LEFT, CENTER or RIGHT label draws one line under another, each aligned on its own, and a JUSTIFIED one breaks there and does not stretch the line before it. n_gui_label_fits() counts the lines.
Headless: the builtin font and a memory bitmap. What is drawn is checked by counting the bands of rows that have ink in them.
#define ALLEGRO_UNSTABLE 1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <allegro5/allegro_primitives.h>
if (!ok) {
} else {
}
}
int opt = 0;
while ((opt = getopt(argc, argv, "V:")) != -1) {
if (opt == 'V') {
} else {
return -1;
}
}
return 0;
}
unsigned char ar, ag, ab, br, bg, bb;
al_unmap_rgb(a, &ar, &ag, &ab);
al_unmap_rgb(b, &br, &bg, &bb);
return abs(ar - br) + abs(ag - bg) + abs(ab - bb);
}
static void ink(
N_GUI_CTX*
gui,
int x0,
int y0,
int w,
int h,
int* bands,
int* widest_gap,
int* first_right) {
ALLEGRO_BITMAP* target = NULL;
int old = al_get_new_bitmap_flags();
int x, y, in_band = 0, gap = 0, band_no = 0;
al_set_new_bitmap_flags(ALLEGRO_MEMORY_BITMAP);
target = al_create_bitmap(400, 300);
al_set_new_bitmap_flags(old);
al_set_target_bitmap(target);
al_clear_to_color(al_map_rgb(0, 0, 0));
ALLEGRO_COLOR bg = al_get_pixel(target, x0 + w - 2, y0 + h - 2);
*bands = 0;
*widest_gap = 0;
*first_right = -1;
for (y = y0; y < y0 + h; y++) {
int row_ink = 0;
for (x = x0; x < x0 + w - 3; x++) {
row_ink = 1;
if (band_no <= 1 && x > *first_right && (in_band || band_no == 0)) *first_right = x;
}
}
if (row_ink && !in_band) {
(*bands)++;
band_no = *bands;
if (*bands > 1 && gap > *widest_gap) *widest_gap = gap;
in_band = 1;
} else if (!row_ink && in_band) {
in_band = 0;
gap = 0;
}
if (!row_ink) gap++;
}
al_set_target_bitmap(NULL);
al_destroy_bitmap(target);
}
int main(
int argc,
char** argv) {
int bands = 0, gap = 0,
right = 0;
float nw = 0.0f, nh = 0.0f;
fprintf(stderr, "Usage: %s [-V LOG_LEVEL]\n", argv[0]);
return 2;
}
if (!al_init() || !al_init_font_addon() || !al_init_primitives_addon()) {
return 1;
}
ALLEGRO_FONT*
font = al_create_builtin_font();
return 1;
}
float fh = (float)al_get_font_line_height(
font);
float pad =
gui->style.label_padding;
nw == (
float)al_get_text_width(
font,
"three") + 2.0f * pad);
expect_true(
"a LEFT label draws each line under the one before", bands == 3);
expect_true(
"a blank line is a line", bands == 2 && gap >= (
int)fh);
expect_true(
"a label with no break measures as it always did",
expect_true(
"a CENTER label draws its lines", bands == 2);
(void)centre;
expect_true(
"a box too short for the lines does not fit",
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 &&
nh == 4.0f * fh);
expect_true(
"a JUSTIFIED label breaks at a line break", bands == 2);
expect_true(
"and does not stretch the line before it across the box",
right >= 0 &&
right < 10 + (
int)pad + al_get_text_width(
font,
"aa bb") + 4);
fprintf(stderr,
"ex_gui_label_lines: %d failure(s)\n",
failures);
return 1;
}
printf("ex_gui_label_lines: ok\n");
return 0;
}
void process_args(int argc, char **argv)
static void expect_true(const char *label, int cond)
static void right(N_GUI_CTX *gui, float cx, float cy)
right press
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.
#define N_GUI_ALIGN_LEFT
left aligned text
void n_gui_label_set_text(N_GUI_CTX *ctx, int widget_id, const char *text)
set label text content
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.
#define N_GUI_WIN_FRAMELESS
frameless window: no title bar drawn, drag via window body unless N_GUI_WIN_FIXED_POSITION is also se...
void n_gui_draw(N_GUI_CTX *ctx)
draw all visible GUI windows and widgets
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.
N_GUI_CTX * n_gui_new_ctx(ALLEGRO_FONT *default_font)
Create a new GUI context.
#define N_GUI_ALIGN_CENTER
center aligned text
void n_gui_destroy_ctx(N_GUI_CTX **ctx)
Destroy a GUI context and all its windows/widgets.
void n_gui_open_window(N_GUI_CTX *ctx, int window_id)
Open (show) a window.
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.
void n_gui_window_set_flags(N_GUI_CTX *ctx, int window_id, int flags)
Set feature flags on a window.
#define N_GUI_ALIGN_JUSTIFIED
justified text (spread words to fill width)
The top-level GUI context that holds all windows.
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
#define LOG_ERR
error conditions
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL ).
#define LOG_INFO
informational
Common headers and low-level functions & define.
GUI system: buttons, sliders, text areas, checkboxes, scrollbars, dropdown menus, windows.