istereo2

diff libs/drawtext/drawtext.h @ 7:a3c4fcc9f8f3

- started a goatkit UI theme - font rendering with drawtext and shaders - asset manager (only used by drawtext for now, will replace respath eventually)
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 24 Sep 2015 06:49:25 +0300
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/libs/drawtext/drawtext.h	Thu Sep 24 06:49:25 2015 +0300
     1.3 @@ -0,0 +1,195 @@
     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 +#include "assman.h"
    1.27 +
    1.28 +struct dtx_font;
    1.29 +struct dtx_glyphmap;
    1.30 +
    1.31 +/* draw buffering modes */
    1.32 +enum {
    1.33 +	DTX_NBF,	/* unbuffered */
    1.34 +	DTX_LBF,	/* line buffered */
    1.35 +	DTX_FBF		/* fully buffered */
    1.36 +};
    1.37 +
    1.38 +struct dtx_box {
    1.39 +	float x, y;
    1.40 +	float width, height;
    1.41 +};
    1.42 +
    1.43 +#ifdef __cplusplus
    1.44 +extern "C" {
    1.45 +#endif
    1.46 +
    1.47 +/* Open a truetype/opentype/whatever font.
    1.48 + *
    1.49 + * If sz is non-zero, the default ASCII glyphmap at the requested point size is
    1.50 + * automatically created as well, and ready to use.
    1.51 + *
    1.52 + * To use other unicode ranges and different font sizes you must first call
    1.53 + * dtx_prepare or dtx_prepare_range before issuing any drawing calls, otherwise
    1.54 + * nothing will be rendered.
    1.55 + */
    1.56 +struct dtx_font *dtx_open_font(const char *fname, int sz);
    1.57 +/* open a font by loading a precompiled glyphmap (see: dtx_save_glyphmap)
    1.58 + * this works even when compiled without freetype support
    1.59 + */
    1.60 +struct dtx_font *dtx_open_font_glyphmap(const char *fname);
    1.61 +/* close a font opened by either of the above */
    1.62 +void dtx_close_font(struct dtx_font *fnt);
    1.63 +
    1.64 +/* prepare an ASCII glyphmap for the specified font size */
    1.65 +void dtx_prepare(struct dtx_font *fnt, int sz);
    1.66 +/* prepare an arbitrary unicode range glyphmap for the specified font size */
    1.67 +void dtx_prepare_range(struct dtx_font *fnt, int sz, int cstart, int cend);
    1.68 +
    1.69 +int dtx_get_font_glyphmap_count(struct dtx_font *fnt);
    1.70 +struct dtx_glyphmap *dtx_get_font_glyphmap_idx(struct dtx_font *fnt, int idx);
    1.71 +
    1.72 +/* Finds the glyphmap that contains the specified character code and matches the requested size
    1.73 + * Returns null if it hasn't been created (you should call dtx_prepare/dtx_prepare_range).
    1.74 + */
    1.75 +struct dtx_glyphmap *dtx_get_font_glyphmap(struct dtx_font *fnt, int sz, int code);
    1.76 +
    1.77 +/* Finds the glyphmap that contains the specified unicode range and matches the requested font size
    1.78 + * Will automatically generate one if it can't find it.
    1.79 + */
    1.80 +struct dtx_glyphmap *dtx_get_font_glyphmap_range(struct dtx_font *fnt, int sz, int cstart, int cend);
    1.81 +
    1.82 +/* Creates and returns a glyphmap for a particular unicode range and font size.
    1.83 + * The generated glyphmap is added to the font's list of glyphmaps.
    1.84 + */
    1.85 +struct dtx_glyphmap *dtx_create_glyphmap_range(struct dtx_font *fnt, int sz, int cstart, int cend);
    1.86 +/* free a glyphmap */
    1.87 +void dtx_free_glyphmap(struct dtx_glyphmap *gmap);
    1.88 +
    1.89 +/* returns a pointer to the raster image of a glyphmap (1 byte per pixel grayscale) */
    1.90 +unsigned char *dtx_get_glyphmap_image(struct dtx_glyphmap *gmap);
    1.91 +/* returns the point size of the glyphmap */
    1.92 +int dtx_get_glyphmap_ptsize(struct dtx_glyphmap *gmap);
    1.93 +/* returns the width of the glyphmap image in pixels */
    1.94 +int dtx_get_glyphmap_width(struct dtx_glyphmap *gmap);
    1.95 +/* returns the height of the glyphmap image in pixels */
    1.96 +int dtx_get_glyphmap_height(struct dtx_glyphmap *gmap);
    1.97 +
    1.98 +/* The following functions can be used even when the library is compiled without
    1.99 + * freetype support.
   1.100 + */
   1.101 +struct dtx_glyphmap *dtx_load_glyphmap(const char *fname);
   1.102 +struct dtx_glyphmap *dtx_load_glyphmap_stream(FILE *fp);
   1.103 +struct dtx_glyphmap *dtx_load_glyphmap_asset(ass_file *fp);
   1.104 +int dtx_save_glyphmap(const char *fname, const struct dtx_glyphmap *gmap);
   1.105 +int dtx_save_glyphmap_stream(FILE *fp, const struct dtx_glyphmap *gmap);
   1.106 +
   1.107 +/* adds a glyphmap to a font */
   1.108 +void dtx_add_glyphmap(struct dtx_font *fnt, struct dtx_glyphmap *gmap);
   1.109 +
   1.110 +/* ---- rendering ---- */
   1.111 +
   1.112 +/* before drawing anything this function must set the font to use */
   1.113 +void dtx_use_font(struct dtx_font *fnt, int sz);
   1.114 +
   1.115 +/* sets the buffering mode
   1.116 + * - DTX_NBUF: every call to dtx_string gets rendered immediately.
   1.117 + * - DTX_LBUF: renders when the buffer is full or the string contains a newline.
   1.118 + * - DTX_FBUF: renders only when the buffer is full (you must call dtx_flush explicitly).
   1.119 + */
   1.120 +void dtx_draw_buffering(int mode);
   1.121 +
   1.122 +/* Sets the vertex attribute indices to use for passing vertex and texture coordinate
   1.123 + * data. By default both are -1 which means you don't have to use a shader, and if you
   1.124 + * do they are accessible through gl_Vertex and gl_MultiTexCoord0, as usual.
   1.125 + *
   1.126 + * NOTE: If you are using OpenGL ES 2.x or OpenGL >= 3.1 pure (non-compatibility) context
   1.127 + * you must specify valid attribute indices.
   1.128 + */
   1.129 +void dtx_vertex_attribs(int vert_attr, int tex_attr);
   1.130 +
   1.131 +/* draws a single glyph at the origin */
   1.132 +void dtx_glyph(int code);
   1.133 +/* draws a utf-8 string starting at the origin. \n \r and \t are handled appropriately. */
   1.134 +void dtx_string(const char *str);
   1.135 +
   1.136 +void dtx_printf(const char *fmt, ...);
   1.137 +
   1.138 +/* render any pending glyphs (see dtx_draw_buffering) */
   1.139 +void dtx_flush(void);
   1.140 +
   1.141 +
   1.142 +/* ---- encodings ---- */
   1.143 +
   1.144 +/* returns a pointer to the next character in a utf-8 stream */
   1.145 +char *dtx_utf8_next_char(char *str);
   1.146 +
   1.147 +/* returns the unicode character codepoint of the utf-8 character starting at str */
   1.148 +int dtx_utf8_char_code(const char *str);
   1.149 +
   1.150 +/* returns the number of bytes of the utf-8 character starting at str */
   1.151 +int dtx_utf8_nbytes(const char *str);
   1.152 +
   1.153 +/* returns the number of utf-8 character in a zero-terminated utf-8 string */
   1.154 +int dtx_utf8_char_count(const char *str);
   1.155 +
   1.156 +/* Converts a unicode code-point to a utf-8 character by filling in the buffer
   1.157 + * passed at the second argument, and returns the number of bytes taken by that
   1.158 + * utf-8 character.
   1.159 + * It's valid to pass a null buffer pointer, in which case only the byte count is
   1.160 + * returned (useful to figure out how much memory to allocate for a buffer).
   1.161 + */
   1.162 +size_t dtx_utf8_from_char_code(int code, char *buf);
   1.163 +
   1.164 +/* Converts a unicode utf-16 wchar_t string to utf-8, filling in the buffer passed
   1.165 + * at the second argument. Returns the size of the resulting string in bytes.
   1.166 + *
   1.167 + * It's valid to pass a null buffer pointer, in which case only the size gets
   1.168 + * calculated and returned, which is useful for figuring out how much memory to
   1.169 + * allocate for the utf-8 buffer.
   1.170 + */
   1.171 +size_t dtx_utf8_from_string(const wchar_t *str, char *buf);
   1.172 +
   1.173 +
   1.174 +/* ---- metrics ---- */
   1.175 +float dtx_line_height(void);
   1.176 +
   1.177 +/* rendered dimensions of a single glyph */
   1.178 +void dtx_glyph_box(int code, struct dtx_box *box);
   1.179 +float dtx_glyph_width(int code);
   1.180 +float dtx_glyph_height(int code);
   1.181 +
   1.182 +/* rendered dimensions of a string */
   1.183 +void dtx_string_box(const char *str, struct dtx_box *box);
   1.184 +float dtx_string_width(const char *str);
   1.185 +float dtx_string_height(const char *str);
   1.186 +
   1.187 +/* returns the horizontal position of the n-th character of the rendered string
   1.188 + * (useful for placing cursors)
   1.189 + */
   1.190 +float dtx_char_pos(const char *str, int n);
   1.191 +
   1.192 +int dtx_char_at_pt(const char *str, float pt);
   1.193 +
   1.194 +#ifdef __cplusplus
   1.195 +}
   1.196 +#endif
   1.197 +
   1.198 +#endif	/* LIBDRAWTEXT_H_ */