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
/*
* Copyright (C) 2013 Open Education Foundation
*
* Copyright (C) 2010-2013 Groupement d'Intérêt Public pour
* l'Education Numérique en Afrique (GIP ENA)
*
* This file is part of OpenBoard.
*
* OpenBoard is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3 of the License,
* with a specific linking exception for the OpenSSL project's
* "OpenSSL" library (or with modified versions of it that use the
* same license as the "OpenSSL" library).
*
* OpenBoard 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenBoard. If not, see <http://www.gnu.org/licenses/>.
*/
/*
The file defines some classes for transformation of PDF content stream.
*/
#ifndef TRANSFORMATION_H
#define TRANSFORMATION_H
#include <sstream>
#include <string>
#include <math.h>
#include <vector>
#include <iostream>
#include "Utils.h"
namespace merge_lib
{
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
class TransformationMatrix
{
public:
TransformationMatrix(double a = 1, double b = 0, double c = 0, double d = 1, double e = 0, double f = 0):
_a(a), _b(b), _c(c), _d(d), _e(e), _f(f)
{}
TransformationMatrix(const TransformationMatrix & copy)
{
setParameters(copy._a, copy._b, copy._c, copy._d, copy._e, copy._f);
}
void setParameters(double a, double b, double c, double d, double e, double f)
{
_a = Utils::normalizeValue(a);
_b = Utils::normalizeValue(b);
_c = Utils::normalizeValue(c);
_d = Utils::normalizeValue(d);
_e = Utils::normalizeValue(e);
_f = Utils::normalizeValue(f);
}
void add(const TransformationMatrix & tm)
{
double newA = _a*tm._a + _b*tm._c;
double newB = _a*tm._b + _b*tm._d;
double newC = _c*tm._a + _d*tm._c;
double newD = _c*tm._b + _d*tm._d;
double newE = _e*tm._a + _f*tm._c + tm._e;
double newF = _e*tm._b + _f*tm._d + tm._f;
// we need to round the values to avoid not-needed transformation
// since 1.e-17 is not 0 from PDF point of view, while such double
// value really means 0.
_a = Utils::normalizeValue(newA);
_b = Utils::normalizeValue(newB);
_c = Utils::normalizeValue(newC);
_d = Utils::normalizeValue(newD);
_e = Utils::normalizeValue(newE);
_f = Utils::normalizeValue(newF);
}
std::string getValue()
{
std::ostringstream value;
value << "[ " << _a << " " << _b << " " << _c << " " << _d << " " << _e << " " << _f << " ]\n";
return value.str();
}
std::string getCMT()
{
std::ostringstream buf;
buf << std::fixed << _a <<" "<< _b <<" "<< _c <<" "<< _d << " "<< _e << " "<< _f << " cm\n";
return buf.str();
}
void recalculateCoordinates(double & x, double &y)
{
double inputX = x;
double inputY = y;
x = _a*inputX + _c*inputY + _e;
y = _b*inputX + _d*inputY + _f;
}
private:
double _a, _b, _c, _d, _e, _f;
};
// base class of transformation CMT
class Transformation
{
public:
Transformation(): _tm(){};
virtual Transformation * getClone() const = 0;
std::string getCMT()
{
return _tm.getCMT();
}
virtual ~Transformation() {};
const TransformationMatrix & getMatrix()
{
return _tm;
}
void addMatrix(const TransformationMatrix & tm)
{
_tm.add(tm);
}
protected:
TransformationMatrix _tm;
};
// rotation CMT
class Rotation: public Transformation
{
public:
Rotation(double angle):Transformation(),_angle(angle)
{
double cosValue = cos(_angle * (M_PI / 180));
double sinValue = sin(_angle * (M_PI / 180));
_tm.setParameters(cosValue, sinValue, -sinValue, cosValue, 0, 0);
};
virtual ~Rotation(){};
virtual Transformation * getClone() const
{
return new Rotation(_angle);
}
protected:
double _angle; // number of degrees to rotate
};
// translation CMT
class Translation: public Transformation
{
public:
Translation(double x, double y):Transformation(),_x(x),_y(y)
{
_tm.setParameters(1, 0, 0, 1, _x, _y);
};
virtual ~Translation(){};
virtual Transformation * getClone() const
{
return new Translation(_x, _y);
}
protected:
double _x;
double _y;
};
// scaling CMT
class Scaling: public Transformation
{
public:
Scaling(double x):Transformation(),_x(x)
{
_tm.setParameters(_x, 0, 0, _x, 0, 0);
};
virtual Transformation * getClone() const
{
return new Scaling(_x);
}
protected:
double _x; // the value to multiply the content
};
// transformation can consist of one or several
// operations like rotation, scaling, translation
typedef std::vector<Transformation *> PageTransformations;
// This is interface class for setting transformation parameters
//
class TransformationDescription
{
public:
TransformationDescription( double x = 0, // leftBottomX coordinate
double y = 0, // leftBottomY coordinate
double scale = 1, // scale (by default = 1 = NONE
int angel = 0): // rotation (0,90,180,270)
_x(x),_y(y),_scale(scale),_angel(angel)
{
if( _angel )
{
_transforms.push_back(new Rotation(_angel));
}
if( !Utils::doubleEquals(_scale,1) && !Utils::doubleEquals(_scale,0) )
{
_transforms.push_back(new Scaling(_scale));
}
}
virtual ~TransformationDescription()
{
for(size_t i = 0;i<_annotsTransforms.size();i++)
{
if( _annotsTransforms[i] )
{
delete _annotsTransforms[i];
_annotsTransforms[i] = 0;
}
_annotsTransforms.clear();
}
for(size_t i = 0;i<_transforms.size();i++)
{
if( _transforms[i] )
{
delete _transforms[i];
_transforms[i] = 0;
}
}
_transforms.clear();
}
void addRotation(int rotation)
{
if( rotation )
{
_angel = (_angel - rotation)%360;
// /Rotation rotate the object, while _angel rotate the coordinate system
// where object is located, that's why
// we should compensate that
_transforms.push_back(new Rotation(360-rotation));
}
}
const PageTransformations & getTransformations() const
{
return _transforms;
}
const PageTransformations getAnnotsTransformations() const
{
PageTransformations trans;
trans = _transforms;
for(size_t i = 0; i < _annotsTransforms.size(); ++i)
{
trans.push_back(_annotsTransforms[i]);
}
return trans;
}
void addAnnotsTransformation( Transformation & trans )
{
_annotsTransforms.push_back(trans.getClone());
}
// method recalculates the final translation in order to put
// object into needed x,y coordinates.
// Page is located from position 0,0
void recalculateTranslation(double width, double height)
{
double dx1 = 0;
double dy1 = 0;
double scaling = ( Utils::doubleEquals(_scale,0))?1:_scale;
switch(_angel)
{
case 0:
dx1 = _x/scaling;
dy1 = _y/scaling;
break;
case -270:
case 90:
dx1 = _y/scaling ;
dy1 = - _x /scaling - height;
break;
case 180:
case -180:
dx1 = - _x /scaling - width;
dy1 = - _y /scaling - height;
break;
case 270:
case -90:
dx1 = - _y/scaling - width;
dy1 = _x/scaling;
break;
default:
std::cerr<<"Unsupported rotation parameter"<<_angel<<std::endl;
break;
}
//std::cerr<< "dx1 = "<<dx1<<"dy1 = "<<dy1<<std::endl;
if( ! (Utils::doubleEquals(dx1,0) && Utils::doubleEquals(dy1,0)) )
{
// This translation is needed to put transformed content into
// desired coordinates
_transforms.push_back(new Translation(dx1,dy1));
}
}
TransformationDescription( const TransformationDescription & copy)
{
*this = copy;
}
TransformationDescription& operator = (const TransformationDescription ©)
{
if( this != © )
{
for(size_t i = 0;i < copy._annotsTransforms.size();i++)
{
_annotsTransforms.push_back(copy._annotsTransforms[i]->getClone());
}
for(size_t i = 0; i < copy._transforms.size(); ++i)
{
_transforms.push_back(copy._transforms[i]->getClone());
}
_x = copy._x;
_y = copy._y;
_scale = copy._scale;
_angel = copy._angel;
}
return *this;
}
std::string getCMT()
{
std::stringstream content;
for(size_t i = 0;i<_transforms.size();i++)
{
content<<_transforms[i]->getCMT();
}
return content.str();
}
private:
double _x;
double _y;
double _scale;
int _angel;
PageTransformations _transforms;
PageTransformations _annotsTransforms;
};
}
#endif