Committed patch 1852208 from Niki W. Waibel's for multibyte characters in the Text...
[synfig.git] / synfig-core / trunk / src / modules / lyr_freetype / lyr_freetype.cpp
index 321692f..c301840 100644 (file)
@@ -1,12 +1,13 @@
 /* === S Y N F I G ========================================================= */
 /*!    \file lyr_freetype.cpp
-**     \brief Template Header
+**     \brief Implementation of the "Text" layer
 **
 **     $Id$
 **
 **     \legal
 **     Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
 **     Copyright (c) 2006 Paul Wise
+**     Copyright (c) 2007 Chris Moore
 **
 **     This package is free software; you can redistribute it and/or
 **     modify it under the terms of the GNU General Public License as
@@ -60,12 +61,12 @@ using namespace synfig;
 
 /* === G L O B A L S ======================================================= */
 
-SYNFIG_LAYER_INIT(lyr_freetype);
-SYNFIG_LAYER_SET_NAME(lyr_freetype,"text");
-SYNFIG_LAYER_SET_LOCAL_NAME(lyr_freetype,_("Simple Text"));
-SYNFIG_LAYER_SET_CATEGORY(lyr_freetype,_("Typography"));
-SYNFIG_LAYER_SET_VERSION(lyr_freetype,"0.2");
-SYNFIG_LAYER_SET_CVS_ID(lyr_freetype,"$Id$");
+SYNFIG_LAYER_INIT(Layer_Freetype);
+SYNFIG_LAYER_SET_NAME(Layer_Freetype,"text");
+SYNFIG_LAYER_SET_LOCAL_NAME(Layer_Freetype,N_("Text"));
+SYNFIG_LAYER_SET_CATEGORY(Layer_Freetype,N_("Other"));
+SYNFIG_LAYER_SET_VERSION(Layer_Freetype,"0.2");
+SYNFIG_LAYER_SET_CVS_ID(Layer_Freetype,"$Id$");
 
 /* === P R O C E D U R E S ================================================= */
 
@@ -88,7 +89,7 @@ TextLine::clear_and_free()
 
 /* === M E T H O D S ======================================================= */
 
-lyr_freetype::lyr_freetype()
+Layer_Freetype::Layer_Freetype()
 {
        face=0;
 
@@ -113,14 +114,14 @@ lyr_freetype::lyr_freetype()
        invert=false;
 }
 
-lyr_freetype::~lyr_freetype()
+Layer_Freetype::~Layer_Freetype()
 {
        if(face)
                FT_Done_Face(face);
 }
 
 void
-lyr_freetype::new_font(const synfig::String &family, int style, int weight)
+Layer_Freetype::new_font(const synfig::String &family, int style, int weight)
 {
        if(
                !new_font_(family,style,weight) &&
@@ -135,7 +136,7 @@ lyr_freetype::new_font(const synfig::String &family, int style, int weight)
 }
 
 bool
-lyr_freetype::new_font_(const synfig::String &font_fam_, int style, int weight)
+Layer_Freetype::new_font_(const synfig::String &font_fam_, int style, int weight)
 {
        synfig::String font_fam(font_fam_);
 
@@ -325,7 +326,7 @@ void fss2path(char *path, FSSpec *fss)
 #endif
 
 bool
-lyr_freetype::new_face(const String &newfont)
+Layer_Freetype::new_face(const String &newfont)
 {
        int error;
        FT_Long face_index=0;
@@ -345,8 +346,8 @@ lyr_freetype::new_face(const String &newfont)
 
        if(get_canvas())
        {
-               if(error)error=FT_New_Face(ft_library,(get_canvas()->get_file_path()+ETL_DIRECTORY_SEPERATOR+newfont).c_str(),face_index,&face);
-               if(error)error=FT_New_Face(ft_library,(get_canvas()->get_file_path()+ETL_DIRECTORY_SEPERATOR+newfont+".ttf").c_str(),face_index,&face);
+               if(error)error=FT_New_Face(ft_library,(get_canvas()->get_file_path()+ETL_DIRECTORY_SEPARATOR+newfont).c_str(),face_index,&face);
+               if(error)error=FT_New_Face(ft_library,(get_canvas()->get_file_path()+ETL_DIRECTORY_SEPARATOR+newfont+".ttf").c_str(),face_index,&face);
        }
 
 #ifdef USE_MAC_FT_FUNCS
@@ -380,7 +381,7 @@ lyr_freetype::new_face(const String &newfont)
                FcResult result;
                if( !FcInit() )
                {
-                       synfig::warning("lyr_freetype: fontconfig: %s",_("unable to initialise"));
+                       synfig::warning("Layer_Freetype: fontconfig: %s",_("unable to initialize"));
                        error = 1;
                } else {
                        FcPattern* pat = FcNameParse((FcChar8 *) newfont.c_str());
@@ -399,7 +400,7 @@ lyr_freetype::new_face(const String &newfont)
                                        error=FT_New_Face(ft_library,(const char*)file,face_index,&face);
                                FcFontSetDestroy(fs);
                        } else
-                               synfig::warning("lyr_freetype: fontconfig: %s",_("empty font set"));
+                               synfig::warning("Layer_Freetype: fontconfig: %s",_("empty font set"));
                }
        }
 #endif
@@ -434,7 +435,7 @@ lyr_freetype::new_face(const String &newfont)
 #endif
        if(error)
        {
-               //synfig::error(strprintf("lyr_freetype:%s (err=%d)",_("Unable to open face."),error));
+               //synfig::error(strprintf("Layer_Freetype:%s (err=%d)",_("Unable to open face."),error));
                return false;
        }
 
@@ -445,11 +446,11 @@ lyr_freetype::new_face(const String &newfont)
 }
 
 bool
-lyr_freetype::set_param(const String & param, const ValueBase &value)
+Layer_Freetype::set_param(const String & param, const ValueBase &value)
 {
        Mutex::Lock lock(mutex);
 /*
-       if(param=="font" && value.same_as(font))
+       if(param=="font" && value.same_type_as(font))
        {
                new_font(etl::basename(value.get(font)),style,weight);
                family=etl::basename(value.get(font));
@@ -474,7 +475,7 @@ lyr_freetype::set_param(const String & param, const ValueBase &value)
 }
 
 ValueBase
-lyr_freetype::get_param(const String& param)const
+Layer_Freetype::get_param(const String& param)const
 {
        EXPORT(font);
        EXPORT(family);
@@ -498,7 +499,7 @@ lyr_freetype::get_param(const String& param)const
 }
 
 Layer::Vocab
-lyr_freetype::get_param_vocab(void)const
+Layer_Freetype::get_param_vocab(void)const
 {
        Layer::Vocab ret(Layer_Composite::get_param_vocab());
 
@@ -537,7 +538,7 @@ lyr_freetype::get_param_vocab(void)const
                .add_enum_value(900, "heavy" ,_("Heavy"))
        );
        ret.push_back(ParamDesc("compress")
-               .set_local_name(_("Hozontal Spacing"))
+               .set_local_name(_("Horizontal Spacing"))
                .set_description(_("Describes how close glyphs are horizontally"))
        );
 
@@ -589,7 +590,7 @@ lyr_freetype::get_param_vocab(void)const
 }
 
 void
-lyr_freetype::sync()
+Layer_Freetype::sync()
 {
        needs_sync_=false;
 
@@ -599,10 +600,10 @@ lyr_freetype::sync()
 }
 
 Color
-lyr_freetype::get_color(Context context, const synfig::Point &pos)const
+Layer_Freetype::get_color(Context context, const synfig::Point &pos)const
 {
        if(needs_sync_)
-               const_cast<lyr_freetype*>(this)->sync();
+               const_cast<Layer_Freetype*>(this)->sync();
 
        if(!face)
                return context.get_color(pos);
@@ -610,18 +611,18 @@ lyr_freetype::get_color(Context context, const synfig::Point &pos)const
 }
 
 bool
-lyr_freetype::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
+Layer_Freetype::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
 {
        static synfig::RecMutex freetype_mutex;
 
        if(needs_sync_)
-               const_cast<lyr_freetype*>(this)->sync();
+               const_cast<Layer_Freetype*>(this)->sync();
 
 
 
 
        int error;
-       Vector size(lyr_freetype::size*2);
+       Vector size(Layer_Freetype::size*2);
 
        if(!context.accelerated_render(surface,quality,renddesc,cb))
                return false;
@@ -632,11 +633,11 @@ lyr_freetype::accelerated_render(Context context,Surface *surface,int quality, c
        // If there is no font loaded, just bail
        if(!face)
        {
-               if(cb)cb->warning(string("lyr_freetype:")+_("No face loaded, no text will be rendered."));
+               if(cb)cb->warning(string("Layer_Freetype:")+_("No face loaded, no text will be rendered."));
                return true;
        }
 
-       String text(lyr_freetype::text);
+       String text(Layer_Freetype::text);
        if(text=="@_FILENAME_@" && get_canvas() && !get_canvas()->get_file_name().empty())
        {
                text=basename(get_canvas()->get_file_name());
@@ -658,7 +659,7 @@ lyr_freetype::accelerated_render(Context context,Surface *surface,int quality, c
     // If the font is the size of a pixel, don't bother rendering any text
        if(w<=1 || h<=1)
        {
-               if(cb)cb->warning(string("lyr_freetype:")+_("Text too small, no text will be rendered."));
+               if(cb)cb->warning(string("Layer_Freetype:")+_("Text too small, no text will be rendered."));
                return true;
        }
 
@@ -677,12 +678,12 @@ lyr_freetype::accelerated_render(Context context,Surface *surface,int quality, c
        const float xerror(abs(size[0]*pw)/(float)face->size->metrics.x_ppem/1.13f/0.996);
        const float yerror(abs(size[1]*ph)/(float)face->size->metrics.y_ppem/1.13f/0.996);
        //synfig::info("xerror=%f, yerror=%f",xerror,yerror);
-       const float compress(lyr_freetype::compress*xerror);
-       const float vcompress(lyr_freetype::vcompress*yerror);
+       const float compress(Layer_Freetype::compress*xerror);
+       const float vcompress(Layer_Freetype::vcompress*yerror);
 
        if(error)
        {
-               if(cb)cb->warning(string("lyr_freetype:")+_("Unable to set face size.")+strprintf(" (err=%d)",error));
+               if(cb)cb->warning(string("Layer_Freetype:")+_("Unable to set face size.")+strprintf(" (err=%d)",error));
        }
 
        FT_GlyphSlot  slot = face->glyph;  // a small shortcut
@@ -696,6 +697,9 @@ lyr_freetype::accelerated_render(Context context,Surface *surface,int quality, c
  --    ** -- CREATE GLYPHS -------------------------------------------------------
        */
 
+       mbstate_t ps;
+       memset(&ps, 0, sizeof(ps));
+
        lines.push_front(TextLine());
        string::const_iterator iter;
        for (iter=text.begin(); iter!=text.end(); ++iter)
@@ -715,7 +719,29 @@ lyr_freetype::accelerated_render(Context context,Surface *surface,int quality, c
                        glyph_index = FT_Get_Char_Index( face, ' ' );
                }
                else
-                       glyph_index = FT_Get_Char_Index( face, *iter );
+               {
+                       wchar_t wc;
+                       size_t converted = mbrtowc(&wc, &(*iter), text.end() - iter, &ps);
+
+                       if(converted == (size_t)(-1))
+                       {
+                               synfig::warning("Layer_Freetype: multibyte: %s",
+                                                               _("Invalid multibyte sequence - is the locale set?\n"));
+                               continue;
+                       }
+
+                       if(converted == (size_t)(-2))
+                       {
+                               synfig::warning("Layer_Freetype: multibyte: %s",
+                                                               _("Can't parse multibyte character.\n"));
+                               continue;
+                       }
+
+                       glyph_index = FT_Get_Char_Index( face, wc );
+
+                       if(converted > 1)
+                               iter += converted - 1;
+               }
 
         // retrieve kerning distance and move pen position
                if ( FT_HAS_KERNING(face) && use_kerning && previous && glyph_index )
@@ -787,6 +813,11 @@ lyr_freetype::accelerated_render(Context context,Surface *surface,int quality, c
        float line_height;
        line_height=vcompress*((float)face->height*(((float)face->size->metrics.y_scale/METRICS_SCALE_ONE)));
 
+       // This module sees to expect pixel height to be negative, as it
+       // usually is.  But rendering to .bmp format causes ph to be
+       // positive, which was causing text to be rendered upside down.
+       if (ph>0) line_height = -line_height;
+
        int     string_height;
        string_height=round_to_int(((lines.size()-1)*line_height+lines.back().actual_height()));
        //synfig::info("string_height=%d",string_height);
@@ -817,7 +848,14 @@ lyr_freetype::accelerated_render(Context context,Surface *surface,int quality, c
        for(curr_line=0,iter=lines.begin();iter!=lines.end();++iter,curr_line++)
        {
                bx=round_to_int((pos[0]-renddesc.get_tl()[0])*pw*CHAR_RESOLUTION-orient[0]*iter->width);
-               by=round_to_int((pos[1]-renddesc.get_tl()[1])*ph*CHAR_RESOLUTION+(1.0-orient[1])*string_height-line_height*curr_line);
+               // I've no idea why 1.5, but it kind of works.  Otherwise,
+               // rendering to .bmp (which renders from bottom to top, due to
+               // the .bmp format describing the image from bottom to top,
+               // renders text in the wrong place.
+               by=round_to_int((pos[1]-renddesc.get_tl()[1])*ph*CHAR_RESOLUTION +
+                                               (1.0-orient[1])*string_height-line_height*curr_line +
+                                               ((ph>0) ? line_height/1.5 : 0));
+
                //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));
                //synfig::info("curr_line=%d, bx=%d, by=%d",curr_line,bx,by);
 
@@ -831,7 +869,7 @@ lyr_freetype::accelerated_render(Context context,Surface *surface,int quality, c
                        pen.x = bx + iter2->pos.x;
                        pen.y = by + iter2->pos.y;
 
-                       //synfig::info("GLYPH: pen.x=%d, pen,y=%d",curr_line,(pen.x+32)>>6,(pen.y+32)>>6);
+                       //synfig::info("GLYPH: line %d, pen.x=%d, pen,y=%d",curr_line,(pen.x+32)>>6,(pen.y+32)>>6);
 
                        error = FT_Glyph_To_Bitmap( &image, ft_render_mode_normal,0/*&pen*/, 1 );
                        if(error) { FT_Done_Glyph( image ); continue; }
@@ -842,7 +880,7 @@ lyr_freetype::accelerated_render(Context context,Surface *surface,int quality, c
                                for(u=0;u<bit->bitmap.width;u++)
                                {
                                        int x=u+((pen.x+32)>>6)+ bit->left;
-                                       int y=v+((pen.y+32)>>6)- bit->top;
+                                       int y=((pen.y+32)>>6) + (bit->top - v) * ((ph<0) ? -1 : 1);
                                        if(     y>=0 &&
                                                x>=0 &&
                                                y<surface->get_h() &&
@@ -866,10 +904,10 @@ lyr_freetype::accelerated_render(Context context,Surface *surface,int quality, c
 }
 
 synfig::Rect
-lyr_freetype::get_bounding_rect()const
+Layer_Freetype::get_bounding_rect()const
 {
        if(needs_sync_)
-               const_cast<lyr_freetype*>(this)->sync();
+               const_cast<Layer_Freetype*>(this)->sync();
 //     if(!is_disabled())
                return synfig::Rect::full_plane();
 }