2014-08-26 12:43:33 -07:00
|
|
|
#include <util/darray.h>
|
2015-10-04 03:30:20 -07:00
|
|
|
#include <util/crc32.h>
|
2014-08-26 12:43:33 -07:00
|
|
|
#include "find-font.h"
|
|
|
|
#include "text-freetype2.h"
|
|
|
|
|
|
|
|
#import <Foundation/Foundation.h>
|
|
|
|
|
2015-10-04 03:30:20 -07:00
|
|
|
extern void save_font_list(void);
|
|
|
|
|
2014-08-26 12:43:33 -07:00
|
|
|
static inline void add_path_font(const char *path)
|
|
|
|
{
|
|
|
|
FT_Face face;
|
|
|
|
FT_Long idx = 0;
|
|
|
|
FT_Long max_faces = 1;
|
|
|
|
|
|
|
|
while (idx < max_faces) {
|
|
|
|
if (FT_New_Face(ft2_lib, path, idx, &face) != 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
build_font_path_info(face, idx++, path);
|
|
|
|
max_faces = face->num_faces;
|
|
|
|
FT_Done_Face(face);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void add_path_fonts(NSFileManager *file_manager, NSString *path)
|
|
|
|
{
|
|
|
|
NSArray *files = NULL;
|
|
|
|
|
|
|
|
files = [file_manager contentsOfDirectoryAtPath:path error:nil];
|
|
|
|
|
|
|
|
for (NSString *file in files) {
|
2019-07-09 11:29:39 -07:00
|
|
|
NSString *full_path =
|
|
|
|
[path stringByAppendingPathComponent:file];
|
2014-08-26 12:43:33 -07:00
|
|
|
|
2017-05-05 22:43:57 -07:00
|
|
|
BOOL is_dir = FALSE;
|
2019-07-09 11:29:39 -07:00
|
|
|
bool folder_exists = [file_manager fileExistsAtPath:full_path
|
|
|
|
isDirectory:&is_dir];
|
2017-05-05 22:43:57 -07:00
|
|
|
|
|
|
|
if (folder_exists && is_dir) {
|
|
|
|
add_path_fonts(file_manager, full_path);
|
|
|
|
} else {
|
|
|
|
add_path_font(full_path.fileSystemRepresentation);
|
|
|
|
}
|
2014-08-26 12:43:33 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void load_os_font_list(void)
|
|
|
|
{
|
|
|
|
@autoreleasepool {
|
|
|
|
BOOL is_dir;
|
|
|
|
NSArray *paths = NSSearchPathForDirectoriesInDomains(
|
2019-07-09 11:29:39 -07:00
|
|
|
NSLibraryDirectory, NSAllDomainsMask, true);
|
2014-08-26 12:43:33 -07:00
|
|
|
|
|
|
|
for (NSString *path in paths) {
|
|
|
|
NSFileManager *file_manager =
|
|
|
|
[NSFileManager defaultManager];
|
|
|
|
NSString *font_path =
|
|
|
|
[path stringByAppendingPathComponent:@"Fonts"];
|
|
|
|
|
|
|
|
bool folder_exists = [file_manager
|
2019-07-09 11:29:39 -07:00
|
|
|
fileExistsAtPath:font_path
|
|
|
|
isDirectory:&is_dir];
|
2014-08-26 12:43:33 -07:00
|
|
|
|
|
|
|
if (folder_exists && is_dir)
|
|
|
|
add_path_fonts(file_manager, font_path);
|
|
|
|
}
|
2015-10-04 03:30:20 -07:00
|
|
|
|
|
|
|
save_font_list();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t add_font_checksum(uint32_t checksum, const char *path)
|
|
|
|
{
|
|
|
|
if (path && *path)
|
|
|
|
checksum = calc_crc32(checksum, path, strlen(path));
|
|
|
|
return checksum;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t add_font_checksum_path(uint32_t checksum,
|
2019-07-09 11:29:39 -07:00
|
|
|
NSFileManager *file_manager,
|
|
|
|
NSString *path)
|
2015-10-04 03:30:20 -07:00
|
|
|
{
|
|
|
|
NSArray *files = NULL;
|
|
|
|
|
|
|
|
files = [file_manager contentsOfDirectoryAtPath:path error:nil];
|
|
|
|
|
|
|
|
for (NSString *file in files) {
|
2019-07-09 11:29:39 -07:00
|
|
|
NSString *full_path =
|
|
|
|
[path stringByAppendingPathComponent:file];
|
2015-10-04 03:30:20 -07:00
|
|
|
|
2019-07-09 11:29:39 -07:00
|
|
|
checksum = add_font_checksum(
|
|
|
|
checksum, full_path.fileSystemRepresentation);
|
2015-10-04 03:30:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return checksum;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t get_font_checksum(void)
|
|
|
|
{
|
|
|
|
uint32_t checksum = 0;
|
|
|
|
|
|
|
|
@autoreleasepool {
|
|
|
|
BOOL is_dir;
|
|
|
|
NSArray *paths = NSSearchPathForDirectoriesInDomains(
|
2019-07-09 11:29:39 -07:00
|
|
|
NSLibraryDirectory, NSAllDomainsMask, true);
|
2015-10-04 03:30:20 -07:00
|
|
|
|
|
|
|
for (NSString *path in paths) {
|
|
|
|
NSFileManager *file_manager =
|
|
|
|
[NSFileManager defaultManager];
|
|
|
|
NSString *font_path =
|
|
|
|
[path stringByAppendingPathComponent:@"Fonts"];
|
|
|
|
|
|
|
|
bool folder_exists = [file_manager
|
2019-07-09 11:29:39 -07:00
|
|
|
fileExistsAtPath:font_path
|
|
|
|
isDirectory:&is_dir];
|
2015-10-04 03:30:20 -07:00
|
|
|
|
|
|
|
if (folder_exists && is_dir)
|
2019-07-09 11:29:39 -07:00
|
|
|
checksum = add_font_checksum_path(
|
|
|
|
checksum, file_manager, font_path);
|
2015-10-04 03:30:20 -07:00
|
|
|
}
|
2014-08-26 12:43:33 -07:00
|
|
|
}
|
2015-10-04 03:30:20 -07:00
|
|
|
|
|
|
|
return checksum;
|
2014-08-26 12:43:33 -07:00
|
|
|
}
|