Author: Carlos Lopez <[email protected]>
Date:   Mon Aug 27 20:10:55 2012 +0200

Layer_Freetype: initial version for Cairo render. It tries to imitate the 
current software render. Not all the features implemented yet.

---

 .../src/modules/lyr_freetype/lyr_freetype.cpp      |  110 ++++++++++++++++++++
 .../src/modules/lyr_freetype/lyr_freetype.h        |    1 +
 2 files changed, 111 insertions(+), 0 deletions(-)

diff --git a/synfig-core/src/modules/lyr_freetype/lyr_freetype.cpp 
b/synfig-core/src/modules/lyr_freetype/lyr_freetype.cpp
index 06d0f92..42443e1 100644
--- a/synfig-core/src/modules/lyr_freetype/lyr_freetype.cpp
+++ b/synfig-core/src/modules/lyr_freetype/lyr_freetype.cpp
@@ -41,6 +41,8 @@
 #include "lyr_freetype.h"
 #endif
 
+#include <pango/pangocairo.h>
+
 using namespace std;
 using namespace etl;
 using namespace synfig;
@@ -911,6 +913,114 @@ Layer_Freetype::accelerated_render(Context 
context,Surface *surface,int quality,
        return true;
 }
 
+////
+bool
+Layer_Freetype::accelerated_cairorender(Context context,cairo_surface_t 
*surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
+{
+       const Point     tl(renddesc.get_tl());
+       const Point br(renddesc.get_br());
+       
+       const int       w(renddesc.get_w());
+       const int       h(renddesc.get_h());
+       
+       // Width and Height of a pixel
+       const Real pw = (br[0] - tl[0]) / w;
+       const Real ph = (br[1] - tl[1]) / h;
+       
+       const double sx(1/pw);
+       const double sy(1/ph);
+       const double tx((-tl[0]+origin[0])*sx);
+       const double ty((-tl[1]+origin[1])*sy);
+
+       // Initially render what's behind us
+       if(!context.accelerated_cairorender(surface,quality,renddesc,cb))
+       {
+               if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Cairo 
Renderer Failure",__LINE__));
+               return false;
+       }
+
+       // Cairo context
+       cairo_t* cr=cairo_create(surface);
+
+       // Pango
+       PangoLayout *layout;
+       PangoFontDescription *font_description;
+       // Pango Font
+       font_description = pango_font_description_new ();
+       pango_font_description_set_family (font_description, font.c_str());
+       pango_font_description_set_weight (font_description, 
PangoWeight(weight));
+       pango_font_description_set_style (font_description, PangoStyle(style));
+       float sizex=1.75*size[0]*sx;
+       pango_font_description_set_absolute_size (font_description, sizex * 
PANGO_SCALE );
+       //Pango Layout
+       layout = pango_cairo_create_layout (cr);
+       pango_layout_set_font_description (layout, font_description);
+       pango_layout_set_text (layout, text.c_str(), -1);
+       pango_layout_set_alignment(layout, PANGO_ALIGN_LEFT);
+       pango_layout_set_single_paragraph_mode(layout, false);
+       PangoRectangle ink_rect;
+       PangoRectangle logical_rect;
+       pango_layout_get_pixel_extents(layout, &ink_rect, &logical_rect);
+       
+       // Render text
+       cairo_save(cr);
+       cairo_set_source_rgba(cr, color.get_r(), color.get_g(), color.get_b(), 
color.get_a());
+       cairo_move_to(cr, tx, ty);
+       int total_lines=pango_layout_get_line_count(layout), i;
+       synfig::info("total lines %d", total_lines);
+       PangoLayoutIter* layoutiter(pango_layout_get_iter(layout));
+       int sup=0, inf, wdiff, baseline;
+       for(i=0;i<total_lines; i++)
+       {
+               synfig::info("i=%d", i);
+               pango_layout_iter_get_line_extents(layoutiter, &ink_rect, 
&logical_rect);
+               pango_extents_to_pixels(&ink_rect, NULL);
+               pango_extents_to_pixels(&logical_rect, NULL);
+               sup=ink_rect.y-logical_rect.y;
+               wdiff=logical_rect.height-ink_rect.height;
+               inf=wdiff-sup;
+               synfig::info("sup=%d", sup);
+               synfig::info("inf=%d", inf);
+               baseline=pango_layout_iter_get_baseline(layoutiter);
+               synfig::info("baseline=%d", baseline);
+               cairo_move_to(cr, tx, ty+baseline/PANGO_SCALE);
+               cairo_rel_move_to(cr, 0, -ink_rect.y+logical_rect.y);
+               pango_cairo_show_layout_line(cr, 
pango_layout_get_line_readonly(layout, i));
+               //cairo_rel_move_to(cr, 0, inf);
+               if(!pango_layout_iter_next_line(layoutiter))
+                       break;
+       }
+//     cairo_move_to (cr, 
tx-ink_rect.x+logical_rect.x-ink_rect.width*orient[0], 
ty-ink_rect.y+logical_rect.y-ink_rect.height*orient[1]);
+//     pango_cairo_show_layout (cr, layout);
+       cairo_restore(cr);
+       if(1)
+       {
+               pango_layout_get_pixel_extents(layout, &ink_rect, 
&logical_rect);       
+               // Render logical and ink rectangles
+               cairo_save(cr);
+               cairo_set_source_rgb(cr, 0.0, 1.0, 0.0);
+               cairo_set_line_width(cr, 1.0);
+               cairo_rectangle(cr, tx+ink_rect.x-0.5, ty+ink_rect.y-0.5, 
ink_rect.width, ink_rect.height);
+               cairo_stroke(cr);
+               cairo_restore(cr);
+
+               cairo_save(cr);
+               cairo_set_line_width(cr, 1.0);
+               cairo_set_source_rgb(cr, 0.0, 0.0, 1.0);
+               cairo_rectangle(cr, tx+logical_rect.x-0.5, 
ty+logical_rect.y-0.5, logical_rect.width, logical_rect.height);
+               cairo_stroke(cr);
+               cairo_restore(cr);
+       }
+       // Destroy and return
+       pango_layout_iter_free(layoutiter);
+       cairo_destroy(cr);
+       g_object_unref (layout);
+       pango_font_description_free (font_description);
+       return true;
+}
+////
+
+
 synfig::Rect
 Layer_Freetype::get_bounding_rect()const
 {
diff --git a/synfig-core/src/modules/lyr_freetype/lyr_freetype.h 
b/synfig-core/src/modules/lyr_freetype/lyr_freetype.h
index 81261bc..9e001b4 100644
--- a/synfig-core/src/modules/lyr_freetype/lyr_freetype.h
+++ b/synfig-core/src/modules/lyr_freetype/lyr_freetype.h
@@ -146,6 +146,7 @@ public:
        virtual ValueBase get_param(const String & param)const;
        virtual Color get_color(Context context, const synfig::Point &pos)const;
        virtual bool accelerated_render(Context context,Surface *surface,int 
quality, const RendDesc &renddesc, ProgressCallback *cb)const;
+       virtual bool accelerated_cairorender(Context context,cairo_surface_t 
*surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const;
 
        virtual Vocab get_param_vocab()const;
 


------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
Synfig-devl mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/synfig-devl

Reply via email to