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
/*
* This program 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, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Rectangle.h"
#include "Utils.h"
#include "Object.h"
#include "Parser.h"
#include <iostream>
#include "core/memcheck.h"
using namespace merge_lib;
Rectangle::Rectangle(const char * rectangleName):
x1(0),
y1(0),
x2(0),
y2(0),
_rectangleName(rectangleName),
_tm()
{}
Rectangle::Rectangle(const char * rectangleName, const std::string content):
x1(0),
y1(0),
x2(0),
y2(0),
_rectangleName(rectangleName)
{
unsigned int rectanglePosition = Parser::findToken(content,rectangleName);
if((int) rectanglePosition == -1 )
{
std::cerr<<"Unable to find rectangle name "<<rectangleName<<" in content\n";
}
size_t beg = content.find("[",rectanglePosition);
size_t end = content.find("]",rectanglePosition);
if((int) beg != -1 && (int) end != -1 )
{
std::string arr = content.substr(beg+1,end-beg-1);
std::stringstream in;
in<<arr;
in>>x1>>y1>>x2>>y2;
}
}
void Rectangle::appendRectangleToString(std::string & content, const char * delimeter)
{
content.append(_getRectangleAsString(delimeter));
}
const std::string Rectangle::_getRectangleAsString(const char * delimeter)
{
std::string result(_rectangleName);
result.append(" [");
result.append(Utils::doubleToStr(x1));
result.append(delimeter);
result.append(Utils::doubleToStr(y1));
result.append(delimeter);
result.append(Utils::doubleToStr(x2));
result.append(delimeter);
result.append(Utils::doubleToStr(y2));
result.append(" ]\n");
return result;
}
void Rectangle::setNewRectangleName(const char * newName)
{
_rectangleName = newName;
}
void Rectangle::recalculateInternalRectangleCoordinates(const PageTransformations & transformations)
{
TransformationMatrix tempTm;
for(size_t i = 0; i < transformations.size(); ++i)
{
tempTm = transformations[i]->getMatrix();
tempTm.add(_tm);
_tm = tempTm;
}
_tm.recalculateCoordinates(x1, y1);
_tm.recalculateCoordinates(x2, y2);
}
void Rectangle::updateRectangle(Object * objectWithRectangle, const char * delimeter)
{
Object * foundObjectWithRectangle;
unsigned int fake;
objectWithRectangle->findObject(std::string(_rectangleName), foundObjectWithRectangle, fake);
std::string objectContent = foundObjectWithRectangle->getObjectContent();
unsigned int rectanglePosition = objectContent.find(_rectangleName);
unsigned int endOfRectangle = objectContent.find("]", rectanglePosition) + 1;
foundObjectWithRectangle->eraseContent(rectanglePosition, endOfRectangle - rectanglePosition);
foundObjectWithRectangle->insertToContent(rectanglePosition, _getRectangleAsString(delimeter));
// reread the objectContent, since it was changed just above;
objectContent = foundObjectWithRectangle->getObjectContent();
//update matrix
unsigned int startOfAP = Parser::findToken(objectContent,"/AP");
unsigned int endOfAP = objectContent.find(">>", startOfAP);
std::vector<Object *> aps = foundObjectWithRectangle->getChildrenByBounds(startOfAP, endOfAP);
for(size_t i = 0; i < aps.size(); ++i)
{
Object * objectWithMatrix = aps[i];
std::string objectContent = objectWithMatrix->getObjectContent();
unsigned int matrixPosition = Parser::findToken(objectContent,"/Matrix");
if((int)matrixPosition == -1)
continue;
unsigned int matrixValueLeftBound = objectContent.find("[", matrixPosition);
unsigned int matrixValueRightBound = objectContent.find("]", matrixValueLeftBound) + 1;
objectWithMatrix->eraseContent(matrixValueLeftBound, matrixValueRightBound - matrixValueLeftBound);
objectWithMatrix->insertToContent(matrixValueLeftBound, _tm.getValue());
}
}
double Rectangle::getWidth()
{
return x2 - x1;
}
double Rectangle::getHeight()
{
return y2 - y1;
}