This repository has been archived by the owner on Jan 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
text.c
272 lines (246 loc) · 8.09 KB
/
text.c
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
/*
* Hocoslamfy, text rendering code file
* Copyright (C) 2013 Nebuleon Fumika <nebuleon@gcw-zero.com>
*
* 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 <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
#include "text.h"
#include "unifont.h"
static uint32_t CutString(const char* String, const uint32_t MaxWidth,
struct StringCut* Cuts, uint32_t CutsAllocated)
{
uint32_t Cut = 0;
uint32_t CutStart = 0, Cur = 0, CutWidth = 0;
uint32_t LastSpace = -1;
bool SpaceInCut = false;
while (String[Cur] != '\0')
{
if (String[Cur] != '\n')
{
if (String[Cur] == ' ')
{
LastSpace = Cur;
SpaceInCut = true;
}
CutWidth += _font_width[(uint8_t) String[Cur]];
}
if (String[Cur] == '\n' || CutWidth > MaxWidth)
{
if (Cut < CutsAllocated)
Cuts[Cut].Start = CutStart;
if (String[Cur] == '\n')
{
if (Cut < CutsAllocated)
Cuts[Cut].End = Cur;
}
else if (CutWidth > MaxWidth)
{
if (SpaceInCut)
{
if (Cut < CutsAllocated)
Cuts[Cut].End = LastSpace;
Cur = LastSpace;
}
else
{
if (Cut < CutsAllocated)
Cuts[Cut].End = Cur;
Cur--; // Next iteration redoes this character
}
}
CutStart = Cur + 1;
CutWidth = 0;
SpaceInCut = false;
Cut++;
}
Cur++;
}
if (Cut < CutsAllocated)
{
Cuts[Cut].Start = CutStart;
Cuts[Cut].End = Cur;
}
return Cut + 1;
}
uint32_t GetSectionRenderedWidth(const char* String, const uint32_t Start, const uint32_t End)
{
uint32_t Result = 0, i;
for (i = Start; i < End; i++)
Result += _font_width[(uint8_t) String[i]];
return Result;
}
void PrintString16(const char* String, uint16_t TextColor,
void* Dest, uint32_t DestPitch, uint32_t X, uint32_t Y, uint32_t Width, uint32_t Height,
enum HorizontalAlignment HorizontalAlignment, enum VerticalAlignment VerticalAlignment)
{
struct StringCut* Cuts = malloc((Height / _font_height) * sizeof(struct StringCut));
uint32_t CutCount = CutString(String, Width, Cuts, Height / _font_height), Cut;
if (CutCount > Height / _font_height)
CutCount = Height / _font_height;
for (Cut = 0; Cut < CutCount; Cut++)
{
uint32_t TextWidth = GetSectionRenderedWidth(String, Cuts[Cut].Start, Cuts[Cut].End);
uint32_t LineX, LineY;
switch (HorizontalAlignment)
{
case LEFT: LineX = X; break;
case CENTER: LineX = X + (Width - TextWidth) / 2; break;
case RIGHT: LineX = (X + Width) - TextWidth; break;
default: LineX = 0; /* shouldn't happen */ break;
}
switch (VerticalAlignment)
{
case TOP:
LineY = Y + Cut * _font_height;
break;
case MIDDLE:
LineY = Y + (Height - CutCount * _font_height) / 2 + Cut * _font_height;
break;
case BOTTOM:
LineY = (Y + Height) - (CutCount - Cut) * _font_height;
break;
default:
LineY = 0; /* shouldn't happen */
break;
}
uint32_t Cur;
for (Cur = Cuts[Cut].Start; Cur < Cuts[Cut].End; Cur++)
{
uint32_t glyph_offset = (uint32_t) String[Cur] * _font_height;
uint32_t glyph_width = _font_width[(uint8_t) String[Cur]];
uint32_t glyph_column, glyph_row;
uint16_t current_halfword;
for(glyph_row = 0; glyph_row < _font_height; glyph_row++, glyph_offset++)
{
current_halfword = _font_bits[glyph_offset];
for (glyph_column = 0; glyph_column < glyph_width; glyph_column++)
{
if ((current_halfword >> (15 - glyph_column)) & 0x01)
*(uint16_t*) ((uint8_t*) Dest + (LineY + glyph_row) * DestPitch + (LineX + glyph_column) * sizeof(uint16_t)) = TextColor;
}
}
LineX += glyph_width;
}
}
free(Cuts);
}
void PrintString32(const char* String, uint32_t TextColor,
void* Dest, uint32_t DestPitch, uint32_t X, uint32_t Y, uint32_t Width, uint32_t Height,
enum HorizontalAlignment HorizontalAlignment, enum VerticalAlignment VerticalAlignment)
{
struct StringCut* Cuts = malloc((Height / _font_height) * sizeof(struct StringCut));
uint32_t CutCount = CutString(String, Width, Cuts, Height / _font_height), Cut;
if (CutCount > Height / _font_height)
CutCount = Height / _font_height;
for (Cut = 0; Cut < CutCount; Cut++)
{
uint32_t TextWidth = GetSectionRenderedWidth(String, Cuts[Cut].Start, Cuts[Cut].End);
uint32_t LineX, LineY;
switch (HorizontalAlignment)
{
case LEFT: LineX = X; break;
case CENTER: LineX = X + (Width - TextWidth) / 2; break;
case RIGHT: LineX = (X + Width) - TextWidth; break;
default: LineX = 0; /* shouldn't happen */ break;
}
switch (VerticalAlignment)
{
case TOP:
LineY = Y + Cut * _font_height;
break;
case MIDDLE:
LineY = Y + (Height - CutCount * _font_height) / 2 + Cut * _font_height;
break;
case BOTTOM:
LineY = (Y + Height) - (CutCount - Cut) * _font_height;
break;
default:
LineY = 0; /* shouldn't happen */
break;
}
uint32_t Cur;
for (Cur = Cuts[Cut].Start; Cur < Cuts[Cut].End; Cur++)
{
uint32_t glyph_offset = (uint32_t) String[Cur] * _font_height;
uint32_t glyph_width = _font_width[(uint8_t) String[Cur]];
uint32_t glyph_column, glyph_row;
uint16_t current_halfword;
for(glyph_row = 0; glyph_row < _font_height; glyph_row++, glyph_offset++)
{
current_halfword = _font_bits[glyph_offset];
for (glyph_column = 0; glyph_column < glyph_width; glyph_column++)
{
if ((current_halfword >> (15 - glyph_column)) & 0x01)
*(uint32_t*) ((uint8_t*) Dest + (LineY + glyph_row) * DestPitch + (LineX + glyph_column) * sizeof(uint32_t)) = TextColor;
}
}
LineX += glyph_width;
}
}
free(Cuts);
}
uint32_t GetRenderedWidth(const char* str)
{
struct StringCut* Cuts = malloc(sizeof(struct StringCut));
uint32_t CutCount = CutString(str, UINT32_MAX, Cuts, 1);
if (CutCount > 1)
{
Cuts = realloc(Cuts, CutCount * sizeof(struct StringCut));
CutString(str, UINT32_MAX, Cuts, CutCount);
}
uint32_t Result = 0, LineWidth, Cut;
for (Cut = 0; Cut < CutCount; Cut++)
{
LineWidth = 0;
uint32_t Cur;
for (Cur = Cuts[Cut].Start; Cur < Cuts[Cut].End; Cur++)
{
LineWidth += _font_width[(uint8_t) str[Cur]];
}
if (LineWidth > Result)
Result = LineWidth;
}
free(Cuts);
return Result;
}
uint32_t GetRenderedHeight(const char* str)
{
return CutString(str, UINT32_MAX, NULL, 0) * _font_height;
}
void PrintStringOutline16(const char* String, uint16_t TextColor, uint16_t OutlineColor,
void* Dest, uint32_t DestPitch, uint32_t X, uint32_t Y, uint32_t Width, uint32_t Height,
enum HorizontalAlignment HorizontalAlignment, enum VerticalAlignment VerticalAlignment)
{
uint32_t sx, sy;
for (sx = 0; sx <= 2; sx++)
for (sy = 0; sy <= 2; sy++)
if (!(sx == 1 && sy == 1))
PrintString16(String, OutlineColor, Dest, DestPitch, X + sx, Y + sy, Width - 2, Height - 2, HorizontalAlignment, VerticalAlignment);
PrintString16(String, TextColor, Dest, DestPitch, X + 1, Y + 1, Width - 2, Height - 2, HorizontalAlignment, VerticalAlignment);
}
void PrintStringOutline32(const char* String, uint32_t TextColor, uint32_t OutlineColor,
void* Dest, uint32_t DestPitch, uint32_t X, uint32_t Y, uint32_t Width, uint32_t Height,
enum HorizontalAlignment HorizontalAlignment, enum VerticalAlignment VerticalAlignment)
{
uint32_t sx, sy;
for (sx = 0; sx <= 2; sx++)
for (sy = 0; sy <= 2; sy++)
if (!(sx == 1 && sy == 1))
PrintString32(String, OutlineColor, Dest, DestPitch, X + sx, Y + sy, Width - 2, Height - 2, HorizontalAlignment, VerticalAlignment);
PrintString32(String, TextColor, Dest, DestPitch, X + 1, Y + 1, Width - 2, Height - 2, HorizontalAlignment, VerticalAlignment);
}