5
// Mike Krüger <mkrueger@novell.com>
7
// Copyright (c) 2010 Novell, Inc (http://www.novell.com)
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:
16
// The above copyright notice and this permission notice shall be included in
17
// all copies or substantial portions of the Software.
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
28
using System.Collections.Generic;
31
using MonoDevelop.Core;
34
namespace MonoDevelop.Ide.Fonts
36
public static class FontService
38
static List<FontDescriptionCodon> fontDescriptions = new List<FontDescriptionCodon> ();
39
static Dictionary<string, FontDescription> loadedFonts = new Dictionary<string, FontDescription> ();
40
static Properties fontProperties;
42
public static IEnumerable<FontDescriptionCodon> FontDescriptions {
44
return fontDescriptions;
50
fontProperties = PropertyService.Get ("FontProperties", new Properties ());
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);
58
case ExtensionChange.Remove:
59
fontDescriptions.Remove (codon);
60
if (loadedFonts.ContainsKey (codon.Name))
61
loadedFonts.Remove (codon.Name);
67
static FontDescription LoadFont (string name)
69
return Pango.FontDescription.FromString (FilterFontName (name));
72
public static string FilterFontName (string name)
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);
86
public static string GetUnderlyingFontName (string name)
88
var result = fontProperties.Get<string> (name);
91
var font = GetFont (name);
93
throw new InvalidOperationException ("Font " + name + " not found.");
94
return font.FontDescription;
99
public static FontDescription GetFontDescription (string name)
101
if (loadedFonts.ContainsKey (name))
102
return loadedFonts [name];
103
return loadedFonts [name] = LoadFont (GetUnderlyingFontName (name));
106
public static FontDescriptionCodon GetFont (string name)
108
foreach (var d in fontDescriptions) {
112
LoggingService.LogError ("Font " + name + " not found.");
116
public static void SetFont (string name, string value)
118
if (loadedFonts.ContainsKey (name))
119
loadedFonts.Remove (name);
120
fontProperties.Set (name, value);
122
if (fontChangeCallbacks.ContainsKey (name))
123
fontChangeCallbacks [name].ForEach (c => c ());
126
static Dictionary<string, List<Action>> fontChangeCallbacks = new Dictionary<string, List<Action>> ();
127
public static void RegisterFontChangedCallback (string fontName, Action callback)
129
if (!fontChangeCallbacks.ContainsKey (fontName))
130
fontChangeCallbacks [fontName] = new List<Action> ();
131
fontChangeCallbacks [fontName].Add (callback);
134
public static void RemoveCallback (Action callback)
136
foreach (var list in fontChangeCallbacks.Values)
137
list.Remove (callback);
b'\\ No newline at end of file'