def make_hPointsList(g): contoursList = [] hPointsList = [] for i in range(len(g)): pointsList = [] for j in g[i].points: pointsList.append(j) contoursList.append(pointsList) for contour_index in range(len(contoursList)): for point_index in range(len(contoursList[contour_index])): currentPoint = contoursList[contour_index][point_index] if point_index == 0: prevPoint = contoursList[contour_index][ len(contoursList[contour_index]) - 1] else: prevPoint = contoursList[contour_index][point_index - 1] if point_index == len(contoursList[contour_index]) - 1: nextPoint = contoursList[contour_index][0] else: nextPoint = contoursList[contour_index][point_index + 1] if currentPoint.type != 'offCurve': directionIN = module.direction(prevPoint, currentPoint) directionOUT = module.direction(currentPoint, nextPoint) vectorIN = module.angle(prevPoint, currentPoint) vectorOUT = module.angle(currentPoint, nextPoint) hPoint = (currentPoint, contour_index, point_index, directionIN, directionOUT, vectorIN, vectorOUT) hPointsList.append(hPoint) return hPointsList
def make_hPointsList(g): contoursList = [] hPointsList = [] for i in range(len(g)): pointsList = [] for j in g[i].points: pointsList.append(j) contoursList.append(pointsList) for contour_index in range(len(contoursList)): for point_index in range(len(contoursList[contour_index])): currentPoint = contoursList[contour_index][point_index] if point_index == 0: prevPoint = contoursList[contour_index][len(contoursList[contour_index])-1] else: prevPoint = contoursList[contour_index][point_index-1] if point_index == len(contoursList[contour_index]) -1: nextPoint = contoursList[contour_index][0] else: nextPoint = contoursList[contour_index][point_index+1] if currentPoint.type != 'offCurve': directionIN = module.direction(prevPoint, currentPoint) directionOUT = module.direction(currentPoint, nextPoint) vectorIN = module.angle(prevPoint, currentPoint) vectorOUT = module.angle(currentPoint, nextPoint) hPoint = (currentPoint, contour_index, point_index, directionIN, directionOUT, vectorIN, vectorOUT) hPointsList.append(hPoint) return hPointsList
def getColor(point1, point2, g): hasSomeBlack = False hasSomeWhite = False color = '' if abs(point2.x - point1.x) < maxStemX or abs(point2.y - point1.y) < maxStemY: hypothLength = int(module.hypothenuse(point1, point2)) for j in range(1, hypothLength-1): cp_x = point1.x + ((j)/hypothLength)*(point2.x - point1.x) cp_y = point1.y + ((j)/hypothLength)*(point2.y - point1.y) if g.pointInside((cp_x, cp_y)): hasSomeBlack = True else: hasSomeWhite = True if hasSomeBlack and hasSomeWhite: break if hasSomeBlack and hasSomeWhite: color = 'Gray' elif hasSomeBlack: color = 'Black' else: color = 'White' return color
def getColor(point1, point2, g): hasSomeBlack = False hasSomeWhite = False color = '' if abs(point2.x - point1.x) < maxStemX or abs(point2.y - point1.y) < maxStemY: hypothLength = int(module.hypothenuse(point1, point2)) for j in range(1, hypothLength - 1): cp_x = point1.x + ((j) / hypothLength) * (point2.x - point1.x) cp_y = point1.y + ((j) / hypothLength) * (point2.y - point1.y) if g.pointInside((cp_x, cp_y)): hasSomeBlack = True else: hasSomeWhite = True if hasSomeBlack and hasSomeWhite: break if hasSomeBlack and hasSomeWhite: color = 'Gray' elif hasSomeBlack: color = 'Black' else: color = 'White' return color
def pred1(x): #print preRot1x, x return module.approxEqual(preRot1x, x)
def pred0(x): #print preRot0x, x return module.approxEqual(preRot0x, x)
def pred1(y): return module.approxEqual(stem[1].y, y)
def pred0(y): return module.approxEqual(stem[0].y, y)
def makeStemsList(f, g_hPoints, g, italicAngle): stemsListX_temp = [] stemsListY_temp = [] stemsListX = [] stemsListY = [] for source_hPoint in range(len(g_hPoints)): for target_hPoint in range(len(g_hPoints)): sourcePoint = g_hPoints[source_hPoint][0] targetPoint = g_hPoints[target_hPoint][0] directionIn_source = g_hPoints[source_hPoint][3] directionOut_source = g_hPoints[source_hPoint][4] directionIn_target = g_hPoints[target_hPoint][3] directionOut_target = g_hPoints[target_hPoint][4] angleIn_source = g_hPoints[source_hPoint][5] angleOut_source = g_hPoints[source_hPoint][6] angleIn_target = g_hPoints[target_hPoint][5] angleOut_target = g_hPoints[target_hPoint][6] color = getColor(sourcePoint, targetPoint, g) if color == 'Black': c_distance = module.distance(sourcePoint, targetPoint) stem = (sourcePoint, targetPoint, c_distance) hypoth = module.hypothenuse(sourcePoint, targetPoint) ## if Source and Target are almost aligned # closeAngle(angleIn_source, angleIn_target) or closeAngle(angleOut_source, angleOut_target) or if module.closeAngle(angleIn_source, angleOut_target) or module.closeAngle( angleOut_source, angleIn_target): ## if Source and Target have opposite direction if module.opposite( directionIn_source, directionIn_target) or module.opposite( directionIn_source, directionOut_target) or module.opposite( directionOut_source, directionIn_target): ## if they are horizontal, treat the stem on the Y axis if (module.isHorizontal(angleIn_source) or module.isHorizontal(angleOut_source)) and ( module.isHorizontal(angleIn_target) or module.isHorizontal(angleOut_target)): if (minStemY - 20 * (minStemY / 100) < c_distance[1] < maxStemY + 20 * (maxStemY / 100)) and ( minStemY - 20 * (minStemY / 100) <= hypoth <= maxStemY + 20 * (maxStemY / 100)): stemsListY_temp.append(stem) ## if they are vertical, treat the stem on the X axis if (module.isVertical(angleIn_source) or module.isVertical(angleOut_source)) and ( module.isVertical(angleIn_target) or module.isVertical(angleOut_target)): if (minStemX - 20 * (minStemX / 100) <= c_distance[0] <= maxStemX + 20 * (maxStemX / 100)) and ( minStemX - 20 * (minStemX / 100) <= hypoth <= maxStemX + 20 * (maxStemX / 100)): stemsListX_temp.append(stem) # avoid duplicates, filters temporary stems yList = [] for stem in stemsListY_temp: def pred0(y): return module.approxEqual(stem[0].y, y) def pred1(y): return module.approxEqual(stem[1].y, y) if not module.exists(yList, pred0) or not module.exists(yList, pred1): stemsListY.append(stem) yList.append(stem[0].y) yList.append(stem[1].y) xList = [] for stem in stemsListX_temp: (preRot0x, preRot0y) = module.rotated(stem[0], italicAngle) (preRot1x, preRot1y) = module.rotated(stem[1], italicAngle) def pred0(x): #print preRot0x, x return module.approxEqual(preRot0x, x) def pred1(x): #print preRot1x, x return module.approxEqual(preRot1x, x) if not module.exists(xList, pred0) or not module.exists(xList, pred1): stemsListX.append(stem) xList.append(preRot0x) xList.append(preRot1x) return (stemsListX, stemsListY)
def startButtonCallBack(self, sender): self.glyphsStemsList = [] self.w.xValuesEditText.set('') self.w.yValuesEditText.set('') self.stemsValuesXList = [] self.stemsValuesYList = [] self.stemSnapHList = [] self.stemSnapVList = [] roundedStemsXList = [] roundedStemsYList = [] originalStemsXList = [] originalStemsYList = [] self.f = CurrentFont() if self.f.info.italicAngle != None: self.ital = -self.f.info.italicAngle else: self.ital = 0 self.progress = self.startProgress("Preparing") tickCount = 0 for g in self.f: if g.selected: tickCount += 1 self.progress.setTickCount(tickCount) self.progress.update("Analysing Selected Glyphs") for g in self.f: if g.selected: self.g_hPoints = make_hPointsList(g) (self.stemsListX, self.stemsListY) = makeStemsList(self.f, self.g_hPoints, g, self.ital) if self.roundTo5 == 1: for stem in self.stemsListX: roundedStemsXList.append( module.roundbase(stem[2][0], 5)) for stem in self.stemsListY: roundedStemsYList.append( module.roundbase(stem[2][1], 5)) self.stemsValuesXList = roundedStemsXList self.stemsValuesYList = roundedStemsYList self.glyphsStemsList.append( (g.name, self.stemsListX, self.stemsListY)) else: for stem in self.stemsListX: originalStemsXList.append(stem[2][0]) for stem in self.stemsListY: originalStemsYList.append(stem[2][1]) self.stemsValuesXList = originalStemsXList self.stemsValuesYList = originalStemsYList self.glyphsStemsList.append( (g.name, self.stemsListX, self.stemsListY)) self.progress.update() self.progress.setTickCount(0) self.progress.update("Sorting Values") valuesXDict = {} for StemXValue in self.stemsValuesXList: try: valuesXDict[StemXValue] += 1 except KeyError: valuesXDict[StemXValue] = 1 #print 'x',valuesXDict valuesYDict = {} for StemYValue in self.stemsValuesYList: try: valuesYDict[StemYValue] += 1 except KeyError: valuesYDict[StemYValue] = 1 #print 'y',valuesYDict keyValueXList = valuesXDict.items() keyValueXList.sort(module.compare) keyValueXList = keyValueXList[:12] keyValueYList = valuesYDict.items() keyValueYList.sort(module.compare) keyValueYList = keyValueYList[:12] for keyValue in keyValueXList: self.stemSnapVList.append(keyValue[0]) #print 'stemSnapH', self.stemSnapHList stemSnapVText = '' for i in self.stemSnapVList: stemSnapVText += str(i) + ' ' self.w.yValuesEditText.set(stemSnapVText) for keyValue in keyValueYList: self.stemSnapHList.append(keyValue[0]) #print 'stemSnapV', self.stemSnapVList stemSnapHText = '' for i in self.stemSnapHList: stemSnapHText += str(i) + ' ' self.w.xValuesEditText.set(stemSnapHText) addObserver(self, "draw", "draw") self.progress.close()
def makeStemsList(f, g_hPoints, g, italicAngle): stemsListX_temp = [] stemsListY_temp = [] stemsListX = [] stemsListY = [] for source_hPoint in range(len(g_hPoints)): for target_hPoint in range(len(g_hPoints)): sourcePoint = g_hPoints[source_hPoint][0] targetPoint = g_hPoints[target_hPoint][0] directionIn_source = g_hPoints[source_hPoint][3] directionOut_source = g_hPoints[source_hPoint][4] directionIn_target = g_hPoints[target_hPoint][3] directionOut_target = g_hPoints[target_hPoint][4] angleIn_source = g_hPoints[source_hPoint][5] angleOut_source = g_hPoints[source_hPoint][6] angleIn_target = g_hPoints[target_hPoint][5] angleOut_target = g_hPoints[target_hPoint][6] color = getColor(sourcePoint, targetPoint, g) if color == 'Black': c_distance = module.distance(sourcePoint, targetPoint) stem = (sourcePoint, targetPoint, c_distance) hypoth = module.hypothenuse(sourcePoint, targetPoint) ## if Source and Target are almost aligned # closeAngle(angleIn_source, angleIn_target) or closeAngle(angleOut_source, angleOut_target) or if module.closeAngle(angleIn_source, angleOut_target) or module.closeAngle(angleOut_source, angleIn_target): ## if Source and Target have opposite direction if module.opposite(directionIn_source, directionIn_target) or module.opposite(directionIn_source, directionOut_target) or module.opposite(directionOut_source, directionIn_target): ## if they are horizontal, treat the stem on the Y axis if (module.isHorizontal(angleIn_source) or module.isHorizontal(angleOut_source)) and (module.isHorizontal(angleIn_target) or module.isHorizontal(angleOut_target)): if (minStemY - 20*(minStemY/100) < c_distance[1] < maxStemY + 20*(maxStemY/100)) and (minStemY - 20*(minStemY/100) <= hypoth <= maxStemY + 20*(maxStemY/100)): stemsListY_temp.append(stem) ## if they are vertical, treat the stem on the X axis if (module.isVertical(angleIn_source) or module.isVertical(angleOut_source)) and (module.isVertical(angleIn_target) or module.isVertical(angleOut_target)): if (minStemX - 20*(minStemX/100) <= c_distance[0] <= maxStemX + 20*(maxStemX/100)) and (minStemX - 20*(minStemX/100)<= hypoth <= maxStemX + 20*(maxStemX/100)): stemsListX_temp.append(stem) # avoid duplicates, filters temporary stems yList = [] for stem in stemsListY_temp: def pred0(y): return module.approxEqual(stem[0].y, y) def pred1(y): return module.approxEqual(stem[1].y, y) if not module.exists(yList, pred0) or not module.exists(yList, pred1): stemsListY.append(stem) yList.append(stem[0].y) yList.append(stem[1].y) xList = [] for stem in stemsListX_temp: (preRot0x, preRot0y) = module.rotated(stem[0], italicAngle) (preRot1x, preRot1y) = module.rotated(stem[1], italicAngle) def pred0(x): #print preRot0x, x return module.approxEqual(preRot0x, x) def pred1(x): #print preRot1x, x return module.approxEqual(preRot1x, x) if not module.exists(xList,pred0) or not module.exists(xList,pred1): stemsListX.append(stem) xList.append(preRot0x) xList.append(preRot1x) return (stemsListX, stemsListY)
def startButtonCallBack(self, sender): self.glyphsStemsList = [] self.w.xValuesEditText.set('') self.w.yValuesEditText.set('') self.stemsValuesXList = [] self.stemsValuesYList = [] self.stemSnapHList = [] self.stemSnapVList = [] roundedStemsXList = [] roundedStemsYList = [] originalStemsXList = [] originalStemsYList = [] self.f = CurrentFont() if self.f.info.italicAngle != None: self.ital = - self.f.info.italicAngle else: self.ital = 0 self.progress = self.startProgress("Preparing") tickCount = 0 for g in self.f: if g.selected: tickCount += 1 self.progress.setTickCount(tickCount) self.progress.update("Analysing Selected Glyphs") for g in self.f: if g.selected: self.g_hPoints = make_hPointsList(g) (self.stemsListX, self.stemsListY) = makeStemsList(self.f, self.g_hPoints, g, self.ital) if self.roundTo5 == 1: for stem in self.stemsListX: roundedStemsXList.append(module.roundbase(stem[2][0], 5)) for stem in self.stemsListY: roundedStemsYList.append(module.roundbase(stem[2][1], 5)) self.stemsValuesXList = roundedStemsXList self.stemsValuesYList = roundedStemsYList self.glyphsStemsList.append((g.name, self.stemsListX, self.stemsListY)) else: for stem in self.stemsListX: originalStemsXList.append(stem[2][0]) for stem in self.stemsListY: originalStemsYList.append(stem[2][1]) self.stemsValuesXList = originalStemsXList self.stemsValuesYList = originalStemsYList self.glyphsStemsList.append((g.name, self.stemsListX, self.stemsListY)) self.progress.update() self.progress.setTickCount(0) self.progress.update("Sorting Values") valuesXDict = {} for StemXValue in self.stemsValuesXList: try: valuesXDict[StemXValue] += 1 except KeyError: valuesXDict[StemXValue] = 1 #print 'x',valuesXDict valuesYDict = {} for StemYValue in self.stemsValuesYList: try: valuesYDict[StemYValue] += 1 except KeyError: valuesYDict[StemYValue] = 1 #print 'y',valuesYDict keyValueXList = valuesXDict.items() keyValueXList.sort(module.compare) keyValueXList = keyValueXList[:12] keyValueYList = valuesYDict.items() keyValueYList.sort(module.compare) keyValueYList = keyValueYList[:12] for keyValue in keyValueXList: self.stemSnapVList.append(keyValue[0]) #print 'stemSnapH', self.stemSnapHList stemSnapVText = '' for i in self.stemSnapVList: stemSnapVText += str(i) + ' ' self.w.yValuesEditText.set(stemSnapVText) for keyValue in keyValueYList: self.stemSnapHList.append(keyValue[0]) #print 'stemSnapV', self.stemSnapVList stemSnapHText = '' for i in self.stemSnapHList: stemSnapHText += str(i) + ' ' self.w.xValuesEditText.set(stemSnapHText) addObserver(self, "draw", "draw") self.progress.close()