Blender  V2.93
blf_font_win32_compat.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
25 #ifdef WIN32
26 
27 # include <stdio.h>
28 
29 # include <ft2build.h>
30 # include FT_FREETYPE_H
31 
32 # include "MEM_guardedalloc.h"
33 
34 # include "BLI_fileops.h"
35 # include "BLI_utildefines.h"
36 
37 # include "blf_internal.h"
38 
39 /* internal freetype defines */
40 # define STREAM_FILE(stream) ((FILE *)stream->descriptor.pointer)
41 # define FT_THROW(e) -1
42 
43 static void ft_ansi_stream_close(FT_Stream stream)
44 {
45  fclose(STREAM_FILE(stream));
46 
47  stream->descriptor.pointer = NULL;
48  stream->size = 0;
49  stream->base = 0;
50 
51  /* WARNING: this works but be careful!
52  * Checked freetype sources, there isn't any access after closing. */
53  MEM_freeN(stream);
54 }
55 
56 static unsigned long ft_ansi_stream_io(FT_Stream stream,
57  unsigned long offset,
58  unsigned char *buffer,
59  unsigned long count)
60 {
61  FILE *file;
62  if (!count && offset > stream->size) {
63  return 1;
64  }
65 
66  file = STREAM_FILE(stream);
67 
68  if (stream->pos != offset) {
69  fseek(file, offset, SEEK_SET);
70  }
71 
72  return fread(buffer, 1, count, file);
73 }
74 
75 static FT_Error FT_Stream_Open__win32_compat(FT_Stream stream, const char *filepathname)
76 {
77  FILE *file;
78  BLI_assert(stream);
79 
80  stream->descriptor.pointer = NULL;
81  stream->pathname.pointer = (char *)filepathname;
82  stream->base = 0;
83  stream->pos = 0;
84  stream->read = NULL;
85  stream->close = NULL;
86 
87  file = BLI_fopen(filepathname, "rb");
88  if (!file) {
89  fprintf(stderr,
90  "FT_Stream_Open: "
91  "could not open `%s'\n",
92  filepathname);
93  return FT_THROW(Cannot_Open_Resource);
94  }
95 
96  fseek(file, 0, SEEK_END);
97  stream->size = ftell(file);
98  if (!stream->size) {
99  fprintf(stderr,
100  "FT_Stream_Open: "
101  "opened `%s' but zero-sized\n",
102  filepathname);
103  fclose(file);
104  return FT_THROW(Cannot_Open_Stream);
105  }
106 
107  fseek(file, 0, SEEK_SET);
108 
109  stream->descriptor.pointer = file;
110  stream->read = ft_ansi_stream_io;
111  stream->close = ft_ansi_stream_close;
112 
113  return FT_Err_Ok;
114 }
115 
116 FT_Error FT_New_Face__win32_compat(FT_Library library,
117  const char *pathname,
118  FT_Long face_index,
119  FT_Face *aface)
120 {
121  FT_Error err;
122  FT_Open_Args open;
123  FT_Stream stream = NULL;
124  stream = MEM_callocN(sizeof(*stream), __func__);
125 
126  open.flags = FT_OPEN_STREAM;
127  open.stream = stream;
128  stream->pathname.pointer = (char *)pathname;
129 
130  err = FT_Stream_Open__win32_compat(stream, pathname);
131  if (err) {
132  MEM_freeN(stream);
133  return err;
134  }
135 
136  err = FT_Open_Face(library, &open, face_index, aface);
137  /* no need to free 'stream', its handled by FT_Open_Face if an error occurs */
138 
139  return err;
140 }
141 
142 #endif /* WIN32 */
#define BLI_assert(a)
Definition: BLI_assert.h:58
File and directory operations.
FILE * BLI_fopen(const char *filename, const char *mode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition: fileops.c:1003
Read Guarded memory(de)allocation.
FILE * file
static FT_Library library
Definition: freetypefont.c:51
static FT_Error err
Definition: freetypefont.c:52
int count
__kernel void ccl_constant KernelData ccl_global void ccl_global char ccl_global int ccl_global char ccl_global unsigned int ccl_global float * buffer
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:41
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:45