libg15render

text.c

Go to the documentation of this file.
00001 /*
00002     This file is part of g15tools.
00003 
00004     g15tools is free software; you can redistribute it and/or modify
00005     it under the terms of the GNU General Public License as published by
00006     the Free Software Foundation; either version 2 of the License, or
00007     (at your option) any later version.
00008 
00009     g15tools is distributed in the hope that it will be useful,
00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012     GNU General Public License for more details.
00013 
00014     You should have received a copy of the GNU General Public License
00015     along with g15lcd; if not, write to the Free Software
00016     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00017 */
00018 
00019 #include "libg15render.h"
00020 
00021 void
00022 g15r_renderCharacterLarge (g15canvas * canvas, int col, int row,
00023                            unsigned char character, unsigned int sx,
00024                            unsigned int sy)
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 }
00048 
00049 void
00050 g15r_renderCharacterMedium (g15canvas * canvas, int col, int row,
00051                             unsigned char character, unsigned int sx,
00052                             unsigned int sy)
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 }
00075 
00076 void
00077 g15r_renderCharacterSmall (g15canvas * canvas, int col, int row,
00078                            unsigned char character, unsigned int sx,
00079                            unsigned int sy)
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 }
00102 
00103 void
00104 g15r_renderString (g15canvas * canvas, unsigned char stringOut[], int row,
00105                    int size, unsigned int sx, unsigned int sy)
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 }
00134 
00135 #ifdef TTF_SUPPORT
00136 
00144 void
00145 g15r_ttfLoad (g15canvas * canvas, char *fontname, int fontsize, int face_num)
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 }
00177 
00178 int
00179 calc_ttf_true_ypos (FT_Face face, int y, int ttf_fontsize)
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 }
00189 
00190 int
00191 calc_ttf_totalstringwidth (FT_Face face, char *str)
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 }
00207 
00208 int
00209 calc_ttf_centering (FT_Face face, char *str)
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 }
00219 
00220 int
00221 calc_ttf_right_justify (FT_Face face, char *str)
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 }
00231 
00232 void
00233 draw_ttf_char (g15canvas * canvas, FT_Bitmap charbitmap,
00234                unsigned char character, int x, int y, int color)
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 }
00249 
00250 void
00251 draw_ttf_str (g15canvas * canvas, char *str, int x, int y, int color,
00252               FT_Face face)
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 }
00269 
00282 void
00283 g15r_ttfPrint (g15canvas * canvas, int x, int y, int fontsize, int face_num,
00284                int color, int center, char *print_string)
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 }
00310 #endif /* TTF_SUPPORT */