1
2from PyObjCTools.TestSupport import *
3from Quartz.CoreGraphics import *
4
5import sys
6
7try:
8    unicode
9except NameError:
10    unicode = str
11
12
13try:
14    long
15except NameError:
16    long = int
17if sys.version_info[0] != 2:
18    def buffer(value):
19        if isinstance(value, bytes):
20            return value
21        return value.encode('latin1')
22
23class TestCGFont (TestCase):
24
25    def testTypes(self):
26        self.assertIsCFType(CGFontRef)
27
28    def testConstants(self):
29        self.assertEqual(kCGFontPostScriptFormatType1, 1)
30        self.assertEqual(kCGFontPostScriptFormatType3, 3)
31        self.assertEqual(kCGFontPostScriptFormatType42, 42)
32
33        self.assertEqual(kCGFontIndexMax, ((1 << 16) - 2))
34        self.assertEqual(kCGFontIndexInvalid, ((1 << 16) - 1))
35        self.assertEqual(kCGGlyphMax, kCGFontIndexMax)
36
37
38        self.assertIsInstance(kCGFontVariationAxisName, unicode)
39        self.assertIsInstance(kCGFontVariationAxisMinValue, unicode)
40        self.assertIsInstance(kCGFontVariationAxisMaxValue, unicode)
41        self.assertIsInstance(kCGFontVariationAxisDefaultValue, unicode)
42
43        self.assertEqual(CGGlyphMin, 0)
44        self.assertEqual(CGGlyphMax, kCGGlyphMax)
45
46
47
48    @min_os_level('10.5')
49    # Most functions should work on 10.4 as well, except for the convenient
50    # contruction functions
51    def testFunctions(self):
52        self.assertIsInstance(CGFontGetTypeID(), (int, long))
53
54        self.assertResultIsCFRetained(CGFontCreateWithFontName)
55        font = CGFontCreateWithFontName("Helvetica")
56        self.assertIsInstance(font, CGFontRef)
57
58        self.assertResultIsCFRetained(CGFontCreateCopyWithVariations)
59        font = CGFontCreateCopyWithVariations(font, None)
60        self.assertIsInstance(font, CGFontRef)
61
62        v = CGFontRetain(font)
63        self.assertTrue(v is font)
64        CGFontRelease(font)
65
66        v = CGFontGetNumberOfGlyphs(font)
67        self.assertIsInstance(v, (int, long))
68
69        v = CGFontGetUnitsPerEm(font)
70        self.assertIsInstance(v, (int, long))
71
72        self.assertResultIsCFRetained(CGFontCopyPostScriptName)
73        v = CGFontCopyPostScriptName(font)
74        self.assertIsInstance(v, unicode)
75
76        self.assertResultIsCFRetained(CGFontCopyFullName)
77        v = CGFontCopyFullName(font)
78        self.assertIsInstance(v, unicode)
79
80        v = CGFontGetAscent(font)
81        self.assertIsInstance(v, (int, long))
82
83        v = CGFontGetDescent(font)
84        self.assertIsInstance(v, (int, long))
85
86        v = CGFontGetLeading(font)
87        self.assertIsInstance(v, (int, long))
88
89        v = CGFontGetCapHeight(font)
90        self.assertIsInstance(v, (int, long))
91
92        v = CGFontGetXHeight(font)
93        self.assertIsInstance(v, (int, long))
94
95        v = CGFontGetFontBBox(font)
96        self.assertIsInstance(v, CGRect)
97
98        v = CGFontGetItalicAngle(font)
99        self.assertIsInstance(v, float)
100
101        v = CGFontGetStemV(font)
102        self.assertIsInstance(v, float)
103
104        v = CGFontCopyVariationAxes(font)
105        self.assertTrue(v is None or isinstance(v, CFArrayRef))
106
107        v = CGFontCopyVariations(font)
108        self.assertTrue(v is None or isinstance(v, CFDictionaryRef))
109
110        self.assertResultHasType(CGFontCanCreatePostScriptSubset, objc._C_BOOL)
111        v = CGFontCanCreatePostScriptSubset(font, kCGFontPostScriptFormatType1)
112        self.assertIsInstance(v, bool)
113
114
115        # PyObjC doesn't wrap ATSUI, therefore we cannot actually call
116        # the function.
117        #self.fail('CGFontCreateWithPlatformFont')
118        self.assertArgHasType(CGFontCreateWithPlatformFont, 0,
119                objc._C_PTR + objc._C_VOID)
120        self.assertResultIsCFRetained(CGFontCreateWithPlatformFont)
121
122
123        #data = open('/Library/Fonts/Webdings.ttf', 'rb').read()
124        with open('/Library/Fonts/Courier New.ttf', 'rb') as fp:
125            data = fp.read()
126        self.assertResultIsCFRetained(CGFontCreateWithDataProvider)
127        font = CGFontCreateWithDataProvider(
128                CGDataProviderCreateWithCFData(buffer(data))
129        )
130        self.assertIsInstance(font, CGFontRef)
131
132        tags = CGFontCopyTableTags(font)
133        self.assertIsInstance(tags, tuple)
134        self.failIfEqual(len(tags), 0)
135        self.assertIsInstance(tags[0], (int, long))
136
137        self.assertResultIsCFRetained(CGFontCopyTableForTag)
138        for tg in tags:
139            data = CGFontCopyTableForTag(font, 0)
140            if data is None:
141                continue
142            self.assertIsInstance(data, CFDataRef)
143
144        v = CGFontCopyGlyphNameForGlyph(font, ord('A'))
145        self.assertIsInstance(v, unicode)
146
147        glyphnames = ['chat', 'conference', 'woman' ]
148
149        v = CGFontGetGlyphWithGlyphName(font, glyphnames[0])
150        self.assertIsInstance(v, (int, long))
151
152        glyphs = [ CGFontGetGlyphWithGlyphName(font, nm)
153                    for nm in glyphnames ]
154
155        self.assertResultHasType(CGFontGetGlyphAdvances, objc._C_BOOL)
156        v, advances = CGFontGetGlyphAdvances(
157                font, glyphs, len(glyphs), None)
158        self.assertIsInstance(v, bool)
159        self.assertEqual(len(advances), 3)
160        for v in advances:
161            self.assertIsInstance(v, (int, long))
162
163        self.assertResultHasType(CGFontGetGlyphBBoxes, objc._C_BOOL)
164        v, bboxes = CGFontGetGlyphBBoxes(
165                font, glyphs, len(glyphs), None)
166        self.assertIsInstance(v, bool)
167        self.assertEqual(len(bboxes), 3)
168        for v in bboxes:
169            self.assertIsInstance(v, CGRect)
170
171        self.assertResultIsCFRetained(CGFontCreatePostScriptSubset)
172        psfont = CGFontCreatePostScriptSubset(
173                font, "pyobjc-characters",
174                 kCGFontPostScriptFormatType42,
175                glyphs, len(glyphs), None)
176        if psfont is not None:
177            self.assertIsInstance(psfont, CFDataRef)
178
179
180        self.assertResultIsCFRetained(CGFontCreatePostScriptEncoding)
181        map = glyphs + [0]*(256-len(glyphs))
182        psfont = CGFontCreatePostScriptEncoding(
183                font, map)
184        self.assertIsInstance(psfont, CFDataRef)
185
186
187
188if __name__ == "__main__":
189    main()
190