libdrawtext

diff src/drawtext.h @ 52:34130f58141a

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