aboutsummaryrefslogtreecommitdiff
path: root/Source/Font.c
blob: 7fc95db7184208a499110e5aad3faae05c1f54eb (plain) (blame)
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
/* *************************************
 * 	Includes
 * *************************************/

#include "Font.h"
#include "System.h"
#include "Gfx.h"
#include <stdarg.h>

/* *************************************
 * 	Defines
 * *************************************/

#define FONT_INTERNAL_TEXT_BUFFER_MAX_SIZE 200
#define FONT_DEFAULT_CHAR_SIZE 16
#define FONT_DEFAULT_INIT_CHAR '!'

/* *************************************
 * 	Structs and enums
 * *************************************/

enum
{
	// We will use the most significant bit
	// for internal purposes.

	SQUARE_BTN_8BIT = 128,
	CROSS_BTN_8BIT,
	CIRCLE_BTN_8BIT,
	TRIANGLE_BTN_8BIT
};

/* *************************************
 * 	Local Prototypes
 * *************************************/

/* *************************************
 * 	Local Variables
 * *************************************/
TYPE_FONT RadioFont, SmallFont;

/* *************************************
 * 	Local Variables
 * *************************************/

static char _internal_text[FONT_INTERNAL_TEXT_BUFFER_MAX_SIZE];
static volatile unsigned char _blend_effect_lum;

bool FontLoadImage(const char* strPath, TYPE_FONT * ptrFont)
{
	if (GfxSpriteFromFile(strPath, &ptrFont->spr) == false)
	{
		return false;
	}

	ptrFont->spr_w = ptrFont->spr.w;
	ptrFont->spr_h = ptrFont->spr.h;
	ptrFont->spr_u = ptrFont->spr.u;
	ptrFont->spr_v = ptrFont->spr.v;

	//Now set default values to font

	ptrFont->char_w = FONT_DEFAULT_CHAR_SIZE;
	ptrFont->char_h = FONT_DEFAULT_CHAR_SIZE;

	ptrFont->spr.attribute |= COLORMODE(COLORMODE_4BPP);
	ptrFont->spr.attribute &= COLORMODE(~(COLORMODE_8BPP | COLORMODE_16BPP | COLORMODE_24BPP));
	ptrFont->spr.r = NORMAL_LUMINANCE;
	ptrFont->spr.g = NORMAL_LUMINANCE;
	ptrFont->spr.b = NORMAL_LUMINANCE;

	//At this point, spr.w and spr.h = real w/h
	ptrFont->char_per_row = (uint8_t)(ptrFont->spr_w / ptrFont->char_w);
	ptrFont->max_ch_wrap = 0;

	ptrFont->spr.w = ptrFont->char_w;
	ptrFont->spr.h = ptrFont->char_h;

	ptrFont->flags = FONT_NOFLAGS;

	ptrFont->init_ch = FONT_DEFAULT_INIT_CHAR;

	Serial_printf("Sprite CX = %d, sprite CY = %d\n",ptrFont->spr.cx, ptrFont->spr.cy);

	return true;
}

void FontSetInitChar(TYPE_FONT * ptrFont, char c)
{
	ptrFont->init_ch = c;
}

void FontSetFlags(TYPE_FONT * ptrFont, FONT_FLAGS flags)
{
	ptrFont->flags = flags;
}

void FontSetSize(TYPE_FONT * ptrFont, short size)
{
	ptrFont->char_w = size;
	ptrFont->char_h = size;

	//At this point, spr.w and spr.h = real w/h
	ptrFont->char_per_row = (uint8_t)(ptrFont->spr_w / ptrFont->char_w);
	ptrFont->max_ch_wrap = 0;

	ptrFont->spr.w = ptrFont->char_w;
	ptrFont->spr.h = ptrFont->char_h;
}

void FontSetMaxCharPerLine(TYPE_FONT* ptrFont, uint8_t max)
{
	ptrFont->max_ch_wrap = max;
}

void FontSetSpacing(TYPE_FONT* ptrFont, short spacing)
{
    ptrFont->char_spacing = spacing;
}

void FontCyclic(void)
{
	_blend_effect_lum -= 8;
}

void FontPrintText(TYPE_FONT * ptrFont, short x, short y, const char* str, ...)
{
	uint16_t i;
	uint8_t line_count = 0;
	int result;
	short orig_x = x;

	va_list ap;

    // Do not print anything if either 1Hz or 2Hz flags are configured
    // and 1/2Hz signal has not been yet emitted by Gfx.
	if (ptrFont->flags & FONT_1HZ_FLASH)
	{
		if (Gfx1HzFlash() == false)
		{
			return;
		}
	}
	else if (ptrFont->flags & FONT_2HZ_FLASH)
	{
		if (Gfx2HzFlash() == false)
		{
			return;
		}
	}

	va_start(ap, str);

	result = vsnprintf(	_internal_text,
						FONT_INTERNAL_TEXT_BUFFER_MAX_SIZE,
						str,
						ap	);

	for (i = 0; i < result ; i++)
	{
		unsigned char _ch = _internal_text[i];

		if (_ch == '\0')
		{
			// End of string
			break;
		}

		switch(_ch)
		{
			case ' ':
			{
				bool linefeed_needed = false;

				// Check if the next word fits on the same line
				if ( (ptrFont->flags & FONT_WRAP_LINE) && (ptrFont->max_ch_wrap != 0) )
				{
					uint16_t j;
					uint8_t aux_line_count = line_count;

					for (j = i + 1; j < result; j++)
					{
						if (_internal_text[j] != ' ')
						{
							if (++aux_line_count >= ptrFont->max_ch_wrap)
							{
								line_count = 0;
								x = orig_x;
								y += ptrFont->char_h;
								linefeed_needed = true;
								break;
							}
						}
						else
						{
							break;
						}
					}
				}

				if (linefeed_needed == false)
				{
					x += ptrFont->char_w;
				}

				continue;
			}
			case '\n':
				x = orig_x;
				y += ptrFont->char_h;
			break;

			case SQUARE_BTN_8BIT:
				GfxDrawButton(x, y, PAD_SQUARE);

				x += BUTTON_SIZE;

				line_count += BUTTON_SIZE / ptrFont->char_w;
			break;

			case CIRCLE_BTN_8BIT:
				GfxDrawButton(x, y, PAD_CIRCLE);

				x += BUTTON_SIZE;
				line_count += BUTTON_SIZE / ptrFont->char_w;
			break;

			case TRIANGLE_BTN_8BIT:
				GfxDrawButton(x, y, PAD_TRIANGLE);

				x += BUTTON_SIZE;
				line_count += BUTTON_SIZE / ptrFont->char_w;
			break;

			case CROSS_BTN_8BIT:
				GfxDrawButton(x, y, PAD_CROSS);

				x += BUTTON_SIZE;
				line_count += BUTTON_SIZE / ptrFont->char_w;
			break;

			default:

				if ( (ptrFont->flags & FONT_WRAP_LINE) && (ptrFont->max_ch_wrap != 0) )
				{
					if (++line_count >= ptrFont->max_ch_wrap)
					{
						line_count = 0;
						x = orig_x;
						y += ptrFont->char_h;
					}
				}

				ptrFont->spr.x = x;
				ptrFont->spr.y = y;
				ptrFont->spr.w = ptrFont->char_w;
				ptrFont->spr.h = ptrFont->char_h;
				ptrFont->spr.u = (short)( (_ch - ptrFont->init_ch) % ptrFont->char_per_row) * ptrFont->char_w;
				ptrFont->spr.u += ptrFont->spr_u; // Add original offset for image
				ptrFont->spr.v = (short)( (_ch - ptrFont->init_ch) / ptrFont->char_per_row) * ptrFont->char_h;
				ptrFont->spr.v += ptrFont->spr_v; // Add original offset for image

				if (ptrFont->flags & FONT_BLEND_EFFECT)
				{
					ptrFont->spr.r += 8;
					ptrFont->spr.g += 8;
					ptrFont->spr.b += 8;
				}
				else
				{
					ptrFont->spr.r = NORMAL_LUMINANCE;
					ptrFont->spr.g = NORMAL_LUMINANCE;
					ptrFont->spr.b = NORMAL_LUMINANCE;
				}
				/*Serial_printf("char_w = %d, char_h = %d, char_per_row = %d, init_ch: %c\n",
						ptrFont->char_w,
						ptrFont->char_h,
						ptrFont->char_per_row,
						ptrFont->init_ch);
				Serial_printf("Char: %c, spr.u = %d, spr.v = %d\n",str[i],ptrFont->spr.u, ptrFont->spr.v);
				Serial_printf("Sprite CX = %d, sprite CY = %d\n",ptrFont->spr.cx, ptrFont->spr.cy);*/
				//Serial_printf("Sprite rgb={%d,%d,%d}\n",ptrFont->spr.r, ptrFont->spr.g, ptrFont->spr.b);

				GfxSortSprite(&ptrFont->spr);
				x += ptrFont->char_spacing;
			break;
		}
	}

	if (ptrFont->flags & FONT_BLEND_EFFECT)
	{
		ptrFont->spr.r = _blend_effect_lum;
		ptrFont->spr.g = _blend_effect_lum;
		ptrFont->spr.b = _blend_effect_lum;
	}

	va_end(ap);
}