test_pdfbase_pdfmetrics.py :  » PDF » ReportLab » ReportLab_2_4 » tests » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » PDF » ReportLab 
ReportLab » ReportLab_2_4 » tests » test_pdfbase_pdfmetrics.py
#Copyright ReportLab Europe Ltd. 2000-2004
#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: test_pdfbase_pdfmetrics.py 3288 2008-09-15 11:03:17Z rgbecker $'''
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

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)
            text = unicode(chr(i),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, 5, 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()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.