def addObjects(self, doc): """Add whatever needed to PDF file, and return a FontDescriptor reference""" from reportlab.pdfbase import pdfdoc fontFile = pdfdoc.PDFStream() fontFile.content = self._binaryData #fontFile.dictionary['Length'] = self._length fontFile.dictionary['Length1'] = self._length1 fontFile.dictionary['Length2'] = self._length2 fontFile.dictionary['Length3'] = self._length3 #fontFile.filters = [pdfdoc.PDFZCompress] fontFileRef = doc.Reference(fontFile, 'fontFile:' + self.pfbFileName) fontDescriptor = pdfdoc.PDFDictionary({ 'Type': '/FontDescriptor', 'Ascent':self.ascent, 'CapHeight':self.capHeight, 'Descent':self.descent, 'Flags': 34, 'FontBBox':pdfdoc.PDFArray(self.bbox), 'FontName':pdfdoc.PDFName(self.name), 'ItalicAngle':self.italicAngle, 'StemV':self.stemV, 'XHeight':self.xHeight, 'FontFile': fontFileRef, }) fontDescriptorRef = doc.Reference(fontDescriptor, 'fontDescriptor:' + self.name) return fontDescriptorRef
def addSubsetObjects(self, doc, fontname, subset): """Generate a TrueType font subset and add it to the PDF document. Returns a PDFReference to the new FontDescriptor object.""" fontFile = pdfdoc.PDFStream() fontFile.content = self.makeSubset(subset) fontFile.dictionary['Length1'] = len(fontFile.content) if doc.compression: fontFile.filters = [pdfdoc.PDFZCompress] fontFileRef = doc.Reference(fontFile, 'fontFile:%s(%s)' % (self.filename, fontname)) flags = self.flags & ~ FF_NONSYMBOLIC flags = flags | FF_SYMBOLIC fontDescriptor = pdfdoc.PDFDictionary({ 'Type': '/FontDescriptor', 'Ascent': self.ascent, 'CapHeight': self.capHeight, 'Descent': self.descent, 'Flags': flags, 'FontBBox': pdfdoc.PDFArray(self.bbox), 'FontName': pdfdoc.PDFName(fontname), 'ItalicAngle': self.italicAngle, 'StemV': self.stemV, 'FontFile2': fontFileRef, }) return doc.Reference(fontDescriptor, 'fontDescriptor:' + fontname)
def format(self, document): """Allow it to be used within pdfdoc framework. This only defines how it is stored, not how it is drawn later.""" dict = pdfdoc.PDFDictionary() dict['Type'] = '/XObject' dict['Subtype'] = '/Image' dict['Width'] = self.width dict['Height'] = self.height dict['BitsPerComponent'] = 8 dict['ColorSpace'] = pdfdoc.PDFName(self.colorSpace) content = string.join(self.imageData[3:-1], '\n') + '\n' strm = pdfdoc.PDFStream(dictionary=dict, content=content) return strm.format(document)
def addObjects(self, doc): """Makes one or more PDF objects to be added to the document. The caller supplies the internal name to be used (typically F1, F2, ... in sequence). This method creates a number of Font and FontDescriptor objects. Every FontDescriptor is a (no more than) 256 character subset of the original TrueType font.""" try: state = self.state[doc] except KeyError: state = self.state[doc] = TTFont.State(self._asciiReadable) state.frozen = 1 for n, subset in enumerate(state.subsets): internalName = self.getSubsetInternalName(n, doc)[1:] baseFontName = (b''.join( (SUBSETN(n), b'+', self.face.name, self.face.subfontNameX))).decode('pdfdoc') pdfFont = pdfdoc.PDFTrueTypeFont() pdfFont.__Comment__ = 'Font %s subset %d' % (self.fontName, n) pdfFont.Name = internalName pdfFont.BaseFont = baseFontName pdfFont.FirstChar = 0 pdfFont.LastChar = len(subset) - 1 widths = list(map(self.face.getCharWidth, subset)) pdfFont.Widths = pdfdoc.PDFArray(widths) cmapStream = pdfdoc.PDFStream() cmapStream.content = makeToUnicodeCMap(baseFontName, subset) if doc.compression: cmapStream.filters = [pdfdoc.PDFZCompress] pdfFont.ToUnicode = doc.Reference(cmapStream, 'toUnicodeCMap:' + baseFontName) pdfFont.FontDescriptor = self.face.addSubsetObjects( doc, baseFontName, subset) # link it in ref = doc.Reference(pdfFont, internalName) fontDict = doc.idToObject['BasicFonts'].dict fontDict[internalName] = pdfFont del self.state[doc]