summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.iOS/Renderers/FontExtensions.cs
blob: 2ddd11c8fa568c1c71cc49bbfb4506a52d25a0f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Xamarin.Forms;
using Xamarin.Forms.Internals;
#if __MOBILE__
using UIKit;
namespace Xamarin.Forms.Platform.iOS
#else
using AppKit;
using UIFont = AppKit.NSFont;

namespace Xamarin.Forms.Platform.MacOS
#endif
{
	public static class FontExtensions
	{
		static readonly Dictionary<ToUIFontKey, UIFont> ToUiFont = new Dictionary<ToUIFontKey, UIFont>();
#if __MOBILE__
		public static UIFont ToUIFont(this Font self)

#else
		public static UIFont ToNSFont(this Font self)
#endif
		{
			var size = (float)self.FontSize;
			if (self.UseNamedSize)
			{
				switch (self.NamedSize)
				{
					case NamedSize.Micro:
						size = 12;
						break;
					case NamedSize.Small:
						size = 14;
						break;
					case NamedSize.Medium:
						size = 17; // as defined by iOS documentation
						break;
					case NamedSize.Large:
						size = 22;
						break;
					default:
						size = 17;
						break;
				}
			}

			var bold = self.FontAttributes.HasFlag(FontAttributes.Bold);
			var italic = self.FontAttributes.HasFlag(FontAttributes.Italic);

			if (self.FontFamily != null)
			{
				try
				{
#if __MOBILE__
					if (UIFont.FamilyNames.Contains(self.FontFamily))
					{
						var descriptor = new UIFontDescriptor().CreateWithFamily(self.FontFamily);

						if (bold || italic)
						{
							var traits = (UIFontDescriptorSymbolicTraits)0;
							if (bold)
								traits = traits | UIFontDescriptorSymbolicTraits.Bold;
							if (italic)
								traits = traits | UIFontDescriptorSymbolicTraits.Italic;

							descriptor = descriptor.CreateWithTraits(traits);
							return UIFont.FromDescriptor(descriptor, size);
						}
					}

					return UIFont.FromName(self.FontFamily, size);

#else

					var descriptor = new NSFontDescriptor().FontDescriptorWithFamily(self.FontFamily);

					if (bold || italic)
					{
						var traits = (NSFontSymbolicTraits)0;
						if (bold)
							traits = traits | NSFontSymbolicTraits.BoldTrait;
						if (italic)
							traits = traits | NSFontSymbolicTraits.ItalicTrait;

						descriptor = descriptor.FontDescriptorWithSymbolicTraits(traits);
						return NSFont.FromDescription(descriptor, size);
					}

					return NSFont.FromFontName(self.FontFamily, size);
#endif
				}
				catch
				{
					Debug.WriteLine("Could not load font named: {0}", self.FontFamily);
				}
			}
			if (bold && italic)
			{
				var defaultFont = UIFont.SystemFontOfSize(size);
#if __MOBILE__
				var descriptor = defaultFont.FontDescriptor.CreateWithTraits(UIFontDescriptorSymbolicTraits.Bold | UIFontDescriptorSymbolicTraits.Italic);
				return UIFont.FromDescriptor(descriptor, 0);
			}
			if (italic)
				return UIFont.ItalicSystemFontOfSize(size);
#else
				var descriptor = defaultFont.FontDescriptor.FontDescriptorWithSymbolicTraits(
					NSFontSymbolicTraits.BoldTrait |
					NSFontSymbolicTraits.ItalicTrait);

				return NSFont.FromDescription(descriptor, 0);
			}
			if (italic)
			{
				Debug.WriteLine("Italic font requested, passing regular one");
				return NSFont.UserFontOfSize(size);
			}
#endif

			if (bold)
				return UIFont.BoldSystemFontOfSize(size);

			return UIFont.SystemFontOfSize(size);
		}

		internal static bool IsDefault(this Span self)
		{
			return self.FontFamily == null && self.FontSize == Device.GetNamedSize(NamedSize.Default, typeof(Label), true) &&
					self.FontAttributes == FontAttributes.None;
		}

#if __MOBILE__
		internal static UIFont ToUIFont(this Label label)
#else
		internal static UIFont ToNSFont(this Label label)
#endif
		{
			var values = label.GetValues(Label.FontFamilyProperty, Label.FontSizeProperty, Label.FontAttributesProperty);
			return ToUIFont((string)values[0], (float)(double)values[1], (FontAttributes)values[2]) ??
					UIFont.SystemFontOfSize(UIFont.LabelFontSize);
		}

#if __MOBILE__
		internal static UIFont ToUIFont(this IFontElement element)
#else
		internal static NSFont ToNSFont(this IFontElement element)
#endif
		{
			return ToUIFont(element.FontFamily, (float)element.FontSize, element.FontAttributes);
		}

		static UIFont _ToUIFont(string family, float size, FontAttributes attributes)
		{
			var bold = (attributes & FontAttributes.Bold) != 0;
			var italic = (attributes & FontAttributes.Italic) != 0;

			if (family != null)
			{
				try
				{
					UIFont result;
#if __MOBILE__
					if (UIFont.FamilyNames.Contains(family))
					{
						var descriptor = new UIFontDescriptor().CreateWithFamily(family);

						if (bold || italic)
						{
							var traits = (UIFontDescriptorSymbolicTraits)0;
							if (bold)
								traits = traits | UIFontDescriptorSymbolicTraits.Bold;
							if (italic)
								traits = traits | UIFontDescriptorSymbolicTraits.Italic;

							descriptor = descriptor.CreateWithTraits(traits);
							result = UIFont.FromDescriptor(descriptor, size);
							if (result != null)
								return result;
						}
					}

					result = UIFont.FromName(family, size);
#else

					var descriptor = new NSFontDescriptor().FontDescriptorWithFamily(family);

					if (bold || italic)
					{
						var traits = (NSFontSymbolicTraits)0;
						if (bold)
							traits = traits | NSFontSymbolicTraits.BoldTrait;
						if (italic)
							traits = traits | NSFontSymbolicTraits.ItalicTrait;

						descriptor = descriptor.FontDescriptorWithSymbolicTraits(traits);
						result = NSFont.FromDescription(descriptor, size);
						if (result != null)
							return result;
					}

					result = NSFont.FromFontName(family, size);
#endif
					if (result != null)
						return result;
				}
				catch
				{
					Debug.WriteLine("Could not load font named: {0}", family);
				}
			}

			if (bold && italic)
			{
				var defaultFont = UIFont.SystemFontOfSize(size);

#if __MOBILE__
				var descriptor = defaultFont.FontDescriptor.CreateWithTraits(UIFontDescriptorSymbolicTraits.Bold | UIFontDescriptorSymbolicTraits.Italic);
				return UIFont.FromDescriptor(descriptor, 0);
			}
			if (italic)
				return UIFont.ItalicSystemFontOfSize(size);
#else
				var descriptor = defaultFont.FontDescriptor.FontDescriptorWithSymbolicTraits(
					NSFontSymbolicTraits.BoldTrait |
					NSFontSymbolicTraits.ItalicTrait);

				return NSFont.FromDescription(descriptor, 0);
			}
			if (italic)
			{
				Debug.WriteLine("Italic font requested, passing regular one");
				return NSFont.UserFontOfSize(size);
			}
#endif
			if (bold)
				return UIFont.BoldSystemFontOfSize(size);

			return UIFont.SystemFontOfSize(size);
		}

		static UIFont ToUIFont(string family, float size, FontAttributes attributes)
		{
			var key = new ToUIFontKey(family, size, attributes);

			lock (ToUiFont)
			{
				UIFont value;
				if (ToUiFont.TryGetValue(key, out value))
					return value;
			}

			var generatedValue = _ToUIFont(family, size, attributes);

			lock (ToUiFont)
			{
				UIFont value;
				if (!ToUiFont.TryGetValue(key, out value))
					ToUiFont.Add(key, value = generatedValue);
				return value;
			}
		}

		struct ToUIFontKey
		{
			internal ToUIFontKey(string family, float size, FontAttributes attributes)
			{
				_family = family;
				_size = size;
				_attributes = attributes;
			}
#pragma warning disable 0414 // these are not called explicitly, but they are used to establish uniqueness. allow it!
			string _family;
			float _size;
			FontAttributes _attributes;
#pragma warning restore 0414
		}
	}
}