def withinLayerBlack(layer, x, y): if not layer.bounds: return definitelyOutside = NSMakePoint(layer.bounds.origin.x - 1, y) pt = NSMakePoint(x, y) intersections = layer.calculateIntersectionsStartPoint_endPoint_decompose_( definitelyOutside, pt, True) return (len(intersections) % 2) == 1
def withinLayerBlack(layer, x, y): if not layer.bounds: return definitelyOutside = NSMakePoint(layer.bounds.origin.x - 1, y) pt = NSMakePoint(x, y) intersections = G.calculate_intersections(layer, definitelyOutside, pt, True) return (len(intersections) % 2) == 1
def line(x1, y1, x2=None, y2=None): # draws a line if x2 is None and y2 is None and isinstance(x1, tuple) and isinstance( y1, tuple): (x1, y1), (x2, y2) = x1, y1 p = NSBezierPath.bezierPath() p.moveToPoint_(NSMakePoint(x1, y1)) p.lineToPoint_(NSMakePoint(x2, y2)) drawPath(p)
def maxPoints(self, points, minY, maxY): right = -10000 left = 10000 for p in points: if p.y >= minY and p.y <= maxY: if p.x > right: right = p.x righty = p.y if p.x < left: left = p.x lefty = p.y return NSMakePoint(left, lefty), NSMakePoint(right, righty)
def getMargins(layer, y): startPoint = NSMakePoint(NSMinX(layer.bounds) - 1, y) endPoint = NSMakePoint(NSMaxX(layer.bounds) + 1, y) result = layer.calculateIntersectionsStartPoint_endPoint_(startPoint, endPoint) count = len(result) if (count <= 2): return (None, None) left = 1 right = count - 2 return (result[left].pointValue().x, result[right].pointValue().x)
def totalMarginList(layer,minY,maxY,angle,minYref,maxYref): # totalMarginList(layer,minY,maxY,angle,minYref,maxYref) #the list of margins y = minY listL = [] listR = [] #calculate default depth, otherwise measurement is None #calculate paralelogram extremes origin=NSMinX(layer.bounds) endpointx=NSMaxX(layer.bounds) endpointy=NSMaxY(layer.bounds) #calculate paralelogram top left xpos=triangle(angle,endpointy)+origin #paralelogram top side width slantWidth=(endpointx-xpos) #default depth dfltDepth=slantWidth #result will be false if all the measured margins are emtpy (no outlines in reference zone) result=False while y <= maxY: lpos, rpos = getMargins(layer, y) #get the default margin measure at a given y position slantPosL=origin+triangle(angle,y)+dfltDepth slantPosR=origin+triangle(angle,y) if lpos is not None: listL.append(NSMakePoint(lpos, y)) if minYref<=y<=maxYref: result=True else: listL.append(NSMakePoint(slantPosL, y)) if rpos is not None: listR.append(NSMakePoint(rpos, y)) if minYref<=y<=maxYref: result=True else: listR.append(NSMakePoint(slantPosR, y)) y += paramFreq #if no measurements are taken, returns false and will abort in main function if result: return listL, listR else: return False,False
def marginList(layer): y = NSMinY(layer.bounds) listL = [] listR = [] # works over glyph copy cleanLayer = layer.copyDecomposedLayer() while y <= NSMaxY(layer.bounds): lpos, rpos = getMargins(cleanLayer, y) if lpos is not None: listL.append(NSMakePoint(lpos, y)) if rpos is not None: listR.append(NSMakePoint(rpos, y)) y += paramFreq return listL, listR
def text(textString, pt): # Draw a text on position "x", "y". NSString.stringWithString_(textString).drawAtPoint_withAttributes_( NSMakePoint(pt[0], pt[1]), { NSFontAttributeName: currentFont, NSForegroundColorAttributeName: currentFillColor })
def transfer_first_left_button_press_cb(doc, event, tool): _loc = event.locationInWindow() _da = doc.getDA() (_x, _y) = _da.convertPoint_fromView_(_loc, None) _da.setTempPoint(NSMakePoint(_x, _y)) _tol = _da.pointSize().width _image = doc.getImage() _active_layer = _image.getActiveLayer() _objdict = _image.mapPoint(_x, _y, _tol, None) if len(_objdict): _image.startAction() try: for _layer in _objdict: if _layer is not _active_layer: _objs = [] for _obj, _pt in _objdict[_layer]: _objs.append(_obj) PythonCAD.Generic.transfer.transfer_objects(_objs, _layer, _active_layer) finally: _image.endAction() else: tool.pushObject(_x) tool.pushObject(_y) tool.setLocation(_x, _y) tool.setHandler("mouse_move", select_mouse_move_cb) tool.setHandler("left_button_press", transfer_second_left_button_press_cb)
def center(self): if len(self) < 1: return None b = self.bounds return NSMakePoint( b.origin.x + b.size.width / 2, b.origin.y + b.size.height / 2, )
def maxPoints(self, points, minY, maxY): #this function returns the extremes for a given set of points in a given zone #filter those outside the range # pointsFilteredL = [ x for x in points[0] if x.y>=minY and x.y<=maxY] # pointsFilteredR = [ x for x in points[0] if x.y>=minY and x.y<=maxY] #sort all given points by x sortPointsByXL = sorted(points[0], key=lambda tup: tup[0]) sortPointsByXR = sorted(points[1], key=lambda tup: tup[0]) #get the extremes position, first and last in the list left, lefty = sortPointsByXL[0] right, righty = sortPointsByXR[-1] return NSMakePoint(left, lefty), NSMakePoint(right, righty)
def deslant(self, margin): """De-slant a list of points (contour) at angle with the point of origin at half the xheight.""" mline = self.xHeight / 2 return [ NSMakePoint(p.x - (p.y - mline) * math.tan(math.radians(self.angle)), p.y) for p in margin ]
def drawDividerInRect_(self, rect): NSSplitView.drawDividerInRect_(self, rect) if self.text: point = NSMakePoint( NSMaxX(rect) - self.text.sizeWithAttributes_(self.attributes).width - 10, rect.origin.y) self.text.drawAtPoint_withAttributes_(point, self.attributes)
def setDepth(self, marginsL, marginsR, lExtreme, rExtreme): depth = self.xHeight * self.paramDepth / 100 maxdepth = lExtreme.x + depth mindepth = rExtreme.x - depth marginsL = [NSMakePoint(min(p.x, maxdepth), p.y) for p in marginsL] marginsR = [NSMakePoint(max(p.x, mindepth), p.y) for p in marginsR] #add all the points at maximum depth if glyph is shorter than overshoot y=marginsL[0].y-paramFreq while y>self.minYref: marginsL.insert(0,NSMakePoint(maxdepth, y)) marginsR.insert(0,NSMakePoint(mindepth, y)) y-=paramFreq y=marginsL[-1].y+paramFreq while y<self.maxYref: marginsL.append(NSMakePoint(maxdepth, y)) marginsR.append(NSMakePoint(mindepth, y)) y+=paramFreq # if marginsL[-1].y<(self.maxYref-paramFreq): # marginsL.append(NSMakePoint(min(p.x, maxdepth), self.maxYref)) # marginsR.append(NSMakePoint(max(p.x, mindepth), self.maxYref)) # if marginsL[0].y>(self.minYref): # marginsL.insert(0,NSMakePoint(min(p.x, maxdepth), self.minYref)) # marginsR.insert(0,NSMakePoint(max(p.x, mindepth), self.minYref)) return marginsL, marginsR
def updateIcon(self, icon): image = NSImage.alloc().initWithSize_(NSMakeSize(48, 48)) image.lockFocus() size = icon.size() icon.drawAtPoint_fromRect_operation_fraction_( NSMakePoint((48 - size.width) / 2, (48 - size.height) / 2), NSMakeRect(0, 0, size.width, size.height), NSCompositeSourceOver, 1) # overlay file transfer direction icon if type(self.transfer) == OutgoingPushFileTransferHandler or ( self.oldTransferInfo and self.oldTransferInfo.direction == "outgoing"): icon = NSImage.imageNamed_("outgoing_file") else: icon = NSImage.imageNamed_("incoming_file") icon.drawAtPoint_fromRect_operation_fraction_( NSMakePoint(2, 4), NSMakeRect(0, 0, size.width, size.height), NSCompositeSourceOver, 1) image.unlockFocus() self.icon.setImage_(image)
def tunni_point(self): p0 = self.start p1 = self.handle1 p2 = self.handle2 p3 = self.end s1_x = p1.x - p0.x s1_y = p1.y - p0.y s2_x = p3.x - p2.x s2_y = p3.y - p2.y d1 = -s2_x * s1_y + s1_x * s2_y d2 = -s2_x * s1_y + s1_x * s2_y if abs(d1) < 0.1 or abs(d2) < 0.1: return None s = (-s1_y * (p0.x - p2.x) + s1_x * (p0.y - p2.y)) / d1 t = (s2_x * (p0.y - p2.y) - s2_y * (p0.x - p2.x)) / d2 return NSMakePoint(p0.x + (t * s1_x), p0.y + (t * s1_y))
def stretch_select_first_left_button_press_cb(doc, event, tool): _loc = event.locationInWindow() _da = doc.getDA() (_x, _y) = _da.convertPoint_fromView_(_loc, None) _da.setTempPoint(NSMakePoint(_x, _y)) _tol = _da.pointSize().width _image = doc.getImage() _active_layer = _image.getActiveLayer() _pt = _active_layer.find('point', _x, _y, _tol) if _pt is not None: _dx, _dy = tool.getDistance() _pt.move(_dx, _dy) # should do this better . . . _init_func = tool.getHandler("initialize") tool.reset() _init_func(doc, tool) doc.getDA().setNeedsDisplay_(True) else: tool.setLocation(_x, _y) tool.setHandler("mouse_move", select_mouse_move_cb) tool.setHandler("left_button_press", stretch_select_second_left_button_press_cb)
def move_select_first_left_button_press_cb(doc, event, tool): _loc = event.locationInWindow() _da = doc.getDA() (_x, _y) = _da.convertPoint_fromView_(_loc, None) _da.setTempPoint(NSMakePoint(_x, _y)) _tol = _da.pointSize().width _image = doc.getImage() _active_layer = _image.getActiveLayer() _objdict = _image.mapPoint(_x, _y, _tol, None) if len(_objdict): _active_layer = _image.getActiveLayer() if _active_layer in _objdict: _objs = [] for _obj, _pt in _objdict[_active_layer]: _objs.append(_obj) move_objects(doc, _objs, tool) else: _pt, _flag = _image.findPoint(_x, _y, _tol) if _pt is not None: _x, _y = _pt.getCoords() tool.setLocation(_x, _y) tool.setHandler("mouse_move", select_mouse_move_cb) tool.setHandler("left_button_press", move_select_second_left_button_press_cb)
def lineTo(pt): # line to point if currentPath is not None: currentPath.lineToPoint_(NSMakePoint(pt[0], pt[1]))
def _interpolatenspoint(self, p1, p2, t): dx = (p2.x - p1.x) * t dy = (p2.y - p1.y) * t return NSMakePoint(p1.x + dx, p1.y + dy)
def closeOpenCounters(self, margin, extreme): initPoint = NSMakePoint(extreme.x, self.minYref) endPoint = NSMakePoint(extreme.x, self.maxYref) margin.insert(0, initPoint) margin.append(endPoint) return margin
import random import math from Foundation import NSMakePoint layer = Glyphs.font.selectedLayers[0] # current layer left, bottom = layer.bounds.origin.x, layer.bounds.origin.y width, height = layer.bounds.size.width, layer.bounds.size.height dust = [] f = layer.completeBezierPath points = 1000 while points: x = random.randrange(left, left + width) y = random.randrange(bottom, bottom + height) if not f.containsPoint_(NSMakePoint(x, y)): continue sides = random.randrange(15, 30) p = GSPath() angle = math.radians(360 / sides) for i in range(sides): p.nodes.append(GSNode((x, y), LINE)) delta = abs(random.gauss(5, 20) / float(sides)) x = x + math.cos(i * angle) * (random.random() + delta) y = y + math.sin(i * angle) * (random.random() + delta) points = points - 1 p.closed = True if p.direction == layer.paths[0].direction: p.reverse()
def split_first_left_button_press_cb(doc, event, tool): _loc = event.locationInWindow() _da = doc.getDA() (_x, _y) = _da.convertPoint_fromView_(_loc, None) _da.setTempPoint(NSMakePoint(_x, _y)) _tol = _da.pointSize().width _image = doc.getImage() _active_layer = _image.getActiveLayer() _objlist = _active_layer.mapPoint((_x, _y), _tol, None) if len(_objlist): for _obj, _pt in _objlist: if isinstance(_obj, PythonCAD.Generic.segment.Segment): _p1, _p2 = _obj.getEndpoints() _lpt = _active_layer.find('point', _pt.x, _pt.y) if _lpt is None: _active_layer.addObject(_pt) _lpt = _pt _s1, _s2 = PythonCAD.Generic.split.split_segment(_obj, _pt) _image.startAction() try: _active_layer.addObject(_s1) _active_layer.addObject(_s2) _active_layer.delObject(_obj) finally: _image.endAction() elif isinstance(_obj, PythonCAD.Generic.arc.Arc): _arc1, _arc2 = PythonCAD.Generic.split.split_arc(_obj, _pt) _image.startAction() try: _active_layer.addObject(_arc1) _active_layer.addObject(_arc2) _active_layer.delObject(_obj) finally: _image.endAction() elif isinstance(_obj, PythonCAD.Generic.circle.Circle): _arc = PythonCAD.Generic.split.split_circle(_obj, _pt) _image.startAction() try: _active_layer.addObject(_arc) _active_layer.delObject(_obj) finally: _image.endAction() elif isinstance(_obj, PythonCAD.Generic.polyline.Polyline): _lpt = _active_layer.find('point', _pt.x, _pt.y) _image.startAction() try: if _lpt is None: _active_layer.addObject(_pt) _lpt = _pt PythonCAD.Generic.split.split_polyline(_obj, _lpt) finally: _image.endAction() else: pass else: tool.pushObject(_x) tool.pushObject(_y) tool.setLocation(_x, _y) tool.setHandler("mouse_move", select_mouse_move_cb) tool.setHandler("left_button_press", split_second_left_button_press_cb)
def drawRect_(self, rect): r = self.bounds() r.size.width -= 0.5 r.size.height += 4 if self.draggedOut: NSColor.colorWithDeviceWhite_alpha_(0.4, 1.0).set() path = NSBezierPath.bezierPathWithRoundedRect_xRadius_yRadius_( r, 5, 5) path.fill() else: if self == self.switcher.activeItem(): NSColor.controlColor().set() else: NSColor.colorWithDeviceWhite_alpha_(0.6, 1.0).set() path = NSBezierPath.bezierPathWithRoundedRect_xRadius_yRadius_( r, 5, 5) path.fill() NSColor.colorWithDeviceRed_green_blue_alpha_(0.3, 0.3, 0.3, 1.0).set() path.stroke() if self.badgeLabel and not self.mouseInside and not self.busyIndicator and not self.composing: # draw the number in redbadge indicator gradient = NSGradient.alloc().initWithStartingColor_endingColor_( NSColor.colorWithDeviceRed_green_blue_alpha_(0.9, 0.2, 0.2, 1), NSColor.colorWithDeviceRed_green_blue_alpha_(1.0, 0.2, 0.2, 1)) size = self.badgeLabel.size() size.width += 4 if size.width < 12: size.width = 12 bez = NSBezierPath.bezierPathWithRoundedRect_xRadius_yRadius_( NSMakeRect(3, 5, size.width, 12), 6, 6) gradient.drawInBezierPath_angle_(bez, 90 + 45) self.badgeLabel.drawInRect_(NSMakeRect(3, 5, size.width, 12)) if not self.mouseInside and not self.busyIndicator and self.composing: rect = NSZeroRect.copy() rect.size = self.composeIcon.size() self.composeIcon.drawAtPoint_fromRect_operation_fraction_( NSMakePoint(1, 3), rect, NSCompositeSourceOver, 1) if not self.busyIndicator and self.screen_sharing_active: rect = NSZeroRect.copy() rect.size = self.screenIcon.size() self.screenIcon.drawAtPoint_fromRect_operation_fraction_( NSMakePoint(17, 3), rect, NSCompositeSourceOver, 1) if not self.draggedOut: shadow = NSShadow.alloc().init() shadow.setShadowOffset_(NSMakeSize(0, -1)) if self == self.switcher.activeItem(): shadow.setShadowColor_(NSColor.whiteColor()) else: shadow.setShadowColor_( NSColor.colorWithDeviceWhite_alpha_(0.7, 1.0)) para = NSParagraphStyle.defaultParagraphStyle().mutableCopy() para.setLineBreakMode_(NSLineBreakByTruncatingTail) para.setAlignment_(NSCenterTextAlignment) attribs = NSDictionary.dictionaryWithObjectsAndKeys_( NSFont.systemFontOfSize_(11), NSFontAttributeName, shadow, NSShadowAttributeName, para, NSParagraphStyleAttributeName) rect = self.bounds() rect.origin.y -= 3 rect.origin.x += 20 rect.origin.x = rect.origin.x + 12 if self.screen_sharing_active else rect.origin.x rect.size.width -= 46 rect.size.width = rect.size.width - 12 if self.screen_sharing_active else rect.size.width self.label.drawInRect_withAttributes_(rect, attribs)
def transformNode(myNode, myTransform): myNode.position = myTransform.transformPoint_( NSMakePoint(myNode.x, myNode.y)) return myNode
def image(image, pt, alpha=1): if type(image) == NSImage: image.drawAtPoint_fromRect_operation_fraction_( NSMakePoint(pt[0], pt[1]), NSZeroRect, NSCompositeSourceOver, 1)
def _italicOnOffPoint(self, p, onoff): mline = self.xHeight / 2 cateto = -p.y + mline if onoff == "off": cateto = -cateto xvar = -rectCateto(self.angle, cateto) return NSMakePoint(p.x + xvar, p.y)
def moveTo(pt): # move to point if currentPath is not None: currentPath.moveToPoint_(NSMakePoint(pt[0], pt[1]))
def makeDragImage(self): if self.delegate is None: return image = NSImage.alloc().initWithSize_(self.frame().size) image.lockFocus() frame = self.frame() frame.origin = NSZeroPoint rect = NSInsetRect(frame, 1.5, 1.5) if self.conferencing and not self.draggedOut: NSColor.selectedControlColor().colorWithAlphaComponent_(0.7).set() else: NSColor.whiteColor().colorWithAlphaComponent_(0.7).set() path = NSBezierPath.bezierPathWithRoundedRect_xRadius_yRadius_( rect, 5.0, 5.0) path.fill() if self.selected: path.setLineWidth_(3) NSColor.grayColor().set() else: path.setLineWidth_(1) NSColor.grayColor().set() path.stroke() NSColor.blackColor().set() point = NSMakePoint(8, NSMaxY(frame) - 20) uri = format_identity_to_string( self.delegate.sessionController.remoteIdentity, check_contact=False, format='compact') NSString.stringWithString_(uri).drawAtPoint_withAttributes_( point, NSDictionary.dictionaryWithObjectsAndKeys_( NSFont.boldSystemFontOfSize_(12), NSFontAttributeName)) point = NSMakePoint(8, 6) if self.conferencing: NSString.stringWithString_( NSLocalizedString( "Drop outside to remove from conference", "Audio status label")).drawAtPoint_withAttributes_( point, NSDictionary.dictionaryWithObjectsAndKeys_( NSFont.systemFontOfSize_(10), NSFontAttributeName)) else: audio_sessions = [ sess.hasStreamOfType("audio") for sess in NSApp.delegate().contactsWindowController. sessionControllersManager.sessionControllers ] if self.delegate.transferEnabled: text = NSLocalizedString( "Drop this over a session or contact", "Audio status label" ) if len(audio_sessions) > 1 else NSLocalizedString( "Drop this over a contact to transfer", "Audio status label") else: text = NSLocalizedString( "Drop this over a session to conference", "Audio status label") NSString.stringWithString_(text).drawAtPoint_withAttributes_( point, NSDictionary.dictionaryWithObjectsAndKeys_( NSFont.systemFontOfSize_(10), NSFontAttributeName)) icon = NSImage.imageNamed_("NSEveryone") rect = frame s = icon.size() p = NSMakePoint( NSWidth(rect) - s.width - 8, rect.size.height - s.height - 8) r = NSMakeRect(0, 0, s.width, s.height) icon.drawAtPoint_fromRect_operation_fraction_(p, r, NSCompositeSourceOver, 0.5) image.unlockFocus() return image
def curveTo(h1, h2, pt): # curve to point with bcps if currentPath is not None: currentPath.curveToPoint_controlPoint1_controlPoint2_( NSMakePoint(pt[0], pt[1]), NSMakePoint(h1[0], h1[1]), NSMakePoint(h2[0], h2[1]))