font.c
Upload User: shyika
Upload Date: 2017-11-25
Package Size: 1227k
Code Size: 3k
Category:

Video Capture

Development Platform:

Unix_Linux

  1. /*
  2. ** font.c
  3. ** 
  4. ** Made by (Arne Caspari)
  5. ** Login   <arne@arne-laptop>
  6. ** 
  7. ** Started on  Tue Dec 19 18:30:39 2006 Arne Caspari
  8. ** Last update Tue Jan  9 11:38:49 2007 Arne Caspari
  9. */
  10. //#include "yuvfont.h"
  11. #include "unicap.h"
  12. #include "ucil.h"
  13. #include <pango/pangoft2.h>
  14. #include <ft2build.h>
  15. #include FT_FREETYPE_H
  16. #define DEFAULT_FONT "Sans"
  17. static void draw_bitmap( unicap_data_buffer_t *dest,
  18.  ucil_color_t *color,
  19.  int xpos, int ypos,
  20.  FT_Bitmap*  bitmap,
  21.  FT_Int      x,
  22.  FT_Int      y)
  23. {
  24.    FT_Int  i, j, p, q;
  25.    FT_Int  x_max = x + bitmap->width;
  26.    FT_Int  y_max = y + bitmap->rows;
  27.    
  28.    
  29.    for ( i = x, p = 0; i < x_max; i++, p++ )
  30.    {
  31.       for ( j = y, q = 0; j < y_max; j++, q++ )
  32.       {
  33.  if ( ( i < 0 ) || 
  34.       ( j < 0 ) ||
  35.       ( ( i + xpos ) >= dest->format.size.width ) || 
  36.       ( ( j + ypos ) >= dest->format.size.height ) )
  37.  {
  38.     continue;
  39.  }
  40.  if( bitmap->buffer[q * bitmap->width + p] )
  41.  {
  42.     ucil_set_pixel_alpha( dest, color, bitmap->buffer[q * bitmap->width + p], i + xpos, j + ypos );
  43.  }
  44.       }
  45.    }
  46. }
  47. ucil_font_object_t *ucil_create_font_object( int size, const char *font )
  48. {
  49.    ucil_font_object_t *fobj;
  50.    PangoFontDescription *desc;
  51.    PangoFT2FontMap      *fontmap;
  52.    
  53.    fobj = g_malloc( sizeof( ucil_font_object_t ) );
  54.    if( font == NULL )
  55.    {
  56.       font = DEFAULT_FONT;
  57.    }
  58.    fontmap = PANGO_FT2_FONT_MAP (pango_ft2_font_map_new ());
  59.    fobj->context = pango_ft2_font_map_create_context (fontmap);
  60.    g_object_weak_ref( G_OBJECT( (PangoContext*)fobj->context ),
  61.       (GWeakNotify) pango_ft2_font_map_substitute_changed,
  62.       fontmap );
  63.    g_object_unref( fontmap );
  64.    desc = pango_font_description_from_string( font );
  65.    if (size > 0)
  66.    {
  67.       pango_font_description_set_size (desc, size * PANGO_SCALE);
  68.    }
  69.    fobj->layout = pango_layout_new( (PangoContext*)fobj->context );
  70.    pango_layout_set_font_description( (PangoLayout*)fobj->layout, desc );
  71.    pango_font_description_free( desc );
  72.    
  73.    return fobj;
  74. }
  75. void ucil_destroy_font_object( ucil_font_object_t *fobj )
  76. {
  77.    g_object_unref( (PangoLayout*)fobj->layout );
  78.    g_object_unref( (PangoContext*)fobj->context );
  79.    g_free( fobj );
  80. }
  81. void ucil_draw_text( unicap_data_buffer_t *dest, ucil_color_t *color, ucil_font_object_t *fobj, const char *text, int x, int y )
  82. {
  83.    FT_Bitmap             bitmap;
  84.    int                   width;
  85.    int                   height;
  86.    pango_layout_set_text( (PangoLayout*)fobj->layout, text, -1 );
  87.    pango_layout_get_size( (PangoLayout*)fobj->layout, &width, &height );
  88.    width /= PANGO_SCALE;
  89.    height /= PANGO_SCALE;
  90.    bitmap.width = width;
  91.    bitmap.pitch = width;//(bitmap.width + 3) & ~3;
  92.    bitmap.rows = height;
  93.    bitmap.buffer = malloc( bitmap.pitch * bitmap.rows );
  94.    bitmap.num_grays = 256;
  95.    bitmap.pixel_mode = ft_pixel_mode_grays;
  96.    memset( bitmap.buffer, 0x00, bitmap.pitch * bitmap.rows );
  97.    pango_ft2_render_layout( &bitmap, (PangoLayout*)fobj->layout, 0, 0 );
  98.    draw_bitmap (dest, color, x, y, &bitmap, 0, 0);
  99.    free (bitmap.buffer);
  100. }
  101. void ucil_text_get_size( ucil_font_object_t *fobj, const char *text, int *width, int *height )
  102. {
  103.    pango_layout_set_text( (PangoLayout*)fobj->layout, text, -1 );
  104.    pango_layout_get_pixel_size( (PangoLayout*)fobj->layout, width, height );
  105. }