<s> '''<STR_LIT:U+0020>''' <EOL> __docformat__ = '<STR_LIT>' <EOL> __version__ = '<STR_LIT>' <EOL> import math <EOL> from ctypes import * <EOL> from ctypes import util <EOL> from pyglet . font import base <EOL> import pyglet . image <EOL> cf = cdll . LoadLibrary ( util . find_library ( '<STR_LIT>' ) ) <EOL> quartz = cdll . LoadLibrary ( util . find_library ( '<STR_LIT>' ) ) <EOL> ct = cdll . LoadLibrary ( util . find_library ( '<STR_LIT>' ) ) <EOL> CFIndex = c_long <EOL> UniChar = c_ushort <EOL> CGGlyph = c_ushort <EOL> import sys <EOL> if sys . maxint > <NUM_LIT:2> ** <NUM_LIT:32> : <EOL> CGFloat = c_double <EOL> else : <EOL> CGFloat = c_float <EOL> class CFRange ( Structure ) : <EOL> _fields_ = [ ( "<STR_LIT:location>" , CFIndex ) , ( "<STR_LIT>" , CFIndex ) ] <EOL> class CGPoint ( Structure ) : <EOL> _fields_ = [ ( "<STR_LIT:x>" , CGFloat ) , ( "<STR_LIT:y>" , CGFloat ) ] <EOL> class CGSize ( Structure ) : <EOL> _fields_ = [ ( "<STR_LIT:width>" , CGFloat ) , ( "<STR_LIT>" , CGFloat ) ] <EOL> class CGRect ( Structure ) : <EOL> _fields_ = [ ( "<STR_LIT>" , CGPoint ) , ( "<STR_LIT:size>" , CGSize ) ] <EOL> cf . CFDictionaryCreateMutable . restype = c_void_p <EOL> cf . CFStringCreateWithCString . restype = c_void_p <EOL> cf . CFAttributedStringCreate . restype = c_void_p <EOL> cf . CFDataCreate . restype = c_void_p <EOL> cf . CFNumberCreate . restype = c_void_p <EOL> ct . CTLineCreateWithAttributedString . restype = c_void_p <EOL> ct . CTFontGetBoundingRectsForGlyphs . restype = CGRect <EOL> ct . CTFontGetAdvancesForGlyphs . restype = c_double <EOL> ct . CTFontGetAscent . restype = CGFloat <EOL> ct . CTFontGetDescent . restype = CGFloat <EOL> ct . CTFontCreateWithGraphicsFont . restype = c_void_p <EOL> ct . CTFontCreateWithGraphicsFont . argtypes = [ c_void_p , CGFloat , c_void_p , c_void_p ] <EOL> ct . CTFontCopyFamilyName . restype = c_void_p <EOL> ct . CTFontCopyFullName . restype = c_void_p <EOL> ct . CTFontDescriptorCreateWithAttributes . restype = c_void_p <EOL> ct . CTFontCreateWithFontDescriptor . restype = c_void_p <EOL> ct . CTFontCreateWithFontDescriptor . argtypes = [ c_void_p , CGFloat , c_void_p ] <EOL> quartz . CGColorSpaceCreateDeviceRGB . restype = c_void_p <EOL> quartz . CGBitmapContextCreate . restype = c_void_p <EOL> quartz . CGBitmapContextCreateImage . restype = c_void_p <EOL> quartz . CGImageGetDataProvider . restype = c_void_p <EOL> quartz . CGDataProviderCopyData . restype = c_void_p <EOL> quartz . CGDataProviderCreateWithCFData . restype = c_void_p <EOL> quartz . CGFontCreateWithDataProvider . restype = c_void_p <EOL> quartz . CGContextSetTextPosition . argtypes = [ c_void_p , CGFloat , CGFloat ] <EOL> quartz . CGFontCreateWithFontName . restype = c_void_p <EOL> kCFStringEncodingUTF8 = <NUM_LIT> <EOL> kCFNumberSInt32Type = <NUM_LIT:3> <EOL> kCTFontAttributeName = c_void_p . in_dll ( ct , '<STR_LIT>' ) <EOL> kCTFontFamilyNameAttribute = c_void_p . in_dll ( ct , '<STR_LIT>' ) <EOL> kCTFontSymbolicTrait = c_void_p . in_dll ( ct , '<STR_LIT>' ) <EOL> kCTFontWeightTrait = c_void_p . in_dll ( ct , '<STR_LIT>' ) <EOL> kCTFontTraitsAttribute = c_void_p . in_dll ( ct , '<STR_LIT>' ) <EOL> kCTFontItalicTrait = ( <NUM_LIT:1> << <NUM_LIT:0> ) <EOL> kCTFontBoldTrait = ( <NUM_LIT:1> << <NUM_LIT:1> ) <EOL> kCGImageAlphaPremultipliedLast = <NUM_LIT:1> <EOL> def CFSTR ( text ) : <EOL> return c_void_p ( cf . CFStringCreateWithCString ( None , text . encode ( '<STR_LIT:utf8>' ) , kCFStringEncodingUTF8 ) ) <EOL> def cfstring_to_string ( cfstring ) : <EOL> length = cf . CFStringGetLength ( cfstring ) <EOL> size = cf . CFStringGetMaximumSizeForEncoding ( length , kCFStringEncodingUTF8 ) <EOL> buffer = c_buffer ( size + <NUM_LIT:1> ) <EOL> result = cf . CFStringGetCString ( cfstring , buffer , len ( buffer ) , kCFStringEncodingUTF8 ) <EOL> if result : <EOL> return buffer . value <EOL> class QuartzGlyphRenderer ( base . GlyphRenderer ) : <EOL> def __init__ ( self , font ) : <EOL> super ( QuartzGlyphRenderer , self ) . __init__ ( font ) <EOL> self . font = font <EOL> def render ( self , text ) : <EOL> ctFont = self . font . ctFont <EOL> attributes = c_void_p ( cf . CFDictionaryCreateMutable ( None , <NUM_LIT:1> , cf . kCFTypeDictionaryKeyCallBacks , cf . kCFTypeDictionaryValueCallBacks ) ) <EOL> cf . CFDictionaryAddValue ( attributes , kCTFontAttributeName , ctFont ) <EOL> string = c_void_p ( cf . CFAttributedStringCreate ( None , CFSTR ( text ) , attributes ) ) <EOL> line = c_void_p ( ct . CTLineCreateWithAttributedString ( string ) ) <EOL> cf . CFRelease ( string ) <EOL> cf . CFRelease ( attributes ) <EOL> count = len ( text ) <EOL> chars = ( UniChar * count ) ( * map ( ord , unicode ( text ) ) ) <EOL> glyphs = ( CGGlyph * count ) ( ) <EOL> ct . CTFontGetGlyphsForCharacters ( ctFont , chars , glyphs , count ) <EOL> rect = ct . CTFontGetBoundingRectsForGlyphs ( ctFont , <NUM_LIT:0> , glyphs , None , count ) <EOL> advance = ct . CTFontGetAdvancesForGlyphs ( ctFont , <NUM_LIT:0> , glyphs , None , count ) <EOL> width = max ( int ( math . ceil ( rect . size . width ) + <NUM_LIT:2> ) , <NUM_LIT:1> ) <EOL> height = max ( int ( math . ceil ( rect . size . height ) + <NUM_LIT:2> ) , <NUM_LIT:1> ) <EOL> baseline = - int ( math . floor ( rect . origin . y ) ) + <NUM_LIT:1> <EOL> lsb = int ( math . floor ( rect . origin . x ) ) - <NUM_LIT:1> <EOL> advance = int ( round ( advance ) ) <EOL> bitsPerComponent = <NUM_LIT:8> <EOL> bytesPerRow = <NUM_LIT:4> * width <EOL> colorSpace = c_void_p ( quartz . CGColorSpaceCreateDeviceRGB ( ) ) <EOL> bitmap = c_void_p ( quartz . CGBitmapContextCreate ( <EOL> None , <EOL> width , <EOL> height , <EOL> bitsPerComponent , <EOL> bytesPerRow , <EOL> colorSpace , <EOL> kCGImageAlphaPremultipliedLast ) ) <EOL> quartz . CGContextSetShouldAntialias ( bitmap , True ) <EOL> quartz . CGContextSetTextPosition ( bitmap , - lsb , baseline ) <EOL> ct . CTLineDraw ( line , bitmap ) <EOL> cf . CFRelease ( line ) <EOL> imageRef = c_void_p ( quartz . CGBitmapContextCreateImage ( bitmap ) ) <EOL> bytesPerRow = quartz . CGImageGetBytesPerRow ( imageRef ) <EOL> dataProvider = c_void_p ( quartz . CGImageGetDataProvider ( imageRef ) ) <EOL> imageData = c_void_p ( quartz . CGDataProviderCopyData ( dataProvider ) ) <EOL> buffersize = cf . CFDataGetLength ( imageData ) <EOL> buffer = ( c_byte * buffersize ) ( ) <EOL> byteRange = CFRange ( <NUM_LIT:0> , buffersize ) <EOL> cf . CFDataGetBytes ( imageData , byteRange , buffer ) <EOL> quartz . CGImageRelease ( imageRef ) <EOL> quartz . CGDataProviderRelease ( imageData ) <EOL> cf . CFRelease ( bitmap ) <EOL> cf . CFRelease ( colorSpace ) <EOL> glyph_image = pyglet . image . ImageData ( width , height , '<STR_LIT>' , buffer , bytesPerRow ) <EOL> glyph = self . font . create_glyph ( glyph_image ) <EOL> glyph . set_bearings ( baseline , lsb , advance ) <EOL> t = list ( glyph . tex_coords ) <EOL> glyph . tex_coords = t [ <NUM_LIT:9> : <NUM_LIT:12> ] + t [ <NUM_LIT:6> : <NUM_LIT:9> ] + t [ <NUM_LIT:3> : <NUM_LIT:6> ] + t [ : <NUM_LIT:3> ] <EOL> return glyph <EOL> class QuartzFont ( base . Font ) : <EOL> glyph_renderer_class = QuartzGlyphRenderer <EOL> _loaded_CGFont_table = { } <EOL> def _lookup_font_with_family_and_traits ( self , family , traits ) : <EOL> if family not in self . _loaded_CGFont_table : <EOL> return None <EOL> fonts = self . _loaded_CGFont_table [ family ] <EOL> if not fonts : <EOL> return None <EOL> if traits in fonts : <EOL> return fonts [ traits ] <EOL> for ( t , f ) in fonts . items ( ) : <EOL> if traits & t : <EOL> return f <EOL> if <NUM_LIT:0> in fonts : <EOL> return fonts [ <NUM_LIT:0> ] <EOL> return fonts . values ( ) [ <NUM_LIT:0> ] <EOL> def _create_font_descriptor ( self , family_name , traits ) : <EOL> attributes = c_void_p ( cf . CFDictionaryCreateMutable ( None , <NUM_LIT:0> , cf . kCFTypeDictionaryKeyCallBacks , cf . kCFTypeDictionaryValueCallBacks ) ) <EOL> cfname = CFSTR ( family_name ) <EOL> cf . CFDictionaryAddValue ( attributes , kCTFontFamilyNameAttribute , cfname ) <EOL> cf . CFRelease ( cfname ) <EOL> itraits = c_int32 ( traits ) <EOL> symTraits = c_void_p ( cf . CFNumberCreate ( None , kCFNumberSInt32Type , byref ( itraits ) ) ) <EOL> if symTraits : <EOL> traitsDict = c_void_p ( cf . CFDictionaryCreateMutable ( None , <NUM_LIT:0> , cf . kCFTypeDictionaryKeyCallBacks , cf . kCFTypeDictionaryValueCallBacks ) ) <EOL> if traitsDict : <EOL> cf . CFDictionaryAddValue ( traitsDict , kCTFontSymbolicTrait , symTraits ) <EOL> cf . CFDictionaryAddValue ( attributes , kCTFontTraitsAttribute , traitsDict ) <EOL> cf . CFRelease ( traitsDict ) <EOL> cf . CFRelease ( symTraits ) <EOL> descriptor = c_void_p ( ct . CTFontDescriptorCreateWithAttributes ( attributes ) ) <EOL> cf . CFRelease ( attributes ) <EOL> return descriptor <EOL> def __init__ ( self , name , size , bold = False , italic = False , dpi = None ) : <EOL> super ( QuartzFont , self ) . __init__ ( ) <EOL> if not name : name = '<STR_LIT>' <EOL> if dpi is None : dpi = <NUM_LIT> <EOL> size = size * dpi / <NUM_LIT> <EOL> traits = <NUM_LIT:0> <EOL> if bold : traits |= kCTFontBoldTrait <EOL> if italic : traits |= kCTFontItalicTrait <EOL> name = unicode ( name ) <EOL> cgFont = self . _lookup_font_with_family_and_traits ( name , traits ) <EOL> if cgFont : <EOL> self . ctFont = c_void_p ( ct . CTFontCreateWithGraphicsFont ( cgFont , size , None , None ) ) <EOL> else : <EOL> descriptor = self . _create_font_descriptor ( name , traits ) <EOL> self . ctFont = c_void_p ( ct . CTFontCreateWithFontDescriptor ( descriptor , size , None ) ) <EOL> assert self . ctFont , "<STR_LIT>" + name <EOL> self . ascent = int ( math . ceil ( ct . CTFontGetAscent ( self . ctFont ) ) ) <EOL> self . descent = - int ( math . ceil ( ct . CTFontGetDescent ( self . ctFont ) ) ) <EOL> @ classmethod <EOL> def have_font ( cls , name ) : <EOL> name = unicode ( name ) <EOL> if name in cls . _loaded_CGFont_table : return True <EOL> cfstring = CFSTR ( name ) <EOL> cgfont = c_void_p ( quartz . CGFontCreateWithFontName ( cfstring ) ) <EOL> cf . CFRelease ( cfstring ) <EOL> if cgfont : <EOL> cf . CFRelease ( cgfont ) <EOL> return True <EOL> return False <EOL> @ classmethod <EOL> def add_font_data ( cls , data ) : <EOL> dataRef = c_void_p ( cf . CFDataCreate ( None , data , len ( data ) ) ) <EOL> provider = c_void_p ( quartz . CGDataProviderCreateWithCFData ( dataRef ) ) <EOL> cgFont = c_void_p ( quartz . CGFontCreateWithDataProvider ( provider ) ) <EOL> cf . CFRelease ( dataRef ) <EOL> quartz . CGDataProviderRelease ( provider ) <EOL> ctFont = c_void_p ( ct . CTFontCreateWithGraphicsFont ( cgFont , <NUM_LIT:1> , None , None ) ) <EOL> string = c_void_p ( ct . CTFontCopyFamilyName ( ctFont ) ) <EOL> familyName = unicode ( cfstring_to_string ( string ) ) <EOL> cf . CFRelease ( string ) <EOL> string = c_void_p ( ct . CTFontCopyFullName ( ctFont ) ) <EOL> fullName = unicode ( cfstring_to_string ( string ) ) <EOL> cf . CFRelease ( string ) <EOL> traits = ct . CTFontGetSymbolicTraits ( ctFont ) <EOL> cf . CFRelease ( ctFont ) <EOL> if familyName not in cls . _loaded_CGFont_table : <EOL> cls . _loaded_CGFont_table [ familyName ] = { } <EOL> cls . _loaded_CGFont_table [ familyName ] [ traits ] = cgFont <EOL> if fullName not in cls . _loaded_CGFont_table : <EOL> cls . _loaded_CGFont_table [ fullName ] = { } <EOL> cls . _loaded_CGFont_table [ fullName ] [ traits ] = cgFont </s>
