tests/test_pdfbase_ttfonts.py
branchpy33
changeset 3794 398ea04239b5
parent 3721 0c93dd8ff567
child 3842 26bd24a3b5a2
equal deleted inserted replaced
3793:cc3f9cc828f7 3794:398ea04239b5
     4 This test uses a sample font (Vera.ttf) taken from Bitstream which is called Vera
     4 This test uses a sample font (Vera.ttf) taken from Bitstream which is called Vera
     5 Serif Regular and is covered under the license in ../fonts/bitstream-vera-license.txt.
     5 Serif Regular and is covered under the license in ../fonts/bitstream-vera-license.txt.
     6 """
     6 """
     7 from reportlab.lib.testutils import setOutDir,makeSuiteForClasses, outputfile, printLocation, NearTestCase
     7 from reportlab.lib.testutils import setOutDir,makeSuiteForClasses, outputfile, printLocation, NearTestCase
     8 setOutDir(__name__)
     8 setOutDir(__name__)
     9 import string
       
    10 from io import StringIO
     9 from io import StringIO
    11 import unittest
    10 import unittest
    12 from reportlab.pdfgen.canvas import Canvas
    11 from reportlab.pdfgen.canvas import Canvas
    13 from reportlab.pdfbase import pdfmetrics
    12 from reportlab.pdfbase import pdfmetrics
    14 from reportlab.pdfbase.pdfdoc import PDFDocument, PDFError
    13 from reportlab.pdfbase.pdfdoc import PDFDocument, PDFError
   250 
   249 
   251     def testSplitString(self):
   250     def testSplitString(self):
   252         "Tests TTFont.splitString"
   251         "Tests TTFont.splitString"
   253         doc = PDFDocument()
   252         doc = PDFDocument()
   254         font = TTFont("Vera", "Vera.ttf")
   253         font = TTFont("Vera", "Vera.ttf")
   255         text = string.join(list(map(utf8, range(0, 511))), "")
   254         text = "".join(list(map(utf8, range(0, 511))))
   256         allchars = string.join(list(map(chr, range(0, 256))), "")
   255         allchars = "".join(list(map(chr, range(0, 256))))
   257         nospace = allchars[:32] + allchars[33:]
   256         nospace = allchars[:32] + allchars[33:]
   258         chunks = [(0, allchars), (1, nospace)]
   257         chunks = [(0, allchars), (1, nospace)]
   259         self.assertEquals(font.splitString(text, doc), chunks)
   258         self.assertEquals(font.splitString(text, doc), chunks)
   260         # Do it twice
   259         # Do it twice
   261         self.assertEquals(font.splitString(text, doc), chunks)
   260         self.assertEquals(font.splitString(text, doc), chunks)
   262 
   261 
   263         text = string.join(list(map(utf8, list(range(510, -1, -1)))), "")
   262         text = "".join(list(map(utf8, list(range(510, -1, -1)))))
   264         allchars = string.join(list(map(chr, list(range(255, -1, -1)))), "")
   263         allchars = "".join(list(map(chr, list(range(255, -1, -1)))))
   265         nospace = allchars[:223] + allchars[224:]
   264         nospace = allchars[:223] + allchars[224:]
   266         chunks = [(1, nospace), (0, allchars)]
   265         chunks = [(1, nospace), (0, allchars)]
   267         self.assertEquals(font.splitString(text, doc), chunks)
   266         self.assertEquals(font.splitString(text, doc), chunks)
   268 
   267 
   269     def testSplitStringSpaces(self):
   268     def testSplitStringSpaces(self):
   271         # glyph must have a code 32, and no other character should have
   270         # glyph must have a code 32, and no other character should have
   272         # that code in any subset, or word spacing will be applied to it.
   271         # that code in any subset, or word spacing will be applied to it.
   273 
   272 
   274         doc = PDFDocument()
   273         doc = PDFDocument()
   275         font = TTFont("Vera", "Vera.ttf")
   274         font = TTFont("Vera", "Vera.ttf")
   276         text = string.join(list(map(utf8, list(range(512, -1, -1)))), "")
   275         text = "".join(list(map(utf8, list(range(512, -1, -1)))))
   277         chunks = font.splitString(text, doc)
   276         chunks = font.splitString(text, doc)
   278         state = font.state[doc]
   277         state = font.state[doc]
   279         self.assertEquals(state.assignments[32], 32)
   278         self.assertEquals(state.assignments[32], 32)
   280         self.assertEquals(state.subsets[0][32], 32)
   279         self.assertEquals(state.subsets[0][32], 32)
   281         self.assertEquals(state.subsets[1][32], 32)
   280         self.assertEquals(state.subsets[1][32], 32)
   283     def testSubsetInternalName(self):
   282     def testSubsetInternalName(self):
   284         "Tests TTFont.getSubsetInternalName"
   283         "Tests TTFont.getSubsetInternalName"
   285         doc = PDFDocument()
   284         doc = PDFDocument()
   286         font = TTFont("Vera", "Vera.ttf")
   285         font = TTFont("Vera", "Vera.ttf")
   287         # Actually generate some subsets
   286         # Actually generate some subsets
   288         text = string.join(list(map(utf8, list(range(0, 513)))), "")
   287         text = "".join(list(map(utf8, list(range(0, 513)))))
   289         font.splitString(text, doc)
   288         font.splitString(text, doc)
   290         self.assertRaises(IndexError, font.getSubsetInternalName, -1, doc)
   289         self.assertRaises(IndexError, font.getSubsetInternalName, -1, doc)
   291         self.assertRaises(IndexError, font.getSubsetInternalName, 3, doc)
   290         self.assertRaises(IndexError, font.getSubsetInternalName, 3, doc)
   292         self.assertEquals(font.getSubsetInternalName(0, doc), "/F1+0")
   291         self.assertEquals(font.getSubsetInternalName(0, doc), "/F1+0")
   293         self.assertEquals(font.getSubsetInternalName(1, doc), "/F1+1")
   292         self.assertEquals(font.getSubsetInternalName(1, doc), "/F1+1")