def draw(self, pmlOp, pageNr, output, pe): if pmlOp.toc: pmlOp.toc.pageObjNr = pe.pageObjs[pageNr].nr # we need to adjust y position since PDF uses baseline of text as # the y pos, but pml uses top of the text as y pos. The Adobe # standard Courier family font metrics give 157 units in 1/1000 # point units as the Descender value, thus giving (1000 - 157) = # 843 units from baseline to top of text. # http://partners.adobe.com/asn/tech/type/ftechnotes.jsp contains # the "Font Metrics for PDF Core 14 Fonts" document. x = pe.x(pmlOp.x) y = pe.y(pmlOp.y) - 0.843 * pmlOp.size newFont = "F%d %d" % (pe.getFontNr(pmlOp.flags), pmlOp.size) if newFont != pe.currentFont: output += "/%s Tf\n" % newFont pe.currentFont = newFont if pmlOp.angle is not None: matrix = TRANSFORM_MATRIX.get(pmlOp.angle) if matrix: output += "BT\n"\ "%f %f %f %f %f %f Tm\n"\ "(%s) Tj\n"\ "ET\n" % (matrix[0], matrix[1], matrix[2], matrix[3], x, y, pe.escapeStr(pmlOp.text)) else: # unsupported angle, don't print it. pass else: output += "BT\n"\ "%f %f Td\n"\ "(%s) Tj\n"\ "ET\n" % (x, y, pe.escapeStr(pmlOp.text)) if pmlOp.flags & pml.UNDERLINED: undLen = fontinfo.getMetrics(pmlOp.flags).getTextWidth( pmlOp.text, pmlOp.size) # all standard PDF fonts have the underline line 100 units # below baseline with a thickness of 50 undY = y - 0.1 * pmlOp.size output += "%f w\n"\ "%f %f m\n"\ "%f %f l\n"\ "S\n" % (0.05 * pmlOp.size, x, undY, x + undLen, undY)
def draw(self, pmlOp, pageNr, canvas, pe): # we need to adjust y position since PDF uses baseline of text as # the y pos, but pml uses top of the text as y pos. The Adobe # standard Courier family font metrics give 157 units in 1/1000 # point units as the Descender value, thus giving (1000 - 157) = # 843 units from baseline to top of text. # http://partners.adobe.com/asn/tech/type/ftechnotes.jsp contains # the "Font Metrics for PDF Core 14 Fonts" document. x = pe.x(pmlOp.x) y = pe.y(pmlOp.y) - 0.843 * pmlOp.size if pe.doc.tocs and pmlOp.toc: key = "id%d" % id(pmlOp) canvas.bookmarkPage(key, fit="XYZ", left=pe.x(pmlOp.x), top=pe.y(pmlOp.y)) canvas.addOutlineEntry(pmlOp.toc.text, key) newFont = (pe.getFontNr(pmlOp.flags), pmlOp.size) if newFont != pe.currentFont: canvas.setFont(*newFont) pe.currentFont = newFont if pmlOp.angle is None or pmlOp.angle == 0.0: canvas.drawString(x, y, pmlOp.text) else: canvas.saveState() canvas.translate(x, y) canvas.rotate(pmlOp.angle) canvas.drawString(0, 0, pmlOp.text) canvas.restoreState() if pmlOp.flags & pml.UNDERLINED: undLen = fontinfo.getMetrics(pmlOp.flags).getTextWidth( pmlOp.text, pmlOp.size) # all standard PDF fonts have the underline line 100 units # below baseline with a thickness of 50 undY = y - 0.1 * pmlOp.size canvas.setLineWidth(0.05 * pmlOp.size) canvas.line(x, undY, x + undLen, undY)
def draw(self, pmlOp, pageNr, canvas, pe): # we need to adjust y position since PDF uses baseline of text as # the y pos, but pml uses top of the text as y pos. The Adobe # standard Courier family font metrics give 157 units in 1/1000 # point units as the Descender value, thus giving (1000 - 157) = # 843 units from baseline to top of text. # http://partners.adobe.com/asn/tech/type/ftechnotes.jsp contains # the "Font Metrics for PDF Core 14 Fonts" document. x = pe.x(pmlOp.x) y = pe.y(pmlOp.y) - 0.843 * pmlOp.size if pe.doc.tocs and pmlOp.toc: key = "id%d" % id(pmlOp) canvas.bookmarkPage(key,fit="XYZ",left=pe.x(pmlOp.x),top=pe.y(pmlOp.y)) canvas.addOutlineEntry(pmlOp.toc.text,key) newFont = (pe.getFontNr(pmlOp.flags), pmlOp.size) if newFont != pe.currentFont: canvas.setFont(*newFont) pe.currentFont = newFont if pmlOp.angle is None or pmlOp.angle == 0.0: canvas.drawString(x,y,pmlOp.text) else: canvas.saveState() canvas.translate(x,y) canvas.rotate(pmlOp.angle) canvas.drawString(0,0,pmlOp.text) canvas.restoreState() if pmlOp.flags & pml.UNDERLINED: undLen = fontinfo.getMetrics(pmlOp.flags).getTextWidth( pmlOp.text, pmlOp.size) # all standard PDF fonts have the underline line 100 units # below baseline with a thickness of 50 undY = y - 0.1 * pmlOp.size canvas.setLineWidth(0.05 * pmlOp.size) canvas.line(x, undY, x + undLen, undY)
def getTextWidth(text, style, size): return (fontinfo.getMetrics(style).getTextWidth(text, size) / 72.0) * 25.4
def getFontNr(self, flags): # the "& 15" gets rid of the underline flag fi = self.fonts.get(flags & 15) if not fi: print "PDF.getfontNr: invalid flags %d" % flags return 0 if fi.number == -1: fi.number = self.fontCnt self.fontCnt += 1 # the "& 15" gets rid of the underline flag pfi = self.doc.fonts.get(flags & 15) if not pfi: fi.pdfObj = self.addObj("<< /Type /Font\n" "/Subtype /Type1\n" "/BaseFont /%s\n" "/Encoding /WinAnsiEncoding\n" ">>" % fi.name) else: self.genWidths() fi.pdfObj = self.addObj( "<< /Type /Font\n" "/Subtype /TrueType\n" "/BaseFont /%s\n" "/Encoding /WinAnsiEncoding\n" "/FirstChar 0\n" "/LastChar 255\n" "/Widths %d 0 R\n" "/FontDescriptor %d 0 R\n" ">>" % (pfi.name, self.widthsObj.nr, self.objectCnt + 1)) fm = fontinfo.getMetrics(flags) if pfi.fontProgram: fpStr = "/FontFile2 %d 0 R\n" % (self.objectCnt + 1) else: fpStr = "" # we use a %s format specifier for the italic angle since # it sometimes contains integers, sometimes floating point # values. self.addObj( "<< /Type /FontDescriptor\n" "/FontName /%s\n" "/FontWeight %d\n" "/Flags %d\n" "/FontBBox [%d %d %d %d]\n" "/ItalicAngle %s\n" "/Ascent %s\n" "/Descent %s\n" "/CapHeight %s\n" "/StemV %s\n" "/StemH %s\n" "/XHeight %d\n" "%s" ">>" % (pfi.name, fm.fontWeight, fm.flags, fm.bbox[0], fm.bbox[1], fm.bbox[2], fm.bbox[3], fm.italicAngle, fm.ascent, fm.descent, fm.capHeight, fm.stemV, fm.stemH, fm.xHeight, fpStr)) if pfi.fontProgram: self.addObj(self.genStream(pfi.fontProgram, True)) return fi.number
def getFontNr(self, flags): # the "& 15" gets rid of the underline flag fi = self.fonts.get(flags & 15) if not fi: print "PDF.getfontNr: invalid flags %d" % flags return 0 if fi.number == -1: fi.number = self.fontCnt self.fontCnt += 1 # the "& 15" gets rid of the underline flag pfi = self.doc.fonts.get(flags & 15) if not pfi: fi.pdfObj = self.addObj("<< /Type /Font\n" "/Subtype /Type1\n" "/BaseFont /%s\n" "/Encoding /WinAnsiEncoding\n" ">>" % fi.name) else: self.genWidths() fi.pdfObj = self.addObj("<< /Type /Font\n" "/Subtype /TrueType\n" "/BaseFont /%s\n" "/Encoding /WinAnsiEncoding\n" "/FirstChar 0\n" "/LastChar 255\n" "/Widths %d 0 R\n" "/FontDescriptor %d 0 R\n" ">>" % (pfi.name, self.widthsObj.nr, self.objectCnt + 1)) fm = fontinfo.getMetrics(flags) if pfi.fontProgram: fpStr = "/FontFile2 %d 0 R\n" % (self.objectCnt + 1) else: fpStr = "" # we use a %s format specifier for the italic angle since # it sometimes contains integers, sometimes floating point # values. self.addObj("<< /Type /FontDescriptor\n" "/FontName /%s\n" "/FontWeight %d\n" "/Flags %d\n" "/FontBBox [%d %d %d %d]\n" "/ItalicAngle %s\n" "/Ascent %s\n" "/Descent %s\n" "/CapHeight %s\n" "/StemV %s\n" "/StemH %s\n" "/XHeight %d\n" "%s" ">>" % (pfi.name, fm.fontWeight, fm.flags, fm.bbox[0], fm.bbox[1], fm.bbox[2], fm.bbox[3], fm.italicAngle, fm.ascent, fm.descent, fm.capHeight, fm.stemV, fm.stemH, fm.xHeight, fpStr)) if pfi.fontProgram: self.addObj(self.genStream(pfi.fontProgram, True)) return fi.number