[longomatch/newui: 88/104] Usa pango for text rendering and support text aligment



commit 5e0ced32721fdb254be0fa2ce9ca72746fed572f
Author: Andoni Morales Alastruey <ylatuya gmail com>
Date:   Tue Aug 26 12:15:09 2014 +0200

    Usa pango for text rendering and support text aligment

 LongoMatch.Core/Common/Enums.cs                    |    6 +
 .../Interfaces/Drawing/IDrawingToolkit.cs          |    1 +
 LongoMatch.Drawing.Cairo/CairoBackend.cs           |   99 ++++++++++++--------
 LongoMatch.Drawing.Cairo/CairoContext.cs           |   15 ++-
 .../LongoMatch.Drawing.Cairo.mdp                   |    1 +
 5 files changed, 80 insertions(+), 42 deletions(-)
---
diff --git a/LongoMatch.Core/Common/Enums.cs b/LongoMatch.Core/Common/Enums.cs
index 58bf685..51f9e52 100644
--- a/LongoMatch.Core/Common/Enums.cs
+++ b/LongoMatch.Core/Common/Enums.cs
@@ -192,6 +192,12 @@ namespace LongoMatch.Common
                Bold
        }
        
+       public enum FontAlignment {
+               Left,
+               Right,
+               Center,
+       }
+       
        public enum ButtonType {
                None,
                Left,
diff --git a/LongoMatch.Core/Interfaces/Drawing/IDrawingToolkit.cs 
b/LongoMatch.Core/Interfaces/Drawing/IDrawingToolkit.cs
index 20d7c2f..1a65abc 100644
--- a/LongoMatch.Core/Interfaces/Drawing/IDrawingToolkit.cs
+++ b/LongoMatch.Core/Interfaces/Drawing/IDrawingToolkit.cs
@@ -44,6 +44,7 @@ namespace LongoMatch.Interfaces.Drawing
                string FontFamily {set;}
                FontSlant FontSlant {set;}
                FontWeight FontWeight {set;}
+               FontAlignment FontAlignment {set;}
                int FontSize {set;}
                LineStyle LineStyle {set;}
                
diff --git a/LongoMatch.Drawing.Cairo/CairoBackend.cs b/LongoMatch.Drawing.Cairo/CairoBackend.cs
index 5516ecb..6e11dae 100644
--- a/LongoMatch.Drawing.Cairo/CairoBackend.cs
+++ b/LongoMatch.Drawing.Cairo/CairoBackend.cs
@@ -19,15 +19,15 @@ using System;
 using Cairo;
 using LongoMatch.Common;
 using LongoMatch.Interfaces.Drawing;
+using FontSlant = LongoMatch.Common.FontSlant;
+using FontWeight = LongoMatch.Common.FontWeight;
+using FontAlignment = LongoMatch.Common.FontAlignment;
 using Color = LongoMatch.Common.Color;
-using FontSlant = Cairo.FontSlant;
-using FontWeight = Cairo.FontWeight;
 using Image = LongoMatch.Common.Image;
-using LFontSlant = LongoMatch.Common.FontSlant;
-using LFontWeight = LongoMatch.Common.FontWeight;
 using LineStyle = LongoMatch.Common.LineStyle;
 using Point = LongoMatch.Common.Point;
 using Gdk;
+using Pango;
 
 namespace LongoMatch.Drawing.Cairo
 {
@@ -35,8 +35,9 @@ namespace LongoMatch.Drawing.Cairo
        {
                IContext context;
                Color savedStrokeColor, savedFillColor;
-               FontSlant fSlant, savedFSlant;
-               FontWeight fWeight, savedFWeight;
+               Style fSlant, savedFSlant;
+               Weight fWeight, savedFWeight;
+               Pango.Alignment fAlignment, savedAlignment;
                int savedLineWidth, savedFontSize;
                bool savedClear;
                LineStyle savedLineStyle;
@@ -50,9 +51,10 @@ namespace LongoMatch.Drawing.Cairo
                        LineWidth = 2;
                        FontSize = 12;
                        FontFamily = "Verdana";
-                       FontWeight = LFontWeight.Normal;
-                       FontSlant = LFontSlant.Normal;
+                       FontWeight = FontWeight.Normal;
+                       FontSlant = FontSlant.Normal;
                        LineStyle = LineStyle.Normal;
+                       FontAlignment = FontAlignment.Center;
                        ClearOperation = false;
                }
 
@@ -88,30 +90,46 @@ namespace LongoMatch.Drawing.Cairo
                        protected get;
                }
 
-               public LFontSlant FontSlant {
+               public FontSlant FontSlant {
                        set {
                                switch (value) {
-                               case LFontSlant.Italic:
-                                       fSlant = FontSlant.Italic;
+                               case FontSlant.Italic:
+                                       fSlant = Style.Italic;
                                        break;
-                               case LFontSlant.Normal:
-                                       fSlant = FontSlant.Normal;
+                               case FontSlant.Normal:
+                                       fSlant = Style.Normal;
                                        break;
-                               case LFontSlant.Oblique:
-                                       fSlant = FontSlant.Oblique;
+                               case FontSlant.Oblique:
+                                       fSlant = Style.Oblique;
                                        break;
                                }
                        }
                }
 
-               public LFontWeight FontWeight {
+               public FontWeight FontWeight {
                        set {
                                switch (value) {
-                               case LFontWeight.Bold:
-                                       fWeight = FontWeight.Bold;
+                               case FontWeight.Bold:
+                                       fWeight = Weight.Semibold;
                                        break;
-                               case LFontWeight.Normal:
-                                       fWeight = FontWeight.Normal;
+                               case FontWeight.Normal:
+                                       fWeight = Weight.Normal;
+                                       break;
+                               }
+                       }
+               }
+               
+               public FontAlignment FontAlignment {
+                       set {
+                               switch (value) {
+                               case FontAlignment.Left:
+                                       fAlignment = Pango.Alignment.Left;
+                                       break;
+                               case FontAlignment.Center:
+                                       fAlignment = Pango.Alignment.Center;
+                                       break;
+                               case FontAlignment.Right:
+                                       fAlignment = Pango.Alignment.Right;
                                        break;
                                }
                        }
@@ -152,6 +170,7 @@ namespace LongoMatch.Drawing.Cairo
                        savedFillColor = FillColor;
                        savedFSlant = fSlant;
                        savedFWeight = fWeight;
+                       savedAlignment = fAlignment;
                        savedLineWidth = LineWidth;
                        savedFontSize = FontSize;
                        savedFontFamily = FontFamily;
@@ -176,6 +195,7 @@ namespace LongoMatch.Drawing.Cairo
                        FillColor = savedFillColor;
                        fSlant = savedFSlant;
                        fWeight = savedFWeight;
+                       fAlignment = savedAlignment;
                        LineWidth = savedLineWidth;
                        FontSize = savedFontSize;
                        FontFamily = savedFontFamily;
@@ -318,28 +338,31 @@ namespace LongoMatch.Drawing.Cairo
 
                public void DrawText (Point point, double width, double height, string text)
                {
-                       TextExtents extents;
-                       FontExtents fextents;
-                       double x, y;
+                       Layout layout;
+                       Pango.Rectangle inkRect, logRect;
                        
                        if (text == null) {
                                return;
                        }
+
+                       layout = (context as CairoContext).PangoLayout;
+                       layout.FontDescription = FontDescription.FromString (
+                               String.Format ("{0} {1}px", FontFamily, FontSize));
+                       layout.FontDescription.Weight = fWeight;
+                       layout.FontDescription.Style = fSlant;
+                       layout.Width = Pango.Units.FromPixels ((int) width);
+                       layout.Alignment = fAlignment;
+                       layout.SetMarkup (text); 
                        SetColor (StrokeColor);
-                       CContext.SelectFontFace (FontFamily, fSlant, fWeight);
-                       CContext.SetFontSize (FontSize);
-                       extents = CContext.TextExtents (text);
-                       fextents = CContext.FontExtents;
-                       x = point.X + width / 2 - (extents.Width / 2 + extents.XBearing);
-                       y = point.Y + height / 2 - (extents.Height / 2 + extents.YBearing);
-                       CContext.MoveTo (x, y);
-                       CContext.ShowText (text);
-                       StrokeAndFill ();
+                       Pango.CairoHelper.UpdateLayout (CContext, layout);
+                       layout.GetPixelExtents (out inkRect, out logRect);
+                       CContext.MoveTo (point.X, point.Y + height / 2 - (double)logRect.Height / 2);
+                       Pango.CairoHelper.ShowLayout (CContext, layout);
                }
 
                public void DrawImage (Image image)
                {
-                       CairoHelper.SetSourcePixbuf (CContext, image.Value, 0, 0);
+                       Gdk.CairoHelper.SetSourcePixbuf (CContext, image.Value, 0, 0);
                        CContext.Paint ();
                }
 
@@ -358,7 +381,7 @@ namespace LongoMatch.Drawing.Cairo
                        CContext.Save ();
                        CContext.Translate (start.X + offset.X, start.Y + offset.Y);
                        CContext.Scale (scaleX, scaleY);
-                       CairoHelper.SetSourcePixbuf (CContext, image.Value, 0, 0);
+                       Gdk.CairoHelper.SetSourcePixbuf (CContext, image.Value, 0, 0);
                        CContext.Paint ();
                        CContext.Restore ();
                }
@@ -418,7 +441,7 @@ namespace LongoMatch.Drawing.Cairo
                        
                        pm = new Pixmap (null, (int)width, (int)height, 24);
                        disableScalling = true;
-                       using (CairoContext c = new CairoContext (CairoHelper.Create (pm))) {
+                       using (CairoContext c = new CairoContext (Gdk.CairoHelper.Create (pm))) {
                                canvas.Draw (c, new Area (new Point (0, 0), width, height));
                        }
                        img = new Image (Gdk.Pixbuf.FromDrawable (pm, Colormap.System, 0, 0, 0, 0,
@@ -432,7 +455,7 @@ namespace LongoMatch.Drawing.Cairo
                {
                        ImageSurface pngSurface = new ImageSurface (Format.ARGB32, (int)width, (int)height);
                        disableScalling = true;
-                       using (CairoContext c = new CairoContext (new Context(pngSurface))) {
+                       using (CairoContext c = new CairoContext (new global::Cairo.Context(pngSurface))) {
                                canvas.Draw (c, new Area (new Point (0, 0), width, height));
                        }
                        pngSurface.WriteToPng (filename);
@@ -440,9 +463,9 @@ namespace LongoMatch.Drawing.Cairo
                        pngSurface.Dispose ();
                }
 
-               Context CContext {
+               global::Cairo.Context CContext {
                        get {
-                               return context.Value as Context;
+                               return context.Value as global::Cairo.Context;
                        }
                }
 
diff --git a/LongoMatch.Drawing.Cairo/CairoContext.cs b/LongoMatch.Drawing.Cairo/CairoContext.cs
index 96ec732..7559177 100644
--- a/LongoMatch.Drawing.Cairo/CairoContext.cs
+++ b/LongoMatch.Drawing.Cairo/CairoContext.cs
@@ -18,6 +18,7 @@
 using Gdk;
 using Cairo;
 using LongoMatch.Interfaces.Drawing;
+using Pango;
 
 namespace LongoMatch.Drawing.Cairo
 {
@@ -25,15 +26,16 @@ namespace LongoMatch.Drawing.Cairo
        {
                public CairoContext (Drawable window)
                {
-                       Value = CairoHelper.Create (window);
+                       Value = Gdk.CairoHelper.Create (window);
+                       PangoLayout = Pango.CairoHelper.CreateLayout (Value as global::Cairo.Context);
                }
 
                public CairoContext (global::Cairo.Surface surface)
                {
-                       Value = new Context (surface);
+                       Value = new global::Cairo.Context (surface);
                }
 
-               public CairoContext (Context context)
+               public CairoContext (global::Cairo.Context context)
                {
                        Value = context;
                }
@@ -42,10 +44,15 @@ namespace LongoMatch.Drawing.Cairo
                        get;
                        protected set;
                }
+               
+               public Layout PangoLayout {
+                       get;
+                       protected set;
+               }
 
                public void Dispose ()
                {
-                       (Value as Context).Dispose ();
+                       (Value as global::Cairo.Context).Dispose ();
                }
        }
 }
diff --git a/LongoMatch.Drawing.Cairo/LongoMatch.Drawing.Cairo.mdp 
b/LongoMatch.Drawing.Cairo/LongoMatch.Drawing.Cairo.mdp
index e756939..a3fbff2 100644
--- a/LongoMatch.Drawing.Cairo/LongoMatch.Drawing.Cairo.mdp
+++ b/LongoMatch.Drawing.Cairo/LongoMatch.Drawing.Cairo.mdp
@@ -35,5 +35,6 @@
     <ProjectReference type="Package" localcopy="False" refto="gtk-sharp, Version=2.12.0.0, Culture=neutral, 
PublicKeyToken=35e10195dab3c99f" />
     <ProjectReference type="Package" localcopy="False" refto="gdk-sharp, Version=2.12.0.0, Culture=neutral, 
PublicKeyToken=35e10195dab3c99f" />
     <ProjectReference type="Package" localcopy="False" refto="atk-sharp, Version=2.12.0.0, Culture=neutral, 
PublicKeyToken=35e10195dab3c99f" />
+    <ProjectReference type="Package" localcopy="False" refto="pango-sharp, Version=2.12.0.0, 
Culture=neutral, PublicKeyToken=35e10195dab3c99f" />
   </References>
 </Project>
\ No newline at end of file


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]