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).
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).
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).
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.
void g15r_ttfLoad (g15canvas *canvas, char *fontname, int fontsize, int face_num)
 Loads a font through the FreeType2 library.
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.


Function Documentation

int calc_ttf_centering ( FT_Face  face,
char *  str 
)

Definition at line 209 of file text.c.

References calc_ttf_totalstringwidth().

Referenced by g15r_ttfPrint().

00210 {
00211   int leftpos;
00212 
00213   leftpos = 80 - (calc_ttf_totalstringwidth (face, str) / 2);
00214   if (leftpos < 1)
00215     leftpos = 1;
00216 
00217   return leftpos;
00218 }

int calc_ttf_right_justify ( FT_Face  face,
char *  str 
)

Definition at line 221 of file text.c.

References calc_ttf_totalstringwidth().

Referenced by g15r_ttfPrint().

00222 {
00223   int leftpos;
00224 
00225   leftpos = 160 - calc_ttf_totalstringwidth (face, str);
00226   if (leftpos < 1)
00227     leftpos = 1;
00228 
00229   return leftpos;
00230 }

int calc_ttf_totalstringwidth ( FT_Face  face,
char *  str 
)

Definition at line 191 of file text.c.

Referenced by calc_ttf_centering(), and calc_ttf_right_justify().

00192 {
00193   FT_GlyphSlot slot = face->glyph;
00194   FT_UInt glyph_index;
00195   int i, errcode;
00196   unsigned int len = strlen (str);
00197   int width = 0;
00198 
00199   for (i = 0; i < len; i++)
00200     {
00201       glyph_index = FT_Get_Char_Index (face, str[i]);
00202       errcode = FT_Load_Glyph (face, glyph_index, 0);
00203       width += slot->advance.x >> 6;
00204     }
00205   return width;
00206 }

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

Definition at line 179 of file text.c.

Referenced by g15r_ttfPrint().

00180 {
00181 
00182   if (!FT_IS_SCALABLE (face))
00183     ttf_fontsize = face->available_sizes->height;
00184 
00185   y += ttf_fontsize * .75;
00186 
00187   return y;
00188 }

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.

References g15canvas::ftLib, and g15r_setPixel().

Referenced by draw_ttf_str().

00235 {
00236   FT_Int char_x, char_y, p, q;
00237   FT_Int x_max = x + charbitmap.width;
00238   FT_Int y_max = y + charbitmap.rows;
00239   static FT_Bitmap tmpbuffer;
00240 
00241   /* convert to 8bit format.. */
00242   FT_Bitmap_Convert (canvas->ftLib, &charbitmap, &tmpbuffer, 1);
00243 
00244   for (char_y = y, q = 0; char_y < y_max; char_y++, q++)
00245       for (char_x = x, p = 0; char_x < x_max; char_x++, p++)
00246           if (tmpbuffer.buffer[q * tmpbuffer.width + p])
00247             g15r_setPixel (canvas, char_x, char_y, color);
00248 }

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.

References draw_ttf_char().

Referenced by g15r_ttfPrint().

00253 {
00254   FT_GlyphSlot slot = face->glyph;
00255   int i, errcode;
00256   unsigned int len = strlen (str);
00257 
00258   for (i = 0; i < len; i++)
00259     {
00260       errcode =
00261         FT_Load_Char (face, str[i],
00262                       FT_LOAD_RENDER | FT_LOAD_MONOCHROME |
00263                       FT_LOAD_TARGET_MONO);
00264       draw_ttf_char (canvas, slot->bitmap, str[i], x + slot->bitmap_left,
00265                      y - slot->bitmap_top, color);
00266       x += slot->advance.x >> 6;
00267     }
00268 }

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.

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

Referenced by g15r_renderString().

00025 {
00026   int helper = character * 8;   /* for our font which is 8x8 */
00027 
00028   int top_left_pixel_x = sx + col * (8);        /* 1 pixel spacing */
00029   int top_left_pixel_y = sy + row * (8);        /* once again 1 pixel spacing */
00030 
00031   int x, y;
00032   for (y = 0; y < 8; ++y)
00033     {
00034       for (x = 0; x < 8; ++x)
00035         {
00036           char font_entry = fontdata_8x8[helper + y];
00037 
00038           if (font_entry & 1 << (7 - x))
00039             g15r_setPixel (canvas, top_left_pixel_x + x, top_left_pixel_y + y,
00040                            G15_COLOR_BLACK);
00041           else
00042             g15r_setPixel (canvas, top_left_pixel_x + x, top_left_pixel_y + y,
00043                            G15_COLOR_WHITE);
00044 
00045         }
00046     }
00047 }

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.

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

Referenced by g15r_renderString().

00053 {
00054   int helper = character * 7 * 5;       /* for our font which is 6x4 */
00055 
00056   int top_left_pixel_x = sx + col * (5);        /* 1 pixel spacing */
00057   int top_left_pixel_y = sy + row * (7);        /* once again 1 pixel spacing */
00058 
00059   int x, y;
00060   for (y = 0; y < 7; ++y)
00061     {
00062       for (x = 0; x < 5; ++x)
00063         {
00064           char font_entry = fontdata_7x5[helper + y * 5 + x];
00065           if (font_entry)
00066             g15r_setPixel (canvas, top_left_pixel_x + x, top_left_pixel_y + y,
00067                            G15_COLOR_BLACK);
00068           else
00069             g15r_setPixel (canvas, top_left_pixel_x + x, top_left_pixel_y + y,
00070                            G15_COLOR_WHITE);
00071 
00072         }
00073     }
00074 }

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.

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

Referenced by g15r_renderString().

00080 {
00081   int helper = character * 6 * 4;       /* for our font which is 6x4 */
00082 
00083   int top_left_pixel_x = sx + col * (4);        /* 1 pixel spacing */
00084   int top_left_pixel_y = sy + row * (6);        /* once again 1 pixel spacing */
00085 
00086   int x, y;
00087   for (y = 0; y < 6; ++y)
00088     {
00089       for (x = 0; x < 4; ++x)
00090         {
00091           char font_entry = fontdata_6x4[helper + y * 4 + x];
00092           if (font_entry)
00093             g15r_setPixel (canvas, top_left_pixel_x + x, top_left_pixel_y + y,
00094                            G15_COLOR_BLACK);
00095           else
00096             g15r_setPixel (canvas, top_left_pixel_x + x, top_left_pixel_y + y,
00097                            G15_COLOR_WHITE);
00098 
00099         }
00100     }
00101 }

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.

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

00106 {
00107 
00108   int i = 0;
00109   for (i; stringOut[i] != NULL; ++i)
00110     {
00111       switch (size)
00112         {
00113         case G15_TEXT_SMALL:
00114           {
00115             g15r_renderCharacterSmall (canvas, i, row, stringOut[i], sx, sy);
00116             break;
00117           }
00118         case G15_TEXT_MED:
00119           {
00120             g15r_renderCharacterMedium (canvas, i, row, stringOut[i], sx, sy);
00121             break;
00122           }
00123         case G15_TEXT_LARGE:
00124           {
00125             g15r_renderCharacterLarge (canvas, i, row, stringOut[i], sx, sy);
00126             break;
00127           }
00128         default:
00129           break;
00130         }
00131     }
00132 
00133 }

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:
canvas A pointer to a g15canvas struct in which the buffer to be operated on is found.
fontname Absolute pathname to font file to be loaded.
fontsize Size in points for font to be loaded.
face_num Slot into which font face will be loaded.

Definition at line 145 of file text.c.

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

00146 {
00147   int errcode = 0;
00148 
00149   if (face_num < 0)
00150     face_num = 0;
00151   if (face_num > G15_MAX_FACE)
00152     face_num = G15_MAX_FACE;
00153 
00154   if (canvas->ttf_fontsize[face_num])
00155     FT_Done_Face (canvas->ttf_face[face_num][0]);       /* destroy the last face */
00156 
00157   if (!canvas->ttf_fontsize[face_num] && !fontsize)
00158     canvas->ttf_fontsize[face_num] = 10;
00159   else
00160     canvas->ttf_fontsize[face_num] = fontsize;
00161 
00162   errcode =
00163     FT_New_Face (canvas->ftLib, fontname, 0, &canvas->ttf_face[face_num][0]);
00164   if (errcode)
00165     {
00166       canvas->ttf_fontsize[face_num] = 0;
00167     }
00168   else
00169     {
00170       if (canvas->ttf_fontsize[face_num]
00171           && FT_IS_SCALABLE (canvas->ttf_face[face_num][0]))
00172         errcode =
00173           FT_Set_Char_Size (canvas->ttf_face[face_num][0], 0,
00174                             canvas->ttf_fontsize[face_num] * 64, 90, 0);
00175     }
00176 }

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:
canvas A pointer to a g15canvas struct in which the buffer to be operated on is found.
x initial x position for string.
y initial y position for string.
fontsize Size of string in points.
face_num Font to be used is loaded in this slot.
color Text will be drawn this color.
center Text will be centered if center == 1 and right justified if center == 2.
print_string Pointer to the string to be printed.

Definition at line 283 of file text.c.

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

00285 {
00286   int errcode = 0;
00287 
00288   if (canvas->ttf_fontsize[face_num])
00289     {
00290       if (fontsize > 0 && FT_IS_SCALABLE (canvas->ttf_face[face_num][0]))
00291         {
00292           canvas->ttf_fontsize[face_num] = fontsize;
00293           int errcode =
00294             FT_Set_Pixel_Sizes (canvas->ttf_face[face_num][0], 0,
00295                                 canvas->ttf_fontsize[face_num]);
00296           if (errcode)
00297             printf ("Trouble setting the Glyph size!\n");
00298         }
00299       y =
00300         calc_ttf_true_ypos (canvas->ttf_face[face_num][0], y,
00301                             canvas->ttf_fontsize[face_num]);
00302       if (center == 1)
00303         x = calc_ttf_centering (canvas->ttf_face[face_num][0], print_string);
00304       else if (center == 2)
00305         x = calc_ttf_right_justify (canvas->ttf_face[face_num][0], print_string);
00306       draw_ttf_str (canvas, print_string, x, y, color,
00307                     canvas->ttf_face[face_num][0]);
00308     }
00309 }


Generated on Thu Feb 7 23:39:32 2008 for libg15render by  doxygen 1.5.4