wmfimportparser.cpp
1/* This file is part of the KDE project
2 * Copyright (c) 2003 thierry lorthiois (lorthioist@wanadoo.fr)
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 version 2 as published by the Free Software Foundation.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Library General Public License for more details.
12 *
13 * You should have received a copy of the GNU Library General Public License
14 * along with this library; see the file COPYING.LIB. If not, write to
15 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16 * Boston, MA 02110-1301, USA.
17*/
18
19#include <kdebug.h>
20#include <shapes/vellipse.h>
21#include <shapes/vrectangle.h>
22#include <shapes/vpolygon.h>
23#include <core/vstroke.h>
24#include <core/vfill.h>
25
26#include "wmfimportparser.h"
27
28/*
29bug : see motar.wmf
30*/
31
32WMFImportParser::WMFImportParser() : KoWmfRead() {
33}
34
35
36bool WMFImportParser::play( VDocument& doc )
37{
38 mDoc = &doc;
39 mScaleX = mScaleY = 1;
40
41 // Play the wmf file
42 return KoWmfRead::play( );
43}
44
45
46//-----------------------------------------------------------------------------
47// Virtual Painter
48
49bool WMFImportParser::begin() {
50 TQRect bounding = boundingRect();
51
52 mBackgroundMode = TQt::TransparentMode;
53 mCurrentOrg.setX( bounding.left() );
54 mCurrentOrg.setY( bounding.top() );
55
56 if ( isStandard() ) {
57 mDoc->setUnit( KoUnit::U_PT );
58 mDoc->setWidth( bounding.width() );
59 mDoc->setHeight( bounding.height() );
60 }
61 else {
62 // Placeable Wmf store the boundingRect() in pixel and the default DPI
63 // The placeable format doesn't have informations on witch Unit to use
64 // so we choose millimeters by default
65 mDoc->setUnit( KoUnit::U_MM );
66 mDoc->setWidth( INCH_TO_POINT( (double)bounding.width() / defaultDpi() ) );
67 mDoc->setHeight( INCH_TO_POINT( (double)bounding.height() / defaultDpi() ) );
68 }
69 if ( (bounding.width() != 0) && (bounding.height() != 0) ) {
70 mScaleX = mDoc->width() / (double)bounding.width();
71 mScaleY = mDoc->height() / (double)bounding.height();
72 }
73 return true;
74}
75
76
77bool WMFImportParser::end() {
78 return true;
79}
80
81
82void WMFImportParser::save() {
83}
84
85
86void WMFImportParser::restore() {
87}
88
89
90void WMFImportParser::setFont( const TQFont & ) {
91}
92
93
94void WMFImportParser::setPen( const TQPen &pen ) {
95 mPen = pen;
96}
97
98
99const TQPen &WMFImportParser::pen() const {
100 return mPen;
101}
102
103
104void WMFImportParser::setBrush( const TQBrush &brush ) {
105 mBrush = brush;
106}
107
108
109void WMFImportParser::setBackgroundColor( const TQColor &c ) {
110 mBackgroundColor = c;
111}
112
113
114void WMFImportParser::setBackgroundMode( TQt::BGMode mode ) {
115 mBackgroundMode = mode;
116}
117
118
119void WMFImportParser::setRasterOp( TQt::RasterOp ) {
120}
121
122
123void WMFImportParser::setWindowOrg( int left, int top ) {
124 mCurrentOrg.setX( left );
125 mCurrentOrg.setY( top );
126}
127
128
129void WMFImportParser::setWindowExt( int width, int height ) {
130 // the wmf file can change width/height during the drawing
131 if ( (width != 0) && (height != 0) ) {
132 mScaleX = mDoc->width() / (double)width;
133 mScaleY = mDoc->height() / (double)height;
134 }
135}
136
137
138void WMFImportParser::setWorldMatrix( const TQWMatrix &, bool ) {
139}
140
141
142void WMFImportParser::setClipRegion( const TQRegion & ) {
143}
144
145
146TQRegion WMFImportParser::clipRegion() {
147 return mClippingRegion;
148}
149
150
151void WMFImportParser::moveTo( int left, int top ) {
152 mCurrentPoint.setX( left );
153 mCurrentPoint.setY( top );
154}
155
156
157void WMFImportParser::lineTo( int left, int top ) {
158 VPath *line = new VPath( mDoc );
159 line->moveTo( KoPoint( coordX(mCurrentPoint.x()), coordY(mCurrentPoint.y()) ) );
160 line->lineTo( KoPoint( coordX(left), coordY(top) ) );
161 appendPen( *line );
162
163 mDoc->append( line );
164 mCurrentPoint.setX( left );
165 mCurrentPoint.setY( top );
166}
167
168
169void WMFImportParser::drawRect( int left, int top, int width, int height ) {
170 VRectangle *rectangle;
171
172 rectangle = new VRectangle( mDoc, KoPoint( coordX(left), coordY(top) ), scaleW(width), scaleH(height), 0 );
173 appendPen( *rectangle );
174 appendBrush( *rectangle );
175
176 mDoc->append( rectangle );
177}
178
179
180void WMFImportParser::drawRoundRect( int left, int top, int width, int height, int roudw, int ) {
181 VRectangle *rectangle;
182
183 // TODO : round rectangle
184 rectangle = new VRectangle( mDoc, KoPoint( coordX(left), coordY(top) ), scaleW(width), scaleH(height), roudw );
185 appendPen( *rectangle );
186 appendBrush( *rectangle );
187
188 mDoc->append( rectangle );
189}
190
191
192void WMFImportParser::drawEllipse( int left, int top, int width, int height ) {
193 VEllipse *ellipse;
194
195 ellipse = new VEllipse( mDoc, KoPoint( coordX(left), coordY(top+height) ), scaleW(width), scaleH(height) );
196 appendPen( *ellipse );
197 appendBrush( *ellipse );
198
199 mDoc->append( ellipse );
200}
201
202
203void WMFImportParser::drawArc( int x, int y, int w, int h, int aStart, int aLen ) {
204 double start = (aStart * 180) / 2880.0;
205 double end = (aLen * 180) / 2880.0;
206 end += start;
207 VEllipse::VEllipseType type = VEllipse::arc;
208
209 VEllipse *arc = new VEllipse( mDoc, KoPoint( coordX(x), coordY(y+h) ), scaleW(w), scaleH(h), type, start, end );
210 appendPen( *arc );
211
212 mDoc->append( arc );
213}
214
215
216void WMFImportParser::drawPie( int x, int y, int w, int h, int aStart, int aLen ) {
217 double start = (aStart * 180) / 2880.0;
218 double end = (aLen * 180) / 2880.0;
219 end += start;
220 VEllipse::VEllipseType type = VEllipse::cut;
221
222 VEllipse *arc = new VEllipse( mDoc, KoPoint( coordX(x), coordY(y+h) ), scaleW(w), scaleH(h), type, start, end );
223 appendPen( *arc );
224 appendBrush( *arc );
225
226 mDoc->append( arc );
227}
228
229
230void WMFImportParser::drawChord( int x, int y, int w, int h, int aStart, int aLen ) {
231 double start = (aStart * 180) / 2880.0;
232 double end = (aLen * 180) / 2880.0;
233 end += start;
234 VEllipse::VEllipseType type = VEllipse::section;
235
236 VEllipse *arc = new VEllipse( mDoc, KoPoint( coordX(x), coordY(y+h) ), scaleW(w), scaleH(h), type, start, end );
237 appendPen( *arc );
238 appendBrush( *arc );
239
240 mDoc->append( arc );
241}
242
243
244void WMFImportParser::drawPolyline( const TQPointArray &pa ) {
245 VPath *polyline = new VPath( mDoc );
246 appendPen( *polyline );
247 appendPoints( *polyline, pa );
248
249 mDoc->append( polyline );
250}
251
252
253void WMFImportParser::drawPolygon( const TQPointArray &pa, bool ) {
254 VPath *polygon = new VPath( mDoc );
255 appendPen( *polygon );
256 appendBrush( *polygon );
257 appendPoints( *polygon, pa );
258
259 polygon->close();
260 mDoc->append( polygon );
261}
262
263
264void WMFImportParser::drawPolyPolygon( TQPtrList<TQPointArray>& listPa, bool ) {
265 VPath *path = new VPath( mDoc );
266
267 if ( listPa.count() > 0 ) {
268 appendPen( *path );
269 appendBrush( *path );
270 appendPoints( *path, *listPa.first() );
271 path->close();
272
273 while ( listPa.next() ) {
274 VPath *newPath = new VPath( mDoc );
275 appendPoints( *newPath, *listPa.current() );
276 newPath->close();
277 path->combine( *newPath );
278 }
279
280 mDoc->append( path );
281 }
282}
283
284
285void WMFImportParser::drawImage( int , int , const TQImage &, int , int , int , int ) {}
286
287
288void WMFImportParser::drawText( int , int , int , int , int , const TQString& , double ) {}
289
290
291//-----------------------------------------------------------------------------
292// Utilities
293
294void WMFImportParser::appendPen( VObject& obj )
295{
296 VStroke stroke( mDoc );
297 stroke.setLineCap( VStroke::capRound );
298
299 if ( mPen.style() == TQt::NoPen ) {
300 stroke.setType( VStroke::none );
301 }
302 else {
303 TQValueList<float> dashes;
304 stroke.setType( VStroke::solid );
305 switch ( mPen.style() ) {
306 case TQt::DashLine :
307 stroke.dashPattern().setArray( dashes << MM_TO_POINT(3) << MM_TO_POINT(2) );
308 break;
309 case TQt::DotLine :
310 stroke.dashPattern().setArray( dashes << MM_TO_POINT(1) << MM_TO_POINT(1) );
311 break;
312 case TQt::DashDotLine :
313 stroke.dashPattern().setArray( dashes << MM_TO_POINT(3) << MM_TO_POINT(1) << MM_TO_POINT(1) << MM_TO_POINT(1) );
314 break;
315 case TQt::DashDotDotLine :
316 stroke.dashPattern().setArray( dashes << MM_TO_POINT(3) << MM_TO_POINT(1) << MM_TO_POINT(1) << MM_TO_POINT(1) << MM_TO_POINT(1) << MM_TO_POINT(1) );
317 break;
318 default:
319 break;
320 }
321 }
322 stroke.setColor( TQColor(mPen.color()) );
323 double width = mPen.width() * mScaleX;
324 stroke.setLineWidth( ((width < 0.99) ? 1 : width) );
325 obj.setStroke( stroke );
326}
327
328
329void WMFImportParser::appendBrush( VObject& obj )
330{
331 VFill fill( mBackgroundColor );
332 fill.setColor( TQColor(mBrush.color()) );
333
334 switch ( mBrush.style() ) {
335 case TQt::NoBrush :
336 fill.setType( VFill::none );
337 break;
338 case TQt::SolidPattern :
339 fill.setType( VFill::solid );
340 break;
341 case TQt::CustomPattern :
342 // TODO: bitmap pattern brush
343 fill.setType( VFill::solid );
344 //fill.pattern().
345 break;
346 default :
347 // TODO: pattern brush
348 if ( mBackgroundMode == TQt::OpaqueMode ) {
349 fill.setColor( mBackgroundColor );
350 fill.setType( VFill::solid );
351 }
352 else {
353 fill.setType( VFill::none );
354 }
355 break;
356 }
357 obj.setFill( fill );
358}
359
360
361void WMFImportParser::appendPoints(VPath &path, const TQPointArray& pa)
362{
363 // list of point array
364 if ( pa.size() > 0 ) {
365 path.moveTo( KoPoint( coordX(pa.point(0).x()), coordY(pa.point(0).y()) ) );
366 }
367 for ( uint i=1 ; i < pa.size() ; i++ ) {
368 path.lineTo( KoPoint( coordX(pa.point(i).x()), coordY(pa.point(i).y()) ) );
369 }
370}
371
bool play(VDocument &doc)
play WMF file on a VDocument.