@@ -848,6 +848,11 @@ def endStream(self):
848
848
self .currentstream .end ()
849
849
self .currentstream = None
850
850
851
+ def outputStream (self , ref , data , * , extra = None ):
852
+ self .beginStream (ref .id , None , extra )
853
+ self .currentstream .write (data )
854
+ self .endStream ()
855
+
851
856
def _write_annotations (self ):
852
857
for annotsObject , annotations in self ._annotations :
853
858
self .writeObject (annotsObject , annotations )
@@ -1052,13 +1057,10 @@ def createType1Descriptor(self, t1font, fontfile):
1052
1057
1053
1058
self .writeObject (fontdescObject , descriptor )
1054
1059
1055
- self .beginStream (fontfileObject .id , None ,
1056
- {'Length1' : len (t1font .parts [0 ]),
1057
- 'Length2' : len (t1font .parts [1 ]),
1058
- 'Length3' : 0 })
1059
- self .currentstream .write (t1font .parts [0 ])
1060
- self .currentstream .write (t1font .parts [1 ])
1061
- self .endStream ()
1060
+ self .outputStream (fontfileObject , b"" .join (t1font .parts [:2 ]),
1061
+ extra = {'Length1' : len (t1font .parts [0 ]),
1062
+ 'Length2' : len (t1font .parts [1 ]),
1063
+ 'Length3' : 0 })
1062
1064
1063
1065
return fontdescObject
1064
1066
@@ -1181,13 +1183,13 @@ def get_char_width(charcode):
1181
1183
charprocs = {}
1182
1184
for charname in sorted (rawcharprocs ):
1183
1185
stream = rawcharprocs [charname ]
1184
- charprocDict = {'Length' : len ( stream ) }
1186
+ charprocDict = {}
1185
1187
# The 2-byte characters are used as XObjects, so they
1186
1188
# need extra info in their dictionary
1187
1189
if charname in multi_byte_chars :
1188
- charprocDict [ 'Type' ] = Name ('XObject' )
1189
- charprocDict [ 'Subtype' ] = Name ('Form' )
1190
- charprocDict [ 'BBox' ] = bbox
1190
+ charprocDict = { 'Type' : Name ('XObject' ),
1191
+ 'Subtype' : Name ('Form' ),
1192
+ 'BBox' : bbox }
1191
1193
# Each glyph includes bounding box information,
1192
1194
# but xpdf and ghostscript can't handle it in a
1193
1195
# Form XObject (they segfault!!!), so we remove it
@@ -1196,9 +1198,7 @@ def get_char_width(charcode):
1196
1198
# value.
1197
1199
stream = stream [stream .find (b"d1" ) + 2 :]
1198
1200
charprocObject = self .reserveObject ('charProc' )
1199
- self .beginStream (charprocObject .id , None , charprocDict )
1200
- self .currentstream .write (stream )
1201
- self .endStream ()
1201
+ self .outputStream (charprocObject , stream , extra = charprocDict )
1202
1202
1203
1203
# Send the glyphs with ccode > 255 to the XObject dictionary,
1204
1204
# and the others to the font itself
@@ -1266,12 +1266,9 @@ def embedTTFType42(font, characters, descriptor):
1266
1266
1267
1267
# Make fontfile stream
1268
1268
descriptor ['FontFile2' ] = fontfileObject
1269
- self .beginStream (
1270
- fontfileObject .id ,
1271
- self .reserveObject ('length of font stream' ),
1272
- {'Length1' : fontdata .getbuffer ().nbytes })
1273
- self .currentstream .write (fontdata .getvalue ())
1274
- self .endStream ()
1269
+ self .outputStream (
1270
+ fontfileObject , fontdata .getvalue (),
1271
+ extra = {'Length1' : fontdata .getbuffer ().nbytes })
1275
1272
1276
1273
# Make the 'W' (Widths) array, CidToGidMap and ToUnicode CMap
1277
1274
# at the same time
@@ -1330,10 +1327,9 @@ def embedTTFType42(font, characters, descriptor):
1330
1327
rawcharprocs = _get_pdf_charprocs (filename , glyph_ids )
1331
1328
for charname in sorted (rawcharprocs ):
1332
1329
stream = rawcharprocs [charname ]
1333
- charprocDict = {'Length' : len (stream )}
1334
- charprocDict ['Type' ] = Name ('XObject' )
1335
- charprocDict ['Subtype' ] = Name ('Form' )
1336
- charprocDict ['BBox' ] = bbox
1330
+ charprocDict = {'Type' : Name ('XObject' ),
1331
+ 'Subtype' : Name ('Form' ),
1332
+ 'BBox' : bbox }
1337
1333
# Each glyph includes bounding box information,
1338
1334
# but xpdf and ghostscript can't handle it in a
1339
1335
# Form XObject (they segfault!!!), so we remove it
@@ -1342,27 +1338,17 @@ def embedTTFType42(font, characters, descriptor):
1342
1338
# value.
1343
1339
stream = stream [stream .find (b"d1" ) + 2 :]
1344
1340
charprocObject = self .reserveObject ('charProc' )
1345
- self .beginStream (charprocObject .id , None , charprocDict )
1346
- self .currentstream .write (stream )
1347
- self .endStream ()
1341
+ self .outputStream (charprocObject , stream , extra = charprocDict )
1348
1342
1349
1343
name = self ._get_xobject_glyph_name (filename , charname )
1350
1344
self .multi_byte_charprocs [name ] = charprocObject
1351
1345
1352
1346
# CIDToGIDMap stream
1353
1347
cid_to_gid_map = "" .join (cid_to_gid_map ).encode ("utf-16be" )
1354
- self .beginStream (cidToGidMapObject .id ,
1355
- None ,
1356
- {'Length' : len (cid_to_gid_map )})
1357
- self .currentstream .write (cid_to_gid_map )
1358
- self .endStream ()
1348
+ self .outputStream (cidToGidMapObject , cid_to_gid_map )
1359
1349
1360
1350
# ToUnicode CMap
1361
- self .beginStream (toUnicodeMapObject .id ,
1362
- None ,
1363
- {'Length' : unicode_cmap })
1364
- self .currentstream .write (unicode_cmap )
1365
- self .endStream ()
1351
+ self .outputStream (toUnicodeMapObject , unicode_cmap )
1366
1352
1367
1353
descriptor ['MaxWidth' ] = max_width
1368
1354
0 commit comments