6 ex__gui__label__lines_8c
gullradriel edited this page 2026-10-09 00:55:18 +02:00

examples/ex_gui_label_lines.c

Functions

Name
int process_args(int argc, char ** argv)
parse -V LOG_LEVEL
int main(int argc, char ** argv)
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 bands of rows have any, the tallest gap between two bands, and the rightmost inked column of the first band
void expect_true(const char * what, int ok)
record one check
int colour_distance(ALLEGRO_COLOR a, ALLEGRO_COLOR b)
how far a pixel is from another, summed over the three channels

Attributes

Name
int log_level
int failures

Defines

Name
ALLEGRO_UNSTABLE

Functions Documentation

function process_args

static int process_args(
    int argc,
    char ** argv
)

parse -V LOG_LEVEL

function main

int main(
    int argc,
    char ** argv
)

function ink

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 bands of rows have any, the tallest gap between two bands, and the rightmost inked column of the first band

function expect_true

static void expect_true(
    const char * what,
    int ok
)

record one check

function colour_distance

static int colour_distance(
    ALLEGRO_COLOR a,
    ALLEGRO_COLOR b
)

how far a pixel is from another, summed over the three channels

Attributes Documentation

variable log_level

static int log_level = 3;

variable failures

static int failures = 0;

Macros Documentation

define ALLEGRO_UNSTABLE

#define ALLEGRO_UNSTABLE 1

Source code



#define ALLEGRO_UNSTABLE 1

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <allegro5/allegro_primitives.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 colour_distance(ALLEGRO_COLOR a, ALLEGRO_COLOR b) {
    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));
    n_gui_draw(gui);
    /* The background is what the label's box holds in its bottom right
     * corner, which no text reaches. */
    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++) {
            if (colour_distance(al_get_pixel(target, x, y), bg) > 60) {
                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;

    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() || !al_init_primitives_addon()) {
        n_log(LOG_ERR, "allegro init failed");
        return 1;
    }
    ALLEGRO_FONT* font = al_create_builtin_font();
    if (!font) return 1;
    N_GUI_CTX* gui = n_gui_new_ctx(font);
    if (!gui) {
        al_destroy_font(font);
        return 1;
    }
    n_gui_set_display_size(gui, 400.0f, 300.0f);
    float fh = (float)al_get_font_line_height(font);
    float pad = gui->style.label_padding;
    int win = n_gui_add_window(gui, "W", 0.0f, 0.0f, 400.0f, 300.0f);
    n_gui_window_set_flags(gui, win, N_GUI_WIN_FRAMELESS);
    n_gui_open_window(gui, win);

    /* Plain labels. */
    int left = n_gui_add_label(gui, win, "one\ntwo\nthree", 10.0f, 10.0f, 150.0f, 60.0f, N_GUI_ALIGN_LEFT);
    expect_true("fits counts every line",
                n_gui_label_fits(gui, left, &nw, &nh) == 1 && nh == 3.0f * fh &&
                    nw == (float)al_get_text_width(font, "three") + 2.0f * pad);
    ink(gui, 10, 10, 150, 60, &bands, &gap, &right);
    expect_true("a LEFT label draws each line under the one before", bands == 3);

    n_gui_label_set_text(gui, left, "above\n\nbelow");
    ink(gui, 10, 10, 150, 60, &bands, &gap, &right);
    expect_true("a blank line is a line", bands == 2 && gap >= (int)fh);

    n_gui_label_set_text(gui, left, "no break here");
    expect_true("a label with no break measures as it always did",
                n_gui_label_fits(gui, left, &nw, &nh) == 1 && nh == fh);

    int centre = n_gui_add_label(gui, win, "a\nwide line", 10.0f, 80.0f, 150.0f, 40.0f, N_GUI_ALIGN_CENTER);
    ink(gui, 10, 80, 150, 40, &bands, &gap, &right);
    expect_true("a CENTER label draws its lines", bands == 2);
    (void)centre;

    /* Not fitting, and cut at its box rather than drawn over what is below:
       nothing of its fourth line may reach the label under it. */
    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);

    /* Justified: a break, and the line before it is not stretched. */
    n_gui_label_set_text(gui, left, "");
    int just = n_gui_add_label(gui, win, "aa bb\ncc", 10.0f, 160.0f, 300.0f, 60.0f, N_GUI_ALIGN_JUSTIFIED);
    ink(gui, 10, 160, 300, 60, &bands, &gap, &right);
    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);
    expect_true("its content height counts the break",
                n_gui_label_fits(gui, just, &nw, &nh) == 1 && nh == 2.0f * fh + pad);

    n_gui_destroy_ctx(&gui);
    al_destroy_font(font);
    if (failures) {
        fprintf(stderr, "ex_gui_label_lines: %d failure(s)\n", failures);
        return 1;
    }
    printf("ex_gui_label_lines: ok\n");
    return 0;
}