-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathft_utils.cc
71 lines (59 loc) · 1.83 KB
/
ft_utils.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//========================================================================
//
// ft_util.cc
//
// FreeType helper functions.
//
// This file is licensed under the GPLv2 or later
//
// Copyright (C) 2022 Adrian Johnson <[email protected]>
//
//========================================================================
#include <cstdio>
#include "ft_utils.h"
#include "gfile.h"
#ifdef _WIN32
static unsigned long ft_stream_read(FT_Stream stream, unsigned long offset, unsigned char *buffer, unsigned long count)
{
FILE *file = (FILE *)stream->descriptor.pointer;
fseek(file, offset, SEEK_SET);
return fread(buffer, 1, count, file);
}
static void ft_stream_close(FT_Stream stream)
{
FILE *file = (FILE *)stream->descriptor.pointer;
fclose(file);
delete stream;
}
#endif
// Same as FT_New_Face() but handles UTF-8 filenames on Windows
FT_Error ft_new_face_from_file(FT_Library library, const char *filename_utf8, FT_Long face_index, FT_Face *aface)
{
#ifdef _WIN32
FILE *file;
long size;
if (!filename_utf8)
return FT_Err_Invalid_Argument;
file = openFile(filename_utf8, "rb");
if (!file)
return FT_Err_Cannot_Open_Resource;
fseek(file, 0, SEEK_END);
size = ftell(file);
rewind(file);
if (size <= 0)
return FT_Err_Cannot_Open_Stream;
FT_StreamRec *stream = new FT_StreamRec;
*stream = {};
stream->size = size;
stream->read = ft_stream_read;
stream->close = ft_stream_close;
stream->descriptor.pointer = file;
FT_Open_Args args = {};
args.flags = FT_OPEN_STREAM;
args.stream = stream;
return FT_Open_Face(library, &args, face_index, aface);
#else
// On POSIX, FT_New_Face mmaps font files. If not Windows, prefer FT_New_Face over our stdio.h based FT_Open_Face.
return FT_New_Face(library, filename_utf8, face_index, aface);
#endif
}