-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageWithAnchor.cpp
More file actions
120 lines (107 loc) · 3.02 KB
/
ImageWithAnchor.cpp
File metadata and controls
120 lines (107 loc) · 3.02 KB
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
#include "stdafx.h"
#include "ImageWithAnchor.h"
#include "CharacterRules.h"
void ImageWithAnchors::scale(double dx, double dy)
{
image.scale(dx,dy);
for(std::map<PartType, Point>::iterator it = anchors.begin(); it != anchors.end(); ++it)
it->second.scale(dx,dy);
}
void ImageWithAnchors::transfer(int dx, int dy)
{
image.transfer(dx,dy);
for(std::map<PartType, Point>::iterator it = anchors.begin(); it != anchors.end(); ++it)
it->second.transfer(dx,dy);
}
CompositeImage ImageWithAnchors::join(std::vector<ImageWithAnchors> & shape_images, const Box & box, const Rule & rule)
{
//ImageWithAnchors & firstImage = shape_images.front();
for(unsigned int i = 1; i < shape_images.size(); ++i)
{
ImageWithAnchors & iwa = shape_images[i];
for (unsigned int j = 0; j < i; ++j)
{
const ImageWithAnchors & fixedImage = shape_images[j];
adjust(shape_images, i, j, rule);
}
}
CompositeImage result;
collect(shape_images, result);
result.fit(box.shrunk_by(4));
return result;
}
bool is_to_side(PartType partType)
{
switch(partType)
{
case ptNone:
case ptAbove:
case ptBelow:
case ptLefter:
case ptRighter:
return true;
default:
return false;
}
}
int get_dx(PartType fixedPart, const ImageWithAnchors & fixedImage, const ImageWithAnchors & adjustedImage)
{
switch(fixedPart)
{
case ptLefter:
return fixedImage.image.box().width() / 5;
case ptRighter:
return - fixedImage.image.box().width() / 5;
default:
return 0;
}
}
int get_dy(PartType fixedPart, const ImageWithAnchors & fixedImage, const ImageWithAnchors & adjustedImage)
{
switch(fixedPart)
{
case ptAbove:
case ptNone:
return fixedImage.image.box().height() / 5;
case ptBelow:
return - fixedImage.image.box().height() / 5;
default:
return 0;
}
}
void ImageWithAnchors::adjust(std::vector<ImageWithAnchors> & shape_images, int adjusted_index, int fixed_index, const Rule & rule)
{
ImageWithAnchors & image = shape_images[adjusted_index];
const ImageWithAnchors & fixedImage = shape_images[fixed_index];
for(std::vector<Join*>::const_iterator it = rule.joins.begin(); it != rule.joins.end(); ++it)
{
Join & join = **it;
if (join.matches_shapes(rule.shapes[adjusted_index], rule.shapes[fixed_index]))
{
PartType fixedPart = join.part_type(rule.shapes[fixed_index]);
PartType adjustedPart = join.part_type(rule.shapes[adjusted_index]);
int dx, dy;
if (is_to_side(fixedPart)) //appears only in =
{
dx = get_dx(fixedPart, fixedImage, image);
dy = get_dy(fixedPart, fixedImage, image);
}
else
{
const Point & fixedPoint = fixedImage.anchors.find(fixedPart)->second;
const Point & adjustedPoint = image.anchors.find(adjustedPart)->second;
dx = fixedPoint.x - adjustedPoint.x;
dy = fixedPoint.y - adjustedPoint.y;
}
image.transfer(dx, dy);
}
}
}
void ImageWithAnchors::collect(const std::vector<ImageWithAnchors> & images, CompositeImage & result)
{
for(std::vector<ImageWithAnchors>::const_iterator it = images.begin(); it != images.end(); ++it)
{
const Image & image = it->image;
result.add(image);
}
}