find-font-cocoa.m 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #include <util/darray.h>
  2. #include "find-font.h"
  3. #include "text-freetype2.h"
  4. #import <Foundation/Foundation.h>
  5. static inline void add_path_font(const char *path)
  6. {
  7. FT_Face face;
  8. FT_Long idx = 0;
  9. FT_Long max_faces = 1;
  10. while (idx < max_faces) {
  11. if (FT_New_Face(ft2_lib, path, idx, &face) != 0)
  12. break;
  13. build_font_path_info(face, idx++, path);
  14. max_faces = face->num_faces;
  15. FT_Done_Face(face);
  16. }
  17. }
  18. static void add_path_fonts(NSFileManager *file_manager, NSString *path)
  19. {
  20. NSArray *files = NULL;
  21. files = [file_manager contentsOfDirectoryAtPath:path error:nil];
  22. for (NSString *file in files) {
  23. NSString *full_path = [path stringByAppendingPathComponent:file];
  24. add_path_font(full_path.fileSystemRepresentation);
  25. }
  26. }
  27. void load_os_font_list(void)
  28. {
  29. @autoreleasepool {
  30. BOOL is_dir;
  31. NSArray *paths = NSSearchPathForDirectoriesInDomains(
  32. NSLibraryDirectory, NSAllDomainsMask, true);
  33. for (NSString *path in paths) {
  34. NSFileManager *file_manager =
  35. [NSFileManager defaultManager];
  36. NSString *font_path =
  37. [path stringByAppendingPathComponent:@"Fonts"];
  38. bool folder_exists = [file_manager
  39. fileExistsAtPath:font_path
  40. isDirectory:&is_dir];
  41. if (folder_exists && is_dir)
  42. add_path_fonts(file_manager, font_path);
  43. }
  44. }
  45. }