Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1.  
  2. #include <stdint.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <math.h>
  6. #include <memory.h>
  7. #include "font_droid.h"
  8. #include <ft2build.h>
  9. #include FT_FREETYPE_H
  10.  
  11. typedef unsigned int color_t;
  12.  
  13. typedef struct
  14. {
  15.     uint32_t    width;
  16.     uint32_t    height;
  17.     uint32_t    pitch;
  18.     uint32_t    handle;
  19.     uint8_t    *data;
  20. }bitmap_t;
  21.  
  22.  
  23. void my_draw_bitmap(bitmap_t *win, FT_Bitmap *bitmap, int dstx, int dsty, int col)
  24. {
  25.     uint8_t *dst;
  26.     uint8_t *src, *tmpsrc;
  27.  
  28.     uint32_t  *tmpdst;
  29.     int i, j;
  30.  
  31.     dst = win->data + dsty * win->pitch + dstx*4;
  32.     src = bitmap->buffer;
  33.  
  34.     for( i = 0; i < bitmap->rows; i++ )
  35.     {
  36.         tmpdst = (uint32_t*)dst;
  37.         tmpsrc = src;
  38.  
  39.         dst+= win->pitch;
  40.         src+= bitmap->pitch;
  41.  
  42.         for( j = 0; j < bitmap->width; j++)
  43.         {
  44.             int a = *tmpsrc++;
  45.             int sr, sg, sb;
  46.             int dr, dg, db;
  47.  
  48.             if( a != 0) a++;
  49.  
  50.             db = *tmpdst & 0xFF;
  51.             dg = (*tmpdst >> 8) & 0xFF;
  52.             dr = (*tmpdst >> 16) &0xFF;
  53.  
  54.             sb = col & 0xFF;
  55.             sg = (col >> 8) & 0xFF;
  56.             sr = (col >> 16) &0xFF;
  57.  
  58.             db = (a*sb + db*(256-a))/256;
  59.             dg = (a*sg + dg*(256-a))/256;
  60.             dr = (a*sr + dr*(256-a))/256;
  61.  
  62.             *tmpdst++ = 0xFF000000|(dr<<16)|(dg<<8)|db;
  63.         };
  64.     }
  65. };
  66.  
  67.  
  68. int draw_text(bitmap_t * winbitmap, FT_Face face, char *text, int x, int y, int color)
  69. {
  70.     FT_UInt glyph_index;
  71.     FT_Bool use_kerning = 0;
  72.     FT_UInt previous;
  73.  
  74.     char ch;
  75.     int err = 0;
  76.  
  77.     use_kerning = FT_HAS_KERNING( face );
  78.     previous = 0;
  79.  
  80.     x <<= 6;
  81.  
  82.     while( ch = *text++ )
  83.     {
  84.         glyph_index = FT_Get_Char_Index( face, ch );
  85.  
  86.         if ( use_kerning && previous && glyph_index )
  87.         {
  88.             FT_Vector delta;
  89.             FT_Get_Kerning( face, previous, glyph_index, FT_KERNING_DEFAULT, &delta );
  90.             x += delta.x ;
  91.         }
  92.  
  93.         err = FT_Load_Glyph( face, glyph_index, FT_LOAD_DEFAULT );
  94.         if ( err )
  95.             continue;
  96.  
  97.         err = FT_Render_Glyph( face->glyph, FT_RENDER_MODE_NORMAL );
  98.         if ( err )
  99.             continue;
  100.  
  101.         my_draw_bitmap(winbitmap, &face->glyph->bitmap, (x >> 6) + face->glyph->bitmap_left,
  102.                         y - face->glyph->bitmap_top, color);
  103.  
  104.         x += face->glyph->advance.x;
  105.         previous = glyph_index;
  106.     };
  107.  
  108.     return err;
  109. };
  110.  
  111. int init_fontlib()
  112. {
  113.     int err;
  114.  
  115.     static FT_Library library;
  116.     FT_Face face = NULL;
  117.  
  118.     err = FT_Init_FreeType( &library );
  119.     if ( err )
  120.     {
  121.         printf("an error occurred during FreeType initialization\n");
  122.         goto done;
  123.     }
  124.  
  125.     err = FT_New_Face( library, "/hd0/1/istokweb.ttf", 0, &face );
  126.  
  127. //    err = FT_New_Memory_Face( library, pdf_font_DroidSans, 139280, 0, &face );
  128.     if ( err == FT_Err_Unknown_File_Format )
  129.     {
  130.         printf("font format is unsupported\n");
  131.         goto done;
  132.  
  133.     }
  134.     else if ( err )
  135.     {
  136.         printf("font file could not be read or broken\n");
  137.         goto done;
  138.  
  139.     }
  140.  
  141.     err = FT_Set_Char_Size( face, 0, 12*64, 96, 96 );
  142. //    err = FT_Set_Pixel_Sizes( face, 0, 100 );
  143.  
  144. done:
  145.  
  146.     return (int)face;
  147. };
  148.  
  149. //    draw_text(face,"/hd0/1/demo", 10, 80, 0x00000000);
  150.  
  151.