vrshoot
diff libs/drawtext/drawtext.h @ 0:b2f14e535253
initial commit
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sat, 01 Feb 2014 19:58:19 +0200 |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/libs/drawtext/drawtext.h Sat Feb 01 19:58:19 2014 +0200 1.3 @@ -0,0 +1,182 @@ 1.4 +/* 1.5 +libdrawtext - a simple library for fast text rendering in OpenGL 1.6 +Copyright (C) 2011-2012 John Tsiombikas <nuclear@member.fsf.org> 1.7 + 1.8 +This program is free software: you can redistribute it and/or modify 1.9 +it under the terms of the GNU Lesser General Public License as published by 1.10 +the Free Software Foundation, either version 3 of the License, or 1.11 +(at your option) any later version. 1.12 + 1.13 +This program is distributed in the hope that it will be useful, 1.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of 1.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1.16 +GNU Lesser General Public License for more details. 1.17 + 1.18 +You should have received a copy of the GNU Lesser General Public License 1.19 +along with this program. If not, see <http://www.gnu.org/licenses/>. 1.20 +*/ 1.21 +#ifndef TEXT_H_ 1.22 +#define TEXT_H_ 1.23 + 1.24 +#include <stdio.h> 1.25 +#include <stdlib.h> 1.26 + 1.27 +struct dtx_font; 1.28 +struct dtx_glyphmap; 1.29 + 1.30 +/* draw buffering modes */ 1.31 +enum { 1.32 + DTX_NBF, /* unbuffered */ 1.33 + DTX_LBF, /* line buffered */ 1.34 + DTX_FBF /* fully buffered */ 1.35 +}; 1.36 + 1.37 +struct dtx_box { 1.38 + float x, y; 1.39 + float width, height; 1.40 +}; 1.41 + 1.42 +#ifdef __cplusplus 1.43 +extern "C" { 1.44 +#endif 1.45 + 1.46 +/* Open a truetype/opentype/whatever font. 1.47 + * 1.48 + * If sz is non-zero, the default ASCII glyphmap at the requested point size is 1.49 + * automatically created as well, and ready to use. 1.50 + * 1.51 + * To use other unicode ranges and different font sizes you must first call 1.52 + * dtx_prepare or dtx_prepare_range before issuing any drawing calls, otherwise 1.53 + * nothing will be rendered. 1.54 + */ 1.55 +struct dtx_font *dtx_open_font(const char *fname, int sz); 1.56 +void dtx_close_font(struct dtx_font *fnt); 1.57 + 1.58 +/* prepare an ASCII glyphmap for the specified font size */ 1.59 +void dtx_prepare(struct dtx_font *fnt, int sz); 1.60 +/* prepare an arbitrary unicode range glyphmap for the specified font size */ 1.61 +void dtx_prepare_range(struct dtx_font *fnt, int sz, int cstart, int cend); 1.62 + 1.63 +/* Finds the glyphmap that contains the specified character code and matches the requested size 1.64 + * Returns null if it hasn't been created (you should call dtx_prepare/dtx_prepare_range). 1.65 + */ 1.66 +struct dtx_glyphmap *dtx_get_font_glyphmap(struct dtx_font *fnt, int sz, int code); 1.67 + 1.68 +/* Finds the glyphmap that contains the specified unicode range and matches the requested font size 1.69 + * Will automatically generate one if it can't find it. 1.70 + */ 1.71 +struct dtx_glyphmap *dtx_get_font_glyphmap_range(struct dtx_font *fnt, int sz, int cstart, int cend); 1.72 + 1.73 +/* Creates and returns a glyphmap for a particular unicode range and font size. 1.74 + * The generated glyphmap is added to the font's list of glyphmaps. 1.75 + */ 1.76 +struct dtx_glyphmap *dtx_create_glyphmap_range(struct dtx_font *fnt, int sz, int cstart, int cend); 1.77 +/* free a glyphmap */ 1.78 +void dtx_free_glyphmap(struct dtx_glyphmap *gmap); 1.79 + 1.80 +/* returns a pointer to the raster image of a glyphmap (1 byte per pixel grayscale) */ 1.81 +unsigned char *dtx_get_glyphmap_image(struct dtx_glyphmap *gmap); 1.82 +/* returns the width of the glyphmap image in pixels */ 1.83 +int dtx_get_glyphmap_width(struct dtx_glyphmap *gmap); 1.84 +/* returns the height of the glyphmap image in pixels */ 1.85 +int dtx_get_glyphmap_height(struct dtx_glyphmap *gmap); 1.86 + 1.87 +/* The following functions can be used even when the library is compiled without 1.88 + * freetype support. (TODO) 1.89 + */ 1.90 +struct dtx_glyphmap *dtx_load_glyphmap(const char *fname); 1.91 +struct dtx_glyphmap *dtx_load_glyphmap_stream(FILE *fp); 1.92 +int dtx_save_glyphmap(const char *fname, const struct dtx_glyphmap *gmap); 1.93 +int dtx_save_glyphmap_stream(FILE *fp, const struct dtx_glyphmap *gmap); 1.94 + 1.95 + 1.96 +/* ---- rendering ---- */ 1.97 + 1.98 +/* before drawing anything this function must set the font to use */ 1.99 +void dtx_use_font(struct dtx_font *fnt, int sz); 1.100 + 1.101 +/* sets the buffering mode 1.102 + * - DTX_NBF: every call to dtx_string gets rendered immediately. 1.103 + * - DTX_LBF: renders when the buffer is full or the string contains a newline. 1.104 + * - DTX_FBF: renders only when the buffer is full (you must call dtx_flush explicitly). 1.105 + */ 1.106 +void dtx_draw_buffering(int mode); 1.107 + 1.108 +/* Sets the vertex attribute indices to use for passing vertex and texture coordinate 1.109 + * data. By default both are -1 which means you don't have to use a shader, and if you 1.110 + * do they are accessible through gl_Vertex and gl_MultiTexCoord0, as usual. 1.111 + * 1.112 + * NOTE: If you are using OpenGL ES 2.x or OpenGL >= 3.1 pure (non-compatibility) context 1.113 + * you must specify valid attribute indices. 1.114 + */ 1.115 +void dtx_vertex_attribs(int vert_attr, int tex_attr); 1.116 + 1.117 +/* draws a single glyph at the origin */ 1.118 +void dtx_glyph(int code); 1.119 +/* draws a utf-8 string starting at the origin. \n \r and \t are handled appropriately. */ 1.120 +void dtx_string(const char *str); 1.121 + 1.122 +void dtx_printf(const char *fmt, ...); 1.123 + 1.124 +/* render any pending glyphs (see dtx_draw_buffering) */ 1.125 +void dtx_flush(void); 1.126 + 1.127 + 1.128 +/* ---- encodings ---- */ 1.129 + 1.130 +/* returns a pointer to the next character in a utf-8 stream */ 1.131 +char *dtx_utf8_next_char(char *str); 1.132 + 1.133 +/* returns the unicode character codepoint of the utf-8 character starting at str */ 1.134 +int dtx_utf8_char_code(const char *str); 1.135 + 1.136 +/* returns the number of bytes of the utf-8 character starting at str */ 1.137 +int dtx_utf8_nbytes(const char *str); 1.138 + 1.139 +/* returns the number of utf-8 character in a zero-terminated utf-8 string */ 1.140 +int dtx_utf8_char_count(const char *str); 1.141 + 1.142 +/* Converts a unicode code-point to a utf-8 character by filling in the buffer 1.143 + * passed at the second argument, and returns the number of bytes taken by that 1.144 + * utf-8 character. 1.145 + * It's valid to pass a null buffer pointer, in which case only the byte count is 1.146 + * returned (useful to figure out how much memory to allocate for a buffer). 1.147 + */ 1.148 +size_t dtx_utf8_from_char_code(int code, char *buf); 1.149 + 1.150 +/* Converts a unicode utf-16 wchar_t string to utf-8, filling in the buffer passed 1.151 + * at the second argument. Returns the size of the resulting string in bytes. 1.152 + * 1.153 + * It's valid to pass a null buffer pointer, in which case only the size gets 1.154 + * calculated and returned, which is useful for figuring out how much memory to 1.155 + * allocate for the utf-8 buffer. 1.156 + */ 1.157 +size_t dtx_utf8_from_string(const wchar_t *str, char *buf); 1.158 + 1.159 + 1.160 +/* ---- metrics ---- */ 1.161 +float dtx_line_height(void); 1.162 +float dtx_baseline(void); 1.163 + 1.164 +/* rendered dimensions of a single glyph */ 1.165 +void dtx_glyph_box(int code, struct dtx_box *box); 1.166 +float dtx_glyph_width(int code); 1.167 +float dtx_glyph_height(int code); 1.168 + 1.169 +/* rendered dimensions of a string */ 1.170 +void dtx_string_box(const char *str, struct dtx_box *box); 1.171 +float dtx_string_width(const char *str); 1.172 +float dtx_string_height(const char *str); 1.173 + 1.174 +/* returns the horizontal position of the n-th character of the rendered string 1.175 + * (useful for placing cursors) 1.176 + */ 1.177 +float dtx_char_pos(const char *str, int n); 1.178 + 1.179 +int dtx_char_at_pt(const char *str, float pt); 1.180 + 1.181 +#ifdef __cplusplus 1.182 +} 1.183 +#endif 1.184 + 1.185 +#endif /* TEXT_H_ */