1from PyObjCTools.TestSupport import *
2import objc
3
4import AppKit
5from AppKit import *
6
7import os
8
9try:
10    unicode
11except NameError:
12    unicode = str
13
14
15try:
16    long
17except NameError:
18    long = int
19
20class TestNSFont(TestCase):
21    def matrixEquals(self, value1, value2):
22        self.assertEqual(len(value1), len(value2))
23        for v1, v2 in zip(value1, value2):
24            # This should probably be 'assertAlmostEquals'
25            self.assertEqual(v1, v2)
26
27
28    def testMatrixMethods(self):
29        o = AppKit.NSFont.boldSystemFontOfSize_(10);
30        m = o.matrix()
31        self.assert_(isinstance(m, tuple))
32        self.assertEqual(len(m), 6)
33
34        nm = o.fontName()
35
36        o = AppKit.NSFont.fontWithName_matrix_(
37                nm, AppKit.NSFontIdentityMatrix)
38        self.assert_(o is not None)
39
40        m = o.matrix()
41        self.assert_(isinstance(m, tuple))
42        self.assertEqual(len(m), 6)
43
44        self.matrixEquals(m, (1.0, 0.0, 0.0, 1.0, 0.0, 0.0))
45
46        # For some reason Tiger transforms this matrix to the one below. The
47        # same thing happens in pure ObjC code.
48        #o = AppKit.NSFont.fontWithName_matrix_(nm, (1.0, 2.0, 3.0, 4.0, 5.0, 6.0))
49        o = AppKit.NSFont.fontWithName_matrix_(nm, (12.0, 0.0, 0.0, 12.0, 0.0, 0.0))
50        self.assert_(o is not None)
51
52        m = o.matrix()
53        self.assert_(isinstance(m, tuple))
54        self.assertEqual(len(m), 6)
55
56        #self.matrixEquals(m, (1.0, 2.0, 3.0, 4.0, 5.0, 6.0))
57        self.matrixEquals(m, (12.0, 0.0, 0.0, 12.0, 0.0, 0.0))
58
59        self.assertRaises(ValueError, AppKit.NSFont.fontWithName_matrix_, nm, "foo")
60        self.assertRaises(ValueError, AppKit.NSFont.fontWithName_matrix_, nm, (1, 2, 3, 4))
61        self.assertRaises(ValueError, AppKit.NSFont.fontWithName_matrix_, nm, (1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0))
62
63
64    def testConstants(self):
65        self.assertEqual(AppKit.NSFontIdentityMatrix, None)
66
67        self.assertEqual(NSControlGlyph, 0xffffff)
68        self.assertEqual(NSNullGlyph, 0)
69        self.assertEqual(NSNativeShortGlyphPacking, 5)
70
71        self.assertEqual(NSFontDefaultRenderingMode, 0)
72        self.assertEqual(NSFontAntialiasedRenderingMode, 1)
73        self.assertEqual(NSFontIntegerAdvancementsRenderingMode, 2)
74        self.assertEqual(NSFontAntialiasedIntegerAdvancementsRenderingMode, 3)
75
76        self.assertIsInstance(NSAntialiasThresholdChangedNotification, unicode)
77        self.assertIsInstance(NSFontSetChangedNotification, unicode)
78
79    @onlyOn32Bit
80    def testConstants_32bitonly(self):
81        self.assertEqual(NSOneByteGlyphPacking, 0)
82        self.assertEqual(NSJapaneseEUCGlyphPacking, 1)
83        self.assertEqual(NSAsciiWithDoubleByteEUCGlyphPacking, 2)
84        self.assertEqual(NSTwoByteGlyphPacking, 3)
85        self.assertEqual(NSFourByteGlyphPacking, 4)
86
87        self.assertEqual(NSGlyphBelow, 1)
88        self.assertEqual(NSGlyphAbove, 2)
89
90        self.assertIsInstance(NSAFMFamilyName, unicode)
91        self.assertIsInstance(NSAFMFontName, unicode)
92        self.assertIsInstance(NSAFMFormatVersion, unicode)
93        self.assertIsInstance(NSAFMFullName, unicode)
94        self.assertIsInstance(NSAFMNotice, unicode)
95        self.assertIsInstance(NSAFMVersion, unicode)
96        self.assertIsInstance(NSAFMWeight, unicode)
97        self.assertIsInstance(NSAFMEncodingScheme, unicode)
98        self.assertIsInstance(NSAFMCharacterSet, unicode)
99        self.assertIsInstance(NSAFMCapHeight, unicode)
100        self.assertIsInstance(NSAFMXHeight, unicode)
101        self.assertIsInstance(NSAFMAscender, unicode)
102        self.assertIsInstance(NSAFMDescender, unicode)
103        self.assertIsInstance(NSAFMUnderlinePosition, unicode)
104        self.assertIsInstance(NSAFMUnderlineThickness, unicode)
105        self.assertIsInstance(NSAFMItalicAngle, unicode)
106        self.assertIsInstance(NSAFMMappingScheme, unicode)
107
108
109
110    @min_os_level('10.7')
111    def testMethods10_7(self):
112        self.assertResultIsBOOL(NSFont.isVertical)
113
114    def testMethods(self):
115        self.assertResultIsBOOL(NSFont.isFixedPitch)
116        self.assertArgHasType(NSFont.getBoundingRects_forGlyphs_count_, 1, b'n^I')
117        self.assertArgSizeInArg(NSFont.getBoundingRects_forGlyphs_count_, 0, 2)
118        self.assertArgSizeInArg(NSFont.getBoundingRects_forGlyphs_count_, 1, 2)
119        self.assertArgHasType(NSFont.getAdvancements_forGlyphs_count_, 1, b'n^I')
120        self.assertArgSizeInArg(NSFont.getAdvancements_forGlyphs_count_, 0, 2)
121        self.assertArgSizeInArg(NSFont.getAdvancements_forGlyphs_count_, 1, 2)
122        self.assertArgSizeInArg(NSFont.getAdvancements_forPackedGlyphs_length_, 0, 2)
123        self.assertArgSizeInArg(NSFont.getAdvancements_forPackedGlyphs_length_, 1, 2)
124
125    @onlyOn32Bit
126    def testMethods32(self):
127        self.assertResultIsBOOL(NSFont.isBaseFont)
128        self.assertResultIsBOOL(NSFont.glyphIsEncoded_)
129        self.assertArgHasType(NSFont.glyphIsEncoded_, 0, b'I')
130        self.assertArgHasType(NSFont.positionOfGlyph_precededByGlyph_isNominal_, 0, b'I')
131        self.assertArgHasType(NSFont.positionOfGlyph_precededByGlyph_isNominal_, 1, b'I')
132        self.assertArgHasType(NSFont.positionOfGlyph_precededByGlyph_isNominal_, 2, b'o^'+objc._C_NSBOOL)
133
134        self.assertArgHasType(NSFont.positionsForCompositeSequence_numberOfGlyphs_pointArray_, 0, b'n^I')
135        self.assertArgSizeInArg(NSFont.positionsForCompositeSequence_numberOfGlyphs_pointArray_, 0, 1)
136        self.assertArgHasType(NSFont.positionsForCompositeSequence_numberOfGlyphs_pointArray_, 2, b'o^'+NSPoint.__typestr__)
137        self.assertArgSizeInArg(NSFont.positionsForCompositeSequence_numberOfGlyphs_pointArray_, 2, 1)
138
139        self.assertArgHasType(NSFont.positionOfGlyph_struckOverGlyph_metricsExist_, 2, b'o^' + objc._C_NSBOOL)
140        self.assertArgHasType(NSFont.positionOfGlyph_struckOverRect_metricsExist_, 2, b'o^' + objc._C_NSBOOL)
141        self.assertArgHasType(NSFont.positionOfGlyph_withRelation_toBaseGlyph_totalAdvancement_metricsExist_, 3, b'o^' + NSSize.__typestr__)
142        self.assertArgHasType(NSFont.positionOfGlyph_withRelation_toBaseGlyph_totalAdvancement_metricsExist_, 4, b'o^' + objc._C_NSBOOL)
143
144
145    def testFunctions(self):
146        glyphs = [ ord('A'), ord('B'), ord('9'), ord('a') ]
147
148        rv, packed = NSConvertGlyphsToPackedGlyphs(glyphs, len(glyphs), NSNativeShortGlyphPacking, None)
149        self.assertIsInstance(rv, (int, long))
150        self.assertIsInstance(packed, str)
151        if rv == 0:
152            self.assertEqual(len(packed), 0)
153
154        else:
155            self.assertEqual(len(packed), rv)
156
157if __name__ == '__main__':
158    main( )
159