libdrawtext

view src/drawtext.h @ 65:16879398fda7

added dtx_line_height query
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 25 Aug 2012 18:27:54 +0300
parents 55c034e70809
children 462bcb69de15
line source
1 /*
2 libdrawtext - a simple library for fast text rendering in OpenGL
3 Copyright (C) 2011-2012 John Tsiombikas <nuclear@member.fsf.org>
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.
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.
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 TEXT_H_
19 #define TEXT_H_
21 #include <stdio.h>
22 #include <stdlib.h>
24 struct dtx_font;
25 struct dtx_glyphmap;
27 /* draw buffering modes */
28 enum {
29 DTX_NBF, /* unbuffered */
30 DTX_LBF, /* line buffered */
31 DTX_FBF /* fully buffered */
32 };
34 struct dtx_box {
35 float x, y;
36 float width, height;
37 };
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
43 /* Open a truetype/opentype/whatever font.
44 *
45 * If sz is non-zero, the default ASCII glyphmap at the requested point size is
46 * automatically created as well, and ready to use.
47 *
48 * To use other unicode ranges and different font sizes you must first call
49 * dtx_prepare or dtx_prepare_range before issuing any drawing calls, otherwise
50 * nothing will be rendered.
51 */
52 struct dtx_font *dtx_open_font(const char *fname, int sz);
53 void dtx_close_font(struct dtx_font *fnt);
55 /* prepare an ASCII glyphmap for the specified font size */
56 void dtx_prepare(struct dtx_font *fnt, int sz);
57 /* prepare an arbitrary unicode range glyphmap for the specified font size */
58 void dtx_prepare_range(struct dtx_font *fnt, int sz, int cstart, int cend);
60 /* Finds the glyphmap that contains the specified character code and matches the requested size
61 * Returns null if it hasn't been created (you should call dtx_prepare/dtx_prepare_range).
62 */
63 struct dtx_glyphmap *dtx_get_font_glyphmap(struct dtx_font *fnt, int sz, int code);
65 /* Finds the glyphmap that contains the specified unicode range and matches the requested font size
66 * Will automatically generate one if it can't find it.
67 */
68 struct dtx_glyphmap *dtx_get_font_glyphmap_range(struct dtx_font *fnt, int sz, int cstart, int cend);
70 /* Creates and returns a glyphmap for a particular unicode range and font size.
71 * The generated glyphmap is added to the font's list of glyphmaps.
72 */
73 struct dtx_glyphmap *dtx_create_glyphmap_range(struct dtx_font *fnt, int sz, int cstart, int cend);
74 /* free a glyphmap */
75 void dtx_free_glyphmap(struct dtx_glyphmap *gmap);
77 /* returns a pointer to the raster image of a glyphmap (1 byte per pixel grayscale) */
78 unsigned char *dtx_get_glyphmap_image(struct dtx_glyphmap *gmap);
79 /* returns the width of the glyphmap image in pixels */
80 int dtx_get_glyphmap_width(struct dtx_glyphmap *gmap);
81 /* returns the height of the glyphmap image in pixels */
82 int dtx_get_glyphmap_height(struct dtx_glyphmap *gmap);
84 /* The following functions can be used even when the library is compiled without
85 * freetype support. (TODO)
86 */
87 struct dtx_glyphmap *dtx_load_glyphmap(const char *fname);
88 struct dtx_glyphmap *dtx_load_glyphmap_stream(FILE *fp);
89 int dtx_save_glyphmap(const char *fname, const struct dtx_glyphmap *gmap);
90 int dtx_save_glyphmap_stream(FILE *fp, const struct dtx_glyphmap *gmap);
93 /* ---- rendering ---- */
95 /* before drawing anything this function must set the font to use */
96 void dtx_use_font(struct dtx_font *fnt, int sz);
98 /* sets the buffering mode
99 * - DTX_NBUF: every call to dtx_string gets rendered immediately.
100 * - DTX_LBUF: renders when the buffer is full or the string contains a newline.
101 * - DTX_FBUF: renders only when the buffer is full (you must call dtx_flush explicitly).
102 */
103 void dtx_draw_buffering(int mode);
105 /* Sets the vertex attribute indices to use for passing vertex and texture coordinate
106 * data. By default both are -1 which means you don't have to use a shader, and if you
107 * do they are accessible through gl_Vertex and gl_MultiTexCoord0, as usual.
108 *
109 * NOTE: If you are using OpenGL ES 2.x or OpenGL >= 3.1 pure (non-compatibility) context
110 * you must specify valid attribute indices.
111 */
112 void dtx_vertex_attribs(int vert_attr, int tex_attr);
114 /* draws a single glyph at the origin */
115 void dtx_glyph(int code);
116 /* draws a utf-8 string starting at the origin. \n \r and \t are handled appropriately. */
117 void dtx_string(const char *str);
119 /* render any pending glyphs (see dtx_draw_buffering) */
120 void dtx_flush(void);
123 /* ---- encodings ---- */
125 /* returns a pointer to the next character in a utf-8 stream */
126 char *dtx_utf8_next_char(char *str);
128 /* returns the unicode character codepoint of the utf-8 character starting at str */
129 int dtx_utf8_char_code(const char *str);
131 /* returns the number of bytes of the utf-8 character starting at str */
132 int dtx_utf8_nbytes(const char *str);
134 /* returns the number of utf-8 character in a zero-terminated utf-8 string */
135 int dtx_utf8_char_count(const char *str);
137 /* Converts a unicode code-point to a utf-8 character by filling in the buffer
138 * passed at the second argument, and returns the number of bytes taken by that
139 * utf-8 character.
140 * It's valid to pass a null buffer pointer, in which case only the byte count is
141 * returned (useful to figure out how much memory to allocate for a buffer).
142 */
143 size_t dtx_utf8_from_char_code(int code, char *buf);
145 /* Converts a unicode utf-16 wchar_t string to utf-8, filling in the buffer passed
146 * at the second argument. Returns the size of the resulting string in bytes.
147 *
148 * It's valid to pass a null buffer pointer, in which case only the size gets
149 * calculated and returned, which is useful for figuring out how much memory to
150 * allocate for the utf-8 buffer.
151 */
152 size_t dtx_utf8_from_string(const wchar_t *str, char *buf);
155 /* ---- metrics ---- */
156 float dtx_line_height(void);
158 /* rendered dimensions of a single glyph */
159 void dtx_glyph_box(int code, struct dtx_box *box);
160 float dtx_glyph_width(int code);
161 float dtx_glyph_height(int code);
163 /* rendered dimensions of a string */
164 void dtx_string_box(const char *str, struct dtx_box *box);
165 float dtx_string_width(const char *str);
166 float dtx_string_height(const char *str);
168 /* returns the horizontal position of the n-th character of the rendered string
169 * (useful for placing cursors)
170 */
171 float dtx_char_pos(const char *str, int n);
173 int dtx_char_at_pt(const char *str, float pt);
175 #ifdef __cplusplus
176 }
177 #endif
179 #endif /* TEXT_H_ */