""" Module StringFormat The StringFormat module allows for character-by-character formatting of strings. It imitates the SPING string drawing and string metrics interface. The string formatting is done with specialized XML syntax within the string. Therefore, the interface for the StringFormat module consists of wrapper functions for the SPING string interface and various XML tags and characters. StringFormat functions drawString(canvas, s, x, y, [font], [color], [angle]) stringWidth(canvas, s, [font]) fontHeight(canvas, [font]) fontAscent(canvas, [font]) fontDescent(canvas, [font]) StringFormat XML tags - bold - italics - underline - superscript - subscript StringFormat XML characters Greek Letter Symbols as specified in MathML """ # How it works: Each tag grouping sets a flag upon entry and # clears the flag upon exit. Each call to handle_data creates a # StringSegment which takes on all of the characteristics specified # by flags currently set. The greek letters can be specified as either # α or . The are essentially transformed into # no matter what and then there is a handler for each greek letter. # To add or change greek letter to symbol font mappings only # the greekchars map needs to change. from sping.pid import Font import xmllib import math #------------------------------------------------------------------------ # constants sizedelta = 2 # amount to reduce font size by for super and sub script subFraction = 0.5 # fraction of font size that a sub script should be lowered superFraction = 0.5 # fraction of font size that a super script should be raised #------------------------------------------------------------------------ # greek mapping dictionary # characters not supported: epsi, Gammad, gammad, kappav, rhov # Upsi, upsi greekchars = { 'alpha':'a', 'beta':'b', 'chi':'c', 'Delta':'D', 'delta':'d', 'epsiv':'e', 'eta':'h', 'Gamma':'G', 'gamma':'g', 'iota':'i', 'kappa':'k', 'Lambda':'L', 'lambda':'l', 'mu':'m', 'nu':'n', 'Omega':'W', 'omega':'w', 'omicron':'x', 'Phi':'F', 'phi':'f', 'phiv':'j', 'Pi':'P', 'pi':'p', 'piv':'v', 'Psi':'Y', 'psi':'y', 'rho':'r', 'Sigma':'S', 'sigma':'s', 'sigmav':'V', 'tau':'t', 'Theta':'Q', 'theta':'q', 'thetav':'j', 'Xi':'X', 'xi':'x', 'zeta':'z' } #------------------------------------------------------------------------ class StringSegment: """class StringSegment contains the intermediate representation of string segments as they are being parsed by the XMLParser. """ def __init__(self): self.super = 0 self.sub = 0 self.bold = 0 self.italic = 0 self.underline = 0 self.s = "" self.width = 0 self.greek = 0 def calcNewFont(self,font): "Given a font (does not accept font==None), creates a \ new font based on the format of this text segment." # if we are a greek character we need to pick a different fontface if self.greek: face = "symbol" else: face = font.face # want to make sure that we don't lose any of the base # font formatting return Font(face=face, size=font.size - (self.super*sizedelta) - (self.sub*sizedelta), underline = self.underline or font.underline, bold = self.bold or font.bold, italic = self.italic or font.italic) def calcNewY(self,font,y): "Returns a new y coordinate depending on its \ whether the string is a sub and super script." # should this take into account angle, I think probably not if self.sub == 1: return y+(font.size * subFraction) elif self.super == 1: return y-(font.size * superFraction) else: return y def dump(self): print "StringSegment: ]%s[" % self.s print "\tsuper = ", self.super print "\tsub = ", self.sub print "\tbold = ", self.bold print "\titalic = ",self.italic print "\tunderline = ", self.underline print "\twidth = ", self.width print "\tgreek = ", self.greek #------------------------------------------------------------------ # The StringFormatter will be able to format the following xml # tags: # < b > < /b > - bold # < i > < /i > - italics # < u > < /u > - underline # < super > < /super > - superscript # < sub > < /sub > - subscript # # It will also be able to handle any MathML specified Greek characters. # # Possible future additions: changing color and font # character-by-character #------------------------------------------------------------------ class StringFormatter(xmllib.XMLParser): #---------------------------------------------------------- # First we will define all of the xml tag handler functions. # # start_(attributes) # end_() # # While parsing the xml StringFormatter will call these # functions to handle the string formatting tags. # At the start of each tag the corresponding field will # be set to 1 and at the end tag the corresponding field will # be set to 0. Then when handle_data is called the options # for that data will be aparent by the current settings. #---------------------------------------------------------- #### bold def start_b( self, attributes ): self.bold = 1 def end_b( self ): self.bold = 0 #### italics def start_i( self, attributes ): self.italic = 1 def end_i( self ): self.italic = 0 #### underline def start_u( self, attributes ): self.underline = 1 def end_u( self ): self.underline = 0 #### super script def start_super( self, attributes ): self.super = 1 def end_super( self ): self.super = 0 #### sub script def start_sub( self, attributes ): self.sub = 1 def end_sub( self ): self.sub = 0 #### greek script def start_greek(self, attributes, letter): # print "creating a greek letter... ", letter self.greek = 1 self.handle_data(letter) def end_greek(self): self.greek = 0 #---------------------------------------------------------------- def __init__(self): xmllib.XMLParser.__init__(self) # initialize list of string segments to empty self.segmentlist = [] # initialize tag values self.sub = 0 self.super = 0 self.bold = 0 self.italic = 0 self.underline = 0 # set up handlers for various tags self.elements = { 'b': (self.start_b, self.end_b), 'u': (self.start_u, self.end_u), 'i': (self.start_i, self.end_i), 'super': (self.start_super, self.end_super), 'sub': (self.start_sub, self.end_sub) } # automatically add handlers for all of the greek characters for item in greekchars.keys(): self.elements[item] = (lambda attr,self=self,letter=greekchars[item]: \ self.start_greek(attr,letter), self.end_greek) # flag for greek characters self.greek = 0 # set up dictionary for greek characters, this is a class variable # should I copy it and then update it? for item in greekchars.keys(): self.entitydefs[item] = '<%s/>' % item #---------------------------------------------------------------- # def syntax_error(self,message): # print message #---------------------------------------------------------------- def handle_data(self,data): "Creates an intermediate representation of string segments." # segment first has data segment = StringSegment() segment.s = data # if sub and super are both one they will cancel each other out if self.sub == 1 and self.super == 1: segment.sub = 0 segment.super = 0 else: segment.sub = self.sub segment.super = self.super # bold, italic, and underline segment.bold = self.bold segment.italic = self.italic segment.underline = self.underline # greek character segment.greek = self.greek self.segmentlist.append(segment) #---------------------------------------------------------------- def parseSegments(self,s): "Given a formatted string will return a list of \ StringSegment objects with their calculated widths." # the xmlparser requires that all text be surrounded by xml # tags, therefore we must throw some unused flags around the # given string self.feed("" + s + "") self.close() # force parsing to complete self.reset() # get rid of any previous data segmentlist = self.segmentlist self.segmentlist = [] return segmentlist #------------------------------------------------------------------------ # These functions just implement an interface layer to SPING def fontHeight(canvas, font=None): "Find the total height (ascent + descent) of the given font." return canvas.fontHeight(font) def fontAscent(canvas, font=None): "Find the ascent (height above base) of the given font." return canvas.fontAscent(font) def fontDescent(canvas, font=None): "Find the descent (extent below base) of the given font." return canvas.fontDescent(font) #------------------------------------------------------------------------ # create an instantiation of the StringFormatter #sformatter = StringFormatter() #------------------------------------------------------------------------ # stringWidth and drawString both have to parse the formatted strings def stringWidth(canvas, s, font=None): "Return the logical width of the string if it were drawn \ in the current font (defaults to canvas.font)." sformatter = StringFormatter() segmentlist = sformatter.parseSegments(s) # to calculate a new font the segments must be given an actual font if not font: font = canvas.defaultFont # sum up the string widths of each formatted segment sum = 0 for seg in segmentlist: sum = sum + canvas.stringWidth(seg.s, seg.calcNewFont(font) ) return sum def rotateXY( x, y, theta): "Rotate (x,y) by theta degrees. Got tranformation \ from page 299 in linear algebra book." radians = theta * math.pi/180.0 # had to change the signs to deal with the fact that the y coordinate # is positive going down the screen return ( math.cos(radians)*x + math.sin(radians)*y, -(math.sin(radians)*x - math.cos(radians)*y)) def drawString( canvas, s, x, y, font=None, color=None, angle=0): "Draw a formatted string starting at location x,y in canvas." sformatter = StringFormatter() segmentlist = sformatter.parseSegments(s) # to calculate a new font the segments must be given an actual font if not font: font = canvas.defaultFont # have each formatted string segment specify its own font startpos = x for seg in segmentlist: # calculate x and y for this segment based on the angle # if the string wasn't at an angle then # (draw_x,draw_y) = (startpos, seg.calcNewY(font, y)) want to # rotate around original x and y (delta_x, delta_y) = rotateXY(startpos-x, seg.calcNewY(font,y)-y, angle) canvas.drawString(seg.s,x+delta_x, y+delta_y, seg.calcNewFont(font),color,angle) # new x start position, startpos is calculated assuming no angle startpos = startpos + canvas.stringWidth(seg.s,seg.calcNewFont(font)) #------------------------------------------------------------------------ # Testing #------------------------------------------------------------------------ from sping.PDF import PDFCanvas def test1(): canvas = PDFCanvas('test1.pdf') drawString(canvas,"hello therehi",10,20) drawString(canvas,"hello!",10,40) print "'hello!' width = ", stringWidth(canvas,"hello!") print "'hello!' SPING width = ", canvas.stringWidth("hello!") drawString(canvas, "hello! goodbye", 10,60) print "'hello! goodbye' width = ", stringWidth(canvas,"hello! goodbye") drawString(canvas, "hello!", 10,80, Font(bold=1)) print "'hello!' Font(bold=1) SPING width = ", canvas.stringWidth("hello!",Font(bold=1)) drawString(canvas, " goodbye", 10,100) print "' goodbye' SPING width = ", canvas.stringWidth(" goodbye") canvas.flush() def test2(): canvas = PDFCanvas('test2.pdf') drawString(canvas, "", 10, 10 ) # drawString(canvas, "&", 10, 10) drawString(canvas, "α", 10,30) # drawString(canvas, "a", 10, 50, Font(face="symbol")) # drawString(canvas, "hello there!", 30, 90, angle= -90) # drawString(canvas, "goodbye! yall", 100, 90, angle= 45) # drawString(canvas, "there is a time and a place2", # 100, 90, angle= -75) canvas.flush() def allTagCombos(canvas,x, y, font=None, color=None, angle=0): """Try out all tags and various combinations of them. \ Starts at given x,y and returns possible next (x,y).""" oldDefault = canvas.defaultFont if font: canvas.defaultFont = font oldx = x dx = stringWidth(canvas, " ") dy = canvas.defaultFont.size*1.5 drawString(canvas, "bold", x, y, color=color, angle=angle ) x = x + stringWidth(canvas,"bold") + dx drawString(canvas, "italic", x, y, color=color, angle=angle ) x = x + stringWidth(canvas,"italic") + dx drawString(canvas, "underline", x, y, color=color, angle=angle ) x = x + stringWidth(canvas,"underline") + dx drawString(canvas, "super", x, y, color=color, angle=angle ) x = x + stringWidth(canvas,"super") + dx drawString(canvas, "sub", x, y, color=color, angle=angle ) y = y + dy drawString(canvas, "bold+underline", oldx, y, color=color, angle=angle ) x = oldx + stringWidth(canvas,"bold+underline") + dx drawString(canvas, "super+italic", x, y, color=color, angle=angle ) x = x + stringWidth(canvas,"super+italic") + dx drawString(canvas, "bold+sub", x, y, color=color, angle=angle ) # x = x + stringWidth(canvas,"bold+sub") + dx y = y + dy canvas.defaultFont = oldDefault return (oldx, y) def stringformatTest(): # change the following line only to try a different SPING backend canvas = PDFCanvas('bigtest1.pdf') ################################################### testing drawString tags # < b > < /b > - bold # < i > < /i > - italics # < u > < /u > - underline # < super > < /super > - superscript # < sub > < /sub > - subscript x = 10 y = canvas.defaultFont.size*1.5 ##### try out each possible tags and all combos (x,y) = allTagCombos(canvas, x, y) ##### now try various fonts (x,y) = allTagCombos(canvas, x, y+30, Font(face="serif")) (x,y) = allTagCombos(canvas, x, y+30, Font(face="monospaced")) # what about rotated (x,y) = allTagCombos(canvas, x, y+30, Font(face="serif"), angle=-30) ##### now try a couple of different font sizes (x,y) = allTagCombos(canvas, x, y+30, Font(size=16)) (x,y) = allTagCombos(canvas, x, y+30, Font(size=9)) ##### now try a different default style setting (x,y) = allTagCombos(canvas, x, y+30, Font(underline=1)) ##### now try a combo of the above 4 and a different color (x,y) = allTagCombos(canvas, x, y+30, color = red) ################################################### testing stringWidth tags sfwidth = stringWidth(canvas, "bold+sub hello underline+super") # break down the various string widths print 'sw("bold+sub") = ', stringWidth(canvas,"bold+sub") print 'sw(" hello ") = ', stringWidth(canvas," hello ") print 'sw("underline+super") = ', \ stringWidth(canvas,"underline+super") pwidth1 = canvas.stringWidth("bold+sub",Font(size=canvas.defaultFont.size-sizedelta, bold=1)) print "pwidth1 = ", pwidth1 pwidth2 = canvas.stringWidth(" hello ") print "pwidth2 = ", pwidth2 pwidth3 = canvas.stringWidth("underline+super", Font(size=canvas.defaultFont.size-sizedelta,underline=1)) print "pwidth3 = ", pwidth3 # these should be the same print "sfwidth = ", sfwidth, " pwidth = ", pwidth1+pwidth2+pwidth3 ################################################### testing greek characters # looks better in a larger font canvas = PDFCanvas('bigtest2.pdf') x = 10 y = canvas.defaultFont.size*1.5 drawString(canvas,"α β Δ ",x,y, Font(size=16), color = blue) print "line starting with alpha should be font size 16" y = y+30 drawString(canvas,"ϵ η Γ ",x,y, color = green) y = y+30 drawString(canvas,"ι κ Λ ",x,y, color = blue) y = y+30 drawString(canvas,"μ ν Ω ",x,y, color = green) print "mu should be underlined, Omega should be big and bold" y = y+30 drawString(canvas,"ο Φ φ ",x,y, color = blue) y = y+30 drawString(canvas,"Π π ϖ ψ ρ",x,y, color = green) y = y+30 drawString(canvas,"Σ σ ς ",x,y, color = blue) print "line starting with sigma should be completely underlined" y = y+30 drawString(canvas,"Θ θ ϑ ξ ζ",x,y, color = green) y= y+30 drawString(canvas,"That's αll folksω",x,y) canvas.flush() #test1() #test2() #stringformatTest()