summaryrefslogtreecommitdiffstats
path: root/kpresenter/KPrBezierCurveObject.cpp
blob: bd148154e4ab1b3f26c27d4abc019161a0743a1b (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
/* This file is part of the KDE project
   Copyright (C) 2001 Toshitaka Fujioka <fujioka@kde.org>
   Copyright (C) 2005-2006 Thorsten Zachmann <zachmann@kde.org>

   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 "KPrBezierCurveObject.h"
#include "KPrCubicBezierCurveObjectIface.h"
#include "KPrQuadricBezierCurveObjectIface.h"
#include "KPrUtils.h"
#include <KoTextZoomHandler.h>
#include <tqpainter.h>
#include <tqwmatrix.h>
#include <tqdom.h>
#include "KoPointArray.h"
#include <kdebug.h>

#include <math.h>
using namespace std;

KPrBezierCurveObject::KPrBezierCurveObject()
: KPrPointObject()
{
}

KPrBezierCurveObject::KPrBezierCurveObject( const KoPointArray &_controlPoints,
                                            const KoPointArray &_allPoints,
                                            const KoSize &_size, const KoPen &_pen,
                                            LineEnd _lineBegin, LineEnd _lineEnd )
: KPrPointObject( _pen, _lineBegin, _lineEnd )
{
    points = KoPointArray( _controlPoints );
    allPoints = KoPointArray( _allPoints );

    ext = _size;
}


KPrBezierCurveObject &KPrBezierCurveObject::operator=( const KPrBezierCurveObject & )
{
    return *this;
}


bool KPrBezierCurveObject::saveOasisObjectAttributes( KPOasisSaveContext &sc ) const
{
    KoRect rect( getRect() );
    sc.xmlWriter.addAttribute("svg:viewBox", TQString( "0 0 %1 %2" ).arg( int( rect.width() * 100 ) )
                                                                   .arg( int( rect.height() * 100 ) ) );

    unsigned int pointCount = points.count();
    unsigned int pos = 0;

    TQString d;
    d += TQString( "M%1 %2" ).arg( int( points.at(pos).x() * 100 ) )
                            .arg( int( points.at(pos).y() * 100 ) );

    while ( pos + 4 <= pointCount )
    {
        d += TQString( "C%1 %2 %3 %4 %5 %6" ).arg( int( points.at( pos + 2 ).x() * 100 ) )
                                            .arg( int( points.at( pos + 2 ).y() * 100 ) )
                                            .arg( int( points.at( pos + 3 ).x() * 100 ) )
                                            .arg( int( points.at( pos + 3 ).y() * 100 ) )
                                            .arg( int( points.at( pos + 1 ).x() * 100 ) )
                                            .arg( int( points.at( pos + 1 ).y() * 100 ) );
        pos += 4;
    }

    if ( pos < pointCount )
    {
        d += TQString( "L%1 %2" ).arg( int( points.at( pos + 1 ).x() * 100 ) )
                                .arg( int( points.at( pos + 1 ).y() * 100 ) );
    }

    sc.xmlWriter.addAttribute( "svg:d", d );
    
    return true;
}


const char * KPrBezierCurveObject::getOasisElementName() const
{
    return "draw:path";
}


void KPrBezierCurveObject::loadOasis( const TQDomElement &element, KoOasisContext & context,  KPrLoadingInfo* info )
{
    kdDebug(33001) << "KPrBezierCurveObject::loadOasis" << endl;
    KPrPointObject::loadOasis( element, context, info );

    allPoints = bezier2polyline( points );

    //load marker
    loadOasisMarker( context );
}

TQDomDocumentFragment KPrBezierCurveObject::save( TQDomDocument& doc, double offset )
{
    return KPrPointObject::save( doc,offset );
}

double KPrBezierCurveObject::load(const TQDomElement &element)
{
    double offset = KPrPointObject::load( element );

    allPoints = bezier2polyline( points );

    return offset;
}

void KPrBezierCurveObject::updatePoints( double _fx, double _fy )
{
    KPrPointObject::updatePoints( _fx, _fy );

    int index = 0;
    KoPointArray tmpPoints;
    KoPointArray::ConstIterator it;
    for ( it = allPoints.begin(); it != allPoints.end(); ++it ) {
        KoPoint point = (*it);
        double tmpX = point.x() * _fx;
        double tmpY = point.y() * _fy;

        tmpPoints.putPoints( index, 1, tmpX,tmpY );
        ++index;
    }
    allPoints = tmpPoints;
}

KoPointArray KPrBezierCurveObject::bezier2polyline( const KoPointArray &bezierPoints )
{
    if ( bezierPoints.isNull() )
        return bezierPoints;

    KoPointArray _points( bezierPoints );
    KoPointArray _allPoints;
    unsigned int pointCount = _points.count();

    if ( pointCount == 2 ) // line
    {
        _allPoints = _points;
    }
    else // cubic bezier curve
    { 
        KoPointArray tmpPointArray;
        unsigned int _tmpIndex = 0;
        unsigned int count = 0;
        while ( count < pointCount ) 
        {
            if ( pointCount >= ( count + 4 ) ) // for cubic bezier curve
            {
                double _firstX = _points.at( count ).x();
                double _firstY = _points.at( count ).y();

                double _fourthX = _points.at( count + 1 ).x();
                double _fourthY = _points.at( count + 1 ).y();

                double _secondX = _points.at( count + 2 ).x();
                double _secondY = _points.at( count + 2 ).y();

                double _thirdX = _points.at( count + 3 ).x();
                double _thirdY = _points.at( count + 3 ).y();

                KoPointArray bezierPoint;
                bezierPoint.putPoints( 0, 4, _firstX,_firstY, _secondX,_secondY,
                                             _thirdX,_thirdY, _fourthX,_fourthY );
                bezierPoint = bezierPoint.cubicBezier();

                KoPointArray::ConstIterator it;
                for ( it = bezierPoint.begin(); it != bezierPoint.end(); ++it ) 
                {
                    KoPoint _point = (*it);
                    tmpPointArray.putPoints( _tmpIndex, 1, _point.x(), _point.y() );
                    ++_tmpIndex;
                }

                count += 4;
            }
            else // for line
            {
                double _x1 = _points.at( count ).x();
                double _y1 = _points.at( count ).y();

                double _x2 = _points.at( count + 1 ).x();
                double _y2 = _points.at( count + 1 ).y();

                tmpPointArray.putPoints( _tmpIndex, 2, _x1,_y1, _x2,_y2 );
                _tmpIndex += 2;
                count += 2;
            }
        }

        _allPoints = tmpPointArray;
    }

    return _allPoints;
}

void KPrBezierCurveObject::flip(bool horizontal )
{
    KPrPointObject::flip( horizontal );

    KoPointArray tmpPoints;
    int index = 0;
    if ( ! horizontal )
    {
        KoPointArray::ConstIterator it;
        double horiz = getSize().height()/2;
        for ( it = allPoints.begin(); it != allPoints.end(); ++it )
        {
            KoPoint point = (*it);
            if ( point.y()> horiz )
                tmpPoints.putPoints( index, 1, point.x(),point.y()- 2*(point.y()-horiz) );
            else
                tmpPoints.putPoints( index, 1, point.x(),point.y()+ 2*(horiz - point.y()) );
            ++index;
        }
    }
    else
    {
        KoPointArray::ConstIterator it;
        double vert = getSize().width()/2;
        for ( it = allPoints.begin(); it != allPoints.end(); ++it )
        {
            KoPoint point = (*it);
            if ( point.y()> vert )
                tmpPoints.putPoints( index, 1, point.x()- 2*(point.x()-vert), point.y() );
            else
                tmpPoints.putPoints( index, 1, point.x()+ 2*(vert - point.x()),point.y() );
            ++index;
        }
    }

    allPoints = tmpPoints;
}


KoPointArray KPrBezierCurveObject::getDrawingPoints() const
{
  return allPoints;
}


KPrCubicBezierCurveObject::KPrCubicBezierCurveObject()
: KPrBezierCurveObject()
{
}


KPrCubicBezierCurveObject::KPrCubicBezierCurveObject( const KoPointArray &_controlPoints, 
                                                      const KoPointArray &_allPoints, 
                                                      const KoSize & _size, const KoPen &_pen, 
                                                      LineEnd _lineBegin, LineEnd _lineEnd )
: KPrBezierCurveObject( _controlPoints, _allPoints, _size, _pen, _lineBegin, _lineEnd )
{
}


DCOPObject* KPrCubicBezierCurveObject::dcopObject()
{
    if ( !dcop )
        dcop = new KPrCubicBezierCurveObjectIface( this );
    return dcop;
}


KPrQuadricBezierCurveObject::KPrQuadricBezierCurveObject()
: KPrBezierCurveObject()
{
}


KPrQuadricBezierCurveObject::KPrQuadricBezierCurveObject( const KoPointArray &_controlPoints, 
                                                          const KoPointArray &_allPoints, 
                                                          const KoSize & _size, const KoPen &_pen, 
                                                          LineEnd _lineBegin, LineEnd _lineEnd )
: KPrBezierCurveObject( _controlPoints, _allPoints, _size, _pen, _lineBegin, _lineEnd )
{
}


DCOPObject* KPrQuadricBezierCurveObject::dcopObject()
{
    if ( !dcop )
        dcop = new KPrQuadricBezierCurveObjectIface( this );
    return dcop;
}