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