def awakeFromNib(self): NSLog("Awake from nib.") self.setPreviewMode(True) self.bodyField.setDelegate_(self) self.urlField.setDelegate_(self) self.titleField.setDelegate_(self) # Style the bodyField. self.bodyField.setFont_(NSFont.fontWithName_size_("Monaco", 13)) self.bodyField.setRichText_(NO) self.bodyField.setUsesFontPanel_(NO) # Authenticate to twitter if we can. if self.twitter.is_authenticated(): self.twitter.login() self.twitterCheckbox.setState_(NSOnState) self.ltp.syndicators.append(self.twitter) # Authenticate to G+ if we can. if self.gplus.is_authenticated(): self.gplus.login() self.gplusCheckbox.setState_(NSOnState) self.ltp.syndicators.append(self.gplus) # Listen to the NSApplicationWillTerminateNotification. center = NSNotificationCenter.defaultCenter() center.addObserver_selector_name_object_(self, "applicationWillTerminateNotification:", NSApplicationWillTerminateNotification, None) self.setupStatusBar() self.didPublish = False self.didPreview = False
def _setFont(self, font): self.font.getNSTextField().setFont_(NSFont.fontWithName_size_(font.fontName(), 10)) size = font.pointSize() if size == int(size): size = int(size) s = u"%s %spt" % (font.displayName(), size) self.font.set(s)
def __init__(self): # Window 'self.w': edY = 200 txY = 14 spX = 10 spY = 5 btnX = 250 btnY = 20 windowWidth = 350 windowHeight = edY * 2 + spY * 6 + txY * 2 + btnY + 14 self.w = vanilla.FloatingWindow( (windowWidth, windowHeight), # default window size "Analyse Manuscript", # window title autosaveName= "com.Tosche.AnalyseManuscript.mainwindow" # stores last window position and size ) # UI elements: self.w.text1 = vanilla.TextBox((spX, spY, -spX, txY), 'Paste your text below...', sizeStyle='small') self.w.dump = vanilla.TextEditor((spX, spY * 2 + txY, -spX, edY), "", callback=self.updateChar) self.w.text2 = vanilla.TextBox((spX, spY * 3 + txY + edY, -spX, txY), "0 Unicode characters", sizeStyle='small') self.w.chars = vanilla.TextEditor( (spX, spY * 4 + txY * 2 + edY, -spX, edY), "", readOnly=True) self.w.chars._textView.setFont_(NSFont.fontWithName_size_("Menlo", 12)) self.w.dump._textView.setAutomaticSpellingCorrectionEnabled_(False) self.w.dump._textView.setAutomaticTextReplacementEnabled_(False) self.w.dump._textView.setContinuousSpellCheckingEnabled_(False) self.w.dump._textView.setGrammarCheckingEnabled_(False) self.w.dump._textView.setAutomaticQuoteSubstitutionEnabled_(False) # Run Button: self.w.markPopup = vanilla.PopUpButton( (spX, -btnY - spY - 7, 70, -spY - 7), [ "Mark", "Red", "Orange", "Brown", "Yellow", "Light Green", "Dark Green", "Cyan", "Blue", "Purple", "Pink", "Light Grey", "Dark Grey" ], callback=self.markGlyphs) self.w.runButton = vanilla.Button( (-btnX - spX, -btnY - spY - 7, -spX, -spY - 7), "Add missing characters", sizeStyle='regular', callback=self.AnalyseManuscriptMain) self.w.setDefaultButton(self.w.runButton) # Open window and focus on it: self.w.open() menu = self.w.markPopup._nsObject.menu() menu.setAutoenablesItems_(False) menu.itemAtIndex_(0).setEnabled_(False) divider = NSMenuItem.separatorItem() menu.insertItem_atIndex_(divider, 1) self.w.makeKey()
def getFontPathOfFont(fontName): font = NSFont.fontWithName_size_(fontName, 25) if font is not None: fontRef = CTFontDescriptorCreateWithNameAndSize( font.fontName(), font.pointSize()) url = CTFontDescriptorCopyAttribute(fontRef, kCTFontURLAttribute) return url.path() return None
def getFontPathOfFont(self, fontName): u"""Answer the path that is source of the given font name. Answer None if the font cannot be found.""" nsFont = NSFont.fontWithName_size_(fontName, 25) if nsFont is not None: fontRef = CTFontDescriptorCreateWithNameAndSize( nsFont.fontName(), nsFont.pointSize()) url = CTFontDescriptorCopyAttribute(fontRef, kCTFontURLAttribute) return url.path() return None
def main(): # "Skia-Regular 14.00 pt. P [] (0x7ff98cb0b410) fobj=0x7ff98cb0ad90, spc=3.50" font = NSFont.fontWithName_size_("HelveticaNeue-Medium",14.0) font = NSFont.fontWithName_size_("Avenir Next LT Pro Ultra Light Italic",14.0) print "font is", font print "font.displayName() is", font.displayName() # attributes = NSDictionary.dictionaryWithObjectsAndKeys_(font, NSFontAttributeName, None ) # print "attributes is", attributes s = "CGColorCreateGenericRGB(0.8, 0.8, 0.8, 0.4)" # print dir(NSAttributedString) # s2 = NSAttributedString.alloc().initWithString_attributes_(s, attributes) # # print "s2 is", s2 s3 = NSAttributedString.alloc().initWithString_attributes_(s, { "NSFont":font }) print "s3 is", s3 # , "s2 == s3", s2 == s3 print "s3.size() is", s3.size() # s3.size() is <NSSize width=293.0 height=21.0> cat = CATextLayer.alloc().init() cat.setString_(s3) # cat.setFont_(font) # cat.setFontSize_(font.pointSize() ) # necessary after setFont? s4 = cat.string() print "s4 is", s4, "s3 == s4", s3 == s4 print "cat.bounds() is", cat.frame()
def DrawText(font, fontsize, fontcolour, x, y, text): attributes = { NSFontAttributeName: NSFont.fontWithName_size_(font, fontsize), NSForegroundColorAttributeName: NSColor.colorWithDeviceCyan_magenta_yellow_black_alpha_( fontcolour[0], fontcolour[1], fontcolour[2], fontcolour[3], 1) } String = NSAttributedString.alloc().initWithString_attributes_( text, attributes) String.drawAtPoint_((x, y))
def __init__(self, posSize, text, callback=None): # there must be a callback as it triggers the creation of the delegate if callback is None: callback = self._fallbackCallback super(FeatureTextEditor, self).__init__(posSize, "", callback=callback) self._nsObject.setHasHorizontalScroller_(True) font = NSFont.fontWithName_size_("Monaco", 10) self._textView.setFont_(font) self._textView.setUsesFindPanel_(True) ## line numbers #ruler = DefconAppKitLineNumberView.alloc().init() #ruler.setClientView_(self._textView) #self._nsObject.setVerticalRulerView_(ruler) #self._nsObject.setHasHorizontalRuler_(False) #self._nsObject.setHasVerticalRuler_(True) #self._nsObject.setRulersVisible_(True) #notificationCenter = NSNotificationCenter.defaultCenter() #notificationCenter.addObserver_selector_name_object_( # ruler, "clientViewSelectionChanged:", NSTextViewDidChangeSelectionNotification, self._textView #) # colors self._mainColor = NSColor.blackColor() self._commentColor = NSColor.colorWithCalibratedWhite_alpha_(.6, 1) self._keywordColor = NSColor.colorWithCalibratedRed_green_blue_alpha_( .8, 0, 0, 1) self._tokenColor = NSColor.colorWithCalibratedRed_green_blue_alpha_( .8, .4, 0, 1) self._classNameColor = NSColor.colorWithCalibratedRed_green_blue_alpha_( 0, 0, .8, 1) self._includeColor = NSColor.colorWithCalibratedRed_green_blue_alpha_( .8, 0, .8, 1) self._stringColor = NSColor.colorWithCalibratedRed_green_blue_alpha_( 0, .6, 0, 1) # build the placard placardW = 65 placardH = 16 self._placardJumps = [] self._placard = vanilla.Group((0, 0, placardW, placardH)) self._placard.featureJumpButton = PlacardPopUpButton( (0, 0, placardW, placardH), [], callback=self._placardFeatureSelectionCallback, sizeStyle="mini") self._nsObject.setPlacard_(self._placard.getNSView()) # registed for syntax coloring notifications self._programmaticallySettingText = False delegate = self._textViewDelegate delegate.vanillaWrapper = weakref.ref(self) notificationCenter = NSNotificationCenter.defaultCenter() notificationCenter.addObserver_selector_name_object_( self._textViewDelegate, "textStorageDidProcessEditing:", NSTextStorageDidProcessEditingNotification, self._textView.textStorage()) # set the text self.set(text)
def tokenString(self): font = NSFont.fontWithName_size_(fallbackFont.fontName(), 10) attr = _textAttributesForStyle(self._style, font) attr[NSForegroundColorAttributeName] = self.color() if NSUnderlineColorAttributeName in attr: attr[NSUnderlineColorAttributeName] = self.color() txt = self.token() if txt == "Token": txt = "Fallback" else: txt = txt.replace("Token.", "") return NSMutableAttributedString.alloc().initWithString_attributes_(txt, attr)
def __init__(self, font, size): self._max_size = NSMakeSize(sys.float_info.max, sys.float_info.max) self._options = 1 << 3 # NSStringDrawingOptions.NSStringDrawingUsesDeiceMetrics # https://developer.apple.com/documentation/uikit/nsstringdrawingoptions?language=objc if font == "<System>": nsfont = NSFont.systemFontOfSize_(size) else: nsfont = NSFont.fontWithName_size_(font, size) self._attributes = NSMutableDictionary.dictionary() self._attributes.setObject_forKey_(nsfont, NSFontAttributeName) self._terminator = '1' self._terminator_width = self._measure(self._terminator)
def macos_font(loader, node): seq = loader.construct_sequence(node) if len(seq) != 2: raise ValueError('only two arguments may be provided, the font name and size') font_name, font_size = seq font = NSFont.fontWithName_size_(font_name, font_size) if font is None: raise ValueError('unable to find the font requested') return bytes(NSKeyedArchiver.archivedDataWithRootObject_(font))
def fontName2FontPath(self, fontName): u"""Answer the unchanged path, if it exists as file. Answer the path that is source of the given font name. Answer None if the font cannot be found.""" # If the font cannot be found by name, then test if the file exists as path and answer it. if os.path.exists(fontName): # return fontName # Otherwise try OSX for the conversion. nsFont = NSFont.fontWithName_size_(fontName, 25) if nsFont is not None: fontRef = CTFontDescriptorCreateWithNameAndSize( nsFont.fontName(), nsFont.pointSize()) url = CTFontDescriptorCopyAttribute(fontRef, kCTFontURLAttribute) return url.path() return None
def __init__(self, posSize, text, callback=None): # there must be a callback as it triggers the creation of the delegate if callback is None: callback = self._fallbackCallback super(FeatureTextEditor, self).__init__(posSize, "", callback=callback) self._nsObject.setHasHorizontalScroller_(True) font = NSFont.fontWithName_size_("Monaco", 10) self._textView.setFont_(font) self._textView.setUsesFindPanel_(True) ## line numbers #ruler = DefconAppKitLineNumberView.alloc().init() #ruler.setClientView_(self._textView) #self._nsObject.setVerticalRulerView_(ruler) #self._nsObject.setHasHorizontalRuler_(False) #self._nsObject.setHasVerticalRuler_(True) #self._nsObject.setRulersVisible_(True) #notificationCenter = NSNotificationCenter.defaultCenter() #notificationCenter.addObserver_selector_name_object_( # ruler, "clientViewSelectionChanged:", NSTextViewDidChangeSelectionNotification, self._textView #) # colors self._mainColor = NSColor.blackColor() self._commentColor = NSColor.colorWithCalibratedWhite_alpha_(.6, 1) self._keywordColor = NSColor.colorWithCalibratedRed_green_blue_alpha_(.8, 0, 0, 1) self._tokenColor = NSColor.colorWithCalibratedRed_green_blue_alpha_(.8, .4, 0, 1) self._classNameColor = NSColor.colorWithCalibratedRed_green_blue_alpha_(0, 0, .8, 1) self._includeColor = NSColor.colorWithCalibratedRed_green_blue_alpha_(.8, 0, .8, 1) self._stringColor = NSColor.colorWithCalibratedRed_green_blue_alpha_(0, .6, 0, 1) # build the placard placardW = 65 placardH = 16 self._placardJumps = [] self._placard = vanilla.Group((0, 0, placardW, placardH)) self._placard.featureJumpButton = PlacardPopUpButton((0, 0, placardW, placardH), [], callback=self._placardFeatureSelectionCallback, sizeStyle="mini") self._nsObject.setPlacard_(self._placard.getNSView()) # registed for syntax coloring notifications self._programmaticallySettingText = False delegate = self._textViewDelegate delegate.vanillaWrapper = weakref.ref(self) notificationCenter = NSNotificationCenter.defaultCenter() notificationCenter.addObserver_selector_name_object_( self._textViewDelegate, "textStorageDidProcessEditing", NSTextStorageDidProcessEditingNotification, self._textView.textStorage()) # set the text self.set(text)
def descent(fontname, fontsize=10): return NSFont.fontWithName_size_(fontname, fontsize).descender()
def fontSize(fontSize): # Set the font size. currentFont = NSFont.fontWithName_size_(currentFont.fontName(), fontSize)
def font(fontName, fontSize=None): # Set the font by PostScript name. # Optionally set the font size. if fontSize is None: fontSize = NSFont.systemFontSize() NSFont.fontWithName_size_(fontName, fontSize)
from AppKit import NSFont monoFont = NSFont.fontWithName_size_("Monaco", 13)
s = FormattedString('bla', font='Helvetica', fontSize=fontSize, lineHeight=lineHeight, fill=(0.4, 0.5, 0.2)) fontSize = 300 #lineHeight = fontSize * leading s.fontSize(fontSize) s.lineHeight(lineHeight) # Keeps value of first part. s.font('Helvetica-Bold') s.fill(0.3, 0.2, 0.9) s += 'bla2' text(s, (0, lineHeight)) print('formatted string leading %s' % s.fontLeading()) print('formatted string size %s' % s.size()) print('formatted string line height %s' % s.fontLineHeight()) ''' print(s.fontAscender()) print(s.fontDescender()) ''' w, h = s.size() fill(None) stroke(0) rect(0, lineHeight, w, h) from AppKit import NSFont font = NSFont.fontWithName_size_("Helvetica", 12) print(font.leading())
from AppKit import NSFontAttributeName, NSFont, NSForegroundColorAttributeName, NSColor, NSActionCell, \ NSBezierPath, NSAttributedString pillTextAttributes = { NSFontAttributeName: NSFont.fontWithName_size_("Helvetica Bold", 12.0), NSForegroundColorAttributeName: NSColor.whiteColor() } pillColor = NSColor.colorWithCalibratedRed_green_blue_alpha_(.75, .75, .8, 1.0) class DefconAppKitPillCell(NSActionCell): def setColor_(self, color): self._color = color def drawWithFrame_inView_(self, frame, view): row = view.selectedRow() columnCount = len(view.tableColumns()) frames = [ view.frameOfCellAtColumn_row_(i, row) for i in range(columnCount) ] selected = frame in frames (x, y), (w, h) = frame y += 1 h -= 2 if selected: pillTextAttributes[NSForegroundColorAttributeName] = self._color foregroundColor = NSColor.whiteColor() else:
edY = 22 txY = 17 spX = 10 spY = 10 btnX = 160 btnY = 22 tab0options = [ "Options", " New Set", " Save Set in Favourites", " Save Set in Favourites As...", " Delete Set from Favourites...", " Copy Set as Flat Text", "Favourites", ] Menlo12 = NSFont.fontWithName_size_("Menlo", 12) toolOrderDragType = "toolOrderDragType" # Load tab0 pairs if Glyphs.defaults["com.Tosche.BubbleKern.favDic"] != None: favDic = Glyphs.defaults["com.Tosche.BubbleKern.favDic"] favDic = NSMutableDictionary.alloc().initWithDictionary_copyItems_( favDic, True) else: # Fallback to default favourite dictionary favDic = { "Sample": [ [ "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z", "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z", False,
def __init__(self, name, size): self.ns_font = NSFont.fontWithName_size_(name, size)
def DrawText(font, fontsize, fontcolour, x, y, text): attributes = {NSFontAttributeName : NSFont.fontWithName_size_(font, fontsize), NSForegroundColorAttributeName: NSColor.colorWithDeviceCyan_magenta_yellow_black_alpha_(fontcolour[0], fontcolour[1], fontcolour[2], fontcolour[3], 1)} String = NSAttributedString.alloc().initWithString_attributes_(text, attributes) String.drawAtPoint_((x, y))
from AppKit import NSFontAttributeName, NSFont, NSForegroundColorAttributeName, NSColor, NSActionCell, \ NSBezierPath, NSAttributedString pillTextAttributes = { NSFontAttributeName: NSFont.fontWithName_size_("Helvetica Bold", 12.0), NSForegroundColorAttributeName: NSColor.whiteColor() } pillColor = NSColor.colorWithCalibratedRed_green_blue_alpha_(.75, .75, .8, 1.0) class DefconAppKitPillCell(NSActionCell): def setColor_(self, color): self._color = color def drawWithFrame_inView_(self, frame, view): row = view.selectedRow() columnCount = len(view.tableColumns()) frames = [view.frameOfCellAtColumn_row_(i, row) for i in xrange(columnCount)] selected = frame in frames (x, y), (w, h) = frame y += 1 h -= 2 if selected: pillTextAttributes[NSForegroundColorAttributeName] = self._color foregroundColor = NSColor.whiteColor()
p1 = NSBezierPath.bezierPathWithOvalInRect_(((0, 0), (100, 100))) p2 = NSBezierPath.bezierPathWithOvalInRect_(((40, 40), (100, 100))) # Outside of bounding box p3 = NSBezierPath.bezierPathWithOvalInRect_(((110, 0), (100, 100))) # In bounding box, doesn't intersect p4 = NSBezierPath.bezierPathWithOvalInRect_(((72, 72), (100, 100))) from cPolymagic import * print intersects(p1, p4) f = NSFont.fontWithName_size_("Helvetica", 72) fp1 = NSBezierPath.bezierPath() fp1.moveToPoint_((100, 100)) fp1.appendBezierPathWithGlyph_inFont_(68, f) fp2 = NSBezierPath.bezierPath() fp2.moveToPoint_((110, 100)) fp2.appendBezierPathWithGlyph_inFont_(68, f) print intersects(fp1, fp2) # Some other thing inside of the function perhaps? print intersects(fp2, fp2) p = union(fp1, fp2)