src/reportlab/pdfbase/pdfdoc.py
branchpy33
changeset 3781 df8b57380768
parent 3747 7155d7117e67
child 3794 398ea04239b5
equal deleted inserted replaced
3780:b809eb915503 3781:df8b57380768
    16 """
    16 """
    17 import string, types, binascii, codecs
    17 import string, types, binascii, codecs
    18 from reportlab.pdfbase import pdfutils
    18 from reportlab.pdfbase import pdfutils
    19 from reportlab.pdfbase.pdfutils import LINEEND # this constant needed in both
    19 from reportlab.pdfbase.pdfutils import LINEEND # this constant needed in both
    20 from reportlab import rl_config
    20 from reportlab import rl_config
    21 from reportlab.lib.utils import import_zlib, open_for_read, fp_str, makeFileName, isSeq, isBytes, isUnicode, _digester
    21 from reportlab.lib.utils import import_zlib, open_for_read, makeFileName, isSeq, isBytes, isUnicode, _digester
       
    22 from reportlab.lib.rl_accel import escapePDF, fp_str, asciiBase85Encode, asciiBase85Decode
    22 from reportlab.pdfbase import pdfmetrics
    23 from reportlab.pdfbase import pdfmetrics
    23 from hashlib import md5
    24 from hashlib import md5
    24 
    25 
    25 from sys import platform
    26 from sys import platform
    26 from sys import version_info
    27 from sys import version_info
   633         if not isinstance(document.encrypt,NoEncryption):
   634         if not isinstance(document.encrypt,NoEncryption):
   634             s = document.encrypt.encode(s)
   635             s = document.encrypt.encode(s)
   635             escape = 1
   636             escape = 1
   636         if escape:
   637         if escape:
   637             try:
   638             try:
   638                 es = "(%s)" % pdfutils._escape(s)
   639                 es = "(%s)" % escapePDF(s)
   639             except:
   640             except:
   640                 raise ValueError("cannot escape %s %s" % (s, repr(s)))
   641                 raise ValueError("cannot escape %s %s" % (s, repr(s)))
   641             if escape&2:
   642             if escape&2:
   642                 es = es.replace('\\012','\n')
   643                 es = es.replace('\\012','\n')
   643             if escape&4 and _isbalanced(s):
   644             if escape&4 and _isbalanced(s):
   644                 es = es.replace('\\(','(').replace('\\)',')')
   645                 es = es.replace('\\(','(').replace('\\)',')')
   645             return es
   646             return es
   646         else:
   647         else:
   647             return b'(' + s + b')'
   648             return b'(' + s + b')'
   648     def __str__(self):
   649     def __str__(self):
   649         return "(%s)" % pdfutils._escape(self.s)
   650         return "(%s)" % escapePDF(self.s)
   650 
   651 
   651 def PDFName(data,lo=chr(0x21),hi=chr(0x7e)):
   652 def PDFName(data,lo=chr(0x21),hi=chr(0x7e)):
   652     # might need to change this to class for encryption
   653     # might need to change this to class for encryption
   653     #  NOTE: RESULT MUST ALWAYS SUPPORT MEANINGFUL COMPARISONS (EQUALITY) AND HASH
   654     #  NOTE: RESULT MUST ALWAYS SUPPORT MEANINGFUL COMPARISONS (EQUALITY) AND HASH
   654     # first convert the name
   655     # first convert the name
   768 PDFZCompress = PDFStreamFilterZCompress()
   769 PDFZCompress = PDFStreamFilterZCompress()
   769 
   770 
   770 class PDFStreamFilterBase85Encode:
   771 class PDFStreamFilterBase85Encode:
   771     pdfname = "ASCII85Decode"
   772     pdfname = "ASCII85Decode"
   772     def encode(self, text):
   773     def encode(self, text):
   773         from .pdfutils import _AsciiBase85Encode, _wrap
   774         from .pdfutils import _wrap
   774         text = _AsciiBase85Encode(text)
   775         text = asciiBase85Encode(text)
   775         if rl_config.wrapA85:
   776         if rl_config.wrapA85:
   776             text = _wrap(text)
   777             text = _wrap(text)
   777         return text
   778         return text
   778     def decode(self, text):
   779     def decode(self, text):
   779         from .pdfutils import _AsciiBase85Decode
   780         return asciiBase85Decode(text)
   780         return _AsciiBase85Decode(text)
       
   781 
   781 
   782 # need only one of these too
   782 # need only one of these too
   783 PDFBase85Encode = PDFStreamFilterBase85Encode()
   783 PDFBase85Encode = PDFStreamFilterBase85Encode()
   784 
   784 
   785 STREAMFMT = ("%(dictionary)s%(LINEEND)s" # dictionary
   785 STREAMFMT = ("%(dictionary)s%(LINEEND)s" # dictionary
  2194         else: #maybe should generate an error, is this right for CMYK?
  2194         else: #maybe should generate an error, is this right for CMYK?
  2195             self.colorSpace = 'DeviceCMYK'
  2195             self.colorSpace = 'DeviceCMYK'
  2196             self._dotrans = 1
  2196             self._dotrans = 1
  2197         self.streamContent = imageFile.read()
  2197         self.streamContent = imageFile.read()
  2198         if rl_config.useA85:
  2198         if rl_config.useA85:
  2199             self.streamContent = pdfutils._AsciiBase85Encode(self.streamContent)
  2199             self.streamContent = asciiBase85Encode(self.streamContent)
  2200             self._filters = 'ASCII85Decode','DCTDecode' #'A85','DCT'
  2200             self._filters = 'ASCII85Decode','DCTDecode' #'A85','DCT'
  2201         else:
  2201         else:
  2202             self._filters = 'DCTDecode', #'DCT'
  2202             self._filters = 'DCTDecode', #'DCT'
  2203         self.mask = None
  2203         self.mask = None
  2204         return True
  2204         return True
  2243             self.width, self.height = im.getSize()
  2243             self.width, self.height = im.getSize()
  2244             raw = im.getRGBData()
  2244             raw = im.getRGBData()
  2245             #assert len(raw) == self.width*self.height, "Wrong amount of data for image expected %sx%s=%s got %s" % (self.width,self.height,self.width*self.height,len(raw))
  2245             #assert len(raw) == self.width*self.height, "Wrong amount of data for image expected %sx%s=%s got %s" % (self.width,self.height,self.width*self.height,len(raw))
  2246             self.streamContent = zlib.compress(raw)
  2246             self.streamContent = zlib.compress(raw)
  2247             if rl_config.useA85:
  2247             if rl_config.useA85:
  2248                 self.streamContent = pdfutils._AsciiBase85Encode(self.streamContent)
  2248                 self.streamContent = asciiBase85Encode(self.streamContent)
  2249                 self._filters = 'ASCII85Decode','FlateDecode' #'A85','Fl'
  2249                 self._filters = 'ASCII85Decode','FlateDecode' #'A85','Fl'
  2250             else:
  2250             else:
  2251                 self._filters = 'FlateDecode', #'Fl'
  2251                 self._filters = 'FlateDecode', #'Fl'
  2252             self.colorSpace= _mode2CS[im.mode]
  2252             self.colorSpace= _mode2CS[im.mode]
  2253             self.bitsPerComponent = 8
  2253             self.bitsPerComponent = 8