forked from OpenXcom/OpenXcom
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFont.cpp
More file actions
150 lines (139 loc) · 3.59 KB
/
Copy pathFont.cpp
File metadata and controls
150 lines (139 loc) · 3.59 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
/*
* Copyright 2010 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 "Surface.h"
namespace OpenXcom
{
// Fonts start with this character
#define FIRST_CHAR '!'
/**
* 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 nchar Number of characters the font contains.
* @param spacing Horizontal spacing between each character.
*/
Font::Font(int width, int height, int nchar, int spacing) : _width(width), _height(height), _nchar(nchar), _chars(), _spacing(spacing)
{
_surface = new Surface(width, height*nchar);
}
/**
* 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 char i = FIRST_CHAR; i < FIRST_CHAR + _nchar; i++)
{
SDL_Rect rect;
int left = -1, right = -1;
for (int x = 0; x < _width; x++)
{
for (int y = (i - FIRST_CHAR) * _height; (y < (i + 1 - FIRST_CHAR) * _height) && (left == -1); y++)
{
Uint8 pixel = _surface->getPixel(x, y);
if (pixel != 0)
{
left = x;
}
}
}
for (int x = _width - 1; x >= 0; x--)
{
for (int y = (i + 1 - FIRST_CHAR) * _height - 1; (y >= (i - FIRST_CHAR) * _height) && (right == -1); y--)
{
Uint8 pixel = _surface->getPixel(x, y);
if (pixel != 0)
{
right = x;
}
}
}
rect.x = left;
rect.y = (i - FIRST_CHAR) * _height;
rect.w = right - left + 1;
rect.h = _height;
_chars[i] = rect;
}
_surface->unlock();
}
/**
* 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 *const 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 *const Font::getSurface() const
{
return _surface;
}
}