def _drawUnspecified(self, position, kind, size, vector=(-1, 1)): if vector is None: vector = (-1, 1) angle = atan2(vector[1], vector[0]) circle_size = size * 1.3 x, y = position NSColor.colorWithCalibratedRed_green_blue_alpha_(0.9, 0.1, 0.0, 0.85).set() t = NSAffineTransform.transform() t.translateXBy_yBy_(x, y) t.rotateByRadians_(angle) myPath = NSBezierPath.alloc().init() myPath.setLineWidth_(0) myPath.appendBezierPathWithOvalInRect_( NSMakeRect(x - 0.5 * circle_size, y - 0.5 * circle_size, circle_size, circle_size)) myPath.stroke() if self.show_labels or distance_between_points(self.mouse_position, position) < size: self._drawTextLabel( transform=t, text=kind, size=size, angle=angle, )
def setPixel(self, event, dragging=False): if self.data is None: return False try: editView = self.editViewController().graphicView() except: return False layer = editView.activeLayer() try: master = layer.parent.parent.masters[layer.layerId] except KeyError: return False if master is None: return False # Get location of click in font coordinates Loc = editView.getActiveLocation_(event) loc_pixel = ((Loc.x - self.rect.origin.x) / self.pixel_size, (Loc.y - self.rect.origin.y) / self.pixel_size / self.pixel_ratio) if self.prev_location != loc_pixel: x, y = loc_pixel current = NSGraphicsContext.currentContext() context = NSGraphicsContext.graphicsContextWithBitmapImageRep_( self.data) if context is None: self.prev_location = loc_pixel print("Could not get context in setPixel") return False NSGraphicsContext.saveGraphicsState() NSGraphicsContext.setCurrentContext_(context) if self.erase: NSColor.whiteColor().set() else: NSColor.blackColor().set() effective_size = self.pen_size / self.pixel_size if dragging and self.prev_location is not None: px, py = self.prev_location path = NSBezierPath.alloc().init() path.setLineCapStyle_(NSRoundLineCapStyle) path.setLineWidth_(effective_size) # path.strokeLineFromPoint_toPoint_(x, y, x + self.pen_size, y + self.pen_size) path.moveToPoint_((px, py)) path.lineToPoint_((x, y)) path.stroke() self.needs_save = True else: half = effective_size / 2 rect = NSMakeRect(x - half, y - half, effective_size, effective_size) path = NSBezierPath.bezierPathWithOvalInRect_(rect) path.fill() self.needs_save = True # For rectangular pens: # NSBezierPath.fillRect_(rect) NSGraphicsContext.setCurrentContext_(current) NSGraphicsContext.restoreGraphicsState() self.prev_location = loc_pixel return True
def drawCoveringBadge(self, x, y, width, height, radius): try: myPath = NSBezierPath.alloc().init() NSColor.colorWithCalibratedRed_green_blue_alpha_(*COLOR).set() myRect = NSRect((x, y), (width, height)) thisPath = NSBezierPath.bezierPathWithRoundedRect_cornerRadius_( myRect, radius) myPath.appendBezierPath_(thisPath) myPath.fill() except: print traceback.format_exc()
def _drawArrow(self, position, kind, size, vector=(-1, 1), level="e"): if vector is None: vector = (-1, 1) angle = atan2(vector[0], -vector[1]) size *= 2 x, y = position head_ratio = 0.7 w = size * 0.5 tail_width = 0.3 chin = 0.5 * (w - w * tail_width) # part under the head if level == "e": arrow_color = error_color else: arrow_color = warning_color NSColor.colorWithCalibratedRed_green_blue_alpha_(*arrow_color).set() t = NSAffineTransform.transform() t.translateXBy_yBy_(x, y) t.rotateByRadians_(angle) myPath = NSBezierPath.alloc().init() myPath.moveToPoint_((0, 0)) myPath.relativeLineToPoint_((-size * head_ratio, w * 0.5)) myPath.relativeLineToPoint_((0, -chin)) myPath.relativeLineToPoint_((-size * (1 - head_ratio), 0)) myPath.relativeLineToPoint_((0, -w * tail_width)) myPath.relativeLineToPoint_((size * (1 - head_ratio), 0)) myPath.relativeLineToPoint_((0, -chin)) myPath.closePath() myPath.transformUsingAffineTransform_(t) myPath.fill() percent = 1 if not self.show_labels: percent = ( -distance_between_points(self.mouse_position, position) / size * 2 + 2) if self.show_labels or percent > 0.2: self._drawTextLabel( transform=t, text=kind, size=size, vector=vector, percent=percent, )
def drawPath(self, path=[]): """ Draw the points from path to a NSBezierPath. """ subpath = NSBezierPath.alloc().init() subpath.moveToPoint_(path[0][0]) for p in path[1:]: if len(p) == 3: # curve A, B, C = p subpath.curveToPoint_controlPoint1_controlPoint2_(C, A, B) else: subpath.lineToPoint_(p[0]) subpath.closePath() NSColor.colorWithCalibratedRed_green_blue_alpha_(0, 0, 1, self.alpha).set() subpath.stroke()
def _drawUnspecified(self, position, kind, size, vector=(-1, 1), level="e"): if vector is None: vector = (-1, 1) angle = atan2(vector[1], vector[0]) circle_size = size * 1.3 x, y = position if level == "e": arrow_color = error_color else: arrow_color = warning_color NSColor.colorWithCalibratedRed_green_blue_alpha_(*arrow_color).set() t = NSAffineTransform.transform() t.translateXBy_yBy_(x, y) t.rotateByRadians_(angle) myPath = NSBezierPath.alloc().init() myPath.setLineWidth_(0) myPath.appendBezierPathWithOvalInRect_( NSMakeRect( x - 0.5 * circle_size, y - 0.5 * circle_size, circle_size, circle_size, )) myPath.stroke() percent = (-distance_between_points(self.mouse_position, position) / size * 2 + 2) if self.show_labels or percent > 0.2: self._drawTextLabel( transform=t, text=kind, size=size, vector=vector, percent=percent, )
def _drawArrow(self, position, kind, size, vector=(-1, 1)): if vector is None: vector = (-1, 1) angle = atan2(vector[0], -vector[1]) size *= 2 x, y = position head_ratio = 0.7 w = size * 0.5 tail_width = 0.3 chin = 0.5 * (w - w * tail_width) # part under the head NSColor.colorWithCalibratedRed_green_blue_alpha_(0.9, 0.1, 0.0, 0.85).set() t = NSAffineTransform.transform() t.translateXBy_yBy_(x, y) t.rotateByRadians_(angle) myPath = NSBezierPath.alloc().init() myPath.moveToPoint_((0, 0)) myPath.relativeLineToPoint_((-size * head_ratio, w * 0.5)) myPath.relativeLineToPoint_((0, -chin)) myPath.relativeLineToPoint_((-size * (1 - head_ratio), 0)) myPath.relativeLineToPoint_((0, -w * tail_width)) myPath.relativeLineToPoint_((size * (1 - head_ratio), 0)) myPath.relativeLineToPoint_((0, -chin)) myPath.closePath() myPath.transformUsingAffineTransform_(t) myPath.fill() if self.show_labels or distance_between_points(self.mouse_position, position) < size: self._drawTextLabel( transform=t, text=kind, size=size, vector=vector, )
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())
def _drawArrow(position, kind, size, vector=(-1, 1), label_size=1): angle = atan2(vector[0], -vector[1]) print vector, "%0.2f°" % degrees(angle) x, y = position head_ratio = 0.7 w = size * 0.5 tail_width = 0.3 chin = 0.5 * (w - w * tail_width) # part under the head NSColor.colorWithCalibratedRed_green_blue_alpha_( 0.9, 0.1, 0.0, 0.85 ).set() t = NSAffineTransform.transform() t.translateXBy_yBy_(x, y) t.rotateByRadians_(angle) myPath = NSBezierPath.alloc().init() myPath.moveToPoint_( (0, 0) ) myPath.relativeLineToPoint_( (-size * head_ratio, w * 0.5) ) myPath.relativeLineToPoint_( (0, -chin) ) myPath.relativeLineToPoint_( (-size * (1 - head_ratio), 0) ) myPath.relativeLineToPoint_( (0, -w * tail_width) ) myPath.relativeLineToPoint_( (size * (1 - head_ratio), 0) ) myPath.relativeLineToPoint_( (0, -chin) ) myPath.closePath() myPath.transformUsingAffineTransform_(t) myPath.fill() drawPath(myPath) myString = NSString.string().stringByAppendingString_(kind) attrs = { NSFontAttributeName: NSFont.systemFontOfSize_(label_size), NSForegroundColorAttributeName: NSColor.colorWithCalibratedRed_green_blue_alpha_( 0.4, 0.4, 0.6, 0.7 ), } bbox = myString.sizeWithAttributes_(attrs) #print bbox p = NSPoint() bw = bbox.width bh = bbox.height #print " ", cos(angle) if -0.5 * pi < angle <= 0.5 * pi: p.x, p.y = ( - size - 20 - bh/2 * sin(angle) - bw/2 * cos(angle), # + bw/2.0 * cos(angle - pi) 0 ) else: p.x, p.y = ( - size - 20 + bh/2 * sin(angle) + bw/2 * cos(angle), # + bw/2.0 * cos(angle - pi) 0, ) p = t.transformPoint_(p) #print p #fontSize(label_size) #text(kind, (p.x - bbox.width/2, p.y - bbox.height/2)) fill(None) rect(p.x - bbox.width/2, p.y - bbox.height/2, bbox.width, bbox.height) fill(1, 0, 0) #oval(p.x -bh/2.0 , p.y -bh/2.0, bh, bh) #myString.drawAtPoint_withAttributes_(p, attrs) rr = NSRect(origin=(p.x -bh/2.0, p.y -bw/2.0), size=(bw, bh)) #print rr myString.drawInRect_withAttributes_(rr, attrs) myString.drawAtPoint_withAttributes_(p, attrs)
"old-duovars/XOPQminYOPQmin.ufo", "../src/1-drawings/RobotoDelta-XOPQmax-YOPQmax.ufo", "old-duovars/XOPQmaxYOPQmax.ufo", "../src/1-drawings/RobotoDelta-XTRAmin.ufo", "old-duovars/XTRAmin.ufo", ] size(2100, 625) translate(25, 25) scale(0.05) for path in reversed(fonts): f = Font(path) pen = CocoaPen(f) save() for char in string.uppercase: pen.path = NSBezierPath.alloc().init() glyph = f[char] glyph.draw(pen) drawPath(pen.path) translate(glyph.width) restore() translate(0, 2000) saveImage(["drawFonts.pdf"])