Blender  V3.3
blf_font_win32_compat.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
11 #ifdef WIN32
12 
13 # include <stdio.h>
14 
15 # include <ft2build.h>
16 # include FT_FREETYPE_H
17 
18 # include "MEM_guardedalloc.h"
19 
20 # include "BLI_fileops.h"
21 # include "BLI_utildefines.h"
22 
23 # include "blf_internal.h"
24 
25 /* internal freetype defines */
26 # define STREAM_FILE(stream) ((FILE *)stream->descriptor.pointer)
27 # define FT_THROW(e) -1
28 
29 static void ft_ansi_stream_close(FT_Stream stream)
30 {
31  fclose(STREAM_FILE(stream));
32 
33  stream->descriptor.pointer = NULL;
34  stream->size = 0;
35  stream->base = 0;
36 
37  /* WARNING: this works but be careful!
38  * Checked freetype sources, there isn't any access after closing. */
39  MEM_freeN(stream);
40 }
41 
42 static unsigned long ft_ansi_stream_io(FT_Stream stream,
43  unsigned long offset,
44  unsigned char *buffer,
45  unsigned long count)
46 {
47  FILE *file;
48  if (!count && offset > stream->size) {
49  return 1;
50  }
51 
52  file = STREAM_FILE(stream);
53 
54  if (stream->pos != offset) {
55  BLI_fseek(file, offset, SEEK_SET);
56  }
57 
58  return fread(buffer, 1, count, file);
59 }
60 
61 static FT_Error FT_Stream_Open__win32_compat(FT_Stream stream, const char *filepathname)
62 {
63  FILE *file;
64  BLI_assert(stream);
65 
66  stream->descriptor.pointer = NULL;
67  stream->pathname.pointer = (char *)filepathname;
68  stream->base = 0;
69  stream->pos = 0;
70  stream->read = NULL;
71  stream->close = NULL;
72 
73  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 
102 FT_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 = NULL;
110  stream = MEM_callocN(sizeof(*stream), __func__);
111 
112  open.flags = FT_OPEN_STREAM;
113  open.stream = stream;
114  stream->pathname.pointer = (char *)pathname;
115 
116  err = FT_Stream_Open__win32_compat(stream, pathname);
117  if (err) {
118  MEM_freeN(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 */
#define BLI_assert(a)
Definition: BLI_assert.h:46
File and directory operations.
FILE * BLI_fopen(const char *filepath, const char *mode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition: fileops.c:906
int BLI_fseek(FILE *stream, int64_t offset, int whence)
Definition: storage.c:160
Read Guarded memory(de)allocation.
FILE * file
int count
ccl_global float * buffer
ccl_gpu_kernel_postfix ccl_global float int int int int float bool int offset
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:27
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:31
static FT_Library library
static FT_Error err