epsimport.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2002, The Karbon Developers
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18*/
19
20#include <tqcstring.h>
21#include <tqstring.h>
22#include <tqfile.h>
23
24#include <kgenericfactory.h>
25#include <KoFilter.h>
26#include <KoFilterChain.h>
27#include <krun.h>
28#include <kprocess.h>
29
30#include <kdebug.h>
31
32#include "epsimport.h"
33#include "pscommentlexer.h"
34
35class EpsImportFactory : KGenericFactory<EpsImport, KoFilter>
36{
37public:
38 EpsImportFactory( void )
39 : KGenericFactory<EpsImport, KoFilter>( "karbonepsimport" )
40 {}
41
42protected:
43 virtual void setupTranslations( void )
44 {
45 TDEGlobal::locale()->insertCatalogue( "kofficefilters" );
46 }
47};
48
49K_EXPORT_COMPONENT_FACTORY( libkarbonepsimport, EpsImportFactory() )
50
51EpsImport::EpsImport( KoFilter*, const char*, const TQStringList& )
52 : KoFilter()
53{
54}
55
56EpsImport::~EpsImport()
57{
58}
59
60KoFilter::ConversionStatus
61EpsImport::convert( const TQCString& from, const TQCString& to )
62{
63 if(
64 to != "application/illustrator" ||
65 (
66 from != "image/x-eps" &&
67 from != "application/postscript" ) )
68 {
69 return KoFilter::NotImplemented;
70 }
71
72 // Copy input filename:
73 TQString input = m_chain->inputFile();
74
75
76 // EPS original bounding box
77 int llx = -1, lly = -1, urx = -1, ury = -1;
78 BoundingBoxExtractor extractor;
79
80 TQFile file (input);
81
82 if ( file.open(IO_ReadOnly) )
83 {
84 extractor.parse(file);
85 llx = extractor.llx();
86 lly = extractor.lly();
87 urx = extractor.urx();
88 ury = extractor.ury();
89 file.close();
90 }
91 else
92 tqDebug ("file could not be opened");
93
94 // sed filter
95 TQString sedFilter = TQString ("sed -e \"s/%%BoundingBox: 0 0 612 792/%%BoundingBox: %1 %2 %3 %4/g\"").
96 arg(llx).arg(lly).arg(urx).arg(ury);
97
98 // Build ghostscript call to convert ps/eps -> ai:
99 TQString command(
100 "gs -q -P- -dBATCH -dNOPAUSE -dSAFER -dPARANOIDSAFER -dNODISPLAY ps2ai.ps ");
101 command += TDEProcess::quote(input);
102 command += " | ";
103 command += sedFilter;
104 command += " > ";
105 command += TDEProcess::quote(m_chain->outputFile());
106
107 tqDebug ("command to execute is (%s)", TQFile::encodeName(command).data());
108
109 // Execute it:
110 if( !system( TQFile::encodeName(command)) )
111 return KoFilter::OK;
112 else
113 return KoFilter::StupidError;
114}
115
116#include "epsimport.moc"
117