Serenity Operating System
at hosted 1013 lines 39 kB view raw
1/* 2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, this 9 * list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27#include "Painter.h" 28#include "Bitmap.h" 29#include "Emoji.h" 30#include "Font.h" 31#include <AK/Assertions.h> 32#include <AK/Function.h> 33#include <AK/Memory.h> 34#include <AK/StdLibExtras.h> 35#include <AK/StringBuilder.h> 36#include <AK/Utf8View.h> 37#include <LibGfx/CharacterBitmap.h> 38#include <math.h> 39#include <stdio.h> 40#include <unistd.h> 41 42#if defined(__GNUC__) && !defined(__clang__) 43# pragma GCC optimize("O3") 44#endif 45 46#ifndef ALWAYS_INLINE 47# if __has_attribute(always_inline) 48# define ALWAYS_INLINE __attribute__((always_inline)) 49# else 50# define ALWAYS_INLINE inline 51# endif 52#endif 53 54namespace Gfx { 55 56template<BitmapFormat format = BitmapFormat::Invalid> 57static ALWAYS_INLINE Color get_pixel(const Gfx::Bitmap& bitmap, int x, int y) 58{ 59 if constexpr (format == BitmapFormat::Indexed8) 60 return bitmap.palette_color(bitmap.bits(y)[x]); 61 if constexpr (format == BitmapFormat::RGB32) 62 return Color::from_rgb(bitmap.scanline(y)[x]); 63 if constexpr (format == BitmapFormat::RGBA32) 64 return Color::from_rgba(bitmap.scanline(y)[x]); 65 return bitmap.get_pixel(x, y); 66} 67 68Painter::Painter(Gfx::Bitmap& bitmap) 69 : m_target(bitmap) 70{ 71 m_state_stack.append(State()); 72 state().font = &Font::default_font(); 73 state().clip_rect = { { 0, 0 }, bitmap.size() }; 74 m_clip_origin = state().clip_rect; 75} 76 77Painter::~Painter() 78{ 79} 80 81void Painter::fill_rect_with_draw_op(const Rect& a_rect, Color color) 82{ 83 auto rect = a_rect.translated(translation()).intersected(clip_rect()); 84 if (rect.is_empty()) 85 return; 86 87 RGBA32* dst = m_target->scanline(rect.top()) + rect.left(); 88 const size_t dst_skip = m_target->pitch() / sizeof(RGBA32); 89 90 for (int i = rect.height() - 1; i >= 0; --i) { 91 for (int j = 0; j < rect.width(); ++j) 92 set_pixel_with_draw_op(dst[j], color); 93 dst += dst_skip; 94 } 95} 96 97void Painter::clear_rect(const Rect& a_rect, Color color) 98{ 99 auto rect = a_rect.translated(translation()).intersected(clip_rect()); 100 if (rect.is_empty()) 101 return; 102 103 ASSERT(m_target->rect().contains(rect)); 104 105 RGBA32* dst = m_target->scanline(rect.top()) + rect.left(); 106 const size_t dst_skip = m_target->pitch() / sizeof(RGBA32); 107 108 for (int i = rect.height() - 1; i >= 0; --i) { 109 fast_u32_fill(dst, color.value(), rect.width()); 110 dst += dst_skip; 111 } 112} 113 114void Painter::fill_rect(const Rect& a_rect, Color color) 115{ 116 if (color.alpha() == 0) 117 return; 118 119 if (draw_op() != DrawOp::Copy) { 120 fill_rect_with_draw_op(a_rect, color); 121 return; 122 } 123 124 if (color.alpha() == 0xff) { 125 clear_rect(a_rect, color); 126 return; 127 } 128 129 auto rect = a_rect.translated(translation()).intersected(clip_rect()); 130 if (rect.is_empty()) 131 return; 132 133 ASSERT(m_target->rect().contains(rect)); 134 135 RGBA32* dst = m_target->scanline(rect.top()) + rect.left(); 136 const size_t dst_skip = m_target->pitch() / sizeof(RGBA32); 137 138 for (int i = rect.height() - 1; i >= 0; --i) { 139 for (int j = 0; j < rect.width(); ++j) 140 dst[j] = Color::from_rgba(dst[j]).blend(color).value(); 141 dst += dst_skip; 142 } 143} 144 145void Painter::fill_rect_with_checkerboard(const Rect& a_rect, const Size& cell_size, Color color_dark, Color color_light) 146{ 147 auto rect = a_rect.translated(translation()).intersected(clip_rect()); 148 if (rect.is_empty()) 149 return; 150 151 RGBA32* dst = m_target->scanline(rect.top()) + rect.left(); 152 const size_t dst_skip = m_target->pitch() / sizeof(RGBA32); 153 154 for (int i = 0; i < rect.height(); ++i) { 155 for (int j = 0; j < rect.width(); ++j) { 156 int cell_row = i / cell_size.height(); 157 int cell_col = j / cell_size.width(); 158 dst[j] = ((cell_row % 2) ^ (cell_col % 2)) ? color_light.value() : color_dark.value(); 159 } 160 dst += dst_skip; 161 } 162} 163 164void Painter::fill_rect_with_gradient(Orientation orientation, const Rect& a_rect, Color gradient_start, Color gradient_end) 165{ 166#ifdef NO_FPU 167 return fill_rect(a_rect, gradient_start); 168#endif 169 auto rect = a_rect.translated(translation()); 170 auto clipped_rect = Rect::intersection(rect, clip_rect()); 171 if (clipped_rect.is_empty()) 172 return; 173 174 int offset = clipped_rect.primary_offset_for_orientation(orientation) - rect.primary_offset_for_orientation(orientation); 175 176 RGBA32* dst = m_target->scanline(clipped_rect.top()) + clipped_rect.left(); 177 const size_t dst_skip = m_target->pitch() / sizeof(RGBA32); 178 179 float increment = (1.0 / ((rect.primary_size_for_orientation(orientation)) / 255.0)); 180 181 int r2 = gradient_start.red(); 182 int g2 = gradient_start.green(); 183 int b2 = gradient_start.blue(); 184 int r1 = gradient_end.red(); 185 int g1 = gradient_end.green(); 186 int b1 = gradient_end.blue(); 187 188 if (orientation == Orientation::Horizontal) { 189 for (int i = clipped_rect.height() - 1; i >= 0; --i) { 190 float c = offset * increment; 191 for (int j = 0; j < clipped_rect.width(); ++j) { 192 dst[j] = Color( 193 r1 / 255.0 * c + r2 / 255.0 * (255 - c), 194 g1 / 255.0 * c + g2 / 255.0 * (255 - c), 195 b1 / 255.0 * c + b2 / 255.0 * (255 - c)) 196 .value(); 197 c += increment; 198 } 199 dst += dst_skip; 200 } 201 } else { 202 float c = offset * increment; 203 for (int i = clipped_rect.height() - 1; i >= 0; --i) { 204 Color color( 205 r1 / 255.0 * c + r2 / 255.0 * (255 - c), 206 g1 / 255.0 * c + g2 / 255.0 * (255 - c), 207 b1 / 255.0 * c + b2 / 255.0 * (255 - c)); 208 for (int j = 0; j < clipped_rect.width(); ++j) { 209 dst[j] = color.value(); 210 } 211 c += increment; 212 dst += dst_skip; 213 } 214 } 215} 216 217void Painter::fill_rect_with_gradient(const Rect& a_rect, Color gradient_start, Color gradient_end) 218{ 219 return fill_rect_with_gradient(Orientation::Horizontal, a_rect, gradient_start, gradient_end); 220} 221 222void Painter::draw_ellipse_intersecting(const Rect& rect, Color color, int thickness) 223{ 224 constexpr int number_samples = 100; // FIXME: dynamically work out the number of samples based upon the rect size 225 double increment = M_PI / number_samples; 226 227 auto ellipse_x = [&](double theta) -> int { 228 return (cos(theta) * rect.width() / sqrt(2)) + rect.center().x(); 229 }; 230 231 auto ellipse_y = [&](double theta) -> int { 232 return (sin(theta) * rect.height() / sqrt(2)) + rect.center().y(); 233 }; 234 235 for (float theta = 0; theta < 2 * M_PI; theta += increment) { 236 draw_line({ ellipse_x(theta), ellipse_y(theta) }, { ellipse_x(theta + increment), ellipse_y(theta + increment) }, color, thickness); 237 } 238} 239 240void Painter::draw_rect(const Rect& a_rect, Color color, bool rough) 241{ 242 Rect rect = a_rect.translated(translation()); 243 auto clipped_rect = rect.intersected(clip_rect()); 244 if (clipped_rect.is_empty()) 245 return; 246 247 int min_y = clipped_rect.top(); 248 int max_y = clipped_rect.bottom(); 249 250 if (rect.top() >= clipped_rect.top() && rect.top() <= clipped_rect.bottom()) { 251 int start_x = rough ? max(rect.x() + 1, clipped_rect.x()) : clipped_rect.x(); 252 int width = rough ? min(rect.width() - 2, clipped_rect.width()) : clipped_rect.width(); 253 fast_u32_fill(m_target->scanline(rect.top()) + start_x, color.value(), width); 254 ++min_y; 255 } 256 if (rect.bottom() >= clipped_rect.top() && rect.bottom() <= clipped_rect.bottom()) { 257 int start_x = rough ? max(rect.x() + 1, clipped_rect.x()) : clipped_rect.x(); 258 int width = rough ? min(rect.width() - 2, clipped_rect.width()) : clipped_rect.width(); 259 fast_u32_fill(m_target->scanline(rect.bottom()) + start_x, color.value(), width); 260 --max_y; 261 } 262 263 bool draw_left_side = rect.left() >= clipped_rect.left(); 264 bool draw_right_side = rect.right() == clipped_rect.right(); 265 266 if (draw_left_side && draw_right_side) { 267 // Specialized loop when drawing both sides. 268 for (int y = min_y; y <= max_y; ++y) { 269 auto* bits = m_target->scanline(y); 270 bits[rect.left()] = color.value(); 271 bits[rect.right()] = color.value(); 272 } 273 } else { 274 for (int y = min_y; y <= max_y; ++y) { 275 auto* bits = m_target->scanline(y); 276 if (draw_left_side) 277 bits[rect.left()] = color.value(); 278 if (draw_right_side) 279 bits[rect.right()] = color.value(); 280 } 281 } 282} 283 284void Painter::draw_bitmap(const Point& p, const CharacterBitmap& bitmap, Color color) 285{ 286 auto rect = Rect(p, bitmap.size()).translated(translation()); 287 auto clipped_rect = rect.intersected(clip_rect()); 288 if (clipped_rect.is_empty()) 289 return; 290 const int first_row = clipped_rect.top() - rect.top(); 291 const int last_row = clipped_rect.bottom() - rect.top(); 292 const int first_column = clipped_rect.left() - rect.left(); 293 const int last_column = clipped_rect.right() - rect.left(); 294 RGBA32* dst = m_target->scanline(clipped_rect.y()) + clipped_rect.x(); 295 const size_t dst_skip = m_target->pitch() / sizeof(RGBA32); 296 const char* bitmap_row = &bitmap.bits()[first_row * bitmap.width() + first_column]; 297 const size_t bitmap_skip = bitmap.width(); 298 299 for (int row = first_row; row <= last_row; ++row) { 300 for (int j = 0; j <= (last_column - first_column); ++j) { 301 char fc = bitmap_row[j]; 302 if (fc == '#') 303 dst[j] = color.value(); 304 } 305 bitmap_row += bitmap_skip; 306 dst += dst_skip; 307 } 308} 309 310void Painter::draw_bitmap(const Point& p, const GlyphBitmap& bitmap, Color color) 311{ 312 auto dst_rect = Rect(p, bitmap.size()).translated(translation()); 313 auto clipped_rect = dst_rect.intersected(clip_rect()); 314 if (clipped_rect.is_empty()) 315 return; 316 const int first_row = clipped_rect.top() - dst_rect.top(); 317 const int last_row = clipped_rect.bottom() - dst_rect.top(); 318 const int first_column = clipped_rect.left() - dst_rect.left(); 319 const int last_column = clipped_rect.right() - dst_rect.left(); 320 RGBA32* dst = m_target->scanline(clipped_rect.y()) + clipped_rect.x(); 321 const size_t dst_skip = m_target->pitch() / sizeof(RGBA32); 322 323 for (int row = first_row; row <= last_row; ++row) { 324 for (int j = 0; j <= (last_column - first_column); ++j) { 325 if (bitmap.bit_at(j + first_column, row)) 326 dst[j] = color.value(); 327 } 328 dst += dst_skip; 329 } 330} 331 332void Painter::blit_scaled(const Rect& dst_rect_raw, const Gfx::Bitmap& source, const Rect& src_rect, float hscale, float vscale) 333{ 334 auto dst_rect = Rect(dst_rect_raw.location(), dst_rect_raw.size()).translated(translation()); 335 auto clipped_rect = dst_rect.intersected(clip_rect()); 336 if (clipped_rect.is_empty()) 337 return; 338 const int first_row = (clipped_rect.top() - dst_rect.top()); 339 const int last_row = (clipped_rect.bottom() - dst_rect.top()); 340 const int first_column = (clipped_rect.left() - dst_rect.left()); 341 RGBA32* dst = m_target->scanline(clipped_rect.y()) + clipped_rect.x(); 342 const size_t dst_skip = m_target->pitch() / sizeof(RGBA32); 343 344 int x_start = first_column + src_rect.left(); 345 for (int row = first_row; row <= last_row; ++row) { 346 int sr = (row + src_rect.top()) * vscale; 347 if (sr >= source.size().height() || sr < 0) { 348 dst += dst_skip; 349 continue; 350 } 351 const RGBA32* sl = source.scanline(sr); 352 for (int x = x_start; x < clipped_rect.width() + x_start; ++x) { 353 int sx = x * hscale; 354 if (sx < source.size().width() && sx >= 0) 355 dst[x - x_start] = sl[sx]; 356 } 357 dst += dst_skip; 358 } 359 return; 360} 361 362void Painter::blit_with_opacity(const Point& position, const Gfx::Bitmap& source, const Rect& src_rect, float opacity) 363{ 364 ASSERT(!m_target->has_alpha_channel()); 365 366 if (!opacity) 367 return; 368 if (opacity >= 1.0f) 369 return blit(position, source, src_rect); 370 371 u8 alpha = 255 * opacity; 372 373 Rect safe_src_rect = Rect::intersection(src_rect, source.rect()); 374 Rect dst_rect(position, safe_src_rect.size()); 375 dst_rect.move_by(state().translation); 376 auto clipped_rect = Rect::intersection(dst_rect, clip_rect()); 377 if (clipped_rect.is_empty()) 378 return; 379 const int first_row = clipped_rect.top() - dst_rect.top(); 380 const int last_row = clipped_rect.bottom() - dst_rect.top(); 381 const int first_column = clipped_rect.left() - dst_rect.left(); 382 const int last_column = clipped_rect.right() - dst_rect.left(); 383 RGBA32* dst = m_target->scanline(clipped_rect.y()) + clipped_rect.x(); 384 const RGBA32* src = source.scanline(src_rect.top() + first_row) + src_rect.left() + first_column; 385 const size_t dst_skip = m_target->pitch() / sizeof(RGBA32); 386 const unsigned src_skip = source.pitch() / sizeof(RGBA32); 387 388 for (int row = first_row; row <= last_row; ++row) { 389 for (int x = 0; x <= (last_column - first_column); ++x) { 390 Color src_color_with_alpha = Color::from_rgb(src[x]); 391 src_color_with_alpha.set_alpha(alpha); 392 Color dst_color = Color::from_rgb(dst[x]); 393 dst[x] = dst_color.blend(src_color_with_alpha).value(); 394 } 395 dst += dst_skip; 396 src += src_skip; 397 } 398} 399 400void Painter::blit_filtered(const Point& position, const Gfx::Bitmap& source, const Rect& src_rect, Function<Color(Color)> filter) 401{ 402 Rect safe_src_rect = src_rect.intersected(source.rect()); 403 auto dst_rect = Rect(position, safe_src_rect.size()).translated(translation()); 404 auto clipped_rect = dst_rect.intersected(clip_rect()); 405 if (clipped_rect.is_empty()) 406 return; 407 const int first_row = clipped_rect.top() - dst_rect.top(); 408 const int last_row = clipped_rect.bottom() - dst_rect.top(); 409 const int first_column = clipped_rect.left() - dst_rect.left(); 410 const int last_column = clipped_rect.right() - dst_rect.left(); 411 RGBA32* dst = m_target->scanline(clipped_rect.y()) + clipped_rect.x(); 412 const RGBA32* src = source.scanline(src_rect.top() + first_row) + src_rect.left() + first_column; 413 const size_t dst_skip = m_target->pitch() / sizeof(RGBA32); 414 const size_t src_skip = source.pitch() / sizeof(RGBA32); 415 416 for (int row = first_row; row <= last_row; ++row) { 417 for (int x = 0; x <= (last_column - first_column); ++x) { 418 u8 alpha = Color::from_rgba(src[x]).alpha(); 419 if (alpha == 0xff) 420 dst[x] = filter(Color::from_rgba(src[x])).value(); 421 else if (!alpha) 422 continue; 423 else 424 dst[x] = Color::from_rgba(dst[x]).blend(filter(Color::from_rgba(src[x]))).value(); 425 } 426 dst += dst_skip; 427 src += src_skip; 428 } 429} 430 431void Painter::blit_brightened(const Point& position, const Gfx::Bitmap& source, const Rect& src_rect) 432{ 433 return blit_filtered(position, source, src_rect, [](Color src) { 434 return src.lightened(); 435 }); 436} 437 438void Painter::blit_dimmed(const Point& position, const Gfx::Bitmap& source, const Rect& src_rect) 439{ 440 return blit_filtered(position, source, src_rect, [](Color src) { 441 return src.to_grayscale().lightened(); 442 }); 443} 444 445void Painter::draw_tiled_bitmap(const Rect& a_dst_rect, const Gfx::Bitmap& source) 446{ 447 auto dst_rect = a_dst_rect.translated(translation()); 448 auto clipped_rect = dst_rect.intersected(clip_rect()); 449 if (clipped_rect.is_empty()) 450 return; 451 const int first_row = (clipped_rect.top() - dst_rect.top()); 452 const int last_row = (clipped_rect.bottom() - dst_rect.top()); 453 const int first_column = (clipped_rect.left() - dst_rect.left()); 454 RGBA32* dst = m_target->scanline(clipped_rect.y()) + clipped_rect.x(); 455 const size_t dst_skip = m_target->pitch() / sizeof(RGBA32); 456 457 if (source.format() == BitmapFormat::RGB32 || source.format() == BitmapFormat::RGBA32) { 458 int x_start = first_column + a_dst_rect.left(); 459 for (int row = first_row; row <= last_row; ++row) { 460 const RGBA32* sl = source.scanline((row + a_dst_rect.top()) 461 % source.size().height()); 462 for (int x = x_start; x < clipped_rect.width() + x_start; ++x) { 463 dst[x - x_start] = sl[x % source.size().width()]; 464 } 465 dst += dst_skip; 466 } 467 return; 468 } 469 470 ASSERT_NOT_REACHED(); 471} 472 473void Painter::blit_offset(const Point& position, 474 const Gfx::Bitmap& source, 475 const Rect& src_rect, 476 const Point& offset) 477{ 478 auto dst_rect = Rect(position, src_rect.size()).translated(translation()); 479 auto clipped_rect = dst_rect.intersected(clip_rect()); 480 if (clipped_rect.is_empty()) 481 return; 482 const int first_row = (clipped_rect.top() - dst_rect.top()); 483 const int last_row = (clipped_rect.bottom() - dst_rect.top()); 484 const int first_column = (clipped_rect.left() - dst_rect.left()); 485 RGBA32* dst = m_target->scanline(clipped_rect.y()) + clipped_rect.x(); 486 const size_t dst_skip = m_target->pitch() / sizeof(RGBA32); 487 488 if (source.format() == BitmapFormat::RGB32 || source.format() == BitmapFormat::RGBA32) { 489 int x_start = first_column + src_rect.left(); 490 for (int row = first_row; row <= last_row; ++row) { 491 int sr = row - offset.y() + src_rect.top(); 492 if (sr >= source.size().height() || sr < 0) { 493 dst += dst_skip; 494 continue; 495 } 496 const RGBA32* sl = source.scanline(sr); 497 for (int x = x_start; x < clipped_rect.width() + x_start; ++x) { 498 int sx = x - offset.x(); 499 if (sx < source.size().width() && sx >= 0) 500 dst[x - x_start] = sl[sx]; 501 } 502 dst += dst_skip; 503 } 504 return; 505 } 506 507 ASSERT_NOT_REACHED(); 508} 509 510void Painter::blit_with_alpha(const Point& position, const Gfx::Bitmap& source, const Rect& src_rect) 511{ 512 ASSERT(source.has_alpha_channel()); 513 Rect safe_src_rect = src_rect.intersected(source.rect()); 514 auto dst_rect = Rect(position, safe_src_rect.size()).translated(translation()); 515 auto clipped_rect = dst_rect.intersected(clip_rect()); 516 if (clipped_rect.is_empty()) 517 return; 518 const int first_row = clipped_rect.top() - dst_rect.top(); 519 const int last_row = clipped_rect.bottom() - dst_rect.top(); 520 const int first_column = clipped_rect.left() - dst_rect.left(); 521 const int last_column = clipped_rect.right() - dst_rect.left(); 522 RGBA32* dst = m_target->scanline(clipped_rect.y()) + clipped_rect.x(); 523 const RGBA32* src = source.scanline(src_rect.top() + first_row) + src_rect.left() + first_column; 524 const size_t dst_skip = m_target->pitch() / sizeof(RGBA32); 525 const size_t src_skip = source.pitch() / sizeof(RGBA32); 526 527 for (int row = first_row; row <= last_row; ++row) { 528 for (int x = 0; x <= (last_column - first_column); ++x) { 529 u8 alpha = Color::from_rgba(src[x]).alpha(); 530 if (alpha == 0xff) 531 dst[x] = src[x]; 532 else if (!alpha) 533 continue; 534 else 535 dst[x] = Color::from_rgba(dst[x]).blend(Color::from_rgba(src[x])).value(); 536 } 537 dst += dst_skip; 538 src += src_skip; 539 } 540} 541 542void Painter::blit(const Point& position, const Gfx::Bitmap& source, const Rect& src_rect, float opacity) 543{ 544 if (opacity < 1.0f) 545 return blit_with_opacity(position, source, src_rect, opacity); 546 if (source.has_alpha_channel()) 547 return blit_with_alpha(position, source, src_rect); 548 auto safe_src_rect = src_rect.intersected(source.rect()); 549 ASSERT(source.rect().contains(safe_src_rect)); 550 auto dst_rect = Rect(position, safe_src_rect.size()).translated(translation()); 551 auto clipped_rect = dst_rect.intersected(clip_rect()); 552 if (clipped_rect.is_empty()) 553 return; 554 const int first_row = clipped_rect.top() - dst_rect.top(); 555 const int last_row = clipped_rect.bottom() - dst_rect.top(); 556 const int first_column = clipped_rect.left() - dst_rect.left(); 557 RGBA32* dst = m_target->scanline(clipped_rect.y()) + clipped_rect.x(); 558 const size_t dst_skip = m_target->pitch() / sizeof(RGBA32); 559 560 if (source.format() == BitmapFormat::RGB32 || source.format() == BitmapFormat::RGBA32) { 561 const RGBA32* src = source.scanline(src_rect.top() + first_row) + src_rect.left() + first_column; 562 const size_t src_skip = source.pitch() / sizeof(RGBA32); 563 for (int row = first_row; row <= last_row; ++row) { 564 fast_u32_copy(dst, src, clipped_rect.width()); 565 dst += dst_skip; 566 src += src_skip; 567 } 568 return; 569 } 570 571 if (source.format() == BitmapFormat::Indexed8) { 572 const u8* src = source.bits(src_rect.top() + first_row) + src_rect.left() + first_column; 573 const size_t src_skip = source.pitch(); 574 for (int row = first_row; row <= last_row; ++row) { 575 for (int i = 0; i < clipped_rect.width(); ++i) 576 dst[i] = source.palette_color(src[i]).value(); 577 dst += dst_skip; 578 src += src_skip; 579 } 580 return; 581 } 582 583 ASSERT_NOT_REACHED(); 584} 585 586template<bool has_alpha_channel, typename GetPixel> 587ALWAYS_INLINE static void do_draw_integer_scaled_bitmap(Gfx::Bitmap& target, const Rect& dst_rect, const Gfx::Bitmap& source, int hfactor, int vfactor, GetPixel get_pixel) 588{ 589 for (int y = source.rect().top(); y <= source.rect().bottom(); ++y) { 590 int dst_y = dst_rect.y() + y * vfactor; 591 for (int x = source.rect().left(); x <= source.rect().right(); ++x) { 592 auto src_pixel = get_pixel(source, x, y); 593 for (int yo = 0; yo < vfactor; ++yo) { 594 auto* scanline = (Color*)target.scanline(dst_y + yo); 595 int dst_x = dst_rect.x() + x * hfactor; 596 for (int xo = 0; xo < hfactor; ++xo) { 597 if constexpr (has_alpha_channel) 598 scanline[dst_x + xo] = scanline[dst_x + xo].blend(src_pixel); 599 else 600 scanline[dst_x + xo] = src_pixel; 601 } 602 } 603 } 604 } 605} 606 607template<bool has_alpha_channel, typename GetPixel> 608ALWAYS_INLINE static void do_draw_scaled_bitmap(Gfx::Bitmap& target, const Rect& dst_rect, const Rect& clipped_rect, const Gfx::Bitmap& source, const Rect& src_rect, int hscale, int vscale, GetPixel get_pixel) 609{ 610 if (dst_rect == clipped_rect && !(dst_rect.width() % src_rect.width()) && !(dst_rect.height() % src_rect.height())) { 611 int hfactor = dst_rect.width() / src_rect.width(); 612 int vfactor = dst_rect.height() / src_rect.height(); 613 if (hfactor == 2 && vfactor == 2) 614 return do_draw_integer_scaled_bitmap<has_alpha_channel>(target, dst_rect, source, 2, 2, get_pixel); 615 if (hfactor == 3 && vfactor == 3) 616 return do_draw_integer_scaled_bitmap<has_alpha_channel>(target, dst_rect, source, 3, 3, get_pixel); 617 if (hfactor == 4 && vfactor == 4) 618 return do_draw_integer_scaled_bitmap<has_alpha_channel>(target, dst_rect, source, 4, 4, get_pixel); 619 return do_draw_integer_scaled_bitmap<has_alpha_channel>(target, dst_rect, source, hfactor, vfactor, get_pixel); 620 } 621 622 for (int y = clipped_rect.top(); y <= clipped_rect.bottom(); ++y) { 623 auto* scanline = (Color*)target.scanline(y); 624 for (int x = clipped_rect.left(); x <= clipped_rect.right(); ++x) { 625 auto scaled_x = ((x - dst_rect.x()) * hscale) >> 16; 626 auto scaled_y = ((y - dst_rect.y()) * vscale) >> 16; 627 auto src_pixel = get_pixel(source, scaled_x, scaled_y); 628 629 if constexpr (has_alpha_channel) { 630 scanline[x] = scanline[x].blend(src_pixel); 631 } else 632 scanline[x] = src_pixel; 633 } 634 } 635} 636 637void Painter::draw_scaled_bitmap(const Rect& a_dst_rect, const Gfx::Bitmap& source, const Rect& src_rect) 638{ 639 auto dst_rect = a_dst_rect; 640 if (dst_rect.size() == src_rect.size()) 641 return blit(dst_rect.location(), source, src_rect); 642 643 auto safe_src_rect = src_rect.intersected(source.rect()); 644 ASSERT(source.rect().contains(safe_src_rect)); 645 dst_rect.move_by(state().translation); 646 auto clipped_rect = dst_rect.intersected(clip_rect()); 647 if (clipped_rect.is_empty()) 648 return; 649 650 int hscale = (src_rect.width() << 16) / dst_rect.width(); 651 int vscale = (src_rect.height() << 16) / dst_rect.height(); 652 653 if (source.has_alpha_channel()) { 654 switch (source.format()) { 655 case BitmapFormat::RGB32: 656 do_draw_scaled_bitmap<true>(*m_target, dst_rect, clipped_rect, source, src_rect, hscale, vscale, get_pixel<BitmapFormat::RGB32>); 657 break; 658 case BitmapFormat::RGBA32: 659 do_draw_scaled_bitmap<true>(*m_target, dst_rect, clipped_rect, source, src_rect, hscale, vscale, get_pixel<BitmapFormat::RGBA32>); 660 break; 661 case BitmapFormat::Indexed8: 662 do_draw_scaled_bitmap<true>(*m_target, dst_rect, clipped_rect, source, src_rect, hscale, vscale, get_pixel<BitmapFormat::Indexed8>); 663 break; 664 default: 665 do_draw_scaled_bitmap<true>(*m_target, dst_rect, clipped_rect, source, src_rect, hscale, vscale, get_pixel<BitmapFormat::Invalid>); 666 break; 667 } 668 } else { 669 switch (source.format()) { 670 case BitmapFormat::RGB32: 671 do_draw_scaled_bitmap<false>(*m_target, dst_rect, clipped_rect, source, src_rect, hscale, vscale, get_pixel<BitmapFormat::RGB32>); 672 break; 673 case BitmapFormat::RGBA32: 674 do_draw_scaled_bitmap<false>(*m_target, dst_rect, clipped_rect, source, src_rect, hscale, vscale, get_pixel<BitmapFormat::RGBA32>); 675 break; 676 case BitmapFormat::Indexed8: 677 do_draw_scaled_bitmap<false>(*m_target, dst_rect, clipped_rect, source, src_rect, hscale, vscale, get_pixel<BitmapFormat::Indexed8>); 678 break; 679 default: 680 do_draw_scaled_bitmap<false>(*m_target, dst_rect, clipped_rect, source, src_rect, hscale, vscale, get_pixel<BitmapFormat::Invalid>); 681 break; 682 } 683 } 684} 685 686[[gnu::flatten]] void Painter::draw_glyph(const Point& point, char ch, Color color) 687{ 688 draw_glyph(point, ch, font(), color); 689} 690 691[[gnu::flatten]] void Painter::draw_glyph(const Point& point, char ch, const Font& font, Color color) 692{ 693 draw_bitmap(point, font.glyph_bitmap(ch), color); 694} 695 696void Painter::draw_emoji(const Point& point, const Gfx::Bitmap& emoji, const Font& font) 697{ 698 if (!font.is_fixed_width()) 699 blit(point, emoji, emoji.rect()); 700 else { 701 Rect dst_rect { 702 point.x(), 703 point.y(), 704 font.glyph_width('x'), 705 font.glyph_height() 706 }; 707 draw_scaled_bitmap(dst_rect, emoji, emoji.rect()); 708 } 709} 710 711void Painter::draw_glyph_or_emoji(const Point& point, u32 codepoint, const Font& font, Color color) 712{ 713 if (codepoint < 256) { 714 // This looks like a regular character. 715 draw_glyph(point, (char)codepoint, font, color); 716 return; 717 } 718 719 // Perhaps it's an emoji? 720 auto* emoji = Emoji::emoji_for_codepoint(codepoint); 721 if (emoji == nullptr) { 722#ifdef EMOJI_DEBUG 723 dbg() << "Failed to find an emoji for codepoint " << codepoint; 724#endif 725 draw_glyph(point, '?', font, color); 726 return; 727 } 728 729 draw_emoji(point, *emoji, font); 730} 731 732void Painter::draw_text_line(const Rect& a_rect, const Utf8View& text, const Font& font, TextAlignment alignment, Color color, TextElision elision) 733{ 734 auto rect = a_rect; 735 Utf8View final_text(text); 736 String elided_text; 737 if (elision == TextElision::Right) { 738 int text_width = font.width(final_text); 739 if (font.width(final_text) > rect.width()) { 740 int glyph_spacing = font.glyph_spacing(); 741 int byte_offset = 0; 742 int new_width = font.width("..."); 743 if (new_width < text_width) { 744 for (auto it = final_text.begin(); it != final_text.end(); ++it) { 745 u32 codepoint = *it; 746 int glyph_width = font.glyph_or_emoji_width(codepoint); 747 // NOTE: Glyph spacing should not be added after the last glyph on the line, 748 // but since we are here because the last glyph does not actually fit on the line, 749 // we don't have to worry about spacing. 750 int width_with_this_glyph_included = new_width + glyph_width + glyph_spacing; 751 if (width_with_this_glyph_included > rect.width()) 752 break; 753 byte_offset = final_text.byte_offset_of(it); 754 new_width += glyph_width + glyph_spacing; 755 } 756 StringBuilder builder; 757 builder.append(final_text.substring_view(0, byte_offset).as_string()); 758 builder.append("..."); 759 elided_text = builder.to_string(); 760 final_text = Utf8View { elided_text }; 761 } 762 } 763 } 764 765 switch (alignment) { 766 case TextAlignment::TopLeft: 767 case TextAlignment::CenterLeft: 768 break; 769 case TextAlignment::TopRight: 770 case TextAlignment::CenterRight: 771 rect.set_x(rect.right() - font.width(final_text)); 772 break; 773 case TextAlignment::Center: { 774 auto shrunken_rect = rect; 775 shrunken_rect.set_width(font.width(final_text)); 776 shrunken_rect.center_within(rect); 777 rect = shrunken_rect; 778 break; 779 } 780 default: 781 ASSERT_NOT_REACHED(); 782 } 783 784 auto point = rect.location(); 785 int space_width = font.glyph_width(' ') + font.glyph_spacing(); 786 787 for (u32 codepoint : final_text) { 788 if (codepoint == ' ') { 789 point.move_by(space_width, 0); 790 continue; 791 } 792 draw_glyph_or_emoji(point, codepoint, font, color); 793 point.move_by(font.glyph_or_emoji_width(codepoint) + font.glyph_spacing(), 0); 794 } 795} 796 797void Painter::draw_text(const Rect& rect, const StringView& text, TextAlignment alignment, Color color, TextElision elision) 798{ 799 draw_text(rect, text, font(), alignment, color, elision); 800} 801 802void Painter::draw_text(const Rect& rect, const StringView& raw_text, const Font& font, TextAlignment alignment, Color color, TextElision elision) 803{ 804 Utf8View text { raw_text }; 805 Vector<Utf8View, 32> lines; 806 807 int start_of_current_line = 0; 808 for (auto it = text.begin(); it != text.end(); ++it) { 809 u32 codepoint = *it; 810 if (codepoint == '\n') { 811 int byte_offset = text.byte_offset_of(it); 812 Utf8View line = text.substring_view(start_of_current_line, byte_offset - start_of_current_line); 813 lines.append(line); 814 start_of_current_line = byte_offset + 1; 815 } 816 } 817 818 if (start_of_current_line != text.byte_length()) { 819 Utf8View line = text.substring_view(start_of_current_line, text.byte_length() - start_of_current_line); 820 lines.append(line); 821 } 822 823 static const int line_spacing = 4; 824 int line_height = font.glyph_height() + line_spacing; 825 Rect bounding_rect { 0, 0, 0, (static_cast<int>(lines.size()) * line_height) - line_spacing }; 826 827 for (auto& line : lines) { 828 auto line_width = font.width(line); 829 if (line_width > bounding_rect.width()) 830 bounding_rect.set_width(line_width); 831 } 832 833 switch (alignment) { 834 case TextAlignment::TopLeft: 835 bounding_rect.set_location(rect.location()); 836 break; 837 case TextAlignment::TopRight: 838 bounding_rect.set_location({ (rect.right() + 1) - bounding_rect.width(), rect.y() }); 839 break; 840 case TextAlignment::CenterLeft: 841 bounding_rect.set_location({ rect.x(), rect.center().y() - (bounding_rect.height() / 2) }); 842 break; 843 case TextAlignment::CenterRight: 844 bounding_rect.set_location({ (rect.right() + 1) - bounding_rect.width(), rect.center().y() - (bounding_rect.height() / 2) }); 845 break; 846 case TextAlignment::Center: 847 bounding_rect.center_within(rect); 848 break; 849 default: 850 ASSERT_NOT_REACHED(); 851 } 852 853 for (size_t i = 0; i < lines.size(); ++i) { 854 auto& line = lines[i]; 855 Rect line_rect { bounding_rect.x(), bounding_rect.y() + static_cast<int>(i) * line_height, bounding_rect.width(), line_height }; 856 line_rect.intersect(rect); 857 draw_text_line(line_rect, line, font, alignment, color, elision); 858 } 859} 860 861void Painter::set_pixel(const Point& p, Color color) 862{ 863 auto point = p; 864 point.move_by(state().translation); 865 if (!clip_rect().contains(point)) 866 return; 867 m_target->scanline(point.y())[point.x()] = color.value(); 868} 869 870[[gnu::always_inline]] inline void Painter::set_pixel_with_draw_op(u32& pixel, const Color& color) 871{ 872 if (draw_op() == DrawOp::Copy) 873 pixel = color.value(); 874 else if (draw_op() == DrawOp::Xor) 875 pixel ^= color.value(); 876} 877 878void Painter::draw_pixel(const Point& position, Color color, int thickness) 879{ 880 ASSERT(draw_op() == DrawOp::Copy); 881 if (thickness == 1) 882 return set_pixel_with_draw_op(m_target->scanline(position.y())[position.x()], color); 883 Rect rect { position.translated(-(thickness / 2), -(thickness / 2)), { thickness, thickness } }; 884 fill_rect(rect.translated(-state().translation), color); 885} 886 887void Painter::draw_line(const Point& p1, const Point& p2, Color color, int thickness, bool dotted) 888{ 889 auto clip_rect = this->clip_rect(); 890 891 auto point1 = p1; 892 point1.move_by(state().translation); 893 894 auto point2 = p2; 895 point2.move_by(state().translation); 896 897 // Special case: vertical line. 898 if (point1.x() == point2.x()) { 899 const int x = point1.x(); 900 if (x < clip_rect.left() || x > clip_rect.right()) 901 return; 902 if (point1.y() > point2.y()) 903 swap(point1, point2); 904 if (point1.y() > clip_rect.bottom()) 905 return; 906 if (point2.y() < clip_rect.top()) 907 return; 908 int min_y = max(point1.y(), clip_rect.top()); 909 int max_y = min(point2.y(), clip_rect.bottom()); 910 if (dotted) { 911 for (int y = min_y; y <= max_y; y += 2) 912 draw_pixel({ x, y }, color, thickness); 913 } else { 914 for (int y = min_y; y <= max_y; ++y) 915 draw_pixel({ x, y }, color, thickness); 916 } 917 return; 918 } 919 920 // Special case: horizontal line. 921 if (point1.y() == point2.y()) { 922 const int y = point1.y(); 923 if (y < clip_rect.top() || y > clip_rect.bottom()) 924 return; 925 if (point1.x() > point2.x()) 926 swap(point1, point2); 927 if (point1.x() > clip_rect.right()) 928 return; 929 if (point2.x() < clip_rect.left()) 930 return; 931 int min_x = max(point1.x(), clip_rect.left()); 932 int max_x = min(point2.x(), clip_rect.right()); 933 if (dotted) { 934 for (int x = min_x; x <= max_x; x += 2) 935 draw_pixel({ x, y }, color, thickness); 936 } else { 937 for (int x = min_x; x <= max_x; ++x) 938 draw_pixel({ x, y }, color, thickness); 939 } 940 return; 941 } 942 943 // FIXME: Implement dotted diagonal lines. 944 ASSERT(!dotted); 945 946 const double adx = abs(point2.x() - point1.x()); 947 const double ady = abs(point2.y() - point1.y()); 948 949 if (adx > ady) { 950 if (point1.x() > point2.x()) 951 swap(point1, point2); 952 } else { 953 if (point1.y() > point2.y()) 954 swap(point1, point2); 955 } 956 957 // FIXME: Implement clipping below. 958 const double dx = point2.x() - point1.x(); 959 const double dy = point2.y() - point1.y(); 960 double error = 0; 961 962 if (dx > dy) { 963 const double y_step = dy == 0 ? 0 : (dy > 0 ? 1 : -1); 964 const double delta_error = fabs(dy / dx); 965 int y = point1.y(); 966 for (int x = point1.x(); x <= point2.x(); ++x) { 967 if (clip_rect.contains(x, y)) 968 draw_pixel({ x, y }, color, thickness); 969 error += delta_error; 970 if (error >= 0.5) { 971 y = (double)y + y_step; 972 error -= 1.0; 973 } 974 } 975 } else { 976 const double x_step = dx == 0 ? 0 : (dx > 0 ? 1 : -1); 977 const double delta_error = fabs(dx / dy); 978 int x = point1.x(); 979 for (int y = point1.y(); y <= point2.y(); ++y) { 980 if (clip_rect.contains(x, y)) 981 draw_pixel({ x, y }, color, thickness); 982 error += delta_error; 983 if (error >= 0.5) { 984 x = (double)x + x_step; 985 error -= 1.0; 986 } 987 } 988 } 989} 990 991void Painter::add_clip_rect(const Rect& rect) 992{ 993 state().clip_rect.intersect(rect.translated(m_clip_origin.location())); 994 state().clip_rect.intersect(m_target->rect()); 995} 996 997void Painter::clear_clip_rect() 998{ 999 state().clip_rect = m_clip_origin; 1000} 1001 1002PainterStateSaver::PainterStateSaver(Painter& painter) 1003 : m_painter(painter) 1004{ 1005 m_painter.save(); 1006} 1007 1008PainterStateSaver::~PainterStateSaver() 1009{ 1010 m_painter.restore(); 1011} 1012 1013}