libdrawtext

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