PDF stream object. This has no pdfRepr method. Instead, call begin(), then output the contents of the stream by calling write(), and finally call end().
| 504 | |
| 505 | |
| 506 | class Stream: |
| 507 | """ |
| 508 | PDF stream object. |
| 509 | |
| 510 | This has no pdfRepr method. Instead, call begin(), then output the |
| 511 | contents of the stream by calling write(), and finally call end(). |
| 512 | """ |
| 513 | __slots__ = ('id', 'len', 'pdfFile', 'file', 'compressobj', 'extra', 'pos') |
| 514 | |
| 515 | def __init__(self, id, len, file, extra=None, png=None): |
| 516 | """ |
| 517 | Parameters |
| 518 | ---------- |
| 519 | id : int |
| 520 | Object id of the stream. |
| 521 | len : Reference or None |
| 522 | An unused Reference object for the length of the stream; |
| 523 | None means to use a memory buffer so the length can be inlined. |
| 524 | file : PdfFile |
| 525 | The underlying object to write the stream to. |
| 526 | extra : dict from Name to anything, or None |
| 527 | Extra key-value pairs to include in the stream header. |
| 528 | png : dict or None |
| 529 | If the data is already png encoded, the decode parameters. |
| 530 | """ |
| 531 | self.id = id # object id |
| 532 | self.len = len # id of length object |
| 533 | self.pdfFile = file |
| 534 | self.file = file.fh # file to which the stream is written |
| 535 | self.compressobj = None # compression object |
| 536 | if extra is None: |
| 537 | self.extra = dict() |
| 538 | else: |
| 539 | self.extra = extra.copy() |
| 540 | if png is not None: |
| 541 | self.extra.update({'Filter': Name('FlateDecode'), |
| 542 | 'DecodeParms': png}) |
| 543 | |
| 544 | self.pdfFile.recordXref(self.id) |
| 545 | if mpl.rcParams['pdf.compression'] and not png: |
| 546 | self.compressobj = zlib.compressobj( |
| 547 | mpl.rcParams['pdf.compression']) |
| 548 | if self.len is None: |
| 549 | self.file = BytesIO() |
| 550 | else: |
| 551 | self._writeHeader() |
| 552 | self.pos = self.file.tell() |
| 553 | |
| 554 | def _writeHeader(self): |
| 555 | write = self.file.write |
| 556 | write(b"%d 0 obj\n" % self.id) |
| 557 | dict = self.extra |
| 558 | dict['Length'] = self.len |
| 559 | if mpl.rcParams['pdf.compression']: |
| 560 | dict['Filter'] = Name('FlateDecode') |
| 561 | |
| 562 | write(pdfRepr(dict)) |
| 563 | write(b"\nstream\n") |
no outgoing calls
no test coverage detected
searching dependent graphs…