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 
21 void
22 g15r_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 
49 void
50 g15r_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 
76 void
77 g15r_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 
103 void
104 g15r_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
136 
144 void
145 g15r_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 
178 int
179 calc_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 
190 int
191 calc_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 
208 int
209 calc_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 
220 int
221 calc_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 
232 void
233 draw_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 
250 void
251 draw_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 
282 void
283 g15r_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 */