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
|
#include "Painter.h"
#include "FrameBuffer.h"
#include "Widget.h"
#include "Font.h"
#include "Window.h"
#include <AK/Assertions.h>
#include <AK/StdLibExtras.h>
Painter::Painter(Widget& widget)
: m_widget(widget)
, m_font(&widget.font())
{
m_target = widget.backing();
ASSERT(m_target);
m_window = widget.window();
m_translation.moveBy(widget.relativePosition());
m_clipRect.setWidth(AbstractScreen::the().width());
m_clipRect.setHeight(AbstractScreen::the().height());
}
Painter::~Painter()
{
}
void Painter::fillRect(const Rect& rect, Color color)
{
Rect r = rect;
r.moveBy(m_translation);
for (int y = max(r.top(), m_clipRect.top()); y < min(r.bottom(), m_clipRect.bottom()); ++y) {
auto* bits = m_target->scanline(y);
for (int x = max(r.left(), m_clipRect.left()); x < min(r.right(), m_clipRect.right()); ++x) {
bits[x] = color.value();
}
}
}
void Painter::drawRect(const Rect& rect, Color color)
{
Rect r = rect;
r.moveBy(m_translation);
for (int y = max(r.top(), m_clipRect.top()); y < min(r.bottom(), m_clipRect.bottom()); ++y) {
auto* bits = m_target->scanline(y);
if (y == r.top() || y == (r.bottom() - 1)) {
for (int x = max(r.left(), m_clipRect.left()); x < min(r.right(), m_clipRect.right()); ++x) {
bits[x] = color.value();
}
} else {
if (r.left() >= m_clipRect.left() && r.left() < m_clipRect.right())
bits[r.left()] = color.value();
if (r.right() >= m_clipRect.left() && r.right() < m_clipRect.right())
bits[r.right() - 1] = color.value();
}
}
}
void Painter::xorRect(const Rect& rect, Color color)
{
Rect r = rect;
r.moveBy(m_translation);
for (int y = max(r.top(), m_clipRect.top()); y < min(r.bottom(), m_clipRect.bottom()); ++y) {
auto* bits = m_target->scanline(y);
if (y == r.top() || y == (r.bottom() - 1)) {
for (int x = max(r.left(), m_clipRect.left()); x < min(r.right(), m_clipRect.right()); ++x) {
bits[x] ^= color.value();
}
} else {
if (r.left() >= m_clipRect.left() && r.left() < m_clipRect.right())
bits[r.left()] ^= color.value();
if (r.right() >= m_clipRect.left() && r.right() < m_clipRect.right())
bits[r.right() - 1] ^= color.value();
}
}
}
void Painter::drawBitmap(const Point& p, const CharacterBitmap& bitmap, Color color)
{
Point point = p;
point.moveBy(m_translation);
for (unsigned row = 0; row < bitmap.height(); ++row) {
int y = point.y() + row;
if (y < m_clipRect.top() || y >= m_clipRect.bottom())
break;
auto* bits = m_target->scanline(y);
for (unsigned j = 0; j < bitmap.width(); ++j) {
int x = point.x() + j;
if (x < m_clipRect.left() || x >= m_clipRect.right())
break;
char fc = bitmap.bits()[row * bitmap.width() + j];
if (fc == '#')
bits[x] = color.value();
}
}
}
void Painter::drawText(const Rect& rect, const String& text, TextAlignment alignment, Color color)
{
Point point;
if (alignment == TextAlignment::TopLeft) {
point = rect.location();
} else if (alignment == TextAlignment::CenterLeft) {
int textWidth = text.length() * font().glyphWidth();
point = { rect.x(), rect.center().y() - (font().glyphHeight() / 2) };
} else if (alignment == TextAlignment::Center) {
int textWidth = text.length() * font().glyphWidth();
point = rect.center();
point.moveBy(-(textWidth / 2), -(font().glyphHeight() / 2));
} else {
ASSERT_NOT_REACHED();
}
for (unsigned i = 0; i < text.length(); ++i) {
byte ch = text[i];
if (ch == ' ')
continue;
auto* bitmap = font().glyphBitmap(ch);
if (!bitmap) {
printf("Font doesn't have 0x%02x ('%c')\n", ch, ch);
ASSERT_NOT_REACHED();
}
int x = point.x() + i * font().glyphWidth();
int y = point.y();
drawBitmap({ x, y }, *bitmap, color);
}
}
void Painter::drawPixel(const Point& p, Color color)
{
auto point = p;
point.moveBy(m_translation);
if (!m_clipRect.contains(point))
return;
m_target->scanline(point.y())[point.x()] = color.value();
}
void Painter::drawLine(const Point& p1, const Point& p2, Color color)
{
auto point1 = p1;
point1.moveBy(m_translation);
auto point2 = p2;
point2.moveBy(m_translation);
// Special case: vertical line.
if (point1.x() == point2.x()) {
const int x = point1.x();
if (x < m_clipRect.left() || x >= m_clipRect.right())
return;
if (point1.y() > point2.y())
std::swap(point1, point2);
for (int y = max(point1.y(), m_clipRect.top()); y <= min(point2.y(), m_clipRect.bottom()); ++y)
m_target->scanline(y)[x] = color.value();
return;
}
if (point1.x() > point2.x())
std::swap(point1, point2);
// Special case: horizontal line.
if (point1.y() == point2.y()) {
const int y = point1.y();
if (y < m_clipRect.top() || y >= m_clipRect.bottom())
return;
if (point1.x() > point2.x())
std::swap(point1, point2);
auto* pixels = m_target->scanline(point1.y());
for (int x = max(point1.x(), m_clipRect.left()); x <= min(point2.x(), m_clipRect.right()); ++x)
pixels[x] = color.value();
return;
}
// FIXME: Implement clipping below.
ASSERT_NOT_REACHED();
const double dx = point2.x() - point1.x();
const double dy = point2.y() - point1.y();
const double deltaError = fabs(dy / dx);
double error = 0;
const double yStep = dy == 0 ? 0 : (dy > 0 ? 1 : -1);
int y = point1.y();
for (int x = point1.x(); x <= point2.x(); ++x) {
m_target->scanline(y)[x] = color.value();
error += deltaError;
if (error >= 0.5) {
y = (double)y + yStep;
error -= 1.0;
}
}
}
void Painter::drawFocusRect(const Rect& rect)
{
Rect focusRect = rect;
focusRect.moveBy(1, 1);
focusRect.setWidth(focusRect.width() - 2);
focusRect.setHeight(focusRect.height() - 2);
drawRect(focusRect, Color(96, 96, 192));
}
|