Exemple #1
0
    def _formatText(self, text):
        "Generates PDF text output operator(s)"
        if log2vis and self.direction in ('LTR', 'RTL'):
            # Use pyfribidi to write the text in the correct visual order.
            text = log2vis(text,
                           directionsMap.get(self.direction, DIR_ON),
                           clean=True)
        canv = self._canvas
        font = pdfmetrics.getFont(self._fontname)
        R = []
        if font._dynamicFont:
            #it's a truetype font and should be utf8.  If an error is raised,
            for subset, t in font.splitString(text, canv._doc):
                if subset != self._curSubset:
                    pdffontname = font.getSubsetInternalName(subset, canv._doc)
                    R.append("%s %s Tf %s TL" %
                             (pdffontname, fp_str(
                                 self._fontsize), fp_str(self._leading)))
                    self._curSubset = subset
                R.append("(%s) Tj" % canv._escape(t))
        elif font._multiByte:
            #all the fonts should really work like this - let them know more about PDF...
            R.append("%s %s Tf %s TL" % (canv._doc.getInternalFontName(
                font.fontName), fp_str(self._fontsize), fp_str(self._leading)))
            R.append("(%s) Tj" % font.formatForPdf(text))
        else:
            #convert to T1  coding
            fc = font
            if isBytes(text):
                try:
                    text = text.decode('utf8')
                except UnicodeDecodeError as e:
                    i, j = e.args[2:4]
                    raise UnicodeDecodeError(
                        *(e.args[:4] + ('%s\n%s-->%s<--%s' %
                                        (e.args[4], text[max(i - 10, 0):i],
                                         text[i:j], text[j:j + 10]), )))

            for f, t in pdfmetrics.unicode2T1(text,
                                              [font] + font.substitutionFonts):
                if f != fc:
                    R.append("%s %s Tf %s TL" %
                             (canv._doc.getInternalFontName(f.fontName),
                              fp_str(self._fontsize), fp_str(self._leading)))
                    fc = f
                R.append("(%s) Tj" % canv._escape(t))
            if font != fc:
                R.append("%s %s Tf %s TL" %
                         (canv._doc.getInternalFontName(self._fontname),
                          fp_str(self._fontsize), fp_str(self._leading)))
        return ' '.join(R)
Exemple #2
0
    def _formatText(self, text):
        "Generates PDF text output operator(s)"
        # Use pyfribidi to write the text in the correct visual order.
        directions = {'LTR': DIR_LTR, 'RTL': DIR_RTL}
        text = log2vis(
            text, directions.get(self.direction, DIR_ON), reordernsm=False)
        text = remove_noprint(text)
        canv = self._canvas
        font = pdfmetrics.getFont(self._fontname)
        R = []
        if font._dynamicFont:
            #it's a truetype font and should be utf8.  If an error is raised,
            for subset, t in font.splitString(text, canv._doc):
                if subset != self._curSubset:
                    pdffontname = font.getSubsetInternalName(subset, canv._doc)
                    R.append(
                        "%s %s Tf %s TL" % (pdffontname, fp_str(
                            self._fontsize), fp_str(self._leading)))
                    self._curSubset = subset
                R.append("(%s) Tj" % canv._escape(t))
        elif font._multiByte:
            #all the fonts should really work like this - let them know more about PDF...
            R.append("%s %s Tf %s TL" % (canv._doc.getInternalFontName(
                font.fontName), fp_str(self._fontsize), fp_str(self._leading)))
            R.append("(%s) Tj" % font.formatForPdf(text))
        else:
            #convert to T1  coding
            fc = font
            if not isinstance(text, unicode):
                try:
                    text = text.decode('utf8')
                except UnicodeDecodeError, e:
                    i, j = e.args[2:4]
                    raise UnicodeDecodeError(
                        *(e.args[:4] + ('%s\n%s-->%s<--%s' %
                                        (e.args[4], text[max(i - 10, 0):i],
                                         text[i:j], text[j:j + 10]), )))

            for f, t in pdfmetrics.unicode2T1(text,
                                              [font] + font.substitutionFonts):
                if f != fc:
                    R.append("%s %s Tf %s TL" % (canv._doc.getInternalFontName(
                        f.fontName), fp_str(self._fontsize),
                                                 fp_str(self._leading)))
                    fc = f
                R.append("(%s) Tj" % canv._escape(t))
            if font != fc:
                R.append("%s %s Tf %s TL" % (canv._doc.getInternalFontName(
                    self._fontname), fp_str(self._fontsize),
                                             fp_str(self._leading)))
Exemple #3
0
 def fribidiText(text,direction):
     return log2vis(text, directionsMap.get(direction,DIR_ON),clean=True) if direction in ('LTR','RTL') else text
Exemple #4
0
 def fribidiText(text, direction):
     return log2vis(text,
                    directionsMap.get(direction, DIR_ON),
                    clean=True) if direction in ('LTR', 'RTL') else text
Exemple #5
0
def stringWidth(text, fontName, fontSize, encoding='utf8'):
    """Compute width of string in points;
    not accelerated as fast enough because of _instanceStringWidthU"""
    text = log2vis(text, DIR_RTL if rl_config.rtl else DIR_LTR)
    text = remove_noprint(text)
    return getFont(fontName).stringWidth(text, fontSize, encoding=encoding)