forked from tSoniq/neofonteditor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNeoCharacter.cc
323 lines (272 loc) · 7.57 KB
/
NeoCharacter.cc
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/** @file NeoCharacter.cc
* @brief Neo font character class definition.
* @copyright (c) 2006 Alquanto. All Rights Reserved.
*/
#include <stdint.h>
#include <string.h>
#include "NeoCharacter.h"
/** Helper macto used to translate (x,y) coordinates to a byte index.
*
* @param x Pixel x-coordinate.
* @param y Pixel y-coordinate.
* @return The byte index.
*/
#define XY_TO_BYTE(x, y) (((x) + ((y) * kNeoCharacterMaxWidth)) / 8)
/** Helper macro used to translate (x,y) coordinates to a bit index within a byte.
*
* @param x Pixel x-coordinate.
* @param y Pixel y-coordinate.
* @return The bit index.
*/
#if (0 == (kNeoCharacterMaxWidth & 7))
#define XY_TO_BIT(x, y) ((x) & 7)
#else
#define XY_TO_BIT(x, y) (((x) + ((y) * kNeoCharacterMaxWidth)) & 7)
#endif
/** Class constructor.
*/
NeoCharacter::NeoCharacter()
:
m_width(8),
m_height(8),
m_bitmap()
{
clear();
}
/** Copy constructor.
*/
NeoCharacter::NeoCharacter(const NeoCharacter &other)
:
m_width(other.m_width),
m_height(other.m_height),
m_bitmap()
{
memcpy(m_bitmap, other.m_bitmap, sizeof m_bitmap);
}
/** Destructor.
*/
NeoCharacter::~NeoCharacter()
{
// Nothing.
}
/** Obtain the width of a character.
*
* @return The width of the character, in pixels.
*/
int NeoCharacter::width() const
{
return m_width;
}
/** Obtain the height of a character.
*
* @return The height of the character, in pixels.
*/
int NeoCharacter::height() const
{
return m_height;
}
/** Set the width of a character.
*
* @param w The new width, in pixels.
* @return The actual width used.
*/
int NeoCharacter::setWidth(int w)
{
if (w > kNeoCharacterMaxWidth) w = kNeoCharacterMaxWidth;
if (w < kNeoCharacterMinWidth) w = kNeoCharacterMinWidth;
m_width = w;
return m_width;
}
/** Set the height of a character.
*
* @param h The new height, in pixels.
* @return The actual height used.
*/
int NeoCharacter::setHeight(int h)
{
if (h > kNeoCharacterMaxHeight) h = kNeoCharacterMaxHeight;
if (h < kNeoCharacterMinHeight) h = kNeoCharacterMinHeight;
if (h > m_height)
{
for (int y = m_height; y < h; y++)
{
for (int x = 0; x < m_width; x++)
{
// Clear newly expanded rows
m_bitmap[XY_TO_BYTE(x,y)] &= ~(1u << XY_TO_BIT(x,y));
}
}
}
m_height = h;
return m_height;
}
/** Clear all pixels in the character.
*/
void NeoCharacter::clear()
{
for (unsigned int i = 0; i < sizeof m_bitmap; i++)
{
m_bitmap[i] = 0;
}
}
/** Read a pixel.
*
* @param x Horizontal coordinate. Zero denotes the left-hand edge, getWidth() - 1 denotes the right-hand edge.
* @param y Vertical coordinate. Zero denotes the upper-edge, getHeight() - 1 denotes the lower-edge.
* @return Zero if the pixel is clear, one if it is set.
*/
int NeoCharacter::getPixel(int x, int y) const
{
if (x < 0 || x >= m_width || y < 0 || y >= m_height) return 0;
else return (m_bitmap[XY_TO_BYTE(x,y)] >> XY_TO_BIT(x,y)) & 1;
}
/** Set a pixel.
*
* @param x Horizontal coordinate. Zero denotes the left-hand edge, getWidth() - 1 denotes the right-hand edge.
* @param y Vertical coordinate. Zero denotes the upper-edge, getHeight() - 1 denotes the lower-edge.
*/
void NeoCharacter::setPixel(int x, int y)
{
if (x >= 0 && x < m_width && y >= 0 && y < m_height)
{
m_bitmap[XY_TO_BYTE(x,y)] |= (1u << XY_TO_BIT(x,y));
}
}
/** Clear a pixel.
*
* @param x Horizontal coordinate. Zero denotes the left-hand edge, getWidth() - 1 denotes the right-hand edge.
* @param y Vertical coordinate. Zero denotes the upper-edge, getHeight() - 1 denotes the lower-edge.
*/
void NeoCharacter::clearPixel(int x, int y)
{
if (x >= 0 && x < m_width && y >= 0 && y < m_height)
{
m_bitmap[XY_TO_BYTE(x,y)] &= ~(1u << XY_TO_BIT(x,y));
}
}
/** Flip a pixel. If the pixel was set, it is cleared. If the pixel was clear, it is set.
*
* @param x Horizontal coordinate. Zero denotes the left-hand edge, getWidth() - 1 denotes the right-hand edge.
* @param y Vertical coordinate. Zero denotes the upper-edge, getHeight() - 1 denotes the lower-edge.
* @return Logical true if the pixel is left set, false if it is left clear.
*/
void NeoCharacter::flipPixel(int x, int y)
{
if (x >= 0 && x < m_width && y >= 0 && y < m_height)
{
m_bitmap[XY_TO_BYTE(x,y)] = m_bitmap[XY_TO_BYTE(x,y)] ^ (1u << XY_TO_BIT(x,y));
}
}
/** Set the value of a pixel at the specified coordinates.
*
* @param x The x-coordinate (origin is left).
* @param y The y-coordinate (origin is top).
* @param v The value to set. Use +1 to set a pixel, 0 to clear it, -1 to flip it.
* @return The final pixel state. 1 for a set pixel, zero for a clear one.
*/
void NeoCharacter::changePixel(int x, int y, int v)
{
if (x >= 0 && x < m_width && y >= 0 && y < m_height)
{
if (v > 0)
{
setPixel(x, y);
}
else if (v == 0)
{
clearPixel(x, y);
}
else
{
flipPixel(x, y);
}
}
}
/** Translate the character.
*
* @param dx The x-displacement (positive => right, negative => left).
* @param dy The y-displacement (positive => down, negative => up).
*/
void NeoCharacter::transformTranslate(int dx, int dy)
{
if (dx < 0) dx = m_width - ((-dx) % m_width);
if (dy < 0) dy = m_height - ((-dy) % m_height);
NeoCharacter temp(*this);
clear();
for (int x = 0; x < m_width; x++)
{
for (int y = 0; y < m_height; y++)
{
if (temp.getPixel(x,y)) setPixel((x+dx) % m_width, (y+dy) % m_height);
}
}
}
/** Reflect the character vertically.
*/
void NeoCharacter::transformFlipV()
{
NeoCharacter temp(*this);
clear();
for (int x = 0; x < m_width; x++)
{
for (int y = 0; y < m_height; y++)
{
if (temp.getPixel(x,y)) setPixel(x, m_height - y - 1);
}
}
}
/** Reflect the character horizontally.
*/
void NeoCharacter::transformFlipH()
{
NeoCharacter temp(*this);
clear();
for (int x = 0; x < m_width; x++)
{
for (int y = 0; y < m_height; y++)
{
if (temp.getPixel(x,y)) setPixel(m_width - x - 1, y);
}
}
}
/** Make the character bolder by smearing pixels to the right. The character width is also increased.
*/
void NeoCharacter::transformBold()
{
setWidth(m_width + 1);
for (int y = 0; y < m_height; y++)
{
clearPixel(m_width - 1, y);
}
for (int x = m_width - 2; x >= 0; x--)
{
for (int y = 0; y < m_height; y++)
{
if (getPixel(x,y)) setPixel(x+1, y);
}
}
}
/** Return the size of the archive data.
*
* @return The number of bytes needed for an archive.
*/
unsigned int NeoCharacter::archiveSize() const
{
return sizeof *this;
}
/** Save the character object to a byte array.
*
* @return A pointer to an array of bytes to receive the archive of at least archiveSize() bytes.
*/
void NeoCharacter::saveArchive(uint8_t *data) const
{
memcpy(data, (void*)this, sizeof *this);
}
/** Load the character object from a byte array.
*
* @param data The data to load. This must contain archiveSize() bytes.
*/
void NeoCharacter::loadArchive(const uint8_t *data)
{
memcpy((void*)this, data, sizeof *this);
}