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