~ubuntu-branches/ubuntu/vivid/emscripten/vivid

« back to all changes in this revision

Viewing changes to tests/freetype/main.c

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2013-05-02 13:11:51 UTC
  • Revision ID: package-import@ubuntu.com-20130502131151-q8dvteqr1ef2x7xz
Tags: upstream-1.4.1~20130504~adb56cb
ImportĀ upstreamĀ versionĀ 1.4.1~20130504~adb56cb

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* example1.c                                                      */
 
2
/*                                                                 */
 
3
/* This small program shows how to print a rotated string with the */
 
4
/* FreeType 2 library.                                             */
 
5
 
 
6
 
 
7
#include <stdio.h>
 
8
#include <string.h>
 
9
#include <math.h>
 
10
#include <stdlib.h>
 
11
 
 
12
#include <ft2build.h>
 
13
#include FT_FREETYPE_H
 
14
 
 
15
 
 
16
int WIDTH = 0;
 
17
int HEIGHT = 0;
 
18
 
 
19
/* origin is the upper left corner */
 
20
unsigned char *image;
 
21
 
 
22
 
 
23
/* Replace this function with something useful. */
 
24
 
 
25
void
 
26
draw_bitmap( FT_Bitmap*  bitmap,
 
27
             FT_Int      x,
 
28
             FT_Int      y)
 
29
{
 
30
  FT_Int  i, j, p, q;
 
31
  FT_Int  x_max = x + bitmap->width;
 
32
  FT_Int  y_max = y + bitmap->rows;
 
33
 
 
34
 
 
35
  for ( i = x, p = 0; i < x_max; i++, p++ )
 
36
  {
 
37
    for ( j = y, q = 0; j < y_max; j++, q++ )
 
38
    {
 
39
      if ( i < 0      || j < 0       ||
 
40
           i >= WIDTH || j >= HEIGHT )
 
41
        continue;
 
42
 
 
43
      image[j*WIDTH + i] |= bitmap->buffer[q * bitmap->width + p];
 
44
    }
 
45
  }
 
46
}
 
47
 
 
48
 
 
49
void
 
50
show_image( void )
 
51
{
 
52
  int  i, j;
 
53
  int count = 0;
 
54
 
 
55
  for ( i = 0; i < HEIGHT; i++ )
 
56
  {
 
57
    for ( j = 0; j < WIDTH; j++ ) {
 
58
      if (image[i*WIDTH + j]) count++;
 
59
      putchar( image[i*WIDTH + j] == 0 ? ' '
 
60
                                : image[i*WIDTH + j] < 128 ? '+'
 
61
                                                           : '*' );
 
62
    }
 
63
    putchar( '\n' );
 
64
  }
 
65
  printf("Non-0s: %d\n", count);
 
66
}
 
67
 
 
68
 
 
69
int
 
70
main( int     argc,
 
71
      char**  argv )
 
72
{
 
73
  FT_Library    library;
 
74
  FT_Face       face;
 
75
 
 
76
  FT_GlyphSlot  slot;
 
77
  FT_Matrix     matrix;                 /* transformation matrix */
 
78
  FT_Vector     pen;                    /* untransformed origin  */
 
79
  FT_Error      error;
 
80
 
 
81
  char*         filename;
 
82
  char*         text;
 
83
 
 
84
  double        angle;
 
85
  int           target_height;
 
86
  int           n, num_chars;
 
87
 
 
88
  if ( argc != 6 )
 
89
  {
 
90
    fprintf ( stderr, "usage: %s font sample-text width height angle\n", argv[0] );
 
91
    exit( 1 );
 
92
  }
 
93
 
 
94
  filename      = argv[1];                           /* first argument     */
 
95
  text          = argv[2];                           /* second argument    */
 
96
  num_chars     = strlen( text );
 
97
  WIDTH         = atoi(argv[3]);
 
98
  HEIGHT        = atoi(argv[4]);
 
99
  angle         = ( ((float)atoi(argv[5])) / 360 ) * 3.14159 * 2;      /* use 25 degrees     */
 
100
  target_height = HEIGHT;
 
101
 
 
102
  image = (unsigned char*)malloc(WIDTH*HEIGHT);
 
103
  for (int x = 0; x < WIDTH; x++)
 
104
    for (int y = 0; y < HEIGHT; y++)
 
105
      image[y*WIDTH + x] = 0;
 
106
 
 
107
  error = FT_Init_FreeType( &library );              /* initialize library */
 
108
  if (error) printf("Init Error! %d\n", error);
 
109
 
 
110
  error = FT_New_Face( library, argv[1], 0, &face ); /* create face object */
 
111
  if (error) printf("New_Face Error! %d\n", error);
 
112
 
 
113
  /* use 50pt at 100dpi */
 
114
  error = FT_Set_Char_Size( face, 50 * 64, 0,
 
115
                            100, 0 );                /* set character size */
 
116
  if (error) printf("Set_Char_Size Error! %d\n", error);
 
117
 
 
118
  slot = face->glyph;
 
119
 
 
120
  /* set up matrix */
 
121
  matrix.xx = (FT_Fixed)( cos( angle ) * 0x10000L );
 
122
  matrix.xy = (FT_Fixed)(-sin( angle ) * 0x10000L );
 
123
  matrix.yx = (FT_Fixed)( sin( angle ) * 0x10000L );
 
124
  matrix.yy = (FT_Fixed)( cos( angle ) * 0x10000L );
 
125
 
 
126
  pen.x = 1000;
 
127
  pen.y = 0;
 
128
 
 
129
  for ( n = 0; n < num_chars; n++ )
 
130
  {
 
131
    /* set transformation */
 
132
    FT_Set_Transform( face, &matrix, &pen );
 
133
 
 
134
    /* load glyph image into the slot (erase previous one) */
 
135
    error = FT_Load_Char( face, text[n], FT_LOAD_RENDER );
 
136
    if ( error )
 
137
      continue;                 /* ignore errors */
 
138
 
 
139
    /* now, draw to our target surface (convert position) */
 
140
    draw_bitmap( &slot->bitmap,
 
141
                 slot->bitmap_left,
 
142
                 target_height - slot->bitmap_top );
 
143
 
 
144
    /* increment pen position */
 
145
    pen.x += slot->advance.x;
 
146
    pen.y += slot->advance.y;
 
147
  }
 
148
 
 
149
  show_image();
 
150
 
 
151
  FT_Done_Face    ( face );
 
152
  FT_Done_FreeType( library );
 
153
 
 
154
  return 0;
 
155
}
 
156
 
 
157
/* EOF */