def measureGlyph(glyph): """Measure the segment lengths of all layers given in master_names. Returns a dictionary with keys of (path_index, segment_index) and values of a list of segment lengths. """ lengths = {} # Reference layer, just used for counting paths and segments ref_layer = glyph.layers[masters[master_names[0]]].background for i in range(len(ref_layer.paths)): for j in range(len(ref_layer.paths[i].segments)): segment_lengths = [] for master_name in master_names: layer = glyph.layers[masters[master_name]].background s = layer.paths[i].segments[j] if len(s) == 4: # curve p0, p1, p2, p3 = s p0 = p0.x, p0.y p1 = p1.x, p1.y p2 = p2.x, p2.y p3 = p3.x, p3.y l = calcCubicArcLength(p0, p1, p2, p3) elif len(s) == 2: p0, p1 = s l = sqrt((p1.y - p0.y)**2 + (p1.x - p0.x)**2) else: print("Unknown segment type:", s) l = 0 segment_lengths.append(int(round(l))) lengths[(i, j)] = segment_lengths return lengths
def getPointsFromCurve(p, div=0.75): points = [] length = calcCubicArcLength(p[0], p[1], p[2], p[3]) t = 0 step = div / length # print("Length:", d, "Steps:", step) while t < 1: points.append(getCubicPoint(t, p[0], p[1], p[2], p[3])) t += step points.append(p[3]) return points
def calcCubicArcLength_cached(a, b, c, d): #return calcCubicArcLength(a, b, c, d) global __length_cache abcd = (a, b, c, d) lc = __length_cache.get(abcd) if lc: return lc else: l = calcCubicArcLength(a, b, c, d) __length_cache[abcd] = l return l
def split_curve(pts): p0, p1, p2, p3 = pts length_arc = calcCubicArcLength(p0, p1, p2, p3) if length_arc <= self.length: nc.append(["curveTo", pts[1:]]) else: d = self.length / length_arc b = (p0, p1, p2, p3) a, b = splitCubicAtT(*b, d) nc.append(["curveTo", a[1:]]) split_curve(b)
def length(self): return calcCubicArcLength(*self.abcd)