#! /usr/bin/python # vim:ts=4:sw=4:softtabstop=4:smarttab:expandtab import sys import csv import os import getopt from struct import pack from struct import unpack class PParser(object): def __init__(self, gd, flatxml): self.gd = gd self.flatdoc = flatxml.split('\n') self.temp = [] foo = self.getData('page.h') or self.getData('book.h') self.ph = foo[0] foo = self.getData('page.w') or self.getData('book.w') self.pw = foo[0] self.gx = self.getData('info.glyph.x') self.gy = self.getData('info.glyph.y') self.gid = self.getData('info.glyph.glyphID') def getData(self, path): result = None cnt = len(self.flatdoc) for j in xrange(cnt): item = self.flatdoc[j] if item.find('=') >= 0: (name, argt) = item.split('=') argres = argt.split('|') else: name = item argres = [] if (name.endswith(path)): result = argres break if (len(argres) > 0) : for j in xrange(0,len(argres)): argres[j] = int(argres[j]) return result def getDataTemp(self, path): result = None cnt = len(self.temp) for j in xrange(cnt): item = self.temp[j] if item.find('=') >= 0: (name, argt) = item.split('=') argres = argt.split('|') else: name = item argres = [] if (name.endswith(path)): result = argres self.temp.pop(j) break if (len(argres) > 0) : for j in xrange(0,len(argres)): argres[j] = int(argres[j]) return result def getImages(self): result = [] self.temp = self.flatdoc while (self.getDataTemp('img') != None): h = self.getDataTemp('img.h')[0] w = self.getDataTemp('img.w')[0] x = self.getDataTemp('img.x')[0] y = self.getDataTemp('img.y')[0] src = self.getDataTemp('img.src')[0] result.append('\n' % (src, x, y, w, h)) return result def getGlyphs(self): result = [] if (self.gid != None) and (len(self.gid) > 0): glyphs = [] for j in set(self.gid): glyphs.append(j) glyphs.sort() for gid in glyphs: id='id="gl%d"' % gid path = self.gd.lookup(id) if path: result.append(id + ' ' + path) return result def convert2SVG(gdict, flat_xml, counter, numfiles, svgDir, raw, meta_array, scaledpi): ml = '' pp = PParser(gdict, flat_xml) ml += '\n' if (raw): ml += '\n' ml += '\n' % (pp.pw / scaledpi, pp.ph / scaledpi, pp.pw -1, pp.ph -1) ml += 'Page %d - %s by %s\n' % (counter, meta_array['Title'],meta_array['Authors']) else: ml += '\n' ml += '\n' ml += 'Page %d - %s by %s\n' % (counter, meta_array['Title'],meta_array['Authors']) ml += '\n' ml += '\n' ml += '\n' ml += '\n' if (counter == 0) : ml += '\n' else: ml += '\n' ml += '' % (pp.pw, pp.ph) if (pp.gid != None): ml += '\n' gdefs = pp.getGlyphs() for j in xrange(0,len(gdefs)): ml += gdefs[j] ml += '\n' img = pp.getImages() if (img != None): for j in xrange(0,len(img)): ml += img[j] if (pp.gid != None): for j in xrange(0,len(pp.gid)): ml += '\n' % (pp.gid[j], pp.gx[j], pp.gy[j]) if (img == None or len(img) == 0) and (pp.gid == None or len(pp.gid) == 0): ml += 'This page intentionally left blank.\nUntil this notice unintentionally gave it content. (gensvg.py)\n' if (raw) : ml += '' else : ml += '\n' if (counter == numfiles - 1) : ml += '\n' else : ml += '\n' ml += '\n' ml += 'zoom in - zoom out\n' ml += '\n' ml += '\n' return ml