find-font-cocoa.m 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #include <util/darray.h>
  2. #include <util/crc32.h>
  3. #include "find-font.h"
  4. #include "text-freetype2.h"
  5. #import <Foundation/Foundation.h>
  6. extern void save_font_list(void);
  7. static inline void add_path_font(const char *path)
  8. {
  9. FT_Face face;
  10. FT_Long idx = 0;
  11. FT_Long max_faces = 1;
  12. while (idx < max_faces) {
  13. if (FT_New_Face(ft2_lib, path, idx, &face) != 0)
  14. break;
  15. build_font_path_info(face, idx++, path);
  16. max_faces = face->num_faces;
  17. FT_Done_Face(face);
  18. }
  19. }
  20. static void add_path_fonts(NSFileManager *file_manager, NSString *path)
  21. {
  22. NSArray *files = NULL;
  23. files = [file_manager contentsOfDirectoryAtPath:path error:nil];
  24. for (NSString *file in files) {
  25. NSString *full_path =
  26. [path stringByAppendingPathComponent:file];
  27. BOOL is_dir = FALSE;
  28. bool folder_exists = [file_manager fileExistsAtPath:full_path
  29. isDirectory:&is_dir];
  30. if (folder_exists && is_dir) {
  31. add_path_fonts(file_manager, full_path);
  32. } else {
  33. add_path_font(full_path.fileSystemRepresentation);
  34. }
  35. }
  36. }
  37. void load_os_font_list(void)
  38. {
  39. @autoreleasepool {
  40. BOOL is_dir;
  41. NSArray *paths = NSSearchPathForDirectoriesInDomains(
  42. NSLibraryDirectory, NSAllDomainsMask, true);
  43. for (NSString *path in paths) {
  44. NSFileManager *file_manager =
  45. [NSFileManager defaultManager];
  46. NSString *font_path =
  47. [path stringByAppendingPathComponent:@"Fonts"];
  48. bool folder_exists = [file_manager
  49. fileExistsAtPath:font_path
  50. isDirectory:&is_dir];
  51. if (folder_exists && is_dir)
  52. add_path_fonts(file_manager, font_path);
  53. }
  54. save_font_list();
  55. }
  56. }
  57. static uint32_t add_font_checksum(uint32_t checksum, const char *path)
  58. {
  59. if (path && *path)
  60. checksum = calc_crc32(checksum, path, strlen(path));
  61. return checksum;
  62. }
  63. static uint32_t add_font_checksum_path(uint32_t checksum,
  64. NSFileManager *file_manager,
  65. NSString *path)
  66. {
  67. NSArray *files = NULL;
  68. files = [file_manager contentsOfDirectoryAtPath:path error:nil];
  69. for (NSString *file in files) {
  70. NSString *full_path =
  71. [path stringByAppendingPathComponent:file];
  72. checksum = add_font_checksum(
  73. checksum, full_path.fileSystemRepresentation);
  74. }
  75. return checksum;
  76. }
  77. uint32_t get_font_checksum(void)
  78. {
  79. uint32_t checksum = 0;
  80. @autoreleasepool {
  81. BOOL is_dir;
  82. NSArray *paths = NSSearchPathForDirectoriesInDomains(
  83. NSLibraryDirectory, NSAllDomainsMask, true);
  84. for (NSString *path in paths) {
  85. NSFileManager *file_manager =
  86. [NSFileManager defaultManager];
  87. NSString *font_path =
  88. [path stringByAppendingPathComponent:@"Fonts"];
  89. bool folder_exists = [file_manager
  90. fileExistsAtPath:font_path
  91. isDirectory:&is_dir];
  92. if (folder_exists && is_dir)
  93. checksum = add_font_checksum_path(
  94. checksum, file_manager, font_path);
  95. }
  96. }
  97. return checksum;
  98. }