forked from aseprite/laf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stream.cpp
75 lines (59 loc) · 1.64 KB
/
stream.cpp
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
// LAF FreeType Wrapper
// Copyright (c) 2016-2018 David Capello
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
#include "ft/stream.h"
#include "base/file_handle.h"
#include <ft2build.h>
#define STREAM_FILE(stream) ((FILE*)stream->descriptor.pointer)
namespace ft {
static void ft_stream_close(FT_Stream stream)
{
fclose(STREAM_FILE(stream));
free(stream);
}
static unsigned long ft_stream_io(FT_Stream stream,
unsigned long offset,
unsigned char* buffer,
unsigned long count)
{
if (!count && offset > stream->size)
return 1;
FILE* file = STREAM_FILE(stream);
if (stream->pos != offset)
fseek(file, (long)offset, SEEK_SET);
return (unsigned long)fread(buffer, 1, count, file);
}
FT_Stream open_stream(const std::string& utf8Filename)
{
FT_Stream stream = nullptr;
stream = (FT_Stream)malloc(sizeof(*stream));
if(!stream)
return nullptr;
memset(stream, 0, sizeof(*stream));
TRACE("FT: Loading font %s... ", utf8Filename.c_str());
FILE* file = base::open_file_raw(utf8Filename, "rb");
if (!file) {
free(stream);
TRACE("FAIL\n");
return nullptr;
}
fseek(file, 0, SEEK_END);
stream->size = (unsigned long)ftell(file);
if (!stream->size) {
fclose(file);
free(stream);
TRACE("FAIL\n");
return nullptr;
}
fseek(file, 0, SEEK_SET);
stream->descriptor.pointer = file;
stream->base = nullptr;
stream->pos = 0;
stream->read = ft_stream_io;
stream->close = ft_stream_close;
TRACE("OK\n");
return stream;
}
} // namespace ft