def drawRect_(self, rect): NSColor.whiteColor().set() NSBezierPath.fillRect_(rect) if Glyphs.font is None: return if not Glyphs.font.selectedLayers: return glyphToRotate = None try: glyphToRotate = Glyphs.font.selectedLayers[0] except: print(traceback.format_exc()) if glyphToRotate is None: return try: previewPath = glyphToRotate.completeBezierPath rotationFactor = self.wrapper._rotationFactor Width = NSWidth(self.frame()) Height = NSHeight(self.frame()) scaleFactor = 0.666666 / (glyphToRotate.parent.parent.upm / min(Width, Height)) ## scaling and zeroing the glyph #------------------------ transform = NSAffineTransform.transform() transform.scaleBy_(scaleFactor) bounds = glyphToRotate.bounds transform.translateXBy_yBy_(-NSMidX(bounds), -NSMidY(bounds)) previewPath.transformUsingAffineTransform_(transform) ## rotation #------------------------ transform = NSAffineTransform.transform() transform.rotateByDegrees_(rotationFactor) previewPath.transformUsingAffineTransform_(transform) ## positioning to the middle of the viewport #------------------------ transform = NSAffineTransform.transform() transform.translateXBy_yBy_(Width / 2, Height / 2 - 8) previewPath.transformUsingAffineTransform_(transform) ## fill path #------------------------ NSColor.blackColor().set() previewPath.fill() except: print(traceback.format_exc())
def initImage(layer, width, height, pixel_size=default_pixel_size, ratio=1): # See https://developer.apple.com/documentation/appkit/nsbitmapimagerep/1395538-init img = NSBitmapImageRep.alloc( ).initWithBitmapDataPlanes_pixelsWide_pixelsHigh_bitsPerSample_samplesPerPixel_hasAlpha_isPlanar_colorSpaceName_bitmapFormat_bytesPerRow_bitsPerPixel_( None, # BitmapDataPlanes int(round(width / pixel_size)), # pixelsWide int(round(height / pixel_size / ratio)), # pixelsHigh 8, # bitsPerSample: 1, 2, 4, 8, 12, or 16 1, # samplesPerPixel: 1 - 5 False, # hasAlpha False, # isPlanar NSDeviceWhiteColorSpace, # colorSpaceName # NSDeviceRGBColorSpace, 0, # bitmapFormat 0, # bytesPerRow 0, # bitsPerPixel ) """ NSCalibratedWhiteColorSpace NSCalibratedBlackColorSpace NSCalibratedRGBColorSpace NSDeviceWhiteColorSpace NSDeviceBlackColorSpace NSDeviceRGBColorSpace NSDeviceCMYKColorSpace NSNamedColorSpace NSCustomColorSpace """ # The image is filled black for some reason, make it white current = NSGraphicsContext.currentContext() context = NSGraphicsContext.graphicsContextWithBitmapImageRep_(img) NSGraphicsContext.setCurrentContext_(context) NSColor.whiteColor().set() # NSBezierPath.setLineWidth_(1) NSBezierPath.fillRect_(NSMakeRect(0, 0, width, int(round(height / ratio)))) NSGraphicsContext.setCurrentContext_(current) return img
def erase_rect(self, r): self._backcolor._ns_color.set() NSBezierPath.fillRect_(_ns_rect(r))
def fill_rect(self, r): self._fillcolor._ns_color.set() NSBezierPath.fillRect_(_ns_rect(r))
def erase_rect(self, r): self._backcolor._ns_color.set() NSBezierPath.fillRect_(_ns_rect(r))
def fill_rect(self, r): self._fillcolor._ns_color.set() NSBezierPath.fillRect_(_ns_rect(r))
def drawRect_(self, rect): self.wrapper._backColour.set() NSBezierPath.fillRect_(rect) sizes = [ 8, 9, 10, 11, 12, 13, 14, 16, 18, 20, 24, 28, 32, 36, 48, 60, 72, 90, 120 ] lineSpace = 8 tab = 30 w = NSWidth(self.frame()) h = NSHeight(self.frame()) glyphNames = self.wrapper._glyphsList insIndex = self.wrapper._instanceIndex if insIndex == 0: font = Glyphs.font m = font.selectedFontMaster else: instance = Glyphs.font.instances[insIndex - 1] font = self.wrapper.instances.get(instance.name) if font is None: font = instance.interpolatedFont self.wrapper.instances[instance.name] = font m = font.masters[0] fullPath = NSBezierPath.alloc().init() advance = 0 self.wrapper._foreColour.set() try: for i, glyphName in enumerate(glyphNames): glyph = self.glyphForName(glyphName, font) if glyph: layer = glyph.layers[m.id] layerPath = layer.completeBezierPath kernValue = 0 # kerning check if i + 1 < len(glyphNames): nextGlyphName = glyphNames[i + 1] nextGlyph = self.glyphForName(nextGlyphName, font) if nextGlyph: nextLayer = nextGlyph.layers[m.id] if nextLayer: kernValue = getKernValue(layer, nextLayer) if kernValue > 10000: kernValue = 0 transform = NSAffineTransform.transform() transform.translateXBy_yBy_(advance, 0) layerPath.transformUsingAffineTransform_(transform) advance += layer.width + kernValue fullPath.appendBezierPath_(layerPath) except: print(traceback.format_exc()) if fullPath is None: return try: sSum = 0 upm = float(font.upm) for i, s in enumerate(sizes): sSum += s + s / 4 transform = NSAffineTransform.transform() transform.scaleBy_(s / upm) transform.translateXBy_yBy_(tab * upm / s, (h - s - sSum) * upm / s) self.drawText(str(s), self.wrapper._foreColour, 10, h - s - sSum - 2) fullPath.transformUsingAffineTransform_(transform) fullPath.fill() transform.invert() fullPath.transformUsingAffineTransform_(transform) except StandardError: print(traceback.format_exc())