summaryrefslogtreecommitdiffstats
path: root/filters/karbon/xaml/xamlexport.cc
blob: e1599a8cf7c16e66b4517820a74d237d76b38083 (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/* This file is part of the KDE project
   Copyright (C) 2002, 2003 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.
*/

// based on the SVG exporter.  Not intended for public release
// Microsoft WVG renamed to XAML Graphics.  Worry about that later.  

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

#include <kgenericfactory.h>
#include <KoFilter.h>
#include <KoFilterChain.h>
#include <KoStore.h>

#include "xamlexport.h"
#include "vcolor.h"
#include "vcomposite.h"
#include "vdashpattern.h"
#include "vdocument.h"
#include "vfill.h"
#include "vgradient.h"
#include "vgroup.h"
#include "vlayer.h"
#include "vpath.h"
#include "vsegment.h"
#include "vselection.h"
#include "vstroke.h"
//#include "vtext.h"  // TODO Convert Text to Paths for basic export.  Not our problem.  
// TODO inline Images?

#include <kdebug.h>


typedef KGenericFactory<XAMLExport, KoFilter> XAMLExportFactory;
K_EXPORT_COMPONENT_FACTORY( libkarbonxamlexport, XAMLExportFactory( "kofficefilters" ) )


XAMLExport::XAMLExport( KoFilter*, const char*, const TQStringList& )
	: KoFilter()
{
	m_gc.setAutoDelete( true );
}

KoFilter::ConversionStatus
XAMLExport::convert( const TQCString& from, const TQCString& to )
{
	// TODO: ???
	if ( to != "image/wvg+xml" || from != "application/x-karbon" )
	{
		return KoFilter::NotImplemented;
	}

	KoStoreDevice* storeIn = m_chain->storageFile( "root", KoStore::Read );
	if( !storeIn )
		return KoFilter::StupidError;

	TQFile fileOut( m_chain->outputFile() );
	if( !fileOut.open( IO_WriteOnly ) )
	{
		delete storeIn;
		return KoFilter::StupidError;
	}

	TQDomDocument domIn;
	domIn.setContent( storeIn );
	TQDomElement docNode = domIn.documentElement();

	m_stream = new TQTextStream( &fileOut );
	TQString body;
	m_body = new TQTextStream( &body, IO_ReadWrite );
	TQString defs;
	m_defs = new TQTextStream( &defs, IO_ReadWrite );


	// load the document and export it:
	VDocument doc;
	doc.load( docNode );
	doc.accept( *this );

	*m_stream << defs;
	*m_stream << body;

	fileOut.close();

	delete m_stream;
	delete m_defs;
	delete m_body;

	return KoFilter::OK;
}

void
XAMLExport::visitVDocument( VDocument& document )
{
	// select all objects:
	document.selection()->append();

	// get the bounding box of the page
	KoRect rect( 0, 0, document.width(), document.height() );

	// standard header:
	*m_defs <<
		"<?xml version=\"1.0\" ?>\n" <<
		/* "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 20010904//EN\" " <<* */
		// "\"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd\">"
	/*<<*/ endl;

	// Add one line comment to identify Content Creator, 
	// probably remove this later
	// TODO: schemas
	// http://schemas.microsoft.com/winfx/avalon/2005 
	// http://schemas.microsoft.com/2003/xaml  
	// need to mention defs too Defenitions namespace xmlns:def="Definition"	
	*m_defs <<
		"<!-- Generator: Karbon14 WVG XAML Graphics export filter  $VERSION/$DATE.  -->" << endl;
	*m_defs <<
		"<Canvas xmlns=\"http://schemas.microsoft.com/winfx/avalon/2005\" Width=\"" << rect.width() << 
		"Height=\"" << rect.height() << "\">" << endl;
	*m_defs << "<Canvas.Resources>" << endl;

	// bleuch: this is horrible, do something about it TODO
	// Microsoft Acrylic has a transform group just like this
	*m_body << "<Transform=\"scale(1, -1) Translate(0, -" << rect.height() << ")\">" << endl;

	// we dont need the selection anymore:
	document.selection()->clear();

	// set up gc
	XAMLGraphicsContext *gc = new XAMLGraphicsContext;
	m_gc.push( gc );

	// export layers:
	VVisitor::visitVDocument( document );

	// end tag:
	*m_body << "</Canvas>" << endl;
	*m_defs << "</Canvas.Resources>" << endl;
	*m_body << "</Canvas>" << endl;
}

TQString
XAMLExport::getID( VObject *obj )
{
	if( obj && !obj->name().isEmpty() )
		return TQString( " Name=\"%1\"" ).arg( obj->name() );
	return TQString();
}

// which markup to use?  Group or Canvas?
// for now assume Group will work.  TODO: Test properly!
void
XAMLExport::visitVGroup( VGroup& group )
{
	*m_body << "<Canvas" << getID( &group ) << ">" << endl;
	VVisitor::visitVGroup( group );
	*m_body << "</Canvas>" << endl;
}

void
XAMLExport::visitVPath( VPath& composite )
{
	*m_body << "<Path" << getID( &composite );

	VVisitor::visitVPath( composite );

	getFill( *( composite.fill() ) );
	getStroke( *( composite.stroke() ) );

	TQString d;
	composite.saveSvgPath( d );
	*m_body << " Data=\"" << d << "\" ";

	if( composite.fillRule() != m_gc.current()->fillRule )
	{
		if( composite.fillRule() == evenOdd )
			*m_body << " FillRule=\"EvenOdd\"";
		else
			*m_body << " FillRule=\"NonZero\"";
	}

	*m_body << " />" << endl;
}

void
XAMLExport::visitVSubpath( VSubpath& )
{
}

TQString createUID()
{
	static unsigned int nr = 0;

	return "defitem" + TQString().setNum( nr++ );
}

void
XAMLExport::getColorStops( const TQPtrVector<VColorStop> &colorStops )
{
	for( unsigned int i = 0; i < colorStops.count() ; i++ )
	{
		*m_defs << "<GradientStop Color=\"";
		getHexColor( m_defs, colorStops.at( i )->color );
		*m_defs << "\" Offset=\"" << TQString().setNum( colorStops.at( i )->rampPoint );
		//  XAML uses ARGB values and other methods such as masks for Transparency/Opacity    #  aa  rrggbb  
		// *m_defs << "\" stop-opacity=\"" << colorStops.at( i )->color.opacity() << "\"" << " />" << endl;
		// Maybe this only applies to gradients, need to check.  
	}
}

void
XAMLExport::getGradient( const VGradient& grad )
{
	TQString uid = createUID();
	if( grad.type() == VGradient::linear )
	{
		// do linear grad
		*m_defs << "<LinearGradientBrush id=\"" << uid << "\" ";
		*m_defs << "GradientUnits=\"UserSpaceOnUse\" ";
		*m_defs << "StartPoint=\"" << grad.origin().x() << ",";
		*m_defs << grad.origin().y() << "\" ";
		*m_defs << "EndPoint=\"" << grad.vector().x() << ",";
		*m_defs << grad.vector().y() << "\" ";
		if( grad.repeatMethod() == VGradient::reflect )
			*m_defs << "SpreadMethod=\"Reflect\" ";
		else if( grad.repeatMethod() == VGradient::repeat )
			*m_defs << "SpreadMethod=\"Repeat\" ";
		*m_defs << ">" << endl;

		// color stops
		getColorStops( grad.colorStops() );

		*m_defs << "</LinearGradientBrush>" << endl;
		*m_body << "url(#" << uid << ")";
	}
	else if( grad.type() == VGradient::radial )
	{
		// do radial grad
		*m_defs << "<RadialGradientBrush Name=\"" << uid << "\" ";
		// *m_defs << "gradientUnits=\"userSpaceOnUse\" "; // Absolute?
		*m_defs << "Center=\"" << grad.origin().x() << ",";
		*m_defs << grad.origin().y() << "\" ";
		// Gradient Origin also known as Focus
		*m_defs << "GradientOrigin=\"" << grad.focalPoint().x() << ",";
		*m_defs << grad.focalPoint().y() << "\" ";
		double r = sqrt( pow( grad.vector().x() - grad.origin().x(), 2 ) + pow( grad.vector().y() - grad.origin().y(), 2 ) );
		*m_defs << "Radius=\"" << TQString().setNum( r ) << "\" ";
		if( grad.repeatMethod() == VGradient::reflect )
			*m_defs << "SpreadMethod=\"Reflect\" ";
		else if( grad.repeatMethod() == VGradient::repeat )
			*m_defs << "SpreadMethod=\"Repeat\" ";
		*m_defs << ">" << endl;

		// color stops
		getColorStops( grad.colorStops() );

		*m_defs << "</RadialGradientBrush>" << endl;
		*m_body << "url(#" << uid << ")";
	}
}

void
XAMLExport::getFill( const VFill& fill )
{
	*m_body << " Fill=\"";
	if( fill.type() == VFill::none )
		*m_body << "none";
	else if( fill.type() == VFill::grad )
		getGradient( fill.gradient() );
	else
		getHexColor( m_body, fill.color() );
	*m_body << "\"";

	if( fill.color().opacity() != m_gc.current()->fill.color().opacity() )
		*m_body << " FillOpacity=\"" << fill.color().opacity() << "\"";
}

void
XAMLExport::getStroke( const VStroke& stroke )
{
	if( stroke.type() != m_gc.current()->stroke.type() )
	{
		*m_body << " Stroke=\"";
		if( stroke.type() == VStroke::none )
			*m_body << "None";
		else if( stroke.type() == VStroke::grad )
			getGradient( stroke.gradient() );
		else
			getHexColor( m_body, stroke.color() );
		*m_body << "\"";
	}

	if( stroke.color().opacity() != m_gc.current()->stroke.color().opacity() )
		*m_body << " StrokeOpacity=\"" << stroke.color().opacity() << "\"";

	if( stroke.lineWidth() != m_gc.current()->stroke.lineWidth() )
		*m_body << " StrokeThickness=\"" << stroke.lineWidth() << "\"";

	if( stroke.lineCap() != m_gc.current()->stroke.lineCap() )
	{
		if( stroke.lineCap() == VStroke::capButt )
			*m_body << " StrokeLineCap=\"Butt\"";
		else if( stroke.lineCap() == VStroke::capRound )
			*m_body << " StrokeLineCap=\"round\"";
		else if( stroke.lineCap() == VStroke::capSquare )
			*m_body << " StrokeLineCap=\"square\"";
	}

	if( stroke.lineJoin() != m_gc.current()->stroke.lineJoin() )
	{
		if( stroke.lineJoin() == VStroke::joinMiter )
		{
			*m_body << " StrokeLineJoin=\"Miter\"";
			*m_body << " StrokeMiterLimit=\"" << stroke.miterLimit() << "\"";
		}
		else if( stroke.lineJoin() == VStroke::joinRound )
			*m_body << " StrokeLineJoin=\"Round\"";
		else if( stroke.lineJoin() == VStroke::joinBevel )
				*m_body << " StrokeLineJoin=\"Bevel\"";
	}

	// dash
	if( stroke.dashPattern().array().count() > 0 )
	{
		*m_body << " StrokeDashOffset=\"" << stroke.dashPattern().offset() << "\"";
		*m_body << " StrokeDashArray=\" ";

		TQValueListConstIterator<float> itr;
		for(itr = stroke.dashPattern().array().begin(); itr != stroke.dashPattern().array().end(); ++itr )
		{
			*m_body << *itr << " ";
		}
		*m_body << "\"";
	}
}

void
XAMLExport::getHexColor( TQTextStream *stream, const VColor& color )
{
	// Convert the various color-spaces to hex

	TQString Output;

	VColor copy( color );
	copy.setColorSpace( VColor::rgb );

	Output.sprintf( "#%02x%02x%02x", int( copy[0] * 255.0 ), int( copy[1] * 255.0 ), int( copy[2] * 255.0 ) );

	*stream << Output;
}

#include "xamlexport.moc"