3dphotoshoot
diff libs/drawtext/drawtext.h @ 10:c71c477521ca
converting to GLES2 and C++
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 31 May 2015 00:40:26 +0300 |
parents | |
children | d7fe157c402d |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/libs/drawtext/drawtext.h Sun May 31 00:40:26 2015 +0300 1.3 @@ -0,0 +1,188 @@ 1.4 +/* 1.5 +libdrawtext - a simple library for fast text rendering in OpenGL 1.6 +Copyright (C) 2011-2014 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 LIBDRAWTEXT_H_ 1.22 +#define LIBDRAWTEXT_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 +/* open a font by loading a precompiled glyphmap (see: dtx_save_glyphmap) 1.57 + * this works even when compiled without freetype support 1.58 + */ 1.59 +struct dtx_font *dtx_open_font_glyphmap(const char *fname); 1.60 +/* close a font opened by either of the above */ 1.61 +void dtx_close_font(struct dtx_font *fnt); 1.62 + 1.63 +/* prepare an ASCII glyphmap for the specified font size */ 1.64 +void dtx_prepare(struct dtx_font *fnt, int sz); 1.65 +/* prepare an arbitrary unicode range glyphmap for the specified font size */ 1.66 +void dtx_prepare_range(struct dtx_font *fnt, int sz, int cstart, int cend); 1.67 + 1.68 +/* Finds the glyphmap that contains the specified character code and matches the requested size 1.69 + * Returns null if it hasn't been created (you should call dtx_prepare/dtx_prepare_range). 1.70 + */ 1.71 +struct dtx_glyphmap *dtx_get_font_glyphmap(struct dtx_font *fnt, int sz, int code); 1.72 + 1.73 +/* Finds the glyphmap that contains the specified unicode range and matches the requested font size 1.74 + * Will automatically generate one if it can't find it. 1.75 + */ 1.76 +struct dtx_glyphmap *dtx_get_font_glyphmap_range(struct dtx_font *fnt, int sz, int cstart, int cend); 1.77 + 1.78 +/* Creates and returns a glyphmap for a particular unicode range and font size. 1.79 + * The generated glyphmap is added to the font's list of glyphmaps. 1.80 + */ 1.81 +struct dtx_glyphmap *dtx_create_glyphmap_range(struct dtx_font *fnt, int sz, int cstart, int cend); 1.82 +/* free a glyphmap */ 1.83 +void dtx_free_glyphmap(struct dtx_glyphmap *gmap); 1.84 + 1.85 +/* returns a pointer to the raster image of a glyphmap (1 byte per pixel grayscale) */ 1.86 +unsigned char *dtx_get_glyphmap_image(struct dtx_glyphmap *gmap); 1.87 +/* returns the width of the glyphmap image in pixels */ 1.88 +int dtx_get_glyphmap_width(struct dtx_glyphmap *gmap); 1.89 +/* returns the height of the glyphmap image in pixels */ 1.90 +int dtx_get_glyphmap_height(struct dtx_glyphmap *gmap); 1.91 + 1.92 +/* The following functions can be used even when the library is compiled without 1.93 + * freetype support. 1.94 + */ 1.95 +struct dtx_glyphmap *dtx_load_glyphmap(const char *fname); 1.96 +struct dtx_glyphmap *dtx_load_glyphmap_stream(FILE *fp); 1.97 +int dtx_save_glyphmap(const char *fname, const struct dtx_glyphmap *gmap); 1.98 +int dtx_save_glyphmap_stream(FILE *fp, const struct dtx_glyphmap *gmap); 1.99 + 1.100 +/* adds a glyphmap to a font */ 1.101 +void dtx_add_glyphmap(struct dtx_font *fnt, struct dtx_glyphmap *gmap); 1.102 + 1.103 +/* ---- rendering ---- */ 1.104 + 1.105 +/* before drawing anything this function must set the font to use */ 1.106 +void dtx_use_font(struct dtx_font *fnt, int sz); 1.107 + 1.108 +/* sets the buffering mode 1.109 + * - DTX_NBUF: every call to dtx_string gets rendered immediately. 1.110 + * - DTX_LBUF: renders when the buffer is full or the string contains a newline. 1.111 + * - DTX_FBUF: renders only when the buffer is full (you must call dtx_flush explicitly). 1.112 + */ 1.113 +void dtx_draw_buffering(int mode); 1.114 + 1.115 +/* Sets the vertex attribute indices to use for passing vertex and texture coordinate 1.116 + * data. By default both are -1 which means you don't have to use a shader, and if you 1.117 + * do they are accessible through gl_Vertex and gl_MultiTexCoord0, as usual. 1.118 + * 1.119 + * NOTE: If you are using OpenGL ES 2.x or OpenGL >= 3.1 pure (non-compatibility) context 1.120 + * you must specify valid attribute indices. 1.121 + */ 1.122 +void dtx_vertex_attribs(int vert_attr, int tex_attr); 1.123 + 1.124 +/* draws a single glyph at the origin */ 1.125 +void dtx_glyph(int code); 1.126 +/* draws a utf-8 string starting at the origin. \n \r and \t are handled appropriately. */ 1.127 +void dtx_string(const char *str); 1.128 + 1.129 +void dtx_printf(const char *fmt, ...); 1.130 + 1.131 +/* render any pending glyphs (see dtx_draw_buffering) */ 1.132 +void dtx_flush(void); 1.133 + 1.134 + 1.135 +/* ---- encodings ---- */ 1.136 + 1.137 +/* returns a pointer to the next character in a utf-8 stream */ 1.138 +char *dtx_utf8_next_char(char *str); 1.139 + 1.140 +/* returns the unicode character codepoint of the utf-8 character starting at str */ 1.141 +int dtx_utf8_char_code(const char *str); 1.142 + 1.143 +/* returns the number of bytes of the utf-8 character starting at str */ 1.144 +int dtx_utf8_nbytes(const char *str); 1.145 + 1.146 +/* returns the number of utf-8 character in a zero-terminated utf-8 string */ 1.147 +int dtx_utf8_char_count(const char *str); 1.148 + 1.149 +/* Converts a unicode code-point to a utf-8 character by filling in the buffer 1.150 + * passed at the second argument, and returns the number of bytes taken by that 1.151 + * utf-8 character. 1.152 + * It's valid to pass a null buffer pointer, in which case only the byte count is 1.153 + * returned (useful to figure out how much memory to allocate for a buffer). 1.154 + */ 1.155 +size_t dtx_utf8_from_char_code(int code, char *buf); 1.156 + 1.157 +/* Converts a unicode utf-16 wchar_t string to utf-8, filling in the buffer passed 1.158 + * at the second argument. Returns the size of the resulting string in bytes. 1.159 + * 1.160 + * It's valid to pass a null buffer pointer, in which case only the size gets 1.161 + * calculated and returned, which is useful for figuring out how much memory to 1.162 + * allocate for the utf-8 buffer. 1.163 + */ 1.164 +size_t dtx_utf8_from_string(const wchar_t *str, char *buf); 1.165 + 1.166 + 1.167 +/* ---- metrics ---- */ 1.168 +float dtx_line_height(void); 1.169 + 1.170 +/* rendered dimensions of a single glyph */ 1.171 +void dtx_glyph_box(int code, struct dtx_box *box); 1.172 +float dtx_glyph_width(int code); 1.173 +float dtx_glyph_height(int code); 1.174 + 1.175 +/* rendered dimensions of a string */ 1.176 +void dtx_string_box(const char *str, struct dtx_box *box); 1.177 +float dtx_string_width(const char *str); 1.178 +float dtx_string_height(const char *str); 1.179 + 1.180 +/* returns the horizontal position of the n-th character of the rendered string 1.181 + * (useful for placing cursors) 1.182 + */ 1.183 +float dtx_char_pos(const char *str, int n); 1.184 + 1.185 +int dtx_char_at_pt(const char *str, float pt); 1.186 + 1.187 +#ifdef __cplusplus 1.188 +} 1.189 +#endif 1.190 + 1.191 +#endif /* LIBDRAWTEXT_H_ */