find-font-unix.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. Copyright (C) 2014 by Leonhard Oelke <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include <fontconfig/fontconfig.h>
  15. #include <util/base.h>
  16. #include <util/dstr.h>
  17. #include "find-font.h"
  18. #include "text-freetype2.h"
  19. void free_os_font_list(void) {}
  20. bool load_cached_os_font_list(void)
  21. {
  22. return true;
  23. }
  24. void load_os_font_list(void) {}
  25. const char *get_font_path(const char *family, uint16_t size, const char *style, uint32_t flags, FT_Long *idx)
  26. {
  27. bool bold = !!(flags & OBS_FONT_BOLD);
  28. bool italic = !!(flags & OBS_FONT_ITALIC);
  29. FcPattern *pattern = FcPatternCreate();
  30. FcPattern *match = NULL;
  31. bool success = false;
  32. FcResult match_result;
  33. /* somewhat of a cheap hack */
  34. static __thread char result[512];
  35. FcPatternAddString(pattern, FC_FAMILY, (const FcChar8 *)family);
  36. FcPatternAddString(pattern, FC_STYLE, (const FcChar8 *)style);
  37. FcPatternAddInteger(pattern, FC_WEIGHT, bold ? FC_WEIGHT_BOLD : FC_WEIGHT_REGULAR);
  38. FcPatternAddInteger(pattern, FC_SLANT, italic ? FC_SLANT_ITALIC : FC_SLANT_ROMAN);
  39. FcPatternAddDouble(pattern, FC_SIZE, (double)size);
  40. FcConfigSubstitute(NULL, pattern, FcMatchPattern);
  41. FcDefaultSubstitute(pattern);
  42. match = FcFontMatch(NULL, pattern, &match_result);
  43. if (match) {
  44. FcChar8 *path = FcPatternFormat(match, (const FcChar8 *)"%{file}");
  45. strncpy(result, (char *)path, 511);
  46. FcStrFree(path);
  47. int fc_index = 0;
  48. FcPatternGetInteger(match, FC_INDEX, 1, &fc_index);
  49. *idx = (FT_Long)fc_index;
  50. FcPatternDestroy(match);
  51. success = true;
  52. } else {
  53. blog(LOG_WARNING, "no matching font for '%s' found", family);
  54. }
  55. FcPatternDestroy(pattern);
  56. return success ? &result[0] : NULL;
  57. }