Blender V5.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
12
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.hh"
23# include "BLI_utildefines.hh"
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
31using namespace blender;
32
33static void ft_ansi_stream_close(FT_Stream stream)
34{
35 fclose(STREAM_FILE(stream));
36
37 stream->descriptor.pointer = nullptr;
38 stream->size = 0;
39 stream->base = 0;
40
41 /* WARNING: this works but be careful!
42 * Checked freetype sources, there isn't any access after closing. */
43 MEM_delete(stream);
44}
45
46static ulong ft_ansi_stream_io(FT_Stream stream, ulong offset, uchar *buffer, ulong count)
47{
48
49 if (!count && offset > stream->size) {
50 return 1;
51 }
52
53 FILE *file = STREAM_FILE(stream);
54
55 if (stream->pos != offset) {
56 BLI_fseek(file, offset, SEEK_SET);
57 }
58
59 return fread(buffer, 1, count, file);
60}
61
62static FT_Error FT_Stream_Open__win32_compat(FT_Stream stream, const char *filepathname)
63{
64 BLI_assert(stream);
65
66 stream->descriptor.pointer = nullptr;
67 stream->pathname.pointer = (char *)filepathname;
68 stream->base = 0;
69 stream->pos = 0;
70 stream->read = nullptr;
71 stream->close = nullptr;
72
73 FILE *file = BLI_fopen(filepathname, "rb");
74 if (!file) {
75 fprintf(stderr,
76 "FT_Stream_Open: "
77 "could not open '%s'\n",
78 filepathname);
79 return FT_THROW(Cannot_Open_Resource);
80 }
81
82 BLI_fseek(file, 0LL, SEEK_END);
83 stream->size = ftell(file);
84 if (!stream->size) {
85 fprintf(stderr,
86 "FT_Stream_Open: "
87 "opened '%s' but zero-sized\n",
88 filepathname);
89 fclose(file);
90 return FT_THROW(Cannot_Open_Stream);
91 }
92
93 BLI_fseek(file, 0LL, SEEK_SET);
94
95 stream->descriptor.pointer = file;
96 stream->read = ft_ansi_stream_io;
97 stream->close = ft_ansi_stream_close;
98
99 return FT_Err_Ok;
100}
101
102FT_Error FT_New_Face__win32_compat(FT_Library library,
103 const char *pathname,
104 FT_Long face_index,
105 FT_Face *aface)
106{
107 FT_Error err;
108 FT_Open_Args open;
109 FT_Stream stream = static_cast<FT_Stream>(MEM_new_zeroed(sizeof(*stream), __func__));
110
111 open.flags = FT_OPEN_STREAM;
112 open.stream = stream;
113
114 stream->pathname.pointer = (void *)pathname;
115
116 err = FT_Stream_Open__win32_compat(stream, pathname);
117 if (err) {
118 MEM_delete(stream);
119 return err;
120 }
121
122 err = FT_Open_Face(library, &open, face_index, aface);
123 /* no need to free 'stream', its handled by FT_Open_Face if an error occurs */
124
125 return err;
126}
127
128#endif /* WIN32 */
File and directory operations.
Read Blender memory allocation and freeing.
#define BLI_assert(a)
unsigned int uchar
void * MEM_new_zeroed(size_t len, const char *str)
Definition mallocn.cc:114
BMesh Iterators.
FILE * BLI_fopen(const char *filepath, const char *mode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
unsigned long ulong
Definition BKE_text.h:57
int BLI_fseek(FILE *stream, int64_t offset, int whence)
Definition storage.cc:201