root/trunk/freetype/examples/example1.d

Revision 22, 2.9 kB (checked in by JJR, 7 years ago)

* Added Dynamic FreeType 2 Bindings for Win32 and Linux

Line 
1 import freetype.ft;
2
3 import std.math;
4 import std.string;
5 import std.stdio;
6
7 const int WIDTH = 640;
8 const int HEIGHT = 480;
9
10
11 /* origin is the upper left corner */
12 ubyte[WIDTH][HEIGHT] image;
13
14
15 /* Replace this function with something useful. */
16
17 void drawBitmap( FT_Bitmap* bitmap, FT_Int x, FT_Int y )
18 {
19     FT_Int i, j, p, q;
20     FT_Int  x_max = x + bitmap.width;
21     FT_Int  y_max = y + bitmap.rows;
22     for ( i = x, p = 0; i < x_max; i++, p++ )
23     {
24         for ( j = y, q = 0; j < y_max; j++, q++ )
25         {
26             if ( i >= WIDTH || j >= HEIGHT )
27                 continue;
28             image[j][i] |= bitmap.buffer[q * bitmap.width + p];
29         }
30     }
31 }
32
33 void showImage()
34 {
35     int  i, j;
36
37     for ( i = 0; i < HEIGHT; i++ )
38     {
39         for ( j = 0; j < WIDTH; j++ )
40             putchar( image[i][j] == 0 ? ' ' : image[i][j] < 128 ? '+' : '*' );
41         putchar( '\n' );
42     }
43 }
44
45 int main( char[][]  args )
46 {
47     FT_Library    library;
48     FT_Face       face;
49
50     FT_GlyphSlot  slot;
51     FT_Matrix     matrix;                 /* transformation matrix */
52     FT_UInt       glyph_index;
53     FT_Vector     pen;                    /* untransformed origin  */
54     FT_Error      error;
55
56     char[]        filename;
57     char*         text;
58
59     double        angle;
60     int           target_height;
61     int           n, num_chars;
62        
63    
64     filename      = args[1].dup;                           /* first argument     */
65     text          = toStringz(args[2].dup);                /* second argument    */
66     num_chars     = strlen( text );
67     angle         = ( 25.0 / 360 ) * 3.14159 * 2;      /* use 25 degrees     */
68     target_height = HEIGHT;
69
70     error = FT_Init_FreeType( &library );              /* initialize library */
71     /* error handling omitted */
72    
73     error = FT_New_Face( library, toStringz(args[1]), 0, &face ); /* create face object */
74     /* error handling omitted */
75    
76     /* use 50pt at 100dpi */
77     error = FT_Set_Char_Size( face, 50 * 64, 0, 100, 0 );  /* set character size */
78     /* error handling omitted */
79    
80     slot = face.glyph;
81    
82     /* set up matrix */
83     matrix.xx = cast(FT_Fixed)( cos( angle ) * 0x10000L );
84     matrix.xy = cast(FT_Fixed)(-sin( angle ) * 0x10000L );
85     matrix.yx = cast(FT_Fixed)( sin( angle ) * 0x10000L );
86     matrix.yy = cast(FT_Fixed)( cos( angle ) * 0x10000L ); 
87
88     /* the pen position in 26.6 cartesian space coordinates; */
89     /* start at (300,200) relative to the upper left corner  */
90     pen.x = 300 * 64;
91     pen.y = ( target_height - 200 ) * 64;
92
93     for ( n = 0; n < num_chars; n++ )
94     {
95         /* set transformation */
96         FT_Set_Transform( face, &matrix, &pen );
97    
98         /* load glyph image into the slot (erase previous one) */
99         error = FT_Load_Char( face, text[n], FT_LOAD_RENDER );
100         if ( error )
101             continue;                 /* ignore errors */
102
103         /* now, draw to our target surface (convert position) */
104         drawBitmap( &slot.bitmap, slot.bitmap_left, target_height - slot.bitmap_top );
105    
106         /* increment pen position */
107         pen.x += slot.advance.x;
108         pen.y += slot.advance.y;
109     }
110
111     showImage();
112    
113     FT_Done_Face    ( face );
114     FT_Done_FreeType( library );
115    
116     return 0;
117 }
118
119 /* EOF */
Note: See TracBrowser for help on using the browser.