def write_text(self, text, x, y, font, *args, **kwargs): # Set font family and size if font: write_font = font elif self.native.font: write_font = self.native.font else: raise ValueError("No font to write with") width, height = write_font.measure(text) textAttributes = NSMutableDictionary.alloc().init() textAttributes[NSFontAttributeName] = write_font._impl.native if "stroke_color" in kwargs and "fill_color" in kwargs: textAttributes[NSStrokeColorAttributeName] = native_color( kwargs["stroke_color"]) # Apply negative NSStrokeWidthAttributeName to get stroke and fill textAttributes[ NSStrokeWidthAttributeName] = -1 * kwargs["text_line_width"] textAttributes[NSForegroundColorAttributeName] = native_color( kwargs["fill_color"]) elif "stroke_color" in kwargs: textAttributes[NSStrokeColorAttributeName] = native_color( kwargs["stroke_color"]) textAttributes[NSStrokeWidthAttributeName] = kwargs[ "text_line_width"] elif "fill_color" in kwargs: textAttributes[NSForegroundColorAttributeName] = native_color( kwargs["fill_color"]) else: raise ValueError("No stroke or fill of write text") text_string = NSAttributedString.alloc().initWithString_attributes_( text, textAttributes) text_string.drawAtPoint(NSPoint(x, y - height))
def write_text(self, text, x, y, font, *args, **kwargs): width, height = self.measure_text(text, font) textAttributes = NSMutableDictionary.alloc().init() textAttributes[NSFontAttributeName] = font.bind( self.interface.factory).native if "stroke_color" in kwargs and "fill_color" in kwargs: textAttributes[NSStrokeColorAttributeName] = native_color( kwargs["stroke_color"]) # Apply negative NSStrokeWidthAttributeName to get stroke and fill textAttributes[ NSStrokeWidthAttributeName] = -1 * kwargs["text_line_width"] textAttributes[NSForegroundColorAttributeName] = native_color( kwargs["fill_color"]) elif "stroke_color" in kwargs: textAttributes[NSStrokeColorAttributeName] = native_color( kwargs["stroke_color"]) textAttributes[NSStrokeWidthAttributeName] = kwargs[ "text_line_width"] elif "fill_color" in kwargs: textAttributes[NSForegroundColorAttributeName] = native_color( kwargs["fill_color"]) else: raise ValueError("No stroke or fill of write text") text_string = NSAttributedString.alloc().initWithString_attributes_( text, textAttributes) text_string.drawAtPoint(NSPoint(x, y - height))
def measure(self, text, tight=False): textAttributes = NSMutableDictionary.alloc().init() textAttributes[NSFontAttributeName] = self.native text_string = NSAttributedString.alloc().initWithString_attributes_(text, textAttributes) size = text_string.size() size.width += 3 return size.width, size.height
def measure(self, text, tight=False): textAttributes = NSMutableDictionary.alloc().init() textAttributes[NSFontAttributeName] = self.native text_string = NSAttributedString.alloc().initWithString_attributes_( text, textAttributes) size = text_string.size() size.width += 3 return size.width, size.height
def measure(self, text, tight=False): textAttributes = NSMutableDictionary.alloc().init() textAttributes[NSFontAttributeName] = self.native text_string = NSAttributedString.alloc().initWithString_attributes_(text, textAttributes) size = text_string.size() # TODO: This is a magic fudge factor... # Replace the magic with SCIENCE. size.width += 3 return size.width, size.height
def _update(self): style = self.interface.style font = InterfaceFont(style.font_family, style.font_size) attributes = NSDictionary.dictionaryWithObjects( [self.interface.url, font.bind(self.interface.factory).native], forKeys=[NSLinkAttributeName, NSFontAttributeName], ) self.attr_string = NSAttributedString.alloc().initWithString( self.interface.text, attributes=attributes) self.native.textStorage.setAttributedString(self.attr_string) self.rehint()