The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

fontconfig.c in libbirdfont/Renderer

This file is a part of the Birdfont project.

Contributing

Send patches or pull requests to johan.mattsson.m@gmail.com.
Clone this repository: git clone https://github.com/johanmattssonm/birdfont.git

Revisions

View the latest version of libbirdfont/Renderer/fontconfig.c.
Fix compilation error
1 /* 2 Copyright (C) 2015 Johan Mattsson 3 4 This library is free software; you can redistribute it and/or modify 5 it under the terms of the GNU Lesser General Public License as 6 published by the Free Software Foundation; either version 3 of the 7 License, or (at your option) any later version. 8 9 This library is distributed in the hope that it will be useful, but 10 WITHOUT ANY WARRANTY; without even the implied warranty of 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 Lesser General Public License for more details. 13 */ 14 15 #include <stdio.h> 16 #include <glib.h> 17 #include <fontconfig/fontconfig.h> 18 19 /** Find a fallback font for a set of characters. 20 * @return A path to the font file. 21 */ 22 gchar* find_font (FcConfig* fontconfig, const gchar* characters) { 23 FcPattern* pattern; 24 FcCharSet* character_set; 25 FcObjectSet* font_properties; 26 FcFontSet* fonts; 27 FcPattern* font; 28 FcChar8* path; 29 gchar* result; 30 gchar* remaining_characters; 31 gunichar character; 32 33 if (fontconfig == NULL) { 34 g_warning("Font config not loaded."); 35 return NULL; 36 } 37 38 result = NULL; 39 pattern = FcPatternCreate (); 40 41 character_set = FcCharSetCreate (); 42 43 remaining_characters = (gchar*) characters; 44 while (TRUE) { 45 character = g_utf8_get_char (remaining_characters); 46 47 if (character == '\0') { 48 break; 49 } 50 51 FcCharSetAddChar(character_set, character); 52 53 remaining_characters = g_utf8_next_char (remaining_characters); 54 } 55 56 FcPatternAddCharSet (pattern, FC_CHARSET, character_set); 57 FcCharSetDestroy (character_set); 58 FcPatternAddInteger (pattern, FC_SLANT, FC_SLANT_ROMAN); 59 60 FcPatternAddBool(pattern, FC_SCALABLE, FcTrue); 61 font_properties = FcObjectSetBuild (FC_FILE, NULL); 62 fonts = FcFontList (fontconfig, pattern, font_properties); 63 64 if (fonts && fonts->nfont > 0) { 65 font = fonts->fonts[0]; 66 if (FcPatternGetString(font, FC_FILE, 0, &path) == FcResultMatch) { 67 result = g_strdup ((gchar*) path); 68 } 69 } 70 71 if (fonts) { 72 FcFontSetDestroy(fonts); 73 } 74 75 if (pattern) { 76 FcPatternDestroy(pattern); 77 } 78 79 return result; 80 } 81 82 /** Find a font file from its family name. 83 * @param font_config fontconfig instance 84 * @param font_name name of the font 85 * @return full path to the font file 86 */ 87 gchar* find_font_file (FcConfig* font_config, const gchar* font_name) { 88 const FcChar8* name; 89 FcPattern* search_pattern; 90 FcPattern* font; 91 FcChar8* file; 92 gchar* path; 93 FcObjectSet* font_properties; 94 FcFontSet* fonts; 95 int i; 96 97 if (font_config == NULL) { 98 g_warning("Font config not loaded."); 99 return NULL; 100 } 101 102 path = NULL; 103 name = font_name; 104 105 search_pattern = FcPatternCreate (); 106 FcPatternAddString (search_pattern, FC_FAMILY, name); 107 FcPatternAddBool (search_pattern, FC_SCALABLE, FcTrue); 108 FcPatternAddInteger (search_pattern, FC_WEIGHT, FC_WEIGHT_MEDIUM); 109 FcPatternAddInteger (search_pattern, FC_SLANT, FC_SLANT_ROMAN); 110 111 font_properties = FcObjectSetBuild (FC_FILE, NULL); 112 fonts = FcFontList (font_config, search_pattern, font_properties); 113 114 if (fonts->nfont > 0) { 115 for (i = 0; i < fonts->nfont; i++) { 116 font = fonts->fonts[i]; 117 118 if (FcPatternGetString(font, FC_FILE, 0, &file) == FcResultMatch) { 119 path = g_strdup ((gchar*) file); 120 break; 121 } 122 } 123 FcPatternDestroy (font); 124 } 125 126 FcPatternDestroy (search_pattern); 127 128 return path; 129 } 130