def test1(): canvas = PDFCanvas('test1.pdf') drawString(canvas, "<u><b>hello there</b></u><super>hi</super>", 10, 20) drawString(canvas, "hello!", 10, 40) print("'hello!' width = ", stringWidth(canvas, "hello!")) print("'hello!' SPING width = ", canvas.stringWidth("hello!")) drawString(canvas, "<b>hello!</b> goodbye", 10, 60) print("'<b>hello!</b> goodbye' width = ", stringWidth(canvas, "<b>hello!</b> 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 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 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, "<b><sub>bold+sub</sub></b> hello <u><super>underline+super</super></u>" ) # break down the various string widths print('sw("<b><sub>bold+sub</sub></b>") = ', stringWidth(canvas, "<b><sub>bold+sub</sub></b>")) print('sw(" hello ") = ', stringWidth(canvas, " hello ")) print('sw("<u><super>underline+super</super></u>") = ', stringWidth(canvas, "<u><super>underline+super</super></u>")) 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, "α β <chi/> Δ <delta/>", x, y, Font(size=16), color=blue) print("line starting with alpha should be font size 16") y = y + 30 drawString(canvas, "ϵ η Γ <gamma/>", x, y, color=green) y = y + 30 drawString(canvas, "ι κ Λ <lambda/>", x, y, color=blue) y = y + 30 drawString(canvas, "<u>μ</u> ν <b>Ω</b> <omega/>", x, y, color=green) print("mu should be underlined, Omega should be big and bold") y = y + 30 drawString(canvas, "ο Φ φ <phiv/>", x, y, color=blue) y = y + 30 drawString(canvas, "Π π ϖ <Psi/> ψ ρ", x, y, color=green) y = y + 30 drawString(canvas, "<u>Σ σ ς <tau/></u>", x, y, color=blue) print("line starting with sigma should be completely underlined") y = y + 30 drawString(canvas, "Θ θ ϑ <Xi/> ξ ζ", x, y, color=green) y = y + 30 drawString(canvas, "That's αll <u>folks</u><super>ω</super>", x, y) canvas.flush()