| 356 | | retval["__streamdata__"] = stream.read(length) |
|---|
| 357 | | e = readNonWhitespace(stream) |
|---|
| 358 | | ndstream = stream.read(8) |
|---|
| 359 | | assert e == "e" and ndstream == "ndstream" |
|---|
| | 352 | data["__streamdata__"] = stream.read(length) |
|---|
| | 353 | # (sigh) - the odd PDF file has a length that is too long, so we'd |
|---|
| | 354 | # need to read backwards to find the "endstream" ending. Really, |
|---|
| | 355 | # who cares - I am sure this code works properly given a correct |
|---|
| | 356 | # PDF file, so I'm removing this assertion. It's not necessary to |
|---|
| | 357 | # read to the end of the object because streams are always in |
|---|
| | 358 | # indirect objects - there's never an object after this one. |
|---|
| | 359 | #e = readNonWhitespace(stream) |
|---|
| | 360 | #ndstream = stream.read(8) |
|---|
| | 361 | #assert e == "e" and ndstream == "ndstream" |
|---|
| | 364 | if data.has_key("__streamdata__"): |
|---|
| | 365 | return StreamObject.initializeFromDictionary(data) |
|---|
| | 366 | else: |
|---|
| | 367 | retval = DictionaryObject() |
|---|
| | 368 | retval.update(data) |
|---|
| | 369 | return retval |
|---|
| | 370 | readFromStream = staticmethod(readFromStream) |
|---|
| | 371 | |
|---|
| | 372 | |
|---|
| | 373 | class StreamObject(DictionaryObject): |
|---|
| | 374 | def __init__(self): |
|---|
| | 375 | self._data = None |
|---|
| | 376 | self.decodedSelf = None |
|---|
| | 377 | |
|---|
| | 378 | def writeToStream(self, stream): |
|---|
| | 379 | self[NameObject("/Length")] = NumberObject(len(self._data)) |
|---|
| | 380 | DictionaryObject.writeToStream(self, stream) |
|---|
| | 381 | del self["/Length"] |
|---|
| | 382 | stream.write("\nstream\n") |
|---|
| | 383 | stream.write(self._data) |
|---|
| | 384 | stream.write("\nendstream") |
|---|
| | 385 | |
|---|
| | 386 | def initializeFromDictionary(data): |
|---|
| | 387 | if data.has_key("/Filter"): |
|---|
| | 388 | retval = EncodedStreamObject() |
|---|
| | 389 | else: |
|---|
| | 390 | retval = DecodedStreamObject() |
|---|
| | 391 | retval._data = data["__streamdata__"] |
|---|
| | 392 | del data["__streamdata__"] |
|---|
| | 393 | del data["/Length"] |
|---|
| | 394 | retval.update(data) |
|---|
| 363 | | readFromStream = staticmethod(readFromStream) |
|---|
| | 396 | initializeFromDictionary = staticmethod(initializeFromDictionary) |
|---|
| | 397 | |
|---|
| | 398 | |
|---|
| | 399 | class DecodedStreamObject(StreamObject): |
|---|
| | 400 | def getData(self): |
|---|
| | 401 | return self._data |
|---|
| | 402 | |
|---|
| | 403 | def setData(self, data): |
|---|
| | 404 | self._data = data |
|---|
| | 405 | |
|---|
| | 406 | |
|---|
| | 407 | class EncodedStreamObject(StreamObject): |
|---|
| | 408 | def __init__(self): |
|---|
| | 409 | self.decodedSelf = None |
|---|
| | 410 | |
|---|
| | 411 | def getData(self): |
|---|
| | 412 | if self.decodedSelf: |
|---|
| | 413 | # cached version of decoded object |
|---|
| | 414 | return self.decodedSelf.getData() |
|---|
| | 415 | else: |
|---|
| | 416 | # create decoded object |
|---|
| | 417 | decoded = StreamObject() |
|---|
| | 418 | decoded._data = filters.decodeStreamData(self) |
|---|
| | 419 | for key, value in self.items(): |
|---|
| | 420 | if not key in ("/Length", "/Filter", "/DecodeParms"): |
|---|
| | 421 | decoded[key] = value |
|---|
| | 422 | self.decodedSelf = decoded |
|---|
| | 423 | return decoded._data |
|---|
| | 424 | |
|---|
| | 425 | def setData(self, data): |
|---|
| | 426 | raise "Creating EncodedStreamObject is not currently supported" |
|---|