Example #1
0
    def make_png(self, tex, fontsize, dpi):
        """
        generates a png file containing latex's rendering of tex string

        returns the filename
        """
        basefile = self.get_basefile(tex, fontsize, dpi)
        pngfile = '%s.png' % basefile

        # see get_rgba for a discussion of the background
        if DEBUG or not os.path.exists(pngfile):
            dvifile = self.make_dvi(tex, fontsize)
            command = [str("dvipng"), "-bg", "Transparent", "-D", str(dpi),
                       "-T", "tight", "-o", os.path.basename(pngfile),
                       os.path.basename(dvifile)]
            mpl.verbose.report(command, 'debug')
            try:
                report = subprocess.check_output(command,
                                                 cwd=self.texcache,
                                                 stderr=subprocess.STDOUT)
            except subprocess.CalledProcessError as exc:
                raise RuntimeError(
                    ('dvipng was not able to process the following '
                     'string:\n%s\n\n'
                     'Here is the full report generated by dvipng:\n%s '
                     '\n\n' % (repr(tex.encode('unicode_escape')),
                               exc.output.decode("utf-8"))))
            mpl.verbose.report(report, 'debug')

        return pngfile
Example #2
0
    def make_ps(self, tex, fontsize):
        """
        generates a postscript file containing latex's rendering of tex string

        returns the file name
        """
        basefile = self.get_basefile(tex, fontsize)
        psfile = '%s.epsf' % basefile

        if DEBUG or not os.path.exists(psfile):
            dvifile = self.make_dvi(tex, fontsize)
            command = [str("dvips"), "-q", "-E", "-o",
                       os.path.basename(psfile),
                       os.path.basename(dvifile)]
            mpl.verbose.report(command, 'debug')
            try:
                report = subprocess.check_output(command,
                                                 cwd=self.texcache,
                                                 stderr=subprocess.STDOUT)
            except subprocess.CalledProcessError as exc:
                raise RuntimeError(
                    ('dvips was not able to process the following '
                     'string:\n%s\n\n'
                     'Here is the full report generated by dvips:\n%s '
                     '\n\n' % (repr(tex.encode('unicode_escape')),
                               exc.output.decode("utf-8"))))
            mpl.verbose.report(report, 'debug')

        return psfile
Example #3
0
    def make_ps(self, tex, fontsize):
        """
        generates a postscript file containing latex's rendering of tex string

        returns the file name
        """
        basefile = self.get_basefile(tex, fontsize)
        psfile = '%s.epsf' % basefile

        if DEBUG or not os.path.exists(psfile):
            dvifile = self.make_dvi(tex, fontsize)
            command = [
                str("dvips"), "-q", "-E", "-o",
                os.path.basename(psfile),
                os.path.basename(dvifile)
            ]
            mpl.verbose.report(command, 'debug')
            try:
                report = subprocess.check_output(command,
                                                 cwd=self.texcache,
                                                 stderr=subprocess.STDOUT)
            except subprocess.CalledProcessError as exc:
                raise RuntimeError(
                    ('dvips was not able to process the following '
                     'string:\n%s\n\n'
                     'Here is the full report generated by dvips:\n%s '
                     '\n\n' % (repr(tex.encode('unicode_escape')),
                               exc.output.decode("utf-8"))))
            mpl.verbose.report(report, 'debug')

        return psfile
Example #4
0
    def make_png(self, tex, fontsize, dpi):
        """
        generates a png file containing latex's rendering of tex string

        returns the filename
        """
        basefile = self.get_basefile(tex, fontsize, dpi)
        pngfile = '%s.png' % basefile

        # see get_rgba for a discussion of the background
        if DEBUG or not os.path.exists(pngfile):
            dvifile = self.make_dvi(tex, fontsize)
            command = [
                str("dvipng"), "-bg", "Transparent", "-D",
                str(dpi), "-T", "tight", "-o",
                os.path.basename(pngfile),
                os.path.basename(dvifile)
            ]
            mpl.verbose.report(command, 'debug')
            try:
                report = subprocess.check_output(command,
                                                 cwd=self.texcache,
                                                 stderr=subprocess.STDOUT)
            except subprocess.CalledProcessError as exc:
                raise RuntimeError(
                    ('dvipng was not able to process the following '
                     'string:\n%s\n\n'
                     'Here is the full report generated by dvipng:\n%s '
                     '\n\n' % (repr(tex.encode('unicode_escape')),
                               exc.output.decode("utf-8"))))
            mpl.verbose.report(report, 'debug')

        return pngfile
Example #5
0
    def make_dvi_preview(self, tex, fontsize):
        """
        generates a dvi file containing latex's layout of tex
        string. It calls make_tex_preview() method and store the size
        information (width, height, descent) in a separte file.

        returns the file name
        """
        basefile = self.get_basefile(tex, fontsize)
        dvifile = '%s.dvi' % basefile
        baselinefile = '%s.baseline' % basefile

        if (DEBUG or not os.path.exists(dvifile)
                or not os.path.exists(baselinefile)):
            texfile = self.make_tex_preview(tex, fontsize)
            command = [
                str("latex"), "-interaction=nonstopmode",
                os.path.basename(texfile)
            ]
            mpl.verbose.report(command, 'debug')
            try:
                report = subprocess.check_output(command,
                                                 cwd=self.texcache,
                                                 stderr=subprocess.STDOUT)
            except subprocess.CalledProcessError as exc:
                raise RuntimeError(
                    ('LaTeX was not able to process the following '
                     'string:\n%s\n\n'
                     'Here is the full report generated by LaTeX:\n%s '
                     '\n\n' % (repr(tex.encode('unicode_escape')),
                               exc.output.decode("utf-8"))))
            mpl.verbose.report(report, 'debug')

            # find the box extent information in the latex output
            # file and store them in ".baseline" file
            m = TexManager._re_vbox.search(report.decode("utf-8"))
            with open(basefile + '.baseline', "w") as fh:
                fh.write(" ".join(m.groups()))

            for fname in glob.glob(basefile + '*'):
                if fname.endswith('dvi'):
                    pass
                elif fname.endswith('tex'):
                    pass
                elif fname.endswith('baseline'):
                    pass
                else:
                    try:
                        os.remove(fname)
                    except OSError:
                        pass

        return dvifile
Example #6
0
    def make_dvi_preview(self, tex, fontsize):
        """
        generates a dvi file containing latex's layout of tex
        string. It calls make_tex_preview() method and store the size
        information (width, height, descent) in a separte file.

        returns the file name
        """
        basefile = self.get_basefile(tex, fontsize)
        dvifile = '%s.dvi' % basefile
        baselinefile = '%s.baseline' % basefile

        if (DEBUG or not os.path.exists(dvifile) or
                not os.path.exists(baselinefile)):
            texfile = self.make_tex_preview(tex, fontsize)
            command = [str("latex"), "-interaction=nonstopmode",
                       os.path.basename(texfile)]
            mpl.verbose.report(command, 'debug')
            try:
                report = subprocess.check_output(command,
                                                 cwd=self.texcache,
                                                 stderr=subprocess.STDOUT)
            except subprocess.CalledProcessError as exc:
                raise RuntimeError(
                    ('LaTeX was not able to process the following '
                     'string:\n%s\n\n'
                     'Here is the full report generated by LaTeX:\n%s '
                     '\n\n' % (repr(tex.encode('unicode_escape')),
                               exc.output.decode("utf-8"))))
            mpl.verbose.report(report, 'debug')

            # find the box extent information in the latex output
            # file and store them in ".baseline" file
            m = TexManager._re_vbox.search(report.decode("utf-8"))
            with open(basefile + '.baseline', "w") as fh:
                fh.write(" ".join(m.groups()))

            for fname in glob.glob(basefile + '*'):
                if fname.endswith('dvi'):
                    pass
                elif fname.endswith('tex'):
                    pass
                elif fname.endswith('baseline'):
                    pass
                else:
                    try:
                        os.remove(fname)
                    except OSError:
                        pass

        return dvifile
Example #7
0
 def _run_checked_subprocess(self, command, tex):
     _log.debug(command)
     try:
         report = subprocess.check_output(command,
                                          cwd=self.texcache,
                                          stderr=subprocess.STDOUT)
     except subprocess.CalledProcessError as exc:
         raise RuntimeError(
             '{prog} was not able to process the following string:\n'
             '{tex!r}\n\n'
             'Here is the full report generated by {prog}:\n'
             '{exc}\n\n'.format(prog=command[0],
                                tex=tex.encode('unicode_escape'),
                                exc=exc.output.decode('utf-8')))
     _log.debug(report)
     return report
Example #8
0
 def _run_checked_subprocess(self, command, tex):
     _log.debug(command)
     try:
         report = subprocess.check_output(command,
                                          cwd=self.texcache,
                                          stderr=subprocess.STDOUT)
     except subprocess.CalledProcessError as exc:
         raise RuntimeError(
             '{prog} was not able to process the following string:\n'
             '{tex!r}\n\n'
             'Here is the full report generated by {prog}:\n'
             '{exc}\n\n'.format(
                 prog=command[0],
                 tex=tex.encode('unicode_escape'),
                 exc=exc.output.decode('utf-8')))
     _log.debug(report)
     return report
Example #9
0
    def make_dvi(self, tex, fontsize):
        """
        generates a dvi file containing latex's layout of tex string

        returns the file name
        """

        if rcParams['text.latex.preview']:
            return self.make_dvi_preview(tex, fontsize)

        basefile = self.get_basefile(tex, fontsize)
        dvifile = '%s.dvi' % basefile

        if DEBUG or not os.path.exists(dvifile):
            texfile = self.make_tex(tex, fontsize)
            command = [
                str("latex"), "-interaction=nonstopmode",
                os.path.basename(texfile)
            ]
            mpl.verbose.report(command, 'debug')
            with Locked(self.texcache):
                try:
                    report = subprocess.check_output(command,
                                                     cwd=self.texcache,
                                                     stderr=subprocess.STDOUT)
                except subprocess.CalledProcessError as exc:
                    raise RuntimeError(
                        ('LaTeX was not able to process the following '
                         'string:\n%s\n\n'
                         'Here is the full report generated by LaTeX:\n%s '
                         '\n\n' % (repr(tex.encode('unicode_escape')),
                                   exc.output.decode("utf-8"))))
                mpl.verbose.report(report, 'debug')
            for fname in glob.glob(basefile + '*'):
                if fname.endswith('dvi'):
                    pass
                elif fname.endswith('tex'):
                    pass
                else:
                    try:
                        os.remove(fname)
                    except OSError:
                        pass

        return dvifile
Example #10
0
    def make_dvi(self, tex, fontsize):
        """
        generates a dvi file containing latex's layout of tex string

        returns the file name
        """

        if rcParams['text.latex.preview']:
            return self.make_dvi_preview(tex, fontsize)

        basefile = self.get_basefile(tex, fontsize)
        dvifile = '%s.dvi' % basefile

        if DEBUG or not os.path.exists(dvifile):
            texfile = self.make_tex(tex, fontsize)
            command = [str("latex"), "-interaction=nonstopmode",
                       os.path.basename(texfile)]
            mpl.verbose.report(command, 'debug')
            with Locked(self.texcache):
                try:
                    report = subprocess.check_output(command,
                                                     cwd=self.texcache,
                                                     stderr=subprocess.STDOUT)
                except subprocess.CalledProcessError as exc:
                    raise RuntimeError(
                        ('LaTeX was not able to process the following '
                         'string:\n%s\n\n'
                         'Here is the full report generated by LaTeX:\n%s '
                         '\n\n' % (repr(tex.encode('unicode_escape')),
                                   exc.output.decode("utf-8"))))
                mpl.verbose.report(report, 'debug')
            for fname in glob.glob(basefile + '*'):
                if fname.endswith('dvi'):
                    pass
                elif fname.endswith('tex'):
                    pass
                else:
                    try:
                        os.remove(fname)
                    except OSError:
                        pass

        return dvifile