~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/xwt/Xwt.Mac/Xwt.Mac/TextLayoutBackendHandler.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// 
 
2
// TextLayoutBackendHandler.cs
 
3
//  
 
4
// Author:
 
5
//       Lluis Sanchez <lluis@xamarin.com>
 
6
// 
 
7
// Copyright (c) 2011 Xamarin Inc
 
8
// 
 
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
 
10
// of this software and associated documentation files (the "Software"), to deal
 
11
// in the Software without restriction, including without limitation the rights
 
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 
13
// copies of the Software, and to permit persons to whom the Software is
 
14
// furnished to do so, subject to the following conditions:
 
15
// 
 
16
// The above copyright notice and this permission notice shall be included in
 
17
// all copies or substantial portions of the Software.
 
18
// 
 
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 
25
// THE SOFTWARE.
 
26
 
 
27
using System;
 
28
using Xwt.Backends;
 
29
using MonoMac.AppKit;
 
30
using MonoMac.Foundation;
 
31
using MonoMac.CoreText;
 
32
using MonoMac.CoreGraphics;
 
33
using Xwt.Engine;
 
34
using Xwt.Drawing;
 
35
 
 
36
using PointF = System.Drawing.PointF;
 
37
using SizeF = System.Drawing.SizeF;
 
38
using RectangleF = System.Drawing.RectangleF;
 
39
 
 
40
namespace Xwt.Mac
 
41
{
 
42
        public class TextLayoutBackendHandler: ITextLayoutBackendHandler
 
43
        {
 
44
                class LayoutInfo
 
45
                {
 
46
                        public CTFramesetter Framesetter;
 
47
                        public string Text;
 
48
                        public NSFont Font;
 
49
                        public float? Width, Height;
 
50
                        public TextTrimming TextTrimming;
 
51
                }
 
52
                
 
53
                public object Create (Xwt.Drawing.Context context)
 
54
                {
 
55
                        return new LayoutInfo ();
 
56
                }
 
57
                
 
58
                public object Create (ICanvasBackend canvas)
 
59
                {
 
60
                        return new LayoutInfo {
 
61
                                Font = (NSFont)canvas.Font
 
62
                        };
 
63
                }
 
64
 
 
65
                public void SetText (object backend, string text)
 
66
                {
 
67
                        LayoutInfo li = (LayoutInfo)backend;
 
68
                        li.Text = text;
 
69
                        InvalidateFramesetter (li);
 
70
                }
 
71
 
 
72
                public void SetFont (object backend, Xwt.Drawing.Font font)
 
73
                {
 
74
                        LayoutInfo li = (LayoutInfo)backend;
 
75
                        li.Font = (NSFont)WidgetRegistry.GetBackend (font);
 
76
                        InvalidateFramesetter (li);
 
77
                }
 
78
                
 
79
                public void SetWidth (object backend, double value)
 
80
                {
 
81
                        LayoutInfo li = (LayoutInfo)backend;
 
82
                        li.Width = value < 0 ? null : (float?)value;
 
83
                }
 
84
                
 
85
                public void SetHeight (object backend, double value)
 
86
                {
 
87
                        LayoutInfo li = (LayoutInfo)backend;
 
88
                        li.Height = value < 0 ? null : (float?)value;
 
89
                }
 
90
                
 
91
                public void SetTrimming (object backend, TextTrimming value)
 
92
                {
 
93
                        LayoutInfo li = (LayoutInfo)backend;
 
94
                        li.TextTrimming = value;
 
95
                }
 
96
 
 
97
                public Size GetSize (object backend)
 
98
                {
 
99
                        LayoutInfo li = (LayoutInfo)backend;
 
100
                        if (li.Framesetter == null)
 
101
                                return Size.Zero;
 
102
 
 
103
                        NSRange unused;
 
104
                        SizeF constraint = new SizeF (li.Width ?? float.MaxValue, float.MaxValue);
 
105
                        return li.Framesetter.SuggestFrameSize (new NSRange (0, li.Text.Length), null, constraint, out unused).ToXwtSize ();
 
106
                }
 
107
 
 
108
                static void InvalidateFramesetter (LayoutInfo li)
 
109
                {
 
110
                        if (li.Framesetter != null) {
 
111
                                li.Framesetter.Dispose ();
 
112
                                li.Framesetter = null;
 
113
                        }
 
114
                        if (!string.IsNullOrEmpty (li.Text))
 
115
                                li.Framesetter = new CTFramesetter (CreateAttributedString (li));
 
116
                }
 
117
 
 
118
                static NSAttributedString CreateAttributedString (LayoutInfo li, string overrideText = null)
 
119
                {
 
120
                        NSDictionary dict;
 
121
                        if (li.Font != null) {
 
122
                                dict = NSDictionary.FromObjectsAndKeys (
 
123
                                        new object[] { li.Font, new NSNumber (true) },
 
124
                                        new object[] { CTStringAttributeKey.Font, CTStringAttributeKey.ForegroundColorFromContext }
 
125
                                );
 
126
                        } else {
 
127
                                dict = NSDictionary.FromObjectsAndKeys (
 
128
                                        new object[] { new NSNumber (true) },
 
129
                                        new object[] { CTStringAttributeKey.ForegroundColorFromContext }
 
130
                                );
 
131
                        }
 
132
                        return new NSAttributedString (overrideText ?? li.Text, dict);
 
133
                }
 
134
                
 
135
                internal static void Draw (CGContext ctx, object layout, double x, double y)
 
136
                {
 
137
                        LayoutInfo li = (LayoutInfo)layout;
 
138
                        if (li.Framesetter == null)
 
139
                                return;
 
140
 
 
141
                        CGPath path = new CGPath ();
 
142
                        path.AddRect (new RectangleF (0, 0, li.Width ?? float.MaxValue, li.Height ?? float.MaxValue));
 
143
                        CTFrame frame = li.Framesetter.GetFrame (new NSRange (0, li.Text.Length), path, null);
 
144
 
 
145
                        CTLine ellipsis = null;
 
146
                        bool ellipsize = li.Width.HasValue && li.TextTrimming == TextTrimming.WordElipsis;
 
147
                        if (ellipsize)
 
148
                                ellipsis = new CTLine (CreateAttributedString (li, "..."));
 
149
 
 
150
                        float lineHeight = layoutManager.DefaultLineHeightForFont (li.Font);
 
151
 
 
152
                        ctx.SaveState ();
 
153
                        ctx.TextMatrix = CGAffineTransform.MakeScale (1f, -1f);
 
154
                        ctx.TranslateCTM ((float)x, (float)y + li.Font.Ascender);
 
155
                        foreach (var line in frame.GetLines ()) {
 
156
                                ctx.TextPosition = PointF.Empty;
 
157
                                if (ellipsize) // we need to create a new CTLine here because the framesetter already truncated the text for the line
 
158
                                        new CTLine (CreateAttributedString (li, li.Text.Substring (line.StringRange.Location)))
 
159
                                                .GetTruncatedLine (li.Width.Value, CTLineTruncation.End, ellipsis).Draw (ctx);
 
160
                                else
 
161
                                        line.Draw (ctx);
 
162
                                ctx.TranslateCTM (0, lineHeight);
 
163
                        }
 
164
                        ctx.RestoreState ();
 
165
                }
 
166
 
 
167
                static readonly NSLayoutManager layoutManager = new NSLayoutManager ();
 
168
        }
 
169
}
 
170