File indexing completed on 2024-05-19 04:36:37
0001 /* This file is part of the TikZKit project. 0002 * 0003 * Copyright (C) 2013 Dominik Haumann <dhaumann@kde.org> 0004 * 0005 * This library is free software; you can redistribute it and/or modify 0006 * it under the terms of the GNU Library General Public License as published 0007 * by the Free Software Foundation, either version 2 of the License, or 0008 * (at your option) any later version. 0009 * 0010 * This library is distributed in the hope that it will be useful, 0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0013 * GNU Library General Public License for more details. 0014 * 0015 * You should have received a copy of the GNU Library General Public License 0016 * along with this library; see the file COPYING.LIB. If not, see 0017 * <http://www.gnu.org/licenses/>. 0018 */ 0019 0020 #include "TexGenerator.h" 0021 #include "NodeItem.h" 0022 0023 #include <QPointer> 0024 #include <QPainter> 0025 #include <QGraphicsView> 0026 #include <QProcess> 0027 #include <QTemporaryFile> 0028 #include <QFileInfo> 0029 #include <QTextStream> 0030 0031 #include <QDebug> 0032 0033 namespace tex { 0034 0035 class TexGeneratorPrivate 0036 { 0037 public: 0038 QProcess * process; 0039 QTemporaryFile * tempFile; 0040 bool postProcessRunning; 0041 }; 0042 0043 TexGenerator::TexGenerator(QObject * parent) 0044 : QObject(parent) 0045 , d(new TexGeneratorPrivate()) 0046 { 0047 d->process = nullptr; 0048 d->tempFile = nullptr; 0049 d->postProcessRunning = false; 0050 } 0051 0052 TexGenerator::~TexGenerator() 0053 { 0054 delete d; 0055 } 0056 0057 QString TexGenerator::pdfFile() 0058 { 0059 if (d->tempFile) { 0060 QFileInfo fi(*d->tempFile); 0061 return fi.baseName() + ".pdf"; 0062 } 0063 return QString(); 0064 } 0065 0066 QString TexGenerator::svgFile() 0067 { 0068 if (d->tempFile) { 0069 QFileInfo fi(*d->tempFile); 0070 return fi.baseName() + ".svg"; 0071 } 0072 return QString(); 0073 } 0074 0075 void TexGenerator::generateImage(const QString& texCode) 0076 { 0077 if (!d->process) { 0078 d->process = new QProcess(this); 0079 0080 connect(d->process, SIGNAL(readyReadStandardError()), 0081 this, SLOT(outputReady())); 0082 connect(d->process, SIGNAL(finished(int, QProcess::ExitStatus)), 0083 this, SLOT(processFinished(int, QProcess::ExitStatus))); 0084 connect(d->process, SIGNAL(error(QProcess::ProcessError)), 0085 this, SLOT(processError(QProcess::ProcessError))); 0086 } else { 0087 if (d->process->state() == QProcess::Running) { 0088 d->process->terminate(); 0089 } 0090 } 0091 0092 if (d->tempFile) { 0093 delete d->tempFile; 0094 } 0095 0096 d->tempFile = new QTemporaryFile("XXXXXX.tex", this); 0097 if (!d->tempFile->open()) { 0098 qWarning() << "Could not create temporary tex file"; 0099 return; 0100 } else { 0101 QTextStream ts(d->tempFile); 0102 ts << "\\documentclass[crop]{standalone}\n" 0103 "\\renewcommand*{\\familydefault}{\\sfdefault}\n" 0104 // "\\usepackage[pdftex,active,tightpage]{preview}\n" 0105 "\\usepackage{amsmath}\n" 0106 "\\usepackage{amssymb}\n" 0107 "\\usepackage{amsfonts}\n" 0108 "\\usepackage{tikz}\n" 0109 "\\begin{document}\n" 0110 // "\\begin{preview}\n" 0111 "\\small\n" 0112 "\\begin{tikzpicture}[inner sep=0pt, outer sep=0pt]\n" 0113 "\\node[align=center] at (0, 0) {"+ texCode + "};\n" 0114 "\\end{tikzpicture}\n" 0115 // "\\end{preview}\n" 0116 "\\end{document}"; 0117 d->tempFile->close(); 0118 } 0119 0120 QStringList args; 0121 args << "-halt-on-error" << d->tempFile->fileName(); 0122 0123 // qDebug() << "launching process: 'latex'"; 0124 d->process->start("latex", args); 0125 } 0126 0127 void TexGenerator::processFinished(int exitCode, QProcess::ExitStatus exitStatus) 0128 { 0129 if (exitCode != 0) { 0130 qDebug() << "process finished with exitCode" << exitCode; 0131 return; 0132 } 0133 0134 if (!d->postProcessRunning) { 0135 QFileInfo fi(*d->tempFile); 0136 QStringList args; 0137 args << "--no-fonts" << (fi.baseName() + ".dvi") 0138 << "-o" << (fi.baseName() + ".svg"); 0139 0140 // qDebug() << "launching process: 'dvisvgm'"; 0141 d->postProcessRunning = true; 0142 d->process->start("dvisvgm", args); 0143 } else { 0144 d->postProcessRunning = false; 0145 0146 QFileInfo fi(*d->tempFile); 0147 Q_EMIT svgReady(fi.baseName() + ".svg"); 0148 } 0149 } 0150 0151 void TexGenerator::processError(QProcess::ProcessError error) 0152 { 0153 // qDebug() << "process error" << error; 0154 } 0155 0156 void TexGenerator::outputReady() 0157 { 0158 // qDebug() << "output ready" << d->process->readAllStandardError(); 0159 } 0160 0161 } 0162 0163 // kate: indent-width 4; replace-tabs on;