find-font-cocoa.m 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. add_path_font(full_path.fileSystemRepresentation);
  27. }
  28. }
  29. void load_os_font_list(void)
  30. {
  31. @autoreleasepool {
  32. BOOL is_dir;
  33. NSArray *paths = NSSearchPathForDirectoriesInDomains(
  34. NSLibraryDirectory, NSAllDomainsMask, true);
  35. for (NSString *path in paths) {
  36. NSFileManager *file_manager =
  37. [NSFileManager defaultManager];
  38. NSString *font_path =
  39. [path stringByAppendingPathComponent:@"Fonts"];
  40. bool folder_exists = [file_manager
  41. fileExistsAtPath:font_path
  42. isDirectory:&is_dir];
  43. if (folder_exists && is_dir)
  44. add_path_fonts(file_manager, font_path);
  45. }
  46. save_font_list();
  47. }
  48. }
  49. static uint32_t add_font_checksum(uint32_t checksum, const char *path)
  50. {
  51. if (path && *path)
  52. checksum = calc_crc32(checksum, path, strlen(path));
  53. return checksum;
  54. }
  55. static uint32_t add_font_checksum_path(uint32_t checksum,
  56. NSFileManager *file_manager, NSString *path)
  57. {
  58. NSArray *files = NULL;
  59. files = [file_manager contentsOfDirectoryAtPath:path error:nil];
  60. for (NSString *file in files) {
  61. NSString *full_path = [path stringByAppendingPathComponent:file];
  62. checksum = add_font_checksum(checksum,
  63. full_path.fileSystemRepresentation);
  64. }
  65. return checksum;
  66. }
  67. uint32_t get_font_checksum(void)
  68. {
  69. uint32_t checksum = 0;
  70. @autoreleasepool {
  71. BOOL is_dir;
  72. NSArray *paths = NSSearchPathForDirectoriesInDomains(
  73. NSLibraryDirectory, NSAllDomainsMask, true);
  74. for (NSString *path in paths) {
  75. NSFileManager *file_manager =
  76. [NSFileManager defaultManager];
  77. NSString *font_path =
  78. [path stringByAppendingPathComponent:@"Fonts"];
  79. bool folder_exists = [file_manager
  80. fileExistsAtPath:font_path
  81. isDirectory:&is_dir];
  82. if (folder_exists && is_dir)
  83. checksum = add_font_checksum_path(checksum,
  84. file_manager, font_path);
  85. }
  86. }
  87. return checksum;
  88. }