find-font-cocoa.m 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 = [path stringByAppendingPathComponent:file];
  26. BOOL is_dir = FALSE;
  27. bool folder_exists = [file_manager
  28. 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, NSString *path)
  65. {
  66. NSArray *files = NULL;
  67. files = [file_manager contentsOfDirectoryAtPath:path error:nil];
  68. for (NSString *file in files) {
  69. NSString *full_path = [path stringByAppendingPathComponent:file];
  70. checksum = add_font_checksum(checksum,
  71. full_path.fileSystemRepresentation);
  72. }
  73. return checksum;
  74. }
  75. uint32_t get_font_checksum(void)
  76. {
  77. uint32_t checksum = 0;
  78. @autoreleasepool {
  79. BOOL is_dir;
  80. NSArray *paths = NSSearchPathForDirectoriesInDomains(
  81. NSLibraryDirectory, NSAllDomainsMask, true);
  82. for (NSString *path in paths) {
  83. NSFileManager *file_manager =
  84. [NSFileManager defaultManager];
  85. NSString *font_path =
  86. [path stringByAppendingPathComponent:@"Fonts"];
  87. bool folder_exists = [file_manager
  88. fileExistsAtPath:font_path
  89. isDirectory:&is_dir];
  90. if (folder_exists && is_dir)
  91. checksum = add_font_checksum_path(checksum,
  92. file_manager, font_path);
  93. }
  94. }
  95. return checksum;
  96. }