-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathText.h
143 lines (129 loc) · 4.88 KB
/
Text.h
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
#ifndef MY_TEXT_H
#define MY_TEXT_H
#include "SVGElement.h"
#include <GdiPlus.h>
using namespace Gdiplus;
class my_text : public SVGElement {
private:
double x, y;
Color stroke;
string fill;
double font_size;
string content;
double stroke_width;
string text_anchor;
string font_style;
string font_family;
string transform;
public:
my_text(string name, string transform, double x, double y, string fill,
Color stroke, double stroke_width, int font_size, string font_style,
string text_anchor, string font_family, string content)
: x(x), y(y), stroke(stroke), font_size(font_size),
font_style(font_style), text_anchor(text_anchor),
font_family(font_family), content(content), fill(fill),
stroke_width(stroke_width), SVGElement(name), transform(transform) {}
void render(Graphics &graphics, vector<LinearGradient> gradients) override {
if (fill[0] == 'u' && fill[1] == 'r' && fill[2] == 'l') {
string font_family_str = font_family;
vector<wstring> font_families;
int pos = font_family_str.find(',');
if (pos != -1) {
int pos = 0;
while ((pos = font_family_str.find(",")) != string::npos) {
font_families.push_back(
wstring(font_family_str.begin(), font_family_str.begin() + pos));
font_family_str.erase(0, pos + 1);
}
} else {
font_families.push_back(
wstring(font_family_str.begin(), font_family_str.end()));
}
FontFamily fontFamily(font_families[0].c_str());
FontStyle style =
(font_style == "italic") ? FontStyleItalic : FontStyleRegular;
Font font(&fontFamily, static_cast<REAL>(font_size), style, UnitPixel);
string id = extractID(fill);
LinearGradientBrush *brush = nullptr;
for (auto gradient : gradients) {
if (gradient.getId() == id) {
brush = gradient.createBrush();
break;
}
}
RectF boundingBox;
graphics.MeasureString(
std::wstring(content.begin(), content.end()).c_str(), -1, &font,
PointF(0, 0), &boundingBox);
REAL text_width = boundingBox.Width;
REAL adjusted_x = static_cast<REAL>(x);
if (text_anchor == "middle") {
adjusted_x -= text_width / 2;
} else if (text_anchor == "end") {
adjusted_x -= text_width;
} else if (text_anchor == "start") {
adjusted_x = static_cast<REAL>(x);
}
PointF pointF(static_cast<REAL>(adjusted_x), static_cast<REAL>(y));
graphics.DrawString(std::wstring(content.begin(), content.end()).c_str(),
-1, &font, pointF, brush);
if (stroke_width != 0) {
Pen pen(stroke, stroke_width);
GraphicsPath path;
StringFormat format;
path.AddString(std::wstring(content.begin(), content.end()).c_str(), -1,
&fontFamily, style, static_cast<REAL>(font_size), pointF,
&format);
graphics.DrawPath(&pen, &path);
}
return;
}
string font_family_str = font_family;
vector<wstring> font_families;
int pos = font_family_str.find(',');
if (pos != -1) {
int pos = 0;
while ((pos = font_family_str.find(",")) != string::npos) {
font_families.push_back(
wstring(font_family_str.begin(), font_family_str.begin() + pos));
font_family_str.erase(0, pos + 1);
}
} else {
font_families.push_back(
wstring(font_family_str.begin(), font_family_str.end()));
}
FontFamily fontFamily(font_families[0].c_str());
FontStyle style =
(font_style == "italic") ? FontStyleItalic : FontStyleRegular;
Font font(&fontFamily, static_cast<REAL>(font_size), style, UnitPixel);
Color filll = stoc(this->fill);
SolidBrush brush(filll);
RectF boundingBox;
graphics.MeasureString(std::wstring(content.begin(), content.end()).c_str(),
-1, &font, PointF(0, 0), &boundingBox);
REAL text_width = boundingBox.Width;
REAL adjusted_x = static_cast<REAL>(x);
if (text_anchor == "middle") {
adjusted_x -= text_width / 2;
} else if (text_anchor == "end") {
adjusted_x -= text_width;
} else if (text_anchor == "start") {
adjusted_x = static_cast<REAL>(x);
}
PointF pointF(static_cast<REAL>(adjusted_x), static_cast<REAL>(y));
graphics.DrawString(std::wstring(content.begin(), content.end()).c_str(),
-1, &font, pointF, &brush);
if (stroke_width != 0) {
Pen pen(stroke, stroke_width);
GraphicsPath path;
StringFormat format;
path.AddString(std::wstring(content.begin(), content.end()).c_str(), -1,
&fontFamily, style, static_cast<REAL>(font_size), pointF,
&format);
graphics.DrawPath(&pen, &path);
}
}
~my_text() override {}
string getTransform() override { return transform; }
};
#endif