Repository navigation
Expand file tree
/
Copy pathFont.cpp
More file actions
185 lines (169 loc) · 4.13 KB
/
Copy pathFont.cpp
File metadata and controls
185 lines (169 loc) · 4.13 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
* Copyright 2010-2013 OpenXcom Developers.
*
* This file is part of OpenXcom.
*
* OpenXcom 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 3 of the License, or
* (at your option) any later version.
*
* OpenXcom 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 OpenXcom. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Font.h"
#include <fstream>
#include <iostream>
#include "Exception.h"
#include "Surface.h"
#include "Language.h"
namespace OpenXcom
{
std::wstring Font::_index = L"";
/**
* Initializes the font with a blank surface sized big enough to
* contain all the characters.
* @param width Width in pixels of each character.
* @param height Height in pixels of each character.
* @param spacing Horizontal spacing between each character.
*/
Font::Font(int width, int height, int spacing) : _width(width), _height(height), _chars(), _spacing(spacing)
{
_surface = new Surface(width, height * _index.length());
}
/**
* Deletes the font's surface.
*/
Font::~Font()
{
delete _surface;
}
/**
* Calculates the real size and position of each character in
* the surface and stores them in SDL_Rect's for future use
* by other classes.
*/
void Font::load()
{
_surface->lock();
for (unsigned int i = 0; i < _index.length(); ++i)
{
SDL_Rect rect;
int left = -1, right = -1;
for (int x = 0; x < _width; ++x)
{
for (unsigned int y = i * _height; y < (i + 1) * _height && left == -1; ++y)
{
Uint8 pixel = _surface->getPixel(x, y);
if (pixel != 0)
{
left = x;
}
}
}
for (int x = _width - 1; x >= 0; --x)
{
for (unsigned int y = (i + 1) * _height; y-- != i * _height && right == -1;)
{
Uint8 pixel = _surface->getPixel(x, y);
if (pixel != 0)
{
right = x;
}
}
}
rect.x = left;
rect.y = i * _height;
rect.w = right - left + 1;
rect.h = _height;
_chars[_index[i]] = rect;
}
_surface->unlock();
}
/**
* Loads the characters contained in each font
* from a UTF-8 file to use as the index.
* @param filename Filename of the index file.
*/
void Font::loadIndex(const std::string &filename)
{
_index = L"";
std::ifstream txtFile (filename.c_str(), std::ios::in | std::ios::binary);
if (!txtFile)
{
throw Exception(filename + " not found");
}
char value;
std::string buffer;
while (txtFile.read(&value, 1))
{
buffer += value;
}
if (!txtFile.eof())
{
throw Exception("Invalid font index");
}
_index = Language::utf8ToWstr(buffer);
if (sizeof(_index[0]) == 1)
_index = _index.substr(0, 256);
txtFile.close();
}
/**
* Returns a particular character from the set stored in the font.
* @param c Character to use for size/position.
* @return Pointer to the font's surface with the respective
* cropping rectangle set up.
*/
Surface *Font::getChar(wchar_t c)
{
if (_chars.find(c) == _chars.end())
{
c = '?';
}
_surface->getCrop()->x = _chars[c].x;
_surface->getCrop()->y = _chars[c].y;
_surface->getCrop()->w = _chars[c].w;
_surface->getCrop()->h = _chars[c].h;
return _surface;
}
/**
* Returns the maximum width for any character in the font.
* @return Width in pixels.
*/
int Font::getWidth() const
{
return _width;
}
/**
* Returns the maximum height for any character in the font.
* @return Height in pixels.
*/
int Font::getHeight() const
{
return _height;
}
/**
* Returns the horizontal spacing for any character in the font.
* @return Spacing in pixels.
* @note This does not refer to character spacing within the surface,
* but to the spacing used when drawing a series of characters.
*/
int Font::getSpacing() const
{
return _spacing;
}
/**
* Returns the surface stored within the font. Used for loading the
* actual graphic into the font.
* @return Pointer to the internal surface.
*/
Surface *Font::getSurface() const
{
return _surface;
}
}