Thanks to http://stackoverflow.com/a/14634033/1447751
| 159 | #ifdef ENABLE_FONTCONFIG |
| 160 | // Thanks to http://stackoverflow.com/a/14634033/1447751 |
| 161 | const std::string get_font_path(const std::string &name) { |
| 162 | std::string path; |
| 163 | const FcChar8 *fcname = reinterpret_cast<const FcChar8 *>(name.c_str()); |
| 164 | FcConfig *config = FcInitLoadConfigAndFonts(); |
| 165 | |
| 166 | // configure the search pattern, |
| 167 | // assume "name" is a std::string with the desired font name in it |
| 168 | FcPattern *pat = FcNameParse(fcname); |
| 169 | FcConfigSubstitute(config, pat, FcMatchPattern); |
| 170 | FcDefaultSubstitute(pat); |
| 171 | |
| 172 | FcResult result; |
| 173 | // find the font |
| 174 | FcPattern *font = FcFontMatch(config, pat, &result); |
| 175 | if (font) { |
| 176 | FcChar8 *fcpath = NULL; |
| 177 | FcChar8 *fcfamily = NULL; |
| 178 | if (FcPatternGetString(font, FC_FAMILY, 0, &fcfamily) == FcResultMatch && |
| 179 | (name.empty() || !FcStrCmpIgnoreCase(fcname, fcfamily)) // Empty name means searching for default font, otherwise make |
| 180 | // sure the returned font is exactly what we searched for |
| 181 | && |
| 182 | FcPatternGetString(font, FC_FILE, 0, &fcpath) == FcResultMatch) |
| 183 | path = std::string(reinterpret_cast<char *>(fcpath)); |
| 184 | FcPatternDestroy(font); |
| 185 | } |
| 186 | |
| 187 | FcPatternDestroy(pat); |
| 188 | return path; |
| 189 | } |
| 190 | #endif |
| 191 | |
| 192 | #ifndef __APPLE__ |