File indexing completed on 2026-04-12 12:12:03

0001 /* This file is part of the KDE project
0002    SPDX-FileCopyrightText: 2002 The Karbon Developers
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "EpsImport.h"
0008 #include "PsCommentLexer.h"
0009 
0010 #include <QString>
0011 #include <QFile>
0012 #include <QDebug>
0013 #include <QLoggingCategory>
0014 
0015 #include <KoFilter.h>
0016 #include <KoFilterChain.h>
0017 
0018 #include <kpluginfactory.h>
0019 #include <kshell.h>
0020 
0021 K_PLUGIN_FACTORY_WITH_JSON(EpsImportFactory, "calligra_filter_eps2svgai.json",
0022                            registerPlugin<EpsImport>();)
0023 
0024 const QLoggingCategory &EPS_LOG()
0025 {
0026     static const QLoggingCategory category("calligra.filter.eps2svgai");
0027     return category;
0028 }
0029 
0030 #define debugEps qCDebug(EPS_LOG)
0031 #define warnEps qCWarning(EPS_LOG)
0032 #define errorEps qCCritical(EPS_LOG)
0033 
0034 EpsImport::EpsImport(QObject*parent, const QVariantList&)
0035         : KoFilter(parent)
0036 {
0037     debugEps << "###   ###   EPS Import Filter";
0038 }
0039 
0040 EpsImport::~EpsImport()
0041 {
0042 }
0043 
0044 KoFilter::ConversionStatus EpsImport::convert(const QByteArray& from, const QByteArray& to)
0045 {
0046     if (from != "image/x-eps" &&
0047             from != "image/eps" &&
0048             from != "application/eps" &&
0049             from != "application/x-eps" &&
0050             from != "application/postscript") {
0051         return KoFilter::NotImplemented;
0052     }
0053 
0054     if (to == "image/svg+xml") {
0055         // Copy filenames
0056         QString input = m_chain->inputFile();
0057         QString output = m_chain->outputFile();
0058 
0059         QString command = QLatin1String("pstoedit -f plot-svg ") +
0060                 KShell::quoteArg(input) + QLatin1Char(' ') +
0061                 KShell::quoteArg(output);
0062 
0063         debugEps << "command to execute is (%s)" << QFile::encodeName(command).data() ;
0064 
0065         // Execute it:
0066         if (! system(QFile::encodeName(command)))
0067             return KoFilter::OK;
0068         else
0069             return KoFilter::StupidError;
0070     }
0071     if (to == "application/illustrator") {
0072 
0073         // Copy input filename:
0074         QString input = m_chain->inputFile();
0075 
0076         // EPS original bounding box
0077         int llx = -1, lly = -1, urx = -1, ury = -1;
0078         BoundingBoxExtractor extractor;
0079 
0080         QFile file(input);
0081 
0082         if (file.open(QIODevice::ReadOnly)) {
0083             extractor.parse(file);
0084             llx = extractor.llx();
0085             lly = extractor.lly();
0086             urx = extractor.urx();
0087             ury = extractor.ury();
0088             file.close();
0089         } else
0090             debugEps << "file could not be opened";
0091 
0092         // sed filter
0093         QString sedFilter = QString("sed -e \"s/%%BoundingBox: 0 0 612 792/%%BoundingBox: %1 %2 %3 %4/g\"").
0094                             arg(llx).arg(lly).arg(urx).arg(ury);
0095 
0096         // Build ghostscript call to convert ps/eps -> ai:
0097         QString command  = QLatin1String(
0098             "gs -q -P- -dBATCH -dNOPAUSE -dSAFER -dPARANOIDSAFER -dNODISPLAY ps2ai.ps ") +
0099             KShell::quoteArg(input) +
0100             " | " +
0101             sedFilter +
0102             " > " +
0103             KShell::quoteArg(m_chain->outputFile());
0104 
0105         debugEps << "command to execute is: " << QFile::encodeName(command);
0106 
0107         // Execute it:
0108         if (!system(QFile::encodeName(command)))
0109             return KoFilter::OK;
0110         else
0111             return KoFilter::StupidError;
0112     }
0113 
0114     return KoFilter::NotImplemented;
0115 }
0116 
0117 #include "EpsImport.moc"
0118