find-font-unix.c 2.2 KB

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