<s> import numpy as np <EOL> from ctypes import byref , c_int32 , c_byte <EOL> from ... ext . cocoapy import cf , ct , quartz , CFRange , CFSTR , CGGlyph , UniChar , kCTFontFamilyNameAttribute , kCTFontBoldTrait , kCTFontItalicTrait , kCTFontSymbolicTrait , kCTFontTraitsAttribute , kCTFontAttributeName , kCGImageAlphaPremultipliedLast , kCFNumberSInt32Type , ObjCClass <EOL> from . _vispy_fonts import _vispy_fonts , _get_vispy_font_filename <EOL> _font_dict = { } <EOL> def _load_vispy_font ( face , bold , italic ) : <EOL> fname = _get_vispy_font_filename ( face , bold , italic ) <EOL> url = cf . CFURLCreateWithFileSystemPath ( None , CFSTR ( fname ) , <NUM_LIT:0> , False ) <EOL> array = ct . CTFontManagerCreateFontDescriptorsFromURL ( url ) <EOL> desc = cf . CFArrayGetValueAtIndex ( array , <NUM_LIT:0> ) <EOL> font = ct . CTFontCreateWithFontDescriptor ( desc , <NUM_LIT> , None ) <EOL> cf . CFRelease ( array ) <EOL> cf . CFRelease ( url ) <EOL> if not font : <EOL> raise RuntimeError ( "<STR_LIT>" % face ) <EOL> key = '<STR_LIT>' % ( face , bold , italic ) <EOL> _font_dict [ key ] = font <EOL> return font <EOL> def _load_font ( face , bold , italic ) : <EOL> key = '<STR_LIT>' % ( face , bold , italic ) <EOL> if key in _font_dict : <EOL> return _font_dict [ key ] <EOL> if face in _vispy_fonts : <EOL> return _load_vispy_font ( face , bold , italic ) <EOL> traits = <NUM_LIT:0> <EOL> traits |= kCTFontBoldTrait if bold else <NUM_LIT:0> <EOL> traits |= kCTFontItalicTrait if italic else <NUM_LIT:0> <EOL> args = [ None , <NUM_LIT:0> , cf . kCFTypeDictionaryKeyCallBacks , <EOL> cf . kCFTypeDictionaryValueCallBacks ] <EOL> attributes = cf . CFDictionaryCreateMutable ( * args ) <EOL> cfname = CFSTR ( face ) <EOL> cf . CFDictionaryAddValue ( attributes , kCTFontFamilyNameAttribute , cfname ) <EOL> cf . CFRelease ( cfname ) <EOL> itraits = c_int32 ( traits ) <EOL> sym_traits = cf . CFNumberCreate ( None , kCFNumberSInt32Type , byref ( itraits ) ) <EOL> if sym_traits : <EOL> args = [ None , <NUM_LIT:0> , cf . kCFTypeDictionaryKeyCallBacks , <EOL> cf . kCFTypeDictionaryValueCallBacks ] <EOL> traits_dict = cf . CFDictionaryCreateMutable ( * args ) <EOL> if traits_dict : <EOL> cf . CFDictionaryAddValue ( traits_dict , kCTFontSymbolicTrait , <EOL> sym_traits ) <EOL> cf . CFDictionaryAddValue ( attributes , kCTFontTraitsAttribute , <EOL> traits_dict ) <EOL> cf . CFRelease ( traits_dict ) <EOL> cf . CFRelease ( sym_traits ) <EOL> desc = ct . CTFontDescriptorCreateWithAttributes ( attributes ) <EOL> cf . CFRelease ( attributes ) <EOL> font = ct . CTFontCreateWithFontDescriptor ( desc , <NUM_LIT> , None ) <EOL> if not font : <EOL> raise RuntimeError ( "<STR_LIT>" % face ) <EOL> _font_dict [ key ] = font <EOL> return font <EOL> def _load_glyph ( f , char , glyphs_dict ) : <EOL> font = _load_font ( f [ '<STR_LIT>' ] , f [ '<STR_LIT>' ] , f [ '<STR_LIT>' ] ) <EOL> args = [ None , <NUM_LIT:0> , cf . kCFTypeDictionaryKeyCallBacks , <EOL> cf . kCFTypeDictionaryValueCallBacks ] <EOL> attributes = cf . CFDictionaryCreateMutable ( * args ) <EOL> desc = ct . CTFontDescriptorCreateWithAttributes ( attributes ) <EOL> cf . CFRelease ( attributes ) <EOL> font = ct . CTFontCreateCopyWithAttributes ( font , f [ '<STR_LIT:size>' ] , None , desc ) <EOL> cf . CFRelease ( desc ) <EOL> if not font : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> args = [ None , <NUM_LIT:1> , cf . kCFTypeDictionaryKeyCallBacks , <EOL> cf . kCFTypeDictionaryValueCallBacks ] <EOL> attributes = cf . CFDictionaryCreateMutable ( * args ) <EOL> cf . CFDictionaryAddValue ( attributes , kCTFontAttributeName , font ) <EOL> string = cf . CFAttributedStringCreate ( None , CFSTR ( char ) , attributes ) <EOL> line = ct . CTLineCreateWithAttributedString ( string ) <EOL> cf . CFRelease ( string ) <EOL> cf . CFRelease ( attributes ) <EOL> chars = ( UniChar * <NUM_LIT:1> ) ( * map ( ord , char ) ) <EOL> glyphs = ( CGGlyph * <NUM_LIT:1> ) ( ) <EOL> ct . CTFontGetGlyphsForCharacters ( font , chars , glyphs , <NUM_LIT:1> ) <EOL> rect = ct . CTFontGetBoundingRectsForGlyphs ( font , <NUM_LIT:0> , glyphs , None , <NUM_LIT:1> ) <EOL> advance = ct . CTFontGetAdvancesForGlyphs ( font , <NUM_LIT:1> , glyphs , None , <NUM_LIT:1> ) <EOL> width = max ( int ( np . ceil ( rect . size . width ) + <NUM_LIT:1> ) , <NUM_LIT:1> ) <EOL> height = max ( int ( np . ceil ( rect . size . height ) + <NUM_LIT:1> ) , <NUM_LIT:1> ) <EOL> left = rect . origin . x <EOL> baseline = - rect . origin . y <EOL> top = height - baseline <EOL> bits_per_component = <NUM_LIT:8> <EOL> bytes_per_row = <NUM_LIT:4> * width <EOL> color_space = quartz . CGColorSpaceCreateDeviceRGB ( ) <EOL> args = [ None , width , height , bits_per_component , bytes_per_row , <EOL> color_space , kCGImageAlphaPremultipliedLast ] <EOL> bitmap = quartz . CGBitmapContextCreate ( * args ) <EOL> quartz . CGContextSetShouldAntialias ( bitmap , True ) <EOL> quartz . CGContextSetTextPosition ( bitmap , - left , baseline ) <EOL> ct . CTLineDraw ( line , bitmap ) <EOL> cf . CFRelease ( line ) <EOL> image_ref = quartz . CGBitmapContextCreateImage ( bitmap ) <EOL> assert quartz . CGImageGetBytesPerRow ( image_ref ) == bytes_per_row <EOL> data_provider = quartz . CGImageGetDataProvider ( image_ref ) <EOL> image_data = quartz . CGDataProviderCopyData ( data_provider ) <EOL> buffer_size = cf . CFDataGetLength ( image_data ) <EOL> assert buffer_size == width * height * <NUM_LIT:4> <EOL> buffer = ( c_byte * buffer_size ) ( ) <EOL> byte_range = CFRange ( <NUM_LIT:0> , buffer_size ) <EOL> cf . CFDataGetBytes ( image_data , byte_range , buffer ) <EOL> quartz . CGImageRelease ( image_ref ) <EOL> quartz . CGDataProviderRelease ( image_data ) <EOL> cf . CFRelease ( bitmap ) <EOL> cf . CFRelease ( color_space ) <EOL> bitmap = np . array ( buffer , np . ubyte ) <EOL> bitmap . shape = ( height , width , <NUM_LIT:4> ) <EOL> bitmap = bitmap [ : , : , <NUM_LIT:3> ] . copy ( ) <EOL> glyph = dict ( char = char , offset = ( left , top ) , bitmap = bitmap , <EOL> advance = advance , kerning = { } ) <EOL> glyphs_dict [ char ] = glyph <EOL> for other_char , other_glyph in glyphs_dict . items ( ) : <EOL> glyph [ '<STR_LIT>' ] [ other_char ] = ( _get_k_p_a ( font , other_char , char ) - <EOL> other_glyph [ '<STR_LIT>' ] ) <EOL> other_glyph [ '<STR_LIT>' ] [ char ] = ( _get_k_p_a ( font , char , other_char ) - <EOL> glyph [ '<STR_LIT>' ] ) <EOL> cf . CFRelease ( font ) <EOL> def _get_k_p_a ( font , left , right ) : <EOL> """<STR_LIT>""" <EOL> chars = left + right <EOL> args = [ None , <NUM_LIT:1> , cf . kCFTypeDictionaryKeyCallBacks , <EOL> cf . kCFTypeDictionaryValueCallBacks ] <EOL> attributes = cf . CFDictionaryCreateMutable ( * args ) <EOL> cf . CFDictionaryAddValue ( attributes , kCTFontAttributeName , font ) <EOL> string = cf . CFAttributedStringCreate ( None , CFSTR ( chars ) , attributes ) <EOL> typesetter = ct . CTTypesetterCreateWithAttributedString ( string ) <EOL> cf . CFRelease ( string ) <EOL> cf . CFRelease ( attributes ) <EOL> range = CFRange ( <NUM_LIT:0> , <NUM_LIT:1> ) <EOL> line = ct . CTTypesetterCreateLine ( typesetter , range ) <EOL> offset = ct . CTLineGetOffsetForStringIndex ( line , <NUM_LIT:1> , None ) <EOL> cf . CFRelease ( line ) <EOL> cf . CFRelease ( typesetter ) <EOL> return offset <EOL> def _list_fonts ( ) : <EOL> manager = ObjCClass ( '<STR_LIT>' ) . sharedFontManager ( ) <EOL> avail = manager . availableFontFamilies ( ) <EOL> fonts = [ avail . objectAtIndex_ ( ii ) . UTF8String ( ) . decode ( '<STR_LIT:utf-8>' ) <EOL> for ii in range ( avail . count ( ) ) ] <EOL> return fonts </s>
