author | robin <robin@reportlab.com> |
Tue, 07 Mar 2017 10:00:34 +0000 | |
changeset 4330 | 617ffa6bbdc8 |
parent 4252 | fe660f227cac |
child 4668 | f8d6dc8f07fc |
permissions | -rw-r--r-- |
4330 | 1 |
#Copyright ReportLab Europe Ltd. 2000-2017 |
2963 | 2 |
#see license.txt for license details |
3 |
#test_pdfbase_pdfmetrics_widths |
|
4 |
""" |
|
5 |
Various tests for PDF metrics. |
|
6 |
||
7 |
The main test prints out a PDF documents enabling checking of widths of every |
|
8 |
glyph in every standard font. Long! |
|
9 |
""" |
|
4252 | 10 |
__version__='3.3.0' |
2984 | 11 |
from reportlab.lib.testutils import setOutDir,makeSuiteForClasses, outputfile, printLocation |
12 |
setOutDir(__name__) |
|
2966 | 13 |
import unittest |
2963 | 14 |
from reportlab.pdfbase import pdfmetrics |
15 |
from reportlab.pdfbase import _fontdata |
|
16 |
from reportlab.pdfgen.canvas import Canvas |
|
17 |
from reportlab.lib import colors |
|
3833 | 18 |
from reportlab.lib.utils import isPy3 |
2963 | 19 |
|
20 |
verbose = 0 |
|
21 |
fontNamesToTest = _fontdata.standardFonts #[0:12] #leaves out Symbol and Dingbats for now |
|
22 |
||
23 |
||
24 |
def decoratePage(c, header): |
|
25 |
c.setFont('Helvetica-Oblique',10) |
|
26 |
c.drawString(72, 800, header) |
|
27 |
c.drawCentredString(297, 54, 'Page %d' % c.getPageNumber()) |
|
28 |
||
29 |
||
30 |
def makeWidthTestForAllGlyphs(canv, fontName, outlining=1): |
|
31 |
"""New page, then runs down doing all the glyphs in one encoding""" |
|
32 |
thisFont = pdfmetrics.getFont(fontName) |
|
33 |
encName = thisFont.encName |
|
34 |
canv.setFont('Helvetica-Bold', 12) |
|
35 |
title = 'Glyph Metrics Test for font %s, ascent=%s, descent=%s, encoding=%s' % (fontName, str(thisFont.face.ascent), str(thisFont.face.descent), encName) |
|
36 |
canv.drawString(80, 750, title) |
|
37 |
canv.setFont('Helvetica-Oblique',10) |
|
38 |
canv.drawCentredString(297, 54, 'Page %d' % canv.getPageNumber()) |
|
39 |
||
40 |
if outlining: |
|
41 |
# put it in the outline |
|
42 |
canv.bookmarkPage('GlyphWidths:' + fontName) |
|
43 |
canv.addOutlineEntry(fontName,'GlyphWidths:' + fontName, level=1) |
|
44 |
||
45 |
y = 720 |
|
46 |
widths = thisFont.widths |
|
47 |
glyphNames = thisFont.encoding.vector |
|
48 |
# need to get the right list of names for the font in question |
|
49 |
for i in range(256): |
|
50 |
if y < 72: |
|
51 |
canv.showPage() |
|
52 |
decoratePage(canv, title) |
|
53 |
y = 750 |
|
54 |
glyphName = glyphNames[i] |
|
55 |
if glyphName is not None: |
|
56 |
canv.setFont('Helvetica', 10) |
|
3833 | 57 |
if isPy3: |
58 |
text = bytes([i]).decode(encName)*30 |
|
59 |
else: |
|
3923
28dcee9a3c13
test_pdfbase_pdfdoc.py/test_pdfbase_pdfmetrics.py fix for python 2
robin
parents:
3833
diff
changeset
|
60 |
text = chr(i).decode(encName).encode('utf8')*30 |
3833 | 61 |
|
2963 | 62 |
try: |
63 |
w = canv.stringWidth(text, fontName, 10) |
|
64 |
canv.drawString(80, y, '%03d %s w=%3d' % (i, glyphName, int((w/3.)*10))) |
|
65 |
canv.setFont(fontName, 10) |
|
66 |
canv.drawString(200, y, text) |
|
67 |
||
68 |
# now work out width and put a red marker next to the end. |
|
69 |
canv.setFillColor(colors.red) |
|
3833 | 70 |
canv.rect(200 + w, y-1, 1, 10, stroke=0, fill=1) |
2963 | 71 |
canv.setFillColor(colors.black) |
72 |
except KeyError: |
|
73 |
canv.drawString(200, y, 'Could not find glyph named "%s"' % glyphName) |
|
74 |
y = y - 12 |
|
75 |
||
76 |
||
77 |
def makeTestDoc(fontNames): |
|
78 |
filename = outputfile('test_pdfbase_pdfmetrics.pdf') |
|
79 |
c = Canvas(filename) |
|
80 |
c.bookmarkPage('Glyph Width Tests') |
|
81 |
c.showOutline() |
|
82 |
c.addOutlineEntry('Glyph Width Tests', 'Glyph Width Tests', level=0) |
|
83 |
if verbose: |
|
3721 | 84 |
print() # get it on a different line to the unittest log output. |
2963 | 85 |
for fontName in fontNames: |
86 |
if verbose: |
|
3721 | 87 |
print('width test for', fontName) |
2963 | 88 |
|
89 |
makeWidthTestForAllGlyphs(c, fontName) |
|
90 |
c.showPage() |
|
91 |
c.save() |
|
92 |
if verbose: |
|
93 |
if verbose: |
|
3721 | 94 |
print('saved',filename) |
2963 | 95 |
|
96 |
||
97 |
class PDFMetricsTestCase(unittest.TestCase): |
|
98 |
"Test various encodings used in PDF files." |
|
99 |
||
100 |
def test0(self): |
|
101 |
"Visual test for correct glyph widths" |
|
102 |
makeTestDoc(fontNamesToTest) |
|
103 |
||
104 |
||
105 |
def makeSuite(): |
|
106 |
return makeSuiteForClasses(PDFMetricsTestCase) |
|
107 |
||
108 |
||
109 |
#noruntests |
|
110 |
if __name__=='__main__': |
|
111 |
usage = """Usage: |
|
112 |
(1) test_pdfbase_pdfmetrics.py - makes doc for all standard fonts |
|
113 |
(2) test_pdfbase_pdfmetrics.py fontname - " " for just one font.""" |
|
114 |
import sys |
|
115 |
verbose = 1 |
|
116 |
# accept font names as arguments; otherwise it does the lot |
|
117 |
if len(sys.argv) > 1: |
|
118 |
for arg in sys.argv[1:]: |
|
119 |
if not arg in fontNamesToTest: |
|
3721 | 120 |
print('unknown font %s' % arg) |
121 |
print(usage) |
|
2963 | 122 |
sys.exit(0) |
123 |
||
124 |
fontNamesToTest = sys.argv[1:] |
|
125 |
||
126 |
unittest.TextTestRunner().run(makeSuite()) |
|
127 |
printLocation() |