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