[longomatch] Center text in blackboard



commit 904ffcef13083a516c4d49e6b01fa18b6597050b
Author: Andoni Morales Alastruey <ylatuya gmail com>
Date:   Tue Oct 21 01:49:46 2014 +0200

    Center text in blackboard

 .../Interfaces/Drawing/IDrawingToolkit.cs          |    2 +
 LongoMatch.Drawing.Cairo/CairoBackend.cs           |   46 +++++++++++++++-----
 LongoMatch.Drawing/Widgets/Blackboard.cs           |   17 +++----
 3 files changed, 45 insertions(+), 20 deletions(-)
---
diff --git a/LongoMatch.Core/Interfaces/Drawing/IDrawingToolkit.cs 
b/LongoMatch.Core/Interfaces/Drawing/IDrawingToolkit.cs
index b3d10fa..e40c1da 100644
--- a/LongoMatch.Core/Interfaces/Drawing/IDrawingToolkit.cs
+++ b/LongoMatch.Core/Interfaces/Drawing/IDrawingToolkit.cs
@@ -72,6 +72,8 @@ namespace LongoMatch.Core.Interfaces.Drawing
                Image Copy (ICanvas canvas, double width, double height);
                Area UserToDevice (Area area);
                void Invoke (EventHandler handler);
+               void MeasureText(string text, out int width, out int height,
+                                string fontFamily, int fontSize, FontWeight fontWeight);
        }
 }
 
diff --git a/LongoMatch.Drawing.Cairo/CairoBackend.cs b/LongoMatch.Drawing.Cairo/CairoBackend.cs
index f08e78e..5600cee 100644
--- a/LongoMatch.Drawing.Cairo/CairoBackend.cs
+++ b/LongoMatch.Drawing.Cairo/CairoBackend.cs
@@ -43,6 +43,7 @@ namespace LongoMatch.Drawing.Cairo
                LineStyle savedLineStyle;
                string savedFontFamily;
                bool disableScalling;
+               Layout layout;
 
                public CairoBackend ()
                {
@@ -55,6 +56,10 @@ namespace LongoMatch.Drawing.Cairo
                        FontSlant = FontSlant.Normal;
                        LineStyle = LineStyle.Normal;
                        FontAlignment = FontAlignment.Center;
+                       if (layout == null) {
+                               layout = new Layout (Gdk.PangoHelper.ContextGet());
+                       }
+
                        ClearOperation = false;
                }
 
@@ -111,17 +116,7 @@ namespace LongoMatch.Drawing.Cairo
 
                public FontWeight FontWeight {
                        set {
-                               switch (value) {
-                               case FontWeight.Light:
-                                       fWeight = Weight.Light;
-                                       break;
-                               case FontWeight.Bold:
-                                       fWeight = Weight.Semibold;
-                                       break;
-                               case FontWeight.Normal:
-                                       fWeight = Weight.Normal;
-                                       break;
-                               }
+                               fWeight = WeightToPangoWeight (value);
                        }
                }
 
@@ -549,6 +544,35 @@ namespace LongoMatch.Drawing.Cairo
                public void Invoke (EventHandler handler) {
                        Gtk.Application.Invoke (handler);
                }
+               
+               public void MeasureText(string text, out int width, out int height,
+                                       string fontFamily, int fontSize, FontWeight fontWeight) {
+                       FontDescription desc =  new FontDescription();
+                       desc.Family = fontFamily;
+                       desc.Size = Pango.Units.FromPixels (fontSize);
+                       desc.Weight = WeightToPangoWeight (fontWeight);
+                       layout.FontDescription = desc;
+                       layout.SetMarkup (GLib.Markup.EscapeText (text));
+                       layout.GetPixelSize (out width, out height);
+               }
+
+               Weight WeightToPangoWeight (FontWeight value)
+               {
+                       Weight weight = Weight.Normal;
+
+                       switch (value) {
+                       case FontWeight.Light:
+                               weight = Weight.Light;
+                               break;
+                       case FontWeight.Bold:
+                               weight = Weight.Semibold;
+                               break;
+                       case FontWeight.Normal:
+                               weight = Weight.Normal;
+                               break;
+                       }
+                       return weight;
+               }
        }
 }
 
diff --git a/LongoMatch.Drawing/Widgets/Blackboard.cs b/LongoMatch.Drawing/Widgets/Blackboard.cs
index 3ec08ea..57bb62e 100644
--- a/LongoMatch.Drawing/Widgets/Blackboard.cs
+++ b/LongoMatch.Drawing/Widgets/Blackboard.cs
@@ -15,16 +15,12 @@
 //  along with this program; if not, write to the Free Software
 //  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
 //
-using System;
 using System.Linq;
-using System.Runtime.Remoting;
 using LongoMatch.Core.Common;
+using LongoMatch.Core.Handlers;
 using LongoMatch.Core.Interfaces.Drawing;
 using LongoMatch.Core.Store;
-using LongoMatch.Drawing.CanvasObjects;
 using LongoMatch.Core.Store.Drawables;
-using LongoMatch.Core.Handlers;
-using LongoMatch.Core.Interfaces;
 
 namespace LongoMatch.Drawing.Widgets
 {
@@ -97,7 +93,7 @@ namespace LongoMatch.Drawing.Widgets
                        get;
                        set;
                }
-               
+
                public LineStyle LineStyle {
                        get;
                        set;
@@ -222,15 +218,18 @@ namespace LongoMatch.Drawing.Widgets
                        case DrawTool.Text:
                        case DrawTool.Player:
                                {
-                                       Text text = new Text (start, 1, FontSize * 1.5, "");
+                                       int width, heigth;
+                                       Text text = new Text (start, 1, 1, "");
                                        if (ConfigureObjectEvent != null) {
                                                ConfigureObjectEvent (text, Tool);
                                        }
                                        if (text.Value == null) {
                                                return;
                                        }
-                                       text.TopRight.X += text.Value.Length * FontSize;
-                                       text.BottomRight.X += text.Value.Length * FontSize;
+                                       Config.DrawingToolkit.MeasureText (text.Value, out width, out heigth,
+                                                                  "Ubuntu", FontSize, FontWeight.Normal);
+                                       text.Update (new Point (start.X - width / 2, start.Y - heigth / 2),
+                                            width, heigth);
                                        text.TextColor = TextColor.Copy ();
                                        text.FillColor = text.StrokeColor = TextBackgroundColor.Copy ();
                                        text.TextSize = FontSize;


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