text_common.h (4032B)
1 /* 2 * This file is part of the Lumidify ToolKit (LTK) 3 * Copyright (c) 2017, 2018, 2020 lumidify <nobody@lumidify.org> 4 * 5 * Permission is hereby granted, free of charge, to any person obtaining a copy 6 * of this software and associated documentation files (the "Software"), to deal 7 * in the Software without restriction, including without limitation the rights 8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 * copies of the Software, and to permit persons to whom the Software is 10 * furnished to do so, subject to the following conditions: 11 * 12 * The above copyright notice and this permission notice shall be included in all 13 * copies or substantial portions of the Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 * SOFTWARE. 22 */ 23 24 #ifndef _TEXT_COMMON_H_ 25 #define _TEXT_COMMON_H_ 26 27 /* 28 Requires the following includes: 29 <X11/Xlib.h>, <X11/Xutil.h>, "stb_truetype.h", 30 "khash.h", <harfbuzz/hb.h>, <fribidi.h>, 31 <fontconfig/fontconfig.h> 32 */ 33 34 typedef struct { 35 stbtt_fontinfo info; 36 hb_font_t *hb; 37 uint16_t id; 38 unsigned int refs; 39 } LtkFont; 40 41 /* Contains general info on glyphs that doesn't change regardless of the context */ 42 typedef struct _LtkGlyphInfo { 43 unsigned int id; 44 unsigned char *alphamap; 45 unsigned int w; 46 unsigned int h; 47 unsigned int xoff; /* x offset from origin to top left corner of glyph */ 48 unsigned int yoff; /* y offset from origin to top left corner of glyph */ 49 unsigned int refs; 50 /* FIXME: does refs need to be long? It could cause problems if a 51 program tries to cache/"keep alive" a lot of pages of text. */ 52 } LtkGlyphInfo; 53 54 /* Contains glyph info specific to one run of text */ 55 typedef struct _LtkGlyph { 56 LtkGlyphInfo *info; 57 int x_offset; /* additional x offset given by harfbuzz */ 58 int y_offset; /* additional y offset given by harfbuzz */ 59 int x_advance; 60 int y_advance; 61 int x_abs; 62 int y_abs; 63 uint32_t cluster; /* index of char in original text - from harfbuzz */ 64 } LtkGlyph; 65 66 /* Hash definitions */ 67 /* glyph id -> glyph info struct */ 68 KHASH_MAP_INIT_INT(glyphinfo, LtkGlyphInfo*) 69 /* font path, size -> glyph cache hash */ 70 KHASH_MAP_INIT_INT(glyphcache, khash_t(glyphinfo)*) 71 /* font path -> font id */ 72 KHASH_MAP_INIT_STR(fontid, uint16_t) 73 /* font id -> font struct */ 74 KHASH_MAP_INIT_INT(fontstruct, LtkFont*) 75 76 typedef struct LtkTextManager { 77 khash_t(fontid) *font_paths; 78 khash_t(fontstruct) *font_cache; 79 khash_t(glyphcache) *glyph_cache; 80 FcPattern *fcpattern; 81 uint16_t default_font; 82 uint16_t font_id_cur; 83 } LtkTextManager; 84 85 uint32_t u8_nextmemchar(const char *s, size_t *i); 86 87 size_t u8_strlen(const char *s); 88 89 size_t u8_wc_toutf8(char *dest, uint32_t ch); 90 91 LtkTextManager *ltk_init_text(char *font_name); 92 93 void ltk_destroy_text_manager(LtkTextManager *tm); 94 95 LtkGlyphInfo *ltk_create_glyph_info(LtkFont *font, unsigned int id, float scale); 96 97 void ltk_destroy_glyph_info(LtkGlyphInfo *gi); 98 99 LtkGlyphInfo *ltk_get_glyph_info(LtkFont *font, unsigned int id, float scale, khash_t(glyphinfo) *cache); 100 101 khint_t ltk_create_glyph_cache(LtkTextManager *tm, uint16_t font_id, uint16_t font_size); 102 103 void ltk_destroy_glyph_cache(khash_t(glyphinfo) *cache); 104 105 void ltk_load_default_font(LtkTextManager *tm, char *name); 106 107 LtkFont *ltk_create_font(char *path, uint16_t id); 108 109 void ltk_destroy_font(LtkFont *font); 110 111 /* FIXME: need to figure out how exactly the whole font system is going to work, especially with default fonts, etc. */ 112 uint16_t ltk_load_font(LtkTextManager *tm, char *path); 113 114 uint16_t ltk_get_font(LtkTextManager *tm, char *path); 115 116 void ltk_destroy_glyph(LtkGlyph *glyph, khash_t(glyphinfo) *cache); 117 118 #endif /* _TEXT_COMMON_H_ */