def mouseDown_(self, event): if self.cropRectangle: p = self.convertPoint_fromView_(event.locationInWindow(), None) if p.x > NSMinX(self.cropRectangle) and p.x < NSMaxX(self.cropRectangle) and\ p.y > NSMinY(self.cropRectangle) and p.y < NSMaxY(self.cropRectangle): self.dragPos = p self.initialPos = self.cropRectangle.origin
def doTrial(anchor, x): startPoint = NSMakePoint(x, NSMinY(Layer.bounds)) endPoint = NSMakePoint(x, NSMaxY(Layer.bounds)) result = Layer.calculateIntersectionsStartPoint_endPoint_( startPoint, endPoint) if len(result) <= 2: return None, None result = result[1:3] # Generally speaking it will be the lowest # Except jeem. Urgh. for r in pairwise(result): distance = r[1].y - r[0].y if distance < 10 or distance > 500: continue result = r break if distance < 10 or distance > 500: return None, None bottomClosest = calcClosestInfo(Layer, NSMakePoint(result[0].x, result[0].y)) bt = bottomClosest["directionAngle"] tt = calcClosestInfo(Layer, NSMakePoint(result[1].x, result[1].y))["directionAngle"] # if anchor == "exit": # bt = bt # tt = - tt score = (distance - targetDistance)**2 print("Square distance diff", score) tangentScore = 5 print("Top tangent was: ", tt) print("Expected top tangent was: ", Layer.master.customParameters["autocursiveattachment_topTangent"]) print("Bottom tangent was: ", bt) print("Expected bottom tangent was: ", Layer.master.customParameters["autocursiveattachment_bottomTangent"]) tangentContribution = ( tangentScore * (bt - Layer.master.customParameters["autocursiveattachment_bottomTangent"]) )**2 + ( tangentScore * (tt - Layer.master.customParameters["autocursiveattachment_topTangent"]))**2 print("Tangent contribution to score", tangentContribution) score = score + tangentContribution # if anchor == "exit": # score = score + x**2 # else: # score = score + (Layer.width-x)**2 placement = NSMakePoint(x, prop * result[1].y + (1 - prop) * (result[0].y)) return score, placement
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 mouseDragged_(self, event): if self.cropRectangle and self.dragPos: p = self.convertPoint_fromView_(event.locationInWindow(), None) dx = self.dragPos.x - p.x dy = self.dragPos.y - p.y newRect = NSMakeRect(self.initialPos.x - dx, self.initialPos.y - dy, NSWidth(self.cropRectangle), NSHeight(self.cropRectangle)) if NSMinX(newRect) < 0: newRect.origin.x = 0 if NSMinY(newRect) < 0: newRect.origin.y = 0 if NSMaxX(newRect) > NSWidth(self.frame()): newRect.origin.x = NSWidth(self.frame()) - NSWidth(newRect) if NSMaxY(newRect) > NSHeight(self.frame()): newRect.origin.y = NSHeight(self.frame()) - NSHeight(newRect) self.cropRectangle = newRect self.setNeedsDisplay_(True)
def setSpace(self, layer, referenceLayer): # get reference glyph maximum points overshoot = self.overshoot() # store min and max y self.minYref = NSMinY(referenceLayer.bounds) - overshoot self.maxYref = NSMaxY(referenceLayer.bounds) + overshoot self.minY = NSMinY(layer.bounds) self.maxY = NSMaxY(layer.bounds) self.output+="Glyph: " + str(layer.parent.name)+"\n" self.output+="Reference layer: " + referenceLayer.parent.name+ " | Factor: " + str(self.factor) + "\n" #get the margins for the full outline #will take measure from minY to maxY. minYref and maxYref are passed to check reference match # totalMarginList(layer,minY,maxY,angle,minYref,maxYref) lTotalMargins, rTotalMargins = totalMarginList(layer,self.minY,self.maxY,self.angle,self.minYref,self.maxYref) #margins will be False, False if there is no measure in the reference zone, and then function stops if not lTotalMargins and not rTotalMargins: self.output += 'The glyph outlines are outside the reference layer zone/height. No match with '+referenceLayer.parent.name+"\n" return # filtes all the margins to the reference zone lZoneMargins, rZoneMargins = zoneMargins(lTotalMargins,rTotalMargins,self.minYref,self.maxYref) #if the font has an angle, we need to deslant if self.angle: self.output+="Using angle: " + str(self.angle)+"\n" lZoneMargins = self.deslant(lZoneMargins) rZoneMargins = self.deslant(rZoneMargins) lTotalMargins = self.deslant(lTotalMargins) rTotalMargins = self.deslant(rTotalMargins) #full shape extreme points lFullExtreme, rFullExtreme = self.maxPoints([lTotalMargins,rTotalMargins], self.minY, self.maxY) # get zone extreme points lExtreme, rExtreme = self.maxPoints([lZoneMargins,rZoneMargins], self.minYref, self.maxYref) # create a closed polygon lPolygon, rPolygon = self.processMargins(lZoneMargins, rZoneMargins, lExtreme, rExtreme) # return # dif between extremes full and zone distanceL = math.ceil(lExtreme.x - lFullExtreme.x) distanceR = math.ceil(rFullExtreme.x - rExtreme.x) # set new sidebearings self.newL = math.ceil(0 - distanceL + self.calculateSBValue(lPolygon)) self.newR = math.ceil(0 - distanceR + self.calculateSBValue(rPolygon)) # tabVersion if '.tosf' in layer.parent.name or '.tf' in layer.parent.name or self.tabVersion: if self.width: self.layerWidth = self.width else: self.layerWidth = layer.width widthShape = rFullExtreme.x - lFullExtreme.x widthActual = widthShape + self.newL + self.newR widthDiff = (self.layerWidth - widthActual) / 2 self.newL += widthDiff self.newR += widthDiff self.newWidth = self.layerWidth self.output += layer.parent.name + ' is tabular and adjusted at width = ' + str(self.layerWidth) # end tabVersion # if there is a metric rule else: if layer.parent.leftMetricsKey is not None or self.LSB == False: self.newL = layer.LSB if layer.parent.rightMetricsKey is not None or self.RSB == False: self.newR = layer.RSB return lPolygon, rPolygon
def setSpace(self, layer, referenceLayer): # get reference glyph maximum points overshoot = self.overshoot() # store min and max y self.minYref = NSMinY(referenceLayer.bounds) - overshoot self.maxYref = NSMaxY(referenceLayer.bounds) + overshoot # bounds lFullMargin, rFullMargin = marginList(layer) lMargins = filter( lambda p: p.y >= self.minYref and p.y <= self.maxYref, lFullMargin) rMargins = filter( lambda p: p.y >= self.minYref and p.y <= self.maxYref, rFullMargin) # create a closed polygon lPolygon, rPolygon = self.processMargins(lMargins, rMargins) lMargins = self.deSlant(lMargins) rMargins = self.deSlant(rMargins) lFullMargin = self.deSlant(lFullMargin) rFullMargin = self.deSlant(rFullMargin) # get extreme points deitalized lFullExtreme, rFullExtreme = self.maxPoints(lFullMargin + rFullMargin, NSMinY(layer.bounds), NSMaxY(layer.bounds)) # get zone extreme points lExtreme, rExtreme = self.maxPoints(lMargins + rMargins, self.minYref, self.maxYref) # dif between extremes full and zone distanceL = math.ceil(lExtreme.x - lFullExtreme.x) distanceR = math.ceil(rFullExtreme.x - rExtreme.x) # set new sidebearings self.newL = math.ceil(0 - distanceL + self.calculateSBValue(lPolygon)) self.newR = math.ceil(0 - distanceR + self.calculateSBValue(rPolygon)) # tabVersion if '.tosf' in layer.parent.name or '.tf' in layer.parent.name or self.tabVersion: if self.width: self.layerWidth = self.width else: self.layerWidth = layer.width widthShape = rFullExtreme.x - lFullExtreme.x widthActual = widthShape + self.newL + self.newR widthDiff = (self.layerWidth - widthActual) / 2 self.newL += widthDiff self.newR += widthDiff self.newWidth = self.layerWidth self.output += layer.parent.name + ' is tabular and adjusted at width = ' + str( self.layerWidth) # fin tabVersion # if there is a metric rule else: if layer.parent.leftMetricsKey is not None or self.LSB == False: self.newL = layer.LSB if layer.parent.rightMetricsKey is not None or self.RSB == False: self.newR = layer.RSB return lPolygon, rPolygon
Layer = Glyphs.font.selectedLayers[0] inout = list( filter(lambda x: x.name == "entry" or x.name == "exit", Layer.anchors)) if not inout: Message("No anchors found", "Create an entry or exit anchor to use this script") distabove = 0 distbelow = 0 bottomTangent = 0 topTangent = 0 for anchor in inout: startPoint = NSMakePoint(anchor.x, NSMinY(Layer.bounds)) endPoint = NSMakePoint(anchor.x, NSMaxY(Layer.bounds)) result = Layer.calculateIntersectionsStartPoint_endPoint_( startPoint, endPoint) if len(result) <= 2: Message("No paths found", "Anchor needs to be within a path") result = sorted(list(set(result[1:-1])), key=lambda p: p.y) # Remove repeated results print("Result was", result) selected = min(pairwise(result), key=lambda x: mysqdistance(anchor.position, x[0]) + mysqdistance(anchor.position, x[1])) print("Selected", selected) distabove = distabove + abs(max([p.y - anchor.y for p in selected])) distbelow = distbelow + abs(max([anchor.y - p.y for p in selected])) bottomClosest = calcClosestInfo(Layer,
def __GSElement__get_box__(self): rect = self.bounds return (NSMinX(rect), NSMinY(rect), NSMaxX(rect), NSMaxY(rect))
def _get_box(self): bounds = self._layer.bounds bounds = (int(round(NSMinX(bounds))), int(round(NSMinY(bounds))), int(round(NSMaxX(bounds))), int(round(NSMaxY(bounds)))) return bounds
def drawHashMarksAndLabelsInRect_(self, rect): bounds = self.bounds() view = self.clientView() rulerBackgroundColor = self.rulerBackgroundColor() if rulerBackgroundColor is not None: rulerBackgroundColor.set() NSRectFill(bounds) if not isinstance(view, NSTextView): return layoutManager = view.layoutManager() container = view.textContainer() text = view.string() nullRange = NSMakeRange(NSNotFound, 0) yinset = view.textContainerInset().height visibleRect = self.scrollView().contentView().bounds() textAttributes = self.textAttributes() lines = self.lineIndices() glyphRange = layoutManager.glyphRangeForBoundingRect_inTextContainer_( visibleRect, container) _range = layoutManager.characterRangeForGlyphRange_actualGlyphRange_( glyphRange, None)[0] _range.length += 1 count = len(lines) index = 0 lineNumber = self.lineNumberForCharacterIndex_inText_( _range.location, text) for line in range(lineNumber, count): index = lines[line] if NSLocationInRange(index, _range): rects, rectCount = layoutManager.rectArrayForCharacterRange_withinSelectedCharacterRange_inTextContainer_rectCount_( NSMakeRange(index, 0), nullRange, container, None) if rectCount > 0: ypos = yinset + NSMinY(rects[0]) - NSMinY(visibleRect) labelText = NSString.stringWithString_("%s" % (line + 1)) stringSize = labelText.sizeWithAttributes_(textAttributes) x = NSWidth(bounds) - stringSize.width - self.RULER_MARGIN y = ypos + (NSHeight(rects[0]) - stringSize.height) / 2.0 w = NSWidth(bounds) - self.RULER_MARGIN * 2.0 h = NSHeight(rects[0]) labelText.drawInRect_withAttributes_( NSMakeRect(x, y, w, h), textAttributes) if index > NSMaxRange(_range): break path = NSBezierPath.bezierPath() path.moveToPoint_( (bounds.origin.x + bounds.size.width, bounds.origin.y)) path.lineToPoint_((bounds.origin.x + bounds.size.width, bounds.origin.y + bounds.size.height)) NSColor.grayColor().set() path.stroke()