if cmd == 'M': x = x * 1.25 y = (-y + 1440) * 1.25 else: if not cursor: # 'm' appear as the first element of a path x = x * 1.25 y = (-y + 1440) * 1.25 else: x = cursor.x + x * 1.25 y = cursor.y + y * -1.25 p = Point(x, y) if not path.start: path.start = p else: sl = StraightLine.from_points(cursor, p) path.extend(sl) path.end = p cursor = p if cmd in 'Ll': while len(nums) > 0: x = nums.pop(0) y = nums.pop(0) if cmd == 'L': x = x * 1.25 y = (-y + 1440) * 1.25 else: x = cursor.x + x * 1.25 y = cursor.y + y * -1.25 p = Point(x, y) sl = StraightLine.from_points(cursor, p) path.extend(sl)
def get_paths(filename): """Get a colletion of Path objects from text data.""" pattern = re.compile("(?:[-,]?[0-9]+(?:\.[0-9]+)?)") paths = [] fp = open(filename, "r") for line in fp.readlines(): path = None cursor = Point(0, 0) # can't initialize to None because of command "moveto" for seg in line.split(): command = seg[0] if command in "Zz": path.close() else: """Get and convert to actual coordinate(number) from a command section""" nums = pattern.findall(seg[1:]) for i in range(0, len(nums)): nums[i] = float(nums[i].strip(",")) * 1.25 # 1.25 is just a size factor """Convert relative coordinate to abstract coordinate Ignore Arc(a) for now""" if command in "mlcsqt": for i in range(0, len(nums)): if i % 2 == 0: nums[i] = nums[i] + cursor.x else: nums[i] = nums[i] + cursor.y elif command == "h": nums[0] = nums[0] + cursor.x elif command == "v": nums[0] = nums[0] + cursor.y """Decide how to proceed with current path Ignore Quadratic Bezier and Arc for now""" if command in "Mm": current_seg = None if not path: path = Path() p1 = Point(nums[0], nums[1]) cursor = p1 else: previous_seg = current_seg if command in "LlHhVv": if command in "Ll": p1 = Point(nums[0], nums[1]) if command in "Hh": p1 = Point(nums[0], cursor.y) if command in "Vv": p1 = Point(cursor.x, nums[0]) current_seg = StraightLine.from_points(cursor, p1) cursor = p1 elif command in "CcSs": p1 = Point(nums[0], nums[1]) p2 = Point(nums[2], nums[3]) if command in "Cc": p3 = Point(nums[4], nums[5]) current_seg = CubicBezier(cursor, p1, p2, p3) cursor = p3 if command in "Ss": if isinstance(previous_seg, CubicBezier): mirror = Point(2 * cursor.x - previous_seg.c2.x, 2 * cursor.y - previous_seg.c2.y) else: mirror = cursor.clone() current_seg = CubicBezier(cursor, mirror, p1, p2) cursor = p2 else: pass path.extend(current_seg) """This is important, since there are empty paths (contain only a "move" command but nothing else) in the input data""" if len(path.segments) > 0: paths.append(path) fp.close() return paths