rendered paste body#!/usr/bin/env pythonimport osimport sysimport cairoimport pangoimport codecsimport pangocairo#########################################################__author__ = "Mayank Jain <majain@redhat.com>" #__version__ = "v0.3" #__copyright__ = "Copyright (c) 2006 Mayank Jain" #__license__ = "GPL" ########################################################### class to generate list of unicode points (filenames) & unicodes for corresponding graphemesclass MakuLabsParser: # Constructor requires a filename to read from def __init__(self, filename): self.filename = filename self.codepoints = [] self.glyphs = [] # returns codepoints (filenames) and glyph's unicodes def parse(self): try: fr = codecs.open(self.filename, "r") except Exception: print "Unable to open GSUB file!" sys.exit(-1) for line in fr.readlines(): data = line.split(",") if len(data) == 4: self.codepoints.append(data[0].strip()) self.glyphs.append(data[2].strip().decode("UTF-8")) return self.codepoints, self.glyphsclass MakuLabsRender: # Constructor requires codepoints and glyphs from MaluLabsParser, width & height of the image # and Font family name to use def __init__(self, codepoints, glyphs, width, height, font): self.codepoints = codepoints self.glyphs = glyphs self.width = width self.height = height self.image_padding = 5 self.font = font self.failures = 0 # Converts "U+0D1E U+0D4D U+0D35" to "0D1E-0D4D-0D35" for writing final file # Returns "0D1E-0D4D-0D35" def fileNameFromUnicode(self, name): new_name = "" points = name.split(" ") for point in points: if point == "": continue new_name += point.split("+")[1] new_name += "-" return new_name[0:len(new_name)-1] # __FIXME__ # Should calculate where to start rendering from so as to centre image perfectly def calculateWidthHeight(self, context): font_metrics = context.get_metrics(context.get_font_description(), context.get_language()) x = pango.PIXELS(font_metrics.get_approximate_char_width()) y = pango.PIXELS(font_metrics.get_ascent() + font_metrics.get_descent()) xx = self.width yy = self.height if x < self.width: xx = (self.width - x)/2 if y < self.height: yy = (self.height - y)/2 return xx/2, yy/2 # Render the image to a PNG surface # Returns filename.PNG def renderImage(self, codepoint, glyph): surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, self.width, self.height) cr = pangocairo.CairoContext(cairo.Context(surface)) cr.set_source_rgb(1.0, 1.0, 1.0) cr.rectangle(0, 0, self.width, self.height) cr.fill() layout = cr.create_layout() cr.set_source_rgb(0, 0, 0) #xx, yy = self.calculateWidthHeight(layout.get_context()) cr.translate(self.image_padding, self.image_padding) layout.set_font_description(pango.FontDescription(self.font)) layout.set_text(glyph.encode("UTF-8")) cr.show_layout(layout) filename = self.fileNameFromUnicode(codepoint) + ".png" surface.write_to_png(filename) return filename # Converts the PNG image to JPEG & writes back to the current dir # Also deletes the PNG image # Returns 0 on success, -1 on error def convertToJpeg(self, filename): try: fr = os.popen("basename " + filename + " .png") basename = fr.read().strip() fr.close() status1 = os.system("convert " + filename + " " + basename + ".jpg") status2 = os.system("rm -f " + filename) except Exception: print "Unable to execute a command required to convert %s format!" % (filename,) sys.exit(-1) if ((status1 != 0) and (status2 != 0)): print "Error in converting " + filename + " to " + basename + ".jpg, please check!" return -1 else: return 0 # Stub to render images one by one def renderImages(self): counter = 0 for codepoint in self.codepoints: filename = self.renderImage(codepoint, self.glyphs[counter]) if self.convertToJpeg(filename) != 0: self.failures += 1 else: print filename.split(".")[0] + ".jpg written (grapheme cluster = " + self.glyphs[counter] + " )" counter += 1 success_percentage = ((counter - self.failures) / counter) * 100 print "Success = %d%s, Failures = %d" % (success_percentage, "%", self.failures) if __name__ == "__main__": usage = """ %s <GSUB file> <Img width> <Img height> <Font Family> Example of <Font Family> can be "Lohit Hindi Bold 15" (c) Mayank Jain <majain@redhat.com> """ if len(sys.argv) != 5: print usage % (sys.argv[0].split("/")[-1],) sys.exit(-1) else: codepoints, glyphs = MakuLabsParser(sys.argv[1]).parse() imageRenderer = MakuLabsRender(codepoints, glyphs, int(sys.argv[2]), int(sys.argv[3]), sys.argv[4]) imageRenderer.renderImages() print "(c) Mayank Jain <majain@redhat.com>"