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.
Needs a display, because it measures real glyphs, so it is not in run_tests.sh with the headless examples; run it under Xvfb.
With -o FILE it also writes the rendered page out, which is how the layout gets looked at rather than only counted.
#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>
#define CHECK(cond, msg) \
do { \
if (!(cond)) { \
n_log(LOG_ERR, "FAIL: %s", (msg)); \
failures++; \
} \
} while (0)
"# 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_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()) {
return 1;
}
al_init_font_addon();
al_init_ttf_addon();
(void)al_init_image_addon();
if (!al_init_primitives_addon()) {
return 1;
}
display = al_create_display(900, 600);
n_log(
LOG_ERR,
"cannot create a display; run this under Xvfb");
return 1;
}
memset(&fonts, 0, sizeof(fonts));
fonts.
bold =
face(
"DejaVuSans-Bold.ttf", 14);
fonts.
italic =
face(
"DejaVuSans-Oblique.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);
fonts.
regular = al_create_builtin_font();
}
return 1;
CHECK(h_wide > 0.0f,
"a height for the sample");
al_clear_to_color(bg);
CHECK(drawn == h_wide,
"draw returns exactly what measure said");
CHECK(h_narrow > h_wide,
"a narrower column is taller");
{
char url[256];
int found = 0, y;
for (y = 20; y < 20 + (int)h_wide && !found; y++) {
int x;
for (x = 20; x < 780 && !found; x += 4)
(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';
-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");
}
{
}
{
float soft, hard;
CHECK(hard > soft,
"hard breaks keep the three lines the source has");
}
{
CHECK(mv >= 0,
"a markdownview");
"the text starts empty rather than NULL");
"set_text round-trips");
"no content height before the first draw");
al_clear_to_color(bg);
"the sample overflows a 240px pane, so there is a bar");
"a scroll past the end is clamped");
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");
}
"NULL text clears rather than crashes");
{
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);
if (al_save_bitmap(out_path, page))
al_destroy_bitmap(page);
}
}
al_destroy_font(fonts.
bold);
al_destroy_font(fonts.
italic);
al_destroy_font(fonts.
mono);
al_destroy_font(fonts.
h1);
al_destroy_font(fonts.
h2);
al_destroy_font(fonts.
h3);
return 1;
}
return 0;
}
ALLEGRO_DISPLAY * display
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...
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.
#define N_GUI_ALIGN_LEFT
left aligned text
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.
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.
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
void n_gui_draw(N_GUI_CTX *ctx)
draw all visible GUI windows and widgets
void n_gui_markdownview_set_text(N_GUI_CTX *ctx, int widget_id, const char *text)
set the Markdown source (copied; NULL clears it).
float n_gui_markdownview_get_scroll(N_GUI_CTX *ctx, int widget_id)
how far down the content the view is, in pixels
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.
void n_gui_destroy_ctx(N_GUI_CTX **ctx)
Destroy a GUI context and all its windows/widgets.
const char * n_gui_markdownview_get_text(N_GUI_CTX *ctx, int widget_id)
the Markdown source, owned by the widget, or NULL
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
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_NOTICE
normal but significant condition
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
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
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
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
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...
ALLEGRO_COLOR link
link text
ALLEGRO_FONT * regular
body text, and the fallback for every other face.
ALLEGRO_FONT * h1
"# " headings, ideally the bold face at a larger size
ALLEGRO_FONT * bolditalic
both at once
ALLEGRO_FONT * h3
"### " and deeper
unsigned parse_flags
n_markdown parse flags.
ALLEGRO_FONT * mono
code and fenced code blocks
ALLEGRO_FONT * h2
"## " headings
ALLEGRO_FONT * italic
italic
#define N_MARKDOWN_ALL
every block kind this parser knows.