[1c3b2d]: gemrb / plugins / TTFImporter / TTFFontManager.cpp Maximize Restore History

Download this file

TTFFontManager.cpp    131 lines (112 with data), 3.0 kB

  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
126
127
128
129
130
/* GemRB - Infinity Engine Emulator
* Copyright (C) 2011 The GemRB Project
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*
*/
#include "Interface.h"
#include "Palette.h"
#include "TTFFont.h"
#include "TTFFontManager.h"
using namespace GemRB;
FT_Library library = NULL;
void loadFT()
{
FT_Error error = FT_Init_FreeType( &library );
if ( error ) {
LogFTError(error);
}
}
void destroyFT()
{
if (library) {
FT_Done_FreeType( library );
}
}
unsigned long TTFFontManager::read(FT_Stream stream,
unsigned long offset,
unsigned char* buffer,
unsigned long count )
{
DataStream* dstream = (DataStream*)stream->descriptor.pointer;
dstream->Seek(offset, GEM_STREAM_START);
return dstream->Read(buffer, count);
}
void TTFFontManager::close( FT_Stream stream )
{
if (stream)
free(stream);
}
TTFFontManager::~TTFFontManager(void)
{
if (face) {
FT_Done_Face(face);
}
}
TTFFontManager::TTFFontManager(void)
: ftStream(NULL), face(NULL)
{}
bool TTFFontManager::Open(DataStream* stream)
{
Close();
if (stream) {
FT_Error error;
ftStream = (FT_Stream)calloc(sizeof(*ftStream), 1);
ftStream->read = read;
ftStream->close = close;
ftStream->descriptor.pointer = stream;
ftStream->pos = stream->GetPos();
ftStream->size = stream->Size();
FT_Open_Args args = FT_Open_Args();
args.flags = FT_OPEN_STREAM;
args.stream = ftStream;
error = FT_Open_Face( library, &args, 0, &face );
if( error ) {
LogFTError(error);
Close();
return false;
}
// we always convert to UTF-16
// TODO: maybe we should allow an override encoding?
FT_Select_Charmap(face, FT_ENCODING_UNICODE);
return true;
}
return false;
}
void TTFFontManager::Close()
{
if (face) {
FT_Done_Face(face);
}
close(ftStream);
}
Font* TTFFontManager::GetFont(unsigned short ptSize,
FontStyle style, Palette* pal)
{
if (!pal) {
Color fore = {0xFF, 0xFF, 0xFF, 0}; //white
Color back = {0x00, 0x00, 0x00, 0}; //black
pal = core->CreatePalette( fore, back );
pal->CreateShadedAlphaChannel();
}
return new TTFFont(face, ptSize, style, pal);
}
#include "plugindef.h"
GEMRB_PLUGIN(0x3AD6427C, "TTF Font Importer")
PLUGIN_RESOURCE(TTFFontManager, "ttf")
PLUGIN_INITIALIZER(loadFT)
PLUGIN_CLEANUP(destroyFT)
END_PLUGIN()