1 /***************************************************************************
2 ADM_vidFont.cpp - description
3 -------------------
4 begin : Sun Dec 15 2002
5 copyright : (C) 2002 by mean
6 email : fixounet@free.fr
7 ***************************************************************************/
8
9 /***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
18
19
20 #include "ADM_default.h"
21 #include "ADM_vidFont.h"
22
23 static FT_Library library; /* handle to library */
24 static int initialized=0; // 0 No init at all, 1 engine inited
25
ADMfont(void)26 ADMfont::ADMfont ( void )
27 {
28 _faceAllocated=0;
29 _use2bytes=0;
30 _hold=0;
31 _value=0;
32
33 }
34
35 /**
36 Deallocate font allocated stuff
37 */
~ADMfont()38 ADMfont::~ADMfont( )
39 {
40
41 if(_faceAllocated)
42 {
43 //
44 FT_Done_Face(_face);
45 _faceAllocated=0;
46
47 }
48 }
49
50
initFreeType(char * fontname)51 int ADMfont::initFreeType( char *fontname )
52 {
53 int error;
54
55 printf("\n ** Initializing FreeType **\n");
56 if(initialized==0)
57 {
58
59 error = FT_Init_FreeType( &library );
60 if ( error )
61 {
62 printf("\n Error Initializing Free Type (%d)\n",error);
63 return 0;
64 }
65 initialized=1;
66 }
67 error = FT_New_Face( library,
68 fontname,
69 0,
70 &_face );
71 if ( error == FT_Err_Unknown_File_Format )
72 {
73 printf("\n Error unknown font format (%d)\n",error);
74 return 0;
75
76 }
77 else if ( error )
78 {
79 printf("\n Error unknown error (font %d)\n",error);
80 return 0;
81 }
82 _faceAllocated=1;
83
84 error = FT_Set_Pixel_Sizes(
85 _face, /* handle to face object */
86 0, /* pixel_width */
87 16 ); /* pixel_height */
88
89 printf("\n ** FreeType Initialized **\n");
90 _hold=0;
91 return 1;
92 }
93 //____________________________________________________
fontSetCharSet(char * charset)94 int ADMfont::fontSetCharSet (char *charset)
95 {
96 printf("OBSOLETE*********\n");
97 return 1;
98 }
99 //---------------------------------
fontSetSize(int size)100 int ADMfont::fontSetSize ( int size)
101 {
102 int error;
103 if(!_faceAllocated)
104 {
105 printf("\n not initialized");
106 return 0;
107 }
108 error = FT_Set_Pixel_Sizes(
109 _face, /* handle to face object */
110 0, /* pixel_width */
111 size ); /* pixel_height */
112
113 return 1;
114 }
115 //____________________________________________________
116
fontDraw(char * target,int c,int prevchar,int stride,int size,int * ww)117 int ADMfont::fontDraw(char *target, int c, int prevchar,int stride, int size,int *ww)
118 {
119
120
121 if(!_faceAllocated)
122 {
123 printf("No face!\n");
124 return 0;
125 }
126 FT_GlyphSlot slot = _face->glyph; // a small shortcut
127 int glyph_index,glyph_prev;
128 int error;
129 FT_Vector delta;
130 int kern;
131
132 //printf("FONT: rendering %d %c\n",c,c);
133 *ww=0;
134 /* Ugly patch t avoid some display problem */
135 #if 0
136 if(c=='\'') c='"';
137 if(prevchar=='\'') prevchar='"';
138 #endif
139 /* Ugly patch t avoid some display problem */
140 glyph_index = FT_Get_Char_Index( _face, c );
141 if(prevchar)
142 glyph_prev=FT_Get_Char_Index( _face, prevchar );
143
144 error = FT_Load_Glyph(
145 _face, /* handle to face object */
146 glyph_index, /* glyph index */
147 0 ); /* load flags, see below */
148 if(error)
149 {
150 printf("Loadglyph error\n");
151 return 0;
152 }
153
154 error = FT_Render_Glyph(
155 slot, /* glyph slot */
156 ft_render_mode_normal); /* render mode */
157
158 if(error)
159 {
160 printf("RenderGlyph error");
161 return 0;
162 }
163
164 // now, draw to our target surface
165 // inspired from MPlayer font rendering
166
167 FT_Bitmap *bitmap=&(slot->bitmap);
168 int heigh;
169 int srow=0;
170
171
172 heigh=bitmap->rows;
173 target+=stride*(size-slot->bitmap_top);
174
175 int correction;
176 // If kerning is available from freetype
177 #ifdef FT_FACE_FLAG_KERNING
178 if(prevchar && FT_HAS_KERNING( _face ))
179 {
180 FT_Get_Kerning( _face,glyph_prev, glyph_index, 0 /*FT_KERNING_DEFAULT*/, &delta );
181 correction=delta.x/64;
182 }
183 else
184 #endif
185 correction=0;
186 target+=correction;
187
188 target+=slot->bitmap_left;
189 //target+=(slot->bitmap_top)*stride;
190 for (int h = heigh; h>0 ; h-- )
191 {
192 for (int w =0;w< bitmap->width; w++ )
193 {
194 if(bitmap->buffer[srow+w])
195 *(target+w) = bitmap->buffer[srow+w];
196
197 }
198 target+=stride;
199 srow+=bitmap->pitch ;
200 }
201
202 // Now advance cursor
203 int advance=0;//correction;
204 *ww=bitmap->width;
205 advance+=slot->advance.x/64;
206
207
208 // printf("FONT: Width %d, adance:%d\n",bitmap->width,advance);
209 // printf("FONT:cur :%c next :%c\n",c,prevchar);
210 // printf("FONT:cur :%d next :%d\n",glyph_index,glyph_prev);
211 // printf("FONT:Raw: %d kerning:%d kerning :%d \n",*ww,delta.x,delta.y);
212
213
214 // FT_Done_Glyph(glyph_index); Mem leak ?
215 *ww=advance;
216 return 1;
217 }
218
219