def compile(self, font, steps=3): """Compile the glyph to a set of poly-line outlines""" self.contours = self.calculateContours(font) self.outlines = [ decomposeOutline(contour, steps) for contour in self.contours ] self.width = glyphquery.width(font, self.glyphName) self.height = glyphquery.lineHeight(font)
def compile( self, font, steps = 3 ): """Compile the glyph to a set of poly-line outlines""" self.contours = self.calculateContours( font ) self.outlines = [ decomposeOutline(contour,steps) for contour in self.contours ] self.width = glyphquery.width( font, self.glyphName ) self.height = glyphquery.lineHeight( font )
def get_font_size(fontsize,text): width = 0 for char in text: width += glyphquery.width(font,glyphquery.glyphName(font,char)) scaling=2000/fontsize #no idea what this is if args.debug: print "label '%s' calculated to be %dx%d" % (text,width/scaling,glyphquery.charHeight(font)/scaling) return(width/scaling,glyphquery.charHeight(font)/scaling)
def getImageSizeFor(str): fontSize = 10.0 unitsPerEm = 2048.0 ppiScale = 96.0 / 72.0 scalingFactor = fontSize * ppiScale / unitsPerEm from ttfquery import describe, glyphquery myfont = describe.openFont("C:\\Windows\\Fonts\\arial.ttf") width = 0 widestWidth = getWidestHMetric() * scalingFactor for char in str: try: print "Width of char %s: %f" % (char, (glyphquery.width(myfont, char) * scalingFactor)) width += glyphquery.width(myfont, char) * scalingFactor except: print "Exception thrown on", char width += widestWidth print "Total width:", width height = getHeight() * scalingFactor return (int(math.ceil(width)), int(math.ceil(height)))
def get_string_width(message, size): taken = 0 char_size = 50 char_list = list(message) for character in char_list: try: char_size = (glyphquery.width(font, character) / size) taken = taken + char_size except: taken = taken + char_size return taken
def straight_stitch_string(message, size, x_offset, y_offset, font): char_list = list(message) points = [] taken = 0 for character in char_list: points.extend( straight_stitch_char(character, size, x_offset + taken, y_offset, font)) try: taken = taken + (glyphquery.width(font, character) / size) except: taken = taken + 30 return points
def satin_stitch_string(message, size, x_offset, y_offset, font): char_list = list(message) i = 0 points = [] taken = 0 char_size = 50 for character in char_list: if (character == ' '): taken = taken + char_size else: points.extend( satin_stitch_char(character, size, x_offset + taken, y_offset, font)) try: char_size = (glyphquery.width(font, character) / size) taken = taken + char_size except: taken = taken + char_size return points, taken
def fill_stitch_string(message, size, x_offset, y_offset, font, slope, density, p_distance): char_list = list(message) points = [] taken = 0 char_size = 50 for character in char_list: if (character == ' '): taken = taken + char_size else: points.extend( fill_stitch_char(character, size, x_offset + taken, y_offset, font, slope, density, p_distance)) try: char_size = (glyphquery.width(font, character) / size) taken = taken + char_size except: taken = taken + char_size return points, taken
def find_whole_trajectory(list_char): x_all = [] y_all = [] contact_all = [] global width print("calculating trajectory...") for char in list_char: if char.isspace(): width += space_width else: x, y, contact = find_char_trajectory(char, width) glyph_name = glyphquery.glyphName(font, char) width += glyphquery.width(font, glyph_name) contact_all.append(contact) if any(isinstance(ls, list) for ls in x): for i in range(len(x)): x_all.append(x[i]) y_all.append(y[i]) else: x_all.append(x) y_all.append(y) return x_all, y_all, contact_all
def convert_text(dict_values, fonts, mtext): layer = dict_values[8][0][0] text = ("".join(dict_values[3][0][0]) if 3 in dict_values else "") + dict_values[1][0][0] xoffset = float(dict_values[10][0][0]) yoffset = float(dict_values[20][0][0]) height = float(dict_values[40][0][0]) rotation = float( dict_values[50][0][0]) / 180.0 * math.pi if 50 in dict_values else 0 font = fonts[dict_values[7][0][0].lower() if 7 in dict_values else "standard"] print("using font", font) if mtext: boxwidth = float(dict_values[41][0][0]) if 41 in dict_values else 1 x_scale = 1 attachment = int(dict_values[71][0][0]) if 71 in dict_values else 1 text = re.sub("\\\\[^;]*;", "", text) text = re.sub("([^\\\\])\\\\P", "\g<1>\\n", text) spacing_fac = float(dict_values[44][0][0]) if 44 in dict_values else 1 else: boxwidth = 1 #None raised error x_scale = float(dict_values[41][0][0]) if 41 in dict_values else 1 xalign = float( dict_values[11][0][0] ) if 11 in dict_values else None #only used for Horizontal text justification typ == 3 yalign = float( dict_values[21][0][0] ) if 11 in dict_values else None #only used for Horizontal text justification typ == 3 drawdir = int( dict_values[71][0][0] ) if 71 in dict_values else 0 #mirroring in X (=2) or Y (=4) direction h_text_just = int( dict_values[72][0][0] ) if 72 in dict_values else 0 #0=left 1=center 2=right 3=aligned v_text_just = int( dict_values[73][0][0] ) if 73 in dict_values else 3 #0=baseline 1=bottom 2=middle 3=top spacing_fac = 1 for number, name in [(51, "'oblique angle'")]: if number in dict_values: print "INFO: sorry but option ", name, "(", number, ") is not supported yet" cheight = float(glyphquery.charHeight(font)) / 1.32 #magic number objects = [] boxwidth /= x_scale * height / cheight charoff = 0 lineno = 1 lines = [[]] for n, letter in enumerate(text): glname = glyphquery.glyphName(font, letter) glwidth = glyphquery.width(font, glname) print(glname, glwidth, letter, charoff) if letter == "\n" or (boxwidth and charoff + glwidth > boxwidth): lines.append(list()) #charoff = 0 # temprarily deactivated, sets charoff always to 0 else... lineno += 1 if letter != "\n": boundaries = [cheight * 10, cheight * 10, 0, 0] for c in glyph.Glyph(glname).calculateContours(font): outlines = glyph.decomposeOutline(c, 2) first = True lines[-1].append({"layer": layer, "command": "G36*"}) for l in outlines: org_x = (l[0] + charoff) * x_scale * height / cheight org_y = l[1] * height / cheight x = org_x * math.cos(rotation) - org_y * math.sin( rotation) + xoffset y = org_x * math.sin(rotation) + org_y * math.cos( rotation) + yoffset # changes polarity -> clear holes in fonts if first and (boundaries[0] < l[0] and l[0] < boundaries[2] and boundaries[1] < l[1] and l[1] < boundaries[3]): lines[-1].pop( ) #pop the last G36; the gerber file is not read correctly if LPC comes within a G36-G37 Block lines[-1].append({"layer": layer, "command": "%LPC*%"}) lines[-1].append({"layer": layer, "command": "G36*"}) boundaries[0] = min(boundaries[0], l[0]) boundaries[1] = min(boundaries[1], l[1]) boundaries[2] = max(boundaries[2], l[0]) boundaries[3] = max(boundaries[3], l[1]) #x*=scaling# #y*=scaling# lines[-1].append({ "layer": layer, "device": "C,0.001*", "x": x, "y": y, "style": "D02" if first else "D01" }) first = False lines[-1].append({"layer": layer, "command": "G37*"}) lines[-1].append({"layer": layer, "command": "%LPD*%"}) charoff += glwidth #print(lines)# #respect the alignment (only affects MTEXT. For TEXT it is already considered if mtext: spacing = height / 3 * 5 * spacing_fac textwidth = charoff * x_scale * height / cheight vertical = int((attachment - 1) / 3) #0=oben 1=mitte 2=unten horizontal = (attachment - 1) % 3 #0=links 1=mitte 2=rechts if horizontal == 0: shift_x = 0 #links elif horizontal == 1: shift_x = -textwidth / 2 #mitte elif horizontal == 2: shift_x = -textwidth #rechts lineno = 0 for line_objects in lines: if vertical == 0: shift_y = -height - (lineno - 1) * spacing #oben elif vertical == 1: shift_y = (len(lines) - lineno) * spacing / 2 #mitte elif vertical == 2: shift_y = (len(lines) - lineno) * spacing #unten lineno += 1 for o in line_objects: if "x" in o: o["x"] += shift_x if "y" in o: o["y"] += shift_y for line_objects in lines: objects.extend(line_objects) return objects, True