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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
|
/* This file is part of the KDE project
Copyright (C) 2001, 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.
*/
#include <tqdom.h>
#include <tqpainter.h>
#include <tqwmatrix.h>
#include <tqregexp.h>
#include <KoPoint.h>
#include <KoRect.h>
#include <KoUnit.h>
#include <KoStore.h>
#include <KoXmlWriter.h>
#include <KoXmlNS.h>
#include <KoGenStyles.h>
#include "vcomposite.h"
#include "vcomposite_iface.h"
#include "vfill.h"
#include "vpainter.h"
#include "vsegment.h"
#include "vstroke.h"
#include "vvisitor.h"
#include "vpath.h"
#include "commands/vtransformcmd.h"
#include "vdocument.h"
#include <kdebug.h>
VPath::VPath( VObject* tqparent, VState state )
: VObject( tqparent, state ), m_fillRule( winding )
{
m_paths.setAutoDelete( true );
// add an initial path:
m_paths.append( new VSubpath( this ) );
// we need a stroke for boundingBox() at anytime:
m_stroke = new VStroke( this );
m_fill = new VFill();
m_drawCenterNode = false;
}
VPath::VPath( const VPath& composite )
: VObject( composite ), SVGPathParser()
{
m_paths.setAutoDelete( true );
VSubpath* path;
VSubpathListIterator itr( composite.m_paths );
for( itr.toFirst(); itr.current(); ++itr )
{
path = itr.current()->clone();
path->setParent( this );
m_paths.append( path );
}
if ( composite.stroke() )
setStroke( *composite.stroke() );
if ( composite.fill() )
setFill( *composite.fill() );
m_drawCenterNode = false;
m_fillRule = composite.m_fillRule;
m_matrix = composite.m_matrix;
}
VPath::~VPath()
{
}
DCOPObject* VPath::dcopObject()
{
if ( !m_dcop )
m_dcop = new VPathIface( this );
return m_dcop;
}
void
VPath::draw( VPainter* painter, const KoRect *rect ) const
{
if(
state() == deleted ||
state() == hidden ||
state() == hidden_locked )
{
return;
}
if( rect && !rect->intersects( boundingBox() ) )
return;
painter->save();
VSubpathListIterator itr( m_paths );
// draw simplistic contour:
if( state() == edit )
{
for( itr.toFirst(); itr.current(); ++itr )
{
if( !itr.current()->isEmpty() )
{
painter->newPath();
painter->setRasterOp( TQt::XorROP );
painter->setPen( TQt::yellow );
painter->setBrush( TQt::NoBrush );
VSubpathIterator jtr( *( itr.current() ) );
for( ; jtr.current(); ++jtr )
{
jtr.current()->draw( painter );
}
painter->strokePath();
}
}
}
else if( state() != edit )
{
// paint fill:
painter->newPath();
painter->setFillRule( m_fillRule );
for( itr.toFirst(); itr.current(); ++itr )
{
if( !itr.current()->isEmpty() )
{
VSubpathIterator jtr( *( itr.current() ) );
for( ; jtr.current(); ++jtr )
{
jtr.current()->draw( painter );
}
}
}
painter->setRasterOp( TQt::CopyROP );
painter->setPen( TQt::NoPen );
painter->setBrush( *fill() );
painter->fillPath();
// draw stroke:
painter->setPen( *stroke() );
painter->setBrush( TQt::NoBrush );
painter->strokePath();
}
painter->restore();
}
const KoPoint&
VPath::currentPoint() const
{
return m_paths.getLast()->currentPoint();
}
bool
VPath::moveTo( const KoPoint& p )
{
// Append a new subpath if current subpath is not empty.
if( !m_paths.getLast()->isEmpty() )
{
VSubpath* path = new VSubpath( this );
m_paths.append( path );
}
return m_paths.getLast()->moveTo( p );
}
bool
VPath::lineTo( const KoPoint& p )
{
return m_paths.getLast()->lineTo( p );
}
bool
VPath::curveTo(
const KoPoint& p1, const KoPoint& p2, const KoPoint& p3 )
{
return m_paths.getLast()->curveTo( p1, p2, p3 );
}
bool
VPath::curve1To( const KoPoint& p2, const KoPoint& p3 )
{
return m_paths.getLast()->curve1To( p2, p3 );
}
bool
VPath::curve2To( const KoPoint& p1, const KoPoint& p3 )
{
return m_paths.getLast()->curve2To( p1, p3 );
}
bool
VPath::arcTo( const KoPoint& p1, const KoPoint& p2, const double r )
{
return m_paths.getLast()->arcTo( p1, p2, r );
}
void
VPath::close()
{
m_paths.getLast()->close();
// Append a new subpath.
VSubpath* path = new VSubpath( this );
path->moveTo( currentPoint() );
m_paths.append( path );
}
bool
VPath::isClosed() const
{
return m_paths.getLast()->isEmpty() || m_paths.getLast()->isClosed();
}
void
VPath::combine( const VPath& composite )
{
VSubpathListIterator itr( composite.m_paths );
for( ; itr.current(); ++itr )
{
combinePath( *( itr.current() ) );
}
}
void
VPath::combinePath( const VSubpath& path )
{
VSubpath* p = path.clone();
p->setParent( this );
// TODO: do complex inside tests instead:
// Make new segments clock wise oriented:
m_paths.append( p );
m_fillRule = fillMode();
}
bool
VPath::pointIsInside( const KoPoint& p ) const
{
// Check if point is inside boundingbox.
if( !boundingBox().contains( p ) )
return false;
VSubpathListIterator itr( m_paths );
for( itr.toFirst(); itr.current(); ++itr )
{
if( itr.current()->pointIsInside( p ) )
return true;
}
return false;
}
bool
VPath::intersects( const VSegment& segment ) const
{
// Check if boundingboxes intersect.
if( !boundingBox().intersects( segment.boundingBox() ) )
return false;
VSubpathListIterator itr( m_paths );
for( itr.toFirst(); itr.current(); ++itr )
{
if( itr.current()->intersects( segment ) )
return true;
}
return false;
}
VFillRule
VPath::fillMode() const
{
return ( m_paths.count() > 1 ) ? evenOdd : winding;
}
const KoRect&
VPath::boundingBox() const
{
if( m_boundingBoxIsInvalid )
{
VSubpathListIterator itr( m_paths );
itr.toFirst();
m_boundingBox = itr.current() ? itr.current()->boundingBox() : KoRect();
for( ++itr; itr.current(); ++itr )
m_boundingBox |= itr.current()->boundingBox();
if( !m_boundingBox.isNull() )
{
// take line width into account:
m_boundingBox.setCoords(
m_boundingBox.left() - 0.5 * stroke()->lineWidth(),
m_boundingBox.top() - 0.5 * stroke()->lineWidth(),
m_boundingBox.right() + 0.5 * stroke()->lineWidth(),
m_boundingBox.bottom() + 0.5 * stroke()->lineWidth() );
}
m_boundingBoxIsInvalid = false;
}
return m_boundingBox;
}
VPath*
VPath::clone() const
{
return new VPath( *this );
}
void
VPath::save( TQDomElement& element ) const
{
if( state() != deleted )
{
TQDomElement me = element.ownerDocument().createElement( "PATH" );
element.appendChild( me );
VObject::save( me );
TQString d;
saveSvgPath( d );
me.setAttribute( "d", d );
//writeTransform( me );
// save fill rule if necessary:
if( !( m_fillRule == evenOdd ) )
me.setAttribute( "fillRule", m_fillRule );
}
}
void
VPath::saveOasis( KoStore *store, KoXmlWriter *docWriter, KoGenStyles &mainStyles, int &index ) const
{
if( state() != deleted )
{
docWriter->startElement( "draw:path" );
TQString d;
saveSvgPath( d );
docWriter->addAttribute( "svg:d", d );
double x = boundingBox().x();
double y = boundingBox().y();
double w = boundingBox().width();
double h = boundingBox().height();
docWriter->addAttribute( "svg:viewBox", TQString( "%1 %2 %3 %4" ).tqarg( x ).tqarg( y ).tqarg( w ).tqarg( h ) );
docWriter->addAttributePt( "svg:x", x );
docWriter->addAttributePt( "svg:y", y );
docWriter->addAttributePt( "svg:width", w );
docWriter->addAttributePt( "svg:height", h );
VObject::saveOasis( store, docWriter, mainStyles, index );
TQWMatrix tmpMat;
tmpMat.scale( 1, -1 );
tmpMat.translate( 0, -document()->height() );
TQString transform = buildOasisTransform( tmpMat );
if( !transform.isEmpty() )
docWriter->addAttribute( "draw:transform", transform );
docWriter->endElement();
}
}
void
VPath::saveOasisFill( KoGenStyles &mainStyles, KoGenStyle &stylesobjectauto ) const
{
if( m_fill )
{
TQWMatrix mat;
mat.scale( 1, -1 );
mat.translate( 0, -document()->height() );
// mirror fill before saving
VFill fill( *m_fill );
fill.transform( mat );
fill.saveOasis( mainStyles, stylesobjectauto );
// save fill rule if necessary:
if( !( m_fillRule == evenOdd ) )
stylesobjectauto.addProperty( "svg:fill-rule", "winding" );
}
}
void
VPath::transformByViewbox( const TQDomElement &element, TQString viewbox )
{
if( ! viewbox.isEmpty() )
{
// allow for viewbox def with ',' or whitespace
TQStringList points = TQStringList::split( ' ', viewbox.replace( ',', ' ' ).simplifyWhiteSpace() );
double w = KoUnit::parseValue( element.attributeNS( KoXmlNS::svg, "width", TQString() ) );
double h = KoUnit::parseValue( element.attributeNS( KoXmlNS::svg, "height", TQString() ) );
double x = KoUnit::parseValue( element.attributeNS( KoXmlNS::svg, "x", TQString() ) );
double y = KoUnit::parseValue( element.attributeNS( KoXmlNS::svg, "y", TQString() ) );
TQWMatrix mat;
mat.translate( x-KoUnit::parseValue( points[0] ), y-KoUnit::parseValue( points[1] ) );
mat.scale( w / KoUnit::parseValue( points[2] ) , h / KoUnit::parseValue( points[3] ) );
VTransformCmd cmd( 0L, mat );
cmd.visitVPath( *this );
}
}
bool
VPath::loadOasis( const TQDomElement &element, KoOasisLoadingContext &context )
{
setState( normal );
TQString viewbox;
if( element.localName() == "path" )
{
TQString data = element.attributeNS( KoXmlNS::svg, "d", TQString() );
if( data.length() > 0 )
{
loadSvgPath( data );
}
m_fillRule = element.attributeNS( KoXmlNS::svg, "fill-rule", TQString() ) == "winding" ? winding : evenOdd;
viewbox = element.attributeNS( KoXmlNS::svg, "viewBox", TQString() );
}
else if( element.localName() == "custom-tqshape" )
{
TQDomNodeList list = element.childNodes();
for( uint i = 0; i < list.count(); ++i )
{
if( list.item( i ).isElement() )
{
TQDomElement e = list.item( i ).toElement();
if( e.namespaceURI() != KoXmlNS::draw )
continue;
if( e.localName() == "enhanced-tqgeometry" )
{
TQString data = e.attributeNS( KoXmlNS::draw, "enhanced-path", TQString() );
if( ! data.isEmpty() )
loadSvgPath( data );
viewbox = e.attributeNS( KoXmlNS::svg, "viewBox", TQString() );
}
}
}
}
transformByViewbox( element, viewbox );
TQString trafo = element.attributeNS( KoXmlNS::draw, "transform", TQString() );
if( !trafo.isEmpty() )
transformOasis( trafo );
return VObject::loadOasis( element, context );
}
void
VPath::load( const TQDomElement& element )
{
setState( normal );
VObject::load( element );
TQString data = element.attribute( "d" );
if( data.length() > 0 )
{
loadSvgPath( data );
}
m_fillRule = element.attribute( "fillRule" ) == 0 ? evenOdd : winding;
TQDomNodeList list = element.childNodes();
for( uint i = 0; i < list.count(); ++i )
{
if( list.item( i ).isElement() )
{
TQDomElement child = list.item( i ).toElement();
if( child.tagName() == "PATH" )
{
VSubpath path( this );
path.load( child );
combinePath( path );
}
else
{
VObject::load( child );
}
}
}
TQString trafo = element.attribute( "transform" );
if( !trafo.isEmpty() )
transform( trafo );
}
void
VPath::loadSvgPath( const TQString &d )
{
//TQTime s;s.start();
parseSVG( d, true );
//kdDebug(38000) << "Parsing time : " << s.elapsed() << endl;
}
void
VPath::saveSvgPath( TQString &d ) const
{
// save paths to svg:
VSubpathListIterator itr( m_paths );
for( itr.toFirst(); itr.current(); ++itr )
{
if( !itr.current()->isEmpty() )
itr.current()->saveSvgPath( d );
}
}
void
VPath::svgMoveTo( double x1, double y1, bool )
{
moveTo( KoPoint( x1, y1 ) );
}
void
VPath::svgLineTo( double x1, double y1, bool )
{
lineTo( KoPoint( x1, y1 ) );
}
void
VPath::svgCurveToCubic( double x1, double y1, double x2, double y2, double x, double y, bool )
{
curveTo( KoPoint( x1, y1 ), KoPoint( x2, y2 ), KoPoint( x, y ) );
}
void
VPath::svgClosePath()
{
close();
}
void
VPath::accept( VVisitor& visitor )
{
visitor.visitVPath( *this );
}
void
VPath::transform( const TQString &transform )
{
VTransformCmd cmd( 0L, parseTransform( transform ) );
cmd.visitVPath( *this );
}
void
VPath::transformOasis( const TQString &transform )
{
VTransformCmd cmd( 0L, parseOasisTransform( transform ) );
cmd.visitVPath( *this );
}
TQWMatrix
VPath::parseTransform( const TQString &transform )
{
TQWMatrix result;
// Split string for handling 1 transform statement at a time
TQStringList subtransforms = TQStringList::split(')', transform);
TQStringList::ConstIterator it = subtransforms.begin();
TQStringList::ConstIterator end = subtransforms.end();
for(; it != end; ++it)
{
TQStringList subtransform = TQStringList::split('(', (*it));
subtransform[0] = subtransform[0].stripWhiteSpace().lower();
subtransform[1] = subtransform[1].simplifyWhiteSpace();
TQRegExp reg("[,( ]");
TQStringList params = TQStringList::split(reg, subtransform[1]);
if(subtransform[0].startsWith(";") || subtransform[0].startsWith(","))
subtransform[0] = subtransform[0].right(subtransform[0].length() - 1);
if(subtransform[0] == "rotate")
{
if(params.count() == 3)
{
double x = params[1].toDouble();
double y = params[2].toDouble();
result.translate(x, y);
result.rotate(params[0].toDouble());
result.translate(-x, -y);
}
else
result.rotate(params[0].toDouble());
}
else if(subtransform[0] == "translate")
{
if(params.count() == 2)
result.translate(params[0].toDouble(), params[1].toDouble());
else // Spec : if only one param given, assume 2nd param to be 0
result.translate(params[0].toDouble() , 0);
}
else if(subtransform[0] == "scale")
{
if(params.count() == 2)
result.scale(params[0].toDouble(), params[1].toDouble());
else // Spec : if only one param given, assume uniform scaling
result.scale(params[0].toDouble(), params[0].toDouble());
}
else if(subtransform[0] == "skewx")
result.shear(tan(params[0].toDouble() * VGlobal::pi_180), 0.0F);
else if(subtransform[0] == "skewy")
result.shear(tan(params[0].toDouble() * VGlobal::pi_180), 0.0F);
else if(subtransform[0] == "skewy")
result.shear(0.0F, tan(params[0].toDouble() * VGlobal::pi_180));
else if(subtransform[0] == "matrix")
{
if(params.count() >= 6)
result.setMatrix(params[0].toDouble(), params[1].toDouble(), params[2].toDouble(), params[3].toDouble(), params[4].toDouble(), params[5].toDouble());
}
}
return result;
}
TQWMatrix
VPath::parseOasisTransform( const TQString &transform )
{
TQWMatrix result;
// Split string for handling 1 transform statement at a time
TQStringList subtransforms = TQStringList::split(')', transform);
TQStringList::ConstIterator it = subtransforms.begin();
TQStringList::ConstIterator end = subtransforms.end();
for(; it != end; ++it)
{
TQStringList subtransform = TQStringList::split('(', (*it));
subtransform[0] = subtransform[0].stripWhiteSpace().lower();
subtransform[1] = subtransform[1].simplifyWhiteSpace();
TQRegExp reg("[,( ]");
TQStringList params = TQStringList::split(reg, subtransform[1]);
if(subtransform[0].startsWith(";") || subtransform[0].startsWith(","))
subtransform[0] = subtransform[0].right(subtransform[0].length() - 1);
if(subtransform[0] == "rotate")
{
// TODO find out what oo2 really does when rotating, it seems severly broken
if(params.count() == 3)
{
double x = KoUnit::parseValue( params[1] );
double y = KoUnit::parseValue( params[2] );
result.translate(x, y);
// oo2 rotates by radians
result.rotate( params[0].toDouble()*VGlobal::one_pi_180 );
result.translate(-x, -y);
}
else
{
// oo2 rotates by radians
result.rotate( params[0].toDouble()*VGlobal::one_pi_180 );
}
}
else if(subtransform[0] == "translate")
{
if(params.count() == 2)
{
double x = KoUnit::parseValue( params[0] );
double y = KoUnit::parseValue( params[1] );
result.translate(x, y);
}
else // Spec : if only one param given, assume 2nd param to be 0
result.translate( KoUnit::parseValue( params[0] ) , 0);
}
else if(subtransform[0] == "scale")
{
if(params.count() == 2)
result.scale(params[0].toDouble(), params[1].toDouble());
else // Spec : if only one param given, assume uniform scaling
result.scale(params[0].toDouble(), params[0].toDouble());
}
else if(subtransform[0] == "skewx")
result.shear(tan(params[0].toDouble()), 0.0F);
else if(subtransform[0] == "skewy")
result.shear(tan(params[0].toDouble()), 0.0F);
else if(subtransform[0] == "skewy")
result.shear(0.0F, tan(params[0].toDouble()));
else if(subtransform[0] == "matrix")
{
if(params.count() >= 6)
result.setMatrix(params[0].toDouble(), params[1].toDouble(), params[2].toDouble(), params[3].toDouble(), KoUnit::parseValue( params[4] ), KoUnit::parseValue( params[5] ) );
}
}
return result;
}
TQString
VPath::buildSvgTransform() const
{
return buildSvgTransform( m_matrix );
}
TQString
VPath::buildSvgTransform( const TQWMatrix &mat ) const
{
TQString transform;
if( !mat.isIdentity() )
{
transform = TQString( "matrix(%1, %2, %3, %4, %5, %6)" ).tqarg( mat.m11() )
.tqarg( mat.m12() )
.tqarg( mat.m21() )
.tqarg( mat.m22() )
.tqarg( mat.dx() )
.tqarg( mat.dy() );
}
return transform;
}
TQString
VPath::buildOasisTransform() const
{
return buildSvgTransform( m_matrix );
}
TQString
VPath::buildOasisTransform( const TQWMatrix &mat ) const
{
TQString transform;
if( !mat.isIdentity() )
{
transform = TQString( "matrix(%1, %2, %3, %4, %5pt, %6pt)" ).tqarg( mat.m11() )
.tqarg( mat.m12() )
.tqarg( mat.m21() )
.tqarg( mat.m22() )
.tqarg( mat.dx() )
.tqarg( mat.dy() );
}
return transform;
}
|