20#include <kontourimport.h>
21#include <KoFilterChain.h>
22#include <kgenericfactory.h>
26#include <shapes/vellipse.h>
27#include <shapes/vrectangle.h>
28#include <shapes/vpolygon.h>
29#include <commands/vtransformcmd.h>
30#include <core/vpath.h>
31#include <core/vfill.h>
32#include <core/vstroke.h>
38typedef KGenericFactory<KontourImport, KoFilter> KontourImportFactory;
39K_EXPORT_COMPONENT_FACTORY( libkarbonkontourimport, KontourImportFactory(
"kofficefilters" ) )
41KontourImport::KontourImport(KoFilter *, const
char *, const TQStringList&) :
47KontourImport::~KontourImport()
52KoFilter::ConversionStatus KontourImport::convert(
const TQCString& from,
const TQCString& to)
55 if ( to !=
"application/x-karbon" || ( from !=
"application/x-kontour" && from !=
"application/x-killustrator") )
56 return KoFilter::NotImplemented;
59 KoStoreDevice* inpdev = m_chain->storageFile(
"root", KoStore::Read );
62 kdError(30502) <<
"Unable to open input stream" << endl;
63 return KoFilter::StorageCreationError;
66 inpdoc.setContent( inpdev );
71 KoStoreDevice* out = m_chain->storageFile(
"root", KoStore::Write );
74 kdError(30502) <<
"Unable to open output file!" << endl;
75 return KoFilter::StorageCreationError;
77 TQCString cstring = outdoc.toCString();
78 out->writeBlock( cstring.data(), cstring.length() );
84KontourImport::parseGObject( VObject *
object,
const TQDomElement &e )
86 if( !e.attribute(
"fillstyle" ).isEmpty() )
89 int fillstyle = e.attribute(
"fillstyle" ).toInt();
94 fill.setType( VFill::solid );
96 c.setNamedColor( e.attribute(
"fillcolor" ) );
98 fill.setColor( color );
107 c.setNamedColor( e.attribute(
"gradcolor1" ) );
109 grad.addStop( color, 0.0, 0.5 );
110 c.setNamedColor( e.attribute(
"gradcolor2" ) );
112 grad.addStop( color2, 1.0, 0.5 );
114 KoRect bbox =
object->boundingBox();
115 grad.setOrigin( KoPoint( bbox.left(), bbox.y() ) );
116 grad.setVector( KoPoint( bbox.right(), bbox.y() ) );
117 grad.setType( (VGradient::VGradientType)e.attribute(
"gradstyle" ).toInt() );
118 fill.setType( VFill::grad );
119 fill.gradient() = grad;
123 object->setFill( fill );
125 if( !e.attribute(
"strokecolor" ).isEmpty() )
128 int strokestyle = e.attribute(
"strokestyle" ).toInt();
129 switch( strokestyle )
131 case 0: stroke.setType( VStroke::none );
136 c.setNamedColor( e.attribute(
"strokecolor" ) );
138 stroke.setColor( color );
141 case 2:
case 3:
case 4:
case 5:
144 c.setNamedColor( e.attribute(
"strokecolor" ) );
146 stroke.setColor( color );
148 TQValueList<float> list;
149 switch ( strokestyle )
158 list << 10 << 5 << 1 << 5;
161 list << 10 << 5 << 1 << 5 << 1 << 5;
165 dash.setArray( list );
166 stroke.dashPattern() = dash;
170 float lineWidth = e.attribute(
"linewidth" ).toFloat();
171 stroke.setLineWidth( lineWidth );
172 object->setStroke( stroke );
175 TQDomElement matrix = e.namedItem(
"matrix" ).toElement();
176 TQWMatrix mat( matrix.attribute(
"m11" ).toDouble(),
177 matrix.attribute(
"m12" ).toDouble(),
178 matrix.attribute(
"m21" ).toDouble(),
179 matrix.attribute(
"m22" ).toDouble(),
180 matrix.attribute(
"dx" ).toDouble(),
181 matrix.attribute(
"dy" ).toDouble() );
185 mat.translate( 0, -m_document.height() );
186 VTransformCmd trafo( 0L, mat );
187 trafo.visit( *
object );
192KontourImport::convert()
194 TQDomElement docElem = inpdoc.documentElement();
198 if( docElem.attribute(
"version" ).toInt() == 2 )
201 height = lay.firstChild().namedItem(
"layout" ).toElement().attribute(
"height" ).toDouble();
202 width = lay.firstChild().namedItem(
"layout" ).toElement().attribute(
"width" ).toDouble();
206 lay = docElem.namedItem(
"page" ).toElement();
207 height = lay.firstChild().toElement().attribute(
"height" ).toDouble();
208 width = lay.firstChild().toElement().attribute(
"width" ).toDouble();
211 m_document.setHeight( ( ( height / 72.0 ) * DPI ) );
212 m_document.setWidth( ( ( width / 72.0 ) * DPI ) );
214 parseGroup( lay.firstChild().toElement() );
216 outdoc = m_document.saveXML();
220KontourImport::parseGroup(
const TQDomElement &e )
223 for( ; !b.isNull(); b = b.nextSibling().toElement() )
225 if ( b.tagName() ==
"rectangle" )
227 int x = b.attribute(
"x" ).toInt();
228 int y = b.attribute(
"y" ).toInt();
229 int width = b.attribute(
"width" ).toInt();
230 int height = b.attribute(
"height" ).toInt();
231 VObject *rect =
new VRectangle( 0L, KoPoint( x, height + y ) , width, height );
232 TQDomElement
object = b.namedItem(
"polyline" ).namedItem(
"gobject" ).toElement();
233 parseGObject( rect,
object );
234 m_document.append( rect );
237 if ( b.tagName() ==
"ellipse" )
239 TQDomElement
object = b.namedItem(
"gobject" ).toElement();
240 TQDomElement matrix =
object.namedItem(
"matrix" ).toElement();
245 double left = ( b.attribute(
"x" ).toDouble() + matrix.attribute(
"dx" ).toInt() ) - ( b.attribute(
"rx" ).toDouble() / 2 );
246 double right = left + b.attribute(
"rx" ).toDouble();
247 double top = ( b.attribute(
"y" ).toDouble() + matrix.attribute(
"dy" ).toInt() ) - ( b.attribute(
"ry" ).toDouble() / 2 );
248 double bottom = top + b.attribute(
"ry" ).toDouble();
249 double height = top - bottom;
250 double width = right - left;
252 VObject *ellipse =
new VEllipse( 0L, KoPoint( left, top ), width, height );
253 parseGObject( ellipse,
object );
254 m_document.append( ellipse );
256 else if( b.tagName() ==
"polyline" )
262 TQDomElement point = b.firstChild().toElement();
263 VPath *path =
new VPath( &m_document );
265 x = point.attribute(
"x" ).toDouble();
266 y = point.attribute(
"y" ).toDouble();
267 path->moveTo( KoPoint( x, y ) );
268 point = point.nextSibling().toElement();
269 for( ; point.tagName() !=
"gobject"; point = point.nextSibling().toElement() )
271 x = point.attribute(
"x" ).toDouble();
272 y = point.attribute(
"y" ).toDouble();
273 path->lineTo( KoPoint( x, y ) );
275 parseGObject( path, point );
276 m_document.append( path );
278 else if( b.tagName() ==
"polygon" )
280 TQDomElement point = b.namedItem(
"polyline" ).firstChild().toElement();
281 VPath *path =
new VPath( &m_document );
283 x = point.attribute(
"x" ).toDouble();
284 y = point.attribute(
"y" ).toDouble();
285 path->moveTo( KoPoint( x, y ) );
286 point = point.nextSibling().toElement();
287 for( ; point.tagName() !=
"gobject"; point = point.nextSibling().toElement() )
289 x = point.attribute(
"x" ).toDouble();
290 y = point.attribute(
"y" ).toDouble();
291 path->lineTo( KoPoint( x, y ) );
295 parseGObject( path, point );
296 m_document.append( path );
298 else if( b.tagName() ==
"bezier" )
300 TQDomElement point = b.namedItem(
"polyline" ).firstChild().toElement();
301 VPath *path =
new VPath( &m_document );
303 x = point.attribute(
"x" ).toDouble();
304 y = point.attribute(
"y" ).toDouble();
305 path->moveTo( KoPoint( x, y ) );
306 point = point.nextSibling().toElement();
307 for( ; point.tagName() !=
"gobject"; point = point.nextSibling().toElement() )
309 x = point.attribute(
"x" ).toDouble();
310 y = point.attribute(
"y" ).toDouble();
311 path->lineTo( KoPoint( x, y ) );
313 parseGObject( path, point );
314 m_document.append( path );
316 else if( b.tagName() ==
"group" || b.tagName() ==
"layer" )
318 parseGroup( b.toElement().firstChild().toElement() );
323#include <kontourimport.moc>