~ubuntu-branches/ubuntu/oneiric/monodevelop/oneiric

« back to all changes in this revision

Viewing changes to src/core/MonoDevelop.Ide/MonoDevelop.Ide.Fonts/FontService.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2011-06-27 17:03:13 UTC
  • mto: (1.8.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 54.
  • Revision ID: james.westby@ubuntu.com-20110627170313-6cvz3s19x6e9hqe9
Import upstream version 2.5.92+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// 
 
2
// FontService.cs
 
3
//  
 
4
// Author:
 
5
//       Mike Krüger <mkrueger@novell.com>
 
6
// 
 
7
// Copyright (c) 2010 Novell, Inc (http://www.novell.com)
 
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 System.Collections.Generic;
 
29
using System.Linq;
 
30
using Mono.Addins;
 
31
using MonoDevelop.Core;
 
32
using Pango;
 
33
 
 
34
namespace MonoDevelop.Ide.Fonts
 
35
{
 
36
        public static class FontService
 
37
        {
 
38
                static List<FontDescriptionCodon> fontDescriptions = new List<FontDescriptionCodon> ();
 
39
                static Dictionary<string, FontDescription> loadedFonts = new Dictionary<string, FontDescription> ();
 
40
                static Properties fontProperties;
 
41
                
 
42
                public static IEnumerable<FontDescriptionCodon> FontDescriptions {
 
43
                        get {
 
44
                                return fontDescriptions;
 
45
                        }
 
46
                }
 
47
                
 
48
                static FontService ()
 
49
                {
 
50
                        fontProperties = PropertyService.Get ("FontProperties", new Properties ());
 
51
                        
 
52
                        AddinManager.AddExtensionNodeHandler ("/MonoDevelop/Ide/Fonts", delegate(object sender, ExtensionNodeEventArgs args) {
 
53
                                var codon = (FontDescriptionCodon)args.ExtensionNode;
 
54
                                switch (args.Change) {
 
55
                                case ExtensionChange.Add:
 
56
                                        fontDescriptions.Add (codon);
 
57
                                        break;
 
58
                                case ExtensionChange.Remove:
 
59
                                        fontDescriptions.Remove (codon);
 
60
                                        if (loadedFonts.ContainsKey (codon.Name))
 
61
                                                loadedFonts.Remove (codon.Name);
 
62
                                        break;
 
63
                                }
 
64
                        }); 
 
65
                }
 
66
                
 
67
                static FontDescription LoadFont (string name)
 
68
                {
 
69
                        return Pango.FontDescription.FromString (FilterFontName (name));
 
70
                }
 
71
                
 
72
                public static string FilterFontName (string name)
 
73
                {
 
74
                        if (name == "_DEFAULT_MONOSPACE")
 
75
                                return DesktopService.DefaultMonospaceFont;
 
76
                        if (name == "_DEFAULT_SANS") {
 
77
                                var label = new Gtk.Label ("");
 
78
                                string result = label.Style.FontDescription.Family + " " + ((int)label.Style.FontDesc.Size / Pango.Scale.PangoScale);
 
79
                                label.Destroy ();
 
80
                                return result;
 
81
                        }
 
82
                        
 
83
                        return name;
 
84
                }
 
85
                
 
86
                public static string GetUnderlyingFontName (string name)
 
87
                {
 
88
                        var result = fontProperties.Get<string> (name);
 
89
                        
 
90
                        if (result == null) {
 
91
                                var font = GetFont (name);
 
92
                                if (font == null)
 
93
                                        throw new InvalidOperationException ("Font " + name + " not found.");
 
94
                                return font.FontDescription;
 
95
                        }
 
96
                        return result;
 
97
                }
 
98
                
 
99
                public static FontDescription GetFontDescription (string name)
 
100
                {
 
101
                        if (loadedFonts.ContainsKey (name))
 
102
                                return loadedFonts [name];
 
103
                        return loadedFonts [name] = LoadFont (GetUnderlyingFontName (name));
 
104
                }
 
105
                
 
106
                public static FontDescriptionCodon GetFont (string name)
 
107
                {
 
108
                        foreach (var d in fontDescriptions) {
 
109
                                if (d.Name == name)
 
110
                                        return d;
 
111
                        }
 
112
                        LoggingService.LogError ("Font " + name + " not found.");
 
113
                        return null;
 
114
                }
 
115
                
 
116
                public static void SetFont (string name, string value)
 
117
                {
 
118
                        if (loadedFonts.ContainsKey (name)) 
 
119
                                loadedFonts.Remove (name);
 
120
                        fontProperties.Set (name, value);
 
121
                        
 
122
                        if (fontChangeCallbacks.ContainsKey (name)) 
 
123
                                fontChangeCallbacks [name].ForEach (c => c ());
 
124
                }
 
125
                
 
126
                static Dictionary<string, List<Action>> fontChangeCallbacks = new Dictionary<string, List<Action>> ();
 
127
                public static void RegisterFontChangedCallback (string fontName, Action callback)
 
128
                {
 
129
                        if (!fontChangeCallbacks.ContainsKey (fontName))
 
130
                                fontChangeCallbacks [fontName] = new List<Action> ();
 
131
                        fontChangeCallbacks [fontName].Add (callback);
 
132
                }
 
133
                
 
134
                public static void RemoveCallback (Action callback)
 
135
                {
 
136
                        foreach (var list in fontChangeCallbacks.Values)
 
137
                                list.Remove (callback);
 
138
                }
 
139
        }
 
140
}
 
 
b'\\ No newline at end of file'