- added libdrawtext
[demo_prior] / libs / drawtext / src / drawtext.h
1 /*
2 libdrawtext - a simple library for fast text rendering in OpenGL
3 Copyright (C) 2011-2019  John Tsiombikas <nuclear@member.fsf.org>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18 #ifndef LIBDRAWTEXT_H_
19 #define LIBDRAWTEXT_H_
20
21 #include <stdio.h>
22 #include <stdlib.h>
23
24 struct dtx_font;
25 struct dtx_glyphmap;
26
27 /* draw buffering modes */
28 enum {
29         DTX_NBF = 0,/* unbuffered */
30         DTX_LBF,        /* line buffered */
31         DTX_FBF         /* fully buffered */
32 };
33
34 /* glyphmap resize filtering */
35 enum {
36         DTX_NEAREST,
37         DTX_LINEAR
38 };
39
40 struct dtx_box {
41         float x, y;
42         float width, height;
43 };
44
45 #ifdef __cplusplus
46 extern "C" {
47 #endif
48
49 /* Open a truetype/opentype/whatever font.
50  *
51  * If sz is non-zero, the default ASCII glyphmap at the requested point size is
52  * automatically created as well, and ready to use.
53  *
54  * To use other unicode ranges and different font sizes you must first call
55  * dtx_prepare or dtx_prepare_range before issuing any drawing calls, otherwise
56  * nothing will be rendered.
57  */
58 struct dtx_font *dtx_open_font(const char *fname, int sz);
59 /* same as dtx_open_font, but open from a memory buffer instead of a file */
60 struct dtx_font *dtx_open_font_mem(void *ptr, int memsz, int fontsz);
61 /* open a font by loading a precompiled glyphmap (see: dtx_save_glyphmap)
62  * this works even when compiled without freetype support
63  */
64 struct dtx_font *dtx_open_font_glyphmap(const char *fname);
65 /* same as dtx_open_font_glyphmap, but open from a memory buffer instead of a file */
66 struct dtx_font *dtx_open_font_glyphmap_mem(void *ptr, int memsz);
67 /* close a font opened by either of the above */
68 void dtx_close_font(struct dtx_font *fnt);
69
70 /* prepare an ASCII glyphmap for the specified font size */
71 void dtx_prepare(struct dtx_font *fnt, int sz);
72 /* prepare an arbitrary unicode range glyphmap for the specified font size */
73 void dtx_prepare_range(struct dtx_font *fnt, int sz, int cstart, int cend);
74
75 /* convert all glyphmaps to distance fields for use with the distance field
76  * font rendering algorithm. This is a convenience function which calls
77  * dtx_calc_glyphmap_distfield and
78  * dtx_resize_glyphmap(..., scale_numer, scale_denom, DTX_LINEAR) for each
79  * glyphmap in this font.
80  */
81 int dtx_calc_font_distfield(struct dtx_font *fnt, int scale_numer, int scale_denom);
82
83 /* Finds the glyphmap that contains the specified character code and matches the requested size
84  * Returns null if it hasn't been created (you should call dtx_prepare/dtx_prepare_range).
85  */
86 struct dtx_glyphmap *dtx_get_font_glyphmap(struct dtx_font *fnt, int sz, int code);
87
88 /* Finds the glyphmap that contains the specified unicode range and matches the requested font size
89  * Will automatically generate one if it can't find it.
90  */
91 struct dtx_glyphmap *dtx_get_font_glyphmap_range(struct dtx_font *fnt, int sz, int cstart, int cend);
92
93 /* returns the number of glyphmaps in this font */
94 int dtx_get_num_glyphmaps(struct dtx_font *fnt);
95 /* returns the Nth glyphmap of this font */
96 struct dtx_glyphmap *dtx_get_glyphmap(struct dtx_font *fnt, int idx);
97
98 /* Creates and returns a glyphmap for a particular unicode range and font size.
99  * The generated glyphmap is added to the font's list of glyphmaps.
100  */
101 struct dtx_glyphmap *dtx_create_glyphmap_range(struct dtx_font *fnt, int sz, int cstart, int cend);
102 /* free a glyphmap */
103 void dtx_free_glyphmap(struct dtx_glyphmap *gmap);
104
105 /* converts a glyphmap to a distance field glyphmap, for use with the distance
106  * field font rendering algorithm.
107  *
108  * It is recommended to use a fairly large font size glyphmap for this, and
109  * then shrink the resulting distance field glyphmap as needed, with
110  * dtx_resize_glyphmap
111  */
112 int dtx_calc_glyphmap_distfield(struct dtx_glyphmap *gmap);
113
114 /* resize a glyphmap by the provided scale factor fraction snum/sdenom
115  * in order to maintain the power of 2 invariant, scaling fractions are only
116  * allowed to be of the form 1/x or x/1, where x is a power of 2
117  */
118 int dtx_resize_glyphmap(struct dtx_glyphmap *gmap, int snum, int sdenom, int filter);
119
120 /* returns a pointer to the raster image of a glyphmap (1 byte per pixel grayscale) */
121 unsigned char *dtx_get_glyphmap_image(struct dtx_glyphmap *gmap);
122 /* returns the width of the glyphmap image in pixels */
123 int dtx_get_glyphmap_width(struct dtx_glyphmap *gmap);
124 /* returns the height of the glyphmap image in pixels */
125 int dtx_get_glyphmap_height(struct dtx_glyphmap *gmap);
126 /* returns the point size represented by this glyphmap */
127 int dtx_get_glyphmap_ptsize(struct dtx_glyphmap *gmap);
128
129 /* The following functions can be used even when the library is compiled without
130  * freetype support.
131  */
132 struct dtx_glyphmap *dtx_load_glyphmap(const char *fname);
133 struct dtx_glyphmap *dtx_load_glyphmap_stream(FILE *fp);
134 struct dtx_glyphmap *dtx_load_glyphmap_mem(void *ptr, int memsz);
135 int dtx_save_glyphmap(const char *fname, const struct dtx_glyphmap *gmap);
136 int dtx_save_glyphmap_stream(FILE *fp, const struct dtx_glyphmap *gmap);
137
138 /* adds a glyphmap to a font */
139 void dtx_add_glyphmap(struct dtx_font *fnt, struct dtx_glyphmap *gmap);
140
141
142 /* ---- options and flags ---- */
143 enum dtx_option {
144         /* options for the OpenGL renderer */
145         DTX_GL_ATTR_VERTEX,   /* vertex attribute location     (default: -1 for standard gl_Vertex) */
146         DTX_GL_ATTR_TEXCOORD, /* texture uv attribute location (default: -1 for gl_MultiTexCoord0) */
147         DTX_GL_ATTR_COLOR,        /* color attribute location      (default: -1 for gl_Color) */
148         /* options for the raster renderer */
149         DTX_RASTER_THRESHOLD, /* opaque/transparent threshold  (default: -1. fully opaque glyphs) */
150         DTX_RASTER_BLEND,     /* glyph alpha blending (0 or 1) (default: 0 (off)) */
151
152         /* generic options */
153         DTX_PADDING = 128,    /* padding between glyphs in pixels (default: 8) */
154         DTX_SAVE_PPM,         /* let dtx_save_glyphmap* save PPM instead of PGM (0 or 1) (default: 0 (PGM)) */
155
156         DTX_FORCE_32BIT_ENUM = 0x7fffffff       /* this is not a valid option */
157 };
158
159 void dtx_set(enum dtx_option opt, int val);
160 int dtx_get(enum dtx_option opt);
161
162
163 /* ---- rendering ---- */
164
165 /* the dtx_target_ functions select which rendering mode to use.
166  * default: opengl
167  */
168 void dtx_target_opengl(void);
169 /* pixels are expected to be RGBA ordered bytes, 4 per pixel
170  * text is rendered with pre-multiplied alpha
171  */
172 void dtx_target_raster(unsigned char *pixels, int width, int height);
173
174
175 /* data structures passed to user-supplied draw callback */
176 struct dtx_vertex { float x, y, s, t; };
177 struct dtx_pixmap {
178         unsigned char *pixels;  /* pixel buffer pointer (8 bits per pixel) */
179         int width, height;              /* dimensions of the pixel buffer */
180         void *udata;    /* user-supplied pointer to data associated with this
181                                          * pixmap. On the first callback invocation this pointer
182                                          * will be null. The user may set it to associate any extra
183                                          * data to this pixmap (such as texture structures or
184                                          * identifiers). Libdrawtext will never modify this pointer.
185                                          */
186 };
187
188 /* user-defined glyph drawing callback type (set with dtx_target_user)
189  * It's called when the output buffer is flushed, with a pointer to the vertex
190  * buffer that needs to be drawn (every 3 vertices forming a triangle), the
191  * number of vertices in the buffer, and a pointer to the current glyphmap
192  * atlas pixmap (see struct dtx_pixmap above).
193  */
194 typedef void (*dtx_user_draw_func)(struct dtx_vertex *v, int vcount,
195                 struct dtx_pixmap *pixmap, void *cls);
196
197 /* set user-supplied draw callback and optional closure pointer, which will
198  * be passed unchanged as the last argument on every invocation of the draw
199  * callback.
200  */
201 void dtx_target_user(dtx_user_draw_func drawfunc, void *cls);
202
203
204 /* position of the origin of the first character to be printed */
205 void dtx_position(float x, float y);
206 /* TODO currently only used by the raster renderer, implement in gl too */
207 void dtx_color(float r, float g, float b, float a);
208
209 /* before drawing anything this function must set the font to use */
210 void dtx_use_font(struct dtx_font *fnt, int sz);
211
212 /* sets the buffering mode
213  * - DTX_NBUF: every call to dtx_string gets rendered immediately.
214  * - DTX_LBUF: renders when the buffer is full or the string contains a newline.
215  * - DTX_FBUF: renders only when the buffer is full (you must call dtx_flush explicitly).
216  */
217 void dtx_draw_buffering(int mode);
218
219 /* Sets the vertex attribute indices to use for passing vertex and texture coordinate
220  * data. By default both are -1 which means you don't have to use a shader, and if you
221  * do they are accessible through gl_Vertex and gl_MultiTexCoord0, as usual.
222  *
223  * NOTE: If you are using OpenGL ES 2.x or OpenGL >= 3.1 core (non-compatibility)
224  * context you must specify valid attribute indices.
225  *
226  * NOTE2: equivalent to:
227  *    dtx_set(DTX_GL_ATTR_VERTEX, vert_attr);
228  *    dtx_set(DTX_GL_ATTR_TEXCOORD, tex_attr);
229  */
230 void dtx_vertex_attribs(int vert_attr, int tex_attr);
231
232 /* draws a single glyph at the origin */
233 void dtx_glyph(int code);
234 /* draws a utf-8 string starting at the origin. \n \r and \t are handled appropriately. */
235 void dtx_string(const char *str);
236 void dtx_substring(const char *str, int start, int end);
237
238 void dtx_printf(const char *fmt, ...);
239
240 /* render any pending glyphs (see dtx_draw_buffering) */
241 void dtx_flush(void);
242
243
244 /* ---- encodings ---- */
245
246 /* returns a pointer to the next character in a utf-8 stream */
247 char *dtx_utf8_next_char(char *str);
248
249 /* returns a pointer to the previous character in a utf-8 stream */
250 char *dtx_utf8_prev_char(char *ptr, char *first);
251
252 /* returns the unicode character codepoint of the utf-8 character starting at str */
253 int dtx_utf8_char_code(const char *str);
254
255 /* returns the number of bytes of the utf-8 character starting at str */
256 int dtx_utf8_nbytes(const char *str);
257
258 /* returns the number of utf-8 characters in a zero-terminated utf-8 string */
259 int dtx_utf8_char_count(const char *str);
260
261 /* returns the number of utf-8 characters in the next N bytes starting from str */
262 int dtx_utf8_char_count_range(const char *str, int nbytes);
263
264 /* Converts a unicode code-point to a utf-8 character by filling in the buffer
265  * passed at the second argument, and returns the number of bytes taken by that
266  * utf-8 character.
267  * It's valid to pass a null buffer pointer, in which case only the byte count is
268  * returned (useful to figure out how much memory to allocate for a buffer).
269  */
270 size_t dtx_utf8_from_char_code(int code, char *buf);
271
272 /* Converts a unicode utf-16 wchar_t string to utf-8, filling in the buffer passed
273  * at the second argument. Returns the size of the resulting string in bytes.
274  *
275  * It's valid to pass a null buffer pointer, in which case only the size gets
276  * calculated and returned, which is useful for figuring out how much memory to
277  * allocate for the utf-8 buffer.
278  */
279 size_t dtx_utf8_from_string(const wchar_t *str, char *buf);
280
281
282 /* ---- metrics ---- */
283 float dtx_line_height(void);
284 float dtx_baseline(void);
285
286 /* rendered dimensions of a single glyph */
287 void dtx_glyph_box(int code, struct dtx_box *box);
288 float dtx_glyph_width(int code);
289 float dtx_glyph_height(int code);
290
291 /* rendered dimensions of a string */
292 void dtx_string_box(const char *str, struct dtx_box *box);
293 void dtx_substring_box(const char *str, int start, int end, struct dtx_box *box);
294 float dtx_string_width(const char *str);
295 float dtx_string_height(const char *str);
296
297 /* returns the horizontal position of the n-th character of the rendered string
298  * (useful for placing cursors)
299  */
300 float dtx_char_pos(const char *str, int n);
301
302 int dtx_char_at_pt(const char *str, float pt);
303
304 #ifdef __cplusplus
305 }
306 #endif
307
308 #endif  /* LIBDRAWTEXT_H_ */