libg15render
text.c File Reference
#include "libg15render.h"

Go to the source code of this file.

Functions

int calc_ttf_centering (FT_Face face, char *str)
 
int calc_ttf_right_justify (FT_Face face, char *str)
 
int calc_ttf_totalstringwidth (FT_Face face, char *str)
 
int calc_ttf_true_ypos (FT_Face face, int y, int ttf_fontsize)
 
void draw_ttf_char (g15canvas *canvas, FT_Bitmap charbitmap, unsigned char character, int x, int y, int color)
 
void draw_ttf_str (g15canvas *canvas, char *str, int x, int y, int color, FT_Face face)
 
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) More...
 
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) More...
 
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) More...
 
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. More...
 
void g15r_ttfLoad (g15canvas *canvas, char *fontname, int fontsize, int face_num)
 Loads a font through the FreeType2 library. More...
 
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. More...
 

Function Documentation

◆ calc_ttf_centering()

int calc_ttf_centering ( FT_Face  face,
char *  str 
)

Definition at line 209 of file text.c.

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 }

References calc_ttf_totalstringwidth().

Referenced by g15r_ttfPrint().

◆ calc_ttf_right_justify()

int calc_ttf_right_justify ( FT_Face  face,
char *  str 
)

Definition at line 221 of file text.c.

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 }

References calc_ttf_totalstringwidth().

Referenced by g15r_ttfPrint().

◆ calc_ttf_totalstringwidth()

int calc_ttf_totalstringwidth ( FT_Face  face,
char *  str 
)

Definition at line 191 of file text.c.

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 }

Referenced by calc_ttf_centering(), and calc_ttf_right_justify().

◆ calc_ttf_true_ypos()

int calc_ttf_true_ypos ( FT_Face  face,
int  y,
int  ttf_fontsize 
)

Definition at line 179 of file text.c.

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 }

Referenced by g15r_ttfPrint().

◆ draw_ttf_char()

void draw_ttf_char ( g15canvas canvas,
FT_Bitmap  charbitmap,
unsigned char  character,
int  x,
int  y,
int  color 
)

Definition at line 233 of file text.c.

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 }

References g15canvas::ftLib, and g15r_setPixel().

Referenced by draw_ttf_str().

◆ draw_ttf_str()

void draw_ttf_str ( g15canvas canvas,
char *  str,
int  x,
int  y,
int  color,
FT_Face  face 
)

Definition at line 251 of file text.c.

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 }

References draw_ttf_char().

Referenced by g15r_ttfPrint().

◆ g15r_renderCharacterLarge()

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 at line 22 of file text.c.

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 }

References fontdata_8x8, G15_COLOR_BLACK, G15_COLOR_WHITE, and g15r_setPixel().

Referenced by g15r_renderString().

◆ g15r_renderCharacterMedium()

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 at line 50 of file text.c.

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 }

References fontdata_7x5, G15_COLOR_BLACK, G15_COLOR_WHITE, and g15r_setPixel().

Referenced by g15r_renderString().

◆ g15r_renderCharacterSmall()

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 at line 77 of file text.c.

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 }

References fontdata_6x4, G15_COLOR_BLACK, G15_COLOR_WHITE, and g15r_setPixel().

Referenced by g15r_renderString().

◆ g15r_renderString()

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 at line 104 of file text.c.

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 }

References G15_TEXT_LARGE, G15_TEXT_MED, G15_TEXT_SMALL, g15r_renderCharacterLarge(), g15r_renderCharacterMedium(), and g15r_renderCharacterSmall().

◆ g15r_ttfLoad()

void g15r_ttfLoad ( g15canvas canvas,
char *  fontname,
int  fontsize,
int  face_num 
)

Loads a font through the FreeType2 library.

Load a font for use with FreeType2 font support

Parameters
canvasA pointer to a g15canvas struct in which the buffer to be operated on is found.
fontnameAbsolute pathname to font file to be loaded.
fontsizeSize in points for font to be loaded.
face_numSlot into which font face will be loaded.

Definition at line 145 of file text.c.

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 }

References g15canvas::ftLib, G15_MAX_FACE, g15canvas::ttf_face, and g15canvas::ttf_fontsize.

◆ g15r_ttfPrint()

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.

Render a string with a FreeType2 font

Parameters
canvasA pointer to a g15canvas struct in which the buffer to be operated on is found.
xinitial x position for string.
yinitial y position for string.
fontsizeSize of string in points.
face_numFont to be used is loaded in this slot.
colorText will be drawn this color.
centerText will be centered if center == 1 and right justified if center == 2.
print_stringPointer to the string to be printed.

Definition at line 283 of file text.c.

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 }

References calc_ttf_centering(), calc_ttf_right_justify(), calc_ttf_true_ypos(), draw_ttf_str(), g15canvas::ttf_face, and g15canvas::ttf_fontsize.

g15r_setPixel
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
calc_ttf_totalstringwidth
int calc_ttf_totalstringwidth(FT_Face face, char *str)
Definition: text.c:191
G15_MAX_FACE
#define G15_MAX_FACE
Definition: libg15render.h:33
g15r_renderCharacterMedium
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
draw_ttf_char
void draw_ttf_char(g15canvas *canvas, FT_Bitmap charbitmap, unsigned char character, int x, int y, int color)
Definition: text.c:233
G15_TEXT_MED
#define G15_TEXT_MED
Definition: libg15render.h:29
g15canvas::ttf_face
FT_Face ttf_face[G15_MAX_FACE][sizeof(FT_Face)]
Definition: libg15render.h:48
fontdata_7x5
unsigned char fontdata_7x5[]
Font data for the medium (7x5) font.
draw_ttf_str
void draw_ttf_str(g15canvas *canvas, char *str, int x, int y, int color, FT_Face face)
Definition: text.c:251
g15canvas::ttf_fontsize
int ttf_fontsize[G15_MAX_FACE]
Definition: libg15render.h:49
G15_TEXT_LARGE
#define G15_TEXT_LARGE
Definition: libg15render.h:30
g15r_renderCharacterSmall
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
fontdata_8x8
unsigned char fontdata_8x8[]
Font data for the large (8x8) font.
G15_COLOR_BLACK
#define G15_COLOR_BLACK
Definition: libg15render.h:27
calc_ttf_true_ypos
int calc_ttf_true_ypos(FT_Face face, int y, int ttf_fontsize)
Definition: text.c:179
fontdata_6x4
unsigned char fontdata_6x4[]
Font data for the small (6x4) font.
g15canvas::ftLib
FT_Library ftLib
Definition: libg15render.h:47
G15_COLOR_WHITE
#define G15_COLOR_WHITE
Definition: libg15render.h:26
calc_ttf_right_justify
int calc_ttf_right_justify(FT_Face face, char *str)
Definition: text.c:221
g15r_renderCharacterLarge
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
G15_TEXT_SMALL
#define G15_TEXT_SMALL
Definition: libg15render.h:28
calc_ttf_centering
int calc_ttf_centering(FT_Face face, char *str)
Definition: text.c:209