font.c (7789B)
1 /* wld: font.c 2 * 3 * Copyright (c) 2013, 2014 Michael Forney 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 13 * all 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 #include "wld-private.h" 25 26 #ifdef WLD_FONTCONFIG 27 #include <fontconfig/fcfreetype.h> 28 #endif 29 #include <string.h> 30 31 EXPORT 32 struct wld_font_context * 33 wld_font_create_context() 34 { 35 struct wld_font_context *context; 36 37 context = malloc(sizeof *context); 38 39 if (!context) 40 goto error0; 41 42 if (FT_Init_FreeType(&context->library) != 0) { 43 DEBUG("Failed to initialize FreeType library\n"); 44 45 goto error1; 46 } 47 48 return context; 49 50 error1: 51 free(context); 52 error0: 53 return NULL; 54 } 55 56 EXPORT 57 void 58 wld_font_destroy_context(struct wld_font_context *context) 59 { 60 FT_Done_FreeType(context->library); 61 free(context); 62 } 63 64 /* A FreeType face put at a size: the font every way of opening one ends in. */ 65 static struct wld_font * 66 open_face(struct wld_font_context *context, FT_Face face, double pixel_size, 67 double aspect) 68 { 69 struct font *font; 70 71 font = malloc(sizeof *font); 72 73 if (!font) { 74 FT_Done_Face(face); 75 return NULL; 76 } 77 78 font->context = context; 79 font->face = face; 80 memset(font->chars, 0, sizeof font->chars); 81 82 if (font->face->face_flags & FT_FACE_FLAG_SCALABLE) { 83 FT_F26Dot6 width, height; 84 85 width = ((unsigned int)pixel_size) << 6; 86 height = ((unsigned int)(pixel_size * aspect)) << 6; 87 88 FT_Set_Char_Size(font->face, width, height, 0, 0); 89 } else { 90 FT_Set_Pixel_Sizes(font->face, (unsigned int)pixel_size, 91 (unsigned int)(pixel_size * aspect)); 92 } 93 94 font->base.ascent = font->face->size->metrics.ascender >> 6; 95 font->base.descent = -font->face->size->metrics.descender >> 6; 96 font->base.height = font->base.ascent + font->base.descent; 97 font->base.max_advance = font->face->size->metrics.max_advance >> 6; 98 99 font->glyphs = calloc(font->face->num_glyphs, sizeof(struct glyph *)); 100 101 return &font->base; 102 } 103 104 EXPORT 105 struct wld_font * 106 wld_font_open_file(struct wld_font_context *context, const char *path, 107 int index, double pixel_size) 108 { 109 FT_Face face; 110 111 DEBUG("Loading font file: %s\n", path); 112 113 if (FT_New_Face(context->library, path, index, &face) != 0) 114 return NULL; 115 116 return open_face(context, face, pixel_size, 1.0); 117 } 118 119 #ifdef WLD_FONTCONFIG 120 EXPORT 121 struct wld_font * 122 wld_font_open_name(struct wld_font_context *context, const char *name) 123 { 124 FcPattern *pattern, *match; 125 FcResult result; 126 127 DEBUG("Opening font with name: %s\n", name); 128 129 pattern = FcNameParse((const FcChar8 *)name); 130 FcConfigSubstitute(NULL, pattern, FcMatchPattern); 131 FcDefaultSubstitute(pattern); 132 133 match = FcFontMatch(NULL, pattern, &result); 134 135 if (!match) 136 return NULL; 137 138 return wld_font_open_pattern(context, match); 139 } 140 141 EXPORT 142 struct wld_font * 143 wld_font_open_pattern(struct wld_font_context *context, FcPattern *match) 144 { 145 char *filename; 146 FT_Face face; 147 FcResult result; 148 double pixel_size = 12, aspect; 149 int index = 0; 150 151 result = FcPatternGetString(match, FC_FILE, 0, (FcChar8 **)&filename); 152 FcPatternGetInteger(match, FC_INDEX, 0, &index); 153 154 if (result == FcResultMatch) { 155 DEBUG("Loading font file: %s\n", filename); 156 157 if (FT_New_Face(context->library, filename, index, &face) == 0) 158 goto load_face; 159 } 160 161 if (FcPatternGetFTFace(match, FC_FT_FACE, 0, &face) != FcResultMatch) { 162 DEBUG("Couldn't determine font filename or FreeType face\n"); 163 return NULL; 164 } 165 166 load_face: 167 FcPatternGetDouble(match, FC_PIXEL_SIZE, 0, &pixel_size); 168 169 if (FcPatternGetDouble(match, FC_ASPECT, 0, &aspect) != FcResultMatch) 170 aspect = 1.0; 171 172 return open_face(context, face, pixel_size, aspect); 173 } 174 #endif 175 176 EXPORT 177 void 178 wld_font_close(struct wld_font *font_base) 179 { 180 struct font *font = (void *)font_base; 181 182 FT_Done_Face(font->face); 183 free(font); 184 } 185 186 FT_UInt 187 font_char_index(struct font *font, uint32_t character) 188 { 189 unsigned slot = character % FONT_CHAR_CACHE; 190 191 /* the key is the character plus one, so an empty slot (0) matches 192 * nothing and a character the face lacks is remembered too */ 193 if (font->chars[slot].key != character + 1) { 194 font->chars[slot].key = character + 1; 195 font->chars[slot].index = FT_Get_Char_Index(font->face, character); 196 } 197 return font->chars[slot].index; 198 } 199 200 bool 201 font_ensure_glyph(struct font *font, FT_UInt glyph_index) 202 { 203 if (glyph_index) { 204 if (!font->glyphs[glyph_index]) { 205 struct glyph *glyph; 206 207 glyph = malloc(sizeof *glyph); 208 209 if (!glyph) 210 return false; 211 212 FT_Load_Glyph(font->face, glyph_index, 213 FT_LOAD_RENDER | FT_LOAD_TARGET_NORMAL); 214 215 FT_Bitmap_New(&glyph->bitmap); 216 217 FT_Bitmap_Copy(font->context->library, 218 &font->face->glyph->bitmap, &glyph->bitmap); 219 220 glyph->advance = font->face->glyph->metrics.horiAdvance >> 6; 221 glyph->x = font->face->glyph->bitmap_left; 222 glyph->y = -font->face->glyph->bitmap_top; 223 224 font->glyphs[glyph_index] = glyph; 225 } 226 227 return true; 228 } 229 230 return false; 231 } 232 233 EXPORT 234 bool 235 wld_font_ensure_char(struct wld_font *font_base, uint32_t character) 236 { 237 struct font *font = (void *)font_base; 238 FT_UInt glyph_index; 239 240 glyph_index = font_char_index(font, character); 241 242 return font_ensure_glyph(font, glyph_index); 243 } 244 245 EXPORT 246 uint32_t 247 wld_font_char_by_name(struct wld_font *font_base, const char *name) 248 { 249 struct font *font = (void *)font_base; 250 FT_UInt glyph_index, index; 251 FT_ULong c; 252 253 if (!FT_HAS_GLYPH_NAMES(font->face)) 254 return 0; 255 glyph_index = FT_Get_Name_Index(font->face, (FT_String *)name); 256 if (!glyph_index) 257 return 0; 258 for (c = FT_Get_First_Char(font->face, &index); index; c = FT_Get_Next_Char(font->face, c, &index)) { 259 if (index == glyph_index) 260 return c; 261 } 262 return 0; 263 } 264 265 EXPORT 266 void 267 wld_font_text_extents_n(struct wld_font *font_base, 268 const char *text, int32_t length, 269 struct wld_extents *extents) 270 { 271 struct font *font = (void *)font_base; 272 int ret; 273 uint32_t c; 274 FT_UInt glyph_index; 275 276 struct glyph *glyph; 277 int32_t x1 = INT32_MAX, y1 = INT32_MAX, x2 = INT32_MIN, y2 = INT32_MIN; 278 279 extents->advance = 0; 280 281 while ((ret = wld_utf8_decode(text, length, &c)) > 0 && c != '\0') { 282 length -= ret; 283 text += ret; 284 glyph_index = font_char_index(font, c); 285 286 if (!font_ensure_glyph(font, glyph_index)) 287 continue; 288 289 glyph = font->glyphs[glyph_index]; 290 if (glyph->bitmap.width && glyph->bitmap.rows) { 291 int32_t gx = (int32_t)extents->advance + glyph->x; 292 293 if (gx < x1) 294 x1 = gx; 295 if (glyph->y < y1) 296 y1 = glyph->y; 297 if (gx + (int32_t)glyph->bitmap.width > x2) 298 x2 = gx + glyph->bitmap.width; 299 if (glyph->y + (int32_t)glyph->bitmap.rows > y2) 300 y2 = glyph->y + glyph->bitmap.rows; 301 } 302 extents->advance += glyph->advance; 303 } 304 305 if (x1 > x2) { 306 extents->ink_x = extents->ink_y = 0; 307 extents->ink_width = extents->ink_height = 0; 308 return; 309 } 310 extents->ink_x = x1; 311 extents->ink_y = y1; 312 extents->ink_width = x2 - x1; 313 extents->ink_height = y2 - y1; 314 }