rev |
line source |
nuclear@7
|
1 /*
|
nuclear@7
|
2 libdrawtext - a simple library for fast text rendering in OpenGL
|
nuclear@7
|
3 Copyright (C) 2011-2014 John Tsiombikas <nuclear@member.fsf.org>
|
nuclear@7
|
4
|
nuclear@7
|
5 This program is free software: you can redistribute it and/or modify
|
nuclear@7
|
6 it under the terms of the GNU Lesser General Public License as published by
|
nuclear@7
|
7 the Free Software Foundation, either version 3 of the License, or
|
nuclear@7
|
8 (at your option) any later version.
|
nuclear@7
|
9
|
nuclear@7
|
10 This program is distributed in the hope that it will be useful,
|
nuclear@7
|
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
nuclear@7
|
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
nuclear@7
|
13 GNU Lesser General Public License for more details.
|
nuclear@7
|
14
|
nuclear@7
|
15 You should have received a copy of the GNU Lesser General Public License
|
nuclear@7
|
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
|
nuclear@7
|
17 */
|
nuclear@7
|
18 #ifndef LIBDRAWTEXT_H_
|
nuclear@7
|
19 #define LIBDRAWTEXT_H_
|
nuclear@7
|
20
|
nuclear@7
|
21 #include <stdio.h>
|
nuclear@7
|
22 #include <stdlib.h>
|
nuclear@7
|
23 #include "assman.h"
|
nuclear@7
|
24
|
nuclear@7
|
25 struct dtx_font;
|
nuclear@7
|
26 struct dtx_glyphmap;
|
nuclear@7
|
27
|
nuclear@7
|
28 /* draw buffering modes */
|
nuclear@7
|
29 enum {
|
nuclear@7
|
30 DTX_NBF, /* unbuffered */
|
nuclear@7
|
31 DTX_LBF, /* line buffered */
|
nuclear@7
|
32 DTX_FBF /* fully buffered */
|
nuclear@7
|
33 };
|
nuclear@7
|
34
|
nuclear@7
|
35 struct dtx_box {
|
nuclear@7
|
36 float x, y;
|
nuclear@7
|
37 float width, height;
|
nuclear@7
|
38 };
|
nuclear@7
|
39
|
nuclear@7
|
40 #ifdef __cplusplus
|
nuclear@7
|
41 extern "C" {
|
nuclear@7
|
42 #endif
|
nuclear@7
|
43
|
nuclear@7
|
44 /* Open a truetype/opentype/whatever font.
|
nuclear@7
|
45 *
|
nuclear@7
|
46 * If sz is non-zero, the default ASCII glyphmap at the requested point size is
|
nuclear@7
|
47 * automatically created as well, and ready to use.
|
nuclear@7
|
48 *
|
nuclear@7
|
49 * To use other unicode ranges and different font sizes you must first call
|
nuclear@7
|
50 * dtx_prepare or dtx_prepare_range before issuing any drawing calls, otherwise
|
nuclear@7
|
51 * nothing will be rendered.
|
nuclear@7
|
52 */
|
nuclear@7
|
53 struct dtx_font *dtx_open_font(const char *fname, int sz);
|
nuclear@7
|
54 /* open a font by loading a precompiled glyphmap (see: dtx_save_glyphmap)
|
nuclear@7
|
55 * this works even when compiled without freetype support
|
nuclear@7
|
56 */
|
nuclear@7
|
57 struct dtx_font *dtx_open_font_glyphmap(const char *fname);
|
nuclear@7
|
58 /* close a font opened by either of the above */
|
nuclear@7
|
59 void dtx_close_font(struct dtx_font *fnt);
|
nuclear@7
|
60
|
nuclear@7
|
61 /* prepare an ASCII glyphmap for the specified font size */
|
nuclear@7
|
62 void dtx_prepare(struct dtx_font *fnt, int sz);
|
nuclear@7
|
63 /* prepare an arbitrary unicode range glyphmap for the specified font size */
|
nuclear@7
|
64 void dtx_prepare_range(struct dtx_font *fnt, int sz, int cstart, int cend);
|
nuclear@7
|
65
|
nuclear@7
|
66 int dtx_get_font_glyphmap_count(struct dtx_font *fnt);
|
nuclear@7
|
67 struct dtx_glyphmap *dtx_get_font_glyphmap_idx(struct dtx_font *fnt, int idx);
|
nuclear@7
|
68
|
nuclear@7
|
69 /* Finds the glyphmap that contains the specified character code and matches the requested size
|
nuclear@7
|
70 * Returns null if it hasn't been created (you should call dtx_prepare/dtx_prepare_range).
|
nuclear@7
|
71 */
|
nuclear@7
|
72 struct dtx_glyphmap *dtx_get_font_glyphmap(struct dtx_font *fnt, int sz, int code);
|
nuclear@7
|
73
|
nuclear@7
|
74 /* Finds the glyphmap that contains the specified unicode range and matches the requested font size
|
nuclear@7
|
75 * Will automatically generate one if it can't find it.
|
nuclear@7
|
76 */
|
nuclear@7
|
77 struct dtx_glyphmap *dtx_get_font_glyphmap_range(struct dtx_font *fnt, int sz, int cstart, int cend);
|
nuclear@7
|
78
|
nuclear@7
|
79 /* Creates and returns a glyphmap for a particular unicode range and font size.
|
nuclear@7
|
80 * The generated glyphmap is added to the font's list of glyphmaps.
|
nuclear@7
|
81 */
|
nuclear@7
|
82 struct dtx_glyphmap *dtx_create_glyphmap_range(struct dtx_font *fnt, int sz, int cstart, int cend);
|
nuclear@7
|
83 /* free a glyphmap */
|
nuclear@7
|
84 void dtx_free_glyphmap(struct dtx_glyphmap *gmap);
|
nuclear@7
|
85
|
nuclear@7
|
86 /* returns a pointer to the raster image of a glyphmap (1 byte per pixel grayscale) */
|
nuclear@7
|
87 unsigned char *dtx_get_glyphmap_image(struct dtx_glyphmap *gmap);
|
nuclear@7
|
88 /* returns the point size of the glyphmap */
|
nuclear@7
|
89 int dtx_get_glyphmap_ptsize(struct dtx_glyphmap *gmap);
|
nuclear@7
|
90 /* returns the width of the glyphmap image in pixels */
|
nuclear@7
|
91 int dtx_get_glyphmap_width(struct dtx_glyphmap *gmap);
|
nuclear@7
|
92 /* returns the height of the glyphmap image in pixels */
|
nuclear@7
|
93 int dtx_get_glyphmap_height(struct dtx_glyphmap *gmap);
|
nuclear@7
|
94
|
nuclear@7
|
95 /* The following functions can be used even when the library is compiled without
|
nuclear@7
|
96 * freetype support.
|
nuclear@7
|
97 */
|
nuclear@7
|
98 struct dtx_glyphmap *dtx_load_glyphmap(const char *fname);
|
nuclear@7
|
99 struct dtx_glyphmap *dtx_load_glyphmap_stream(FILE *fp);
|
nuclear@7
|
100 struct dtx_glyphmap *dtx_load_glyphmap_asset(ass_file *fp);
|
nuclear@7
|
101 int dtx_save_glyphmap(const char *fname, const struct dtx_glyphmap *gmap);
|
nuclear@7
|
102 int dtx_save_glyphmap_stream(FILE *fp, const struct dtx_glyphmap *gmap);
|
nuclear@7
|
103
|
nuclear@7
|
104 /* adds a glyphmap to a font */
|
nuclear@7
|
105 void dtx_add_glyphmap(struct dtx_font *fnt, struct dtx_glyphmap *gmap);
|
nuclear@7
|
106
|
nuclear@7
|
107 /* ---- rendering ---- */
|
nuclear@7
|
108
|
nuclear@7
|
109 /* before drawing anything this function must set the font to use */
|
nuclear@7
|
110 void dtx_use_font(struct dtx_font *fnt, int sz);
|
nuclear@7
|
111
|
nuclear@7
|
112 /* sets the buffering mode
|
nuclear@7
|
113 * - DTX_NBUF: every call to dtx_string gets rendered immediately.
|
nuclear@7
|
114 * - DTX_LBUF: renders when the buffer is full or the string contains a newline.
|
nuclear@7
|
115 * - DTX_FBUF: renders only when the buffer is full (you must call dtx_flush explicitly).
|
nuclear@7
|
116 */
|
nuclear@7
|
117 void dtx_draw_buffering(int mode);
|
nuclear@7
|
118
|
nuclear@7
|
119 /* Sets the vertex attribute indices to use for passing vertex and texture coordinate
|
nuclear@7
|
120 * data. By default both are -1 which means you don't have to use a shader, and if you
|
nuclear@7
|
121 * do they are accessible through gl_Vertex and gl_MultiTexCoord0, as usual.
|
nuclear@7
|
122 *
|
nuclear@7
|
123 * NOTE: If you are using OpenGL ES 2.x or OpenGL >= 3.1 pure (non-compatibility) context
|
nuclear@7
|
124 * you must specify valid attribute indices.
|
nuclear@7
|
125 */
|
nuclear@7
|
126 void dtx_vertex_attribs(int vert_attr, int tex_attr);
|
nuclear@7
|
127
|
nuclear@7
|
128 /* draws a single glyph at the origin */
|
nuclear@7
|
129 void dtx_glyph(int code);
|
nuclear@7
|
130 /* draws a utf-8 string starting at the origin. \n \r and \t are handled appropriately. */
|
nuclear@7
|
131 void dtx_string(const char *str);
|
nuclear@7
|
132
|
nuclear@7
|
133 void dtx_printf(const char *fmt, ...);
|
nuclear@7
|
134
|
nuclear@7
|
135 /* render any pending glyphs (see dtx_draw_buffering) */
|
nuclear@7
|
136 void dtx_flush(void);
|
nuclear@7
|
137
|
nuclear@7
|
138
|
nuclear@7
|
139 /* ---- encodings ---- */
|
nuclear@7
|
140
|
nuclear@7
|
141 /* returns a pointer to the next character in a utf-8 stream */
|
nuclear@7
|
142 char *dtx_utf8_next_char(char *str);
|
nuclear@7
|
143
|
nuclear@7
|
144 /* returns the unicode character codepoint of the utf-8 character starting at str */
|
nuclear@7
|
145 int dtx_utf8_char_code(const char *str);
|
nuclear@7
|
146
|
nuclear@7
|
147 /* returns the number of bytes of the utf-8 character starting at str */
|
nuclear@7
|
148 int dtx_utf8_nbytes(const char *str);
|
nuclear@7
|
149
|
nuclear@7
|
150 /* returns the number of utf-8 character in a zero-terminated utf-8 string */
|
nuclear@7
|
151 int dtx_utf8_char_count(const char *str);
|
nuclear@7
|
152
|
nuclear@7
|
153 /* Converts a unicode code-point to a utf-8 character by filling in the buffer
|
nuclear@7
|
154 * passed at the second argument, and returns the number of bytes taken by that
|
nuclear@7
|
155 * utf-8 character.
|
nuclear@7
|
156 * It's valid to pass a null buffer pointer, in which case only the byte count is
|
nuclear@7
|
157 * returned (useful to figure out how much memory to allocate for a buffer).
|
nuclear@7
|
158 */
|
nuclear@7
|
159 size_t dtx_utf8_from_char_code(int code, char *buf);
|
nuclear@7
|
160
|
nuclear@7
|
161 /* Converts a unicode utf-16 wchar_t string to utf-8, filling in the buffer passed
|
nuclear@7
|
162 * at the second argument. Returns the size of the resulting string in bytes.
|
nuclear@7
|
163 *
|
nuclear@7
|
164 * It's valid to pass a null buffer pointer, in which case only the size gets
|
nuclear@7
|
165 * calculated and returned, which is useful for figuring out how much memory to
|
nuclear@7
|
166 * allocate for the utf-8 buffer.
|
nuclear@7
|
167 */
|
nuclear@7
|
168 size_t dtx_utf8_from_string(const wchar_t *str, char *buf);
|
nuclear@7
|
169
|
nuclear@7
|
170
|
nuclear@7
|
171 /* ---- metrics ---- */
|
nuclear@7
|
172 float dtx_line_height(void);
|
nuclear@7
|
173
|
nuclear@7
|
174 /* rendered dimensions of a single glyph */
|
nuclear@7
|
175 void dtx_glyph_box(int code, struct dtx_box *box);
|
nuclear@7
|
176 float dtx_glyph_width(int code);
|
nuclear@7
|
177 float dtx_glyph_height(int code);
|
nuclear@7
|
178
|
nuclear@7
|
179 /* rendered dimensions of a string */
|
nuclear@7
|
180 void dtx_string_box(const char *str, struct dtx_box *box);
|
nuclear@7
|
181 float dtx_string_width(const char *str);
|
nuclear@7
|
182 float dtx_string_height(const char *str);
|
nuclear@7
|
183
|
nuclear@7
|
184 /* returns the horizontal position of the n-th character of the rendered string
|
nuclear@7
|
185 * (useful for placing cursors)
|
nuclear@7
|
186 */
|
nuclear@7
|
187 float dtx_char_pos(const char *str, int n);
|
nuclear@7
|
188
|
nuclear@7
|
189 int dtx_char_at_pt(const char *str, float pt);
|
nuclear@7
|
190
|
nuclear@7
|
191 #ifdef __cplusplus
|
nuclear@7
|
192 }
|
nuclear@7
|
193 #endif
|
nuclear@7
|
194
|
nuclear@7
|
195 #endif /* LIBDRAWTEXT_H_ */
|