def read(buf, max_num_pts_per_contour): glyph_proto = font_pb2.glyph() glyph_proto.ParseFromString(open(buf, 'rb').read()) glyph = glyph_proto.glyph[0] num_pts_per_contour = glyph.contour_locations if not num_pts_per_contour: return "" if max(num_pts_per_contour) < max_num_pts_per_contour: return buf else: return ""
def pkl2protos(protoDir, pickleName, fileNum): ''' This function takes one pickle and proto directory and puts all of its glyphs into protos in the proto dir Parameters ---------- proto_dir: the directory where all the protos should be saved pickle: the location of the pickle to be put into protos ''' try: with open(pickleName, 'rb') as fontString: fontDict = pickle.load(fontString) for glyph in fontDict: # basically we are going to flatten everything into just an array of # points # we will keep track of the location of where each contour stops so we # can reconstruct on the other end. proto = font_pb2.glyph() if glyph in CHARS and notRepeat(glyph, fontDict): contours = fontDict[glyph] num_contours = len(contours) points = [] contour_locations = [] for _, c in contours.items(): contour_locations.append(len(c)) for curve in c: for point in curve: points += point #write it in newGlyph = proto.glyph.add() newGlyph.num_contours = num_contours points = list(points) newGlyph.bezier_points.extend(points) newGlyph.contour_locations.extend(contour_locations) newGlyph.font_name = pickleName newGlyph.glyph_name = glyph #save it up with open("{}/{}{}".format(protoDir, glyph, fileNum), 'ab') as f: f.write(proto.SerializeToString()) except Exception as E: print(pickleName, E, sys.exc_info()[0])
def read(buf, max_num_pts_per_contour, num_samples): glyph_proto = font_pb2.glyph() glyph_proto.ParseFromString(open(buf, 'rb').read()) glyph = glyph_proto.glyph[0] bezier_points = deque(glyph.bezier_points) num_pts_per_contour = glyph.contour_locations if not num_pts_per_contour: return 2 * [None] if len(num_pts_per_contour) > 3: return 2 * [None] if max(num_pts_per_contour) > max_num_pts_per_contour: return 2 * [None] else: contours = [] samples = np.zeros((3, num_samples, 2), np.float32) bottom = 10 top = -10 for i, num_pts in enumerate(num_pts_per_contour): pts = np.array( [bezier_points.popleft() for _ in range(num_pts * 6)]) contour = pts.reshape([-1, 6]).reshape(-1, 3, 2) if contour.shape[0] == 0: return 2 * [None] bottom = np.minimum(bottom, np.amin(contour)) top = np.maximum(top, np.amax(contour)) contours.append(contour) for i, contour in enumerate(contours): contours[i] = (contour - bottom) / (top - bottom) samples[i] = sample_qb(contours[i], num_samples) return contours, samples
def read(buf, max_num_pts_per_contour, num_samples): glyph_proto = font_pb2.glyph() glyph_proto.ParseFromString(open(buf, 'rb').read()) glyph = glyph_proto.glyph[0] bezier_points = deque(glyph.bezier_points) num_pts_per_contour = glyph.contour_locations if not num_pts_per_contour: return None if len(num_pts_per_contour) > 3: return None if max(num_pts_per_contour) > max_num_pts_per_contour: return None else: contours = np.zeros((3, num_samples, 2), np.float32) for i, num_pts in enumerate(num_pts_per_contour): pts = np.array( [bezier_points.popleft() for _ in range(num_pts * 6)]) contour = pts.reshape([-1, 6]).reshape(-1, 3, 2) contours[i] = sample_qb(contour, num_samples) return contours