libg15render
text.c
Go to the documentation of this file.
1/*
2 This file is part of g15tools.
3
4 g15tools is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8
9 g15tools is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with g15lcd; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17*/
18
19#include "libg15render.h"
20
21void
22g15r_renderCharacterLarge (g15canvas * canvas, int col, int row,
23 unsigned char character, unsigned int sx,
24 unsigned int sy)
25{
26 int helper = character * 8; /* for our font which is 8x8 */
27
28 int top_left_pixel_x = sx + col * (8); /* 1 pixel spacing */
29 int top_left_pixel_y = sy + row * (8); /* once again 1 pixel spacing */
30
31 int x, y;
32 for (y = 0; y < 8; ++y)
33 {
34 for (x = 0; x < 8; ++x)
35 {
36 char font_entry = fontdata_8x8[helper + y];
37
38 if (font_entry & 1 << (7 - x))
39 g15r_setPixel (canvas, top_left_pixel_x + x, top_left_pixel_y + y,
41 else
42 g15r_setPixel (canvas, top_left_pixel_x + x, top_left_pixel_y + y,
44
45 }
46 }
47}
48
49void
50g15r_renderCharacterMedium (g15canvas * canvas, int col, int row,
51 unsigned char character, unsigned int sx,
52 unsigned int sy)
53{
54 int helper = character * 7 * 5; /* for our font which is 6x4 */
55
56 int top_left_pixel_x = sx + col * (5); /* 1 pixel spacing */
57 int top_left_pixel_y = sy + row * (7); /* once again 1 pixel spacing */
58
59 int x, y;
60 for (y = 0; y < 7; ++y)
61 {
62 for (x = 0; x < 5; ++x)
63 {
64 char font_entry = fontdata_7x5[helper + y * 5 + x];
65 if (font_entry)
66 g15r_setPixel (canvas, top_left_pixel_x + x, top_left_pixel_y + y,
68 else
69 g15r_setPixel (canvas, top_left_pixel_x + x, top_left_pixel_y + y,
71
72 }
73 }
74}
75
76void
77g15r_renderCharacterSmall (g15canvas * canvas, int col, int row,
78 unsigned char character, unsigned int sx,
79 unsigned int sy)
80{
81 int helper = character * 6 * 4; /* for our font which is 6x4 */
82
83 int top_left_pixel_x = sx + col * (4); /* 1 pixel spacing */
84 int top_left_pixel_y = sy + row * (6); /* once again 1 pixel spacing */
85
86 int x, y;
87 for (y = 0; y < 6; ++y)
88 {
89 for (x = 0; x < 4; ++x)
90 {
91 char font_entry = fontdata_6x4[helper + y * 4 + x];
92 if (font_entry)
93 g15r_setPixel (canvas, top_left_pixel_x + x, top_left_pixel_y + y,
95 else
96 g15r_setPixel (canvas, top_left_pixel_x + x, top_left_pixel_y + y,
98
99 }
100 }
101}
102
103void
104g15r_renderString (g15canvas * canvas, unsigned char stringOut[], int row,
105 int size, unsigned int sx, unsigned int sy)
106{
107
108 int i = 0;
109 for (i; stringOut[i] != NULL; ++i)
110 {
111 switch (size)
112 {
113 case G15_TEXT_SMALL:
114 {
115 g15r_renderCharacterSmall (canvas, i, row, stringOut[i], sx, sy);
116 break;
117 }
118 case G15_TEXT_MED:
119 {
120 g15r_renderCharacterMedium (canvas, i, row, stringOut[i], sx, sy);
121 break;
122 }
123 case G15_TEXT_LARGE:
124 {
125 g15r_renderCharacterLarge (canvas, i, row, stringOut[i], sx, sy);
126 break;
127 }
128 default:
129 break;
130 }
131 }
132
133}
134
135#ifdef TTF_SUPPORT
144void
145g15r_ttfLoad (g15canvas * canvas, char *fontname, int fontsize, int face_num)
146{
147 int errcode = 0;
148
149 if (face_num < 0)
150 face_num = 0;
151 if (face_num > G15_MAX_FACE)
152 face_num = G15_MAX_FACE;
153
154 if (canvas->ttf_fontsize[face_num])
155 FT_Done_Face (canvas->ttf_face[face_num][0]); /* destroy the last face */
156
157 if (!canvas->ttf_fontsize[face_num] && !fontsize)
158 canvas->ttf_fontsize[face_num] = 10;
159 else
160 canvas->ttf_fontsize[face_num] = fontsize;
161
162 errcode =
163 FT_New_Face (canvas->ftLib, fontname, 0, &canvas->ttf_face[face_num][0]);
164 if (errcode)
165 {
166 canvas->ttf_fontsize[face_num] = 0;
167 }
168 else
169 {
170 if (canvas->ttf_fontsize[face_num]
171 && FT_IS_SCALABLE (canvas->ttf_face[face_num][0]))
172 errcode =
173 FT_Set_Char_Size (canvas->ttf_face[face_num][0], 0,
174 canvas->ttf_fontsize[face_num] * 64, 90, 0);
175 }
176}
177
178int
179calc_ttf_true_ypos (FT_Face face, int y, int ttf_fontsize)
180{
181
182 if (!FT_IS_SCALABLE (face))
183 ttf_fontsize = face->available_sizes->height;
184
185 y += ttf_fontsize * .75;
186
187 return y;
188}
189
190int
191calc_ttf_totalstringwidth (FT_Face face, char *str)
192{
193 FT_GlyphSlot slot = face->glyph;
194 FT_UInt glyph_index;
195 int i, errcode;
196 unsigned int len = strlen (str);
197 int width = 0;
198
199 for (i = 0; i < len; i++)
200 {
201 glyph_index = FT_Get_Char_Index (face, str[i]);
202 errcode = FT_Load_Glyph (face, glyph_index, 0);
203 width += slot->advance.x >> 6;
204 }
205 return width;
206}
207
208int
209calc_ttf_centering (FT_Face face, char *str)
210{
211 int leftpos;
212
213 leftpos = 80 - (calc_ttf_totalstringwidth (face, str) / 2);
214 if (leftpos < 1)
215 leftpos = 1;
216
217 return leftpos;
218}
219
220int
221calc_ttf_right_justify (FT_Face face, char *str)
222{
223 int leftpos;
224
225 leftpos = 160 - calc_ttf_totalstringwidth (face, str);
226 if (leftpos < 1)
227 leftpos = 1;
228
229 return leftpos;
230}
231
232void
233draw_ttf_char (g15canvas * canvas, FT_Bitmap charbitmap,
234 unsigned char character, int x, int y, int color)
235{
236 FT_Int char_x, char_y, p, q;
237 FT_Int x_max = x + charbitmap.width;
238 FT_Int y_max = y + charbitmap.rows;
239 static FT_Bitmap tmpbuffer;
240
241 /* convert to 8bit format.. */
242 FT_Bitmap_Convert (canvas->ftLib, &charbitmap, &tmpbuffer, 1);
243
244 for (char_y = y, q = 0; char_y < y_max; char_y++, q++)
245 for (char_x = x, p = 0; char_x < x_max; char_x++, p++)
246 if (tmpbuffer.buffer[q * tmpbuffer.width + p])
247 g15r_setPixel (canvas, char_x, char_y, color);
248}
249
250void
251draw_ttf_str (g15canvas * canvas, char *str, int x, int y, int color,
252 FT_Face face)
253{
254 FT_GlyphSlot slot = face->glyph;
255 int i, errcode;
256 unsigned int len = strlen (str);
257
258 for (i = 0; i < len; i++)
259 {
260 errcode =
261 FT_Load_Char (face, str[i],
262 FT_LOAD_RENDER | FT_LOAD_MONOCHROME |
263 FT_LOAD_TARGET_MONO);
264 draw_ttf_char (canvas, slot->bitmap, str[i], x + slot->bitmap_left,
265 y - slot->bitmap_top, color);
266 x += slot->advance.x >> 6;
267 }
268}
269
282void
283g15r_ttfPrint (g15canvas * canvas, int x, int y, int fontsize, int face_num,
284 int color, int center, char *print_string)
285{
286 int errcode = 0;
287
288 if (canvas->ttf_fontsize[face_num])
289 {
290 if (fontsize > 0 && FT_IS_SCALABLE (canvas->ttf_face[face_num][0]))
291 {
292 canvas->ttf_fontsize[face_num] = fontsize;
293 int errcode =
294 FT_Set_Pixel_Sizes (canvas->ttf_face[face_num][0], 0,
295 canvas->ttf_fontsize[face_num]);
296 if (errcode)
297 printf ("Trouble setting the Glyph size!\n");
298 }
299 y =
300 calc_ttf_true_ypos (canvas->ttf_face[face_num][0], y,
301 canvas->ttf_fontsize[face_num]);
302 if (center == 1)
303 x = calc_ttf_centering (canvas->ttf_face[face_num][0], print_string);
304 else if (center == 2)
305 x = calc_ttf_right_justify (canvas->ttf_face[face_num][0], print_string);
306 draw_ttf_str (canvas, print_string, x, y, color,
307 canvas->ttf_face[face_num][0]);
308 }
309}
310#endif /* TTF_SUPPORT */
#define G15_COLOR_BLACK
Definition: libg15render.h:27
unsigned char fontdata_7x5[]
Font data for the medium (7x5) font.
#define G15_TEXT_SMALL
Definition: libg15render.h:28
void g15r_setPixel(g15canvas *canvas, unsigned int x, unsigned int y, int val)
Sets the value of the pixel at (x, y)
Definition: screen.c:50
unsigned char fontdata_8x8[]
Font data for the large (8x8) font.
unsigned char fontdata_6x4[]
Font data for the small (6x4) font.
#define G15_TEXT_LARGE
Definition: libg15render.h:30
#define G15_COLOR_WHITE
Definition: libg15render.h:26
#define G15_MAX_FACE
Definition: libg15render.h:33
#define G15_TEXT_MED
Definition: libg15render.h:29
This structure holds the data need to render objects to the LCD screen.
Definition: libg15render.h:37
FT_Library ftLib
Definition: libg15render.h:47
int ttf_fontsize[G15_MAX_FACE]
Definition: libg15render.h:49
FT_Face ttf_face[G15_MAX_FACE][sizeof(FT_Face)]
Definition: libg15render.h:48
void g15r_ttfPrint(g15canvas *canvas, int x, int y, int fontsize, int face_num, int color, int center, char *print_string)
Prints a string in a given font.
Definition: text.c:283
int calc_ttf_right_justify(FT_Face face, char *str)
Definition: text.c:221
int calc_ttf_centering(FT_Face face, char *str)
Definition: text.c:209
void g15r_renderCharacterSmall(g15canvas *canvas, int col, int row, unsigned char character, unsigned int sx, unsigned int sy)
Renders a character in the small font at (x, y)
Definition: text.c:77
void g15r_renderCharacterMedium(g15canvas *canvas, int col, int row, unsigned char character, unsigned int sx, unsigned int sy)
Renders a character in the meduim font at (x, y)
Definition: text.c:50
void draw_ttf_str(g15canvas *canvas, char *str, int x, int y, int color, FT_Face face)
Definition: text.c:251
int calc_ttf_true_ypos(FT_Face face, int y, int ttf_fontsize)
Definition: text.c:179
void g15r_renderCharacterLarge(g15canvas *canvas, int col, int row, unsigned char character, unsigned int sx, unsigned int sy)
Renders a character in the large font at (x, y)
Definition: text.c:22
void g15r_renderString(g15canvas *canvas, unsigned char stringOut[], int row, int size, unsigned int sx, unsigned int sy)
Renders a string with font size in row.
Definition: text.c:104
void g15r_ttfLoad(g15canvas *canvas, char *fontname, int fontsize, int face_num)
Loads a font through the FreeType2 library.
Definition: text.c:145
int calc_ttf_totalstringwidth(FT_Face face, char *str)
Definition: text.c:191
void draw_ttf_char(g15canvas *canvas, FT_Bitmap charbitmap, unsigned char character, int x, int y, int color)
Definition: text.c:233