Crude attempt to get fonts not to render upside-down when rendering to .bmp files.
[synfig.git] / synfig-core / trunk / src / modules / lyr_freetype / lyr_freetype.cpp
index a3b051e..875c0ce 100644 (file)
@@ -7,6 +7,7 @@
 **     \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
@@ -62,8 +63,8 @@ using namespace synfig;
 
 SYNFIG_LAYER_INIT(Layer_Freetype);
 SYNFIG_LAYER_SET_NAME(Layer_Freetype,"text");
-SYNFIG_LAYER_SET_LOCAL_NAME(Layer_Freetype,_("Simple Text"));
-SYNFIG_LAYER_SET_CATEGORY(Layer_Freetype,_("Typography"));
+SYNFIG_LAYER_SET_LOCAL_NAME(Layer_Freetype,_("Text"));
+SYNFIG_LAYER_SET_CATEGORY(Layer_Freetype,_("Other"));
 SYNFIG_LAYER_SET_VERSION(Layer_Freetype,"0.2");
 SYNFIG_LAYER_SET_CVS_ID(Layer_Freetype,"$Id$");
 
@@ -345,8 +346,8 @@ Layer_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
@@ -787,6 +788,11 @@ Layer_Freetype::accelerated_render(Context context,Surface *surface,int quality,
        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);
@@ -818,6 +824,13 @@ Layer_Freetype::accelerated_render(Context context,Surface *surface,int quality,
        {
                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.
+               if (ph>0) by += line_height/1.5;
+
                //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 +844,7 @@ Layer_Freetype::accelerated_render(Context context,Surface *surface,int quality,
                        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 +855,7 @@ Layer_Freetype::accelerated_render(Context context,Surface *surface,int quality,
                                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() &&