find-font-unix.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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,
  26. uint32_t flags, FT_Long *idx)
  27. {
  28. bool bold = !!(flags & OBS_FONT_BOLD);
  29. bool italic = !!(flags & OBS_FONT_ITALIC);
  30. FcPattern *pattern = FcPatternCreate();
  31. FcPattern *match = NULL;
  32. bool success = false;
  33. FcResult match_result;
  34. /* somewhat of a cheap hack */
  35. static __thread char result[512];
  36. FcPatternAddString(pattern, FC_FAMILY, (const FcChar8 *)family);
  37. FcPatternAddString(pattern, FC_STYLE, (const FcChar8 *)style);
  38. FcPatternAddInteger(pattern, FC_WEIGHT,
  39. bold ? FC_WEIGHT_BOLD : FC_WEIGHT_REGULAR);
  40. FcPatternAddInteger(pattern, FC_SLANT,
  41. italic ? FC_SLANT_ITALIC : FC_SLANT_ROMAN);
  42. FcPatternAddDouble(pattern, FC_SIZE, (double)size);
  43. FcConfigSubstitute(NULL, pattern, FcMatchPattern);
  44. FcDefaultSubstitute(pattern);
  45. match = FcFontMatch(NULL, pattern, &match_result);
  46. if (match) {
  47. FcChar8 *path =
  48. FcPatternFormat(match, (const FcChar8 *)"%{file}");
  49. strncpy(result, (char *)path, 511);
  50. FcStrFree(path);
  51. int fc_index = 0;
  52. FcPatternGetInteger(match, FC_INDEX, 1, &fc_index);
  53. *idx = (FT_Long)fc_index;
  54. FcPatternDestroy(match);
  55. success = true;
  56. } else {
  57. blog(LOG_WARNING, "no matching font for '%s' found", family);
  58. }
  59. FcPatternDestroy(pattern);
  60. return success ? &result[0] : NULL;
  61. }