author | rptlab |
Tue, 30 Apr 2013 14:20:22 +0100 | |
branch | py33 |
changeset 3721 | 0c93dd8ff567 |
parent 3457 | 47d2296b4a9f |
child 3794 | 398ea04239b5 |
permissions | -rw-r--r-- |
2963 | 1 |
|
2 |
"""Test TrueType font subsetting & embedding code. |
|
3 |
||
2977
beca8d75f400
reportlab: remove LetErrorRobot-Chrome, rina & luxi. Add DarkGardenMK and fix tests
rgbecker
parents:
2966
diff
changeset
|
4 |
This test uses a sample font (Vera.ttf) taken from Bitstream which is called Vera |
beca8d75f400
reportlab: remove LetErrorRobot-Chrome, rina & luxi. Add DarkGardenMK and fix tests
rgbecker
parents:
2966
diff
changeset
|
5 |
Serif Regular and is covered under the license in ../fonts/bitstream-vera-license.txt. |
2963 | 6 |
""" |
2984 | 7 |
from reportlab.lib.testutils import setOutDir,makeSuiteForClasses, outputfile, printLocation, NearTestCase |
8 |
setOutDir(__name__) |
|
2963 | 9 |
import string |
3721 | 10 |
from io import StringIO |
2966 | 11 |
import unittest |
2963 | 12 |
from reportlab.pdfgen.canvas import Canvas |
13 |
from reportlab.pdfbase import pdfmetrics |
|
14 |
from reportlab.pdfbase.pdfdoc import PDFDocument, PDFError |
|
15 |
from reportlab.pdfbase.ttfonts import TTFont, TTFontFace, TTFontFile, TTFOpenFile, \ |
|
16 |
TTFontParser, TTFontMaker, TTFError, \ |
|
17 |
parse_utf8, makeToUnicodeCMap, \ |
|
18 |
FF_SYMBOLIC, FF_NONSYMBOLIC, \ |
|
19 |
calcChecksum, add32 |
|
3457
47d2296b4a9f
text_pddfbase_ttfonts.py: fix against ttfAsciiReadable=0
rgbecker
parents:
3326
diff
changeset
|
20 |
from reportlab import rl_config |
2963 | 21 |
|
22 |
def utf8(code): |
|
23 |
"Convert a given UCS character index into UTF-8" |
|
24 |
if code < 0 or code > 0x7FFFFFFF: |
|
3721 | 25 |
raise ValueError('Invalid UCS character 0x%x' % code) |
2963 | 26 |
elif code < 0x00000080: |
27 |
return chr(code) |
|
28 |
elif code < 0x00000800: |
|
29 |
return '%c%c' % \ |
|
30 |
(0xC0 + (code >> 6), |
|
31 |
0x80 + (code & 0x3F)) |
|
32 |
elif code < 0x00010000: |
|
33 |
return '%c%c%c' % \ |
|
34 |
(0xE0 + (code >> 12), |
|
35 |
0x80 + ((code >> 6) & 0x3F), |
|
36 |
0x80 + (code & 0x3F)) |
|
37 |
elif code < 0x00200000: |
|
38 |
return '%c%c%c%c' % \ |
|
39 |
(0xF0 + (code >> 18), |
|
40 |
0x80 + ((code >> 12) & 0x3F), |
|
41 |
0x80 + ((code >> 6) & 0x3F), |
|
42 |
0x80 + (code & 0x3F)) |
|
43 |
elif code < 0x04000000: |
|
44 |
return '%c%c%c%c%c' % \ |
|
45 |
(0xF8 + (code >> 24), |
|
46 |
0x80 + ((code >> 18) & 0x3F), |
|
47 |
0x80 + ((code >> 12) & 0x3F), |
|
48 |
0x80 + ((code >> 6) & 0x3F), |
|
49 |
0x80 + (code & 0x3F)) |
|
50 |
else: |
|
51 |
return '%c%c%c%c%c%c' % \ |
|
52 |
(0xFC + (code >> 30), |
|
53 |
0x80 + ((code >> 24) & 0x3F), |
|
54 |
0x80 + ((code >> 18) & 0x3F), |
|
55 |
0x80 + ((code >> 12) & 0x3F), |
|
56 |
0x80 + ((code >> 6) & 0x3F), |
|
57 |
0x80 + (code & 0x3F)) |
|
58 |
||
59 |
def _simple_subset_generation(fn,npages,alter=0): |
|
60 |
c = Canvas(outputfile(fn)) |
|
61 |
c.setFont('Helvetica', 30) |
|
62 |
c.drawString(100,700, 'Unicode TrueType Font Test %d pages' % npages) |
|
63 |
# Draw a table of Unicode characters |
|
3721 | 64 |
for p in range(npages): |
2977
beca8d75f400
reportlab: remove LetErrorRobot-Chrome, rina & luxi. Add DarkGardenMK and fix tests
rgbecker
parents:
2966
diff
changeset
|
65 |
for fontName in ('Vera','VeraBI'): |
2963 | 66 |
c.setFont(fontName, 10) |
3721 | 67 |
for i in range(32): |
68 |
for j in range(32): |
|
2963 | 69 |
ch = utf8(i * 32 + j+p*alter) |
3326 | 70 |
c.drawString(80 + j * 13 + int(j / 16.0) * 4, 600 - i * 13 - int(i / 8.0) * 8, ch) |
2963 | 71 |
c.showPage() |
72 |
c.save() |
|
73 |
||
74 |
class TTFontsTestCase(unittest.TestCase): |
|
75 |
"Make documents with TrueType fonts" |
|
76 |
||
77 |
def testTTF(self): |
|
78 |
"Test PDF generation with TrueType fonts" |
|
2977
beca8d75f400
reportlab: remove LetErrorRobot-Chrome, rina & luxi. Add DarkGardenMK and fix tests
rgbecker
parents:
2966
diff
changeset
|
79 |
pdfmetrics.registerFont(TTFont("Vera", "Vera.ttf")) |
beca8d75f400
reportlab: remove LetErrorRobot-Chrome, rina & luxi. Add DarkGardenMK and fix tests
rgbecker
parents:
2966
diff
changeset
|
80 |
pdfmetrics.registerFont(TTFont("VeraBI", "VeraBI.ttf")) |
2963 | 81 |
_simple_subset_generation('test_pdfbase_ttfonts1.pdf',1) |
82 |
_simple_subset_generation('test_pdfbase_ttfonts3.pdf',3) |
|
83 |
_simple_subset_generation('test_pdfbase_ttfonts35.pdf',3,5) |
|
84 |
||
85 |
# Do it twice with the same font object |
|
86 |
c = Canvas(outputfile('test_pdfbase_ttfontsadditional.pdf')) |
|
87 |
# Draw a table of Unicode characters |
|
2977
beca8d75f400
reportlab: remove LetErrorRobot-Chrome, rina & luxi. Add DarkGardenMK and fix tests
rgbecker
parents:
2966
diff
changeset
|
88 |
c.setFont('Vera', 10) |
2963 | 89 |
c.drawString(100, 700, 'Hello, ' + utf8(0xffee)) |
90 |
c.save() |
|
91 |
||
92 |
||
93 |
class TTFontFileTestCase(NearTestCase): |
|
94 |
"Tests TTFontFile, TTFontParser and TTFontMaker classes" |
|
95 |
||
96 |
def testFontFileFailures(self): |
|
97 |
"Tests TTFontFile constructor error checks" |
|
98 |
self.assertRaises(TTFError, TTFontFile, "nonexistent file") |
|
99 |
self.assertRaises(TTFError, TTFontFile, StringIO("")) |
|
100 |
self.assertRaises(TTFError, TTFontFile, StringIO("invalid signature")) |
|
101 |
self.assertRaises(TTFError, TTFontFile, StringIO("OTTO - OpenType not supported yet")) |
|
102 |
self.assertRaises(TTFError, TTFontFile, StringIO("\0\1\0\0")) |
|
103 |
||
104 |
def testFontFileReads(self): |
|
105 |
"Tests TTFontParset.read_xxx" |
|
106 |
||
107 |
class FakeTTFontFile(TTFontParser): |
|
108 |
def __init__(self, data): |
|
109 |
self._ttf_data = data |
|
110 |
self._pos = 0 |
|
111 |
||
112 |
ttf = FakeTTFontFile("\x81\x02\x03\x04" "\x85\x06" "ABCD" "\x7F\xFF" "\x80\x00" "\xFF\xFF") |
|
3721 | 113 |
self.assertEquals(ttf.read_ulong(), 0x81020304) # big-endian |
2963 | 114 |
self.assertEquals(ttf._pos, 4) |
115 |
self.assertEquals(ttf.read_ushort(), 0x8506) |
|
116 |
self.assertEquals(ttf._pos, 6) |
|
117 |
self.assertEquals(ttf.read_tag(), 'ABCD') |
|
118 |
self.assertEquals(ttf._pos, 10) |
|
119 |
self.assertEquals(ttf.read_short(), 0x7FFF) |
|
120 |
self.assertEquals(ttf.read_short(), -0x8000) |
|
121 |
self.assertEquals(ttf.read_short(), -1) |
|
122 |
||
123 |
def testFontFile(self): |
|
124 |
"Tests TTFontFile and TTF parsing code" |
|
2977
beca8d75f400
reportlab: remove LetErrorRobot-Chrome, rina & luxi. Add DarkGardenMK and fix tests
rgbecker
parents:
2966
diff
changeset
|
125 |
ttf = TTFontFile("Vera.ttf") |
beca8d75f400
reportlab: remove LetErrorRobot-Chrome, rina & luxi. Add DarkGardenMK and fix tests
rgbecker
parents:
2966
diff
changeset
|
126 |
self.assertEquals(ttf.name, "BitstreamVeraSans-Roman") |
2963 | 127 |
self.assertEquals(ttf.flags, FF_SYMBOLIC) |
128 |
self.assertEquals(ttf.italicAngle, 0.0) |
|
2977
beca8d75f400
reportlab: remove LetErrorRobot-Chrome, rina & luxi. Add DarkGardenMK and fix tests
rgbecker
parents:
2966
diff
changeset
|
129 |
self.assertNear(ttf.ascent,759.765625) |
beca8d75f400
reportlab: remove LetErrorRobot-Chrome, rina & luxi. Add DarkGardenMK and fix tests
rgbecker
parents:
2966
diff
changeset
|
130 |
self.assertNear(ttf.descent,-240.234375) |
beca8d75f400
reportlab: remove LetErrorRobot-Chrome, rina & luxi. Add DarkGardenMK and fix tests
rgbecker
parents:
2966
diff
changeset
|
131 |
self.assertEquals(ttf.capHeight, 759.765625) |
beca8d75f400
reportlab: remove LetErrorRobot-Chrome, rina & luxi. Add DarkGardenMK and fix tests
rgbecker
parents:
2966
diff
changeset
|
132 |
self.assertNear(ttf.bbox, [-183.10546875, -235.83984375, 1287.109375, 928.22265625]) |
2963 | 133 |
self.assertEquals(ttf.stemV, 87) |
2977
beca8d75f400
reportlab: remove LetErrorRobot-Chrome, rina & luxi. Add DarkGardenMK and fix tests
rgbecker
parents:
2966
diff
changeset
|
134 |
self.assertEquals(ttf.defaultWidth, 600.09765625) |
2963 | 135 |
|
136 |
def testAdd32(self): |
|
137 |
"Test add32" |
|
138 |
self.assertEquals(add32(10, -6), 4) |
|
3721 | 139 |
self.assertEquals(add32(6, -10), -4&0xFFFFFFFF) |
140 |
self.assertEquals(add32(0x80000000, -1), 0x7FFFFFFF) |
|
141 |
self.assertEquals(add32(0x7FFFFFFF, 1), 0x80000000) |
|
2963 | 142 |
|
143 |
def testChecksum(self): |
|
144 |
"Test calcChecksum function" |
|
145 |
self.assertEquals(calcChecksum(""), 0) |
|
146 |
self.assertEquals(calcChecksum("\1"), 0x01000000) |
|
147 |
self.assertEquals(calcChecksum("\x01\x02\x03\x04\x10\x20\x30\x40"), 0x11223344) |
|
3721 | 148 |
self.assertEquals(calcChecksum("\x81"), 0x81000000) |
149 |
self.assertEquals(calcChecksum("\x81\x02"), 0x81020000) |
|
150 |
self.assertEquals(calcChecksum("\x81\x02\x03"), 0x81020300) |
|
151 |
self.assertEquals(calcChecksum("\x81\x02\x03\x04"), 0x81020304) |
|
152 |
self.assertEquals(calcChecksum("\x81\x02\x03\x04\x05"), 0x86020304) |
|
2963 | 153 |
self.assertEquals(calcChecksum("\x41\x02\x03\x04\xD0\x20\x30\x40"), 0x11223344) |
154 |
self.assertEquals(calcChecksum("\xD1\x02\x03\x04\x40\x20\x30\x40"), 0x11223344) |
|
155 |
self.assertEquals(calcChecksum("\x81\x02\x03\x04\x90\x20\x30\x40"), 0x11223344) |
|
3721 | 156 |
self.assertEquals(calcChecksum("\x7F\xFF\xFF\xFF\x00\x00\x00\x01"), 0x80000000) |
2963 | 157 |
|
158 |
def testFontFileChecksum(self): |
|
159 |
"Tests TTFontFile and TTF parsing code" |
|
2977
beca8d75f400
reportlab: remove LetErrorRobot-Chrome, rina & luxi. Add DarkGardenMK and fix tests
rgbecker
parents:
2966
diff
changeset
|
160 |
file = TTFOpenFile("Vera.ttf")[1].read() |
2963 | 161 |
TTFontFile(StringIO(file), validate=1) # should not fail |
162 |
file1 = file[:12345] + "\xFF" + file[12346:] # change one byte |
|
163 |
self.assertRaises(TTFError, TTFontFile, StringIO(file1), validate=1) |
|
164 |
file1 = file[:8] + "\xFF" + file[9:] # change one byte |
|
165 |
self.assertRaises(TTFError, TTFontFile, StringIO(file1), validate=1) |
|
166 |
||
167 |
def testSubsetting(self): |
|
168 |
"Tests TTFontFile and TTF parsing code" |
|
2977
beca8d75f400
reportlab: remove LetErrorRobot-Chrome, rina & luxi. Add DarkGardenMK and fix tests
rgbecker
parents:
2966
diff
changeset
|
169 |
ttf = TTFontFile("Vera.ttf") |
2963 | 170 |
subset = ttf.makeSubset([0x41, 0x42]) |
171 |
subset = TTFontFile(StringIO(subset), 0) |
|
172 |
for tag in ('cmap', 'head', 'hhea', 'hmtx', 'maxp', 'name', 'OS/2', |
|
173 |
'post', 'cvt ', 'fpgm', 'glyf', 'loca', 'prep'): |
|
174 |
self.assert_(subset.get_table(tag)) |
|
175 |
||
176 |
subset.seek_table('loca') |
|
177 |
for n in range(4): |
|
178 |
pos = subset.read_ushort() # this is actually offset / 2 |
|
179 |
self.failIf(pos % 2 != 0, "glyph %d at +%d should be long aligned" % (n, pos * 2)) |
|
180 |
||
2977
beca8d75f400
reportlab: remove LetErrorRobot-Chrome, rina & luxi. Add DarkGardenMK and fix tests
rgbecker
parents:
2966
diff
changeset
|
181 |
self.assertEquals(subset.name, "BitstreamVeraSans-Roman") |
2963 | 182 |
self.assertEquals(subset.flags, FF_SYMBOLIC) |
183 |
self.assertEquals(subset.italicAngle, 0.0) |
|
2977
beca8d75f400
reportlab: remove LetErrorRobot-Chrome, rina & luxi. Add DarkGardenMK and fix tests
rgbecker
parents:
2966
diff
changeset
|
184 |
self.assertNear(subset.ascent,759.765625) |
beca8d75f400
reportlab: remove LetErrorRobot-Chrome, rina & luxi. Add DarkGardenMK and fix tests
rgbecker
parents:
2966
diff
changeset
|
185 |
self.assertNear(subset.descent,-240.234375) |
beca8d75f400
reportlab: remove LetErrorRobot-Chrome, rina & luxi. Add DarkGardenMK and fix tests
rgbecker
parents:
2966
diff
changeset
|
186 |
self.assertEquals(subset.capHeight, 759.765625) |
beca8d75f400
reportlab: remove LetErrorRobot-Chrome, rina & luxi. Add DarkGardenMK and fix tests
rgbecker
parents:
2966
diff
changeset
|
187 |
self.assertNear(subset.bbox, [-183.10546875, -235.83984375, 1287.109375, 928.22265625]) |
2963 | 188 |
self.assertEquals(subset.stemV, 87) |
189 |
||
190 |
def testFontMaker(self): |
|
191 |
"Tests TTFontMaker class" |
|
192 |
ttf = TTFontMaker() |
|
193 |
ttf.add("ABCD", "xyzzy") |
|
194 |
ttf.add("QUUX", "123") |
|
195 |
ttf.add("head", "12345678xxxx") |
|
196 |
stm = ttf.makeStream() |
|
197 |
ttf = TTFontParser(StringIO(stm), 0) |
|
198 |
self.assertEquals(ttf.get_table("ABCD"), "xyzzy") |
|
199 |
self.assertEquals(ttf.get_table("QUUX"), "123") |
|
200 |
||
201 |
||
202 |
class TTFontFaceTestCase(unittest.TestCase): |
|
203 |
"Tests TTFontFace class" |
|
204 |
||
205 |
def testAddSubsetObjects(self): |
|
206 |
"Tests TTFontFace.addSubsetObjects" |
|
2977
beca8d75f400
reportlab: remove LetErrorRobot-Chrome, rina & luxi. Add DarkGardenMK and fix tests
rgbecker
parents:
2966
diff
changeset
|
207 |
face = TTFontFace("Vera.ttf") |
2963 | 208 |
doc = PDFDocument() |
209 |
fontDescriptor = face.addSubsetObjects(doc, "TestFont", [ 0x78, 0x2017 ]) |
|
210 |
fontDescriptor = doc.idToObject[fontDescriptor.name].dict |
|
211 |
self.assertEquals(fontDescriptor['Type'], '/FontDescriptor') |
|
212 |
self.assertEquals(fontDescriptor['Ascent'], face.ascent) |
|
213 |
self.assertEquals(fontDescriptor['CapHeight'], face.capHeight) |
|
214 |
self.assertEquals(fontDescriptor['Descent'], face.descent) |
|
215 |
self.assertEquals(fontDescriptor['Flags'], (face.flags & ~FF_NONSYMBOLIC) | FF_SYMBOLIC) |
|
216 |
self.assertEquals(fontDescriptor['FontName'], "/TestFont") |
|
217 |
self.assertEquals(fontDescriptor['FontBBox'].sequence, face.bbox) |
|
218 |
self.assertEquals(fontDescriptor['ItalicAngle'], face.italicAngle) |
|
219 |
self.assertEquals(fontDescriptor['StemV'], face.stemV) |
|
220 |
fontFile = fontDescriptor['FontFile2'] |
|
221 |
fontFile = doc.idToObject[fontFile.name] |
|
222 |
self.assert_(fontFile.content != "") |
|
223 |
||
224 |
||
225 |
class TTFontTestCase(NearTestCase): |
|
226 |
"Tests TTFont class" |
|
227 |
||
228 |
def testParseUTF8(self): |
|
229 |
"Tests parse_utf8" |
|
230 |
self.assertEquals(parse_utf8(""), []) |
|
231 |
for i in range(0, 0x80): |
|
232 |
self.assertEquals(parse_utf8(chr(i)), [i]) |
|
233 |
for i in range(0x80, 0xA0): |
|
234 |
self.assertRaises(ValueError, parse_utf8, chr(i)) |
|
235 |
self.assertEquals(parse_utf8("abc"), [0x61, 0x62, 0x63]) |
|
236 |
self.assertEquals(parse_utf8("\xC2\xA9x"), [0xA9, 0x78]) |
|
237 |
self.assertEquals(parse_utf8("\xE2\x89\xA0x"), [0x2260, 0x78]) |
|
238 |
self.assertRaises(ValueError, parse_utf8, "\xE2\x89x") |
|
239 |
# for i in range(0, 0xFFFF): - overkill |
|
3721 | 240 |
for i in list(range(0x80, 0x200)) + list(range(0x300, 0x400)) + [0xFFFE, 0xFFFF]: |
2963 | 241 |
self.assertEquals(parse_utf8(utf8(i)), [i]) |
242 |
||
243 |
def testStringWidth(self): |
|
244 |
"Test TTFont.stringWidth" |
|
2977
beca8d75f400
reportlab: remove LetErrorRobot-Chrome, rina & luxi. Add DarkGardenMK and fix tests
rgbecker
parents:
2966
diff
changeset
|
245 |
font = TTFont("Vera", "Vera.ttf") |
2963 | 246 |
self.assert_(font.stringWidth("test", 10) > 0) |
247 |
width = font.stringWidth(utf8(0x2260) * 2, 1000) |
|
248 |
expected = font.face.getCharWidth(0x2260) * 2 |
|
249 |
self.assertNear(width,expected) |
|
250 |
||
251 |
def testSplitString(self): |
|
252 |
"Tests TTFont.splitString" |
|
253 |
doc = PDFDocument() |
|
2977
beca8d75f400
reportlab: remove LetErrorRobot-Chrome, rina & luxi. Add DarkGardenMK and fix tests
rgbecker
parents:
2966
diff
changeset
|
254 |
font = TTFont("Vera", "Vera.ttf") |
3721 | 255 |
text = string.join(list(map(utf8, range(0, 511))), "") |
256 |
allchars = string.join(list(map(chr, range(0, 256))), "") |
|
2963 | 257 |
nospace = allchars[:32] + allchars[33:] |
258 |
chunks = [(0, allchars), (1, nospace)] |
|
259 |
self.assertEquals(font.splitString(text, doc), chunks) |
|
260 |
# Do it twice |
|
261 |
self.assertEquals(font.splitString(text, doc), chunks) |
|
262 |
||
3721 | 263 |
text = string.join(list(map(utf8, list(range(510, -1, -1)))), "") |
264 |
allchars = string.join(list(map(chr, list(range(255, -1, -1)))), "") |
|
2963 | 265 |
nospace = allchars[:223] + allchars[224:] |
266 |
chunks = [(1, nospace), (0, allchars)] |
|
267 |
self.assertEquals(font.splitString(text, doc), chunks) |
|
268 |
||
269 |
def testSplitStringSpaces(self): |
|
270 |
# In order for justification (word spacing) to work, the space |
|
271 |
# 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. |
|
273 |
||
274 |
doc = PDFDocument() |
|
2977
beca8d75f400
reportlab: remove LetErrorRobot-Chrome, rina & luxi. Add DarkGardenMK and fix tests
rgbecker
parents:
2966
diff
changeset
|
275 |
font = TTFont("Vera", "Vera.ttf") |
3721 | 276 |
text = string.join(list(map(utf8, list(range(512, -1, -1)))), "") |
2963 | 277 |
chunks = font.splitString(text, doc) |
278 |
state = font.state[doc] |
|
279 |
self.assertEquals(state.assignments[32], 32) |
|
280 |
self.assertEquals(state.subsets[0][32], 32) |
|
281 |
self.assertEquals(state.subsets[1][32], 32) |
|
282 |
||
283 |
def testSubsetInternalName(self): |
|
284 |
"Tests TTFont.getSubsetInternalName" |
|
285 |
doc = PDFDocument() |
|
2977
beca8d75f400
reportlab: remove LetErrorRobot-Chrome, rina & luxi. Add DarkGardenMK and fix tests
rgbecker
parents:
2966
diff
changeset
|
286 |
font = TTFont("Vera", "Vera.ttf") |
2963 | 287 |
# Actually generate some subsets |
3721 | 288 |
text = string.join(list(map(utf8, list(range(0, 513)))), "") |
2963 | 289 |
font.splitString(text, doc) |
290 |
self.assertRaises(IndexError, font.getSubsetInternalName, -1, doc) |
|
291 |
self.assertRaises(IndexError, font.getSubsetInternalName, 3, doc) |
|
292 |
self.assertEquals(font.getSubsetInternalName(0, doc), "/F1+0") |
|
293 |
self.assertEquals(font.getSubsetInternalName(1, doc), "/F1+1") |
|
294 |
self.assertEquals(font.getSubsetInternalName(2, doc), "/F1+2") |
|
295 |
self.assertEquals(doc.delayedFonts, [font]) |
|
296 |
||
297 |
def testAddObjectsEmpty(self): |
|
298 |
"TTFont.addObjects should not fail when no characters were used" |
|
2977
beca8d75f400
reportlab: remove LetErrorRobot-Chrome, rina & luxi. Add DarkGardenMK and fix tests
rgbecker
parents:
2966
diff
changeset
|
299 |
font = TTFont("Vera", "Vera.ttf") |
2963 | 300 |
doc = PDFDocument() |
301 |
font.addObjects(doc) |
|
302 |
||
303 |
def no_longer_testAddObjectsResets(self): |
|
304 |
"Test that TTFont.addObjects resets the font" |
|
305 |
# Actually generate some subsets |
|
306 |
doc = PDFDocument() |
|
2977
beca8d75f400
reportlab: remove LetErrorRobot-Chrome, rina & luxi. Add DarkGardenMK and fix tests
rgbecker
parents:
2966
diff
changeset
|
307 |
font = TTFont("Vera", "Vera.ttf") |
2963 | 308 |
font.splitString('a', doc) # create some subset |
309 |
doc = PDFDocument() |
|
310 |
font.addObjects(doc) |
|
311 |
self.assertEquals(font.frozen, 0) |
|
312 |
self.assertEquals(font.nextCode, 0) |
|
313 |
self.assertEquals(font.subsets, []) |
|
314 |
self.assertEquals(font.assignments, {}) |
|
315 |
font.splitString('ba', doc) # should work |
|
316 |
||
317 |
def testParallelConstruction(self): |
|
318 |
"Test that TTFont can be used for different documents at the same time" |
|
3457
47d2296b4a9f
text_pddfbase_ttfonts.py: fix against ttfAsciiReadable=0
rgbecker
parents:
3326
diff
changeset
|
319 |
ttfAsciiReadable = rl_config.ttfAsciiReadable |
47d2296b4a9f
text_pddfbase_ttfonts.py: fix against ttfAsciiReadable=0
rgbecker
parents:
3326
diff
changeset
|
320 |
try: |
47d2296b4a9f
text_pddfbase_ttfonts.py: fix against ttfAsciiReadable=0
rgbecker
parents:
3326
diff
changeset
|
321 |
rl_config.ttfAsciiReadable = 1 |
47d2296b4a9f
text_pddfbase_ttfonts.py: fix against ttfAsciiReadable=0
rgbecker
parents:
3326
diff
changeset
|
322 |
doc1 = PDFDocument() |
47d2296b4a9f
text_pddfbase_ttfonts.py: fix against ttfAsciiReadable=0
rgbecker
parents:
3326
diff
changeset
|
323 |
doc2 = PDFDocument() |
47d2296b4a9f
text_pddfbase_ttfonts.py: fix against ttfAsciiReadable=0
rgbecker
parents:
3326
diff
changeset
|
324 |
font = TTFont("Vera", "Vera.ttf") |
3721 | 325 |
self.assertEquals(font.splitString('hello ', doc1), [(0, 'hello ')]) |
326 |
self.assertEquals(font.splitString('hello ', doc2), [(0, 'hello ')]) |
|
327 |
self.assertEquals(font.splitString('\u0410\u0411'.encode('UTF-8'), doc1), [(0, '\x80\x81')]) |
|
328 |
self.assertEquals(font.splitString('\u0412'.encode('UTF-8'), doc2), [(0, '\x80')]) |
|
3457
47d2296b4a9f
text_pddfbase_ttfonts.py: fix against ttfAsciiReadable=0
rgbecker
parents:
3326
diff
changeset
|
329 |
font.addObjects(doc1) |
3721 | 330 |
self.assertEquals(font.splitString('\u0413'.encode('UTF-8'), doc2), [(0, '\x81')]) |
3457
47d2296b4a9f
text_pddfbase_ttfonts.py: fix against ttfAsciiReadable=0
rgbecker
parents:
3326
diff
changeset
|
331 |
font.addObjects(doc2) |
47d2296b4a9f
text_pddfbase_ttfonts.py: fix against ttfAsciiReadable=0
rgbecker
parents:
3326
diff
changeset
|
332 |
finally: |
47d2296b4a9f
text_pddfbase_ttfonts.py: fix against ttfAsciiReadable=0
rgbecker
parents:
3326
diff
changeset
|
333 |
rl_config.ttfAsciiReadable = ttfAsciiReadable |
2963 | 334 |
|
335 |
def testAddObjects(self): |
|
336 |
"Test TTFont.addObjects" |
|
337 |
# Actually generate some subsets |
|
3457
47d2296b4a9f
text_pddfbase_ttfonts.py: fix against ttfAsciiReadable=0
rgbecker
parents:
3326
diff
changeset
|
338 |
ttfAsciiReadable = rl_config.ttfAsciiReadable |
47d2296b4a9f
text_pddfbase_ttfonts.py: fix against ttfAsciiReadable=0
rgbecker
parents:
3326
diff
changeset
|
339 |
try: |
47d2296b4a9f
text_pddfbase_ttfonts.py: fix against ttfAsciiReadable=0
rgbecker
parents:
3326
diff
changeset
|
340 |
rl_config.ttfAsciiReadable = 1 |
47d2296b4a9f
text_pddfbase_ttfonts.py: fix against ttfAsciiReadable=0
rgbecker
parents:
3326
diff
changeset
|
341 |
doc = PDFDocument() |
47d2296b4a9f
text_pddfbase_ttfonts.py: fix against ttfAsciiReadable=0
rgbecker
parents:
3326
diff
changeset
|
342 |
font = TTFont("Vera", "Vera.ttf") |
47d2296b4a9f
text_pddfbase_ttfonts.py: fix against ttfAsciiReadable=0
rgbecker
parents:
3326
diff
changeset
|
343 |
font.splitString('a', doc) # create some subset |
47d2296b4a9f
text_pddfbase_ttfonts.py: fix against ttfAsciiReadable=0
rgbecker
parents:
3326
diff
changeset
|
344 |
internalName = font.getSubsetInternalName(0, doc)[1:] |
47d2296b4a9f
text_pddfbase_ttfonts.py: fix against ttfAsciiReadable=0
rgbecker
parents:
3326
diff
changeset
|
345 |
font.addObjects(doc) |
47d2296b4a9f
text_pddfbase_ttfonts.py: fix against ttfAsciiReadable=0
rgbecker
parents:
3326
diff
changeset
|
346 |
pdfFont = doc.idToObject[internalName] |
47d2296b4a9f
text_pddfbase_ttfonts.py: fix against ttfAsciiReadable=0
rgbecker
parents:
3326
diff
changeset
|
347 |
self.assertEquals(doc.idToObject['BasicFonts'].dict[internalName], pdfFont) |
47d2296b4a9f
text_pddfbase_ttfonts.py: fix against ttfAsciiReadable=0
rgbecker
parents:
3326
diff
changeset
|
348 |
self.assertEquals(pdfFont.Name, internalName) |
47d2296b4a9f
text_pddfbase_ttfonts.py: fix against ttfAsciiReadable=0
rgbecker
parents:
3326
diff
changeset
|
349 |
self.assertEquals(pdfFont.BaseFont, "AAAAAA+BitstreamVeraSans-Roman") |
47d2296b4a9f
text_pddfbase_ttfonts.py: fix against ttfAsciiReadable=0
rgbecker
parents:
3326
diff
changeset
|
350 |
self.assertEquals(pdfFont.FirstChar, 0) |
47d2296b4a9f
text_pddfbase_ttfonts.py: fix against ttfAsciiReadable=0
rgbecker
parents:
3326
diff
changeset
|
351 |
self.assertEquals(pdfFont.LastChar, 127) |
47d2296b4a9f
text_pddfbase_ttfonts.py: fix against ttfAsciiReadable=0
rgbecker
parents:
3326
diff
changeset
|
352 |
self.assertEquals(len(pdfFont.Widths.sequence), 128) |
47d2296b4a9f
text_pddfbase_ttfonts.py: fix against ttfAsciiReadable=0
rgbecker
parents:
3326
diff
changeset
|
353 |
toUnicode = doc.idToObject[pdfFont.ToUnicode.name] |
47d2296b4a9f
text_pddfbase_ttfonts.py: fix against ttfAsciiReadable=0
rgbecker
parents:
3326
diff
changeset
|
354 |
self.assert_(toUnicode.content != "") |
47d2296b4a9f
text_pddfbase_ttfonts.py: fix against ttfAsciiReadable=0
rgbecker
parents:
3326
diff
changeset
|
355 |
fontDescriptor = doc.idToObject[pdfFont.FontDescriptor.name] |
47d2296b4a9f
text_pddfbase_ttfonts.py: fix against ttfAsciiReadable=0
rgbecker
parents:
3326
diff
changeset
|
356 |
self.assertEquals(fontDescriptor.dict['Type'], '/FontDescriptor') |
47d2296b4a9f
text_pddfbase_ttfonts.py: fix against ttfAsciiReadable=0
rgbecker
parents:
3326
diff
changeset
|
357 |
finally: |
47d2296b4a9f
text_pddfbase_ttfonts.py: fix against ttfAsciiReadable=0
rgbecker
parents:
3326
diff
changeset
|
358 |
rl_config.ttfAsciiReadable = ttfAsciiReadable |
2963 | 359 |
|
360 |
def testMakeToUnicodeCMap(self): |
|
361 |
"Test makeToUnicodeCMap" |
|
362 |
self.assertEquals(makeToUnicodeCMap("TestFont", [ 0x1234, 0x4321, 0x4242 ]), |
|
363 |
"""/CIDInit /ProcSet findresource begin |
|
364 |
12 dict begin |
|
365 |
begincmap |
|
366 |
/CIDSystemInfo |
|
367 |
<< /Registry (TestFont) |
|
368 |
/Ordering (TestFont) |
|
369 |
/Supplement 0 |
|
370 |
>> def |
|
371 |
/CMapName /TestFont def |
|
372 |
/CMapType 2 def |
|
373 |
1 begincodespacerange |
|
374 |
<00> <02> |
|
375 |
endcodespacerange |
|
376 |
3 beginbfchar |
|
377 |
<00> <1234> |
|
378 |
<01> <4321> |
|
379 |
<02> <4242> |
|
380 |
endbfchar |
|
381 |
endcmap |
|
382 |
CMapName currentdict /CMap defineresource pop |
|
383 |
end |
|
384 |
end""") |
|
385 |
||
386 |
||
387 |
def makeSuite(): |
|
388 |
suite = makeSuiteForClasses( |
|
389 |
TTFontsTestCase, |
|
390 |
TTFontFileTestCase, |
|
391 |
TTFontFaceTestCase, |
|
392 |
TTFontTestCase) |
|
393 |
return suite |
|
394 |
||
395 |
||
396 |
#noruntests |
|
397 |
if __name__ == "__main__": |
|
398 |
unittest.TextTestRunner().run(makeSuite()) |
|
399 |
printLocation() |