-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathgraphic_win32.cpp
352 lines (296 loc) · 9.54 KB
/
graphic_win32.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
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
#include "graphic_win32.h"
#include "utils/exceptions.h"
#include "utils/nums.h"
#include "utils/utils.h"
// fix error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
#include <comdef.h>
#include <gdiplus.h>
#include <windows.h>
using namespace std;
using namespace microtex;
std::wstring microtex::win32ToWideString(const std::string& str) {
std::wstring wstr;
auto l = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, nullptr, 0);
wstr.resize(l + 10);
MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, &wstr[0], (int)wstr.size());
return wstr;
}
/**************************************************************************************************/
std::map<std::string, sptr<Font_win32>> Font_win32::_win32Faces;
sptr<Font_win32> Font_win32::getOrCreate(const std::string& file) {
auto it = _win32Faces.find(file);
if (it != _win32Faces.end()) {
return it->second;
}
// add font to font collection, some like fontconfig
Gdiplus::PrivateFontCollection c;
const auto& wfile = win32ToWideString(file);
c.AddFontFile(wfile.c_str());
auto ff = new Gdiplus::FontFamily();
int num = 0;
c.GetFamilies(1, ff, &num);
if (num <= 0) {
throw microtex::ex_invalid_state("Cannot load font file " + file);
}
// search order:
// regular -> bold -> italic -> bold-italic
int style;
if (ff->IsStyleAvailable(Gdiplus::FontStyleRegular)) {
style = Gdiplus::FontStyleRegular;
} else if (ff->IsStyleAvailable(Gdiplus::FontStyleBold)) {
style = Gdiplus::FontStyleBold;
} else if (ff->IsStyleAvailable(Gdiplus::FontStyleItalic)) {
style = Gdiplus::FontStyleItalic;
} else if (ff->IsStyleAvailable(Gdiplus::FontStyleBoldItalic)) {
style = Gdiplus::FontStyleBoldItalic;
} else {
throw microtex::ex_invalid_state("No available font in file " + file);
}
auto f = sptrOf<Font_win32>(ff, style);
_win32Faces[file] = f;
return f;
}
Font_win32::Font_win32(const Gdiplus::FontFamily* family, int style, bool isSystem)
: _family(family), _style(style), _isSystem(isSystem) {}
sptr<Gdiplus::Font> Font_win32::getFont(float size) {
auto f = new Gdiplus::Font(_family, size, _style, Gdiplus::UnitPixel);
return sptr<Gdiplus::Font>(f);
}
int Font_win32::getEmHeight() {
return _family->GetEmHeight(_style);
}
int Font_win32::getCellAscent() {
return _family->GetCellAscent(_style);
}
bool Font_win32::operator==(const Font& ft) const {
const auto& f = static_cast<const Font_win32&>(ft);
return f._family == _family;
}
Font_win32::~Font_win32() noexcept {
if (!_isSystem) delete _family;
}
/**************************************************************************************************/
Gdiplus::Graphics* TextLayout_win32::_g = nullptr;
Gdiplus::Bitmap* TextLayout_win32::_img = nullptr;
const Gdiplus::StringFormat* TextLayout_win32::_format = nullptr;
TextLayout_win32::TextLayout_win32(const std::string& src, FontStyle style, float size)
: _fontSize(size) {
if (_img == nullptr) {
_img = new Gdiplus::Bitmap(1, 1, PixelFormat32bppARGB);
_g = Gdiplus::Graphics::FromImage(_img);
_format = Gdiplus::StringFormat::GenericTypographic();
}
const auto* f = Gdiplus::FontFamily::GenericSerif();
if (microtex::isSansSerif(style)) {
f = Gdiplus::FontFamily::GenericSansSerif();
}
if (microtex::isMono(style)) {
f = Gdiplus::FontFamily::GenericMonospace();
}
int s = Gdiplus::FontStyleRegular;
if (microtex::isBold(style)) {
s |= Gdiplus::FontStyleBold;
}
if (microtex::isItalic(style)) {
s |= Gdiplus::FontStyleItalic;
}
if (!f->IsStyleAvailable(s)) {
s = Gdiplus::FontStyleRegular;
}
_font = sptrOf<Font_win32>(f, s, true);
_txt = win32ToWideString(src);
}
void TextLayout_win32::getBounds(Rect& r) {
int em = _font->getEmHeight();
int ascent = _font->getCellAscent();
float ap = _fontSize * ascent / em;
auto f = _font->getFont(_fontSize);
Gdiplus::RectF r1;
_g->MeasureString(
_txt.c_str(),
(int)wcslen(_txt.c_str()),
f.get(),
Gdiplus::PointF(0, 0),
_format,
&r1
);
r.x = 0;
r.y = -ap;
r.w = r1.Width;
r.h = r1.Height;
}
void TextLayout_win32::draw(Graphics2D& g2, float x, float y) {
auto& g = static_cast<Graphics2D_win32&>(g2);
auto prev = g2.getFont();
auto prevSize = g2.getFontSize();
g.setFont(_font);
g.setFontSize(_fontSize);
g.drawText(_txt, x, y);
g.setFontSize(prevSize);
g.setFont(prev);
}
/**************************************************************************************************/
sptr<microtex::Font> PlatformFactory_gdi::createFont(const std::string& file) {
return Font_win32::getOrCreate(file);
}
sptr<TextLayout>
PlatformFactory_gdi::createTextLayout(const std::string& src, FontStyle style, float size) {
return sptrOf<TextLayout_win32>(src, style, size);
}
/**************************************************************************************************/
const Gdiplus::StringFormat* Graphics2D_win32::_format;
Graphics2D_win32::Graphics2D_win32(Gdiplus::Graphics* g) {
if (_format == nullptr) {
_format = Gdiplus::StringFormat::GenericTypographic();
}
_fontSize = 0.f;
_sx = _sy = 1.f;
_color = black;
_g = g;
_g->ResetTransform();
_g->SetSmoothingMode(Gdiplus::SmoothingModeHighQuality);
_g->SetTextRenderingHint(Gdiplus::TextRenderingHintAntiAlias);
_pen = new Gdiplus::Pen(Gdiplus::Color(_color));
_brush = new Gdiplus::SolidBrush(Gdiplus::Color(_color));
}
Graphics2D_win32::~Graphics2D_win32() {
delete _pen;
delete _brush;
}
void Graphics2D_win32::setColor(color color) {
_color = color;
_pen->SetColor(Gdiplus::Color(color));
_brush->SetColor(Gdiplus::Color(color));
}
color Graphics2D_win32::getColor() const {
return _color;
}
void Graphics2D_win32::setStroke(const Stroke& s) {
_stroke = s;
_pen->SetWidth(s.lineWidth);
Gdiplus::LineCap c;
switch (s.cap) {
case CAP_BUTT: c = Gdiplus::LineCapFlat; break;
case CAP_ROUND: c = Gdiplus::LineCapRound; break;
case CAP_SQUARE: c = Gdiplus::LineCapSquare; break;
}
_pen->SetLineCap(c, c, Gdiplus::DashCapRound);
Gdiplus::LineJoin j;
switch (s.join) {
case JOIN_BEVEL: j = Gdiplus::LineJoinBevel; break;
case JOIN_ROUND: j = Gdiplus::LineJoinRound; break;
case JOIN_MITER: j = Gdiplus::LineJoinMiter; break;
}
_pen->SetLineJoin(j);
_pen->SetMiterLimit(s.miterLimit);
}
const Stroke& Graphics2D_win32::getStroke() const {
return _stroke;
}
void Graphics2D_win32::setStrokeWidth(float w) {
_stroke.lineWidth = w;
_pen->SetWidth(w);
}
void Graphics2D_win32::setDash(const std::vector<float>& dash) {
// todo
}
std::vector<float> Graphics2D_win32::getDash() {
// todo
return {};
}
sptr<microtex::Font> Graphics2D_win32::getFont() const {
return _font;
}
void Graphics2D_win32::setFont(const sptr<Font>& font) {
_font = std::static_pointer_cast<Font_win32>(font);
}
float Graphics2D_win32::getFontSize() const {
return _fontSize;
}
void Graphics2D_win32::setFontSize(float size) {
_fontSize = size;
}
void Graphics2D_win32::translate(float dx, float dy) {
_g->TranslateTransform(dx, dy);
}
void Graphics2D_win32::scale(float sx, float sy) {
_sx *= sx;
_sy *= sy;
_g->ScaleTransform(sx, sy);
}
void Graphics2D_win32::rotate(float angle) {
_g->RotateTransform(angle / PI * 180);
}
void Graphics2D_win32::rotate(float angle, float px, float py) {
_g->TranslateTransform(px, py);
_g->RotateTransform(angle / PI * 180);
_g->TranslateTransform(-px, -py);
}
void Graphics2D_win32::reset() {
_g->ResetTransform();
_sx = _sy = 1.f;
}
float Graphics2D_win32::sx() const {
return _sx;
}
float Graphics2D_win32::sy() const {
return _sy;
}
void Graphics2D_win32::drawGlyph(u16 glyph, float x, float y) {
if (_font == nullptr) return;
auto f = _font->getFont(_fontSize);
auto p = Gdiplus::PointF(x, y);
_g->DrawDriverString(&glyph, 1, f.get(), _brush, &p, 0, nullptr);
}
bool Graphics2D_win32::beginPath(i32 id) {
// not supported
return false;
}
void Graphics2D_win32::moveTo(float x, float y) {
// not supported
}
void Graphics2D_win32::lineTo(float x, float y) {
// not supported
}
void Graphics2D_win32::cubicTo(float x1, float y1, float x2, float y2, float x3, float y3) {
// not supported
}
void Graphics2D_win32::quadTo(float x1, float y1, float x2, float y2) {
// not supported
}
void Graphics2D_win32::closePath() {
// not supported
}
void Graphics2D_win32::fillPath(i32 id) {
// not supported
}
void Graphics2D_win32::drawText(const std::wstring& src, float x, float y) {
auto f = std::static_pointer_cast<Font_win32>(_font);
int em = f->getEmHeight();
int ascent = f->getCellAscent();
float ap = _fontSize * ascent / em;
int len = src.length();
auto font = _font->getFont(_fontSize);
Gdiplus::RectF r1, r2;
_g->MeasureString(src.c_str(), len, font.get(), Gdiplus::PointF(0, 0), &r1);
_g->MeasureString(src.c_str(), len, font.get(), Gdiplus::PointF(0, 0), _format, &r2);
float off = (r1.Width - r2.Width) / 2.f;
_g->DrawString(src.c_str(), len, font.get(), Gdiplus::PointF(x - off, y - ap), _brush);
}
void Graphics2D_win32::drawLine(float x1, float y1, float x2, float y2) {
_g->DrawLine(_pen, x1, y1, x2, y2);
}
void Graphics2D_win32::drawRect(float x, float y, float w, float h) {
_g->DrawRectangle(_pen, x, y, w, h);
}
void Graphics2D_win32::fillRect(float x, float y, float w, float h) {
_g->FillRectangle(_brush, x, y, w, h);
}
void Graphics2D_win32::drawRoundRect(float x, float y, float w, float h, float rx, float ry) {
// not supported
drawRect(x, y, w, h);
}
void Graphics2D_win32::fillRoundRect(float x, float y, float w, float h, float rx, float ry) {
// not supported
fillRect(x, y, w, h);
}