<s> from vanilla import * <EOL> from AppKit import * <EOL> from defconAppKit . windows . baseWindow import BaseWindowController <EOL> from fnmatch import fnmatch <EOL> import string <EOL> def splitName ( name ) : <EOL> """<STR_LIT>""" <EOL> baseName , suffix = '<STR_LIT>' , '<STR_LIT>' <EOL> nameElements = name . split ( '<STR_LIT:.>' ) <EOL> if len ( nameElements ) > <NUM_LIT:0> : <EOL> baseName = nameElements [ <NUM_LIT:0> ] <EOL> if len ( nameElements ) > <NUM_LIT:1> : <EOL> suffix = '<STR_LIT:.>' . join ( nameElements [ <NUM_LIT:1> : ] ) <EOL> else : <EOL> suffix = '<STR_LIT>' <EOL> return baseName , suffix <EOL> UNICODE_CATEGORY_ORDER = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> UNICODE_CATEGORIES = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> UNICODE_CATEGORY_ORDER_COMBINED = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> UNICODE_CATEGORIES_COMBINED = { <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> } <EOL> def dec2hex ( n , uni = <NUM_LIT:1> ) : <EOL> hex = "<STR_LIT>" % n <EOL> if uni == <NUM_LIT:1> : <EOL> while len ( hex ) <= <NUM_LIT:3> : <EOL> hex = '<STR_LIT:0>' + str ( hex ) <EOL> return hex <EOL> def readUnicode ( unicodeInteger ) : <EOL> if isinstance ( unicodeInteger , int ) : <EOL> return dec2hex ( unicodeInteger ) <EOL> else : <EOL> return str ( unicodeInteger ) <EOL> def hex2dec ( s ) : <EOL> try : <EOL> return int ( s , <NUM_LIT:16> ) <EOL> except : <EOL> pass <EOL> def writeUnicode ( unicodeString ) : <EOL> if type ( unicodeString ) is str : <EOL> return hex2dec ( unicodeString ) <EOL> else : <EOL> return int ( unicodeString ) <EOL> def reverseDict ( d ) : <EOL> """<STR_LIT>""" <EOL> newDict = { } <EOL> keys = d . keys ( ) <EOL> keys . sort ( ) <EOL> for k in keys : <EOL> v = d [ k ] <EOL> if v not in newDict . keys ( ) : <EOL> newDict [ v ] = k <EOL> return newDict <EOL> class SelectGlyphSearch ( BaseWindowController ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> """<STR_LIT>""" <EOL> title = "<STR_LIT>" <EOL> self . w = Window ( ( <NUM_LIT> , <NUM_LIT> ) , title , minSize = ( <NUM_LIT> , <NUM_LIT> ) ) <EOL> f = self . getFont ( ) <EOL> if not f : <EOL> print "<STR_LIT>" <EOL> return <EOL> self . setExistingSelection ( f . selection [ : ] ) <EOL> tabList = [ '<STR_LIT:Name>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> self . w . tabs = Tabs ( ( <NUM_LIT:10> , <NUM_LIT:10> , - <NUM_LIT:10> , - <NUM_LIT:10> ) , tabList , sizeStyle = "<STR_LIT>" ) <EOL> for i , tabName in enumerate ( tabList ) : <EOL> if i == <NUM_LIT:0> or i == <NUM_LIT:1> or i == <NUM_LIT:2> or i == <NUM_LIT:3> : <EOL> self . w . tabs [ i ] . searchBox = SearchBox ( ( <NUM_LIT:10> , <NUM_LIT:10> , - <NUM_LIT:50> , <NUM_LIT> ) , callback = self . searchBoxCallback , placeholder = tabName ) <EOL> else : <EOL> self . w . tabs [ i ] . searchBox = ComboBox ( ( <NUM_LIT:10> , <NUM_LIT:10> , - <NUM_LIT:50> , <NUM_LIT> ) , self . getUnicodeCategoriesList ( ) , callback = self . searchBoxCallback ) <EOL> self . w . tabs [ i ] . refresh = Button ( ( - <NUM_LIT> , <NUM_LIT:10> , <NUM_LIT:30> , <NUM_LIT> ) , unichr ( <NUM_LIT> ) , callback = self . refreshCallback ) <EOL> self . w . tabs [ i ] . glyphList = List ( ( <NUM_LIT:10> , <NUM_LIT> , - <NUM_LIT:10> , - <NUM_LIT> ) , [ ] ) <EOL> y = - <NUM_LIT> <EOL> self . w . sourceName = TextBox ( ( <NUM_LIT:20> , y , - <NUM_LIT:10> , <NUM_LIT> ) , '<STR_LIT>' , sizeStyle = "<STR_LIT>" ) <EOL> y += <NUM_LIT> <EOL> self . w . replaceSelection = Button ( ( <NUM_LIT:20> , y , <NUM_LIT> , <NUM_LIT> ) , '<STR_LIT>' , sizeStyle = "<STR_LIT>" , callback = self . replaceSelection ) <EOL> self . w . addToSelection = Button ( ( <NUM_LIT> , y , <NUM_LIT> , <NUM_LIT> ) , '<STR_LIT>' , sizeStyle = "<STR_LIT>" , callback = self . addToSelection ) <EOL> y += <NUM_LIT> <EOL> self . w . intersectWithSelection = Button ( ( <NUM_LIT:20> , y , <NUM_LIT> , <NUM_LIT> ) , '<STR_LIT>' , sizeStyle = "<STR_LIT>" , callback = self . intersectWithSelection ) <EOL> self . w . subtractFromSelection = Button ( ( <NUM_LIT> , y , <NUM_LIT> , <NUM_LIT> ) , '<STR_LIT>' , sizeStyle = "<STR_LIT>" , callback = self . subtractFromSelection ) <EOL> y += <NUM_LIT:30> <EOL> self . w . applySelectionText = TextBox ( ( <NUM_LIT:20> , y , <NUM_LIT> , <NUM_LIT> ) , '<STR_LIT>' , sizeStyle = "<STR_LIT>" ) <EOL> self . w . applySelectionRadio = RadioGroup ( ( <NUM_LIT> , y - <NUM_LIT:3> , <NUM_LIT> , <NUM_LIT> ) , <EOL> [ "<STR_LIT>" , "<STR_LIT>" ] , <EOL> isVertical = False , sizeStyle = "<STR_LIT>" ) <EOL> self . w . applySelectionRadio . set ( <NUM_LIT:0> ) <EOL> self . w . printButton = Button ( ( - <NUM_LIT:50> , y - <NUM_LIT:3> , <NUM_LIT:30> , <NUM_LIT> ) , unichr ( <NUM_LIT> ) , callback = self . printCallback , sizeStyle = "<STR_LIT>" ) <EOL> self . setUpBaseWindowBehavior ( ) <EOL> self . w . open ( ) <EOL> def windowCloseCallback ( self , sender ) : <EOL> BaseWindowController . windowCloseCallback ( self , sender ) <EOL> def getFont ( self ) : <EOL> """<STR_LIT>""" <EOL> f = CurrentFont ( ) <EOL> return f <EOL> def setFontSource ( self , f ) : <EOL> if f is not None : <EOL> self . w . sourceName . set ( '<STR_LIT>' + self . getFontName ( f ) ) <EOL> else : <EOL> self . w . sourceName . set ( '<STR_LIT>' ) <EOL> def getFontName ( self , f = None ) : <EOL> if f is None : <EOL> f = self . getFont ( ) <EOL> return '<STR_LIT:U+0020>' . join ( [ f . info . familyName , f . info . styleName ] ) <EOL> def getAllFonts ( self ) : <EOL> return AllFonts ( ) <EOL> def getGlyphOrder ( self ) : <EOL> return self . getFont ( ) . glyphOrder <EOL> def getUnicodeData ( self ) : <EOL> return self . getFont ( ) . naked ( ) . unicodeData <EOL> def getFontAndGlyphOrderAndUnicodeData ( self ) : <EOL> """<STR_LIT>""" <EOL> f = self . getFont ( ) <EOL> glyphOrder = f . glyphOrder <EOL> unicodeData = f . naked ( ) . unicodeData <EOL> return f , glyphOrder , unicodeData <EOL> def getExistingSelection ( self ) : <EOL> return self . selection <EOL> def setExistingSelection ( self , selection ) : <EOL> self . selection = selection <EOL> def getNewSelection ( self ) : <EOL> """<STR_LIT>""" <EOL> i = self . w . tabs . get ( ) <EOL> tabAll = self . w . tabs [ i ] . glyphList . get ( ) <EOL> tabSelectedIndexes = self . w . tabs [ i ] . glyphList . getSelection ( ) <EOL> newSelection = [ ] <EOL> for selectedIndex in tabSelectedIndexes : <EOL> newSelection . append ( tabAll [ selectedIndex ] ) <EOL> return newSelection <EOL> def searchBoxCallback ( self , sender ) : <EOL> """<STR_LIT:U+0020>""" <EOL> searchTerm = sender . get ( ) <EOL> self . doSearch ( searchTerm ) <EOL> def refreshCallback ( self , sender ) : <EOL> """<STR_LIT>""" <EOL> i = self . w . tabs . get ( ) <EOL> self . doSearch ( self . w . tabs [ i ] . searchBox . get ( ) ) <EOL> def printCallback ( self , sender ) : <EOL> print self . getNewSelection ( ) <EOL> def doSearchInGlyphName ( self , searchTerm , searchIn ) : <EOL> searchResults = [ ] <EOL> for gname in searchIn : <EOL> if fnmatch ( gname , searchTerm ) : <EOL> searchResults . append ( gname ) <EOL> return searchResults <EOL> def doSearchInBaseName ( self , searchTerm , searchIn ) : <EOL> searchResults = [ ] <EOL> for gname in searchIn : <EOL> baseName , suffix = splitName ( gname ) <EOL> if fnmatch ( baseName , searchTerm ) : <EOL> searchResults . append ( gname ) <EOL> return searchResults <EOL> def doSearchInSuffix ( self , searchTerm , searchIn ) : <EOL> searchResults = [ ] <EOL> for gname in searchIn : <EOL> baseName , suffix = splitName ( gname ) <EOL> if fnmatch ( suffix , searchTerm ) : <EOL> searchResults . append ( gname ) <EOL> return searchResults <EOL> def getUnicodeCategoriesList ( self ) : <EOL> categoryList = [ ] <EOL> for metaCategoryName in UNICODE_CATEGORY_ORDER_COMBINED : <EOL> categoryList . append ( metaCategoryName ) <EOL> categoryList . append ( '<STR_LIT>' ) <EOL> for catshort in UNICODE_CATEGORY_ORDER : <EOL> categoryList . append ( UNICODE_CATEGORIES [ catshort ] ) <EOL> return categoryList <EOL> def doSearchInUnicode ( self , searchTerm , searchIn , unicodeData = None ) : <EOL> searchResults = [ ] <EOL> if unicodeData is None : <EOL> unicodeData = self . getUnicodeData ( ) <EOL> for gname in searchIn : <EOL> dec = unicodeData . unicodeForGlyphName ( gname ) <EOL> hex = readUnicode ( dec ) <EOL> if fnmatch ( hex , searchTerm ) and not gname in searchResults : <EOL> searchResults . append ( gname ) <EOL> searchResults . sort ( ) <EOL> return searchResults <EOL> """<STR_LIT>""" <EOL> searchResults . sort ( ) <EOL> return searchResults <EOL> def doSearchInUnicodeCategory ( self , searchTerm , searchIn , unicodeData = None ) : <EOL> searchResults = [ ] <EOL> if unicodeData is None : <EOL> unicodeData = self . getUnicodeData ( ) <EOL> reverseUnicodeCategories = reverseDict ( UNICODE_CATEGORIES ) <EOL> try : <EOL> searchTerms = [ reverseUnicodeCategories [ searchTerm ] ] <EOL> except : <EOL> searchTerms = UNICODE_CATEGORIES_COMBINED [ searchTerm ] <EOL> for gname in searchIn : <EOL> if unicodeData . categoryForGlyphName ( gname ) in searchTerms : <EOL> searchResults . append ( gname ) <EOL> return searchResults <EOL> def doSearch ( self , searchTerm ) : <EOL> f , glyphOrder , unicodeData = self . getFontAndGlyphOrderAndUnicodeData ( ) <EOL> self . setFontSource ( f ) <EOL> i = self . w . tabs . get ( ) <EOL> if not searchTerm : <EOL> self . setFontSource ( None ) <EOL> searchResults = [ ] <EOL> if i == <NUM_LIT:0> : <EOL> searchResults = self . doSearchInGlyphName ( searchTerm , glyphOrder ) <EOL> elif i == <NUM_LIT:1> : <EOL> searchResults = self . doSearchInBaseName ( searchTerm , glyphOrder ) <EOL> elif i == <NUM_LIT:2> : <EOL> searchResults = self . doSearchInSuffix ( searchTerm , glyphOrder ) <EOL> elif i == <NUM_LIT:3> : <EOL> searchResults = self . doSearchInUnicode ( searchTerm , glyphOrder , unicodeData ) <EOL> elif i == <NUM_LIT:4> : <EOL> searchResults = self . doSearchInUnicodeCategory ( searchTerm , glyphOrder , unicodeData ) <EOL> else : <EOL> print '<STR_LIT>' , i <EOL> self . w . tabs [ i ] . glyphList . set ( searchResults ) <EOL> self . w . tabs [ i ] . glyphList . setSelection ( range ( <NUM_LIT:0> , len ( searchResults ) ) ) <EOL> def selectInFont ( self ) : <EOL> applySelectionIndex = self . w . applySelectionRadio . get ( ) <EOL> if applySelectionIndex == <NUM_LIT:1> : <EOL> fonts = self . getAllFonts ( ) <EOL> elif applySelectionIndex == <NUM_LIT:0> : <EOL> fonts = [ self . getFont ( ) ] <EOL> for f in fonts : <EOL> fontSet = set ( f . keys ( ) ) <EOL> selectionSet = set ( self . getExistingSelection ( ) ) <EOL> resultSet = fontSet . intersection ( selectionSet ) <EOL> f . selection = list ( resultSet ) <EOL> f . update ( ) <EOL> def addToSelection ( self , sender ) : <EOL> existingSet = set ( self . getExistingSelection ( ) ) <EOL> newSet = set ( self . getNewSelection ( ) ) <EOL> bothSet = existingSet | newSet <EOL> self . setExistingSelection ( list ( bothSet ) ) <EOL> self . selectInFont ( ) <EOL> def intersectWithSelection ( self , sender ) : <EOL> existingSet = set ( self . getExistingSelection ( ) ) <EOL> newSet = set ( self . getNewSelection ( ) ) <EOL> bothSet = existingSet & newSet <EOL> self . setExistingSelection ( list ( bothSet ) ) <EOL> self . selectInFont ( ) <EOL> def subtractFromSelection ( self , sender ) : <EOL> existingSet = set ( self . getExistingSelection ( ) ) <EOL> newSet = set ( self . getNewSelection ( ) ) <EOL> diffSet = existingSet - newSet <EOL> self . setExistingSelection ( list ( diffSet ) ) <EOL> self . selectInFont ( ) <EOL> def replaceSelection ( self , sender ) : <EOL> self . setExistingSelection ( self . getNewSelection ( ) ) <EOL> self . selectInFont ( ) <EOL> def clearSelection ( self , sender ) : <EOL> self . setExistingSelection ( [ ] ) <EOL> self . selectInFont ( ) <EOL> if __name__ is "<STR_LIT:__main__>" : <EOL> OpenWindow ( SelectGlyphSearch ) </s>
