GFXprim
2D bitmap graphics library with emphasis on speed and correctness
Loading...
Searching...
No Matches
gp_text.h
1// SPDX-License-Identifier: LGPL-2.1-or-later
2/*
3 * Copyright (C) 2009-2011 Jiri "BlueBear" Dluhos
4 * <jiri.bluebear.dluhos@gmail.com>
5 *
6 * Copyright (C) 2009-2023 Cyril Hrubis <metan@ucw.cz>
7 */
8
9#ifndef TEXT_GP_TEXT_H
10#define TEXT_GP_TEXT_H
11
12#include <stdarg.h>
13
14#include <core/gp_types.h>
15#include <text/gp_text_style.h>
16#include <text/gp_text_metric.h>
17#include <text/gp_fonts.h>
18
19/* How the rendered text should be aligned.
20 * For gp_text(), the alignment is relative to the specified point:
21 *
22 * - GP_ALIGN_LEFT draws the text to the left of the point,
23 * - GP_ALIGN_CENTER centers it at the point horizontally,
24 * - GP_ALIGN_RIGHT draws the text to the right of the point
25 * - GP_TEXT_BEARING apply bearing at the start of the string
26 * - GP_VALIGN_ABOVE (or TOP) draws the text above the point
27 * - GP_VALIGN_CENTER centers the text vertically at the point
28 * - GP_VALIGN_BASELINE places the text baseline at the point
29 * - GP_VALIGN_BELOW (or BOTTOM) draws the text below the point
30 * - GP_TEXT_NOBG mix the alpha pixels with data read from the pixmap
31 * rather than mixing them with bg_color
32 * (which is slightly slower)
33 */
34typedef enum gp_text_attr {
35 GP_ALIGN_LEFT = 0x01,
36 GP_ALIGN_CENTER = 0x02,
37 GP_ALIGN_RIGHT = 0x03,
38 GP_ALIGN_HORIZ = 0x03,
39 GP_TEXT_BEARING = 0x04,
40 GP_VALIGN_ABOVE = 0x10,
41 GP_VALIGN_TOP = GP_VALIGN_ABOVE,
42 GP_VALIGN_CENTER = 0x20,
43 GP_VALIGN_BASELINE = 0x30,
44 GP_VALIGN_BELOW = 0x40,
45 GP_VALIGN_BOTTOM = GP_VALIGN_BELOW,
46 GP_TEXT_NOBG = 0x80,
47} gp_text_attr;
48
49/*
50 * Raw version, doesn't use Text aligment.
51 *
52 * If flags are set to GP_TEXT_NOBG the the bg_color is ignored and
53 * the pixmap pixels are used for alpha mixing.
54 */
55gp_size gp_text_raw(gp_pixmap *pixmap, const gp_text_style *style,
56 gp_coord x, gp_coord y, uint8_t flags,
57 gp_pixel fg_color, gp_pixel bg_color,
58 const char *str, size_t max_chars);
59
60/*
61 * Draws a string.
62 *
63 * The string is rendered to pixmap (horizontally) with defined text style.
64 * The x and y coordinates determines point defined by aligment flags.
65 *
66 * The background color is ignored for 1bpp font formats.
67 */
68gp_size gp_text(gp_pixmap *pixmap, const gp_text_style *style,
69 gp_coord x, gp_coord y, int align,
70 gp_pixel fg_color, gp_pixel bg_color, const char *str);
71
94gp_size gp_glyph_draw(gp_pixmap *pixmap, const gp_text_style *style,
95 gp_coord x, gp_coord y, int flags,
96 gp_pixel fg_color, gp_pixel bg_color,
97 uint32_t glyph);
98
99/*
100 * Aligns a string between x1 and x2 based ond horizontal aligment flags. The
101 * caller must make sure that the string will fit horizontally between x1 and x2.
102 */
103static inline gp_size gp_text_xxy(gp_pixmap *pixmap, const gp_text_style *style,
104 gp_coord x1, gp_coord x2, gp_coord y, int align,
105 gp_pixel fg_color, gp_pixel bg_color, const char *str)
106{
107 gp_coord x, len;
108
109 if (x1 < x2) {
110 x = x1;
111 len = x2 - x1;
112 } else {
113 x = x2;
114 len = x1 - x2;
115 }
116
117 int halign = align & GP_ALIGN_HORIZ;
118
119 align &= ~GP_ALIGN_HORIZ;
120
121 switch (halign) {
122 case GP_ALIGN_LEFT:
123 align |= GP_ALIGN_RIGHT;
124 break;
125 case GP_ALIGN_CENTER:
126 x += len/2;
127 align |= GP_ALIGN_CENTER;
128 break;
129 case GP_ALIGN_RIGHT:
130 x += len;
131 align |= GP_ALIGN_LEFT;
132 break;
133 }
134
135 return gp_text(pixmap, style, x, y, align, fg_color, bg_color, str);
136}
137
138/*
139 * Same as the gp_text() but the number of characters can be limited.
140 */
141gp_size gp_text_ext(gp_pixmap *pixmap, const gp_text_style *style,
142 gp_coord x, gp_coord y, int align,
143 gp_pixel fg_color, gp_pixel bg_color,
144 const char *str, size_t max_chars);
145
146/*
147 * Same as above, but printf like and returns text width in pixels.
148 */
149gp_size gp_print(gp_pixmap *pixmap, const gp_text_style *style,
150 gp_coord x, gp_coord y, int align,
151 gp_pixel fg_color, gp_pixel bg_color, const char *fmt, ...)
152 __attribute__ ((format (printf, 8, 9)));
153
154
155gp_size gp_vprint(gp_pixmap *pixmap, const gp_text_style *style,
156 gp_coord x, gp_coord y, int align,
157 gp_pixel fg_color, gp_pixel bg_color,
158 const char *fmt, va_list va);
159
160__attribute__ ((format (printf, 9, 10)))
161static inline gp_size gp_print_xxy(gp_pixmap *pixmap, const gp_text_style *style,
162 gp_coord x1, gp_coord x2, gp_coord y, int align,
163 gp_pixel fg_color, gp_pixel bg_color, const char *fmt, ...)
164{
165 gp_coord x, len;
166 gp_size ret;
167 va_list va;
168
169 if (x1 < x2) {
170 x = x1;
171 len = x2 - x1;
172 } else {
173 x = x2;
174 len = x1 - x2;
175 }
176
177 int halign = align & GP_ALIGN_HORIZ;
178
179 align &= ~GP_ALIGN_HORIZ;
180
181 switch (halign) {
182 case GP_ALIGN_LEFT:
183 align |= GP_ALIGN_RIGHT;
184 break;
185 case GP_ALIGN_CENTER:
186 x += len/2;
187 align |= GP_ALIGN_CENTER;
188 break;
189 case GP_ALIGN_RIGHT:
190 x += len;
191 align |= GP_ALIGN_LEFT;
192 break;
193 }
194
195 va_start(va, fmt);
196 ret = gp_vprint(pixmap, style, x, y, align, fg_color, bg_color, fmt, va);
197 va_end(va);
198
199 return ret;
200}
201
202/*
203 * Clears rectangle that would be used to draw text of size pixels.
204 */
205void gp_text_clear(gp_pixmap *pixmap, const gp_text_style *style,
206 gp_coord x, gp_coord y, int align,
207 gp_pixel bg_color, gp_size size);
208
209/*
210 * Dtto, but with string.
211 */
212void gp_text_clear_str(gp_pixmap *pixmap, const gp_text_style *style,
213 gp_coord x, gp_coord y, int align,
214 gp_pixel bg_color, const char *str);
215
216#endif /* TEXT_GP_TEXT_H */
A common types.
uint32_t gp_pixel
Pixel integer value.
Definition gp_types.h:33
int gp_coord
Integer type for coordinates i.e. x, y, ...
Definition gp_types.h:19
unsigned int gp_size
Integer type for sizes i.e. w, h, ...
Definition gp_types.h:24
A pixmap buffer.
Definition gp_pixmap.h:33