GFXprim
2D bitmap graphics library with emphasis on speed and correctness
Loading...
Searching...
No Matches
gp_markup_builder.h
Go to the documentation of this file.
1// SPDX-License-Identifier: LGPL-2.1-or-later
2/*
3 * Copyright (C) 2022-2023 Cyril Hrubis <metan@ucw.cz>
4 */
5
15#ifndef UTILS_GP_MARKUP_BUILDER_H
16#define UTILS_GP_MARKUP_BUILDER_H
17
18#include <utils/gp_markup.h>
19
23typedef struct gp_markup_builder {
24 size_t glyph_cnt;
25 gp_markup_glyph *glyphs;
27
37 uint32_t glyph, uint8_t fmt, uint16_t fg_color)
38{
39 if (!self->glyphs)
40 goto ret;
41
42 self->glyphs[self->glyph_cnt].glyph = glyph;
43 self->glyphs[self->glyph_cnt].fmt = fmt;
44 self->glyphs[self->glyph_cnt].fg_color = fg_color;
45
46ret:
47 self->glyph_cnt++;
48}
49
56static inline void gp_markup_builder_space(gp_markup_builder *self, uint8_t fmt)
57{
58 if (!self->glyphs)
59 goto ret;
60
61 if (!self->glyph_cnt)
62 return;
63
64 if (self->glyphs[self->glyph_cnt-1].glyph == ' ' ||
65 self->glyphs[self->glyph_cnt-1].glyph == '\n')
66 return;
67
68 self->glyphs[self->glyph_cnt].glyph = ' ';
69 self->glyphs[self->glyph_cnt].fmt = fmt;
70 self->glyphs[self->glyph_cnt].space_padd = 0;
71
72ret:
73 self->glyph_cnt++;
74}
75
82{
83 if (!self->glyphs)
84 goto ret;
85
86 self->glyphs[self->glyph_cnt].glyph = '\n';
87 self->glyphs[self->glyph_cnt].fmt = 0;
88
89ret:
90 self->glyph_cnt++;
91}
92
99{
100 if (!self->glyphs)
101 goto ret;
102
103 self->glyphs[self->glyph_cnt].glyph = '\n';
104 self->glyphs[self->glyph_cnt].fmt = GP_MARKUP_STRIKE;
105
106ret:
107 self->glyph_cnt++;
108}
109
116{
117 if (!self->glyphs)
118 return;
119
120 if (self->glyph_cnt && self->glyphs[self->glyph_cnt - 1].glyph == ' ')
121 self->glyph_cnt--;
122
123 self->glyphs[self->glyph_cnt].glyph = 0;
124 self->glyphs[self->glyph_cnt].fmt = 0;
125}
126
134{
135 gp_markup *ret = malloc(sizeof(gp_markup) + sizeof(gp_markup_glyph) * (self->glyph_cnt + 1));
136
137 if (!ret)
138 return NULL;
139
140 self->glyphs = ret->glyphs;
141 self->glyph_cnt = 0;
142
143 return ret;
144}
145
146#endif /* UTILS_GP_MARKUP_BUILDER_H */
A simple text markup format.
@ GP_MARKUP_STRIKE
Definition gp_markup.h:36
static void gp_markup_builder_finish(gp_markup_builder *self)
Finishes a markup.
static gp_markup * gp_markup_builder_alloc(gp_markup_builder *self)
Allocates a buffer for a markup.
static void gp_markup_builder_glyph(gp_markup_builder *self, uint32_t glyph, uint8_t fmt, uint16_t fg_color)
Appends a markup glyph.
static void gp_markup_builder_space(gp_markup_builder *self, uint8_t fmt)
Appends a markup space.
static void gp_markup_builder_newline(gp_markup_builder *self)
Appends a markup newline.
static void gp_markup_builder_hline(gp_markup_builder *self)
Appends a markup horizontal line.
A markup builder.
A markup glyph.
Definition gp_markup.h:67
uint16_t fmt
A text format.
Definition gp_markup.h:75
uint32_t glyph
An unicode glyph.
Definition gp_markup.h:69
uint16_t fg_color
A text color.
Definition gp_markup.h:82
A text markup.
Definition gp_markup.h:97