def parse(dxf_file, segarc, new_origin=True): font = Font() DXF_source = " " dxf_import = DXF_CLASS() dxf_import.GET_DXF_DATA(dxf_file, tol_deg=segarc) dxfcoords = dxf_import.DXF_COORDS_GET(new_origin) if "POTRACE" in dxf_import.comment.upper(): DXF_source = "POTRACE" if "INKSCAPE" in dxf_import.comment.upper(): DXF_source = "INKSCAPE" # save the character to our dictionary key = ord("F") stroke_list = [] bbox = BoundingBox() for line in dxfcoords: line = Line(line[0:4]) stroke_list.append(line) bbox.extend(line) font.add_character(Character(key, stroke_list)) return font, DXF_source
def get_char_bbox_used(self, string): bbox = BoundingBox() for char in string: bbox.extend(self[ord(char)]) return bbox
def test_center(self): bbox = BoundingBox(0, 10, 0, 10) self.assertEquals(bbox.center(), (5.0, 5.0)) bbox.pad(500) self.assertEquals(bbox.center(), (5.0, 5.0))
def test_pad(self): bbox = BoundingBox(0, 100, 0, 100).pad(10) self.assertEquals(bbox, BoundingBox(-10, 110, -10, 110)) bbox = BoundingBox(10, 12, 10, 12) bbox.pad(-0.8) self.assertEquals(bbox, BoundingBox(10.8, 11.2, 10.8, 11.2))
def test_initizialize(self): bbox = BoundingBox() self.assertEquals(bbox.xmin, INFINITY) self.assertEquals(bbox.xmax, -INFINITY) self.assertEquals(bbox.ymin, INFINITY) self.assertEquals(bbox.ymax, -INFINITY) xmin, xmax, ymin, ymax = (0, 12, 123, 400) bbox = BoundingBox(xmin, xmax, ymin, ymax) self.assertEquals(bbox.xmin, xmin) self.assertEquals(bbox.xmax, xmax) self.assertEquals(bbox.ymin, ymin) self.assertEquals(bbox.ymax, ymax)
class Font(object): """ The complete characterset of a font """ def __init__(self): self.characters = {} self.bbox = BoundingBox() def __len__(self): return len(self.characters) def __getitem__(self, key): # Space or Linefeed if key in (32, 10): return Character(' ', []) try: return self.characters[key] except: # return a question mark for chars missing in this characterset return self.characters[63] def __iter__(self): for char in self.characters: yield char def add_character(self, char): self.characters[char.key] = char self.bbox.extend(char.bounds()) def get_char_bbox_used(self, string): bbox = BoundingBox() for char in string: bbox.extend(self[ord(char)]) return bbox def get_character_width(self): return max(self.characters[key].get_xmax() for key in self.characters) def line_height(self): return self.bbox.ymax def line_depth(self): return self.bbox.ymin
def test_extend_with_lines(self): bbox = BoundingBox() with open('tests/files/lines.csv', 'r') as csvfile: lines = [map(float, line) for line in csv.reader(csvfile, delimiter=',')] minx, maxx, miny, maxy = (INFINITY, -INFINITY, INFINITY, -INFINITY) for line in lines: bbox.extend(Line(map(float, line))) minx = min(minx, line[0], line[2]) maxx = max(maxx, line[0], line[2]) miny = min(miny, line[1], line[3]) maxy = max(maxy, line[1], line[3]) self.assertEquals(str(bbox), str(BoundingBox(minx, maxx, miny, maxy)))
def test_extend_with_lines(self): bbox = BoundingBox() with open('tests/files/lines.csv', 'r') as csvfile: csv_file = csv.reader(csvfile, delimiter=',') lines = [list(map(float, line)) for line in csv_file] minx, maxx, miny, maxy = (INFINITY, -INFINITY, INFINITY, -INFINITY) for line in lines: bbox.extend(Line(map(float, line))) minx = min(minx, line[0], line[2]) maxx = max(maxx, line[0], line[2]) miny = min(miny, line[1], line[3]) maxy = max(maxy, line[1], line[3]) self.assertEquals(str(bbox), str(BoundingBox(minx, maxx, miny, maxy)))
class Shape(object): def __init__(self, vertices, index_lists): self.vertices = vertices self.index_lists = index_lists self.bounding_box = BoundingBox() for v in self.vertices: self.bounding_box.bound_point(v) def get_lines(self): return [LineStripe(self.vertices, index_list) for index_list in self.index_lists] @staticmethod def load(filename): with open(filename, 'r') as fobj: vertices = [] lines = [] for line in fobj: data = line.split() if data[0] == 'v': vertices.append(Vec3(float(data[1]), float(data[2]), float(data[3]))) elif data[0] == 'f': lines.append([int(d)-1 for d in data[1:]]) print('loaded ' + str(len(vertices)) + ' vertices, ' + str(len(lines)) + ' lines') return Shape(vertices, lines)
def test_width(self): bbox = BoundingBox(0, 10, 0, 10) self.assertEquals(bbox.width(), 10) bbox = BoundingBox(-10, 10, -10, 10) self.assertEquals(bbox.width(), 20)
def test_extend_with_2tuple(self): bbox = BoundingBox(0, 1, 2, 4).extend(100, 500) self.assertEquals(bbox, BoundingBox(0, 100, 2, 500))
def test_extend_with_4tuple(self): bbox = BoundingBox(0, 1, 2, 4).extend(0, 1, 3, 8) self.assertEquals(bbox, BoundingBox(0, 1, 2, 8))
def test_extend_with_line(self): bbox = BoundingBox(0, 0, 0, 0) bbox.extend(Line((0, 0, 100, 100))) self.assertEquals(bbox, BoundingBox(0, 100, 0, 100))
def test_extend_with_bbox(self): bbox = BoundingBox() bbox.extend(BoundingBox(0, 1.0, 2, 4)) bbox.extend(BoundingBox(1, 4, 8, 100)) self.assertEquals(bbox, BoundingBox(0.0, 4, 2, 100))
def __init__(self): self.characters = {} self.bbox = BoundingBox()
def test_height(self): bbox = BoundingBox(0, 10, 0, 10) self.assertEquals(bbox.height(), 10)
def test_str(self): bbox = BoundingBox(0, 1, 2, 3) self.assertEquals(str(bbox), 'BoundingBox([0.0, 1.0, 2.0, 3.0])')
def __init__(self, vertices, index_lists): self.vertices = vertices self.index_lists = index_lists self.bounding_box = BoundingBox() for v in self.vertices: self.bounding_box.bound_point(v)
def bounds(self): return BoundingBox(self.get_xmin(), self.get_xmax(), self.get_ymin(), self.get_ymax())
def bounds(self): return BoundingBox(self.xmin, self.xmax, self.ymin, self.ymax)