summaryrefslogtreecommitdiffstats
path: root/filters/karbon/eps/epsimport.cc
blob: 72bb1334dc293f56839cc14ecd65ee06c7cd3771 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/* This file is part of the KDE project
   Copyright (C) 2002, The Karbon Developers

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/

#include <tqcstring.h>
#include <tqstring.h>
#include <tqfile.h>

#include <kgenericfactory.h>
#include <KoFilter.h>
#include <KoFilterChain.h>
#include <krun.h>
#include <kprocess.h>

#include <kdebug.h>

#include "epsimport.h"
#include "pscommentlexer.h"

class EpsImportFactory : KGenericFactory<EpsImport, KoFilter>
{
public:
	EpsImportFactory( void )
		: KGenericFactory<EpsImport, KoFilter>( "karbonepsimport" )
	{}

protected:
	virtual void setupTranslations( void )
	{
		TDEGlobal::locale()->insertCatalogue( "kofficefilters" );
	}
};

K_EXPORT_COMPONENT_FACTORY( libkarbonepsimport, EpsImportFactory() )

EpsImport::EpsImport( KoFilter*, const char*, const TQStringList& )
	: KoFilter()
{
}

EpsImport::~EpsImport()
{
}

KoFilter::ConversionStatus
EpsImport::convert( const TQCString& from, const TQCString& to )
{
	if(
		to != "application/illustrator" ||
		(
			from != "image/x-eps" &&
			from != "application/postscript" ) )
	{
		return KoFilter::NotImplemented;
	}

	// Copy input filename:
	TQString input = m_chain->inputFile();


	// EPS original bounding box
	int llx = -1, lly = -1, urx = -1, ury = -1;
 	BoundingBoxExtractor extractor;

	TQFile file (input);

	if ( file.open(IO_ReadOnly) )
	{
		extractor.parse (*TQT_TQIODEVICE(&file));
		llx = extractor.llx();
		lly = extractor.lly();
		urx = extractor.urx();
		ury = extractor.ury();
    		file.close();
  	}
	else
		tqDebug ("file could not be opened");

	// sed filter
	TQString sedFilter = TQString ("sed -e \"s/%%BoundingBox: 0 0 612 792/%%BoundingBox: %1 %2 %3 %4/g\"").
            arg(llx).arg(lly).arg(urx).arg(ury);

	// Build ghostscript call to convert ps/eps -> ai:
	TQString command(
		"gs -q -P- -dBATCH -dNOPAUSE -dSAFER -dPARANOIDSAFER -dNODISPLAY ps2ai.ps ");
	command += TDEProcess::quote(input);
	command += " | ";
	command += sedFilter;
	command += " > ";
	command += TDEProcess::quote(m_chain->outputFile());

	tqDebug ("command to execute is (%s)", TQFile::encodeName(command).data());

	// Execute it:
	if( !system( TQFile::encodeName(command)) )
		return KoFilter::OK;
	else
		return KoFilter::StupidError;
}

#include "epsimport.moc"