freetype-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [ft-devel] Research BDF fonts


From: Werner LEMBERG
Subject: Re: [ft-devel] Research BDF fonts
Date: Tue, 05 Sep 2017 22:37:49 +0200 (CEST)

>     I also testing it on truetype font and receive well working font
> rendering image in "dump.txt".  I feel that this is problem only
> with raster BDF fonts.

Yes.

>     What I doing wrong?

Your example code is only suitable for outline fonts.  Attached is an
improved version that works with bitmap fonts also.


    Werner
/* example1.c                                                      */
/*                                                                 */
/* This small program shows how to print a rotated string with the */
/* FreeType 2 library.                                             */


#include <stdio.h>
#include <string.h>
#include <math.h>

#include <ft2build.h>
#include FT_FREETYPE_H
#include FT_BITMAP_H


#define WIDTH   640
#define HEIGHT  480


/* origin is the upper left corner */
unsigned char image[HEIGHT][WIDTH];


/* Replace this function with something useful. */

void
draw_bitmap( FT_Bitmap*  bitmap,
             FT_Int      x,
             FT_Int      y)
{
  FT_Int  i, j, p, q;
  FT_Int  x_max = x + bitmap->width;
  FT_Int  y_max = y + bitmap->rows;


  for ( i = x, p = 0; i < x_max; i++, p++ )
  {
    for ( j = y, q = 0; j < y_max; j++, q++ )
    {
      if ( i < 0      || j < 0       ||
           i >= WIDTH || j >= HEIGHT )
        continue;

      image[j][i] |= bitmap->buffer[q * bitmap->width + p];
    }
  }
}


void
show_image( void )
{
  int  i, j;


  for ( i = 0; i < HEIGHT; i++ )
  {
    for ( j = 0; j < WIDTH; j++ )
      putchar( image[i][j] == 0 ? ' '
                                : image[i][j] < 128 ? '+'
                                                    : '*' );
    putchar( '\n' );
  }
}


int
main( int     argc,
      char**  argv )
{
  FT_Library    library;
  FT_Face       face;

  FT_GlyphSlot  slot;
  FT_Matrix     matrix;                 /* transformation matrix */
  FT_Vector     pen;                    /* untransformed origin  */
  FT_Bitmap     bitmap;
  FT_Error      error;

  char*         filename;
  char*         text;

  double        angle;
  int           target_height;
  int           n, num_chars;


  if ( argc != 3 )
  {
    fprintf ( stderr, "usage: %s font sample-text\n", argv[0] );
    exit( 1 );
  }

  filename      = argv[1];                           /* first argument     */
  text          = argv[2];                           /* second argument    */
  num_chars     = strlen( text );
  angle         = ( 25.0 / 360 ) * 3.14159 * 2;      /* use 25 degrees     */
  target_height = HEIGHT;

  error = FT_Init_FreeType( &library );              /* initialize library */
  /* error handling omitted */

  error = FT_New_Face( library, filename, 0, &face );/* create face object */
  /* error handling omitted */

  if ( FT_IS_SCALABLE( face ) )
  {
    /* use 50pt at 100dpi */
    error = FT_Set_Char_Size( face, 50 * 64, 0,
                              100, 0 );              /* set character size */
  }
  else
  {
    /* use size of (first) bitmap strike */
    error = FT_Select_Size( face, 0 );
  }
  /* error handling omitted */

  slot = face->glyph;

  /* set up matrix */
  matrix.xx = (FT_Fixed)( cos( angle ) * 0x10000L );
  matrix.xy = (FT_Fixed)(-sin( angle ) * 0x10000L );
  matrix.yx = (FT_Fixed)( sin( angle ) * 0x10000L );
  matrix.yy = (FT_Fixed)( cos( angle ) * 0x10000L );

  /* the pen position in 26.6 cartesian space coordinates; */
  /* start at (300,200) relative to the upper left corner  */
  pen.x = 300 * 64;
  pen.y = ( target_height - 200 ) * 64;

  FT_Bitmap_Init( &bitmap );

  for ( n = 0; n < num_chars; n++ )
  {
    FT_Int  bitmap_left;
    FT_Int  bitmap_top;


    /* set transformation (works for outline fonts only) */
    FT_Set_Transform( face, &matrix, &pen );

    /* load glyph image into the slot (erase previous one) */
    error = FT_Load_Char( face, text[n], FT_LOAD_RENDER );
    if ( error )
      continue;                 /* ignore errors */

    /* convert to 8bpp */
    if ( slot->bitmap.rows && slot->bitmap.width )
    {
      /* due to a bug in FreeType 2.8.0 and earlier the advertised   */
      /* use of `FT_Bitmap_Convert' multiple times can fail if there */
      /* are empty bitmaps in the chain of calls                     */
      FT_Bitmap_Convert( library, &slot->bitmap, &bitmap, 1 );
    }

    if ( FT_IS_SCALABLE( face ) )
    {
      bitmap_left = slot->bitmap_left;
      bitmap_top  = slot->bitmap_top;
    }
    else
    {
      bitmap_left = pen.x / 64;
      bitmap_top  = pen.y / 64;
    }

    /* now, draw to our target surface (convert position) */
    draw_bitmap( &bitmap,
                 bitmap_left,
                 target_height - bitmap_top );

    /* increment pen position */
    pen.x += slot->advance.x;
    pen.y += slot->advance.y;
  }

  FT_Bitmap_Done( library, &bitmap );

  show_image();

  FT_Done_Face    ( face );
  FT_Done_FreeType( library );

  return 0;
}

/* EOF */

reply via email to

[Prev in Thread] Current Thread [Next in Thread]