Blender V4.3
blf_font_win32_compat.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
13#ifdef WIN32
14
15# include <stdio.h>
16
17# include <ft2build.h>
18# include FT_FREETYPE_H
19
20# include "MEM_guardedalloc.h"
21
22# include "BLI_fileops.h"
23# include "BLI_utildefines.h"
24
25# include "blf_internal.hh"
26
27/* internal freetype defines */
28# define STREAM_FILE(stream) static_cast<FILE *>(stream->descriptor.pointer)
29# define FT_THROW(e) -1
30
31static void ft_ansi_stream_close(FT_Stream stream)
32{
33 fclose(STREAM_FILE(stream));
34
35 stream->descriptor.pointer = nullptr;
36 stream->size = 0;
37 stream->base = 0;
38
39 /* WARNING: this works but be careful!
40 * Checked freetype sources, there isn't any access after closing. */
41 MEM_freeN(stream);
42}
43
44static ulong ft_ansi_stream_io(FT_Stream stream, ulong offset, uchar *buffer, ulong count)
45{
46
47 if (!count && offset > stream->size) {
48 return 1;
49 }
50
51 FILE *file = STREAM_FILE(stream);
52
53 if (stream->pos != offset) {
54 BLI_fseek(file, offset, SEEK_SET);
55 }
56
57 return fread(buffer, 1, count, file);
58}
59
60static FT_Error FT_Stream_Open__win32_compat(FT_Stream stream, const char *filepathname)
61{
62 BLI_assert(stream);
63
64 stream->descriptor.pointer = nullptr;
65 stream->pathname.pointer = (char *)filepathname;
66 stream->base = 0;
67 stream->pos = 0;
68 stream->read = nullptr;
69 stream->close = nullptr;
70
71 FILE *file = BLI_fopen(filepathname, "rb");
72 if (!file) {
73 fprintf(stderr,
74 "FT_Stream_Open: "
75 "could not open `%s'\n",
76 filepathname);
77 return FT_THROW(Cannot_Open_Resource);
78 }
79
80 BLI_fseek(file, 0LL, SEEK_END);
81 stream->size = ftell(file);
82 if (!stream->size) {
83 fprintf(stderr,
84 "FT_Stream_Open: "
85 "opened `%s' but zero-sized\n",
86 filepathname);
87 fclose(file);
88 return FT_THROW(Cannot_Open_Stream);
89 }
90
91 BLI_fseek(file, 0LL, SEEK_SET);
92
93 stream->descriptor.pointer = file;
94 stream->read = ft_ansi_stream_io;
95 stream->close = ft_ansi_stream_close;
96
97 return FT_Err_Ok;
98}
99
100FT_Error FT_New_Face__win32_compat(FT_Library library,
101 const char *pathname,
102 FT_Long face_index,
103 FT_Face *aface)
104{
105 FT_Error err;
106 FT_Open_Args open;
107 FT_Stream stream = static_cast<FT_Stream>(MEM_callocN(sizeof(*stream), __func__));
108
109 open.flags = FT_OPEN_STREAM;
110 open.stream = stream;
111
112 stream->pathname.pointer = (void *)pathname;
113
114 err = FT_Stream_Open__win32_compat(stream, pathname);
115 if (err) {
116 MEM_freeN(stream);
117 return err;
118 }
119
120 err = FT_Open_Face(library, &open, face_index, aface);
121 /* no need to free 'stream', its handled by FT_Open_Face if an error occurs */
122
123 return err;
124}
125
126#endif /* WIN32 */
#define BLI_assert(a)
Definition BLI_assert.h:50
File and directory operations.
FILE * BLI_fopen(const char *filepath, const char *mode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
int BLI_fseek(FILE *stream, int64_t offset, int whence)
Definition storage.cc:188
unsigned char uchar
unsigned long ulong
Read Guarded memory(de)allocation.
FILE * file
int count
void MEM_freeN(void *vmemh)
Definition mallocn.cc:105
void *(* MEM_callocN)(size_t len, const char *str)
Definition mallocn.cc:42