File: test_pdfbase_pdfmetrics.py

package info (click to toggle)
python-reportlab 3.1.8-3%2Bdeb8u1
  • links: PTS
  • area: main
  • in suites: jessie
  • size: 6,996 kB
  • ctags: 9,483
  • sloc: python: 69,727; ansic: 19,108; xml: 1,494; makefile: 416; java: 193; sh: 100
file content (127 lines) | stat: -rw-r--r-- 4,269 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#Copyright ReportLab Europe Ltd. 2000-2012
#see license.txt for license details
#test_pdfbase_pdfmetrics_widths
"""
Various tests for PDF metrics.

The main test prints out a PDF documents enabling checking of widths of every
glyph in every standard font.  Long!
"""
__version__='''$Id$'''
from reportlab.lib.testutils import setOutDir,makeSuiteForClasses, outputfile, printLocation
setOutDir(__name__)
import unittest
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase import _fontdata
from reportlab.pdfgen.canvas import Canvas
from reportlab.lib import colors
from reportlab.lib.utils import isPy3

verbose = 0
fontNamesToTest = _fontdata.standardFonts #[0:12]  #leaves out Symbol and Dingbats for now


def decoratePage(c, header):
    c.setFont('Helvetica-Oblique',10)
    c.drawString(72, 800, header)
    c.drawCentredString(297, 54, 'Page %d' % c.getPageNumber())


def makeWidthTestForAllGlyphs(canv, fontName, outlining=1):
    """New page, then runs down doing all the glyphs in one encoding"""
    thisFont = pdfmetrics.getFont(fontName)
    encName = thisFont.encName
    canv.setFont('Helvetica-Bold', 12)
    title = 'Glyph Metrics Test for font %s, ascent=%s, descent=%s, encoding=%s' % (fontName, str(thisFont.face.ascent), str(thisFont.face.descent), encName)
    canv.drawString(80, 750,  title)
    canv.setFont('Helvetica-Oblique',10)
    canv.drawCentredString(297, 54, 'Page %d' % canv.getPageNumber())

    if outlining:
        # put it in the outline
        canv.bookmarkPage('GlyphWidths:' + fontName)
        canv.addOutlineEntry(fontName,'GlyphWidths:' + fontName, level=1)

    y = 720
    widths = thisFont.widths
    glyphNames = thisFont.encoding.vector
    # need to get the right list of names for the font in question
    for i in range(256):
        if y < 72:
            canv.showPage()
            decoratePage(canv, title)
            y = 750
        glyphName = glyphNames[i]
        if glyphName is not None:
            canv.setFont('Helvetica', 10)
            if isPy3:
                text = bytes([i]).decode(encName)*30
            else:
                text = chr(i).decode(encName).encode('utf8')*30

            try:
                w = canv.stringWidth(text, fontName, 10)
                canv.drawString(80, y, '%03d   %s w=%3d' % (i, glyphName, int((w/3.)*10)))
                canv.setFont(fontName, 10)
                canv.drawString(200, y, text)

                # now work out width and put a red marker next to the end.
                canv.setFillColor(colors.red)
                canv.rect(200 + w, y-1, 1, 10, stroke=0, fill=1)
                canv.setFillColor(colors.black)
            except KeyError:
                canv.drawString(200, y, 'Could not find glyph named "%s"' % glyphName)
            y = y - 12


def makeTestDoc(fontNames):
    filename = outputfile('test_pdfbase_pdfmetrics.pdf')
    c = Canvas(filename)
    c.bookmarkPage('Glyph Width Tests')
    c.showOutline()
    c.addOutlineEntry('Glyph Width Tests', 'Glyph Width Tests', level=0)
    if verbose:
        print()   # get it on a different line to the unittest log output.
    for fontName in fontNames:
        if verbose:
            print('width test for', fontName)

        makeWidthTestForAllGlyphs(c, fontName)
        c.showPage()
    c.save()
    if verbose:
        if verbose:
            print('saved',filename)


class PDFMetricsTestCase(unittest.TestCase):
    "Test various encodings used in PDF files."

    def test0(self):
        "Visual test for correct glyph widths"
        makeTestDoc(fontNamesToTest)


def makeSuite():
    return makeSuiteForClasses(PDFMetricsTestCase)


#noruntests
if __name__=='__main__':
    usage = """Usage:
    (1) test_pdfbase_pdfmetrics.py     -  makes doc for all standard fonts
    (2) test_pdfbase_pdfmetrics.py fontname - " " for just one font."""
    import sys
    verbose = 1
    # accept font names as arguments; otherwise it does the lot
    if len(sys.argv) > 1:
        for arg in sys.argv[1:]:
            if not arg in fontNamesToTest:
                print('unknown font %s' % arg)
                print(usage)
                sys.exit(0)

        fontNamesToTest = sys.argv[1:]

    unittest.TextTestRunner().run(makeSuite())
    printLocation()