-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathViewbox.cpp
127 lines (100 loc) · 3.04 KB
/
Viewbox.cpp
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
#include "Viewbox.h"
#include <iostream>
// Constructor
Viewbox::Viewbox(): viewX(0), viewY(0), viewWidth(0), viewHeight(0), portWidth(0), portHeight(0), preservedForm(""), preservedMode("") {}
Viewbox::Viewbox(float viewX, float viewY, float viewWidth, float viewHeight,
float portWidth, float portHeight,
const std::string& preservedForm, const std::string& preservedMode)
: viewX(viewX), viewY(viewY), viewWidth(viewWidth), viewHeight(viewHeight),
portWidth(portWidth), portHeight(portHeight),
preservedForm(preservedForm), preservedMode(preservedMode) {}
// Getter
float Viewbox::getViewX() const {
return viewX;
}
float Viewbox::getViewY() const {
return viewY;
}
float Viewbox::getViewWidth() const {
return viewWidth;
}
float Viewbox::getViewHeight() const {
return viewHeight;
}
float Viewbox::getPortWidth() const {
return portWidth;
}
float Viewbox::getPortHeight() const {
return portHeight;
}
std::string Viewbox::getPreservedForm() const {
return preservedForm;
}
std::string Viewbox::getPreservedMode() const {
return preservedMode;
}
void Viewbox::parseViewbox(rapidxml::xml_node<>* node) {
for (rapidxml::xml_attribute<>* attr = node->first_attribute(); attr; attr = attr->next_attribute()) {
std::string attrName = attr->name();
std::string attrValue = attr->value();
if (attrName == "viewBox") {
std::stringstream ss(attrValue);
std::string token;
std::vector<float> values;
while (std::getline(ss, token, ' ')) {
values.push_back(std::stof(token));
}
if (values.size() == 4) {
viewX = values[0];
viewY = values[1];
viewWidth = values[2];
viewHeight = values[3];
}
}
else if (attrName == "preserveAspectRatio") {
std::stringstream ss(attrValue);
std::string token;
std::vector<std::string> values;
while (std::getline(ss, token, ' ')) {
values.push_back(token);
}
if (values.size() == 1) {
preservedForm = values[0];
}
else if (values.size() == 2) {
preservedForm = values[0];
preservedMode = values[1];
}
}
else if (attrName == "width") {
portWidth = std::stof(attrValue);
}
else if (attrName == "height") {
portHeight = std::stof(attrValue);
}
}
}
void Viewbox::render(Graphics& graphics, RECT& window) {
float width = 0, height = 0;
float scaleX = 1, scaleY = 1, scale = 1;
float translateX, translateY;
if (portWidth == 0 || portHeight == 0) {
width = window.right - window.left;
height = window.bottom - window.top;
}
else {
width = portWidth;
height = portHeight;
}
scaleX = width / viewWidth;
scaleY = height / viewHeight;
scale = min(scaleX, scaleY);
translateX = (width - viewWidth * scale) / 2;
translateY = (height - viewHeight * scale) / 2;
graphics.TranslateTransform(translateX, translateY);
graphics.ScaleTransform(scale, scale);
graphics.SetSmoothingMode(Gdiplus::SmoothingModeAntiAlias);
graphics.SetCompositingMode(Gdiplus::CompositingModeSourceOver);
graphics.SetPixelOffsetMode(Gdiplus::PixelOffsetModeHighQuality);
graphics.SetInterpolationMode(Gdiplus::InterpolationModeHighQuality);
}