Welcome to mirror list, hosted at ThFree Co, Russian Federation.

blf_font_win32_compat.c « intern « blenfont « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4bb2a010dd9812af94c23b46f3dd1ceeb86f880f (plain)
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/* SPDX-License-Identifier: GPL-2.0-or-later */

/** \file
 * \ingroup blf
 *
 * Workaround for win32 which needs to use BLI_fopen to access files.
 *
 * defines #FT_New_Face__win32_compat, a drop-in replacement for \a #FT_New_Face.
 */

#ifdef WIN32

#  include <stdio.h>

#  include <ft2build.h>
#  include FT_FREETYPE_H

#  include "MEM_guardedalloc.h"

#  include "BLI_fileops.h"
#  include "BLI_utildefines.h"

#  include "blf_internal.h"

/* internal freetype defines */
#  define STREAM_FILE(stream) ((FILE *)stream->descriptor.pointer)
#  define FT_THROW(e) -1

static void ft_ansi_stream_close(FT_Stream stream)
{
  fclose(STREAM_FILE(stream));

  stream->descriptor.pointer = NULL;
  stream->size = 0;
  stream->base = 0;

  /* WARNING: this works but be careful!
   * Checked freetype sources, there isn't any access after closing. */
  MEM_freeN(stream);
}

static ulong ft_ansi_stream_io(FT_Stream stream, ulong offset, uchar *buffer, ulong count)
{
  FILE *file;
  if (!count && offset > stream->size) {
    return 1;
  }

  file = STREAM_FILE(stream);

  if (stream->pos != offset) {
    BLI_fseek(file, offset, SEEK_SET);
  }

  return fread(buffer, 1, count, file);
}

static FT_Error FT_Stream_Open__win32_compat(FT_Stream stream, const char *filepathname)
{
  FILE *file;
  BLI_assert(stream);

  stream->descriptor.pointer = NULL;
  stream->pathname.pointer = (char *)filepathname;
  stream->base = 0;
  stream->pos = 0;
  stream->read = NULL;
  stream->close = NULL;

  file = BLI_fopen(filepathname, "rb");
  if (!file) {
    fprintf(stderr,
            "FT_Stream_Open: "
            "could not open `%s'\n",
            filepathname);
    return FT_THROW(Cannot_Open_Resource);
  }

  BLI_fseek(file, 0LL, SEEK_END);
  stream->size = ftell(file);
  if (!stream->size) {
    fprintf(stderr,
            "FT_Stream_Open: "
            "opened `%s' but zero-sized\n",
            filepathname);
    fclose(file);
    return FT_THROW(Cannot_Open_Stream);
  }

  BLI_fseek(file, 0LL, SEEK_SET);

  stream->descriptor.pointer = file;
  stream->read = ft_ansi_stream_io;
  stream->close = ft_ansi_stream_close;

  return FT_Err_Ok;
}

FT_Error FT_New_Face__win32_compat(FT_Library library,
                                   const char *pathname,
                                   FT_Long face_index,
                                   FT_Face *aface)
{
  FT_Error err;
  FT_Open_Args open;
  FT_Stream stream = NULL;
  stream = MEM_callocN(sizeof(*stream), __func__);

  open.flags = FT_OPEN_STREAM;
  open.stream = stream;
  stream->pathname.pointer = (char *)pathname;

  err = FT_Stream_Open__win32_compat(stream, pathname);
  if (err) {
    MEM_freeN(stream);
    return err;
  }

  err = FT_Open_Face(library, &open, face_index, aface);
  /* no need to free 'stream', its handled by FT_Open_Face if an error occurs */

  return err;
}

#endif /* WIN32 */