• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

parley

  • sources
  • kde-4.12
  • kdeedu
  • parley
  • src
  • practice
latexrenderer.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  Copyright 2010 Daniel Laidig <laidig@kde.org>
3  Copyright 2009 Alexander Rieder <alexanderrieder@gmail.com>
4  ***************************************************************************/
5 
6 /***************************************************************************
7  * *
8  * This program is free software; you can redistribute it and/or modify *
9  * it under the terms of the GNU General Public License as published by *
10  * the Free Software Foundation; either version 2 of the License, or *
11  * (at your option) any later version. *
12  * *
13  ***************************************************************************/
14 
15 #include "latexrenderer.h"
16 
17 #include <klocale.h>
18 #include <kdebug.h>
19 #include <ktemporaryfile.h>
20 #include <kstandarddirs.h>
21 #include <kprocess.h>
22 
23 #include <QLabel>
24 #include <QProcess>
25 #include <QFileInfo>
26 #include <QColor>
27 #include <complex>
28 
29 using namespace Practice;
30 
31 const char* texTemplate = "\\documentclass[12pt,fleqn]{article} \n "\
32  "\\usepackage{latexsym,amsfonts,amssymb,ulem} \n "\
33  "\\usepackage[dvips]{graphicx} \n "\
34  "\\setlength\\textwidth{5in} \n "\
35  "\\setlength{\\parindent}{0pt} \n "\
36  "\\usepackage{amsmath} \n "\
37  "\\usepackage{color} \n "\
38  "\\pagestyle{empty} \n "\
39  "\\begin{document} \n "\
40  "{\\definecolor{mycolor}{rgb}{%1} \n "\
41  "{\\color{mycolor} \n "\
42  "%2 } \n "\
43  "\\end{document}\n";
44 
45 LatexRenderer::LatexRenderer(QObject* parent)
46  : QObject(parent), m_label(0)
47 {
48 
49 }
50 
51 void LatexRenderer::renderLatex(QString tex)
52 {
53  if (m_label) {
54  m_label->setText(i18n("Rendering..."));
55  m_label->setToolTip(tex);
56  }
57 
58  if (tex.startsWith(QLatin1String("$$"))) {
59  tex.replace(0,2,"\\begin{eqnarray*}").remove(-2,2).append("\\end{eqnarray*}");
60  } else {
61  tex.remove(0,2).remove(-2,2);
62  }
63  kDebug()<<"rendering as latex";
64 
65  QString dir=KGlobal::dirs()->saveLocation("tmp", "parley/");
66 
67  //Check if the parley subdir exists, if not, create it
68  KTemporaryFile *texFile=new KTemporaryFile();
69  texFile->setPrefix( "parley/" );
70  texFile->setSuffix( ".tex" );
71  texFile->open();
72 
73  QColor color = m_label->palette().color(QPalette::WindowText);
74  QString colorString = QString::number(color.redF()) + ','
75  + QString::number(color.greenF()) + ','
76  + QString::number(color.blueF());
77  QString expressionTex = QString(texTemplate).arg(colorString, tex.trimmed());
78 
79  texFile->write(expressionTex.toUtf8());
80  texFile->flush();
81 
82  QString fileName = texFile->fileName();
83  kDebug()<<"fileName: "<<fileName;
84  m_latexFilename=fileName;
85  m_latexFilename.replace(".tex", ".eps");
86  KProcess *p=new KProcess( this );
87  p->setWorkingDirectory(dir);
88 
89  (*p)<<"latex"<<"-interaction=batchmode"<<"-halt-on-error"<<fileName;
90 
91  connect(p, SIGNAL( finished(int, QProcess::ExitStatus) ), this, SLOT( convertToPs() ) );
92  connect(p, SIGNAL( error(QProcess::ProcessError) ), this, SLOT( latexRendered() ) );
93  p->start();
94 }
95 
96 bool LatexRenderer::isLatex(const QString& tex)
97 {
98  return tex.length() > 4 && tex.mid(2, tex.length()-4).simplified().length() > 0 &&
99  ((tex.startsWith(QLatin1String("$$")) && tex.endsWith(QLatin1String("$$"))) ||
100  (tex.startsWith(QString::fromUtf8("§§")) && tex.endsWith(QString::fromUtf8("§§"))));
101 }
102 
103 void LatexRenderer::convertToPs()
104 {
105  kDebug()<<"converting to ps";
106  QString dviFile=m_latexFilename;
107  dviFile.replace(".eps", ".dvi");
108  KProcess *p=new KProcess( this );
109  kDebug()<<"running: "<<"dvips"<<"-E"<<"-o"<<m_latexFilename<<dviFile;
110  (*p)<<"dvips"<<"-E"<<"-o"<<m_latexFilename<<dviFile;
111 
112  connect(p, SIGNAL( finished(int, QProcess::ExitStatus) ), this, SLOT( convertToImage() ) );
113  connect(p, SIGNAL( error(QProcess::ProcessError) ), this, SLOT( latexRendered() ) );
114  p->start();
115 }
116 
117 void LatexRenderer::convertToImage()
118 {
119  kDebug()<<"converting to ps";
120  QString pngFile=m_latexFilename;
121  pngFile.replace(".eps", ".png");
122  KProcess *p=new KProcess( this );
123  kDebug()<<"running:"<<"convert"<<m_latexFilename<<pngFile;
124  (*p)<<"convert"<<"-density"<<"85"<<m_latexFilename<<pngFile;
125 
126  connect(p, SIGNAL( finished(int, QProcess::ExitStatus) ), this, SLOT( latexRendered() ) );
127  connect(p, SIGNAL( error(QProcess::ProcessError) ), this, SLOT( latexRendered() ) );
128  p->start();
129 }
130 
131 void LatexRenderer::latexRendered()
132 {
133  kDebug()<<"rendered file "<<m_latexFilename;
134 
135  QString pngFile=m_latexFilename;
136  pngFile.replace(".eps", ".png");
137  if(QFileInfo(pngFile).exists())
138  {
139  QPixmap pixmap(pngFile);
140  m_label->setPixmap(pixmap);
141  m_label->setMinimumSize(pixmap.size().boundedTo(QSize(600, 300)));
142  } else {
143  m_label->setText(i18n("LaTeX error.")); //TODO: better error handling and error messages
144  }
145 
146  //cleanup the temp directory a bit...
147  QString dir=KGlobal::dirs()->saveLocation("tmp", "parley/");
148  QStringList extensions;
149  extensions<<".log"<<".aux"<<".tex"<<".dvi"<<".eps"<<".png";
150  foreach(const QString& ext, extensions)
151  {
152  QString s=m_latexFilename;
153  s.replace(".eps", ext);
154  QFile f(s);
155  f.remove();
156  }
157 }
158 
159 #include "latexrenderer.moc"
QObject
texTemplate
const char * texTemplate
Definition: latexrenderer.cpp:31
latexrenderer.h
Practice::LatexRenderer::LatexRenderer
LatexRenderer(QObject *parent=0)
Definition: latexrenderer.cpp:45
Practice::LatexRenderer::renderLatex
void renderLatex(QString tex)
Definition: latexrenderer.cpp:51
Practice::LatexRenderer::isLatex
static bool isLatex(const QString &tex)
Definition: latexrenderer.cpp:96
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:42:06 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

parley

Skip menu "parley"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • kstars
  • libkdeedu
  •   keduvocdocument
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal