Subversion Repositories Kolibri OS

Rev

Rev 5021 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | 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. #include <pixlib2.h>
  11.  
  12. extern char res_def_font[];
  13.  
  14. typedef struct
  15. {
  16.   int  l;
  17.   int  t;
  18.   int  r;
  19.   int  b;
  20. }rect_t;
  21.  
  22. typedef unsigned int color_t;
  23.  
  24. unsigned int ansi2utf32(unsigned char ch);
  25.  
  26. static void my_draw_bitmap(uint8_t *pixmap, uint32_t pitch, FT_Bitmap *bitmap, int dstx, int dsty, int col)
  27. {
  28.     uint8_t *dst;
  29.     uint8_t *src, *tmpsrc;
  30.  
  31.     uint32_t  *tmpdst;
  32.     int i, j;
  33.  
  34.     dst = pixmap + dsty * pitch + dstx*4;
  35.     src = bitmap->buffer;
  36.  
  37.     for( i = 0; i < bitmap->rows; i++ )
  38.     {
  39.         tmpdst = (uint32_t*)dst;
  40.         tmpsrc = src;
  41.  
  42.         dst+= pitch;
  43.         src+= bitmap->pitch;
  44.  
  45.         for( j = 0; j < bitmap->width; j++)
  46.         {
  47.             int a = *tmpsrc++;
  48.             int sr, sg, sb;
  49.             int dr, dg, db;
  50.  
  51.             if( a != 0) a++;
  52.  
  53.             db = *tmpdst & 0xFF;
  54.             dg = (*tmpdst >> 8) & 0xFF;
  55.             dr = (*tmpdst >> 16) &0xFF;
  56.  
  57.             sb = col & 0xFF;
  58.             sg = (col >> 8) & 0xFF;
  59.             sr = (col >> 16) &0xFF;
  60.  
  61.             db = (a*sb + db*(256-a))/256;
  62.             dg = (a*sg + dg*(256-a))/256;
  63.             dr = (a*sr + dr*(256-a))/256;
  64.  
  65.             *tmpdst++ = 0xFF000000|(dr<<16)|(dg<<8)|db;
  66.         };
  67.     }
  68. };
  69.  
  70.  
  71. int draw_text_ext(void *pixmap, uint32_t pitch, FT_Face face, char *text, rect_t *rc, int color)
  72. {
  73.     FT_UInt glyph_index;
  74.     FT_Bool use_kerning = 0;
  75.     FT_UInt previous;
  76.     int x, y, w;
  77.     char ch;
  78.     int err = 0;
  79.  
  80.     use_kerning = FT_HAS_KERNING( face );
  81.     previous = 0;
  82.  
  83.     x = rc->l << 6;
  84.     y = rc->b;
  85.  
  86.     w = (rc->r - rc->l) << 6;
  87.  
  88.     while( ch = *text++ )
  89.     {
  90.         glyph_index = FT_Get_Char_Index( face, ansi2utf32(ch) );
  91.  
  92.         if ( use_kerning && previous && glyph_index )
  93.         {
  94.             FT_Vector delta;
  95.             FT_Get_Kerning( face, previous, glyph_index, FT_KERNING_DEFAULT, &delta );
  96.             x += delta.x ;
  97.         }
  98.  
  99.         if( x + face->glyph->advance.x > w)
  100.             break;
  101.  
  102.         err = FT_Load_Glyph( face, glyph_index, FT_LOAD_DEFAULT );
  103.         if ( err )
  104.             continue;
  105.  
  106.         err = FT_Render_Glyph( face->glyph, FT_RENDER_MODE_NORMAL );
  107.         if ( err )
  108.             continue;
  109.  
  110.         my_draw_bitmap(pixmap, pitch, &face->glyph->bitmap, (x >> 6) + face->glyph->bitmap_left,
  111.                        y - face->glyph->bitmap_top, color);
  112.  
  113.         x += face->glyph->advance.x;
  114.         previous = glyph_index;
  115.     };
  116.  
  117.     return err;
  118. };
  119.  
  120.  
  121. int init_fontlib()
  122. {
  123.     int err;
  124.  
  125.     static FT_Library library;
  126.     FT_Face face = NULL;
  127.  
  128.     err = FT_Init_FreeType( &library );
  129.     if ( err )
  130.     {
  131.         printf("an error occurred during FreeType initialization\n");
  132.         goto done;
  133.     }
  134.  
  135. //    err = FT_New_Face( library, "/hd0/1/IstokWeb.ttf", 0, &face );
  136.  
  137.     err = FT_New_Memory_Face( library, res_def_font, 277996, 0, &face );
  138.     printf("err %d\n", err);
  139.     if ( err == FT_Err_Unknown_File_Format )
  140.     {
  141.         printf("font format is unsupported\n");
  142.         goto done;
  143.  
  144.     }
  145.     else if ( err )
  146.     {
  147.         printf("font file could not be read or broken\n");
  148.         goto done;
  149.  
  150.     }
  151.  
  152.     err = FT_Set_Char_Size( face, 0, 11*64, 96, 96 );
  153. //    err = FT_Set_Pixel_Sizes( face, 0, 100 );
  154.  
  155. done:
  156.  
  157.     return (int)face;
  158. };
  159.  
  160.  
  161. unsigned int ansi2utf32(unsigned char ch)
  162. {
  163.     if(ch < 0x80)
  164.         return ch;
  165.  
  166.     if(ch < 0xB0)
  167.         return 0x410-0x80 + ch;
  168.  
  169.     if(ch < 0xE0)
  170.         return 0;
  171.  
  172.     if(ch < 0xF0)
  173.         return 0x440-0xE0 + ch;
  174.  
  175.     if(ch == 0xF0)
  176.         return 0x401;
  177.     else if(ch==0xF1)
  178.         return 0x451;
  179.     else return 0;
  180. }
  181.  
  182.  
  183.