-
Notifications
You must be signed in to change notification settings - Fork 559
Expand file tree
/
Copy pathFontTest.cs
More file actions
218 lines (187 loc) · 6.51 KB
/
FontTest.cs
File metadata and controls
218 lines (187 loc) · 6.51 KB
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
//
// Unit tests for CTFont
//
// Authors:
// Sebastien Pouliot <sebastien@xamarin.com>
//
// Copyright 2015 Xamarin Inc. All rights reserved.
//
using System.Linq;
using CoreGraphics;
using CoreText;
#if MONOMAC
using AppKit;
#else
using UIKit;
#endif
namespace MonoTouchFixtures.CoreText {
[TestFixture]
[Preserve (AllMembers = true)]
public class FontTest {
[Test]
public void CTFontCreateWithNameAndOptions ()
{
TestRuntime.AssertXcodeVersion (5, 0);
using (var font = new CTFont ("HoeflerText-Regular", 10, CTFontOptions.Default)) {
Assert.That (font.Handle, Is.Not.EqualTo (IntPtr.Zero), "Handle");
if (TestRuntime.CheckXcodeVersion (11, 0))
Assert.That (font.HasTable (CTFontTable.ScalableVectorGraphics), Is.EqualTo (false), "HasTable");
}
}
[Test]
public void CTFontCreateWithFontDescriptorAndOptions ()
{
TestRuntime.AssertXcodeVersion (5, 0);
CTFontDescriptorAttributes fda = new CTFontDescriptorAttributes () {
FamilyName = "Courier",
StyleName = "Bold",
Size = 16.0f
};
using (var fd = new CTFontDescriptor (fda))
using (var font = new CTFont (fd, 10, CTFontOptions.Default)) {
Assert.That (font.Handle, Is.Not.EqualTo (IntPtr.Zero), "Handle");
}
}
[Test]
public void GetCascadeList ()
{
TestRuntime.AssertXcodeVersion (5, 0);
using (var font = new CTFont ("HoeflerText-Regular", 10, CTFontOptions.Default)) {
Assert.NotNull (font.GetDefaultCascadeList (null), "null");
}
}
[Test]
public void GetLocalizedName ()
{
TestRuntime.AssertXcodeVersion (5, 0);
using (var font = new CTFont ("HoeflerText-Regular", 10, CTFontOptions.Default)) {
Assert.NotNull (font.GetLocalizedName (CTFontNameKey.Copyright), "1");
// We need to check if we are using english as our main language since this is the known case
// that the following code works. It fails with spanish for example but it is a false positive
// because the localized name for this font Full option does not have a spanish representation
var language = NSLocale.PreferredLanguages [0];
if (language == "en") {
string str;
Assert.NotNull (font.GetLocalizedName (CTFontNameKey.Full, out str), "2");
Assert.NotNull (str, "out str");
}
}
}
[Test]
public void GetGlyphsForCharacters_35048 ()
{
using (var font = CreateAppleColorEmojiFont ())
using (var ctfont = font.ToCTFont ((nfloat) 10.0)) {
ushort [] gid = new ushort [2];
Assert.True (ctfont.GetGlyphsForCharacters ("\ud83d\ude00".ToCharArray (), gid), "GetGlyphsForCharacters");
Assert.That (gid [0], Is.Not.EqualTo (0), "0");
Assert.That (gid [1], Is.EqualTo (0), "1");
}
}
[Test]
public void CTFontCreateForString ()
{
TestRuntime.AssertXcodeVersion (5, 0);
using (var f1 = new CTFont ("HoeflerText-Regular", 10, CTFontOptions.Default))
using (var f2 = f1.ForString ("xamarin", new NSRange (0, 3))) {
Assert.That (f2.Handle, Is.Not.EqualTo (IntPtr.Zero), "Handle");
}
}
[Test]
public void CTFontCreateForStringWithLanguage ()
{
TestRuntime.AssertXcodeVersion (11, 0);
using (var f1 = new CTFont ("HoeflerText-Regular", 10, CTFontOptions.Default)) {
using (var f2 = f1.ForString ("xamarin", new NSRange (0, 3), null))
Assert.That (f2.Handle, Is.Not.EqualTo (IntPtr.Zero), "f2");
using (var f3 = f1.ForString ("xamarin", new NSRange (0, 3), "FR"))
Assert.That (f3.Handle, Is.Not.EqualTo (IntPtr.Zero), "f3");
}
}
static CGFont CreateAppleColorEmojiFont ()
{
var font = CGFont.CreateWithFontName ("AppleColorEmoji");
if (font is null)
Assert.Ignore ("Unable to create the 'AppleColorEmoji' font.");
return font;
}
[Test]
public void CTFontCopyNameForGlyph ()
{
TestRuntime.AssertXcodeVersion (12, 0);
using (var ctfont = new CTFont ("HoeflerText-Regular", 10, CTFontOptions.Default))
Assert.That (ctfont.GetGlyphName ((ushort) 65), Is.EqualTo ("asciicircum"), "1");
using (var font = CreateAppleColorEmojiFont ())
using (var ctfont = font.ToCTFont ((nfloat) 10.0))
Assert.Null (ctfont.GetGlyphName ('\ud83d'), "2");
}
[Test]
public void DrawImage ()
{
TestRuntime.AssertXcodeVersion (16, 0);
using var font = new CTFont ("HoeflerText-Regular", 10, CTFontOptions.Default);
using var provider = new AdaptiveImageProvider ();
using var space = CGColorSpace.CreateDeviceRGB ();
using var context = new CGBitmapContext (null, 10, 10, 8, 40, space, CGBitmapFlags.PremultipliedLast);
font.DrawImage (provider, CGPoint.Empty, context);
Assert.AreEqual (1, provider.Count, "#Count");
}
[Test]
public void GetTypographicBoundsForAdaptiveImageProvider ()
{
TestRuntime.AssertXcodeVersion (16, 0);
using var font = new CTFont ("HoeflerText-Regular", 10, CTFontOptions.Default);
using var provider = new AdaptiveImageProvider ();
var bounds = font.GetTypographicBoundsForAdaptiveImageProvider (provider);
var candidates = new object [] {
new CGRect (0, -3.90625, 13, 16.40625),
new CGRect (0, -3.90625, 35, 16.40625)
};
Assert.That (bounds, Is.AnyOf (candidates).Using<CGRect> ((x, y) => x == y), "Bounds");
Assert.AreEqual (0, provider.Count, "#Count");
}
[Test]
public void GetAttribute ()
{
using (var font = new CTFont ("HoeflerText-Regular", 10, CTFontOptions.Default)) {
using (var name = font.GetAttribute (CTFontDescriptorAttributeKey.Name)) {
Assert.NotNull (name, "Name");
}
}
}
class AdaptiveImageProvider : NSObject, ICTAdaptiveImageProviding {
public int Count;
public CGImage? GetImage (CGSize proposedSize, nfloat scaleFactor, out CGPoint imageOffset, out CGSize imageSize)
{
imageOffset = default (CGPoint);
imageSize = default (CGSize);
Count++;
return null;
}
}
[Test]
public void GetVariationAxes ()
{
using (var font = new CTFont ("HoeflerText-Regular", 10)) {
var axes = font.GetVariationAxes ();
Assert.IsNotNull (axes, "axes");
// HoeflerText-Regular has no variation axes, so we expect an empty array
Assert.That (axes.Length, Is.EqualTo (0), "Length");
}
}
[Test]
public void UIFontType_SystemFont ()
{
TestRuntime.AssertXcodeVersion (26, 4);
using var font = new CTFont (CTFontUIFontType.System, 12, "en");
Assert.That (font.UIFontType, Is.EqualTo (CTFontUIFontType.System), "System");
}
[Test]
public void UIFontType_RegularFont ()
{
TestRuntime.AssertXcodeVersion (26, 4);
using var font = new CTFont ("HoeflerText-Regular", 10);
Assert.That (font.UIFontType, Is.EqualTo (CTFontUIFontType.None), "None");
}
}
}