1
// -*- Mode: c++; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 4; -*-
2
/* This file is part of the KDE project
3
Copyright (C) 2001 Toshitaka Fujioka <fujioka@kde.org>
4
Copyright (C) 2005-2006 Thorsten Zachmann <zachmann@kde.org>
6
This library is free software; you can redistribute it and/or
7
modify it under the terms of the GNU Library General Public
8
License as published by the Free Software Foundation; either
9
version 2 of the License, or (at your option) any later version.
11
This library is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
Library General Public License for more details.
16
You should have received a copy of the GNU Library General Public License
17
along with this library; see the file COPYING.LIB. If not, write to
18
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19
* Boston, MA 02110-1301, USA.
22
#include "KPrBezierCurveObject.h"
23
#include "KPrCubicBezierCurveObjectIface.h"
24
#include "KPrQuadricBezierCurveObjectIface.h"
26
#include <KoTextZoomHandler.h>
30
#include "KoPointArray.h"
36
KPrBezierCurveObject::KPrBezierCurveObject()
41
KPrBezierCurveObject::KPrBezierCurveObject( const KoPointArray &_controlPoints,
42
const KoPointArray &_allPoints,
43
const KoSize &_size, const KoPen &_pen,
44
LineEnd _lineBegin, LineEnd _lineEnd )
45
: KPrPointObject( _pen, _lineBegin, _lineEnd )
47
points = KoPointArray( _controlPoints );
48
allPoints = KoPointArray( _allPoints );
54
KPrBezierCurveObject &KPrBezierCurveObject::operator=( const KPrBezierCurveObject & )
60
bool KPrBezierCurveObject::saveOasisObjectAttributes( KPOasisSaveContext &sc ) const
62
KoRect rect( getRect() );
63
sc.xmlWriter.addAttribute("svg:viewBox", QString( "0 0 %1 %2" ).arg( int( rect.width() * 100 ) )
64
.arg( int( rect.height() * 100 ) ) );
66
unsigned int pointCount = points.count();
70
d += QString( "M%1 %2" ).arg( int( points.at(pos).x() * 100 ) )
71
.arg( int( points.at(pos).y() * 100 ) );
73
while ( pos + 4 <= pointCount )
75
d += QString( "C%1 %2 %3 %4 %5 %6" ).arg( int( points.at( pos + 2 ).x() * 100 ) )
76
.arg( int( points.at( pos + 2 ).y() * 100 ) )
77
.arg( int( points.at( pos + 3 ).x() * 100 ) )
78
.arg( int( points.at( pos + 3 ).y() * 100 ) )
79
.arg( int( points.at( pos + 1 ).x() * 100 ) )
80
.arg( int( points.at( pos + 1 ).y() * 100 ) );
84
if ( pos < pointCount )
86
d += QString( "L%1 %2" ).arg( int( points.at( pos + 1 ).x() * 100 ) )
87
.arg( int( points.at( pos + 1 ).y() * 100 ) );
90
sc.xmlWriter.addAttribute( "svg:d", d );
96
const char * KPrBezierCurveObject::getOasisElementName() const
102
void KPrBezierCurveObject::loadOasis( const QDomElement &element, KoOasisContext & context, KPrLoadingInfo* info )
104
kdDebug(33001) << "KPrBezierCurveObject::loadOasis" << endl;
105
KPrPointObject::loadOasis( element, context, info );
107
allPoints = bezier2polyline( points );
110
loadOasisMarker( context );
113
QDomDocumentFragment KPrBezierCurveObject::save( QDomDocument& doc, double offset )
115
return KPrPointObject::save( doc,offset );
118
double KPrBezierCurveObject::load(const QDomElement &element)
120
double offset = KPrPointObject::load( element );
122
allPoints = bezier2polyline( points );
127
void KPrBezierCurveObject::updatePoints( double _fx, double _fy )
129
KPrPointObject::updatePoints( _fx, _fy );
132
KoPointArray tmpPoints;
133
KoPointArray::ConstIterator it;
134
for ( it = allPoints.begin(); it != allPoints.end(); ++it ) {
135
KoPoint point = (*it);
136
double tmpX = point.x() * _fx;
137
double tmpY = point.y() * _fy;
139
tmpPoints.putPoints( index, 1, tmpX,tmpY );
142
allPoints = tmpPoints;
145
KoPointArray KPrBezierCurveObject::bezier2polyline( const KoPointArray &bezierPoints )
147
if ( bezierPoints.isNull() )
150
KoPointArray _points( bezierPoints );
151
KoPointArray _allPoints;
152
unsigned int pointCount = _points.count();
154
if ( pointCount == 2 ) // line
156
_allPoints = _points;
158
else // cubic bezier curve
160
KoPointArray tmpPointArray;
161
unsigned int _tmpIndex = 0;
162
unsigned int count = 0;
163
while ( count < pointCount )
165
if ( pointCount >= ( count + 4 ) ) // for cubic bezier curve
167
double _firstX = _points.at( count ).x();
168
double _firstY = _points.at( count ).y();
170
double _fourthX = _points.at( count + 1 ).x();
171
double _fourthY = _points.at( count + 1 ).y();
173
double _secondX = _points.at( count + 2 ).x();
174
double _secondY = _points.at( count + 2 ).y();
176
double _thirdX = _points.at( count + 3 ).x();
177
double _thirdY = _points.at( count + 3 ).y();
179
KoPointArray bezierPoint;
180
bezierPoint.putPoints( 0, 4, _firstX,_firstY, _secondX,_secondY,
181
_thirdX,_thirdY, _fourthX,_fourthY );
182
bezierPoint = bezierPoint.cubicBezier();
184
KoPointArray::ConstIterator it;
185
for ( it = bezierPoint.begin(); it != bezierPoint.end(); ++it )
187
KoPoint _point = (*it);
188
tmpPointArray.putPoints( _tmpIndex, 1, _point.x(), _point.y() );
196
double _x1 = _points.at( count ).x();
197
double _y1 = _points.at( count ).y();
199
double _x2 = _points.at( count + 1 ).x();
200
double _y2 = _points.at( count + 1 ).y();
202
tmpPointArray.putPoints( _tmpIndex, 2, _x1,_y1, _x2,_y2 );
208
_allPoints = tmpPointArray;
214
void KPrBezierCurveObject::flip(bool horizontal )
216
KPrPointObject::flip( horizontal );
218
KoPointArray tmpPoints;
222
KoPointArray::ConstIterator it;
223
double horiz = getSize().height()/2;
224
for ( it = allPoints.begin(); it != allPoints.end(); ++it )
226
KoPoint point = (*it);
227
if ( point.y()> horiz )
228
tmpPoints.putPoints( index, 1, point.x(),point.y()- 2*(point.y()-horiz) );
230
tmpPoints.putPoints( index, 1, point.x(),point.y()+ 2*(horiz - point.y()) );
236
KoPointArray::ConstIterator it;
237
double vert = getSize().width()/2;
238
for ( it = allPoints.begin(); it != allPoints.end(); ++it )
240
KoPoint point = (*it);
241
if ( point.y()> vert )
242
tmpPoints.putPoints( index, 1, point.x()- 2*(point.x()-vert), point.y() );
244
tmpPoints.putPoints( index, 1, point.x()+ 2*(vert - point.x()),point.y() );
249
allPoints = tmpPoints;
253
KoPointArray KPrBezierCurveObject::getDrawingPoints() const
259
KPrCubicBezierCurveObject::KPrCubicBezierCurveObject()
260
: KPrBezierCurveObject()
265
KPrCubicBezierCurveObject::KPrCubicBezierCurveObject( const KoPointArray &_controlPoints,
266
const KoPointArray &_allPoints,
267
const KoSize & _size, const KoPen &_pen,
268
LineEnd _lineBegin, LineEnd _lineEnd )
269
: KPrBezierCurveObject( _controlPoints, _allPoints, _size, _pen, _lineBegin, _lineEnd )
274
DCOPObject* KPrCubicBezierCurveObject::dcopObject()
277
dcop = new KPrCubicBezierCurveObjectIface( this );
282
KPrQuadricBezierCurveObject::KPrQuadricBezierCurveObject()
283
: KPrBezierCurveObject()
288
KPrQuadricBezierCurveObject::KPrQuadricBezierCurveObject( const KoPointArray &_controlPoints,
289
const KoPointArray &_allPoints,
290
const KoSize & _size, const KoPen &_pen,
291
LineEnd _lineBegin, LineEnd _lineEnd )
292
: KPrBezierCurveObject( _controlPoints, _allPoints, _size, _pen, _lineBegin, _lineEnd )
297
DCOPObject* KPrQuadricBezierCurveObject::dcopObject()
300
dcop = new KPrQuadricBezierCurveObjectIface( this );