def getVariationAxesForFont(font): """ Return a dictionary { axis tag: { name: , minValue: , maxValue: } } """ axes = OrderedDict() variationAxesDescriptions = CoreText.CTFontCopyVariationAxes(font) if variationAxesDescriptions is None: # non-variable fonts have no axes descriptions return axes for variationAxesDescription in variationAxesDescriptions: tag = convertIntToVariationTag(variationAxesDescription[ CoreText.kCTFontVariationAxisIdentifierKey]) name = variationAxesDescription[CoreText.kCTFontVariationAxisNameKey] minValue = variationAxesDescription[ CoreText.kCTFontVariationAxisMinimumValueKey] maxValue = variationAxesDescription[ CoreText.kCTFontVariationAxisMaximumValueKey] defaultValue = variationAxesDescription[ CoreText.kCTFontVariationAxisDefaultValueKey] data = dict(name=name, minValue=minValue, maxValue=maxValue, defaultValue=defaultValue) axes[tag] = data return axes
def getNamedInstancesForFont(font): """ Return a dict { postscriptName: location } of all named instances in a given font. """ instances = OrderedDict() if font is None: return instances fontDescriptor = font.fontDescriptor() url = CoreText.CTFontDescriptorCopyAttribute(fontDescriptor, CoreText.kCTFontURLAttribute) if url is None: return instances variationAxesDescriptions = CoreText.CTFontCopyVariationAxes(font) if variationAxesDescriptions is None: # non-variable fonts have no named instances return instances tagNameMap = {} for variationAxesDescription in variationAxesDescriptions: tag = convertIntToVariationTag(variationAxesDescription[ CoreText.kCTFontVariationAxisIdentifierKey]) name = variationAxesDescription[CoreText.kCTFontVariationAxisNameKey] tagNameMap[tag] = name ft = TTFont(url.path(), lazy=True, fontNumber=0) if "fvar" in ft: cgFont, _ = CoreText.CTFontCopyGraphicsFont(font, None) fvar = ft["fvar"] for instance in fvar.instances: fontVariations = dict() for axis, value in instance.coordinates.items(): fontVariations[tagNameMap[axis]] = value varFont = CoreText.CGFontCreateCopyWithVariations( cgFont, fontVariations) postScriptName = CoreText.CGFontCopyPostScriptName(varFont) instances[postScriptName] = instance.coordinates ft.close() return instances
def getVariationAxesForFontName(fontName): axes = OrderedDict() font = CoreText.CTFontCreateWithName(fontName, 12, None) variationAxesDescriptions = CoreText.CTFontCopyVariationAxes(font) if variationAxesDescriptions is None: # 'normal' fonts have no axes descriptions return axes for variationAxesDescription in variationAxesDescriptions: tag = convertIntToVariationTag(variationAxesDescription[ CoreText.kCTFontVariationAxisIdentifierKey]) name = variationAxesDescription[CoreText.kCTFontVariationAxisNameKey] minValue = variationAxesDescription[ CoreText.kCTFontVariationAxisMinimumValueKey] maxValue = variationAxesDescription[ CoreText.kCTFontVariationAxisMaximumValueKey] defaultValue = variationAxesDescription[ CoreText.kCTFontVariationAxisDefaultValueKey] data = dict(name=name, minValue=minValue, maxValue=maxValue, defaultValue=defaultValue) axes[tag] = data return axes
def testFunctions(self): font = CoreText.CTFontCreateWithName("Optima Bold", 14, None) self.assertIsInstance(font, CoreText.CTFontRef) self.assertResultIsCFRetained(CoreText.CTFontCreateWithName) descriptor = CoreText.CTFontDescriptorCreateWithNameAndSize( "Optima Bold", 14.0) self.assertIsInstance(descriptor, CoreText.CTFontDescriptorRef) font = CoreText.CTFontCreateWithFontDescriptor(descriptor, 5.0, None) self.assertIsInstance(font, CoreText.CTFontRef) self.assertResultIsCFRetained(CoreText.CTFontCreateWithFontDescriptor) font = CoreText.CTFontCreateUIFontForLanguage( CoreText.kCTFontMiniSystemFontType, 10.0, "nl_NL") self.assertIsInstance(font, CoreText.CTFontRef) self.assertResultIsCFRetained(CoreText.CTFontCreateUIFontForLanguage) font2 = CoreText.CTFontCreateCopyWithAttributes(font, 9.0, None, None) self.assertIsInstance(font2, CoreText.CTFontRef) self.assertResultIsCFRetained(CoreText.CTFontCreateCopyWithAttributes) font2 = CoreText.CTFontCreateCopyWithSymbolicTraits( font, 14.0, None, CoreText.kCTFontBoldTrait, CoreText.kCTFontBoldTrait) self.assertIsInstance(font2, CoreText.CTFontRef) self.assertResultIsCFRetained(CoreText.CTFontCreateCopyWithAttributes) font2 = CoreText.CTFontCreateCopyWithFamily(font, 14.0, None, "Lucida Grande") self.assertIsInstance(font2, CoreText.CTFontRef) self.assertResultIsCFRetained(CoreText.CTFontCreateCopyWithFamily) font2 = CoreText.CTFontCreateForString(font, "hello world", CoreText.CFRange(1, 4)) self.assertIsInstance(font2, CoreText.CTFontRef) self.assertResultIsCFRetained(CoreText.CTFontCreateForString) descriptor = CoreText.CTFontCopyFontDescriptor(font) self.assertIsInstance(descriptor, CoreText.CTFontDescriptorRef) v = CoreText.CTFontCopyAttribute(font, "size") self.assertIsNone(v) self.assertResultIsCFRetained(CoreText.CTFontCopyAttribute) v = CoreText.CTFontGetSize(font) self.assertIsInstance(v, float) v = CoreText.CTFontGetMatrix(font) self.assertIsInstance(v, Quartz.CGAffineTransform) v = CoreText.CTFontGetSymbolicTraits(font) self.assertIsInstance(v, int) v = CoreText.CTFontCopyTraits(font) self.assertIsInstance(v, CoreText.CFDictionaryRef) v = CoreText.CTFontCopyPostScriptName(font) self.assertIsInstance(v, str) v = CoreText.CTFontCopyFamilyName(font) self.assertIsInstance(v, str) v = CoreText.CTFontCopyFullName(font) self.assertIsInstance(v, str) v = CoreText.CTFontCopyDisplayName(font) self.assertIsInstance(v, str) v = CoreText.CTFontCopyName(font, CoreText.kCTFontCopyrightNameKey) self.assertIsInstance(v, str) v, locname = CoreText.CTFontCopyLocalizedName( font, CoreText.kCTFontCopyrightNameKey, None) self.assertIsInstance(v, (str, type(None))) self.assertIsInstance(locname, (str, type(None))) self.assertArgIsOut(CoreText.CTFontCopyLocalizedName, 2) v = CoreText.CTFontCopyCharacterSet(font) # self.assertIsInstance(v, CoreText.CFCharacterSetRef) self.assertIn("CharacterSet", type(v).__name__) v = CoreText.CTFontGetStringEncoding(font) self.assertIsInstance(v, int) v = CoreText.CTFontCopySupportedLanguages(font) self.assertIsInstance(v, CoreText.CFArrayRef) self.assertArgIsOut(CoreText.CTFontGetGlyphsForCharacters, 2) v, gl = CoreText.CTFontGetGlyphsForCharacters(font, "hello", None, 5) self.assertTrue(v is True) self.assertIsInstance(gl, tuple) self.assertEqual(len(gl), 5) self.assertIsInstance(gl[0], int) v = CoreText.CTFontGetAscent(font) self.assertIsInstance(v, float) v = CoreText.CTFontGetDescent(font) self.assertIsInstance(v, float) v = CoreText.CTFontGetLeading(font) self.assertIsInstance(v, float) v = CoreText.CTFontGetUnitsPerEm(font) self.assertIsInstance(v, int) v = CoreText.CTFontGetGlyphCount(font) self.assertIsInstance(v, int) v = CoreText.CTFontGetBoundingBox(font) self.assertIsInstance(v, CoreText.CGRect) v = CoreText.CTFontGetUnderlinePosition(font) self.assertIsInstance(v, float) v = CoreText.CTFontGetUnderlineThickness(font) self.assertIsInstance(v, float) v = CoreText.CTFontGetSlantAngle(font) self.assertIsInstance(v, float) v = CoreText.CTFontGetCapHeight(font) self.assertIsInstance(v, float) v = CoreText.CTFontGetXHeight(font) self.assertIsInstance(v, float) v = CoreText.CTFontGetGlyphWithName(font, "A") self.assertIsInstance(v, int) v, r = CoreText.CTFontGetBoundingRectsForGlyphs( font, CoreText.kCTFontDefaultOrientation, gl, None, 5) self.assertIsInstance(v, Quartz.CGRect) self.assertIsInstance(r, tuple) self.assertEqual(len(r), 5) self.assertIsInstance(r[0], Quartz.CGRect) v, r = CoreText.CTFontGetAdvancesForGlyphs( font, CoreText.kCTFontDefaultOrientation, gl, None, 5) self.assertIsInstance(v, float) self.assertIsInstance(r, tuple) self.assertEqual(len(r), 5) self.assertIsInstance(r[0], Quartz.CGSize) r = CoreText.CTFontGetVerticalTranslationsForGlyphs(font, gl, None, 5) self.assertIsInstance(r, tuple) self.assertEqual(len(r), 5) self.assertIsInstance(r[0], Quartz.CGSize) v = CoreText.CTFontCreatePathForGlyph(font, gl[0], None) self.assertIsInstance(v, CoreText.CGPathRef) self.assertResultIsCFRetained(CoreText.CTFontCopyVariationAxes) v = CoreText.CTFontCopyVariationAxes(font) if v is not None: self.assertIsInstance(v, CoreText.CFArrayRef) self.assertResultIsCFRetained(CoreText.CTFontCopyVariation) v = CoreText.CTFontCopyVariation(font) if v is not None: self.assertIsInstance(v, CoreText.CFDictionaryRef) v = CoreText.CTFontCopyFeatures(font) self.assertIsInstance(v, CoreText.CFArrayRef) self.assertResultIsCFRetained(CoreText.CTFontCopyFeatures) self.assertResultIsCFRetained(CoreText.CTFontCopyFeatureSettings) v = CoreText.CTFontCopyFeatureSettings(font) if v is not None: self.assertIsInstance(v, CoreText.CFArrayRef) v, o = CoreText.CTFontCopyGraphicsFont(font, None) self.assertIsInstance(v, CoreText.CGFontRef) if o is not None: self.assertIsInstance(o, CoreText.CTFontDescriptorRef) self.assertResultIsCFRetained(CoreText.CTFontCopyGraphicsFont) self.assertArgIsCFRetained(CoreText.CTFontCopyGraphicsFont, 1) self.assertArgIsOut(CoreText.CTFontCopyGraphicsFont, 1) v = CoreText.CTFontCreateWithGraphicsFont(v, 20.5, None, None) self.assertIsInstance(v, CoreText.CTFontRef) self.assertResultIsCFRetained(CoreText.CTFontCreateWithGraphicsFont) if 0: # Cannot tests these because the ATS framework is not # wrapped. v, o = CoreText.CTFontGetPlatformFont(font, None) self.assertIsInstance(v, CoreText.ATSFontRef) self.assertIsInstance(o, CoreText.CTFontDescriptorRef) v = CoreText.CTFontCreateWithPlatformFont(v, 22.5, None, None) self.assertIsInstance(v, CoreText.CGFontRef) self.assertArgIsCFRetained(CoreText.CTFontGetPlatformFont, 1) self.assertResultIsCFRetained(CoreText.CTFontCreateWithPlatformFont) v = CoreText.CTFontCopyAvailableTables( font, CoreText.kCTFontTableOptionNoOptions) self.assertIsInstance(v, tuple) v = CoreText.CTFontCopyTable(font, v[0], 0) self.assertIsInstance(v, CoreText.CFDataRef) v = CoreText.CTFontGetTypeID() self.assertIsInstance(v, int) v, a = CoreText.CTFontGetLigatureCaretPositions(font, gl[0], None, 5) self.assertIsInstance(v, int) self.assertIsInstance(a, tuple) self.assertEqual(len(a), 5) self.assertTrue(all(isinstance(x, float) for x in a))