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