- Home: https://github.com/1Hyena/libamp
- Issue tracker: https://github.com/1Hyena/libamp/issues
In LibAMP, the letters AMP stand for "ANSI Map" (as in ANSI art), and the Lib prefix indicates that it is a library for the C programming language.
AnsMap (AMP) is a data structure that stores the instructions for printing ANSI art graphics to the terminal. The AnsMap Library is accompanied by a set of methods for ansmap construction and for the conversion of ansmap images to ANSI escape code sequences.
In this context, an ansmap is a digital text-based image represented as a grid of individual cells called glyphs. Each glyph stores color and style information to form a complete picture.
The AnsMap Library offers a specialized application programming interface (API) designed for creating terminal applications and games like multi-user dungeons (MUDs) and roguelikes. It includes the following features:
-
Header-only: The AnsMap Library is implemented in a single header file with no dependencies other than the standard C library.
-
Non-allocating: LibAMP does not involve any heap memory allocations.
-
UTF8-only: The library expects UTF-8 encoding of the input text and does not attempt to detect Unicode encoding errors.
-
Multi-palette: The generated ANSI escape sequences can include color codes either specific to the standard 16 color palette or to the 24 bit true color mode.
-
Text wrapping: The LibAMP API allows for multiline text printing with the user specified maximum line width parameter.
-
Inline style markers: In rich text printing, style information can be embedded into the source text at the location where the style needs to be applied.
-
Portable: LibAMP builds and functions on Linux. It should be relatively simple to make it run on most other platforms as long as the platform provides the C standard library.
-
Permissive license: LibAMP is available under the MIT license.
LibAMP uses a unique file format that is easily readable by humans, allowing for
convenient image editing using a standard text editor with a fixed-width font.
The recommended file extension for AMP files is .amp.
An AMP file is divided into layers, with the first layer dedicated to text content and required to be present, even if it has zero height (indicating no text to be printed on the ansmap). Subsequent layers are optional and contain style markers such as foreground color, background color, and text decoration specifications. Each style marker occupying the same cell must be represented by a different layer.
A valid AMP file begins with Unicode box-drawing characters ╔ and ═. The
number of ═ characters following the first character determines the width of
the ansmap. The sequence is terminated by ╗.
Subsequent lines must start with ║, ╠, or ╚. If a line starts with ║,
the count of ║ characters at the beginning of subsequent lines determines the
height of that layer. The layer with the greatest height defines the ansmap's
overall height.
If a line starts with ║, it must be followed by Unicode characters equal to
the ansmap's width, then another ║. This sequence defines the contents of one
row in a given layer. Lines starting with ╠ indicate the end of a layer and
the start of a new one if followed by ═ characters equal to the ansmap's width
and a ╣. Lines starting with ╚ mark the end of a layer and the end of the
AMP file if followed by ═ characters equal to the ansmap's width and a ╝.
Here is an example of the contents of ansiart.amp and its resulting ANSI escape sequences displayed in the terminal:
The letters and symbols in the second and third box in the example above serve as style markers for the cells in the first box. Each style marker applies to a single cell, and their effects can be combined by using multiple style layers. The position of a style marker on the first layer is determined by its placement when the style layer is superimposed on the first layer.
For the key of style markers, please refer to the ExRich example as it includes the list of available style markers and their definitions.
Clone the repository and include the header file in your project. Compile using a C compiler (C23 or later required).
The usage of the library can be divided into two main parts: creating and viewing. The following sections provide a brief overview of each.
To create an ansmap image, you can include the amp.h header file directly in your codebase. The library is implemented in a single C header file for easy integration.
The main functions to use are amp_init() and amp_print_text(). The
initialization function is necessary to specify the image resolution and data
buffer. The text printing function takes the initialized ansmap pointer as an
argument and prints the desired text on it.
While the initialization of the ansmap data structure typically requires an
external data buffer for storing the image state in memory, it can be omitted.
If omitted, the library will try to use the limited amount of integrated memory
associated with each ansmap. The amount of integrated memory is defined by the
AMP_BUF_SIZE macro, which the user can define according to their needs in each
compilation unit.
To determine the size of the external data buffer before initialization, the API
provides the amp_calc_size() function, which takes the image resolution as its
arguments.
To display an ansmap in the terminal, use the amp_to_ans() function. It
requires a pointer to the ansmap image structure and a pointer to the memory
location to copy the resulting ANSI escape code sequences that the terminal can
render. If nullptr is used for the memory location, the output will be written
to the standard output of the program.
To compile all the examples, run the make command in the examples directory.
The exunicode example demonstrates the library's ability to represent unicode glyphs on the ansmap. In addition, it features text wrapping.
libamp/examples/src/exunicode.c
Lines 21 to 36 in ce0207e
The exrich example prints the list of available rich text markers with the respective text samples.
Lines 29 to 41 in ce0207e
The exencode program generates rich text on an ansmap, displays the result in the terminal, and exports the ansmap image as an AMP document, which is also displayed in the terminal.
libamp/examples/src/exencode.c
Lines 15 to 25 in ce0207e
The exdecode example demonstrates how the human-readable AMP documents can be imported from memory.
libamp/examples/src/exdecode.c
Lines 7 to 40 in ce0207e
The exampfile example is very similar to ExDecode. The only difference is that instead of deserializing a hardcoded AMP document, the program loads a user specified AMP file from the file system.
The ex24bit example demonstrates how to use the 24 bit true color mode when converting the ansmap into a sequence of ANSI escape codes.
Lines 28 to 32 in ce0207e
The exmemory example shows how to allocate memory from the heap and use it for the storage of an ansmap image. It then fills the image with randomly chosen and styled printable ASCII characters.
libamp/examples/src/exmemory.c
Lines 17 to 49 in ce0207e
The extiles example shows how to fill a canvas with tiles from a different ansmap image.
Lines 111 to 133 in ce0207e
-
- amp_map_rgb (red, green, blue) →
struct amp_color_type - amp_unmap_rgb (color, &red, &green, &blue)
- amp_lookup_color (color index) →
struct amp_color_type
- amp_map_rgb (red, green, blue) →
-
- amp_calc_size (width, height) →
size_t - amp_init (&ansmap, width, height, &data, data size) →
size_t
- amp_calc_size (width, height) →
-
- amp_get_palette (&ansmap) →
AMP_PALETTE - amp_get_width (&ansmap) →
uint32_t - amp_get_height (&ansmap) →
uint32_t - amp_get_glyph (&ansmap, x, y) →
const char * - amp_get_style (&ansmap, x, y) →
AMP_STYLE - amp_get_bg_color (&ansmap, x, y) →
struct amp_color_type - amp_get_fg_color (&ansmap, x, y) →
struct amp_color_type
- amp_get_palette (&ansmap) →
-
- amp_clear (&ansmap)
- amp_print_glyph (&ansmap, x, y, style, &string)
- amp_print_line (&ansmap, x, y, style, alignment, &string)
- amp_snprint_linef (&, x, y, style, align, &buf, buf size, &fmt, ...) →
ssize_t - amp_print_text (&ansmap, x, y, style, max width, alignment, &string) →
size_t - amp_snprint_textf (&, x, y, style, max width, align, &buf, buf size, &fmt, ...) →
ssize_t - amp_print_rich_text (&ansmap, x, y, style, max width, alignment, &string) →
size_t - amp_snprint_rich_textf (&, x, y, style, max width, align, &buf, buf size, &fmt, ...) →
ssize_t - amp_put_glyph (&ansmap, x, y, &string) →
const char * - amp_put_style (&ansmap, x, y, style) →
bool - amp_set_bg_color (&ansmap, x, y, color) →
bool - amp_set_fg_color (&ansmap, x, y, color) →
bool - amp_draw_ansmap (&dst_amp, dst_x, dst_y, &src_amp) →
void - amp_draw_ansmap_region (&dst_amp, dst_x, dst_y, &src_amp, src_x, src_y, width, height) →
void
-
- amp_set_palette (&ansmap, palette)
- amp_to_ans (&ansmap, &data, data size) →
ssize_t - amp_row_to_ans (&ansmap, y, &data, data size) →
ssize_t - amp_clip_to_ans (&ansmap, x, y, width, &data, data size) →
ssize_t - amp_doc_parse_size (&data, data size, &width, &height) →
size_t - amp_decode (&ansmap, &data, data size) →
size_t - amp_encode (&ansmap, settings, &data, data size) →
ssize_t - amp_stdout (&string, string size) →
ssize_t
Lines 329 to 336 in ce0207e
Examples: ex24bit
Lines 338 to 346 in ce0207e
Lines 348 to 353 in ce0207e
Lines 127 to 133 in ce0207e
Lines 135 to 146 in ce0207e
Examples: exhello, exunicode, exmultiline, ex24bit, exmemory
Lines 391 to 394 in ce0207e
Lines 381 to 384 in ce0207e
Lines 386 to 389 in ce0207e
Lines 248 to 256 in ce0207e
Lines 270 to 276 in ce0207e
Lines 289 to 296 in ce0207e
Lines 309 to 316 in ce0207e
Lines 148 to 151 in ce0207e
Examples: exrich
Lines 161 to 169 in ce0207e
Examples: exmemory
Lines 171 to 181 in ce0207e
Examples: exhello
Lines 445 to 466 in ce0207e
Lines 183 to 197 in ce0207e
Examples: exmultiline, exunicode
Lines 396 to 418 in ce0207e
Lines 364 to 379 in ce0207e
Examples: exrich
Lines 420 to 443 in ce0207e
Examples: exrich
Lines 258 to 268 in ce0207e
Lines 278 to 287 in ce0207e
Lines 298 to 307 in ce0207e
Examples: ex24bit
Lines 318 to 327 in ce0207e
Lines 519 to 530 in ce0207e
Lines 532 to 548 in ce0207e
Examples: extiles
Lines 153 to 159 in ce0207e
Examples: ex24bit
Lines 199 to 212 in ce0207e
Examples: exhello, exunicode, exmultiline, ex24bit, exmemory
Lines 214 to 228 in ce0207e
Lines 230 to 246 in ce0207e
Lines 490 to 504 in ce0207e
Examples: exdecode
Lines 506 to 517 in ce0207e
Examples: exdecode
Lines 468 to 488 in ce0207e
Examples: exencode
Lines 355 to 362 in ce0207e
Examples: exhello, exunicode, exmultiline, ex24bit, exmemory
The AnsMap Library has been authored by Erich Erstu and is released under the MIT license.









