6 n__gui__md_8h
gullradriel edited this page 2026-10-09 00:55:18 +02:00
This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

include/nilorea/n_gui_md.h

Lay out and draw n_markdown's block model with Allegro fonts. More...

Structures

Name
struct N_GUI_MD_STYLE
Colours and metrics.
struct N_GUI_MD_FONTS
The faces a render uses.

Functions

Name
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
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_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
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

Detailed Description

Lay out and draw n_markdown's block model with Allegro fonts.

Author: Castagnier Mickael

Version: 1.0

The renderer half of n_markdown: it takes the text, parses it, and turns the blocks and styled spans into glyph runs at a given width. Bold, italic and code runs get their own face, a link gets its own colour and can be hit-tested, and paragraphs, headings, quotes, code blocks, lists and tables get vertical structure.

n_gui_md_height() and n_gui_md_draw() walk identically – the measure pass is the draw pass with the per-line callback left off – so a caller that caches a height cannot drift from what is painted. n_gui_md_link_at() is the same walk again.

It depends on Allegro and on n_markdown, and deliberately not on n_gui: a caller lays these out inside its own chat bubble, list row or window without the widget system, and N_GUI_TYPE_MARKDOWNVIEW is a thin widget over this for the callers that do want one.

Nothing here is retained: every call parses, lays out and frees. A caller drawing the same long text every frame should be doing it only on the frames it actually redraws.

Functions Documentation

function n_gui_md_style_default

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

Parameters:

  • out the style to fill. Must not be NULL.
  • text body text colour
  • background what the text is drawn on, used to derive the code panel
  • line_height the regular face's line height, in pixels

The derived colours are the obvious ones: a heading and body text share a colour, a link is blue, code sits on a panel lifted or dropped from the background depending on which is darker. The metrics are fractions of line_height, so they follow the font.

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

Parameters:

  • fonts the faces. Must not be NULL, nor fonts->regular.
  • style colours and metrics. Must not be NULL.
  • x left edge the text would be drawn at
  • y top edge
  • width the width it would wrap to
  • text the markdown source. NULL is treated as empty.
  • px the point to test
  • py the point to test
  • url receives the url, NUL-terminated and bounded by url_cap
  • url_cap size of url

Return: 1 when a link was under the point and url was written, else 0

Lays the text out exactly as n_gui_md_draw() would at the same position and width, and reports the link whose glyph cell the point falls in.

function n_gui_md_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

Parameters:

  • fonts the faces. Must not be NULL, nor fonts->regular.
  • style colours and metrics. Must not be NULL.
  • width the width to wrap to, in pixels
  • text the markdown source. NULL is treated as empty.

Return: the height in pixels, never negative

The same walk n_gui_md_draw() makes, with nothing drawn, so the two agree to the pixel. Independent of any y: measuring at the origin gives the height a draw anywhere will use.

function n_gui_md_draw

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

Parameters:

  • fonts the faces. Must not be NULL, nor fonts->regular.
  • style colours and metrics. Must not be NULL.
  • x left edge
  • y top edge
  • width the width to wrap to, in pixels
  • text the markdown source. NULL is treated as empty.

Return: the height used, equal to n_gui_md_height() for the same inputs

Draws into the current Allegro target with the current transform and clipping rectangle; a caller wanting the overflow clipped sets one.

Source code

/*
 * 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
 */


#ifndef N_GUI_MD_H
#define N_GUI_MD_H

#ifdef __cplusplus
extern "C" {
#endif

#include <allegro5/allegro.h>
#include <allegro5/allegro_font.h>

#include <nilorea/n_markdown.h>

#include <stddef.h>

typedef struct N_GUI_MD_FONTS {
    ALLEGRO_FONT* regular;
    ALLEGRO_FONT* bold;
    ALLEGRO_FONT* italic;
    ALLEGRO_FONT* bolditalic;
    ALLEGRO_FONT* mono;
    ALLEGRO_FONT* h1;
    ALLEGRO_FONT* h2;
    ALLEGRO_FONT* h3;
} N_GUI_MD_FONTS;

typedef struct N_GUI_MD_STYLE {
    ALLEGRO_COLOR text;
    ALLEGRO_COLOR heading;
    ALLEGRO_COLOR link;
    ALLEGRO_COLOR code;
    ALLEGRO_COLOR code_bg;
    ALLEGRO_COLOR quote;
    ALLEGRO_COLOR rule;
    float block_gap;
    float indent;
    float cell_pad;
    float col_min;
    unsigned parse_flags;
    int hard_breaks;
} N_GUI_MD_STYLE;

void n_gui_md_style_default(N_GUI_MD_STYLE* out, ALLEGRO_COLOR text, ALLEGRO_COLOR background, float line_height);

float n_gui_md_height(const N_GUI_MD_FONTS* fonts, const N_GUI_MD_STYLE* style, float width, const char* text);

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);

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);

#ifdef __cplusplus
}
#endif

#endif /* N_GUI_MD_H */