forked from aseprite/aseprite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphics.cpp
609 lines (505 loc) · 15.5 KB
/
graphics.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
// Aseprite UI Library
// Copyright (C) 2019-2024 Igara Studio S.A.
// Copyright (C) 2001-2018 David Capello
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "ui/graphics.h"
#include "base/string.h"
#include "gfx/clip.h"
#include "gfx/matrix.h"
#include "gfx/path.h"
#include "gfx/point.h"
#include "gfx/rect.h"
#include "gfx/region.h"
#include "gfx/size.h"
#include "os/draw_text.h"
#include "os/font.h"
#include "os/sampling.h"
#include "os/surface.h"
#include "os/system.h"
#include "os/window.h"
#include "ui/display.h"
#include "ui/scale.h"
#include "ui/theme.h"
#include <algorithm>
#include <cctype>
#include <string>
namespace ui {
Graphics::Graphics(Display* display, const os::SurfaceRef& surface, int dx, int dy)
: m_display(display)
, m_surface(surface)
, m_dx(dx)
, m_dy(dy)
{
}
Graphics::~Graphics()
{
// If we were drawing in the screen surface, we mark these regions
// as dirty for the final flip.
if (m_display)
m_display->dirtyRect(m_dirtyBounds);
}
int Graphics::width() const
{
return m_surface->width();
}
int Graphics::height() const
{
return m_surface->height();
}
int Graphics::getSaveCount() const
{
return m_surface->getSaveCount();
}
gfx::Rect Graphics::getClipBounds() const
{
return m_surface->getClipBounds().offset(-m_dx, -m_dy);
}
void Graphics::saveClip()
{
m_surface->saveClip();
}
void Graphics::restoreClip()
{
m_surface->restoreClip();
}
bool Graphics::clipRect(const gfx::Rect& rc)
{
return m_surface->clipRect(gfx::Rect(rc).offset(m_dx, m_dy));
}
void Graphics::save()
{
m_surface->save();
}
void Graphics::concat(const gfx::Matrix& matrix)
{
m_surface->concat(matrix);
}
void Graphics::setMatrix(const gfx::Matrix& matrix)
{
m_surface->setMatrix(matrix);
}
void Graphics::resetMatrix()
{
m_surface->resetMatrix();
}
void Graphics::restore()
{
m_surface->restore();
}
gfx::Matrix Graphics::matrix() const
{
return m_surface->matrix();
}
gfx::Color Graphics::getPixel(int x, int y)
{
os::SurfaceLock lock(m_surface.get());
return m_surface->getPixel(m_dx+x, m_dy+y);
}
void Graphics::putPixel(gfx::Color color, int x, int y)
{
dirty(gfx::Rect(m_dx+x, m_dy+y, 1, 1));
os::SurfaceLock lock(m_surface.get());
m_surface->putPixel(color, m_dx+x, m_dy+y);
}
void Graphics::drawHLine(int x, int y, int w, const Paint& paint)
{
dirty(gfx::Rect(m_dx+x, m_dy+y, w, 1));
os::SurfaceLock lock(m_surface.get());
m_surface->drawRect(gfx::Rect(m_dx+x, m_dy+y, w, 1), paint);
}
void Graphics::drawHLine(gfx::Color color, int x, int y, int w)
{
dirty(gfx::Rect(m_dx+x, m_dy+y, w, 1));
os::SurfaceLock lock(m_surface.get());
os::Paint paint;
paint.color(color);
m_surface->drawRect(gfx::Rect(m_dx+x, m_dy+y, w, 1), paint);
}
void Graphics::drawVLine(int x, int y, int h, const Paint& paint)
{
dirty(gfx::Rect(m_dx+x, m_dy+y, 1, h));
os::SurfaceLock lock(m_surface.get());
m_surface->drawRect(gfx::Rect(m_dx+x, m_dy+y, 1, h), paint);
}
void Graphics::drawVLine(gfx::Color color, int x, int y, int h)
{
dirty(gfx::Rect(m_dx+x, m_dy+y, 1, h));
os::SurfaceLock lock(m_surface.get());
os::Paint paint;
paint.color(color);
m_surface->drawRect(gfx::Rect(m_dx+x, m_dy+y, 1, h), paint);
}
void Graphics::drawLine(gfx::Color color, const gfx::Point& _a, const gfx::Point& _b)
{
gfx::Point a(m_dx+_a.x, m_dy+_a.y);
gfx::Point b(m_dx+_b.x, m_dy+_b.y);
dirty(gfx::Rect(a, b));
os::SurfaceLock lock(m_surface.get());
os::Paint paint;
paint.color(color);
m_surface->drawLine(a, b, paint);
}
void Graphics::drawPath(gfx::Path& path, const Paint& paint)
{
os::SurfaceLock lock(m_surface.get());
auto m = matrix();
save();
setMatrix(gfx::Matrix::MakeTrans(m_dx, m_dy));
concat(m);
m_surface->drawPath(path, paint);
dirty(matrix().mapRect(path.bounds()).inflate(1, 1));
restore();
}
void Graphics::drawRect(const gfx::Rect& rcOrig, const Paint& paint)
{
gfx::Rect rc(rcOrig);
rc.offset(m_dx, m_dy);
dirty(rc);
os::SurfaceLock lock(m_surface.get());
m_surface->drawRect(rc, paint);
}
void Graphics::drawRect(gfx::Color color, const gfx::Rect& rcOrig)
{
gfx::Rect rc(rcOrig);
rc.offset(m_dx, m_dy);
dirty(rc);
os::SurfaceLock lock(m_surface.get());
os::Paint paint;
paint.color(color);
paint.style(os::Paint::Stroke);
m_surface->drawRect(rc, paint);
}
void Graphics::fillRect(gfx::Color color, const gfx::Rect& rcOrig)
{
gfx::Rect rc(rcOrig);
rc.offset(m_dx, m_dy);
dirty(rc);
os::SurfaceLock lock(m_surface.get());
os::Paint paint;
paint.color(color);
paint.style(os::Paint::Fill);
m_surface->drawRect(rc, paint);
}
void Graphics::fillRegion(gfx::Color color, const gfx::Region& rgn)
{
for (gfx::Region::iterator it=rgn.begin(), end=rgn.end(); it!=end; ++it)
fillRect(color, *it);
}
void Graphics::fillAreaBetweenRects(gfx::Color color,
const gfx::Rect& outer, const gfx::Rect& inner)
{
if (!outer.intersects(inner))
fillRect(color, outer);
else {
gfx::Region rgn(outer);
rgn.createSubtraction(rgn, gfx::Region(inner));
fillRegion(color, rgn);
}
}
void Graphics::drawSurface(os::Surface* surface, int x, int y)
{
dirty(gfx::Rect(m_dx+x, m_dy+y, surface->width(), surface->height()));
os::SurfaceLock lockSrc(surface);
os::SurfaceLock lockDst(m_surface.get());
m_surface->drawSurface(surface, m_dx+x, m_dy+y);
}
void Graphics::drawSurface(os::Surface* surface,
const gfx::Rect& srcRect,
const gfx::Rect& dstRect,
const os::Sampling& sampling,
const ui::Paint* paint)
{
dirty(gfx::Rect(m_dx+dstRect.x, m_dy+dstRect.y,
dstRect.w, dstRect.h));
os::SurfaceLock lockSrc(surface);
os::SurfaceLock lockDst(m_surface.get());
m_surface->drawSurface(
surface,
srcRect,
gfx::Rect(dstRect).offset(m_dx, m_dy),
sampling,
paint);
}
void Graphics::drawRgbaSurface(os::Surface* surface, int x, int y)
{
dirty(gfx::Rect(m_dx+x, m_dy+y, surface->width(), surface->height()));
os::SurfaceLock lockSrc(surface);
os::SurfaceLock lockDst(m_surface.get());
m_surface->drawRgbaSurface(surface, m_dx+x, m_dy+y);
}
void Graphics::drawRgbaSurface(os::Surface* surface, int srcx, int srcy, int dstx, int dsty, int w, int h)
{
dirty(gfx::Rect(m_dx+dstx, m_dy+dsty, w, h));
os::SurfaceLock lockSrc(surface);
os::SurfaceLock lockDst(m_surface.get());
m_surface->drawRgbaSurface(surface, srcx, srcy, m_dx+dstx, m_dy+dsty, w, h);
}
void Graphics::drawColoredRgbaSurface(os::Surface* surface, gfx::Color color, int x, int y)
{
dirty(gfx::Rect(m_dx+x, m_dy+y, surface->width(), surface->height()));
os::SurfaceLock lockSrc(surface);
os::SurfaceLock lockDst(m_surface.get());
m_surface->drawColoredRgbaSurface(surface, color, gfx::ColorNone,
gfx::Clip(m_dx+x, m_dy+y, 0, 0, surface->width(), surface->height()));
}
void Graphics::drawColoredRgbaSurface(os::Surface* surface, gfx::Color color,
int srcx, int srcy, int dstx, int dsty, int w, int h)
{
dirty(gfx::Rect(m_dx+dstx, m_dy+dsty, w, h));
os::SurfaceLock lockSrc(surface);
os::SurfaceLock lockDst(m_surface.get());
m_surface->drawColoredRgbaSurface(surface, color, gfx::ColorNone,
gfx::Clip(m_dx+dstx, m_dy+dsty, srcx, srcy, w, h));
}
void Graphics::drawSurfaceNine(os::Surface* surface,
const gfx::Rect& src,
const gfx::Rect& center,
const gfx::Rect& dst,
const bool drawCenter,
const ui::Paint* paint)
{
gfx::Rect displacedDst(m_dx+dst.x, m_dy+dst.y, dst.w, dst.h);
dirty(displacedDst);
os::SurfaceLock lockSrc(surface);
os::SurfaceLock lockDst(m_surface.get());
m_surface->drawSurfaceNine(surface, src, center, displacedDst, drawCenter, paint);
}
void Graphics::setFont(const os::FontRef& font)
{
m_font = font;
}
void Graphics::drawText(const std::string& str,
gfx::Color fg, gfx::Color bg,
const gfx::Point& origPt,
os::DrawTextDelegate* delegate)
{
gfx::Point pt(m_dx+origPt.x, m_dy+origPt.y);
os::SurfaceLock lock(m_surface.get());
gfx::Rect textBounds =
os::draw_text(m_surface.get(), m_font.get(), str, fg, bg, pt.x, pt.y, delegate);
dirty(gfx::Rect(pt.x, pt.y, textBounds.w, textBounds.h));
}
namespace {
class DrawUITextDelegate : public os::DrawTextDelegate {
public:
DrawUITextDelegate(os::Surface* surface,
os::Font* font, const int mnemonic)
: m_surface(surface)
, m_font(font)
, m_mnemonic(std::tolower(mnemonic))
, m_underscoreColor(gfx::ColorNone) {
}
gfx::Rect bounds() const { return m_bounds; }
void preProcessChar(const int index,
const int codepoint,
gfx::Color& fg,
gfx::Color& bg,
const gfx::Rect& charBounds) override {
if (m_surface) {
if (m_mnemonic &&
// TODO use ICU library to lower unicode chars
std::tolower(codepoint) == m_mnemonic) {
m_underscoreColor = fg;
m_mnemonic = 0; // Just one time
}
else {
m_underscoreColor = gfx::ColorNone;
}
}
}
bool preDrawChar(const gfx::Rect& charBounds) override {
m_bounds |= charBounds;
return true;
}
void postDrawChar(const gfx::Rect& charBounds) override {
if (!gfx::is_transparent(m_underscoreColor)) {
// TODO underscore height = guiscale() should be configurable from ui::Theme
int dy = 0;
if (m_font->type() == os::FontType::FreeType) // TODO use other method to locate the underline
dy += guiscale();
gfx::Rect underscoreBounds(charBounds.x, charBounds.y+charBounds.h+dy,
charBounds.w, guiscale());
os::Paint paint;
paint.color(m_underscoreColor);
paint.style(os::Paint::Fill);
m_surface->drawRect(underscoreBounds, paint);
m_bounds |= underscoreBounds;
}
}
private:
os::Surface* m_surface;
os::Font* m_font;
int m_mnemonic;
gfx::Color m_underscoreColor;
gfx::Rect m_bounds;
};
}
void Graphics::drawUIText(const std::string& str, gfx::Color fg, gfx::Color bg,
const gfx::Point& pt, const int mnemonic)
{
os::SurfaceLock lock(m_surface.get());
int x = m_dx+pt.x;
int y = m_dy+pt.y;
DrawUITextDelegate delegate(m_surface.get(), m_font.get(), mnemonic);
os::draw_text(m_surface.get(), m_font.get(), str,
fg, bg, x, y, &delegate);
dirty(delegate.bounds());
}
void Graphics::drawAlignedUIText(const std::string& str, gfx::Color fg, gfx::Color bg,
const gfx::Rect& rc, const int align)
{
doUIStringAlgorithm(str, fg, bg, rc, align, true);
}
gfx::Size Graphics::measureUIText(const std::string& str)
{
return gfx::Size(
Graphics::measureUITextLength(str, m_font.get()),
m_font->height());
}
// static
int Graphics::measureUITextLength(const std::string& str, os::Font* font)
{
DrawUITextDelegate delegate(nullptr, font, 0);
os::draw_text(nullptr, font, str,
gfx::ColorNone, gfx::ColorNone, 0, 0,
&delegate);
return delegate.bounds().w;
}
gfx::Size Graphics::fitString(const std::string& str, int maxWidth, int align)
{
return doUIStringAlgorithm(str, gfx::ColorNone, gfx::ColorNone,
gfx::Rect(0, 0, maxWidth, 0), align, false);
}
gfx::Size Graphics::doUIStringAlgorithm(const std::string& str, gfx::Color fg, gfx::Color bg, const gfx::Rect& rc, int align, bool draw)
{
gfx::Point pt(0, rc.y);
if ((align & (MIDDLE | BOTTOM)) != 0) {
gfx::Size preSize = doUIStringAlgorithm(str, gfx::ColorNone, gfx::ColorNone, rc, 0, false);
if (align & MIDDLE)
pt.y = rc.y + rc.h/2 - preSize.h/2;
else if (align & BOTTOM)
pt.y = rc.y + rc.h - preSize.h;
}
gfx::Size calculatedSize(0, 0);
std::size_t beg, end, newBeg;
std::string line;
int lineSeparation = 2*guiscale();
// Draw line-by-line
for (beg=end=0; end != std::string::npos && beg<str.size(); ) {
pt.x = rc.x;
// Without word-wrap
if ((align & (WORDWRAP | CHARWRAP)) == 0) {
end = str.find('\n', beg);
if (end != std::string::npos)
newBeg = end+1;
else
newBeg = std::string::npos;
}
// With char-wrap
else if ((align & CHARWRAP) == CHARWRAP) {
for (end=beg+1; end<str.size(); ++end) {
// If we are out of the available width (rc.w) using the new "end"
if ((rc.w > 0) &&
(m_font->textLength(str.substr(beg, end-beg).c_str()) > rc.w)) {
if (end > beg+1)
--end;
break;
}
}
newBeg = end;
}
// With word-wrap
else {
std::size_t old_end = std::string::npos;
for (std::size_t new_word_beg=beg;;) {
end = str.find_first_of(" \n", new_word_beg);
// If we have already a word to print (old_end != npos), and
// we are out of the available width (rc.w) using the new "end"
if ((old_end != std::string::npos) &&
(rc.w > 0) &&
(pt.x+m_font->textLength(str.substr(beg, end-beg).c_str()) > rc.w)) {
// We go back to the "old_end" and paint from "beg" to "end"
end = old_end;
break;
}
// If we have more words to print...
else if (end != std::string::npos) {
// Force line break, now we have to paint from "beg" to "end"
if (str[end] == '\n')
break;
// White-space, this is a beginning of a new word.
new_word_beg = end+1;
}
// We are in the end of text
else
break;
old_end = end;
}
newBeg = end+1;
}
// Get the entire line to be painted
if (end != std::string::npos)
line = str.substr(beg, end-beg);
else
line = str.substr(beg);
gfx::Size lineSize(
m_font->textLength(line.c_str()),
m_font->height()+lineSeparation);
calculatedSize.w = std::max(calculatedSize.w, lineSize.w);
// Render the text
if (draw) {
int xout;
if ((align & CENTER) == CENTER)
xout = pt.x + rc.w/2 - lineSize.w/2;
else if ((align & RIGHT) == RIGHT)
xout = pt.x + rc.w - lineSize.w;
else
xout = pt.x;
drawText(line, fg, bg, gfx::Point(xout, pt.y));
if (!gfx::is_transparent(bg))
fillAreaBetweenRects(bg,
gfx::Rect(rc.x, pt.y, rc.w, lineSize.h),
gfx::Rect(xout, pt.y, lineSize.w, lineSize.h));
}
pt.y += lineSize.h;
calculatedSize.h += lineSize.h;
beg = newBeg;
if (pt.y+lineSize.h >= rc.y2())
break;
}
if (calculatedSize.h > 0)
calculatedSize.h -= lineSeparation;
// Fill bottom area
if (draw && !gfx::is_transparent(bg)) {
if (pt.y < rc.y+rc.h)
fillRect(bg, gfx::Rect(rc.x, pt.y, rc.w, rc.y+rc.h-pt.y));
}
return calculatedSize;
}
void Graphics::invalidate(const gfx::Rect& bounds)
{
dirty(gfx::Rect(bounds).offset(m_dx, m_dy));
}
void Graphics::dirty(const gfx::Rect& bounds)
{
gfx::Rect rc = m_surface->getClipBounds();
rc = rc.createIntersection(bounds);
if (!rc.isEmpty())
m_dirtyBounds |= rc;
}
//////////////////////////////////////////////////////////////////////
// ScreenGraphics
ScreenGraphics::ScreenGraphics(Display* display)
: Graphics(display, AddRef(display->surface()), 0, 0)
{
setFont(AddRef(get_theme()->getDefaultFont()));
}
ScreenGraphics::~ScreenGraphics()
{
}
} // namespace ui