Ejemplo n.º 1
0
def set_cyr_font():
    # fname - the font file name without extension
    fname = 'a010013l'

    # faceName - view a010013l.AFM file as a plain text and look at
    # row beginning with 'FontName' word (it's usually the fourth row).
    # The word after 'FontName' is the faceName ('URWGothicL-Book' in this case).
    faceName = 'URWGothicL-Book'

    # Define new Type 1 font
    cyrFace = pdfmetrics.EmbeddedType1Face(fname + '.afm', fname + '.pfb')

    # Create a new encoding called 'CP1251'
    cyrenc = pdfmetrics.Encoding('CP1251')

    # Fill in the tuple with Unicode glyphs in accordance with cp1251 (win1251)
    # encoding
    cp1251 = (
        'afii10051', 'afii10052', 'quotesinglbase', 'afii10100', 'quotedblbase',
        'ellipsis', 'dagger', 'daggerdbl', 'Euro', 'perthousand', 'afii10058',
        'guilsinglleft', 'afii10059', 'afii10061', 'afii10060', 'afii10145',
        'afii10099', 'quoteleft', 'quoteright', 'quotedblleft', 'quotedblright',
        'bullet', 'endash', 'emdash', 'tilde', 'trademark', 'afii10106',
        'guilsinglright', 'afii10107', 'afii10109', 'afii10108', 'afii10193',
        'space', 'afii10062', 'afii10110', 'afii10057', 'currency', 'afii10050',
        'brokenbar', 'section', 'afii10023', 'copyright', 'afii10053',
        'guillemotleft', 'logicalnot', 'hyphen', 'registered', 'afii10056',
        'degree', 'plusminus', 'afii10055', 'afii10103', 'afii10098', 'mu1',
        'paragraph', 'periodcentered', 'afii10071', 'afii61352', 'afii10101',
        'guillemotright', 'afii10105', 'afii10054', 'afii10102', 'afii10104',
        'afii10017', 'afii10018', 'afii10019', 'afii10020', 'afii10021',
        'afii10022', 'afii10024', 'afii10025', 'afii10026', 'afii10027',
        'afii10028', 'afii10029', 'afii10030', 'afii10031', 'afii10032',
        'afii10033', 'afii10034', 'afii10035', 'afii10036', 'afii10037',
        'afii10038', 'afii10039', 'afii10040', 'afii10041', 'afii10042',
        'afii10043', 'afii10044', 'afii10045', 'afii10046', 'afii10047',
        'afii10048', 'afii10049', 'afii10065', 'afii10066', 'afii10067',
        'afii10068', 'afii10069', 'afii10070', 'afii10072', 'afii10073',
        'afii10074', 'afii10075', 'afii10076', 'afii10077', 'afii10078',
        'afii10079', 'afii10080', 'afii10081', 'afii10082', 'afii10083',
        'afii10084', 'afii10085', 'afii10086', 'afii10087', 'afii10088',
        'afii10089', 'afii10090', 'afii10091', 'afii10092', 'afii10093',
        'afii10094', 'afii10095', 'afii10096', 'afii10097'
    )

    # Replace glyphs from code 128 to code 256 with cp1251 values
    for i in range(128, 256):
        cyrenc[i] = cp1251[i - 128]

    # Register newly created encoding
    pdfmetrics.registerEncoding(cyrenc)

    # Register type face
    pdfmetrics.registerTypeFace(cyrFace)

    # Register the font with adding '1251' to its name
    pdfmetrics.registerFont(pdfmetrics.Font(faceName + '1251', faceName, 'CP1251'))

    return faceName + '1251'  # def set_cyr_font
Ejemplo n.º 2
0
 def cyrillic(c, n):
     fname = 'font'
     faceName = 'TimesNewRomanPSMT'
     cyrFace = pdfmetrics.EmbeddedType1Face(fname + '.afm', fname + '.pfb')
     cyrEnc = pdfmetrics.Encoding('CP1251')
     cp1251 = (
         'afii10051', 'afii10052', 'quotesinglbase', 'afii10100',
         'quotedblbase', 'ellipsis', 'dagger', 'daggerdbl', 'Euro',
         'perthousand', 'afii10058', 'guilsinglleft', 'afii10059',
         'afii10061', 'afii10060', 'afii10145', 'afii10099', 'quoteleft',
         'quoteright', 'quotedblleft', 'quotedblright', 'bullet', 'endash',
         'emdash', 'tilde', 'trademark', 'afii10106', 'guilsinglright',
         'afii10107', 'afii10109', 'afii10108', 'afii10193', 'space',
         'afii10062', 'afii10110', 'afii10057', 'currency', 'afii10050',
         'brokenbar', 'section', 'afii10023', 'copyright', 'afii10053',
         'guillemotleft', 'logicalnot', 'hyphen', 'registered', 'afii10056',
         'degree', 'plusminus', 'afii10055', 'afii10103', 'afii10098',
         'mu1', 'paragraph', 'periodcentered', 'afii10071', 'afii61352',
         'afii10101', 'guillemotright', 'afii10105', 'afii10054',
         'afii10102', 'afii10104', 'afii10017', 'afii10018', 'afii10019',
         'afii10020', 'afii10021', 'afii10022', 'afii10024', 'afii10025',
         'afii10026', 'afii10027', 'afii10028', 'afii10029', 'afii10030',
         'afii10031', 'afii10032', 'afii10033', 'afii10034', 'afii10035',
         'afii10036', 'afii10037', 'afii10038', 'afii10039', 'afii10040',
         'afii10041', 'afii10042', 'afii10043', 'afii10044', 'afii10045',
         'afii10046', 'afii10047', 'afii10048', 'afii10049', 'afii10065',
         'afii10066', 'afii10067', 'afii10068', 'afii10069', 'afii10070',
         'afii10072', 'afii10073', 'afii10074', 'afii10075', 'afii10076',
         'afii10077', 'afii10078', 'afii10079', 'afii10080', 'afii10081',
         'afii10082', 'afii10083', 'afii10084', 'afii10085', 'afii10086',
         'afii10087', 'afii10088', 'afii10089', 'afii10090', 'afii10091',
         'afii10092', 'afii10093', 'afii10094', 'afii10095', 'afii10096',
         'afii10097')
     for i in range(128, 256):
         cyrEnc[i] = cp1251[i - 128]
     pdfmetrics.registerEncoding(cyrEnc)
     pdfmetrics.registerTypeFace(cyrFace)
     pdfmetrics.registerFont(
         pdfmetrics.Font(faceName + '1251', faceName, 'CP1251'))
     c.setFont(faceName + '1251', n)
    def test0(self):
        "Make custom encodings of standard fonts"

        # make a custom encoded font.
        c = Canvas(outputfile('test_pdfbase_encodings.pdf'))
        c.setPageCompression(0)
        c.setFont('Helvetica', 12)
        c.drawString(
            100, 700,
            'The text below should be in a custom encoding in which all vowels become "z"'
        )

        # invent a new language where vowels are replaced with letter 'z'
        zenc = pdfmetrics.Encoding('EncodingWithoutVowels', 'WinAnsiEncoding')
        for ch in 'aeiou':
            zenc[ord(ch)] = 'z'
        for ch in 'AEIOU':
            zenc[ord(ch)] = 'Z'
        pdfmetrics.registerEncoding(zenc)

        # now we can make a font based on this encoding
        # AR hack/workaround: the name of the encoding must be a Python codec!
        f = pdfmetrics.Font('FontWithoutVowels', 'Helvetica-Oblique',
                            'EncodingWithoutVowels')
        pdfmetrics.registerFont(f)

        c.setFont('FontWithoutVowels', 12)
        c.drawString(125, 675, "The magic word is squamish ossifrage")

        # now demonstrate adding a Euro to MacRoman, which lacks one
        c.setFont('Helvetica', 12)
        c.drawString(
            100, 650,
            "MacRoman encoding lacks a Euro.  We'll make a Mac font with the Euro at #219:"
        )

        # WinAnsi Helvetica
        pdfmetrics.registerFont(
            pdfmetrics.Font('Helvetica-WinAnsi', 'Helvetica-Oblique',
                            'WinAnsiEncoding'))
        c.setFont('Helvetica-WinAnsi', 12)
        c.drawString(125, 625, 'WinAnsi with Euro: character 128 = "\200"')

        pdfmetrics.registerFont(
            pdfmetrics.Font('MacHelvNoEuro', 'Helvetica-Oblique',
                            'MacRomanEncoding'))
        c.setFont('MacHelvNoEuro', 12)
        c.drawString(125, 600,
                     'Standard MacRoman, no Euro: Character 219 = "\333"'
                     )  # oct(219)=0333

        # now make our hacked encoding
        euroMac = pdfmetrics.Encoding('MacWithEuro', 'MacRomanEncoding')
        euroMac[219] = 'Euro'
        pdfmetrics.registerEncoding(euroMac)

        pdfmetrics.registerFont(
            pdfmetrics.Font('MacHelvWithEuro', 'Helvetica-Oblique',
                            'MacWithEuro'))

        c.setFont('MacHelvWithEuro', 12)
        c.drawString(125, 575,
                     'Hacked MacRoman with Euro: Character 219 = "\333"'
                     )  # oct(219)=0333

        # now test width setting with and without _rl_accel - harder
        # make an encoding where 'm' becomes 'i'
        c.setFont('Helvetica', 12)
        c.drawString(
            100, 500,
            "Recode 'm' to 'i' and check we can measure widths. Boxes should surround letters."
        )
        sample = 'Mmmmm. ' * 6 + 'Mmmm'

        c.setFont('Helvetica-Oblique', 12)
        c.drawString(125, 475, sample)
        w = c.stringWidth(sample, 'Helvetica-Oblique', 12)
        c.rect(125, 475, w, 12)

        narrowEnc = pdfmetrics.Encoding('m-to-i')
        narrowEnc[ord('m')] = 'i'
        narrowEnc[ord('M')] = 'I'
        pdfmetrics.registerEncoding(narrowEnc)

        pdfmetrics.registerFont(
            pdfmetrics.Font('narrow', 'Helvetica-Oblique', 'm-to-i'))
        c.setFont('narrow', 12)
        c.drawString(125, 450, sample)
        w = c.stringWidth(sample, 'narrow', 12)
        c.rect(125, 450, w, 12)

        c.setFont('Helvetica', 12)
        c.drawString(
            100, 400,
            "Symbol & Dingbats fonts - check we still get valid PDF in StandardEncoding"
        )
        c.setFont('Symbol', 12)
        c.drawString(100, 375, 'abcdefghijklmn')
        c.setFont('ZapfDingbats', 12)
        c.drawString(300, 375, 'abcdefghijklmn')

        c.save()
Ejemplo n.º 4
0
# Create a new canvas for cyrillic.pdf file
c = canvas.Canvas('cyrillic.pdf')

# fname - the font file name without extension
fname = 'a010013l'

# faceName - view a010013l.AFM file as a plain text and look at
# row beginning with 'FontName' word (it's usually the fourth row).
# The word after 'FontName' is the faceName ('URWGothicL-Book' in this case).
faceName = 'URWGothicL-Book'

# Define new Type 1 font
cyrFace = pdfmetrics.EmbeddedType1Face(fname + '.afm', fname + '.pfb')

# Create a new encoding called 'CP1251'
cyrenc = pdfmetrics.Encoding('CP1251')

# Fill in the tuple with Unicode glyphs in accordance with cp1251 (win1251)
# encoding
cp1251 = ('afii10051', 'afii10052', 'quotesinglbase', 'afii10100',
          'quotedblbase', 'ellipsis', 'dagger', 'daggerdbl', 'Euro',
          'perthousand', 'afii10058', 'guilsinglleft', 'afii10059',
          'afii10061', 'afii10060', 'afii10145', 'afii10099', 'quoteleft',
          'quoteright', 'quotedblleft', 'quotedblright', 'bullet', 'endash',
          'emdash', 'tilde', 'trademark', 'afii10106', 'guilsinglright',
          'afii10107', 'afii10109', 'afii10108', 'afii10193', 'space',
          'afii10062', 'afii10110', 'afii10057', 'currency', 'afii10050',
          'brokenbar', 'section', 'afii10023', 'copyright', 'afii10053',
          'guillemotleft', 'logicalnot', 'hyphen', 'registered', 'afii10056',
          'degree', 'plusminus', 'afii10055', 'afii10103', 'afii10098', 'mu1',
          'paragraph', 'periodcentered', 'afii10071', 'afii61352', 'afii10101',