meh
[synfig.git] / synfig-core / trunk / src / modules / lyr_freetype / lyr_freetype.cpp
1 /*! ========================================================================
2 ** Synfig
3 ** Template File
4 ** $Id: lyr_freetype.cpp,v 1.5 2005/01/24 05:00:18 darco Exp $
5 **
6 ** Copyright (c) 2002 Robert B. Quattlebaum Jr.
7 **
8 ** This software and associated documentation
9 ** are CONFIDENTIAL and PROPRIETARY property of
10 ** the above-mentioned copyright holder.
11 **
12 ** You may not copy, print, publish, or in any
13 ** other way distribute this software without
14 ** a prior written agreement with
15 ** the copyright holder.
16 **
17 ** === N O T E S ===========================================================
18 **
19 ** ========================================================================= */
20
21 /* === H E A D E R S ======================================================= */
22
23 #define SYNFIG_LAYER
24
25 #ifdef USING_PCH
26 #       include "pch.h"
27 #else
28 #ifdef HAVE_CONFIG_H
29 #       include <config.h>
30 #endif
31
32 #include "lyr_freetype.h"
33
34
35 #endif
36
37 using namespace std;
38 using namespace etl;
39 using namespace synfig;
40
41 /* === M A C R O S ========================================================= */
42
43 #define MAX_GLYPHS              2000
44
45 #define PANGO_STYLE_NORMAL (0)
46 #define PANGO_STYLE_OBLIQUE (1)
47 #define PANGO_STYLE_ITALIC (2)
48
49
50 #define WEIGHT_NORMAL (400)
51 #define WEIGHT_BOLD (700)
52
53 /* === G L O B A L S ======================================================= */
54
55 SYNFIG_LAYER_INIT(lyr_freetype);
56 SYNFIG_LAYER_SET_NAME(lyr_freetype,"text");
57 SYNFIG_LAYER_SET_LOCAL_NAME(lyr_freetype,_("Simple Text"));
58 SYNFIG_LAYER_SET_CATEGORY(lyr_freetype,_("Typography"));
59 SYNFIG_LAYER_SET_VERSION(lyr_freetype,"0.2");
60 SYNFIG_LAYER_SET_CVS_ID(lyr_freetype,"$Id: lyr_freetype.cpp,v 1.5 2005/01/24 05:00:18 darco Exp $");
61
62 /* === P R O C E D U R E S ================================================= */
63
64 /*Glyph::~Glyph()
65 {
66         if(glyph)FT_Done_Glyph(glyph);  
67 }
68 */
69 void
70 TextLine::clear_and_free()
71 {
72         std::vector<Glyph>::iterator iter;
73         for(iter=glyph_table.begin();iter!=glyph_table.end();++iter)
74         {
75                 if(iter->glyph)FT_Done_Glyph(iter->glyph);
76                 iter->glyph=0;
77         }
78         glyph_table.clear();
79 }
80
81 /* === M E T H O D S ======================================================= */
82
83 lyr_freetype::lyr_freetype()
84 {
85         face=0;
86         
87         size=Vector(0.25,0.25);
88         text=_("Text Layer");
89         color=Color::black();
90         pos=Vector(0,0);
91         orient=Vector(0.5,0.5);
92         compress=1.0;
93         vcompress=1.0;
94         weight=WEIGHT_NORMAL;
95         style=PANGO_STYLE_NORMAL;
96         family="Sans Serif";
97         use_kerning=true;
98         grid_fit=false;
99         old_version=false;
100         set_blend_method(Color::BLEND_COMPOSITE);
101         needs_sync_=true;
102         
103         new_font(family,style,weight);
104         
105         invert=false;
106 }
107
108 lyr_freetype::~lyr_freetype()
109 {
110         if(face)
111                 FT_Done_Face(face);
112 }
113
114 void
115 lyr_freetype::new_font(const synfig::String &family, int style, int weight)
116 {               
117         if(
118                 !new_font_(family,style,weight) &&
119                 !new_font_(family,style,WEIGHT_NORMAL) &&
120                 !new_font_(family,PANGO_STYLE_NORMAL,weight) &&
121                 !new_font_(family,PANGO_STYLE_NORMAL,WEIGHT_NORMAL) &&
122                 !new_font_("sans serif",style,weight) &&
123                 !new_font_("sans serif",style,WEIGHT_NORMAL) &&
124                 !new_font_("sans serif",PANGO_STYLE_NORMAL,weight)
125         )
126                 new_font_("sans serif",PANGO_STYLE_NORMAL,WEIGHT_NORMAL);
127 }
128
129 bool
130 lyr_freetype::new_font_(const synfig::String &font_fam_, int style, int weight)
131 {
132         synfig::String font_fam(font_fam_);
133
134         if(new_face(font_fam_))
135                 return true;
136         
137         //start evil hack
138         for(unsigned int i=0;i<font_fam.size();i++)font_fam[i]=tolower(font_fam[i]);
139         //end evil hack
140
141         if(font_fam=="arial black")
142 #ifndef __APPLE__
143         if(new_face("ariblk"))
144                         return true;
145                 else
146 #endif
147                 font_fam="sans serif";
148         
149         if(font_fam=="sans serif" || font_fam=="arial")
150         {
151                 String arial("arial");
152                 if(weight>WEIGHT_NORMAL)
153                         arial+='b';
154                 if(style==PANGO_STYLE_ITALIC||style==PANGO_STYLE_OBLIQUE)
155                         arial+='i';
156                 else
157                         if(weight>WEIGHT_NORMAL) arial+='d';
158
159                 if(new_face(arial))
160                         return true;
161 #ifdef __APPLE__
162                 if(new_face("Helvetica RO"))
163                         return true;
164 #endif
165         }
166
167         if(font_fam=="comic" || font_fam=="comic sans")
168         {
169                 String filename("comic");
170                 if(weight>WEIGHT_NORMAL)
171                         filename+='b';
172                 if(style==PANGO_STYLE_ITALIC||style==PANGO_STYLE_OBLIQUE)
173                         filename+='i';
174                 else if(weight>WEIGHT_NORMAL) filename+='d';
175
176                 if(new_face(filename))
177                         return true;
178         }
179
180         if(font_fam=="courier" || font_fam=="courier new")
181         {
182                 String filename("cour");
183                 if(weight>WEIGHT_NORMAL)
184                         filename+='b';
185                 if(style==PANGO_STYLE_ITALIC||style==PANGO_STYLE_OBLIQUE)
186                         filename+='i';
187                 else if(weight>WEIGHT_NORMAL) filename+='d';
188
189                 if(new_face(filename))
190                         return true;
191         }
192
193         if(font_fam=="serif" || font_fam=="times" || font_fam=="times new roman")
194         {
195                 String filename("times");
196                 if(weight>WEIGHT_NORMAL)
197                         filename+='b';
198                 if(style==PANGO_STYLE_ITALIC||style==PANGO_STYLE_OBLIQUE)
199                         filename+='i';
200                 else if(weight>WEIGHT_NORMAL) filename+='d';
201
202                 if(new_face(filename))
203                         return true;
204         }
205         
206         if(font_fam=="trebuchet")
207         {
208                 String filename("trebuc");
209                 if(weight>WEIGHT_NORMAL)
210                         filename+='b';
211                 if(style==PANGO_STYLE_ITALIC||style==PANGO_STYLE_OBLIQUE)
212                 {
213                         filename+='i';
214                         if(weight<=WEIGHT_NORMAL) filename+='t';
215                 }
216                 else if(weight>WEIGHT_NORMAL) filename+='d';
217
218                 if(new_face(filename))
219                         return true;
220         }
221                 
222         
223         if(font_fam=="sans serif" || font_fam=="luxi sans")
224         {
225                 {
226                         String luxi("luxis");
227                         if(weight>WEIGHT_NORMAL)
228                                 luxi+='b';
229                         else
230                                 luxi+='r';
231                         if(style==PANGO_STYLE_ITALIC||style==PANGO_STYLE_OBLIQUE)
232                                 luxi+='i';
233                         
234                         
235                         if(new_face(luxi))
236                                 return true;
237                 }
238                 if(new_face("arial"))
239                         return true;
240                 if(new_face("Arial"))
241                         return true;
242         }
243         if(font_fam=="serif" || font_fam=="times" || font_fam=="times new roman" || font_fam=="luxi serif")
244         {
245                 {
246                         String luxi("luxir");
247                         if(weight>WEIGHT_NORMAL)
248                                 luxi+='b';
249                         else
250                                 luxi+='r';
251                         if(style==PANGO_STYLE_ITALIC||style==PANGO_STYLE_OBLIQUE)
252                                 luxi+='i';
253                         
254                         if(new_face(luxi))
255                                 return true;
256                 }
257                 if(new_face("Times New Roman"))
258                         return true;
259                 if(new_face("Times"))
260                         return true;
261         }
262         if(font_fam=="luxi")
263         {
264                 {
265                         String luxi("luxim");
266                         if(weight>WEIGHT_NORMAL)
267                                 luxi+='b';
268                         else
269                                 luxi+='r';
270                         if(style==PANGO_STYLE_ITALIC||style==PANGO_STYLE_OBLIQUE)
271                                 luxi+='i';
272                         
273                         if(new_face(luxi))
274                                 return true;
275                 }
276
277                 if(new_face("Times New Roman"))
278                         return true;
279                 if(new_face("Times"))
280                         return true;
281         }
282         
283         return new_face(font_fam_) || new_face(font_fam);
284         
285         return false;
286 }
287
288 #ifdef USE_MAC_FT_FUNCS
289 void fss2path(char *path, FSSpec *fss)
290 {
291   int l;             //fss->name contains name of last item in path
292   for(l=0; l<(fss->name[0]); l++) path[l] = fss->name[l + 1]; 
293   path[l] = 0;
294
295   if(fss->parID != fsRtParID) //path is more than just a volume name
296   { 
297     int i, len;
298     CInfoPBRec pb;
299     
300     pb.dirInfo.ioNamePtr = fss->name;
301     pb.dirInfo.ioVRefNum = fss->vRefNum;
302     pb.dirInfo.ioDrParID = fss->parID;
303     do
304     {
305       pb.dirInfo.ioFDirIndex = -1;  //get parent directory name
306       pb.dirInfo.ioDrDirID = pb.dirInfo.ioDrParID;   
307       if(PBGetCatInfoSync(&pb) != noErr) break;
308
309       len = fss->name[0] + 1;
310       for(i=l; i>=0;  i--) path[i + len] = path[i];
311       for(i=1; i<len; i++) path[i - 1] = fss->name[i]; //add to start of path
312       path[i - 1] = ':';
313       l += len;
314 } while(pb.dirInfo.ioDrDirID != fsRtDirID); //while more directory levels
315   }
316 }
317 #endif
318
319 bool
320 lyr_freetype::new_face(const String &newfont)
321 {
322         int error;
323         FT_Long face_index=0;
324
325         // If we are already loaded, don't bother reloading.
326         if(face && font==newfont)
327                 return true;
328
329         if(face)
330         {
331                 FT_Done_Face(face);
332                 face=0;
333         }
334
335         error=FT_New_Face(ft_library,newfont.c_str(),face_index,&face);
336         if(error)error=FT_New_Face(ft_library,(newfont+".ttf").c_str(),face_index,&face);
337
338         if(get_canvas())
339         {
340                 if(error)error=FT_New_Face(ft_library,(get_canvas()->get_file_path()+ETL_DIRECTORY_SEPERATOR+newfont).c_str(),face_index,&face);
341                 if(error)error=FT_New_Face(ft_library,(get_canvas()->get_file_path()+ETL_DIRECTORY_SEPERATOR+newfont+".ttf").c_str(),face_index,&face);
342         }
343
344 #ifdef USE_MAC_FT_FUNCS
345         if(error)
346         {
347                 FSSpec fs_spec;
348                 error=FT_GetFile_From_Mac_Name(newfont.c_str(),&fs_spec,&face_index);
349                 if(!error)
350                 {
351                         char filename[512];
352                         fss2path(filename,&fs_spec);
353                         //FSSpecToNativePathName(fs_spec,filename,sizeof(filename)-1, 0);
354                         
355                         error=FT_New_Face(ft_library, filename, face_index,&face);
356                         //error=FT_New_Face_From_FSSpec(ft_library, &fs_spec, face_index,&face);
357                         synfig::info(__FILE__":%d: \"%s\" (%s) -- ft_error=%d",__LINE__,newfont.c_str(),filename,error);
358                 }
359                 else
360                 {
361                         synfig::info(__FILE__":%d: \"%s\" -- ft_error=%d",__LINE__,newfont.c_str(),error);
362                         // Unable to generate fs_spec
363                 }
364                   
365         }
366 #endif
367
368 #ifdef WIN32
369         if(error)error=FT_New_Face(ft_library,("C:\\WINDOWS\\FONTS\\"+newfont).c_str(),face_index,&face);
370         if(error)error=FT_New_Face(ft_library,("C:\\WINDOWS\\FONTS\\"+newfont+".ttf").c_str(),face_index,&face);
371 #else
372
373 #ifdef __APPLE__
374         if(error)error=FT_New_Face(ft_library,("~/Library/Fonts/"+newfont).c_str(),face_index,&face);
375         if(error)error=FT_New_Face(ft_library,("~/Library/Fonts/"+newfont+".ttf").c_str(),face_index,&face);
376         if(error)error=FT_New_Face(ft_library,("~/Library/Fonts/"+newfont+".dfont").c_str(),face_index,&face);
377
378         if(error)error=FT_New_Face(ft_library,("/Library/Fonts/"+newfont).c_str(),face_index,&face);
379         if(error)error=FT_New_Face(ft_library,("/Library/Fonts/"+newfont+".ttf").c_str(),face_index,&face);
380         if(error)error=FT_New_Face(ft_library,("/Library/Fonts/"+newfont+".dfont").c_str(),face_index,&face);
381 #endif
382
383         if(error)error=FT_New_Face(ft_library,("/usr/X11R6/lib/X11/fonts/type1/"+newfont).c_str(),face_index,&face);
384         if(error)error=FT_New_Face(ft_library,("/usr/X11R6/lib/X11/fonts/type1/"+newfont+".ttf").c_str(),face_index,&face);
385
386         if(error)error=FT_New_Face(ft_library,("/usr/share/fonts/truetype/"+newfont).c_str(),face_index,&face);
387         if(error)error=FT_New_Face(ft_library,("/usr/share/fonts/truetype/"+newfont+".ttf").c_str(),face_index,&face);
388
389         if(error)error=FT_New_Face(ft_library,("/usr/X11R6/lib/X11/fonts/TTF/"+newfont).c_str(),face_index,&face);
390         if(error)error=FT_New_Face(ft_library,("/usr/X11R6/lib/X11/fonts/TTF/"+newfont+".ttf").c_str(),face_index,&face);
391
392         if(error)error=FT_New_Face(ft_library,("/usr/X11R6/lib/X11/fonts/truetype/"+newfont).c_str(),face_index,&face);
393         if(error)error=FT_New_Face(ft_library,("/usr/X11R6/lib/X11/fonts/truetype/"+newfont+".ttf").c_str(),face_index,&face);
394
395 #endif
396         if(error)
397         {
398                 //synfig::error(strprintf("lyr_freetype:%s (err=%d)",_("Unable to open face."),error));
399                 return false;
400         }
401
402         font=newfont;
403
404         needs_sync_=true;
405         return true;
406 }
407
408 bool
409 lyr_freetype::set_param(const String & param, const ValueBase &value)
410 {
411         Mutex::Lock lock(mutex);
412 /*
413         if(param=="font" && value.same_as(font))
414         {
415                 new_font(etl::basename(value.get(font)),style,weight);
416                 family=etl::basename(value.get(font));
417                 return true;
418         }
419 */
420         IMPORT_PLUS(family,new_font(family,style,weight));
421         IMPORT_PLUS(weight,new_font(family,style,weight));
422         IMPORT_PLUS(style,new_font(family,style,weight));
423         IMPORT_PLUS(size, if(old_version){size/=2.0;} needs_sync_=true );
424         IMPORT_PLUS(text,needs_sync_=true);
425         IMPORT_PLUS(pos,needs_sync_=true);
426         IMPORT(color);
427         IMPORT(invert);
428         IMPORT_PLUS(orient,needs_sync_=true);
429         IMPORT_PLUS(compress,needs_sync_=true);
430         IMPORT_PLUS(vcompress,needs_sync_=true);
431         IMPORT_PLUS(use_kerning,needs_sync_=true);
432         IMPORT_PLUS(grid_fit,needs_sync_=true);
433         
434         return Layer_Composite::set_param(param,value);
435 }
436
437 ValueBase
438 lyr_freetype::get_param(const String& param)const
439 {
440         EXPORT(font);
441         EXPORT(family);
442         EXPORT(style);
443         EXPORT(weight);
444         EXPORT(size);
445         EXPORT(text);
446         EXPORT(color);
447         EXPORT(pos);
448         EXPORT(orient);
449         EXPORT(compress);
450         EXPORT(vcompress);
451         EXPORT(use_kerning);
452         EXPORT(grid_fit);
453         EXPORT(invert);
454         
455         EXPORT_NAME();
456         EXPORT_VERSION();
457
458         return Layer_Composite::get_param(param);
459 }
460
461 Layer::Vocab
462 lyr_freetype::get_param_vocab(void)const
463 {
464         Layer::Vocab ret(Layer_Composite::get_param_vocab());
465
466         ret.push_back(ParamDesc("text")
467                 .set_local_name(_("Text"))
468                 .set_description(_("Text to Render"))
469                 .set_hint("paragraph")
470         );
471
472         ret.push_back(ParamDesc("color")
473                 .set_local_name(_("Color"))
474                 .set_description(_("Color of the text"))
475         );
476
477         ret.push_back(ParamDesc("family")
478                 .set_local_name(_("Font Family"))
479                 .set_hint("font_family")
480         );
481
482         ret.push_back(ParamDesc("style")
483                 .set_local_name(_("Style"))
484                 .set_hint("enum")
485                 .add_enum_value(PANGO_STYLE_NORMAL, "normal" ,_("Normal"))
486                 .add_enum_value(PANGO_STYLE_OBLIQUE, "oblique" ,_("Oblique"))
487                 .add_enum_value(PANGO_STYLE_ITALIC, "italic" ,_("Italic"))
488         );
489
490         ret.push_back(ParamDesc("weight")
491                 .set_local_name(_("Weight"))
492                 .set_hint("enum")
493                 .add_enum_value(200, "ultralight" ,_("Ultralight"))
494                 .add_enum_value(300, "light" ,_("light"))
495                 .add_enum_value(400, "normal" ,_("Normal"))
496                 .add_enum_value(700, "bold" ,_("Bold"))
497                 .add_enum_value(800, "ultrabold" ,_("Ultrabold"))
498                 .add_enum_value(900, "heavy" ,_("Heavy"))
499         );
500         ret.push_back(ParamDesc("compress")
501                 .set_local_name(_("Hozontal Spacing"))
502                 .set_description(_("Describes how close glyphs are horizontally"))
503         );
504
505         ret.push_back(ParamDesc("vcompress")
506                 .set_local_name(_("Vertical Spacing"))
507                 .set_description(_("Describes how close lines of text are vertically"))
508         );
509
510         ret.push_back(ParamDesc("size")
511                 .set_local_name(_("Size"))
512                 .set_description(_("Size of the text"))
513                 .set_hint("size")
514                 .set_origin("pos")
515                 .set_scalar(1)
516         );
517
518         ret.push_back(ParamDesc("orient")
519                 .set_local_name(_("Orientation"))
520                 .set_description(_("Text Orientation"))
521                 .set_invisible_duck()
522         );
523
524         ret.push_back(ParamDesc("pos")
525                 .set_local_name(_("Position"))
526                 .set_description(_("Text Position"))
527         );
528
529         ret.push_back(ParamDesc("font")
530                 .set_local_name(_("Font"))
531                 .set_description(_("Filename of the font to use"))
532                 .set_hint("filename")
533                 .not_critical()
534                 .hidden()
535         );
536
537         ret.push_back(ParamDesc("use_kerning")
538                 .set_local_name(_("Kerning"))
539                 .set_description(_("Enables/Disables font kerning (If the font supports it)"))
540         );
541
542         ret.push_back(ParamDesc("grid_fit")
543                 .set_local_name(_("Sharpen Edges"))
544                 .set_description(_("Turn this off if you are going to be animating the text"))
545         );
546         ret.push_back(ParamDesc("invert")
547                 .set_local_name(_("Invert"))
548         );
549         return ret;
550 }
551
552 void
553 lyr_freetype::sync()
554 {
555         needs_sync_=false;
556         
557         
558         
559         
560 }
561
562 Color
563 lyr_freetype::get_color(Context context, const synfig::Point &pos)const
564 {
565         if(needs_sync_)
566                 const_cast<lyr_freetype*>(this)->sync();
567         
568         if(!face)
569                 return context.get_color(pos);
570         return context.get_color(pos);
571 }
572
573 bool
574 lyr_freetype::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
575 {
576         static synfig::RecMutex freetype_mutex;
577
578         if(needs_sync_)
579                 const_cast<lyr_freetype*>(this)->sync();
580
581         
582         
583         
584         int error;
585         Vector size(lyr_freetype::size*2);
586         
587         if(!context.accelerated_render(surface,quality,renddesc,cb))
588                 return false;
589         
590         if(is_disabled() || text.empty())
591                 return true;
592         
593         // If there is no font loaded, just bail
594         if(!face)
595         {
596                 if(cb)cb->warning(string("lyr_freetype:")+_("No face loaded, no text will be rendered."));
597                 return true;
598         }
599
600         String text(lyr_freetype::text);
601         if(text=="@_FILENAME_@" && get_canvas() && !get_canvas()->get_file_name().empty())
602         {
603                 text=basename(get_canvas()->get_file_name());
604         }
605         
606         // Width and Height of a pixel
607         Vector::value_type pw=renddesc.get_w()/(renddesc.get_br()[0]-renddesc.get_tl()[0]);
608         Vector::value_type ph=renddesc.get_h()/(renddesc.get_br()[1]-renddesc.get_tl()[1]);
609
610     // Calculate character width and height
611         int w=abs(round_to_int(size[0]*pw));
612         int h=abs(round_to_int(size[1]*ph));
613
614     //int bx=(int)((pos[0]-renddesc.get_tl()[0])*pw*64+0.5);
615     //int by=(int)((pos[1]-renddesc.get_tl()[1])*ph*64+0.5);
616     int bx=0;
617     int by=0;
618         
619     // If the font is the size of a pixel, don't bother rendering any text
620         if(w<=1 || h<=1)
621         {
622                 if(cb)cb->warning(string("lyr_freetype:")+_("Text too small, no text will be rendered."));
623                 return true;
624         }
625
626         synfig::RecMutex::Lock lock(freetype_mutex);
627
628 #define CHAR_RESOLUTION         (64)
629         error = FT_Set_Char_Size(
630                 face,                                           // handle to face object           
631                 (int)CHAR_RESOLUTION,   // char_width in 1/64th of points 
632                 (int)CHAR_RESOLUTION,   // char_height in 1/64th of points 
633                 round_to_int(abs(size[0]*pw*CHAR_RESOLUTION)),                                          // horizontal device resolution    
634                 round_to_int(abs(size[1]*ph*CHAR_RESOLUTION)) );                                                // vertical device resolution      
635
636         // Here is where we can compensate for the
637         // error in freetype's rendering engine.
638         const float xerror(abs(size[0]*pw)/(float)face->size->metrics.x_ppem/1.13f/0.996);
639         const float yerror(abs(size[1]*ph)/(float)face->size->metrics.y_ppem/1.13f/0.996);
640         //synfig::info("xerror=%f, yerror=%f",xerror,yerror);
641         const float compress(lyr_freetype::compress*xerror);
642         const float vcompress(lyr_freetype::vcompress*yerror);
643
644         if(error)
645         {
646                 if(cb)cb->error(string("lyr_freetype:")+_("Unable to set face size.")+strprintf(" (err=%d)",error));
647                 return false;
648         }
649
650         FT_GlyphSlot  slot = face->glyph;  // a small shortcut
651         FT_UInt       glyph_index(0);
652         FT_UInt       previous(0);
653         int u,v;
654
655         std::list<TextLine> lines;
656
657         /*
658  --     ** -- CREATE GLYPHS -------------------------------------------------------
659         */
660
661         lines.push_front(TextLine());
662         string::const_iterator iter;
663         for (iter=text.begin(); iter!=text.end(); ++iter)
664         {
665                 int multiplier(1);
666                 if(*iter=='\n')
667                 {
668                         lines.push_front(TextLine());
669                         bx=0;
670                         by=0;
671                         previous=0;
672                         continue;
673                 }
674                 if(*iter=='\t')
675                 {
676                         multiplier=8;
677                         glyph_index = FT_Get_Char_Index( face, ' ' );
678                 }
679                 else
680                         glyph_index = FT_Get_Char_Index( face, *iter );
681
682         // retrieve kerning distance and move pen position
683                 if ( FT_HAS_KERNING(face) && use_kerning && previous && glyph_index )
684                 {
685                         FT_Vector  delta;
686
687                         if(grid_fit)
688                                 FT_Get_Kerning( face, previous, glyph_index, ft_kerning_default, &delta );
689                         else    
690                                 FT_Get_Kerning( face, previous, glyph_index, ft_kerning_unfitted, &delta );
691
692                         if(compress<1.0f)
693                         {
694                                 bx += round_to_int(delta.x*compress);
695                                 by += round_to_int(delta.y*compress);
696                         }
697                         else
698                         {
699                                 bx += delta.x;
700                                 by += delta.y;
701                         }
702         }
703                 
704                 Glyph curr_glyph;
705                 
706         // store current pen position
707         curr_glyph.pos.x = bx;
708         curr_glyph.pos.y = by;
709
710         // load glyph image into the slot. DO NOT RENDER IT !!
711         if(grid_fit)
712                         error = FT_Load_Glyph( face, glyph_index, FT_LOAD_DEFAULT);
713                 else
714                         error = FT_Load_Glyph( face, glyph_index, FT_LOAD_DEFAULT|FT_LOAD_NO_HINTING );
715         if (error) continue;  // ignore errors, jump to next glyph
716
717         // extract glyph image and store it in our table
718         error = FT_Get_Glyph( face->glyph, &curr_glyph.glyph );
719         if (error) continue;  // ignore errors, jump to next glyph
720
721         // record current glyph index
722         previous = glyph_index;
723                 
724                 // Update the line width
725                 lines.front().width=bx+slot->advance.x;
726                 
727                 // increment pen position
728                 if(multiplier>1)
729                         bx += round_to_int(slot->advance.x*multiplier*compress)-bx%round_to_int(slot->advance.x*multiplier*compress);
730                 else
731                         bx += round_to_int(slot->advance.x*compress*multiplier);
732
733                 //bx += round_to_int(slot->advance.x*compress*multiplier);
734                 //by += round_to_int(slot->advance.y*compress);
735                 by += slot->advance.y*multiplier;
736
737                 lines.front().glyph_table.push_back(curr_glyph);
738                 
739         }
740       
741
742         //float string_height;
743         //string_height=(((lines.size()-1)*face->size->metrics.height+lines.back().actual_height()));
744
745         //int string_height=face->size->metrics.ascender;
746 //#define METRICS_SCALE_ONE             (65536.0f)
747 #define METRICS_SCALE_ONE               ((float)(1<<16))
748         
749         float line_height;
750         line_height=vcompress*((float)face->height*(((float)face->size->metrics.y_scale/METRICS_SCALE_ONE)));
751
752         int     string_height;
753         string_height=round_to_int(((lines.size()-1)*line_height+lines.back().actual_height()));
754         //synfig::info("string_height=%d",string_height);
755         //synfig::info("line_height=%f",line_height);
756         
757         /*
758  --     ** -- RENDER THE GLYPHS ---------------------------------------------------
759         */
760
761         Surface src_;
762         Surface *src_surface;
763         
764         src_surface=surface;
765
766         if(invert)
767         {
768                 src_=*surface;
769                 Surface::alpha_pen pen(surface->begin(),get_amount(),get_blend_method());
770                 
771                 surface->fill(color,pen,src_.get_w(),src_.get_h());
772                 
773                 src_surface=&src_;
774         }
775         
776         {
777         std::list<TextLine>::iterator iter;
778         int curr_line;
779         for(curr_line=0,iter=lines.begin();iter!=lines.end();++iter,curr_line++)
780         {
781                 bx=round_to_int((pos[0]-renddesc.get_tl()[0])*pw*CHAR_RESOLUTION-orient[0]*iter->width);
782                 by=round_to_int((pos[1]-renddesc.get_tl()[1])*ph*CHAR_RESOLUTION+(1.0-orient[1])*string_height-line_height*curr_line);
783                 //by=round_to_int(vcompress*((pos[1]-renddesc.get_tl()[1])*ph*64+(1.0-orient[1])*string_height-face->size->metrics.height*curr_line));
784                 //synfig::info("curr_line=%d, bx=%d, by=%d",curr_line,bx,by);
785                 
786                 std::vector<Glyph>::iterator iter2;
787                 for(iter2=iter->glyph_table.begin();iter2!=iter->glyph_table.end();++iter2)
788                 {
789                         FT_Glyph  image(iter2->glyph);
790                         FT_Vector pen;
791                         FT_BitmapGlyph  bit;
792                                 
793                         pen.x = bx + iter2->pos.x;
794                         pen.y = by + iter2->pos.y;
795                         
796                         //synfig::info("GLYPH: pen.x=%d, pen,y=%d",curr_line,(pen.x+32)>>6,(pen.y+32)>>6);
797                 
798                         error = FT_Glyph_To_Bitmap( &image, ft_render_mode_normal,0/*&pen*/, 1 );
799                         if(error) { FT_Done_Glyph( image ); continue; }
800                 
801                         bit = (FT_BitmapGlyph)image;
802
803                         for(v=0;v<bit->bitmap.rows;v++)
804                                 for(u=0;u<bit->bitmap.width;u++)
805                                 {
806                                         int x=u+((pen.x+32)>>6)+ bit->left;
807                                         int y=v+((pen.y+32)>>6)- bit->top;
808                                         if(     y>=0 &&
809                                                 x>=0 &&
810                                                 y<surface->get_h() &&
811                                                 x<surface->get_w())
812                                         {
813                                                 float myamount=(float)bit->bitmap.buffer[v*bit->bitmap.pitch+u]/255.0f;
814                                                 if(invert)
815                                                         myamount=1.0f-myamount;
816                                                 (*surface)[y][x]=Color::blend(color,(*src_surface)[y][x],myamount*get_amount(),get_blend_method());
817                                         }
818                                 }
819                 
820                         FT_Done_Glyph( image );
821                 }
822                 //iter->clear_and_free();
823         }
824         }
825    
826
827         return true;
828 }
829
830 synfig::Rect
831 lyr_freetype::get_bounding_rect()const
832 {
833         if(needs_sync_)
834                 const_cast<lyr_freetype*>(this)->sync();
835 //      if(!is_disabled())
836                 return synfig::Rect::full_plane();
837 }