Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_gui_md.c

n_gui_md regression: measure equals draw, every block kind lays out, a link can be found, a narrower column is a taller one, and the N_GUI_TYPE_MARKDOWNVIEW widget over it scrolls and follows its theme.

n_gui_md regression: measure equals draw, every block kind lays out, a link can be found, a narrower column is a taller one, and the N_GUI_TYPE_MARKDOWNVIEW widget over it scrolls and follows its theme.

Author
Castagnier Mickael
Version
1.0

Needs a display, because it measures real glyphs, so it is not in run_tests.sh with the headless examples; run it under Xvfb.

xvfb-run -a ./examples/ex_gui_md

With -o FILE it also writes the rendered page out, which is how the layout gets looked at rather than only counted.

/*
* Nilorea Library
* Copyright (C) 2005-2026 Castagnier Mickael
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "nilorea/n_gui.h"
#include "nilorea/n_log.h"
#include <allegro5/allegro_font.h>
#include <allegro5/allegro_image.h>
#include <allegro5/allegro_primitives.h>
#include <allegro5/allegro_ttf.h>
#include <stdio.h>
#include <string.h>
static int failures = 0;
#define CHECK(cond, msg) \
do { \
if (!(cond)) { \
n_log(LOG_ERR, "FAIL: %s", (msg)); \
failures++; \
} \
} while (0)
static const char* SAMPLE =
"# A heading\n"
"\n"
"A paragraph with **bold**, *italic*, `code` and a\n"
"[link](https://example.com/page) in it, hard wrapped by whoever wrote it\n"
"and re-wrapped by the renderer.\n"
"\n"
"## A smaller heading\n"
"\n"
"> a quoted line\n"
"\n"
"```\n"
"a fenced code block\n"
"keeping its own line breaks\n"
"```\n"
"\n"
"- a list item\n"
"- one that carries on\n"
" onto a second line\n"
"\n"
"| Key | Effect |\n"
"|:----|-------:|\n"
"| `G` | back to the grid |\n"
"| Escape | close a dialog, otherwise go back to the grid |\n";
static ALLEGRO_FONT* face(const char* name, int px) {
static const char* const dirs[] = {
"/usr/share/fonts/truetype/dejavu/",
"/usr/share/fonts/TTF/",
"/usr/share/fonts/dejavu/",
NULL};
char path[512];
int i;
for (i = 0; dirs[i]; i++) {
ALLEGRO_FONT* f;
(void)snprintf(path, sizeof(path), "%s%s", dirs[i], name);
f = al_load_ttf_font(path, px, 0);
if (f)
return f;
}
return NULL;
}
int main(int argc, char** argv) {
ALLEGRO_DISPLAY* display;
ALLEGRO_COLOR bg = al_map_rgb(33, 37, 46);
ALLEGRO_COLOR fg = al_map_rgb(220, 223, 228);
const char* out_path = NULL;
float h_wide, h_narrow, drawn;
int i;
for (i = 1; i < argc; i++)
if (strcmp(argv[i], "-o") == 0 && i + 1 < argc)
out_path = argv[++i];
if (!al_init()) {
n_log(LOG_ERR, "cannot initialise Allegro");
return 1;
}
al_init_font_addon();
al_init_ttf_addon();
(void)al_init_image_addon();
if (!al_init_primitives_addon()) {
n_log(LOG_ERR, "cannot initialise the primitives addon");
return 1;
}
display = al_create_display(900, 600);
if (!display) {
n_log(LOG_ERR, "cannot create a display; run this under Xvfb");
return 1;
}
memset(&fonts, 0, sizeof(fonts));
fonts.regular = face("DejaVuSans.ttf", 14);
fonts.bold = face("DejaVuSans-Bold.ttf", 14);
fonts.italic = face("DejaVuSans-Oblique.ttf", 14);
fonts.bolditalic = face("DejaVuSans-BoldOblique.ttf", 14);
fonts.mono = face("DejaVuSansMono.ttf", 14);
fonts.h1 = face("DejaVuSans-Bold.ttf", 21);
fonts.h2 = face("DejaVuSans-Bold.ttf", 17);
fonts.h3 = face("DejaVuSans-Bold.ttf", 15);
if (!fonts.regular) {
/* No TTF anywhere: the builtin bitmap font still proves the layout,
and every other face staying NULL exercises the fallbacks. */
fonts.regular = al_create_builtin_font();
n_log(LOG_NOTICE, "no TTF found, using the builtin font");
}
CHECK(fonts.regular != NULL, "a regular face");
if (!fonts.regular)
return 1;
n_gui_md_style_default(&style, fg, bg, (float)al_get_font_line_height(fonts.regular));
CHECK(style.parse_flags == N_MARKDOWN_ALL, "the default style asks for every block kind");
CHECK(style.hard_breaks == 0, "the default style reflows paragraphs");
/* the whole point of the module's shape: one walk, two entry points */
h_wide = n_gui_md_height(&fonts, &style, 760.0f, SAMPLE);
CHECK(h_wide > 0.0f, "a height for the sample");
al_clear_to_color(bg);
drawn = n_gui_md_draw(&fonts, &style, 20.0f, 20.0f, 760.0f, SAMPLE);
CHECK(drawn == h_wide, "draw returns exactly what measure said");
/* a narrower column wraps more, so it is taller; a measure is independent
of where it would be drawn */
h_narrow = n_gui_md_height(&fonts, &style, 300.0f, SAMPLE);
CHECK(h_narrow > h_wide, "a narrower column is taller");
/* the link is found where it was drawn, and nowhere else */
{
char url[256];
int found = 0, y;
/* walk down the left half of the laid-out page looking for it: the
exact line it lands on depends on the font, and the point of the
check is that the hit test agrees with the draw at all */
for (y = 20; y < 20 + (int)h_wide && !found; y++) {
int x;
for (x = 20; x < 780 && !found; x += 4)
found = n_gui_md_link_at(&fonts, &style, 20.0f, 20.0f, 760.0f, SAMPLE,
(float)x, (float)y, url, sizeof(url));
}
CHECK(found, "the link is somewhere in the laid-out page");
if (found)
CHECK(strcmp(url, "https://example.com/page") == 0, "and it is the right url");
url[0] = 'x';
CHECK(!n_gui_md_link_at(&fonts, &style, 20.0f, 20.0f, 760.0f, SAMPLE,
-100.0f, -100.0f, url, sizeof(url)),
"no link outside the page");
CHECK(url[0] == '\0', "a miss clears the url rather than leaving it stale");
}
/* the awkward inputs */
{
CHECK(n_gui_md_height(&fonts, &style, 300.0f, NULL) == 0.0f, "NULL text has no height");
CHECK(n_gui_md_height(&fonts, &style, 300.0f, "") == 0.0f, "empty text has no height");
CHECK(n_gui_md_height(NULL, &style, 300.0f, SAMPLE) == 0.0f, "no fonts, no height");
CHECK(n_gui_md_height(&fonts, NULL, 300.0f, SAMPLE) == 0.0f, "no style, no height");
/* a width of zero or less must terminate, not spin: every flow clamps
to one pixel, so a glyph always fits somewhere */
CHECK(n_gui_md_height(&fonts, &style, 0.0f, SAMPLE) > 0.0f, "a zero width still lays out");
CHECK(n_gui_md_height(&fonts, &style, -10.0f, SAMPLE) > 0.0f, "a negative width too");
}
/* hard_breaks is what a chat message wants, and it makes a difference */
{
N_GUI_MD_STYLE chat = style;
float soft, hard;
chat.parse_flags = 0;
soft = n_gui_md_height(&fonts, &chat, 760.0f, "one\ntwo\nthree\n");
chat.hard_breaks = 1;
hard = n_gui_md_height(&fonts, &chat, 760.0f, "one\ntwo\nthree\n");
CHECK(hard > soft, "hard breaks keep the three lines the source has");
}
/* ---- the widget over it ---------------------------------------------- */
{
CHECK(gui != NULL, "a gui context");
if (gui) {
int win = n_gui_add_window(gui, "Doc", 0, 0, 400, 300);
int mv = n_gui_add_markdownview(gui, win, 10, 10, 360, 240, &fonts);
CHECK(mv >= 0, "a markdownview");
"the text starts empty rather than NULL");
"set_text round-trips");
/* the height is not known until it has been laid out once, which
is the draw: that is the contract, and it is why the bar cannot
disagree with the page */
"no content height before the first draw");
al_clear_to_color(bg);
"the sample overflows a 240px pane, so there is a bar");
/* the scroll clamps to what there is, in both directions */
"a scroll past the end is clamped");
CHECK(n_gui_markdownview_get_scroll(gui, mv) == 0.0f, "and so is one before it");
/* asking for the style hands over the colours for good */
CHECK(live != NULL, "the live style");
if (live) {
live->link = al_map_rgb(255, 0, 0);
CHECK(live->link.r > 0.9f, "a colour set by the caller survives a draw");
}
CHECK(strcmp(n_gui_markdownview_get_text(gui, mv), "") == 0,
"NULL text clears rather than crashes");
/* the awkward ids. A window id is not tested against: window and
widget ids are separate spaces, so one can legitimately name a
widget. A widget of another type is the real guard. */
CHECK(n_gui_markdownview_style(gui, 12345) == NULL, "no such widget, no style");
{
int other = n_gui_add_label(gui, win, "not a view", 10, 260, 100, 20,
CHECK(other >= 0, "a label to test the type guard with");
"another widget type is not a markdownview");
"and has no markdown text");
}
CHECK(gui == NULL, "the context is nulled on destroy");
}
}
if (out_path) {
ALLEGRO_BITMAP* page = al_create_bitmap(800, (int)h_wide + 40);
if (page) {
al_set_target_bitmap(page);
al_clear_to_color(bg);
(void)n_gui_md_draw(&fonts, &style, 20.0f, 20.0f, 760.0f, SAMPLE);
if (al_save_bitmap(out_path, page))
n_log(LOG_NOTICE, "wrote %s", out_path);
al_set_target_backbuffer(display);
al_destroy_bitmap(page);
}
}
/* The caller owns the faces: n_gui_md never took them. */
al_destroy_font(fonts.regular);
al_destroy_font(fonts.bold);
al_destroy_font(fonts.italic);
al_destroy_font(fonts.bolditalic);
al_destroy_font(fonts.mono);
al_destroy_font(fonts.h1);
al_destroy_font(fonts.h2);
al_destroy_font(fonts.h3);
al_destroy_display(display);
if (failures) {
n_log(LOG_ERR, "ex_gui_md: %d failure(s)", failures);
return 1;
}
n_log(LOG_NOTICE, "ex_gui_md: all checks passed");
return 0;
}
static int failures
Definition ex_atlas.c:51
int main(void)
Definition ex_atlas.c:117
#define CHECK(cond, msg)
ALLEGRO_DISPLAY * display
Definition ex_fluid.c:54
static N_GUI_CTX * gui
static ALLEGRO_FONT * face(const char *name, int px)
Load a face, trying the usual places; NULL when there is none, which the renderer is documented to su...
Definition ex_gui_md.c:87
N_GUI_MD_STYLE * n_gui_markdownview_style(N_GUI_CTX *ctx, int widget_id)
the view's style, to read or to change in place, or NULL if there is no such widget.
Definition n_gui.c:5881
#define N_GUI_ALIGN_LEFT
left aligned text
Definition n_gui.h:212
int n_gui_add_markdownview(N_GUI_CTX *ctx, int window_id, float x, float y, float w, float h, const N_GUI_MD_FONTS *fonts)
add a read-only scrolling Markdown view.
Definition n_gui.c:5814
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
void n_gui_markdownview_scroll_to(N_GUI_CTX *ctx, int widget_id, float y)
scroll to y pixels down the content, clamped to what there is
Definition n_gui.c:5893
void n_gui_draw(N_GUI_CTX *ctx)
draw all visible GUI windows and widgets
Definition n_gui.c:11735
void n_gui_markdownview_set_text(N_GUI_CTX *ctx, int widget_id, const char *text)
set the Markdown source (copied; NULL clears it).
Definition n_gui.c:5852
float n_gui_markdownview_get_scroll(N_GUI_CTX *ctx, int widget_id)
how far down the content the view is, in pixels
Definition n_gui.c:5903
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
void n_gui_destroy_ctx(N_GUI_CTX **ctx)
Destroy a GUI context and all its windows/widgets.
Definition n_gui.c:1209
const char * n_gui_markdownview_get_text(N_GUI_CTX *ctx, int widget_id)
the Markdown source, owned by the widget, or NULL
Definition n_gui.c:5867
float n_gui_markdownview_content_height(N_GUI_CTX *ctx, int widget_id)
the height of the whole document at the width it was last drawn at, or 0 before the first draw
Definition n_gui.c:5908
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_NOTICE
normal but significant condition
Definition n_log.h:104
a sound sample
GUI system: buttons, sliders, text areas, checkboxes, scrollbars, dropdown menus, windows.
int n_gui_md_link_at(const N_GUI_MD_FONTS *fonts, const N_GUI_MD_STYLE *style, float x, float y, float width, const char *text, float px, float py, char *url, size_t url_cap)
the url of the link under (px, py), if there is one
Definition n_gui_md.c:663
float n_gui_md_draw(const N_GUI_MD_FONTS *fonts, const N_GUI_MD_STYLE *style, float x, float y, float width, const char *text)
draw text as markdown at (x, y), wrapped to width
Definition n_gui_md.c:657
void n_gui_md_style_default(N_GUI_MD_STYLE *out, ALLEGRO_COLOR text, ALLEGRO_COLOR background, float line_height)
fill a style from two colours and a line height
Definition n_gui_md.c:622
float n_gui_md_height(const N_GUI_MD_FONTS *fonts, const N_GUI_MD_STYLE *style, float width, const char *text)
height of text laid out as markdown at width, in pixels
Definition n_gui_md.c:651
Lay out and draw n_markdown's block model with Allegro fonts.
int hard_breaks
Non-zero to break a paragraph where its source breaks, which is what a chat message wants: somebody p...
Definition n_gui_md.h:122
ALLEGRO_FONT * bold
bold
Definition n_gui_md.h:69
ALLEGRO_COLOR link
link text
Definition n_gui_md.h:93
ALLEGRO_FONT * regular
body text, and the fallback for every other face.
Definition n_gui_md.h:67
ALLEGRO_FONT * h1
"# " headings, ideally the bold face at a larger size
Definition n_gui_md.h:77
ALLEGRO_FONT * bolditalic
both at once
Definition n_gui_md.h:73
ALLEGRO_FONT * h3
"### " and deeper
Definition n_gui_md.h:81
unsigned parse_flags
n_markdown parse flags.
Definition n_gui_md.h:113
ALLEGRO_FONT * mono
code and fenced code blocks
Definition n_gui_md.h:75
ALLEGRO_FONT * h2
"## " headings
Definition n_gui_md.h:79
ALLEGRO_FONT * italic
italic
Definition n_gui_md.h:71
The faces a render uses.
Definition n_gui_md.h:65
Colours and metrics.
Definition n_gui_md.h:87
Generic log system.
#define N_MARKDOWN_ALL
every block kind this parser knows.
Definition n_markdown.h:90