2020-01-18 08:38:21 +00:00
/*
2021-04-09 09:09:48 +00:00
* Copyright ( c ) 2018 - 2021 , Andreas Kling < kling @ serenityos . org >
2021-04-24 13:20:51 +00:00
* Copyright ( c ) 2021 , Idan Horowitz < idan . horowitz @ serenityos . org >
2021-09-03 12:24:38 +00:00
* Copyright ( c ) 2021 , Mustafa Quraish < mustafa @ cs . toronto . edu >
2020-01-18 08:38:21 +00:00
*
2021-04-22 08:24:48 +00:00
* SPDX - License - Identifier : BSD - 2 - Clause
2020-01-18 08:38:21 +00:00
*/
2018-10-10 14:49:36 +00:00
# include "Painter.h"
2020-03-08 11:05:14 +00:00
# include "Bitmap.h"
2019-09-06 17:23:36 +00:00
# include "Emoji.h"
2018-10-11 21:45:57 +00:00
# include "Font.h"
2020-12-29 17:25:13 +00:00
# include "FontDatabase.h"
2020-11-16 15:35:41 +00:00
# include "Gamma.h"
2018-10-10 14:49:36 +00:00
# include <AK/Assertions.h>
2021-01-16 14:51:56 +00:00
# include <AK/Debug.h>
2020-03-30 17:39:37 +00:00
# include <AK/Function.h>
2021-07-17 16:29:28 +00:00
# include <AK/Math.h>
2020-03-08 11:05:14 +00:00
# include <AK/Memory.h>
2021-04-14 23:25:07 +00:00
# include <AK/Queue.h>
2020-05-06 07:25:12 +00:00
# include <AK/QuickSort.h>
2018-12-21 01:18:16 +00:00
# include <AK/StdLibExtras.h>
2019-04-04 13:19:04 +00:00
# include <AK/StringBuilder.h>
2020-05-17 18:32:23 +00:00
# include <AK/Utf32View.h>
2019-09-06 17:23:36 +00:00
# include <AK/Utf8View.h>
2020-02-06 11:04:00 +00:00
# include <LibGfx/CharacterBitmap.h>
2020-10-27 20:17:50 +00:00
# include <LibGfx/Palette.h>
2020-04-16 19:03:17 +00:00
# include <LibGfx/Path.h>
2021-04-24 13:20:51 +00:00
# include <LibGfx/TextDirection.h>
2021-07-25 21:20:11 +00:00
# include <LibGfx/TextLayout.h>
2019-06-05 16:22:11 +00:00
# include <stdio.h>
2019-02-28 17:57:36 +00:00
2020-01-30 23:59:03 +00:00
# if defined(__GNUC__) && !defined(__clang__)
2020-03-08 11:05:14 +00:00
# pragma GCC optimize("O3")
2020-01-30 23:59:03 +00:00
# endif
2020-02-06 10:56:38 +00:00
namespace Gfx {
2020-02-14 22:02:47 +00:00
template < BitmapFormat format = BitmapFormat : : Invalid >
2020-04-30 09:43:25 +00:00
ALWAYS_INLINE Color get_pixel ( const Gfx : : Bitmap & bitmap , int x , int y )
2019-05-11 01:53:28 +00:00
{
2020-02-14 22:02:47 +00:00
if constexpr ( format = = BitmapFormat : : Indexed8 )
2020-09-05 23:10:33 +00:00
return bitmap . palette_color ( bitmap . scanline_u8 ( y ) [ x ] ) ;
2020-06-17 19:37:16 +00:00
if constexpr ( format = = BitmapFormat : : Indexed4 )
2020-09-05 23:10:33 +00:00
return bitmap . palette_color ( bitmap . scanline_u8 ( y ) [ x ] ) ;
2020-06-17 19:37:16 +00:00
if constexpr ( format = = BitmapFormat : : Indexed2 )
2020-09-05 23:10:33 +00:00
return bitmap . palette_color ( bitmap . scanline_u8 ( y ) [ x ] ) ;
2020-06-17 19:37:16 +00:00
if constexpr ( format = = BitmapFormat : : Indexed1 )
2020-09-05 23:10:33 +00:00
return bitmap . palette_color ( bitmap . scanline_u8 ( y ) [ x ] ) ;
2021-03-16 10:48:42 +00:00
if constexpr ( format = = BitmapFormat : : BGRx8888 )
2019-05-11 01:53:28 +00:00
return Color : : from_rgb ( bitmap . scanline ( y ) [ x ] ) ;
2021-03-16 10:48:42 +00:00
if constexpr ( format = = BitmapFormat : : BGRA8888 )
2019-05-11 01:53:28 +00:00
return Color : : from_rgba ( bitmap . scanline ( y ) [ x ] ) ;
return bitmap . get_pixel ( x , y ) ;
}
2021-01-19 17:10:47 +00:00
Painter : : Painter ( Gfx : : Bitmap & bitmap )
2019-02-25 15:04:08 +00:00
: m_target ( bitmap )
2019-01-12 02:42:50 +00:00
{
2021-01-19 17:10:47 +00:00
int scale = bitmap . scale ( ) ;
2021-03-16 10:48:42 +00:00
VERIFY ( bitmap . format ( ) = = Gfx : : BitmapFormat : : BGRx8888 | | bitmap . format ( ) = = Gfx : : BitmapFormat : : BGRA8888 ) ;
2021-02-23 19:42:32 +00:00
VERIFY ( bitmap . physical_width ( ) % scale = = 0 ) ;
VERIFY ( bitmap . physical_height ( ) % scale = = 0 ) ;
2019-03-09 15:48:02 +00:00
m_state_stack . append ( State ( ) ) ;
2020-12-29 17:25:13 +00:00
state ( ) . font = & FontDatabase : : default_font ( ) ;
2021-05-03 14:37:05 +00:00
state ( ) . clip_rect = { { 0 , 0 } , bitmap . size ( ) } ;
state ( ) . scale = scale ;
2019-03-09 15:48:02 +00:00
m_clip_origin = state ( ) . clip_rect ;
2019-01-12 02:42:50 +00:00
}
2018-10-10 14:49:36 +00:00
Painter : : ~ Painter ( )
{
2018-10-10 18:06:58 +00:00
}
2018-10-10 14:49:36 +00:00
2020-06-10 08:57:59 +00:00
void Painter : : fill_rect_with_draw_op ( const IntRect & a_rect , Color color )
2019-02-01 04:23:05 +00:00
{
2021-05-03 14:37:05 +00:00
VERIFY ( scale ( ) = = 1 ) ; // FIXME: Add scaling support.
auto rect = a_rect . translated ( translation ( ) ) . intersected ( clip_rect ( ) ) ;
2019-02-01 04:23:05 +00:00
if ( rect . is_empty ( ) )
return ;
RGBA32 * dst = m_target - > scanline ( rect . top ( ) ) + rect . left ( ) ;
2019-05-06 12:04:54 +00:00
const size_t dst_skip = m_target - > pitch ( ) / sizeof ( RGBA32 ) ;
2019-02-01 04:23:05 +00:00
for ( int i = rect . height ( ) - 1 ; i > = 0 ; - - i ) {
for ( int j = 0 ; j < rect . width ( ) ; + + j )
2021-01-13 01:22:15 +00:00
set_physical_pixel_with_draw_op ( dst [ j ] , color ) ;
2019-02-01 04:23:05 +00:00
dst + = dst_skip ;
}
}
2020-06-10 08:57:59 +00:00
void Painter : : clear_rect ( const IntRect & a_rect , Color color )
2019-11-25 10:34:55 +00:00
{
2021-05-03 14:37:05 +00:00
auto rect = a_rect . translated ( translation ( ) ) . intersected ( clip_rect ( ) ) ;
2019-11-25 10:34:55 +00:00
if ( rect . is_empty ( ) )
return ;
2021-05-03 14:37:05 +00:00
VERIFY ( m_target - > rect ( ) . contains ( rect ) ) ;
rect * = scale ( ) ;
2019-11-25 10:34:55 +00:00
RGBA32 * dst = m_target - > scanline ( rect . top ( ) ) + rect . left ( ) ;
const size_t dst_skip = m_target - > pitch ( ) / sizeof ( RGBA32 ) ;
for ( int i = rect . height ( ) - 1 ; i > = 0 ; - - i ) {
fast_u32_fill ( dst , color . value ( ) , rect . width ( ) ) ;
dst + = dst_skip ;
}
}
2021-01-20 14:59:22 +00:00
void Painter : : fill_physical_rect ( const IntRect & physical_rect , Color color )
LibGfx: Make it possible to apply an (integer) scale to a Painter
This adds a scale factor to Painter, which will be used for HighDPI
support. It's also a step towards general affine transforms on Painters.
All of Painter's public API takes logical coordinates, while some
internals deal with physical coordinates now. If scale == 1, logical
and physical coordinates are the same. For scale == 2, a 200x100 bitmap
would be covered by a logical {0, 0, 100, 50} rect, while its physical
size would be {0, 0, 200, 100}.
Most of Painter's functions just assert that scale() == 1 is for now,
but most functions called by WindowServer are updated to handle
arbitrary (integer) scale.
Also add a new Demo "LibGfxScaleDemo" that covers the converted
functions and that can be used to iteratively add scaling support
to more functions.
To make Painter's interface deal with logical coordinates only,
make translation() and clip_rect() non-public.
2021-01-12 20:53:07 +00:00
{
2021-01-20 14:59:22 +00:00
// Callers must do clipping.
2021-05-03 14:37:05 +00:00
RGBA32 * dst = m_target - > scanline ( physical_rect . top ( ) ) + physical_rect . left ( ) ;
const size_t dst_skip = m_target - > pitch ( ) / sizeof ( RGBA32 ) ;
for ( int i = physical_rect . height ( ) - 1 ; i > = 0 ; - - i ) {
for ( int j = 0 ; j < physical_rect . width ( ) ; + + j )
dst [ j ] = Color : : from_rgba ( dst [ j ] ) . blend ( color ) . value ( ) ;
dst + = dst_skip ;
LibGfx: Make it possible to apply an (integer) scale to a Painter
This adds a scale factor to Painter, which will be used for HighDPI
support. It's also a step towards general affine transforms on Painters.
All of Painter's public API takes logical coordinates, while some
internals deal with physical coordinates now. If scale == 1, logical
and physical coordinates are the same. For scale == 2, a 200x100 bitmap
would be covered by a logical {0, 0, 100, 50} rect, while its physical
size would be {0, 0, 200, 100}.
Most of Painter's functions just assert that scale() == 1 is for now,
but most functions called by WindowServer are updated to handle
arbitrary (integer) scale.
Also add a new Demo "LibGfxScaleDemo" that covers the converted
functions and that can be used to iteratively add scaling support
to more functions.
To make Painter's interface deal with logical coordinates only,
make translation() and clip_rect() non-public.
2021-01-12 20:53:07 +00:00
}
}
2020-06-10 08:57:59 +00:00
void Painter : : fill_rect ( const IntRect & a_rect , Color color )
2018-10-10 18:06:58 +00:00
{
2019-11-16 18:26:17 +00:00
if ( color . alpha ( ) = = 0 )
return ;
2019-03-09 15:48:02 +00:00
if ( draw_op ( ) ! = DrawOp : : Copy ) {
2019-02-01 04:23:05 +00:00
fill_rect_with_draw_op ( a_rect , color ) ;
return ;
}
2019-11-25 10:34:55 +00:00
if ( color . alpha ( ) = = 0xff ) {
clear_rect ( a_rect , color ) ;
return ;
}
2021-05-03 14:37:05 +00:00
auto rect = a_rect . translated ( translation ( ) ) . intersected ( clip_rect ( ) ) ;
2021-01-20 14:59:22 +00:00
if ( rect . is_empty ( ) )
return ;
2021-05-03 14:37:05 +00:00
VERIFY ( m_target - > rect ( ) . contains ( rect ) ) ;
2021-01-20 14:59:22 +00:00
2021-05-03 14:37:05 +00:00
fill_physical_rect ( rect * scale ( ) , color ) ;
2018-10-10 14:49:36 +00:00
}
2018-10-10 18:06:58 +00:00
2020-06-10 08:57:59 +00:00
void Painter : : fill_rect_with_dither_pattern ( const IntRect & a_rect , Color color_a , Color color_b )
2020-05-09 23:00:21 +00:00
{
2021-05-03 14:37:05 +00:00
VERIFY ( scale ( ) = = 1 ) ; // FIXME: Add scaling support.
auto rect = a_rect . translated ( translation ( ) ) . intersected ( clip_rect ( ) ) ;
2020-05-09 23:00:21 +00:00
if ( rect . is_empty ( ) )
return ;
RGBA32 * dst = m_target - > scanline ( rect . top ( ) ) + rect . left ( ) ;
const size_t dst_skip = m_target - > pitch ( ) / sizeof ( RGBA32 ) ;
for ( int i = 0 ; i < rect . height ( ) ; + + i ) {
for ( int j = 0 ; j < rect . width ( ) ; + + j ) {
2021-07-15 16:59:49 +00:00
bool checkboard_use_a = ( ( rect . left ( ) + i ) & 1 ) ^ ( ( rect . top ( ) + j ) & 1 ) ;
2020-09-01 17:10:09 +00:00
if ( checkboard_use_a & & ! color_a . alpha ( ) )
continue ;
if ( ! checkboard_use_a & & ! color_b . alpha ( ) )
continue ;
2020-05-10 22:14:31 +00:00
dst [ j ] = checkboard_use_a ? color_a . value ( ) : color_b . value ( ) ;
2020-05-09 23:00:21 +00:00
}
dst + = dst_skip ;
}
}
2020-06-10 08:57:59 +00:00
void Painter : : fill_rect_with_checkerboard ( const IntRect & a_rect , const IntSize & cell_size , Color color_dark , Color color_light )
2020-04-05 11:01:13 +00:00
{
2021-05-03 14:37:05 +00:00
VERIFY ( scale ( ) = = 1 ) ; // FIXME: Add scaling support.
auto rect = a_rect . translated ( translation ( ) ) . intersected ( clip_rect ( ) ) ;
2020-04-05 11:01:13 +00:00
if ( rect . is_empty ( ) )
return ;
RGBA32 * dst = m_target - > scanline ( rect . top ( ) ) + rect . left ( ) ;
const size_t dst_skip = m_target - > pitch ( ) / sizeof ( RGBA32 ) ;
2021-07-06 09:50:27 +00:00
int first_cell_column = rect . x ( ) / cell_size . width ( ) ;
2021-07-07 08:43:03 +00:00
int prologue_length = min ( rect . width ( ) , cell_size . width ( ) - ( rect . x ( ) % cell_size . width ( ) ) ) ;
int number_of_aligned_strips = ( rect . width ( ) - prologue_length ) / cell_size . width ( ) ;
2021-07-06 09:50:27 +00:00
2020-04-05 11:01:13 +00:00
for ( int i = 0 ; i < rect . height ( ) ; + + i ) {
2021-05-15 09:18:45 +00:00
int y = rect . y ( ) + i ;
int cell_row = y / cell_size . height ( ) ;
2021-07-06 09:50:27 +00:00
bool odd_row = cell_row & 1 ;
// Prologue: Paint the unaligned part up to the first intersection.
int j = 0 ;
int cell_column = first_cell_column ;
2021-07-07 08:43:03 +00:00
{
bool odd_cell = cell_column & 1 ;
auto color = ( odd_row ^ odd_cell ) ? color_light . value ( ) : color_dark . value ( ) ;
fast_u32_fill ( & dst [ j ] , color , prologue_length ) ;
j + = prologue_length ;
2021-07-06 09:50:27 +00:00
}
// Aligned run: Paint the maximum number of aligned cell strips.
for ( int strip = 0 ; strip < number_of_aligned_strips ; + + strip ) {
+ + cell_column ;
bool odd_cell = cell_column & 1 ;
auto color = ( odd_row ^ odd_cell ) ? color_light . value ( ) : color_dark . value ( ) ;
fast_u32_fill ( & dst [ j ] , color , cell_size . width ( ) ) ;
j + = cell_size . width ( ) ;
2020-04-05 11:01:13 +00:00
}
2021-07-06 09:50:27 +00:00
// Epilogue: Paint the unaligned part until the end of the rect.
2021-07-07 08:43:03 +00:00
if ( j ! = rect . width ( ) ) {
+ + cell_column ;
bool odd_cell = cell_column & 1 ;
auto color = ( odd_row ^ odd_cell ) ? color_light . value ( ) : color_dark . value ( ) ;
int epilogue_length = rect . width ( ) - j ;
fast_u32_fill ( & dst [ j ] , color , epilogue_length ) ;
j + = epilogue_length ;
2021-07-06 09:50:27 +00:00
}
2020-04-05 11:01:13 +00:00
dst + = dst_skip ;
}
}
2020-06-10 08:57:59 +00:00
void Painter : : fill_rect_with_gradient ( Orientation orientation , const IntRect & a_rect , Color gradient_start , Color gradient_end )
2019-01-25 04:01:27 +00:00
{
2021-02-12 21:25:08 +00:00
if ( gradient_start = = gradient_end ) {
fill_rect ( a_rect , gradient_start ) ;
return ;
}
LibGfx: Make it possible to apply an (integer) scale to a Painter
This adds a scale factor to Painter, which will be used for HighDPI
support. It's also a step towards general affine transforms on Painters.
All of Painter's public API takes logical coordinates, while some
internals deal with physical coordinates now. If scale == 1, logical
and physical coordinates are the same. For scale == 2, a 200x100 bitmap
would be covered by a logical {0, 0, 100, 50} rect, while its physical
size would be {0, 0, 200, 100}.
Most of Painter's functions just assert that scale() == 1 is for now,
but most functions called by WindowServer are updated to handle
arbitrary (integer) scale.
Also add a new Demo "LibGfxScaleDemo" that covers the converted
functions and that can be used to iteratively add scaling support
to more functions.
To make Painter's interface deal with logical coordinates only,
make translation() and clip_rect() non-public.
2021-01-12 20:53:07 +00:00
2021-05-03 14:37:05 +00:00
auto rect = to_physical ( a_rect ) ;
auto clipped_rect = IntRect : : intersection ( rect , clip_rect ( ) * scale ( ) ) ;
2019-01-25 04:01:27 +00:00
if ( clipped_rect . is_empty ( ) )
return ;
2020-03-30 14:59:45 +00:00
int offset = clipped_rect . primary_offset_for_orientation ( orientation ) - rect . primary_offset_for_orientation ( orientation ) ;
2019-01-25 04:01:27 +00:00
RGBA32 * dst = m_target - > scanline ( clipped_rect . top ( ) ) + clipped_rect . left ( ) ;
2019-05-06 12:04:54 +00:00
const size_t dst_skip = m_target - > pitch ( ) / sizeof ( RGBA32 ) ;
2019-01-25 04:01:27 +00:00
2021-05-03 14:37:05 +00:00
float increment = ( 1.0 / ( ( rect . primary_size_for_orientation ( orientation ) ) ) ) ;
2021-02-12 21:25:08 +00:00
float alpha_increment = increment * ( ( float ) gradient_end . alpha ( ) - ( float ) gradient_start . alpha ( ) ) ;
2019-01-25 04:01:27 +00:00
2020-03-30 14:59:45 +00:00
if ( orientation = = Orientation : : Horizontal ) {
for ( int i = clipped_rect . height ( ) - 1 ; i > = 0 ; - - i ) {
float c = offset * increment ;
2021-02-12 21:25:08 +00:00
float c_alpha = gradient_start . alpha ( ) + offset * alpha_increment ;
2020-03-30 14:59:45 +00:00
for ( int j = 0 ; j < clipped_rect . width ( ) ; + + j ) {
2021-02-12 21:25:08 +00:00
auto color = gamma_accurate_blend ( gradient_start , gradient_end , c ) ;
color . set_alpha ( c_alpha ) ;
dst [ j ] = color . value ( ) ;
c_alpha + = alpha_increment ;
2020-03-30 14:59:45 +00:00
c + = increment ;
}
dst + = dst_skip ;
}
} else {
float c = offset * increment ;
2021-02-12 21:25:08 +00:00
float c_alpha = gradient_start . alpha ( ) + offset * alpha_increment ;
2020-03-30 14:59:45 +00:00
for ( int i = clipped_rect . height ( ) - 1 ; i > = 0 ; - - i ) {
2021-03-07 22:39:01 +00:00
auto color = gamma_accurate_blend ( gradient_end , gradient_start , c ) ;
2021-02-12 21:25:08 +00:00
color . set_alpha ( c_alpha ) ;
2020-03-30 14:59:45 +00:00
for ( int j = 0 ; j < clipped_rect . width ( ) ; + + j ) {
dst [ j ] = color . value ( ) ;
}
2021-02-12 21:25:08 +00:00
c_alpha + = alpha_increment ;
2019-01-25 04:01:27 +00:00
c + = increment ;
2020-03-30 14:59:45 +00:00
dst + = dst_skip ;
2019-01-25 04:01:27 +00:00
}
}
}
2020-06-10 08:57:59 +00:00
void Painter : : fill_rect_with_gradient ( const IntRect & a_rect , Color gradient_start , Color gradient_end )
2020-03-30 14:59:45 +00:00
{
return fill_rect_with_gradient ( Orientation : : Horizontal , a_rect , gradient_start , gradient_end ) ;
}
2021-06-04 11:38:30 +00:00
void Painter : : fill_rect_with_rounded_corners ( const IntRect & a_rect , Color color , int radius )
{
return fill_rect_with_rounded_corners ( a_rect , color , radius , radius , radius , radius ) ;
}
2021-05-15 21:33:21 +00:00
void Painter : : fill_rect_with_rounded_corners ( const IntRect & a_rect , Color color , int top_left_radius , int top_right_radius , int bottom_right_radius , int bottom_left_radius )
{
// Fasttrack for rects without any border radii
if ( ! top_left_radius & & ! top_right_radius & & ! bottom_right_radius & & ! bottom_left_radius )
return fill_rect ( a_rect , color ) ;
// Fully transparent, dont care.
if ( color . alpha ( ) = = 0 )
return ;
// FIXME: Allow for elliptically rounded corners
IntRect top_left_corner = {
a_rect . x ( ) ,
a_rect . y ( ) ,
top_left_radius ,
top_left_radius
} ;
IntRect top_right_corner = {
a_rect . x ( ) + a_rect . width ( ) - top_right_radius ,
a_rect . y ( ) ,
top_right_radius ,
top_right_radius
} ;
IntRect bottom_right_corner = {
a_rect . x ( ) + a_rect . width ( ) - bottom_right_radius ,
a_rect . y ( ) + a_rect . height ( ) - bottom_right_radius ,
bottom_right_radius ,
bottom_right_radius
} ;
IntRect bottom_left_corner = {
a_rect . x ( ) ,
a_rect . y ( ) + a_rect . height ( ) - bottom_left_radius ,
bottom_left_radius ,
bottom_left_radius
} ;
IntRect top_rect = {
a_rect . x ( ) + top_left_radius ,
a_rect . y ( ) ,
a_rect . width ( ) - top_left_radius - top_right_radius , top_left_radius
} ;
IntRect right_rect = {
a_rect . x ( ) + a_rect . width ( ) - top_right_radius ,
a_rect . y ( ) + top_right_radius ,
top_right_radius ,
a_rect . height ( ) - top_right_radius - bottom_right_radius
} ;
IntRect bottom_rect = {
a_rect . x ( ) + bottom_left_radius ,
a_rect . y ( ) + a_rect . height ( ) - bottom_right_radius ,
a_rect . width ( ) - bottom_left_radius - bottom_right_radius ,
bottom_right_radius
} ;
IntRect left_rect = {
a_rect . x ( ) ,
a_rect . y ( ) + top_left_radius ,
bottom_left_radius ,
a_rect . height ( ) - top_left_radius - bottom_left_radius
} ;
IntRect inner = {
left_rect . x ( ) + left_rect . width ( ) ,
left_rect . y ( ) ,
a_rect . width ( ) - left_rect . width ( ) - right_rect . width ( ) ,
a_rect . height ( ) - top_rect . height ( ) - bottom_rect . height ( )
} ;
fill_rect ( top_rect , color ) ;
fill_rect ( right_rect , color ) ;
fill_rect ( bottom_rect , color ) ;
fill_rect ( left_rect , color ) ;
fill_rect ( inner , color ) ;
if ( top_left_radius )
fill_rounded_corner ( top_left_corner , top_left_radius , color , CornerOrientation : : TopLeft ) ;
if ( top_right_radius )
fill_rounded_corner ( top_right_corner , top_right_radius , color , CornerOrientation : : TopRight ) ;
if ( bottom_left_radius )
fill_rounded_corner ( bottom_left_corner , bottom_left_radius , color , CornerOrientation : : BottomLeft ) ;
if ( bottom_right_radius )
fill_rounded_corner ( bottom_right_corner , bottom_right_radius , color , CornerOrientation : : BottomRight ) ;
}
void Painter : : fill_rounded_corner ( const IntRect & a_rect , int radius , Color color , CornerOrientation orientation )
{
// Care about clipping
auto translated_a_rect = a_rect . translated ( translation ( ) ) ;
auto rect = translated_a_rect . intersected ( clip_rect ( ) ) ;
if ( rect . is_empty ( ) )
return ;
VERIFY ( m_target - > rect ( ) . contains ( rect ) ) ;
// We got cut on the top!
// FIXME: Also account for clipping on the x-axis
int clip_offset = 0 ;
if ( translated_a_rect . y ( ) < rect . y ( ) )
clip_offset = rect . y ( ) - translated_a_rect . y ( ) ;
2021-06-22 21:12:36 +00:00
radius * = scale ( ) ;
rect * = scale ( ) ;
clip_offset * = scale ( ) ;
2021-05-15 21:33:21 +00:00
RGBA32 * dst = m_target - > scanline ( rect . top ( ) ) + rect . left ( ) ;
const size_t dst_skip = m_target - > pitch ( ) / sizeof ( RGBA32 ) ;
IntPoint circle_center ;
switch ( orientation ) {
case CornerOrientation : : TopLeft :
circle_center = { radius , radius + 1 } ;
break ;
case CornerOrientation : : TopRight :
circle_center = { - 1 , radius + 1 } ;
break ;
case CornerOrientation : : BottomRight :
circle_center = { - 1 , 0 } ;
break ;
case CornerOrientation : : BottomLeft :
circle_center = { radius , 0 } ;
break ;
default :
VERIFY_NOT_REACHED ( ) ;
}
int radius2 = radius * radius ;
auto is_in_circle = [ & ] ( int x , int y ) {
int distance2 = ( circle_center . x ( ) - x ) * ( circle_center . x ( ) - x ) + ( circle_center . y ( ) - y ) * ( circle_center . y ( ) - y ) ;
// To reflect the grid and be compatible with the draw_circle_arc_intersecting algorithm
// add 1/2 to the radius
return distance2 < = ( radius2 + radius + 0.25 ) ;
} ;
for ( int i = rect . height ( ) - 1 ; i > = 0 ; - - i ) {
for ( int j = 0 ; j < rect . width ( ) ; + + j )
if ( is_in_circle ( j , rect . height ( ) - i + clip_offset ) )
2021-05-21 22:15:06 +00:00
dst [ j ] = Color : : from_rgba ( dst [ j ] ) . blend ( color ) . value ( ) ;
2021-05-15 21:33:21 +00:00
dst + = dst_skip ;
}
}
2021-05-16 14:14:33 +00:00
void Painter : : draw_circle_arc_intersecting ( const IntRect & a_rect , const IntPoint & center , int radius , Color color , int thickness )
{
if ( thickness < = 0 )
return ;
// Care about clipping
auto translated_a_rect = a_rect . translated ( translation ( ) ) ;
auto rect = translated_a_rect . intersected ( clip_rect ( ) ) ;
if ( rect . is_empty ( ) )
return ;
VERIFY ( m_target - > rect ( ) . contains ( rect ) ) ;
// We got cut on the top!
// FIXME: Also account for clipping on the x-axis
int clip_offset = 0 ;
if ( translated_a_rect . y ( ) < rect . y ( ) )
clip_offset = rect . y ( ) - translated_a_rect . y ( ) ;
if ( thickness > radius )
thickness = radius ;
int radius2 = radius * radius ;
auto is_on_arc = [ & ] ( int x , int y ) {
int distance2 = ( center . x ( ) - x ) * ( center . x ( ) - x ) + ( center . y ( ) - y ) * ( center . y ( ) - y ) ;
// Is within a circle of radius 1/2 around (x,y), so basically within the current pixel.
// Technically this is angle-dependent and should be between 1/2 and sqrt(2)/2, but this works.
return distance2 < = ( radius2 + radius + 0.25 ) & & distance2 > = ( radius2 - radius + 0.25 ) ;
} ;
RGBA32 * dst = m_target - > scanline ( rect . top ( ) ) + rect . left ( ) ;
const size_t dst_skip = m_target - > pitch ( ) / sizeof ( RGBA32 ) ;
for ( int i = rect . height ( ) - 1 ; i > = 0 ; - - i ) {
for ( int j = 0 ; j < rect . width ( ) ; + + j )
if ( is_on_arc ( j , rect . height ( ) - i + clip_offset ) )
2021-05-21 22:15:06 +00:00
dst [ j ] = Color : : from_rgba ( dst [ j ] ) . blend ( color ) . value ( ) ;
2021-05-16 14:14:33 +00:00
dst + = dst_skip ;
}
return draw_circle_arc_intersecting ( a_rect , center , radius - 1 , color , thickness - 1 ) ;
}
2020-06-10 08:57:59 +00:00
void Painter : : fill_ellipse ( const IntRect & a_rect , Color color )
2020-05-24 18:03:48 +00:00
{
2021-05-03 14:37:05 +00:00
VERIFY ( scale ( ) = = 1 ) ; // FIXME: Add scaling support.
auto rect = a_rect . translated ( translation ( ) ) . intersected ( clip_rect ( ) ) ;
2020-05-24 18:03:48 +00:00
if ( rect . is_empty ( ) )
return ;
2021-05-03 14:37:05 +00:00
VERIFY ( m_target - > rect ( ) . contains ( rect ) ) ;
2021-05-08 17:15:03 +00:00
for ( int i = 1 ; i < a_rect . height ( ) ; i + + ) {
double y = a_rect . height ( ) * 0.5 - i ;
double x = a_rect . width ( ) * sqrt ( 0.25 - y * y / a_rect . height ( ) / a_rect . height ( ) ) ;
draw_line ( { a_rect . x ( ) + a_rect . width ( ) / 2 - ( int ) x , a_rect . y ( ) + i } , { a_rect . x ( ) + a_rect . width ( ) / 2 + ( int ) x - 1 , a_rect . y ( ) + i } , color ) ;
2020-05-24 18:03:48 +00:00
}
}
2020-06-10 08:57:59 +00:00
void Painter : : draw_ellipse_intersecting ( const IntRect & rect , Color color , int thickness )
2019-12-26 22:19:45 +00:00
{
2021-05-03 14:37:05 +00:00
VERIFY ( scale ( ) = = 1 ) ; // FIXME: Add scaling support.
2019-12-26 22:19:45 +00:00
constexpr int number_samples = 100 ; // FIXME: dynamically work out the number of samples based upon the rect size
double increment = M_PI / number_samples ;
auto ellipse_x = [ & ] ( double theta ) - > int {
2021-07-17 16:29:28 +00:00
return ( AK : : cos ( theta ) * rect . width ( ) / AK : : sqrt ( 2. ) ) + rect . center ( ) . x ( ) ;
2019-12-26 22:19:45 +00:00
} ;
auto ellipse_y = [ & ] ( double theta ) - > int {
2021-07-17 16:29:28 +00:00
return ( AK : : sin ( theta ) * rect . height ( ) / AK : : sqrt ( 2. ) ) + rect . center ( ) . y ( ) ;
2019-12-26 22:19:45 +00:00
} ;
2021-04-15 07:36:14 +00:00
for ( auto theta = 0.0 ; theta < 2 * M_PI ; theta + = increment ) {
2019-12-26 22:19:45 +00:00
draw_line ( { ellipse_x ( theta ) , ellipse_y ( theta ) } , { ellipse_x ( theta + increment ) , ellipse_y ( theta + increment ) } , color , thickness ) ;
}
}
2021-05-03 14:37:05 +00:00
template < typename RectType , typename Callback >
static void for_each_pixel_around_rect_clockwise ( const RectType & rect , Callback callback )
2020-10-26 19:43:59 +00:00
{
if ( rect . is_empty ( ) )
return ;
for ( auto x = rect . left ( ) ; x < = rect . right ( ) ; + + x ) {
callback ( x , rect . top ( ) ) ;
}
for ( auto y = rect . top ( ) + 1 ; y < = rect . bottom ( ) ; + + y ) {
callback ( rect . right ( ) , y ) ;
}
for ( auto x = rect . right ( ) - 1 ; x > = rect . left ( ) ; - - x ) {
callback ( x , rect . bottom ( ) ) ;
}
for ( auto y = rect . bottom ( ) - 1 ; y > rect . top ( ) ; - - y ) {
callback ( rect . left ( ) , y ) ;
}
}
2021-05-03 14:37:05 +00:00
void Painter : : draw_focus_rect ( const IntRect & rect , Color color )
2020-10-26 19:43:59 +00:00
{
2021-05-03 14:37:05 +00:00
VERIFY ( scale ( ) = = 1 ) ; // FIXME: Add scaling support.
2020-10-26 19:43:59 +00:00
if ( rect . is_empty ( ) )
return ;
bool state = false ;
for_each_pixel_around_rect_clockwise ( rect , [ & ] ( auto x , auto y ) {
if ( state )
set_pixel ( x , y , color ) ;
state = ! state ;
} ) ;
}
2021-05-03 14:37:05 +00:00
void Painter : : draw_rect ( const IntRect & a_rect , Color color , bool rough )
2018-10-11 21:14:51 +00:00
{
2021-05-03 14:37:05 +00:00
IntRect rect = a_rect . translated ( translation ( ) ) ;
auto clipped_rect = rect . intersected ( clip_rect ( ) ) ;
if ( clipped_rect . is_empty ( ) )
return ;
int min_y = clipped_rect . top ( ) ;
int max_y = clipped_rect . bottom ( ) ;
int scale = this - > scale ( ) ;
2021-01-13 01:22:15 +00:00
2021-05-03 14:37:05 +00:00
if ( rect . top ( ) > = clipped_rect . top ( ) & & rect . top ( ) < = clipped_rect . bottom ( ) ) {
int start_x = rough ? max ( rect . x ( ) + 1 , clipped_rect . x ( ) ) : clipped_rect . x ( ) ;
int width = rough ? min ( rect . width ( ) - 2 , clipped_rect . width ( ) ) : clipped_rect . width ( ) ;
for ( int i = 0 ; i < scale ; + + i )
fill_physical_scanline_with_draw_op ( rect . top ( ) * scale + i , start_x * scale , width * scale , color ) ;
+ + min_y ;
}
if ( rect . bottom ( ) > = clipped_rect . top ( ) & & rect . bottom ( ) < = clipped_rect . bottom ( ) ) {
int start_x = rough ? max ( rect . x ( ) + 1 , clipped_rect . x ( ) ) : clipped_rect . x ( ) ;
int width = rough ? min ( rect . width ( ) - 2 , clipped_rect . width ( ) ) : clipped_rect . width ( ) ;
for ( int i = 0 ; i < scale ; + + i )
fill_physical_scanline_with_draw_op ( max_y * scale + i , start_x * scale , width * scale , color ) ;
- - max_y ;
}
bool draw_left_side = rect . left ( ) > = clipped_rect . left ( ) ;
bool draw_right_side = rect . right ( ) = = clipped_rect . right ( ) ;
if ( draw_left_side & & draw_right_side ) {
// Specialized loop when drawing both sides.
for ( int y = min_y * scale ; y < = max_y * scale ; + + y ) {
auto * bits = m_target - > scanline ( y ) ;
for ( int i = 0 ; i < scale ; + + i )
set_physical_pixel_with_draw_op ( bits [ rect . left ( ) * scale + i ] , color ) ;
for ( int i = 0 ; i < scale ; + + i )
set_physical_pixel_with_draw_op ( bits [ rect . right ( ) * scale + i ] , color ) ;
}
} else {
for ( int y = min_y * scale ; y < = max_y * scale ; + + y ) {
auto * bits = m_target - > scanline ( y ) ;
if ( draw_left_side )
for ( int i = 0 ; i < scale ; + + i )
set_physical_pixel_with_draw_op ( bits [ rect . left ( ) * scale + i ] , color ) ;
if ( draw_right_side )
for ( int i = 0 ; i < scale ; + + i )
set_physical_pixel_with_draw_op ( bits [ rect . right ( ) * scale + i ] , color ) ;
}
}
2018-10-11 21:14:51 +00:00
}
2021-09-03 12:24:38 +00:00
void Painter : : draw_rect_with_thickness ( const IntRect & rect , Color color , int thickness )
{
VERIFY ( scale ( ) = = 1 ) ; // FIXME: Add scaling support.
IntPoint p1 = rect . location ( ) ;
IntPoint p2 = { rect . location ( ) . x ( ) + rect . width ( ) , rect . location ( ) . y ( ) } ;
IntPoint p3 = { rect . location ( ) . x ( ) + rect . width ( ) , rect . location ( ) . y ( ) + rect . height ( ) } ;
IntPoint p4 = { rect . location ( ) . x ( ) , rect . location ( ) . y ( ) + rect . height ( ) } ;
draw_line ( p1 , p2 , color , thickness ) ;
draw_line ( p2 , p3 , color , thickness ) ;
draw_line ( p3 , p4 , color , thickness ) ;
draw_line ( p4 , p1 , color , thickness ) ;
}
2020-06-10 08:57:59 +00:00
void Painter : : draw_bitmap ( const IntPoint & p , const CharacterBitmap & bitmap , Color color )
2018-10-12 10:29:58 +00:00
{
2021-02-23 19:42:32 +00:00
VERIFY ( scale ( ) = = 1 ) ; // FIXME: Add scaling support.
LibGfx: Make it possible to apply an (integer) scale to a Painter
This adds a scale factor to Painter, which will be used for HighDPI
support. It's also a step towards general affine transforms on Painters.
All of Painter's public API takes logical coordinates, while some
internals deal with physical coordinates now. If scale == 1, logical
and physical coordinates are the same. For scale == 2, a 200x100 bitmap
would be covered by a logical {0, 0, 100, 50} rect, while its physical
size would be {0, 0, 200, 100}.
Most of Painter's functions just assert that scale() == 1 is for now,
but most functions called by WindowServer are updated to handle
arbitrary (integer) scale.
Also add a new Demo "LibGfxScaleDemo" that covers the converted
functions and that can be used to iteratively add scaling support
to more functions.
To make Painter's interface deal with logical coordinates only,
make translation() and clip_rect() non-public.
2021-01-12 20:53:07 +00:00
2020-06-10 08:57:59 +00:00
auto rect = IntRect ( p , bitmap . size ( ) ) . translated ( translation ( ) ) ;
2019-04-16 11:58:02 +00:00
auto clipped_rect = rect . intersected ( clip_rect ( ) ) ;
2019-01-25 05:37:56 +00:00
if ( clipped_rect . is_empty ( ) )
return ;
2019-01-25 01:39:45 +00:00
const int first_row = clipped_rect . top ( ) - rect . top ( ) ;
const int last_row = clipped_rect . bottom ( ) - rect . top ( ) ;
const int first_column = clipped_rect . left ( ) - rect . left ( ) ;
const int last_column = clipped_rect . right ( ) - rect . left ( ) ;
2019-01-25 05:37:56 +00:00
RGBA32 * dst = m_target - > scanline ( clipped_rect . y ( ) ) + clipped_rect . x ( ) ;
2019-05-06 12:04:54 +00:00
const size_t dst_skip = m_target - > pitch ( ) / sizeof ( RGBA32 ) ;
2019-01-25 05:37:56 +00:00
const char * bitmap_row = & bitmap . bits ( ) [ first_row * bitmap . width ( ) + first_column ] ;
2019-01-25 01:39:45 +00:00
const size_t bitmap_skip = bitmap . width ( ) ;
for ( int row = first_row ; row < = last_row ; + + row ) {
2019-01-25 05:37:56 +00:00
for ( int j = 0 ; j < = ( last_column - first_column ) ; + + j ) {
2019-01-25 01:39:45 +00:00
char fc = bitmap_row [ j ] ;
2018-10-12 10:29:58 +00:00
if ( fc = = ' # ' )
2019-01-25 01:39:45 +00:00
dst [ j ] = color . value ( ) ;
2018-10-12 10:29:58 +00:00
}
2019-01-25 01:39:45 +00:00
bitmap_row + = bitmap_skip ;
dst + = dst_skip ;
2018-10-12 10:29:58 +00:00
}
}
2020-06-10 08:57:59 +00:00
void Painter : : draw_bitmap ( const IntPoint & p , const GlyphBitmap & bitmap , Color color )
2019-02-05 05:43:33 +00:00
{
2021-01-20 14:59:22 +00:00
auto dst_rect = IntRect ( p , bitmap . size ( ) ) . translated ( translation ( ) ) ;
2019-04-16 11:58:02 +00:00
auto clipped_rect = dst_rect . intersected ( clip_rect ( ) ) ;
2019-02-05 05:43:33 +00:00
if ( clipped_rect . is_empty ( ) )
return ;
2019-02-10 06:46:29 +00:00
const int first_row = clipped_rect . top ( ) - dst_rect . top ( ) ;
const int last_row = clipped_rect . bottom ( ) - dst_rect . top ( ) ;
const int first_column = clipped_rect . left ( ) - dst_rect . left ( ) ;
const int last_column = clipped_rect . right ( ) - dst_rect . left ( ) ;
2021-01-20 14:59:22 +00:00
int scale = this - > scale ( ) ;
RGBA32 * dst = m_target - > scanline ( clipped_rect . y ( ) * scale ) + clipped_rect . x ( ) * scale ;
2019-05-06 12:04:54 +00:00
const size_t dst_skip = m_target - > pitch ( ) / sizeof ( RGBA32 ) ;
2019-02-05 05:43:33 +00:00
2021-01-20 14:59:22 +00:00
if ( scale = = 1 ) {
LibGfx: Make it possible to apply an (integer) scale to a Painter
This adds a scale factor to Painter, which will be used for HighDPI
support. It's also a step towards general affine transforms on Painters.
All of Painter's public API takes logical coordinates, while some
internals deal with physical coordinates now. If scale == 1, logical
and physical coordinates are the same. For scale == 2, a 200x100 bitmap
would be covered by a logical {0, 0, 100, 50} rect, while its physical
size would be {0, 0, 200, 100}.
Most of Painter's functions just assert that scale() == 1 is for now,
but most functions called by WindowServer are updated to handle
arbitrary (integer) scale.
Also add a new Demo "LibGfxScaleDemo" that covers the converted
functions and that can be used to iteratively add scaling support
to more functions.
To make Painter's interface deal with logical coordinates only,
make translation() and clip_rect() non-public.
2021-01-12 20:53:07 +00:00
for ( int row = first_row ; row < = last_row ; + + row ) {
for ( int j = 0 ; j < = ( last_column - first_column ) ; + + j ) {
if ( bitmap . bit_at ( j + first_column , row ) )
dst [ j ] = color . value ( ) ;
}
dst + = dst_skip ;
}
} else {
for ( int row = first_row ; row < = last_row ; + + row ) {
for ( int j = 0 ; j < = ( last_column - first_column ) ; + + j ) {
2021-01-20 14:59:22 +00:00
if ( bitmap . bit_at ( ( j + first_column ) , row ) ) {
for ( int iy = 0 ; iy < scale ; + + iy )
for ( int ix = 0 ; ix < scale ; + + ix )
dst [ j * scale + ix + iy * dst_skip ] = color . value ( ) ;
}
LibGfx: Make it possible to apply an (integer) scale to a Painter
This adds a scale factor to Painter, which will be used for HighDPI
support. It's also a step towards general affine transforms on Painters.
All of Painter's public API takes logical coordinates, while some
internals deal with physical coordinates now. If scale == 1, logical
and physical coordinates are the same. For scale == 2, a 200x100 bitmap
would be covered by a logical {0, 0, 100, 50} rect, while its physical
size would be {0, 0, 200, 100}.
Most of Painter's functions just assert that scale() == 1 is for now,
but most functions called by WindowServer are updated to handle
arbitrary (integer) scale.
Also add a new Demo "LibGfxScaleDemo" that covers the converted
functions and that can be used to iteratively add scaling support
to more functions.
To make Painter's interface deal with logical coordinates only,
make translation() and clip_rect() non-public.
2021-01-12 20:53:07 +00:00
}
2021-01-20 14:59:22 +00:00
dst + = dst_skip * scale ;
2019-02-05 05:43:33 +00:00
}
}
}
2020-06-10 08:57:59 +00:00
void Painter : : draw_triangle ( const IntPoint & a , const IntPoint & b , const IntPoint & c , Color color )
2020-04-17 22:13:22 +00:00
{
2021-09-05 10:25:02 +00:00
IntPoint p0 ( to_physical ( a ) ) ;
IntPoint p1 ( to_physical ( b ) ) ;
IntPoint p2 ( to_physical ( c ) ) ;
2020-04-17 22:13:22 +00:00
2021-03-05 22:29:58 +00:00
// sort points from top to bottom
2020-04-17 22:13:22 +00:00
if ( p0 . y ( ) > p1 . y ( ) )
swap ( p0 , p1 ) ;
if ( p0 . y ( ) > p2 . y ( ) )
swap ( p0 , p2 ) ;
if ( p1 . y ( ) > p2 . y ( ) )
swap ( p1 , p2 ) ;
2021-03-05 22:29:58 +00:00
// return if top and bottom points are on same line
if ( p0 . y ( ) = = p2 . y ( ) )
return ;
// return if top is below clip rect or bottom is above clip rect
2020-04-17 22:13:22 +00:00
auto clip = clip_rect ( ) ;
if ( p0 . y ( ) > = clip . bottom ( ) )
return ;
if ( p2 . y ( ) < clip . top ( ) )
return ;
2021-03-05 22:29:58 +00:00
int rgba = color . value ( ) ;
2020-04-17 22:13:22 +00:00
2021-03-05 22:29:58 +00:00
float dx02 = ( float ) ( p2 . x ( ) - p0 . x ( ) ) / ( p2 . y ( ) - p0 . y ( ) ) ;
2020-04-17 22:13:22 +00:00
float x01 = p0 . x ( ) ;
float x02 = p0 . x ( ) ;
2021-03-05 22:29:58 +00:00
if ( p0 . y ( ) ! = p1 . y ( ) ) { // p0 and p1 are on different lines
float dx01 = ( float ) ( p1 . x ( ) - p0 . x ( ) ) / ( p1 . y ( ) - p0 . y ( ) ) ;
2020-04-17 22:13:22 +00:00
2021-03-05 22:29:58 +00:00
int top = p0 . y ( ) ;
if ( top < clip . top ( ) ) {
x01 + = dx01 * ( clip . top ( ) - top ) ;
x02 + = dx02 * ( clip . top ( ) - top ) ;
top = clip . top ( ) ;
}
for ( int y = top ; y < p1 . y ( ) & & y < clip . bottom ( ) ; + + y ) { // XXX <=?
int start = x01 > x02 ? max ( ( int ) x02 , clip . left ( ) ) : max ( ( int ) x01 , clip . left ( ) ) ;
int end = x01 > x02 ? min ( ( int ) x01 , clip . right ( ) ) : min ( ( int ) x02 , clip . right ( ) ) ;
auto * scanline = m_target - > scanline ( y ) ;
for ( int x = start ; x < end ; x + + ) {
scanline [ x ] = rgba ;
}
x01 + = dx01 ;
x02 + = dx02 ;
2020-04-17 22:13:22 +00:00
}
}
2021-03-05 22:29:58 +00:00
// return if middle point and bottom point are on same line
if ( p1 . y ( ) = = p2 . y ( ) )
return ;
2020-04-17 22:13:22 +00:00
2021-03-05 22:29:58 +00:00
float x12 = p1 . x ( ) ;
float dx12 = ( float ) ( p2 . x ( ) - p1 . x ( ) ) / ( p2 . y ( ) - p1 . y ( ) ) ;
int top = p1 . y ( ) ;
2020-04-17 22:13:22 +00:00
if ( top < clip . top ( ) ) {
2020-04-18 11:21:32 +00:00
x02 + = dx02 * ( clip . top ( ) - top ) ;
x12 + = dx12 * ( clip . top ( ) - top ) ;
2020-04-17 22:13:22 +00:00
top = clip . top ( ) ;
}
LibGfx: Make it possible to apply an (integer) scale to a Painter
This adds a scale factor to Painter, which will be used for HighDPI
support. It's also a step towards general affine transforms on Painters.
All of Painter's public API takes logical coordinates, while some
internals deal with physical coordinates now. If scale == 1, logical
and physical coordinates are the same. For scale == 2, a 200x100 bitmap
would be covered by a logical {0, 0, 100, 50} rect, while its physical
size would be {0, 0, 200, 100}.
Most of Painter's functions just assert that scale() == 1 is for now,
but most functions called by WindowServer are updated to handle
arbitrary (integer) scale.
Also add a new Demo "LibGfxScaleDemo" that covers the converted
functions and that can be used to iteratively add scaling support
to more functions.
To make Painter's interface deal with logical coordinates only,
make translation() and clip_rect() non-public.
2021-01-12 20:53:07 +00:00
for ( int y = top ; y < p2 . y ( ) & & y < clip . bottom ( ) ; + + y ) { // XXX <=?
2020-04-17 22:13:22 +00:00
int start = x12 > x02 ? max ( ( int ) x02 , clip . left ( ) ) : max ( ( int ) x12 , clip . left ( ) ) ;
int end = x12 > x02 ? min ( ( int ) x12 , clip . right ( ) ) : min ( ( int ) x02 , clip . right ( ) ) ;
auto * scanline = m_target - > scanline ( y ) ;
for ( int x = start ; x < end ; x + + ) {
scanline [ x ] = rgba ;
}
x02 + = dx02 ;
x12 + = dx12 ;
}
}
2021-03-21 08:40:14 +00:00
struct BlitState {
enum AlphaState {
NoAlpha = 0 ,
SrcAlpha = 1 ,
DstAlpha = 2 ,
BothAlpha = SrcAlpha | DstAlpha
} ;
const RGBA32 * src ;
RGBA32 * dst ;
size_t src_pitch ;
size_t dst_pitch ;
int row_count ;
int column_count ;
float opacity ;
} ;
template < BlitState : : AlphaState has_alpha >
static void do_blit_with_opacity ( BlitState & state )
{
for ( int row = 0 ; row < state . row_count ; + + row ) {
for ( int x = 0 ; x < state . column_count ; + + x ) {
Color dest_color = ( has_alpha & BlitState : : DstAlpha ) ? Color : : from_rgba ( state . dst [ x ] ) : Color : : from_rgb ( state . dst [ x ] ) ;
if constexpr ( has_alpha & BlitState : : SrcAlpha ) {
Color src_color_with_alpha = Color : : from_rgba ( state . src [ x ] ) ;
float pixel_opacity = src_color_with_alpha . alpha ( ) / 255.0 ;
src_color_with_alpha . set_alpha ( 255 * ( state . opacity * pixel_opacity ) ) ;
state . dst [ x ] = dest_color . blend ( src_color_with_alpha ) . value ( ) ;
} else {
Color src_color_with_alpha = Color : : from_rgb ( state . src [ x ] ) ;
src_color_with_alpha . set_alpha ( state . opacity * 255 ) ;
state . dst [ x ] = dest_color . blend ( src_color_with_alpha ) . value ( ) ;
}
}
state . dst + = state . dst_pitch ;
state . src + = state . src_pitch ;
}
}
2021-02-13 18:10:27 +00:00
void Painter : : blit_with_opacity ( const IntPoint & position , const Gfx : : Bitmap & source , const IntRect & a_src_rect , float opacity , bool apply_alpha )
2019-02-19 00:42:53 +00:00
{
2021-02-23 19:42:32 +00:00
VERIFY ( scale ( ) > = source . scale ( ) & & " painter doesn't support downsampling scale factors " ) ;
2019-02-19 00:42:53 +00:00
2021-02-13 18:10:27 +00:00
if ( opacity > = 1.0f & & ! ( source . has_alpha_channel ( ) & & apply_alpha ) )
2021-01-25 02:41:38 +00:00
return blit ( position , source , a_src_rect ) ;
2019-02-19 00:42:53 +00:00
2021-01-25 02:41:38 +00:00
IntRect safe_src_rect = IntRect : : intersection ( a_src_rect , source . rect ( ) ) ;
if ( scale ( ) ! = source . scale ( ) )
return draw_scaled_bitmap ( { position , safe_src_rect . size ( ) } , source , safe_src_rect , opacity ) ;
auto dst_rect = IntRect ( position , safe_src_rect . size ( ) ) . translated ( translation ( ) ) ;
2021-01-25 17:25:02 +00:00
auto clipped_rect = dst_rect . intersected ( clip_rect ( ) ) ;
2019-02-19 00:42:53 +00:00
if ( clipped_rect . is_empty ( ) )
return ;
2021-01-25 02:41:38 +00:00
int scale = this - > scale ( ) ;
auto src_rect = a_src_rect * scale ;
clipped_rect * = scale ;
dst_rect * = scale ;
2019-02-19 00:42:53 +00:00
const int first_row = clipped_rect . top ( ) - dst_rect . top ( ) ;
const int last_row = clipped_rect . bottom ( ) - dst_rect . top ( ) ;
const int first_column = clipped_rect . left ( ) - dst_rect . left ( ) ;
const int last_column = clipped_rect . right ( ) - dst_rect . left ( ) ;
2021-03-21 08:40:14 +00:00
BlitState blit_state {
. src = source . scanline ( src_rect . top ( ) + first_row ) + src_rect . left ( ) + first_column ,
. dst = m_target - > scanline ( clipped_rect . y ( ) ) + clipped_rect . x ( ) ,
. src_pitch = source . pitch ( ) / sizeof ( RGBA32 ) ,
. dst_pitch = m_target - > pitch ( ) / sizeof ( RGBA32 ) ,
. row_count = last_row - first_row + 1 ,
. column_count = last_column - first_column + 1 ,
. opacity = opacity
2021-02-13 18:10:27 +00:00
} ;
2021-03-21 08:40:14 +00:00
if ( source . has_alpha_channel ( ) & & apply_alpha ) {
if ( m_target - > has_alpha_channel ( ) )
do_blit_with_opacity < BlitState : : BothAlpha > ( blit_state ) ;
else
do_blit_with_opacity < BlitState : : SrcAlpha > ( blit_state ) ;
} else {
if ( m_target - > has_alpha_channel ( ) )
do_blit_with_opacity < BlitState : : DstAlpha > ( blit_state ) ;
else
do_blit_with_opacity < BlitState : : NoAlpha > ( blit_state ) ;
}
2019-02-19 00:42:53 +00:00
}
2020-06-10 08:57:59 +00:00
void Painter : : blit_filtered ( const IntPoint & position , const Gfx : : Bitmap & source , const IntRect & src_rect , Function < Color ( Color ) > filter )
2019-04-10 14:14:44 +00:00
{
2021-02-23 19:42:32 +00:00
VERIFY ( ( source . scale ( ) = = 1 | | source . scale ( ) = = scale ( ) ) & & " blit_filtered only supports integer upsampling " ) ;
LibGfx: Make it possible to apply an (integer) scale to a Painter
This adds a scale factor to Painter, which will be used for HighDPI
support. It's also a step towards general affine transforms on Painters.
All of Painter's public API takes logical coordinates, while some
internals deal with physical coordinates now. If scale == 1, logical
and physical coordinates are the same. For scale == 2, a 200x100 bitmap
would be covered by a logical {0, 0, 100, 50} rect, while its physical
size would be {0, 0, 200, 100}.
Most of Painter's functions just assert that scale() == 1 is for now,
but most functions called by WindowServer are updated to handle
arbitrary (integer) scale.
Also add a new Demo "LibGfxScaleDemo" that covers the converted
functions and that can be used to iteratively add scaling support
to more functions.
To make Painter's interface deal with logical coordinates only,
make translation() and clip_rect() non-public.
2021-01-12 20:53:07 +00:00
2020-06-10 08:57:59 +00:00
IntRect safe_src_rect = src_rect . intersected ( source . rect ( ) ) ;
auto dst_rect = IntRect ( position , safe_src_rect . size ( ) ) . translated ( translation ( ) ) ;
2019-04-16 11:58:02 +00:00
auto clipped_rect = dst_rect . intersected ( clip_rect ( ) ) ;
2019-04-10 14:14:44 +00:00
if ( clipped_rect . is_empty ( ) )
return ;
2021-01-25 19:54:38 +00:00
int scale = this - > scale ( ) ;
clipped_rect * = scale ;
dst_rect * = scale ;
safe_src_rect * = source . scale ( ) ;
2019-04-10 14:14:44 +00:00
const int first_row = clipped_rect . top ( ) - dst_rect . top ( ) ;
const int last_row = clipped_rect . bottom ( ) - dst_rect . top ( ) ;
const int first_column = clipped_rect . left ( ) - dst_rect . left ( ) ;
const int last_column = clipped_rect . right ( ) - dst_rect . left ( ) ;
RGBA32 * dst = m_target - > scanline ( clipped_rect . y ( ) ) + clipped_rect . x ( ) ;
2019-05-06 12:04:54 +00:00
const size_t dst_skip = m_target - > pitch ( ) / sizeof ( RGBA32 ) ;
2019-04-10 14:14:44 +00:00
2021-01-25 19:54:38 +00:00
int s = scale / source . scale ( ) ;
if ( s = = 1 ) {
const RGBA32 * src = source . scanline ( safe_src_rect . top ( ) + first_row ) + safe_src_rect . left ( ) + first_column ;
const size_t src_skip = source . pitch ( ) / sizeof ( RGBA32 ) ;
for ( int row = first_row ; row < = last_row ; + + row ) {
for ( int x = 0 ; x < = ( last_column - first_column ) ; + + x ) {
u8 alpha = Color : : from_rgba ( src [ x ] ) . alpha ( ) ;
2021-02-11 06:29:16 +00:00
if ( alpha = = 0xff ) {
auto color = filter ( Color : : from_rgba ( src [ x ] ) ) ;
if ( color . alpha ( ) = = 0xff )
dst [ x ] = color . value ( ) ;
else
dst [ x ] = Color : : from_rgba ( dst [ x ] ) . blend ( color ) . value ( ) ;
} else if ( ! alpha )
2021-01-25 19:54:38 +00:00
continue ;
else
dst [ x ] = Color : : from_rgba ( dst [ x ] ) . blend ( filter ( Color : : from_rgba ( src [ x ] ) ) ) . value ( ) ;
}
dst + = dst_skip ;
src + = src_skip ;
}
} else {
for ( int row = first_row ; row < = last_row ; + + row ) {
const RGBA32 * src = source . scanline ( safe_src_rect . top ( ) + row / s ) + safe_src_rect . left ( ) + first_column / s ;
for ( int x = 0 ; x < = ( last_column - first_column ) ; + + x ) {
u8 alpha = Color : : from_rgba ( src [ x / s ] ) . alpha ( ) ;
2021-02-11 06:29:16 +00:00
if ( alpha = = 0xff ) {
auto color = filter ( Color : : from_rgba ( src [ x / s ] ) ) ;
if ( color . alpha ( ) = = 0xff )
dst [ x ] = color . value ( ) ;
else
dst [ x ] = Color : : from_rgba ( dst [ x ] ) . blend ( color ) . value ( ) ;
} else if ( ! alpha )
2021-01-25 19:54:38 +00:00
continue ;
else
dst [ x ] = Color : : from_rgba ( dst [ x ] ) . blend ( filter ( Color : : from_rgba ( src [ x / s ] ) ) ) . value ( ) ;
}
dst + = dst_skip ;
2019-04-10 14:14:44 +00:00
}
}
}
2020-06-10 08:57:59 +00:00
void Painter : : blit_brightened ( const IntPoint & position , const Gfx : : Bitmap & source , const IntRect & src_rect )
2020-03-30 17:39:37 +00:00
{
return blit_filtered ( position , source , src_rect , [ ] ( Color src ) {
return src . lightened ( ) ;
} ) ;
}
2020-06-10 08:57:59 +00:00
void Painter : : blit_dimmed ( const IntPoint & position , const Gfx : : Bitmap & source , const IntRect & src_rect )
2020-03-30 17:39:37 +00:00
{
return blit_filtered ( position , source , src_rect , [ ] ( Color src ) {
return src . to_grayscale ( ) . lightened ( ) ;
} ) ;
}
2020-06-10 08:57:59 +00:00
void Painter : : draw_tiled_bitmap ( const IntRect & a_dst_rect , const Gfx : : Bitmap & source )
2019-05-26 17:14:03 +00:00
{
2021-02-23 19:42:32 +00:00
VERIFY ( ( source . scale ( ) = = 1 | | source . scale ( ) = = scale ( ) ) & & " draw_tiled_bitmap only supports integer upsampling " ) ;
LibGfx: Make it possible to apply an (integer) scale to a Painter
This adds a scale factor to Painter, which will be used for HighDPI
support. It's also a step towards general affine transforms on Painters.
All of Painter's public API takes logical coordinates, while some
internals deal with physical coordinates now. If scale == 1, logical
and physical coordinates are the same. For scale == 2, a 200x100 bitmap
would be covered by a logical {0, 0, 100, 50} rect, while its physical
size would be {0, 0, 200, 100}.
Most of Painter's functions just assert that scale() == 1 is for now,
but most functions called by WindowServer are updated to handle
arbitrary (integer) scale.
Also add a new Demo "LibGfxScaleDemo" that covers the converted
functions and that can be used to iteratively add scaling support
to more functions.
To make Painter's interface deal with logical coordinates only,
make translation() and clip_rect() non-public.
2021-01-12 20:53:07 +00:00
2019-10-19 09:05:21 +00:00
auto dst_rect = a_dst_rect . translated ( translation ( ) ) ;
2019-05-26 17:14:03 +00:00
auto clipped_rect = dst_rect . intersected ( clip_rect ( ) ) ;
if ( clipped_rect . is_empty ( ) )
return ;
2021-01-22 18:33:57 +00:00
int scale = this - > scale ( ) ;
clipped_rect * = scale ;
dst_rect * = scale ;
2019-05-27 02:36:16 +00:00
const int first_row = ( clipped_rect . top ( ) - dst_rect . top ( ) ) ;
const int last_row = ( clipped_rect . bottom ( ) - dst_rect . top ( ) ) ;
const int first_column = ( clipped_rect . left ( ) - dst_rect . left ( ) ) ;
2019-05-26 17:14:03 +00:00
RGBA32 * dst = m_target - > scanline ( clipped_rect . y ( ) ) + clipped_rect . x ( ) ;
const size_t dst_skip = m_target - > pitch ( ) / sizeof ( RGBA32 ) ;
2021-03-16 10:48:42 +00:00
if ( source . format ( ) = = BitmapFormat : : BGRx8888 | | source . format ( ) = = BitmapFormat : : BGRA8888 ) {
2021-01-22 18:33:57 +00:00
int s = scale / source . scale ( ) ;
if ( s = = 1 ) {
2021-01-22 23:53:09 +00:00
int x_start = first_column + a_dst_rect . left ( ) * scale ;
2021-01-22 18:33:57 +00:00
for ( int row = first_row ; row < = last_row ; + + row ) {
2021-01-22 23:53:09 +00:00
const RGBA32 * sl = source . scanline ( ( row + a_dst_rect . top ( ) * scale ) % source . physical_height ( ) ) ;
2021-01-22 18:33:57 +00:00
for ( int x = x_start ; x < clipped_rect . width ( ) + x_start ; + + x ) {
dst [ x - x_start ] = sl [ x % source . physical_width ( ) ] ;
}
dst + = dst_skip ;
}
} else {
2021-01-22 23:53:09 +00:00
int x_start = first_column + a_dst_rect . left ( ) * scale ;
2021-01-22 18:33:57 +00:00
for ( int row = first_row ; row < = last_row ; + + row ) {
2021-01-22 23:53:09 +00:00
const RGBA32 * sl = source . scanline ( ( ( row + a_dst_rect . top ( ) * scale ) / s ) % source . physical_height ( ) ) ;
2021-01-22 18:33:57 +00:00
for ( int x = x_start ; x < clipped_rect . width ( ) + x_start ; + + x ) {
2021-01-22 23:53:09 +00:00
dst [ x - x_start ] = sl [ ( x / s ) % source . physical_width ( ) ] ;
2021-01-22 18:33:57 +00:00
}
dst + = dst_skip ;
2019-05-27 02:36:16 +00:00
}
2019-05-26 17:14:03 +00:00
}
2019-05-27 02:36:16 +00:00
return ;
2019-05-26 17:14:03 +00:00
}
2019-05-27 02:36:16 +00:00
2021-02-23 19:42:32 +00:00
VERIFY_NOT_REACHED ( ) ;
2019-05-26 17:14:03 +00:00
}
2019-04-10 14:14:44 +00:00
2021-01-22 16:12:00 +00:00
void Painter : : blit_offset ( const IntPoint & a_position , const Gfx : : Bitmap & source , const IntRect & a_src_rect , const IntPoint & offset )
2019-05-27 03:10:23 +00:00
{
2021-01-22 16:12:00 +00:00
auto src_rect = IntRect { a_src_rect . location ( ) - offset , a_src_rect . size ( ) } ;
auto position = a_position ;
if ( src_rect . x ( ) < 0 ) {
position . set_x ( position . x ( ) - src_rect . x ( ) ) ;
src_rect . set_x ( 0 ) ;
2019-05-27 03:10:23 +00:00
}
2021-01-22 16:12:00 +00:00
if ( src_rect . y ( ) < 0 ) {
position . set_y ( position . y ( ) - src_rect . y ( ) ) ;
src_rect . set_y ( 0 ) ;
}
blit ( position , source , src_rect ) ;
2019-05-27 03:10:23 +00:00
}
2021-02-09 00:27:51 +00:00
void Painter : : blit ( const IntPoint & position , const Gfx : : Bitmap & source , const IntRect & a_src_rect , float opacity , bool apply_alpha )
2019-02-10 06:46:29 +00:00
{
2021-02-23 19:42:32 +00:00
VERIFY ( scale ( ) > = source . scale ( ) & & " painter doesn't support downsampling scale factors " ) ;
2021-01-19 17:10:47 +00:00
2021-02-13 18:10:27 +00:00
if ( opacity < 1.0f | | ( source . has_alpha_channel ( ) & & apply_alpha ) )
return blit_with_opacity ( position , source , a_src_rect , opacity , apply_alpha ) ;
2021-01-22 23:56:01 +00:00
auto safe_src_rect = a_src_rect . intersected ( source . rect ( ) ) ;
2021-01-19 17:10:47 +00:00
if ( scale ( ) ! = source . scale ( ) )
2021-01-22 23:56:01 +00:00
return draw_scaled_bitmap ( { position , safe_src_rect . size ( ) } , source , safe_src_rect , opacity ) ;
2021-01-19 17:10:47 +00:00
// If we get here, the Painter might have a scale factor, but the source bitmap has the same scale factor.
// We need to transform from logical to physical coordinates, but we can just copy pixels without resampling.
2021-01-20 14:59:22 +00:00
auto dst_rect = IntRect ( position , safe_src_rect . size ( ) ) . translated ( translation ( ) ) ;
2019-04-16 11:58:02 +00:00
auto clipped_rect = dst_rect . intersected ( clip_rect ( ) ) ;
2019-02-10 06:46:29 +00:00
if ( clipped_rect . is_empty ( ) )
return ;
2021-01-20 14:59:22 +00:00
// All computations below are in physical coordinates.
int scale = this - > scale ( ) ;
auto src_rect = a_src_rect * scale ;
clipped_rect * = scale ;
dst_rect * = scale ;
2019-02-10 06:46:29 +00:00
const int first_row = clipped_rect . top ( ) - dst_rect . top ( ) ;
const int last_row = clipped_rect . bottom ( ) - dst_rect . top ( ) ;
const int first_column = clipped_rect . left ( ) - dst_rect . left ( ) ;
RGBA32 * dst = m_target - > scanline ( clipped_rect . y ( ) ) + clipped_rect . x ( ) ;
2019-05-06 12:04:54 +00:00
const size_t dst_skip = m_target - > pitch ( ) / sizeof ( RGBA32 ) ;
2019-02-10 06:46:29 +00:00
2021-03-16 10:48:42 +00:00
if ( source . format ( ) = = BitmapFormat : : BGRx8888 | | source . format ( ) = = BitmapFormat : : BGRA8888 ) {
2019-05-06 17:32:56 +00:00
const RGBA32 * src = source . scanline ( src_rect . top ( ) + first_row ) + src_rect . left ( ) + first_column ;
const size_t src_skip = source . pitch ( ) / sizeof ( RGBA32 ) ;
for ( int row = first_row ; row < = last_row ; + + row ) {
2019-07-03 19:17:35 +00:00
fast_u32_copy ( dst , src , clipped_rect . width ( ) ) ;
2019-05-06 17:32:56 +00:00
dst + = dst_skip ;
src + = src_skip ;
}
return ;
}
2021-03-16 11:09:15 +00:00
if ( source . format ( ) = = BitmapFormat : : RGBA8888 ) {
const u32 * src = source . scanline ( src_rect . top ( ) + first_row ) + src_rect . left ( ) + first_column ;
const size_t src_skip = source . pitch ( ) / sizeof ( u32 ) ;
for ( int row = first_row ; row < = last_row ; + + row ) {
for ( int i = 0 ; i < clipped_rect . width ( ) ; + + i ) {
u32 rgba = src [ i ] ;
u32 bgra = ( rgba & 0xff00ff00 )
| ( ( rgba & 0x000000ff ) < < 16 )
| ( ( rgba & 0x00ff0000 ) > > 16 ) ;
dst [ i ] = bgra ;
}
dst + = dst_skip ;
src + = src_skip ;
}
return ;
}
2020-06-17 19:37:16 +00:00
if ( Bitmap : : is_indexed ( source . format ( ) ) ) {
2020-09-05 23:10:33 +00:00
const u8 * src = source . scanline_u8 ( src_rect . top ( ) + first_row ) + src_rect . left ( ) + first_column ;
2019-05-06 17:32:56 +00:00
const size_t src_skip = source . pitch ( ) ;
for ( int row = first_row ; row < = last_row ; + + row ) {
for ( int i = 0 ; i < clipped_rect . width ( ) ; + + i )
dst [ i ] = source . palette_color ( src [ i ] ) . value ( ) ;
dst + = dst_skip ;
src + = src_skip ;
}
return ;
2019-02-10 06:46:29 +00:00
}
2019-05-06 17:32:56 +00:00
2021-02-23 19:42:32 +00:00
VERIFY_NOT_REACHED ( ) ;
2019-02-10 06:46:29 +00:00
}
2019-05-11 01:53:28 +00:00
template < bool has_alpha_channel , typename GetPixel >
LibGfx: Make it possible to apply an (integer) scale to a Painter
This adds a scale factor to Painter, which will be used for HighDPI
support. It's also a step towards general affine transforms on Painters.
All of Painter's public API takes logical coordinates, while some
internals deal with physical coordinates now. If scale == 1, logical
and physical coordinates are the same. For scale == 2, a 200x100 bitmap
would be covered by a logical {0, 0, 100, 50} rect, while its physical
size would be {0, 0, 200, 100}.
Most of Painter's functions just assert that scale() == 1 is for now,
but most functions called by WindowServer are updated to handle
arbitrary (integer) scale.
Also add a new Demo "LibGfxScaleDemo" that covers the converted
functions and that can be used to iteratively add scaling support
to more functions.
To make Painter's interface deal with logical coordinates only,
make translation() and clip_rect() non-public.
2021-01-12 20:53:07 +00:00
ALWAYS_INLINE static void do_draw_integer_scaled_bitmap ( Gfx : : Bitmap & target , const IntRect & dst_rect , const IntRect & src_rect , const Gfx : : Bitmap & source , int hfactor , int vfactor , GetPixel get_pixel , float opacity )
2019-05-11 01:53:28 +00:00
{
2020-08-05 12:40:11 +00:00
bool has_opacity = opacity ! = 1.0f ;
LibGfx: Make it possible to apply an (integer) scale to a Painter
This adds a scale factor to Painter, which will be used for HighDPI
support. It's also a step towards general affine transforms on Painters.
All of Painter's public API takes logical coordinates, while some
internals deal with physical coordinates now. If scale == 1, logical
and physical coordinates are the same. For scale == 2, a 200x100 bitmap
would be covered by a logical {0, 0, 100, 50} rect, while its physical
size would be {0, 0, 200, 100}.
Most of Painter's functions just assert that scale() == 1 is for now,
but most functions called by WindowServer are updated to handle
arbitrary (integer) scale.
Also add a new Demo "LibGfxScaleDemo" that covers the converted
functions and that can be used to iteratively add scaling support
to more functions.
To make Painter's interface deal with logical coordinates only,
make translation() and clip_rect() non-public.
2021-01-12 20:53:07 +00:00
for ( int y = 0 ; y < src_rect . height ( ) ; + + y ) {
2019-05-11 14:53:53 +00:00
int dst_y = dst_rect . y ( ) + y * vfactor ;
LibGfx: Make it possible to apply an (integer) scale to a Painter
This adds a scale factor to Painter, which will be used for HighDPI
support. It's also a step towards general affine transforms on Painters.
All of Painter's public API takes logical coordinates, while some
internals deal with physical coordinates now. If scale == 1, logical
and physical coordinates are the same. For scale == 2, a 200x100 bitmap
would be covered by a logical {0, 0, 100, 50} rect, while its physical
size would be {0, 0, 200, 100}.
Most of Painter's functions just assert that scale() == 1 is for now,
but most functions called by WindowServer are updated to handle
arbitrary (integer) scale.
Also add a new Demo "LibGfxScaleDemo" that covers the converted
functions and that can be used to iteratively add scaling support
to more functions.
To make Painter's interface deal with logical coordinates only,
make translation() and clip_rect() non-public.
2021-01-12 20:53:07 +00:00
for ( int x = 0 ; x < src_rect . width ( ) ; + + x ) {
auto src_pixel = get_pixel ( source , x + src_rect . left ( ) , y + src_rect . top ( ) ) ;
2020-08-05 12:40:11 +00:00
if ( has_opacity )
src_pixel . set_alpha ( src_pixel . alpha ( ) * opacity ) ;
2019-05-11 03:34:20 +00:00
for ( int yo = 0 ; yo < vfactor ; + + yo ) {
2019-05-11 14:53:53 +00:00
auto * scanline = ( Color * ) target . scanline ( dst_y + yo ) ;
int dst_x = dst_rect . x ( ) + x * hfactor ;
2019-05-11 03:34:20 +00:00
for ( int xo = 0 ; xo < hfactor ; + + xo ) {
if constexpr ( has_alpha_channel )
2019-05-11 14:53:53 +00:00
scanline [ dst_x + xo ] = scanline [ dst_x + xo ] . blend ( src_pixel ) ;
2019-05-11 03:34:20 +00:00
else
2019-05-11 14:53:53 +00:00
scanline [ dst_x + xo ] = src_pixel ;
2019-05-11 03:34:20 +00:00
}
}
}
}
}
template < bool has_alpha_channel , typename GetPixel >
LibGfx: Add a draw_scaled_bitmap() variant that takes a FloatRect as src_rect
Consider
draw_scaled_bitmap({0, 0, 10, 10}, source, {0, 0, 5, 5}).
Imagine wanting to split that up into two calls, like e.g. the
compositor when redrawing the background with damage rects. You really
want to be able to say
draw_scaled_bitmap({0, 0, 5, 10}, source, {0, 0, 2.5, 5})
but up to now you couldn't. Now you can.
This makes painting very low-res images (such as tile.png) in mode
"stretch" work much better.
2021-01-22 20:13:47 +00:00
ALWAYS_INLINE static void do_draw_scaled_bitmap ( Gfx : : Bitmap & target , const IntRect & dst_rect , const IntRect & clipped_rect , const Gfx : : Bitmap & source , const FloatRect & src_rect , GetPixel get_pixel , float opacity )
2019-05-11 03:34:20 +00:00
{
LibGfx: Add a draw_scaled_bitmap() variant that takes a FloatRect as src_rect
Consider
draw_scaled_bitmap({0, 0, 10, 10}, source, {0, 0, 5, 5}).
Imagine wanting to split that up into two calls, like e.g. the
compositor when redrawing the background with damage rects. You really
want to be able to say
draw_scaled_bitmap({0, 0, 5, 10}, source, {0, 0, 2.5, 5})
but up to now you couldn't. Now you can.
This makes painting very low-res images (such as tile.png) in mode
"stretch" work much better.
2021-01-22 20:13:47 +00:00
IntRect int_src_rect = enclosing_int_rect ( src_rect ) ;
if ( dst_rect = = clipped_rect & & int_src_rect = = src_rect & & ! ( dst_rect . width ( ) % int_src_rect . width ( ) ) & & ! ( dst_rect . height ( ) % int_src_rect . height ( ) ) ) {
int hfactor = dst_rect . width ( ) / int_src_rect . width ( ) ;
int vfactor = dst_rect . height ( ) / int_src_rect . height ( ) ;
2019-05-11 03:34:20 +00:00
if ( hfactor = = 2 & & vfactor = = 2 )
LibGfx: Add a draw_scaled_bitmap() variant that takes a FloatRect as src_rect
Consider
draw_scaled_bitmap({0, 0, 10, 10}, source, {0, 0, 5, 5}).
Imagine wanting to split that up into two calls, like e.g. the
compositor when redrawing the background with damage rects. You really
want to be able to say
draw_scaled_bitmap({0, 0, 5, 10}, source, {0, 0, 2.5, 5})
but up to now you couldn't. Now you can.
This makes painting very low-res images (such as tile.png) in mode
"stretch" work much better.
2021-01-22 20:13:47 +00:00
return do_draw_integer_scaled_bitmap < has_alpha_channel > ( target , dst_rect , int_src_rect , source , 2 , 2 , get_pixel , opacity ) ;
2019-05-11 03:34:20 +00:00
if ( hfactor = = 3 & & vfactor = = 3 )
LibGfx: Add a draw_scaled_bitmap() variant that takes a FloatRect as src_rect
Consider
draw_scaled_bitmap({0, 0, 10, 10}, source, {0, 0, 5, 5}).
Imagine wanting to split that up into two calls, like e.g. the
compositor when redrawing the background with damage rects. You really
want to be able to say
draw_scaled_bitmap({0, 0, 5, 10}, source, {0, 0, 2.5, 5})
but up to now you couldn't. Now you can.
This makes painting very low-res images (such as tile.png) in mode
"stretch" work much better.
2021-01-22 20:13:47 +00:00
return do_draw_integer_scaled_bitmap < has_alpha_channel > ( target , dst_rect , int_src_rect , source , 3 , 3 , get_pixel , opacity ) ;
2019-05-11 03:34:20 +00:00
if ( hfactor = = 4 & & vfactor = = 4 )
LibGfx: Add a draw_scaled_bitmap() variant that takes a FloatRect as src_rect
Consider
draw_scaled_bitmap({0, 0, 10, 10}, source, {0, 0, 5, 5}).
Imagine wanting to split that up into two calls, like e.g. the
compositor when redrawing the background with damage rects. You really
want to be able to say
draw_scaled_bitmap({0, 0, 5, 10}, source, {0, 0, 2.5, 5})
but up to now you couldn't. Now you can.
This makes painting very low-res images (such as tile.png) in mode
"stretch" work much better.
2021-01-22 20:13:47 +00:00
return do_draw_integer_scaled_bitmap < has_alpha_channel > ( target , dst_rect , int_src_rect , source , 4 , 4 , get_pixel , opacity ) ;
return do_draw_integer_scaled_bitmap < has_alpha_channel > ( target , dst_rect , int_src_rect , source , hfactor , vfactor , get_pixel , opacity ) ;
2019-05-11 03:34:20 +00:00
}
2020-08-05 12:40:11 +00:00
bool has_opacity = opacity ! = 1.0f ;
2021-09-10 17:18:49 +00:00
i64 shift = ( i64 ) 1 < < 32 ;
i64 hscale = ( src_rect . width ( ) * shift ) / dst_rect . width ( ) ;
i64 vscale = ( src_rect . height ( ) * shift ) / dst_rect . height ( ) ;
i64 src_left = src_rect . left ( ) * shift ;
i64 src_top = src_rect . top ( ) * shift ;
2020-07-23 18:32:39 +00:00
2019-05-11 01:53:28 +00:00
for ( int y = clipped_rect . top ( ) ; y < = clipped_rect . bottom ( ) ; + + y ) {
auto * scanline = ( Color * ) target . scanline ( y ) ;
for ( int x = clipped_rect . left ( ) ; x < = clipped_rect . right ( ) ; + + x ) {
2021-09-10 17:18:49 +00:00
auto scaled_x = ( ( x - dst_rect . x ( ) ) * hscale + src_left ) > > 32 ;
auto scaled_y = ( ( y - dst_rect . y ( ) ) * vscale + src_top ) > > 32 ;
LibGfx: Add a draw_scaled_bitmap() variant that takes a FloatRect as src_rect
Consider
draw_scaled_bitmap({0, 0, 10, 10}, source, {0, 0, 5, 5}).
Imagine wanting to split that up into two calls, like e.g. the
compositor when redrawing the background with damage rects. You really
want to be able to say
draw_scaled_bitmap({0, 0, 5, 10}, source, {0, 0, 2.5, 5})
but up to now you couldn't. Now you can.
This makes painting very low-res images (such as tile.png) in mode
"stretch" work much better.
2021-01-22 20:13:47 +00:00
auto src_pixel = get_pixel ( source , scaled_x , scaled_y ) ;
2020-08-05 12:40:11 +00:00
if ( has_opacity )
src_pixel . set_alpha ( src_pixel . alpha ( ) * opacity ) ;
2019-05-11 01:53:28 +00:00
if constexpr ( has_alpha_channel ) {
scanline [ x ] = scanline [ x ] . blend ( src_pixel ) ;
} else
scanline [ x ] = src_pixel ;
}
}
}
2021-01-19 17:10:47 +00:00
void Painter : : draw_scaled_bitmap ( const IntRect & a_dst_rect , const Gfx : : Bitmap & source , const IntRect & a_src_rect , float opacity )
2019-03-22 03:20:10 +00:00
{
LibGfx: Add a draw_scaled_bitmap() variant that takes a FloatRect as src_rect
Consider
draw_scaled_bitmap({0, 0, 10, 10}, source, {0, 0, 5, 5}).
Imagine wanting to split that up into two calls, like e.g. the
compositor when redrawing the background with damage rects. You really
want to be able to say
draw_scaled_bitmap({0, 0, 5, 10}, source, {0, 0, 2.5, 5})
but up to now you couldn't. Now you can.
This makes painting very low-res images (such as tile.png) in mode
"stretch" work much better.
2021-01-22 20:13:47 +00:00
draw_scaled_bitmap ( a_dst_rect , source , FloatRect { a_src_rect } , opacity ) ;
}
void Painter : : draw_scaled_bitmap ( const IntRect & a_dst_rect , const Gfx : : Bitmap & source , const FloatRect & a_src_rect , float opacity )
{
IntRect int_src_rect = enclosing_int_rect ( a_src_rect ) ;
if ( scale ( ) = = source . scale ( ) & & a_src_rect = = int_src_rect & & a_dst_rect . size ( ) = = int_src_rect . size ( ) )
return blit ( a_dst_rect . location ( ) , source , int_src_rect , opacity ) ;
LibGfx: Make it possible to apply an (integer) scale to a Painter
This adds a scale factor to Painter, which will be used for HighDPI
support. It's also a step towards general affine transforms on Painters.
All of Painter's public API takes logical coordinates, while some
internals deal with physical coordinates now. If scale == 1, logical
and physical coordinates are the same. For scale == 2, a 200x100 bitmap
would be covered by a logical {0, 0, 100, 50} rect, while its physical
size would be {0, 0, 200, 100}.
Most of Painter's functions just assert that scale() == 1 is for now,
but most functions called by WindowServer are updated to handle
arbitrary (integer) scale.
Also add a new Demo "LibGfxScaleDemo" that covers the converted
functions and that can be used to iteratively add scaling support
to more functions.
To make Painter's interface deal with logical coordinates only,
make translation() and clip_rect() non-public.
2021-01-12 20:53:07 +00:00
auto dst_rect = to_physical ( a_dst_rect ) ;
2021-01-19 17:10:47 +00:00
auto src_rect = a_src_rect * source . scale ( ) ;
2021-01-20 14:59:22 +00:00
auto clipped_rect = dst_rect . intersected ( clip_rect ( ) * scale ( ) ) ;
2019-03-22 03:20:10 +00:00
if ( clipped_rect . is_empty ( ) )
return ;
2021-01-25 17:25:56 +00:00
if ( source . has_alpha_channel ( ) | | opacity ! = 1.0f ) {
2019-05-11 01:53:28 +00:00
switch ( source . format ( ) ) {
2021-03-16 10:48:42 +00:00
case BitmapFormat : : BGRx8888 :
do_draw_scaled_bitmap < true > ( * m_target , dst_rect , clipped_rect , source , src_rect , get_pixel < BitmapFormat : : BGRx8888 > , opacity ) ;
2019-06-05 16:22:11 +00:00
break ;
2021-03-16 10:48:42 +00:00
case BitmapFormat : : BGRA8888 :
do_draw_scaled_bitmap < true > ( * m_target , dst_rect , clipped_rect , source , src_rect , get_pixel < BitmapFormat : : BGRA8888 > , opacity ) ;
2019-06-05 16:22:11 +00:00
break ;
2020-02-14 22:02:47 +00:00
case BitmapFormat : : Indexed8 :
2021-01-22 14:59:29 +00:00
do_draw_scaled_bitmap < true > ( * m_target , dst_rect , clipped_rect , source , src_rect , get_pixel < BitmapFormat : : Indexed8 > , opacity ) ;
2019-06-05 16:22:11 +00:00
break ;
2020-06-17 19:37:16 +00:00
case BitmapFormat : : Indexed4 :
2021-01-22 14:59:29 +00:00
do_draw_scaled_bitmap < true > ( * m_target , dst_rect , clipped_rect , source , src_rect , get_pixel < BitmapFormat : : Indexed4 > , opacity ) ;
2020-06-17 19:37:16 +00:00
break ;
case BitmapFormat : : Indexed2 :
2021-01-22 14:59:29 +00:00
do_draw_scaled_bitmap < true > ( * m_target , dst_rect , clipped_rect , source , src_rect , get_pixel < BitmapFormat : : Indexed2 > , opacity ) ;
2020-06-17 19:37:16 +00:00
break ;
case BitmapFormat : : Indexed1 :
2021-01-22 14:59:29 +00:00
do_draw_scaled_bitmap < true > ( * m_target , dst_rect , clipped_rect , source , src_rect , get_pixel < BitmapFormat : : Indexed1 > , opacity ) ;
2020-06-17 19:37:16 +00:00
break ;
2019-06-05 16:22:11 +00:00
default :
2021-01-22 14:59:29 +00:00
do_draw_scaled_bitmap < true > ( * m_target , dst_rect , clipped_rect , source , src_rect , get_pixel < BitmapFormat : : Invalid > , opacity ) ;
2019-06-05 16:22:11 +00:00
break ;
2019-05-11 01:53:28 +00:00
}
} else {
switch ( source . format ( ) ) {
2021-03-16 10:48:42 +00:00
case BitmapFormat : : BGRx8888 :
do_draw_scaled_bitmap < false > ( * m_target , dst_rect , clipped_rect , source , src_rect , get_pixel < BitmapFormat : : BGRx8888 > , opacity ) ;
2019-06-05 16:22:11 +00:00
break ;
2020-02-14 22:02:47 +00:00
case BitmapFormat : : Indexed8 :
2021-01-22 14:59:29 +00:00
do_draw_scaled_bitmap < false > ( * m_target , dst_rect , clipped_rect , source , src_rect , get_pixel < BitmapFormat : : Indexed8 > , opacity ) ;
2019-06-05 16:22:11 +00:00
break ;
default :
2021-01-22 14:59:29 +00:00
do_draw_scaled_bitmap < false > ( * m_target , dst_rect , clipped_rect , source , src_rect , get_pixel < BitmapFormat : : Invalid > , opacity ) ;
2019-06-05 16:22:11 +00:00
break ;
2019-03-22 03:20:10 +00:00
}
}
}
2020-08-05 20:31:20 +00:00
FLATTEN void Painter : : draw_glyph ( const IntPoint & point , u32 code_point , Color color )
2019-01-15 03:44:47 +00:00
{
2020-08-05 20:31:20 +00:00
draw_glyph ( point , code_point , font ( ) , color ) ;
2019-01-15 03:44:47 +00:00
}
2020-08-05 20:31:20 +00:00
FLATTEN void Painter : : draw_glyph ( const IntPoint & point , u32 code_point , const Font & font , Color color )
2019-03-09 20:24:12 +00:00
{
2021-01-02 17:22:22 +00:00
auto glyph = font . glyph ( code_point ) ;
2021-07-19 23:43:39 +00:00
auto top_left = point + IntPoint ( glyph . left_bearing ( ) , 0 ) ;
2021-01-03 16:31:18 +00:00
2021-01-02 17:22:22 +00:00
if ( glyph . is_glyph_bitmap ( ) ) {
2021-01-03 16:31:18 +00:00
draw_bitmap ( top_left , glyph . glyph_bitmap ( ) , color ) ;
2021-01-02 17:22:22 +00:00
} else {
blit_filtered ( top_left , * glyph . bitmap ( ) , glyph . bitmap ( ) - > rect ( ) , [ color ] ( Color pixel ) - > Color {
return pixel . multiply ( color ) ;
} ) ;
}
2019-03-09 20:24:12 +00:00
}
2020-06-10 08:57:59 +00:00
void Painter : : draw_emoji ( const IntPoint & point , const Gfx : : Bitmap & emoji , const Font & font )
2019-09-04 20:47:03 +00:00
{
if ( ! font . is_fixed_width ( ) )
2019-10-19 16:36:45 +00:00
blit ( point , emoji , emoji . rect ( ) ) ;
2019-09-04 20:47:03 +00:00
else {
2020-06-10 08:57:59 +00:00
IntRect dst_rect {
2019-09-04 20:47:03 +00:00
point . x ( ) ,
point . y ( ) ,
font . glyph_width ( ' x ' ) ,
font . glyph_height ( )
} ;
2019-10-19 16:36:45 +00:00
draw_scaled_bitmap ( dst_rect , emoji , emoji . rect ( ) ) ;
2019-09-04 20:47:03 +00:00
}
}
2020-08-05 20:31:20 +00:00
void Painter : : draw_glyph_or_emoji ( const IntPoint & point , u32 code_point , const Font & font , Color color )
2019-09-04 20:47:03 +00:00
{
2021-01-03 18:24:59 +00:00
if ( font . contains_glyph ( code_point ) ) {
draw_glyph ( point , code_point , font , color ) ;
2019-09-04 20:47:03 +00:00
return ;
}
// Perhaps it's an emoji?
2020-08-05 20:31:20 +00:00
auto * emoji = Emoji : : emoji_for_code_point ( code_point ) ;
2019-09-04 20:47:03 +00:00
if ( emoji = = nullptr ) {
2021-02-07 12:03:24 +00:00
dbgln_if ( EMOJI_DEBUG , " Failed to find an emoji for code_point {} " , code_point ) ;
2019-09-04 20:47:03 +00:00
draw_glyph ( point , ' ? ' , font , color ) ;
return ;
}
draw_emoji ( point , * emoji , font ) ;
}
2021-07-25 21:20:11 +00:00
template < typename DrawGlyphFunction >
void draw_text_line ( IntRect const & a_rect , Utf8View const & text , Font const & font , TextAlignment alignment , TextDirection direction , DrawGlyphFunction draw_glyph )
2018-10-10 18:06:58 +00:00
{
2019-07-12 17:43:29 +00:00
auto rect = a_rect ;
2019-04-04 13:19:04 +00:00
2019-09-06 17:23:36 +00:00
switch ( alignment ) {
case TextAlignment : : TopLeft :
case TextAlignment : : CenterLeft :
2021-05-21 00:03:02 +00:00
case TextAlignment : : BottomLeft :
2019-09-06 17:23:36 +00:00
break ;
case TextAlignment : : TopRight :
case TextAlignment : : CenterRight :
2020-08-21 14:52:39 +00:00
case TextAlignment : : BottomRight :
2021-07-25 21:20:11 +00:00
rect . set_x ( rect . right ( ) - font . width ( text ) ) ;
2019-09-06 17:23:36 +00:00
break ;
case TextAlignment : : Center : {
2019-07-12 17:43:29 +00:00
auto shrunken_rect = rect ;
2021-07-25 21:20:11 +00:00
shrunken_rect . set_width ( font . width ( text ) ) ;
2019-07-12 17:43:29 +00:00
shrunken_rect . center_within ( rect ) ;
rect = shrunken_rect ;
2019-09-06 17:23:36 +00:00
break ;
}
default :
2021-02-23 19:42:32 +00:00
VERIFY_NOT_REACHED ( ) ;
2018-10-10 23:48:09 +00:00
}
2018-10-10 18:06:58 +00:00
2020-09-19 16:32:10 +00:00
if ( is_vertically_centered_text_alignment ( alignment ) ) {
int distance_from_baseline_to_bottom = ( font . glyph_height ( ) - 1 ) - font . baseline ( ) ;
2021-04-12 18:47:09 +00:00
rect . translate_by ( 0 , distance_from_baseline_to_bottom / 2 ) ;
2020-09-19 16:32:10 +00:00
}
2019-07-12 17:43:29 +00:00
auto point = rect . location ( ) ;
2019-03-09 20:24:12 +00:00
int space_width = font . glyph_width ( ' ' ) + font . glyph_spacing ( ) ;
2019-07-12 17:43:29 +00:00
2021-04-24 13:20:51 +00:00
if ( direction = = TextDirection : : RTL ) {
2021-04-12 18:47:09 +00:00
point . translate_by ( rect . width ( ) , 0 ) ; // Start drawing from the end
space_width = - space_width ; // Draw spaces backwards
2021-04-24 13:20:51 +00:00
}
2021-07-25 21:20:11 +00:00
for ( u32 code_point : text ) {
2020-08-05 20:31:20 +00:00
if ( code_point = = ' ' ) {
2021-04-12 18:47:09 +00:00
point . translate_by ( space_width , 0 ) ;
2018-10-12 10:29:58 +00:00
continue ;
2019-03-06 10:03:10 +00:00
}
2020-10-20 15:51:04 +00:00
IntSize glyph_size ( font . glyph_or_emoji_width ( code_point ) + font . glyph_spacing ( ) , font . glyph_height ( ) ) ;
2021-04-24 13:20:51 +00:00
if ( direction = = TextDirection : : RTL )
2021-04-12 18:47:09 +00:00
point . translate_by ( - glyph_size . width ( ) , 0 ) ; // If we are drawing right to left, we have to move backwards before drawing the glyph
2020-10-20 15:51:04 +00:00
draw_glyph ( { point , glyph_size } , code_point ) ;
2021-04-24 13:20:51 +00:00
if ( direction = = TextDirection : : LTR )
2021-04-12 18:47:09 +00:00
point . translate_by ( glyph_size . width ( ) , 0 ) ;
2018-10-10 18:06:58 +00:00
}
}
2020-10-20 15:51:04 +00:00
static inline size_t draw_text_get_length ( const Utf8View & text )
2019-07-12 17:43:29 +00:00
{
2020-10-20 15:51:04 +00:00
return text . byte_length ( ) ;
2019-07-12 17:43:29 +00:00
}
2021-07-25 21:20:11 +00:00
Vector < DirectionalRun > Painter : : split_text_into_directional_runs ( Utf8View const & text , TextDirection initial_direction )
2021-04-24 13:20:51 +00:00
{
// FIXME: This is a *very* simplified version of the UNICODE BIDIRECTIONAL ALGORITHM (https://www.unicode.org/reports/tr9/), that can render most bidirectional text
// but also produces awkward results in a large amount of edge cases. This should probably be replaced with a fully spec compliant implementation at some point.
// FIXME: Support HTML "dir" attribute (how?)
u8 paragraph_embedding_level = initial_direction = = TextDirection : : LTR ? 0 : 1 ;
Vector < u8 > embedding_levels ;
embedding_levels . ensure_capacity ( text . length ( ) ) ;
for ( size_t i = 0 ; i < text . length ( ) ; i + + )
embedding_levels . unchecked_append ( paragraph_embedding_level ) ;
// FIXME: Support Explicit Directional Formatting Characters
Vector < BidirectionalClass > character_classes ;
character_classes . ensure_capacity ( text . length ( ) ) ;
for ( u32 code_point : text )
character_classes . unchecked_append ( get_char_bidi_class ( code_point ) ) ;
// resolving weak types
BidirectionalClass paragraph_class = initial_direction = = TextDirection : : LTR ? BidirectionalClass : : STRONG_LTR : BidirectionalClass : : STRONG_RTL ;
for ( size_t i = 0 ; i < character_classes . size ( ) ; i + + ) {
if ( character_classes [ i ] ! = BidirectionalClass : : WEAK_SEPARATORS )
continue ;
for ( ssize_t j = i - 1 ; j > = 0 ; j - - ) {
auto character_class = character_classes [ j ] ;
if ( character_class ! = BidirectionalClass : : STRONG_RTL & & character_class ! = BidirectionalClass : : STRONG_LTR )
continue ;
character_classes [ i ] = character_class ;
break ;
}
if ( character_classes [ i ] = = BidirectionalClass : : WEAK_SEPARATORS )
character_classes [ i ] = paragraph_class ;
}
// resolving neutral types
auto left_side = BidirectionalClass : : NEUTRAL ;
auto sequence_length = 0 ;
for ( size_t i = 0 ; i < character_classes . size ( ) ; i + + ) {
auto character_class = character_classes [ i ] ;
if ( left_side = = BidirectionalClass : : NEUTRAL ) {
if ( character_class ! = BidirectionalClass : : NEUTRAL )
left_side = character_class ;
else
character_classes [ i ] = paragraph_class ;
continue ;
}
if ( character_class ! = BidirectionalClass : : NEUTRAL ) {
BidirectionalClass sequence_class ;
if ( bidi_class_to_direction ( left_side ) = = bidi_class_to_direction ( character_class ) ) {
sequence_class = left_side = = BidirectionalClass : : STRONG_RTL ? BidirectionalClass : : STRONG_RTL : BidirectionalClass : : STRONG_LTR ;
} else {
sequence_class = paragraph_class ;
}
for ( auto j = 0 ; j < sequence_length ; j + + ) {
character_classes [ i - j - 1 ] = sequence_class ;
}
sequence_length = 0 ;
left_side = character_class ;
} else {
sequence_length + + ;
}
}
for ( auto i = 0 ; i < sequence_length ; i + + )
character_classes [ character_classes . size ( ) - i - 1 ] = paragraph_class ;
// resolving implicit levels
for ( size_t i = 0 ; i < character_classes . size ( ) ; i + + ) {
auto character_class = character_classes [ i ] ;
if ( ( embedding_levels [ i ] % 2 ) = = 0 ) {
if ( character_class = = BidirectionalClass : : STRONG_RTL )
embedding_levels [ i ] + = 1 ;
else if ( character_class = = BidirectionalClass : : WEAK_NUMBERS | | character_class = = BidirectionalClass : : WEAK_SEPARATORS )
embedding_levels [ i ] + = 2 ;
} else {
if ( character_class = = BidirectionalClass : : STRONG_LTR | | character_class = = BidirectionalClass : : WEAK_NUMBERS | | character_class = = BidirectionalClass : : WEAK_SEPARATORS )
embedding_levels [ i ] + = 1 ;
}
}
// splitting into runs
auto run_code_points_start = text . begin ( ) ;
auto next_code_points_slice = [ & ] ( auto length ) {
Vector < u32 > run_code_points ;
run_code_points . ensure_capacity ( length ) ;
for ( size_t j = 0 ; j < length ; + + j , + + run_code_points_start )
run_code_points . unchecked_append ( * run_code_points_start ) ;
return run_code_points ;
} ;
Vector < DirectionalRun > runs ;
size_t start = 0 ;
u8 level = embedding_levels [ 0 ] ;
for ( size_t i = 1 ; i < embedding_levels . size ( ) ; + + i ) {
if ( embedding_levels [ i ] = = level )
continue ;
auto code_points_slice = next_code_points_slice ( i - start ) ;
runs . append ( { move ( code_points_slice ) , level } ) ;
start = i ;
level = embedding_levels [ i ] ;
}
auto code_points_slice = next_code_points_slice ( embedding_levels . size ( ) - start ) ;
runs . append ( { move ( code_points_slice ) , level } ) ;
// reordering resolved levels
// FIXME: missing special cases for trailing whitespace characters
u8 minimum_level = 128 ;
u8 maximum_level = 0 ;
for ( auto & run : runs ) {
minimum_level = min ( minimum_level , run . embedding_level ( ) ) ;
maximum_level = max ( minimum_level , run . embedding_level ( ) ) ;
}
if ( ( minimum_level % 2 ) = = 0 )
minimum_level + + ;
auto runs_count = runs . size ( ) - 1 ;
while ( maximum_level < = minimum_level ) {
size_t run_index = 0 ;
while ( run_index < runs_count ) {
while ( run_index < runs_count & & runs [ run_index ] . embedding_level ( ) < maximum_level )
run_index + + ;
auto reverse_start = run_index ;
while ( run_index < = runs_count & & runs [ run_index ] . embedding_level ( ) > = maximum_level )
run_index + + ;
auto reverse_end = run_index - 1 ;
while ( reverse_start < reverse_end ) {
swap ( runs [ reverse_start ] , runs [ reverse_end ] ) ;
reverse_start + + ;
reverse_end - - ;
}
}
maximum_level - - ;
}
// mirroring RTL mirror characters
for ( auto & run : runs ) {
if ( run . direction ( ) = = TextDirection : : LTR )
continue ;
for ( auto & code_point : run . code_points ( ) ) {
code_point = get_mirror_char ( code_point ) ;
}
}
return runs ;
}
2021-07-25 21:20:11 +00:00
bool Painter : : text_contains_bidirectional_text ( Utf8View const & text , TextDirection initial_direction )
2021-04-24 13:20:51 +00:00
{
for ( u32 code_point : text ) {
auto char_class = get_char_bidi_class ( code_point ) ;
if ( char_class = = BidirectionalClass : : NEUTRAL )
continue ;
if ( bidi_class_to_direction ( char_class ) ! = initial_direction )
return true ;
}
return false ;
}
2021-07-25 21:20:11 +00:00
template < typename DrawGlyphFunction >
void Painter : : do_draw_text ( IntRect const & rect , Utf8View const & text , Font const & font , TextAlignment alignment , TextElision elision , TextWrapping wrapping , DrawGlyphFunction draw_glyph )
2019-07-12 17:43:29 +00:00
{
2021-04-24 13:20:51 +00:00
if ( draw_text_get_length ( text ) = = 0 )
return ;
2021-07-25 21:20:11 +00:00
TextLayout layout ( & font , text , rect ) ;
2019-07-12 17:43:29 +00:00
2021-05-21 09:30:21 +00:00
static const int line_spacing = 4 ;
2019-07-12 17:43:29 +00:00
int line_height = font . glyph_height ( ) + line_spacing ;
2020-05-17 18:32:23 +00:00
2021-07-25 21:20:11 +00:00
auto lines = layout . lines ( elision , wrapping , line_spacing ) ;
auto bounding_rect = layout . bounding_rect ( wrapping , line_spacing ) ;
2020-05-17 18:32:23 +00:00
switch ( alignment ) {
case TextAlignment : : TopLeft :
bounding_rect . set_location ( rect . location ( ) ) ;
break ;
case TextAlignment : : TopRight :
bounding_rect . set_location ( { ( rect . right ( ) + 1 ) - bounding_rect . width ( ) , rect . y ( ) } ) ;
break ;
case TextAlignment : : CenterLeft :
bounding_rect . set_location ( { rect . x ( ) , rect . center ( ) . y ( ) - ( bounding_rect . height ( ) / 2 ) } ) ;
break ;
case TextAlignment : : CenterRight :
bounding_rect . set_location ( { ( rect . right ( ) + 1 ) - bounding_rect . width ( ) , rect . center ( ) . y ( ) - ( bounding_rect . height ( ) / 2 ) } ) ;
break ;
case TextAlignment : : Center :
bounding_rect . center_within ( rect ) ;
break ;
2021-05-21 00:03:02 +00:00
case TextAlignment : : BottomLeft :
bounding_rect . set_location ( { rect . x ( ) , ( rect . bottom ( ) + 1 ) - bounding_rect . height ( ) } ) ;
break ;
2020-10-25 10:50:38 +00:00
case TextAlignment : : BottomRight :
bounding_rect . set_location ( { ( rect . right ( ) + 1 ) - bounding_rect . width ( ) , ( rect . bottom ( ) + 1 ) - bounding_rect . height ( ) } ) ;
break ;
2020-05-17 18:32:23 +00:00
default :
2021-02-23 19:42:32 +00:00
VERIFY_NOT_REACHED ( ) ;
2020-05-17 18:32:23 +00:00
}
2021-08-31 19:18:53 +00:00
bounding_rect . intersect ( rect ) ;
2020-05-17 18:32:23 +00:00
for ( size_t i = 0 ; i < lines . size ( ) ; + + i ) {
2021-09-04 11:06:25 +00:00
auto line = Utf8View { lines [ i ] } ;
2021-04-24 13:20:51 +00:00
2020-06-10 08:57:59 +00:00
IntRect line_rect { bounding_rect . x ( ) , bounding_rect . y ( ) + static_cast < int > ( i ) * line_height , bounding_rect . width ( ) , line_height } ;
2020-05-17 18:32:23 +00:00
line_rect . intersect ( rect ) ;
2021-04-24 13:20:51 +00:00
TextDirection line_direction = get_text_direction ( line ) ;
2021-09-04 11:06:25 +00:00
if ( text_contains_bidirectional_text ( line , line_direction ) ) { // Slow Path: The line contains mixed BiDi classes
auto directional_runs = split_text_into_directional_runs ( line , line_direction ) ;
2021-04-24 13:20:51 +00:00
auto current_dx = line_direction = = TextDirection : : LTR ? 0 : line_rect . width ( ) ;
for ( auto & directional_run : directional_runs ) {
auto run_width = font . width ( directional_run . text ( ) ) ;
if ( line_direction = = TextDirection : : RTL )
current_dx - = run_width ;
auto run_rect = line_rect . translated ( current_dx , 0 ) ;
run_rect . set_width ( run_width ) ;
2021-07-25 21:20:11 +00:00
// NOTE: DirectionalRun returns Utf32View which isn't
// compatible with draw_text_line.
StringBuilder builder ;
builder . append ( directional_run . text ( ) ) ;
2021-09-04 11:26:28 +00:00
auto line_text = Utf8View { builder . string_view ( ) } ;
2021-07-25 21:20:11 +00:00
2021-09-04 11:06:25 +00:00
draw_text_line ( run_rect , line_text , font , alignment , directional_run . direction ( ) , draw_glyph ) ;
2021-04-24 13:20:51 +00:00
if ( line_direction = = TextDirection : : LTR )
current_dx + = run_width ;
}
} else {
2021-09-04 11:06:25 +00:00
draw_text_line ( line_rect , line , font , alignment , line_direction , draw_glyph ) ;
2021-04-24 13:20:51 +00:00
}
2020-05-17 18:32:23 +00:00
}
}
2021-07-25 21:20:11 +00:00
void Painter : : draw_text ( const IntRect & rect , const StringView & text , TextAlignment alignment , Color color , TextElision elision , TextWrapping wrapping )
2020-05-17 18:32:23 +00:00
{
2021-07-25 21:20:11 +00:00
draw_text ( rect , text , font ( ) , alignment , color , elision , wrapping ) ;
2020-10-20 15:51:04 +00:00
}
2020-05-17 18:32:23 +00:00
2021-07-25 21:20:11 +00:00
void Painter : : draw_text ( const IntRect & rect , const Utf32View & text , TextAlignment alignment , Color color , TextElision elision , TextWrapping wrapping )
2020-10-20 15:51:04 +00:00
{
2021-07-25 21:20:11 +00:00
draw_text ( rect , text , font ( ) , alignment , color , elision , wrapping ) ;
2020-10-20 15:51:04 +00:00
}
2020-05-17 18:32:23 +00:00
2021-07-25 21:20:11 +00:00
void Painter : : draw_text ( const IntRect & rect , const StringView & raw_text , const Font & font , TextAlignment alignment , Color color , TextElision elision , TextWrapping wrapping )
2020-10-20 15:51:04 +00:00
{
Utf8View text { raw_text } ;
2021-07-25 21:20:11 +00:00
do_draw_text ( rect , text , font , alignment , elision , wrapping , [ & ] ( const IntRect & r , u32 code_point ) {
2020-10-20 15:51:04 +00:00
draw_glyph_or_emoji ( r . location ( ) , code_point , font , color ) ;
} ) ;
}
2020-05-17 18:32:23 +00:00
2021-07-25 21:20:11 +00:00
void Painter : : draw_text ( const IntRect & rect , const Utf32View & raw_text , const Font & font , TextAlignment alignment , Color color , TextElision elision , TextWrapping wrapping )
2020-10-20 15:51:04 +00:00
{
2021-07-25 21:20:11 +00:00
// FIXME: UTF-32 should eventually be completely removed, but for the time
// being some places might depend on it, so we do some internal conversion.
StringBuilder builder ;
builder . append ( raw_text ) ;
auto text = Utf8View { builder . string_view ( ) } ;
do_draw_text ( rect , text , font , alignment , elision , wrapping , [ & ] ( const IntRect & r , u32 code_point ) {
2020-10-20 15:51:04 +00:00
draw_glyph_or_emoji ( r . location ( ) , code_point , font , color ) ;
} ) ;
}
2019-07-12 17:43:29 +00:00
2021-07-25 21:20:11 +00:00
void Painter : : draw_text ( Function < void ( const IntRect & , u32 ) > draw_one_glyph , const IntRect & rect , const Utf8View & text , const Font & font , TextAlignment alignment , TextElision elision , TextWrapping wrapping )
2020-10-20 15:51:04 +00:00
{
2021-02-23 19:42:32 +00:00
VERIFY ( scale ( ) = = 1 ) ; // FIXME: Add scaling support.
LibGfx: Make it possible to apply an (integer) scale to a Painter
This adds a scale factor to Painter, which will be used for HighDPI
support. It's also a step towards general affine transforms on Painters.
All of Painter's public API takes logical coordinates, while some
internals deal with physical coordinates now. If scale == 1, logical
and physical coordinates are the same. For scale == 2, a 200x100 bitmap
would be covered by a logical {0, 0, 100, 50} rect, while its physical
size would be {0, 0, 200, 100}.
Most of Painter's functions just assert that scale() == 1 is for now,
but most functions called by WindowServer are updated to handle
arbitrary (integer) scale.
Also add a new Demo "LibGfxScaleDemo" that covers the converted
functions and that can be used to iteratively add scaling support
to more functions.
To make Painter's interface deal with logical coordinates only,
make translation() and clip_rect() non-public.
2021-01-12 20:53:07 +00:00
2021-07-25 21:20:11 +00:00
do_draw_text ( rect , text , font , alignment , elision , wrapping , [ & ] ( const IntRect & r , u32 code_point ) {
2020-10-20 15:51:04 +00:00
draw_one_glyph ( r , code_point ) ;
} ) ;
}
2019-07-12 17:43:29 +00:00
2021-07-25 21:20:11 +00:00
void Painter : : draw_text ( Function < void ( const IntRect & , u32 ) > draw_one_glyph , const IntRect & rect , const StringView & raw_text , const Font & font , TextAlignment alignment , TextElision elision , TextWrapping wrapping )
2020-10-20 15:51:04 +00:00
{
2021-02-23 19:42:32 +00:00
VERIFY ( scale ( ) = = 1 ) ; // FIXME: Add scaling support.
LibGfx: Make it possible to apply an (integer) scale to a Painter
This adds a scale factor to Painter, which will be used for HighDPI
support. It's also a step towards general affine transforms on Painters.
All of Painter's public API takes logical coordinates, while some
internals deal with physical coordinates now. If scale == 1, logical
and physical coordinates are the same. For scale == 2, a 200x100 bitmap
would be covered by a logical {0, 0, 100, 50} rect, while its physical
size would be {0, 0, 200, 100}.
Most of Painter's functions just assert that scale() == 1 is for now,
but most functions called by WindowServer are updated to handle
arbitrary (integer) scale.
Also add a new Demo "LibGfxScaleDemo" that covers the converted
functions and that can be used to iteratively add scaling support
to more functions.
To make Painter's interface deal with logical coordinates only,
make translation() and clip_rect() non-public.
2021-01-12 20:53:07 +00:00
2021-07-25 21:20:11 +00:00
Utf8View text { raw_text } ;
do_draw_text ( rect , text , font , alignment , elision , wrapping , [ & ] ( const IntRect & r , u32 code_point ) {
2020-10-20 15:51:04 +00:00
draw_one_glyph ( r , code_point ) ;
} ) ;
}
2019-07-12 17:43:29 +00:00
2021-07-25 21:20:11 +00:00
void Painter : : draw_text ( Function < void ( const IntRect & , u32 ) > draw_one_glyph , const IntRect & rect , const Utf32View & raw_text , const Font & font , TextAlignment alignment , TextElision elision , TextWrapping wrapping )
2020-10-20 15:51:04 +00:00
{
2021-02-23 19:42:32 +00:00
VERIFY ( scale ( ) = = 1 ) ; // FIXME: Add scaling support.
LibGfx: Make it possible to apply an (integer) scale to a Painter
This adds a scale factor to Painter, which will be used for HighDPI
support. It's also a step towards general affine transforms on Painters.
All of Painter's public API takes logical coordinates, while some
internals deal with physical coordinates now. If scale == 1, logical
and physical coordinates are the same. For scale == 2, a 200x100 bitmap
would be covered by a logical {0, 0, 100, 50} rect, while its physical
size would be {0, 0, 200, 100}.
Most of Painter's functions just assert that scale() == 1 is for now,
but most functions called by WindowServer are updated to handle
arbitrary (integer) scale.
Also add a new Demo "LibGfxScaleDemo" that covers the converted
functions and that can be used to iteratively add scaling support
to more functions.
To make Painter's interface deal with logical coordinates only,
make translation() and clip_rect() non-public.
2021-01-12 20:53:07 +00:00
2021-07-25 21:20:11 +00:00
// FIXME: UTF-32 should eventually be completely removed, but for the time
// being some places might depend on it, so we do some internal conversion.
StringBuilder builder ;
builder . append ( raw_text ) ;
auto text = Utf8View { builder . string_view ( ) } ;
do_draw_text ( rect , text , font , alignment , elision , wrapping , [ & ] ( const IntRect & r , u32 code_point ) {
2020-10-20 15:51:04 +00:00
draw_one_glyph ( r , code_point ) ;
} ) ;
2019-07-12 17:43:29 +00:00
}
2020-06-10 08:57:59 +00:00
void Painter : : set_pixel ( const IntPoint & p , Color color )
2018-10-12 12:58:16 +00:00
{
2021-05-03 14:37:05 +00:00
VERIFY ( scale ( ) = = 1 ) ; // FIXME: Add scaling support.
auto point = p ;
point . translate_by ( state ( ) . translation ) ;
2019-03-09 15:48:02 +00:00
if ( ! clip_rect ( ) . contains ( point ) )
2018-10-12 20:50:28 +00:00
return ;
2019-01-09 01:06:04 +00:00
m_target - > scanline ( point . y ( ) ) [ point . x ( ) ] = color . value ( ) ;
2018-10-12 12:58:16 +00:00
}
2021-01-13 01:22:15 +00:00
ALWAYS_INLINE void Painter : : set_physical_pixel_with_draw_op ( u32 & pixel , const Color & color )
2019-01-11 02:52:09 +00:00
{
2021-01-13 01:22:15 +00:00
// This always sets a single physical pixel, independent of scale().
// This should only be called by routines that already handle scale.
LibGfx: Make it possible to apply an (integer) scale to a Painter
This adds a scale factor to Painter, which will be used for HighDPI
support. It's also a step towards general affine transforms on Painters.
All of Painter's public API takes logical coordinates, while some
internals deal with physical coordinates now. If scale == 1, logical
and physical coordinates are the same. For scale == 2, a 200x100 bitmap
would be covered by a logical {0, 0, 100, 50} rect, while its physical
size would be {0, 0, 200, 100}.
Most of Painter's functions just assert that scale() == 1 is for now,
but most functions called by WindowServer are updated to handle
arbitrary (integer) scale.
Also add a new Demo "LibGfxScaleDemo" that covers the converted
functions and that can be used to iteratively add scaling support
to more functions.
To make Painter's interface deal with logical coordinates only,
make translation() and clip_rect() non-public.
2021-01-12 20:53:07 +00:00
2020-09-08 04:25:30 +00:00
switch ( draw_op ( ) ) {
case DrawOp : : Copy :
2019-01-11 02:52:09 +00:00
pixel = color . value ( ) ;
2020-09-08 04:25:30 +00:00
break ;
case DrawOp : : Xor :
pixel = color . xored ( Color : : from_rgba ( pixel ) ) . value ( ) ;
break ;
case DrawOp : : Invert :
pixel = Color : : from_rgba ( pixel ) . inverted ( ) . value ( ) ;
break ;
}
}
2021-01-13 01:22:15 +00:00
ALWAYS_INLINE void Painter : : fill_physical_scanline_with_draw_op ( int y , int x , int width , const Color & color )
2020-09-08 04:25:30 +00:00
{
2021-01-13 01:22:15 +00:00
// This always draws a single physical scanline, independent of scale().
// This should only be called by routines that already handle scale.
LibGfx: Make it possible to apply an (integer) scale to a Painter
This adds a scale factor to Painter, which will be used for HighDPI
support. It's also a step towards general affine transforms on Painters.
All of Painter's public API takes logical coordinates, while some
internals deal with physical coordinates now. If scale == 1, logical
and physical coordinates are the same. For scale == 2, a 200x100 bitmap
would be covered by a logical {0, 0, 100, 50} rect, while its physical
size would be {0, 0, 200, 100}.
Most of Painter's functions just assert that scale() == 1 is for now,
but most functions called by WindowServer are updated to handle
arbitrary (integer) scale.
Also add a new Demo "LibGfxScaleDemo" that covers the converted
functions and that can be used to iteratively add scaling support
to more functions.
To make Painter's interface deal with logical coordinates only,
make translation() and clip_rect() non-public.
2021-01-12 20:53:07 +00:00
2020-09-08 04:25:30 +00:00
switch ( draw_op ( ) ) {
case DrawOp : : Copy :
fast_u32_fill ( m_target - > scanline ( y ) + x , color . value ( ) , width ) ;
break ;
case DrawOp : : Xor : {
auto * pixel = m_target - > scanline ( y ) + x ;
auto * end = pixel + width ;
while ( pixel < end ) {
* pixel = Color : : from_rgba ( * pixel ) . xored ( color ) . value ( ) ;
pixel + + ;
}
break ;
}
case DrawOp : : Invert : {
auto * pixel = m_target - > scanline ( y ) + x ;
auto * end = pixel + width ;
while ( pixel < end ) {
* pixel = Color : : from_rgba ( * pixel ) . inverted ( ) . value ( ) ;
pixel + + ;
}
break ;
}
}
2019-01-11 02:52:09 +00:00
}
2021-05-03 14:37:05 +00:00
void Painter : : draw_physical_pixel ( const IntPoint & physical_position , Color color , int thickness )
2019-06-23 08:00:02 +00:00
{
LibGfx: Make it possible to apply an (integer) scale to a Painter
This adds a scale factor to Painter, which will be used for HighDPI
support. It's also a step towards general affine transforms on Painters.
All of Painter's public API takes logical coordinates, while some
internals deal with physical coordinates now. If scale == 1, logical
and physical coordinates are the same. For scale == 2, a 200x100 bitmap
would be covered by a logical {0, 0, 100, 50} rect, while its physical
size would be {0, 0, 200, 100}.
Most of Painter's functions just assert that scale() == 1 is for now,
but most functions called by WindowServer are updated to handle
arbitrary (integer) scale.
Also add a new Demo "LibGfxScaleDemo" that covers the converted
functions and that can be used to iteratively add scaling support
to more functions.
To make Painter's interface deal with logical coordinates only,
make translation() and clip_rect() non-public.
2021-01-12 20:53:07 +00:00
// This always draws a single physical pixel, independent of scale().
// This should only be called by routines that already handle scale
// (including scaling thickness).
2021-02-23 19:42:32 +00:00
VERIFY ( draw_op ( ) = = DrawOp : : Copy ) ;
LibGfx: Make it possible to apply an (integer) scale to a Painter
This adds a scale factor to Painter, which will be used for HighDPI
support. It's also a step towards general affine transforms on Painters.
All of Painter's public API takes logical coordinates, while some
internals deal with physical coordinates now. If scale == 1, logical
and physical coordinates are the same. For scale == 2, a 200x100 bitmap
would be covered by a logical {0, 0, 100, 50} rect, while its physical
size would be {0, 0, 200, 100}.
Most of Painter's functions just assert that scale() == 1 is for now,
but most functions called by WindowServer are updated to handle
arbitrary (integer) scale.
Also add a new Demo "LibGfxScaleDemo" that covers the converted
functions and that can be used to iteratively add scaling support
to more functions.
To make Painter's interface deal with logical coordinates only,
make translation() and clip_rect() non-public.
2021-01-12 20:53:07 +00:00
2021-05-03 14:37:05 +00:00
if ( thickness = = 1 ) { // Implies scale() == 1.
2021-01-19 17:10:47 +00:00
auto & pixel = m_target - > scanline ( physical_position . y ( ) ) [ physical_position . x ( ) ] ;
2021-01-13 01:22:15 +00:00
return set_physical_pixel_with_draw_op ( pixel , Color : : from_rgba ( pixel ) . blend ( color ) ) ;
2021-01-12 20:00:04 +00:00
}
LibGfx: Make it possible to apply an (integer) scale to a Painter
This adds a scale factor to Painter, which will be used for HighDPI
support. It's also a step towards general affine transforms on Painters.
All of Painter's public API takes logical coordinates, while some
internals deal with physical coordinates now. If scale == 1, logical
and physical coordinates are the same. For scale == 2, a 200x100 bitmap
would be covered by a logical {0, 0, 100, 50} rect, while its physical
size would be {0, 0, 200, 100}.
Most of Painter's functions just assert that scale() == 1 is for now,
but most functions called by WindowServer are updated to handle
arbitrary (integer) scale.
Also add a new Demo "LibGfxScaleDemo" that covers the converted
functions and that can be used to iteratively add scaling support
to more functions.
To make Painter's interface deal with logical coordinates only,
make translation() and clip_rect() non-public.
2021-01-12 20:53:07 +00:00
2021-05-03 14:37:05 +00:00
IntRect rect { physical_position , { thickness , thickness } } ;
rect . intersect ( clip_rect ( ) * scale ( ) ) ;
LibGfx: Make it possible to apply an (integer) scale to a Painter
This adds a scale factor to Painter, which will be used for HighDPI
support. It's also a step towards general affine transforms on Painters.
All of Painter's public API takes logical coordinates, while some
internals deal with physical coordinates now. If scale == 1, logical
and physical coordinates are the same. For scale == 2, a 200x100 bitmap
would be covered by a logical {0, 0, 100, 50} rect, while its physical
size would be {0, 0, 200, 100}.
Most of Painter's functions just assert that scale() == 1 is for now,
but most functions called by WindowServer are updated to handle
arbitrary (integer) scale.
Also add a new Demo "LibGfxScaleDemo" that covers the converted
functions and that can be used to iteratively add scaling support
to more functions.
To make Painter's interface deal with logical coordinates only,
make translation() and clip_rect() non-public.
2021-01-12 20:53:07 +00:00
fill_physical_rect ( rect , color ) ;
2019-06-23 08:00:02 +00:00
}
2021-08-05 14:34:03 +00:00
void Painter : : draw_line ( IntPoint const & a_p1 , IntPoint const & a_p2 , Color color , int thickness , LineStyle style , Color alternate_color )
2018-10-12 12:58:16 +00:00
{
2020-06-13 18:38:36 +00:00
if ( color . alpha ( ) = = 0 )
return ;
2021-05-03 14:37:05 +00:00
auto clip_rect = this - > clip_rect ( ) * scale ( ) ;
2019-05-07 15:01:55 +00:00
2021-07-09 16:18:46 +00:00
auto const p1 = thickness > 1 ? a_p1 . translated ( - ( thickness / 2 ) , - ( thickness / 2 ) ) : a_p1 ;
auto const p2 = thickness > 1 ? a_p2 . translated ( - ( thickness / 2 ) , - ( thickness / 2 ) ) : a_p2 ;
2021-05-03 14:36:57 +00:00
auto point1 = to_physical ( p1 ) ;
auto point2 = to_physical ( p2 ) ;
2021-05-03 14:37:05 +00:00
thickness * = scale ( ) ;
2021-05-03 14:36:57 +00:00
2021-08-05 14:34:03 +00:00
auto alternate_color_is_transparent = alternate_color = = Color : : Transparent ;
2018-10-12 12:58:16 +00:00
// Special case: vertical line.
2021-05-03 14:36:57 +00:00
if ( point1 . x ( ) = = point2 . x ( ) ) {
const int x = point1 . x ( ) ;
2019-05-07 15:01:55 +00:00
if ( x < clip_rect . left ( ) | | x > clip_rect . right ( ) )
2018-10-12 20:50:28 +00:00
return ;
2021-05-03 14:36:57 +00:00
if ( point1 . y ( ) > point2 . y ( ) )
swap ( point1 , point2 ) ;
if ( point1 . y ( ) > clip_rect . bottom ( ) )
2019-02-03 02:37:55 +00:00
return ;
2021-05-03 14:36:57 +00:00
if ( point2 . y ( ) < clip_rect . top ( ) )
2019-02-03 02:37:55 +00:00
return ;
2021-05-03 14:36:57 +00:00
int min_y = max ( point1 . y ( ) , clip_rect . top ( ) ) ;
int max_y = min ( point2 . y ( ) , clip_rect . bottom ( ) ) ;
2020-05-10 10:58:33 +00:00
if ( style = = LineStyle : : Dotted ) {
2021-05-03 14:37:05 +00:00
for ( int y = min_y ; y < = max_y ; y + = thickness * 2 )
LibGfx: Make it possible to apply an (integer) scale to a Painter
This adds a scale factor to Painter, which will be used for HighDPI
support. It's also a step towards general affine transforms on Painters.
All of Painter's public API takes logical coordinates, while some
internals deal with physical coordinates now. If scale == 1, logical
and physical coordinates are the same. For scale == 2, a 200x100 bitmap
would be covered by a logical {0, 0, 100, 50} rect, while its physical
size would be {0, 0, 200, 100}.
Most of Painter's functions just assert that scale() == 1 is for now,
but most functions called by WindowServer are updated to handle
arbitrary (integer) scale.
Also add a new Demo "LibGfxScaleDemo" that covers the converted
functions and that can be used to iteratively add scaling support
to more functions.
To make Painter's interface deal with logical coordinates only,
make translation() and clip_rect() non-public.
2021-01-12 20:53:07 +00:00
draw_physical_pixel ( { x , y } , color , thickness ) ;
2020-05-10 15:17:26 +00:00
} else if ( style = = LineStyle : : Dashed ) {
2021-05-03 14:37:05 +00:00
for ( int y = min_y ; y < = max_y ; y + = thickness * 6 ) {
LibGfx: Make it possible to apply an (integer) scale to a Painter
This adds a scale factor to Painter, which will be used for HighDPI
support. It's also a step towards general affine transforms on Painters.
All of Painter's public API takes logical coordinates, while some
internals deal with physical coordinates now. If scale == 1, logical
and physical coordinates are the same. For scale == 2, a 200x100 bitmap
would be covered by a logical {0, 0, 100, 50} rect, while its physical
size would be {0, 0, 200, 100}.
Most of Painter's functions just assert that scale() == 1 is for now,
but most functions called by WindowServer are updated to handle
arbitrary (integer) scale.
Also add a new Demo "LibGfxScaleDemo" that covers the converted
functions and that can be used to iteratively add scaling support
to more functions.
To make Painter's interface deal with logical coordinates only,
make translation() and clip_rect() non-public.
2021-01-12 20:53:07 +00:00
draw_physical_pixel ( { x , y } , color , thickness ) ;
2021-05-03 14:37:05 +00:00
draw_physical_pixel ( { x , min ( y + thickness , max_y ) } , color , thickness ) ;
draw_physical_pixel ( { x , min ( y + thickness * 2 , max_y ) } , color , thickness ) ;
2021-08-05 14:34:03 +00:00
if ( ! alternate_color_is_transparent ) {
draw_physical_pixel ( { x , min ( y + thickness * 3 , max_y ) } , alternate_color , thickness ) ;
draw_physical_pixel ( { x , min ( y + thickness * 4 , max_y ) } , alternate_color , thickness ) ;
draw_physical_pixel ( { x , min ( y + thickness * 5 , max_y ) } , alternate_color , thickness ) ;
}
2020-05-10 15:17:26 +00:00
}
2019-11-27 19:52:11 +00:00
} else {
2021-05-03 14:37:05 +00:00
for ( int y = min_y ; y < = max_y ; y + = thickness )
LibGfx: Make it possible to apply an (integer) scale to a Painter
This adds a scale factor to Painter, which will be used for HighDPI
support. It's also a step towards general affine transforms on Painters.
All of Painter's public API takes logical coordinates, while some
internals deal with physical coordinates now. If scale == 1, logical
and physical coordinates are the same. For scale == 2, a 200x100 bitmap
would be covered by a logical {0, 0, 100, 50} rect, while its physical
size would be {0, 0, 200, 100}.
Most of Painter's functions just assert that scale() == 1 is for now,
but most functions called by WindowServer are updated to handle
arbitrary (integer) scale.
Also add a new Demo "LibGfxScaleDemo" that covers the converted
functions and that can be used to iteratively add scaling support
to more functions.
To make Painter's interface deal with logical coordinates only,
make translation() and clip_rect() non-public.
2021-01-12 20:53:07 +00:00
draw_physical_pixel ( { x , y } , color , thickness ) ;
2019-11-27 19:52:11 +00:00
}
2018-10-12 12:58:16 +00:00
return ;
}
// Special case: horizontal line.
2021-05-03 14:36:57 +00:00
if ( point1 . y ( ) = = point2 . y ( ) ) {
const int y = point1 . y ( ) ;
2019-05-07 15:01:55 +00:00
if ( y < clip_rect . top ( ) | | y > clip_rect . bottom ( ) )
2018-10-12 20:50:28 +00:00
return ;
2021-05-03 14:36:57 +00:00
if ( point1 . x ( ) > point2 . x ( ) )
swap ( point1 , point2 ) ;
if ( point1 . x ( ) > clip_rect . right ( ) )
2019-02-03 02:37:55 +00:00
return ;
2021-05-03 14:36:57 +00:00
if ( point2 . x ( ) < clip_rect . left ( ) )
2019-02-03 02:37:55 +00:00
return ;
2021-05-03 14:36:57 +00:00
int min_x = max ( point1 . x ( ) , clip_rect . left ( ) ) ;
int max_x = min ( point2 . x ( ) , clip_rect . right ( ) ) ;
2020-05-10 10:58:33 +00:00
if ( style = = LineStyle : : Dotted ) {
2021-05-03 14:37:05 +00:00
for ( int x = min_x ; x < = max_x ; x + = thickness * 2 )
LibGfx: Make it possible to apply an (integer) scale to a Painter
This adds a scale factor to Painter, which will be used for HighDPI
support. It's also a step towards general affine transforms on Painters.
All of Painter's public API takes logical coordinates, while some
internals deal with physical coordinates now. If scale == 1, logical
and physical coordinates are the same. For scale == 2, a 200x100 bitmap
would be covered by a logical {0, 0, 100, 50} rect, while its physical
size would be {0, 0, 200, 100}.
Most of Painter's functions just assert that scale() == 1 is for now,
but most functions called by WindowServer are updated to handle
arbitrary (integer) scale.
Also add a new Demo "LibGfxScaleDemo" that covers the converted
functions and that can be used to iteratively add scaling support
to more functions.
To make Painter's interface deal with logical coordinates only,
make translation() and clip_rect() non-public.
2021-01-12 20:53:07 +00:00
draw_physical_pixel ( { x , y } , color , thickness ) ;
2020-05-10 15:17:26 +00:00
} else if ( style = = LineStyle : : Dashed ) {
2021-05-03 14:37:05 +00:00
for ( int x = min_x ; x < = max_x ; x + = thickness * 6 ) {
LibGfx: Make it possible to apply an (integer) scale to a Painter
This adds a scale factor to Painter, which will be used for HighDPI
support. It's also a step towards general affine transforms on Painters.
All of Painter's public API takes logical coordinates, while some
internals deal with physical coordinates now. If scale == 1, logical
and physical coordinates are the same. For scale == 2, a 200x100 bitmap
would be covered by a logical {0, 0, 100, 50} rect, while its physical
size would be {0, 0, 200, 100}.
Most of Painter's functions just assert that scale() == 1 is for now,
but most functions called by WindowServer are updated to handle
arbitrary (integer) scale.
Also add a new Demo "LibGfxScaleDemo" that covers the converted
functions and that can be used to iteratively add scaling support
to more functions.
To make Painter's interface deal with logical coordinates only,
make translation() and clip_rect() non-public.
2021-01-12 20:53:07 +00:00
draw_physical_pixel ( { x , y } , color , thickness ) ;
2021-05-03 14:37:05 +00:00
draw_physical_pixel ( { min ( x + thickness , max_x ) , y } , color , thickness ) ;
draw_physical_pixel ( { min ( x + thickness * 2 , max_x ) , y } , color , thickness ) ;
2021-08-05 14:34:03 +00:00
if ( ! alternate_color_is_transparent ) {
draw_physical_pixel ( { min ( x + thickness * 3 , max_x ) , y } , alternate_color , thickness ) ;
draw_physical_pixel ( { min ( x + thickness * 4 , max_x ) , y } , alternate_color , thickness ) ;
draw_physical_pixel ( { min ( x + thickness * 5 , max_x ) , y } , alternate_color , thickness ) ;
}
2020-05-10 15:17:26 +00:00
}
2019-11-27 19:52:11 +00:00
} else {
2021-05-03 14:37:05 +00:00
for ( int x = min_x ; x < = max_x ; x + = thickness )
LibGfx: Make it possible to apply an (integer) scale to a Painter
This adds a scale factor to Painter, which will be used for HighDPI
support. It's also a step towards general affine transforms on Painters.
All of Painter's public API takes logical coordinates, while some
internals deal with physical coordinates now. If scale == 1, logical
and physical coordinates are the same. For scale == 2, a 200x100 bitmap
would be covered by a logical {0, 0, 100, 50} rect, while its physical
size would be {0, 0, 200, 100}.
Most of Painter's functions just assert that scale() == 1 is for now,
but most functions called by WindowServer are updated to handle
arbitrary (integer) scale.
Also add a new Demo "LibGfxScaleDemo" that covers the converted
functions and that can be used to iteratively add scaling support
to more functions.
To make Painter's interface deal with logical coordinates only,
make translation() and clip_rect() non-public.
2021-01-12 20:53:07 +00:00
draw_physical_pixel ( { x , y } , color , thickness ) ;
2019-11-27 19:52:11 +00:00
}
2018-10-12 12:58:16 +00:00
return ;
}
2020-05-10 15:17:26 +00:00
// FIXME: Implement dotted/dashed diagonal lines.
2021-02-23 19:42:32 +00:00
VERIFY ( style = = LineStyle : : Solid ) ;
2019-11-27 19:52:11 +00:00
2021-05-03 14:36:57 +00:00
const int adx = abs ( point2 . x ( ) - point1 . x ( ) ) ;
const int ady = abs ( point2 . y ( ) - point1 . y ( ) ) ;
2019-05-07 15:01:55 +00:00
if ( adx > ady ) {
2021-05-03 14:36:57 +00:00
if ( point1 . x ( ) > point2 . x ( ) )
swap ( point1 , point2 ) ;
2019-05-07 15:01:55 +00:00
} else {
2021-05-03 14:36:57 +00:00
if ( point1 . y ( ) > point2 . y ( ) )
swap ( point1 , point2 ) ;
2019-05-07 15:01:55 +00:00
}
2018-10-12 20:50:28 +00:00
2019-05-07 15:01:55 +00:00
// FIXME: Implement clipping below.
2021-05-03 14:36:57 +00:00
const int dx = point2 . x ( ) - point1 . x ( ) ;
const int dy = point2 . y ( ) - point1 . y ( ) ;
2021-04-03 14:11:35 +00:00
int error = 0 ;
2019-05-07 15:01:55 +00:00
if ( dx > dy ) {
2021-04-03 14:11:35 +00:00
const int y_step = dy = = 0 ? 0 : ( dy > 0 ? 1 : - 1 ) ;
const int delta_error = 2 * abs ( dy ) ;
2021-05-03 14:36:57 +00:00
int y = point1 . y ( ) ;
for ( int x = point1 . x ( ) ; x < = point2 . x ( ) ; + + x ) {
2019-05-07 15:01:55 +00:00
if ( clip_rect . contains ( x , y ) )
LibGfx: Make it possible to apply an (integer) scale to a Painter
This adds a scale factor to Painter, which will be used for HighDPI
support. It's also a step towards general affine transforms on Painters.
All of Painter's public API takes logical coordinates, while some
internals deal with physical coordinates now. If scale == 1, logical
and physical coordinates are the same. For scale == 2, a 200x100 bitmap
would be covered by a logical {0, 0, 100, 50} rect, while its physical
size would be {0, 0, 200, 100}.
Most of Painter's functions just assert that scale() == 1 is for now,
but most functions called by WindowServer are updated to handle
arbitrary (integer) scale.
Also add a new Demo "LibGfxScaleDemo" that covers the converted
functions and that can be used to iteratively add scaling support
to more functions.
To make Painter's interface deal with logical coordinates only,
make translation() and clip_rect() non-public.
2021-01-12 20:53:07 +00:00
draw_physical_pixel ( { x , y } , color , thickness ) ;
2019-05-07 15:01:55 +00:00
error + = delta_error ;
2021-04-03 14:11:35 +00:00
if ( error > = dx ) {
y + = y_step ;
error - = 2 * dx ;
2019-05-07 15:01:55 +00:00
}
}
} else {
2021-04-03 14:11:35 +00:00
const int x_step = dx = = 0 ? 0 : ( dx > 0 ? 1 : - 1 ) ;
const int delta_error = 2 * abs ( dx ) ;
2021-05-03 14:36:57 +00:00
int x = point1 . x ( ) ;
for ( int y = point1 . y ( ) ; y < = point2 . y ( ) ; + + y ) {
2019-05-07 15:01:55 +00:00
if ( clip_rect . contains ( x , y ) )
LibGfx: Make it possible to apply an (integer) scale to a Painter
This adds a scale factor to Painter, which will be used for HighDPI
support. It's also a step towards general affine transforms on Painters.
All of Painter's public API takes logical coordinates, while some
internals deal with physical coordinates now. If scale == 1, logical
and physical coordinates are the same. For scale == 2, a 200x100 bitmap
would be covered by a logical {0, 0, 100, 50} rect, while its physical
size would be {0, 0, 200, 100}.
Most of Painter's functions just assert that scale() == 1 is for now,
but most functions called by WindowServer are updated to handle
arbitrary (integer) scale.
Also add a new Demo "LibGfxScaleDemo" that covers the converted
functions and that can be used to iteratively add scaling support
to more functions.
To make Painter's interface deal with logical coordinates only,
make translation() and clip_rect() non-public.
2021-01-12 20:53:07 +00:00
draw_physical_pixel ( { x , y } , color , thickness ) ;
2019-05-07 15:01:55 +00:00
error + = delta_error ;
2021-04-03 14:11:35 +00:00
if ( error > = dy ) {
x + = x_step ;
error - = 2 * dy ;
2019-05-07 15:01:55 +00:00
}
2018-10-12 12:58:16 +00:00
}
}
}
2018-10-13 18:59:25 +00:00
2020-10-20 22:03:07 +00:00
static bool can_approximate_bezier_curve ( const FloatPoint & p1 , const FloatPoint & p2 , const FloatPoint & control )
{
constexpr static int tolerance = 15 ;
auto p1x = 3 * control . x ( ) - 2 * p1 . x ( ) - p2 . x ( ) ;
auto p1y = 3 * control . y ( ) - 2 * p1 . y ( ) - p2 . y ( ) ;
auto p2x = 3 * control . x ( ) - 2 * p2 . x ( ) - p1 . x ( ) ;
auto p2y = 3 * control . y ( ) - 2 * p2 . y ( ) - p1 . y ( ) ;
p1x = p1x * p1x ;
p1y = p1y * p1y ;
p2x = p2x * p2x ;
p2y = p2y * p2y ;
return max ( p1x , p2x ) + max ( p1y , p2y ) < = tolerance ;
}
LibGfx: Make it possible to apply an (integer) scale to a Painter
This adds a scale factor to Painter, which will be used for HighDPI
support. It's also a step towards general affine transforms on Painters.
All of Painter's public API takes logical coordinates, while some
internals deal with physical coordinates now. If scale == 1, logical
and physical coordinates are the same. For scale == 2, a 200x100 bitmap
would be covered by a logical {0, 0, 100, 50} rect, while its physical
size would be {0, 0, 200, 100}.
Most of Painter's functions just assert that scale() == 1 is for now,
but most functions called by WindowServer are updated to handle
arbitrary (integer) scale.
Also add a new Demo "LibGfxScaleDemo" that covers the converted
functions and that can be used to iteratively add scaling support
to more functions.
To make Painter's interface deal with logical coordinates only,
make translation() and clip_rect() non-public.
2021-01-12 20:53:07 +00:00
// static
2020-05-06 07:25:12 +00:00
void Painter : : for_each_line_segment_on_bezier_curve ( const FloatPoint & control_point , const FloatPoint & p1 , const FloatPoint & p2 , Function < void ( const FloatPoint & , const FloatPoint & ) > & callback )
2020-05-05 01:15:17 +00:00
{
2021-04-14 23:25:07 +00:00
struct SegmentDescriptor {
FloatPoint control_point ;
FloatPoint p1 ;
FloatPoint p2 ;
} ;
2020-05-05 01:15:17 +00:00
2021-04-14 23:25:07 +00:00
static constexpr auto split_quadratic_bezier_curve = [ ] ( const FloatPoint & original_control , const FloatPoint & p1 , const FloatPoint & p2 , auto & segments ) {
auto po1_midpoint = original_control + p1 ;
po1_midpoint / = 2 ;
2020-05-06 07:25:12 +00:00
2021-04-14 23:25:07 +00:00
auto po2_midpoint = original_control + p2 ;
po2_midpoint / = 2 ;
2020-07-22 06:46:15 +00:00
2021-04-14 23:25:07 +00:00
auto new_segment = po1_midpoint + po2_midpoint ;
new_segment / = 2 ;
2020-07-22 06:46:15 +00:00
2021-04-14 23:25:07 +00:00
segments . enqueue ( { po1_midpoint , p1 , new_segment } ) ;
segments . enqueue ( { po2_midpoint , new_segment , p2 } ) ;
} ;
Queue < SegmentDescriptor > segments ;
segments . enqueue ( { control_point , p1 , p2 } ) ;
while ( ! segments . is_empty ( ) ) {
auto segment = segments . dequeue ( ) ;
2020-07-22 06:46:15 +00:00
2021-04-14 23:25:07 +00:00
if ( can_approximate_bezier_curve ( segment . p1 , segment . p2 , segment . control_point ) )
callback ( segment . p1 , segment . p2 ) ;
else
split_quadratic_bezier_curve ( segment . control_point , segment . p1 , segment . p2 , segments ) ;
}
}
2020-07-22 06:46:15 +00:00
2021-04-14 23:25:07 +00:00
void Painter : : for_each_line_segment_on_bezier_curve ( const FloatPoint & control_point , const FloatPoint & p1 , const FloatPoint & p2 , Function < void ( const FloatPoint & , const FloatPoint & ) > & & callback )
{
for_each_line_segment_on_bezier_curve ( control_point , p1 , p2 , callback ) ;
2020-07-22 06:46:15 +00:00
}
2020-06-10 08:57:59 +00:00
void Painter : : draw_quadratic_bezier_curve ( const IntPoint & control_point , const IntPoint & p1 , const IntPoint & p2 , Color color , int thickness , LineStyle style )
2020-05-06 07:25:12 +00:00
{
2021-05-03 14:37:05 +00:00
VERIFY ( scale ( ) = = 1 ) ; // FIXME: Add scaling support.
2020-07-22 06:46:15 +00:00
for_each_line_segment_on_bezier_curve ( FloatPoint ( control_point ) , FloatPoint ( p1 ) , FloatPoint ( p2 ) , [ & ] ( const FloatPoint & fp1 , const FloatPoint & fp2 ) {
2021-05-03 14:37:05 +00:00
draw_line ( IntPoint ( fp1 . x ( ) , fp1 . y ( ) ) , IntPoint ( fp2 . x ( ) , fp2 . y ( ) ) , color , thickness , style ) ;
2020-07-22 06:46:15 +00:00
} ) ;
}
LibGfx: Make it possible to apply an (integer) scale to a Painter
This adds a scale factor to Painter, which will be used for HighDPI
support. It's also a step towards general affine transforms on Painters.
All of Painter's public API takes logical coordinates, while some
internals deal with physical coordinates now. If scale == 1, logical
and physical coordinates are the same. For scale == 2, a 200x100 bitmap
would be covered by a logical {0, 0, 100, 50} rect, while its physical
size would be {0, 0, 200, 100}.
Most of Painter's functions just assert that scale() == 1 is for now,
but most functions called by WindowServer are updated to handle
arbitrary (integer) scale.
Also add a new Demo "LibGfxScaleDemo" that covers the converted
functions and that can be used to iteratively add scaling support
to more functions.
To make Painter's interface deal with logical coordinates only,
make translation() and clip_rect() non-public.
2021-01-12 20:53:07 +00:00
// static
2020-07-22 06:46:15 +00:00
void Painter : : for_each_line_segment_on_elliptical_arc ( const FloatPoint & p1 , const FloatPoint & p2 , const FloatPoint & center , const FloatPoint radii , float x_axis_rotation , float theta_1 , float theta_delta , Function < void ( const FloatPoint & , const FloatPoint & ) > & callback )
{
2021-05-22 19:33:07 +00:00
if ( radii . x ( ) < = 0 | | radii . y ( ) < = 0 )
return ;
2021-04-14 23:25:07 +00:00
2021-05-22 19:33:07 +00:00
auto start = p1 ;
auto end = p2 ;
2021-04-14 23:25:07 +00:00
2021-05-22 19:33:07 +00:00
if ( theta_delta < 0 ) {
swap ( start , end ) ;
theta_1 = theta_1 + theta_delta ;
2021-07-05 16:56:06 +00:00
theta_delta = fabsf ( theta_delta ) ;
2021-05-22 19:33:07 +00:00
}
auto relative_start = start - center ;
auto a = radii . x ( ) ;
auto b = radii . y ( ) ;
2021-04-14 23:25:07 +00:00
2021-05-22 19:33:07 +00:00
// The segments are at most 1 long
auto largest_radius = max ( a , b ) ;
double theta_step = atan ( 1 / ( double ) largest_radius ) ;
2021-04-14 23:25:07 +00:00
2021-05-22 19:33:07 +00:00
FloatPoint current_point = relative_start ;
FloatPoint next_point = { 0 , 0 } ;
2021-04-14 23:25:07 +00:00
2021-07-17 16:29:28 +00:00
auto sin_x_axis = AK : : sin ( x_axis_rotation ) ;
auto cos_x_axis = AK : : cos ( x_axis_rotation ) ;
2021-05-22 19:33:07 +00:00
auto rotate_point = [ sin_x_axis , cos_x_axis ] ( FloatPoint & p ) {
auto original_x = p . x ( ) ;
auto original_y = p . y ( ) ;
p . set_x ( original_x * cos_x_axis - original_y * sin_x_axis ) ;
p . set_y ( original_x * sin_x_axis + original_y * cos_x_axis ) ;
2021-04-14 23:25:07 +00:00
} ;
2021-05-22 19:33:07 +00:00
for ( double theta = theta_1 ; theta < = ( ( double ) theta_1 + ( double ) theta_delta ) ; theta + = theta_step ) {
2021-07-17 16:29:28 +00:00
next_point . set_x ( a * AK : : cos < float > ( theta ) ) ;
next_point . set_y ( b * AK : : sin < float > ( theta ) ) ;
2021-05-22 19:33:07 +00:00
rotate_point ( next_point ) ;
callback ( current_point + center , next_point + center ) ;
current_point = next_point ;
2020-07-22 06:46:15 +00:00
}
2021-05-22 19:33:07 +00:00
callback ( current_point + center , end ) ;
2020-07-22 06:46:15 +00:00
}
LibGfx: Make it possible to apply an (integer) scale to a Painter
This adds a scale factor to Painter, which will be used for HighDPI
support. It's also a step towards general affine transforms on Painters.
All of Painter's public API takes logical coordinates, while some
internals deal with physical coordinates now. If scale == 1, logical
and physical coordinates are the same. For scale == 2, a 200x100 bitmap
would be covered by a logical {0, 0, 100, 50} rect, while its physical
size would be {0, 0, 200, 100}.
Most of Painter's functions just assert that scale() == 1 is for now,
but most functions called by WindowServer are updated to handle
arbitrary (integer) scale.
Also add a new Demo "LibGfxScaleDemo" that covers the converted
functions and that can be used to iteratively add scaling support
to more functions.
To make Painter's interface deal with logical coordinates only,
make translation() and clip_rect() non-public.
2021-01-12 20:53:07 +00:00
// static
2020-07-22 06:46:15 +00:00
void Painter : : for_each_line_segment_on_elliptical_arc ( const FloatPoint & p1 , const FloatPoint & p2 , const FloatPoint & center , const FloatPoint radii , float x_axis_rotation , float theta_1 , float theta_delta , Function < void ( const FloatPoint & , const FloatPoint & ) > & & callback )
{
for_each_line_segment_on_elliptical_arc ( p1 , p2 , center , radii , x_axis_rotation , theta_1 , theta_delta , callback ) ;
}
void Painter : : draw_elliptical_arc ( const IntPoint & p1 , const IntPoint & p2 , const IntPoint & center , const FloatPoint & radii , float x_axis_rotation , float theta_1 , float theta_delta , Color color , int thickness , LineStyle style )
{
2021-05-03 14:37:05 +00:00
VERIFY ( scale ( ) = = 1 ) ; // FIXME: Add scaling support.
2020-07-22 06:46:15 +00:00
for_each_line_segment_on_elliptical_arc ( FloatPoint ( p1 ) , FloatPoint ( p2 ) , FloatPoint ( center ) , radii , x_axis_rotation , theta_1 , theta_delta , [ & ] ( const FloatPoint & fp1 , const FloatPoint & fp2 ) {
2021-05-03 14:37:05 +00:00
draw_line ( IntPoint ( fp1 . x ( ) , fp1 . y ( ) ) , IntPoint ( fp2 . x ( ) , fp2 . y ( ) ) , color , thickness , style ) ;
2020-05-06 07:25:12 +00:00
} ) ;
}
2020-06-10 08:57:59 +00:00
void Painter : : add_clip_rect ( const IntRect & rect )
2019-01-24 22:40:12 +00:00
{
2021-05-03 14:37:05 +00:00
state ( ) . clip_rect . intersect ( rect . translated ( translation ( ) ) ) ;
state ( ) . clip_rect . intersect ( m_target - > rect ( ) ) ; // FIXME: This shouldn't be necessary?
2019-01-24 22:40:12 +00:00
}
void Painter : : clear_clip_rect ( )
{
2019-03-09 15:48:02 +00:00
state ( ) . clip_rect = m_clip_origin ;
2019-01-24 22:40:12 +00:00
}
2019-04-15 23:01:03 +00:00
PainterStateSaver : : PainterStateSaver ( Painter & painter )
: m_painter ( painter )
{
m_painter . save ( ) ;
}
PainterStateSaver : : ~ PainterStateSaver ( )
{
m_painter . restore ( ) ;
}
2020-02-06 10:56:38 +00:00
2020-04-16 19:03:17 +00:00
void Painter : : stroke_path ( const Path & path , Color color , int thickness )
{
2021-05-03 14:37:05 +00:00
VERIFY ( scale ( ) = = 1 ) ; // FIXME: Add scaling support.
2020-04-16 19:03:17 +00:00
FloatPoint cursor ;
for ( auto & segment : path . segments ( ) ) {
2020-07-22 06:46:15 +00:00
switch ( segment . type ( ) ) {
case Segment : : Type : : Invalid :
2021-02-23 19:42:32 +00:00
VERIFY_NOT_REACHED ( ) ;
2020-04-16 19:03:17 +00:00
break ;
2020-07-22 06:46:15 +00:00
case Segment : : Type : : MoveTo :
cursor = segment . point ( ) ;
break ;
case Segment : : Type : : LineTo :
2020-07-26 04:31:47 +00:00
draw_line ( cursor . to_type < int > ( ) , segment . point ( ) . to_type < int > ( ) , color , thickness ) ;
2020-07-22 06:46:15 +00:00
cursor = segment . point ( ) ;
2020-04-16 19:03:17 +00:00
break ;
2020-07-22 06:46:15 +00:00
case Segment : : Type : : QuadraticBezierCurveTo : {
auto & through = static_cast < const QuadraticBezierCurveSegment & > ( segment ) . through ( ) ;
2020-07-26 04:31:47 +00:00
draw_quadratic_bezier_curve ( through . to_type < int > ( ) , cursor . to_type < int > ( ) , segment . point ( ) . to_type < int > ( ) , color , thickness ) ;
2020-07-22 06:46:15 +00:00
cursor = segment . point ( ) ;
2020-04-16 19:03:17 +00:00
break ;
2020-07-22 06:46:15 +00:00
}
case Segment : : Type : : EllipticalArcTo :
auto & arc = static_cast < const EllipticalArcSegment & > ( segment ) ;
2020-07-26 04:31:47 +00:00
draw_elliptical_arc ( cursor . to_type < int > ( ) , segment . point ( ) . to_type < int > ( ) , arc . center ( ) . to_type < int > ( ) , arc . radii ( ) , arc . x_axis_rotation ( ) , arc . theta_1 ( ) , arc . theta_delta ( ) , color , thickness ) ;
2020-07-22 06:46:15 +00:00
cursor = segment . point ( ) ;
2020-05-05 01:15:17 +00:00
break ;
2020-04-16 19:03:17 +00:00
}
}
}
2020-12-12 17:34:39 +00:00
[[maybe_unused]] static void approximately_place_on_int_grid ( FloatPoint ffrom , FloatPoint fto , IntPoint & from , IntPoint & to , Optional < IntPoint > previous_to )
{
auto diffs = fto - ffrom ;
// Truncate all first (round down).
from = ffrom . to_type < int > ( ) ;
to = fto . to_type < int > ( ) ;
// There are 16 possible configurations, by deciding to round each
// coord up or down (and there are four coords, from.x from.y to.x to.y)
// we will simply choose one which most closely matches the correct slope
// with the following heuristic:
// - if the x diff is positive or zero (that is, a right-to-left slant), round 'from.x' up and 'to.x' down.
// - if the x diff is negative (that is, a left-to-right slant), round 'from.x' down and 'to.x' up.
// Note that we do not need to touch the 'y' attribute, as that is our scanline.
if ( diffs . x ( ) > = 0 ) {
from . set_x ( from . x ( ) + 1 ) ;
} else {
to . set_x ( to . x ( ) + 1 ) ;
}
if ( previous_to . has_value ( ) & & from . x ( ) ! = previous_to . value ( ) . x ( ) ) // The points have to line up, since we're using these lines to fill a shape.
from . set_x ( previous_to . value ( ) . x ( ) ) ;
}
2020-05-06 07:25:12 +00:00
void Painter : : fill_path ( Path & path , Color color , WindingRule winding_rule )
{
2021-05-03 14:37:05 +00:00
VERIFY ( scale ( ) = = 1 ) ; // FIXME: Add scaling support.
2020-05-08 16:52:38 +00:00
const auto & segments = path . split_lines ( ) ;
2020-05-06 07:25:12 +00:00
if ( segments . size ( ) = = 0 )
return ;
2020-07-22 06:46:15 +00:00
Vector < Path : : SplitLineSegment > active_list ;
2020-05-06 07:25:12 +00:00
active_list . ensure_capacity ( segments . size ( ) ) ;
// first, grab the segments for the very first scanline
2020-12-12 17:34:39 +00:00
int first_y = path . bounding_box ( ) . bottom_right ( ) . y ( ) + 1 ;
int last_y = path . bounding_box ( ) . top_left ( ) . y ( ) - 1 ;
float scanline = first_y ;
2020-05-06 07:25:12 +00:00
size_t last_active_segment { 0 } ;
for ( auto & segment : segments ) {
if ( segment . maximum_y ! = scanline )
break ;
active_list . append ( segment ) ;
+ + last_active_segment ;
}
auto is_inside_shape = [ winding_rule ] ( int winding_number ) {
if ( winding_rule = = WindingRule : : Nonzero )
return winding_number ! = 0 ;
if ( winding_rule = = WindingRule : : EvenOdd )
return winding_number % 2 = = 0 ;
2021-02-23 19:42:32 +00:00
VERIFY_NOT_REACHED ( ) ;
2020-05-06 07:25:12 +00:00
} ;
2020-06-10 08:57:59 +00:00
auto increment_winding = [ winding_rule ] ( int & winding_number , const IntPoint & from , const IntPoint & to ) {
2020-05-06 07:25:12 +00:00
if ( winding_rule = = WindingRule : : EvenOdd ) {
+ + winding_number ;
return ;
}
if ( winding_rule = = WindingRule : : Nonzero ) {
if ( from . dy_relative_to ( to ) < 0 )
+ + winding_number ;
else
- - winding_number ;
return ;
}
2021-02-23 19:42:32 +00:00
VERIFY_NOT_REACHED ( ) ;
2020-05-06 07:25:12 +00:00
} ;
while ( scanline > = last_y ) {
2020-12-12 17:34:39 +00:00
Optional < IntPoint > previous_to ;
2020-05-06 07:25:12 +00:00
if ( active_list . size ( ) ) {
// sort the active list by 'x' from right to left
quick_sort ( active_list , [ ] ( const auto & line0 , const auto & line1 ) {
return line1 . x < line0 . x ;
} ) ;
2021-05-01 19:10:08 +00:00
if constexpr ( FILL_PATH_DEBUG ) {
if ( ( int ) scanline % 10 = = 0 ) {
draw_text ( IntRect ( active_list . last ( ) . x - 20 , scanline , 20 , 10 ) , String : : number ( ( int ) scanline ) ) ;
}
2020-05-06 07:25:12 +00:00
}
if ( active_list . size ( ) > 1 ) {
2021-04-14 20:18:49 +00:00
auto winding_number { winding_rule = = WindingRule : : Nonzero ? 1 : 0 } ;
2020-05-06 07:25:12 +00:00
for ( size_t i = 1 ; i < active_list . size ( ) ; + + i ) {
auto & previous = active_list [ i - 1 ] ;
auto & current = active_list [ i ] ;
2020-12-12 17:34:39 +00:00
IntPoint from , to ;
IntPoint truncated_from { previous . x , scanline } ;
IntPoint truncated_to { current . x , scanline } ;
approximately_place_on_int_grid ( { previous . x , scanline } , { current . x , scanline } , from , to , previous_to ) ;
2020-05-06 07:25:12 +00:00
if ( is_inside_shape ( winding_number ) ) {
// The points between this segment and the previous are
// inside the shape
2021-01-16 14:51:56 +00:00
2021-02-07 12:03:24 +00:00
dbgln_if ( FILL_PATH_DEBUG , " y={}: {} at {}: {} -- {} " , scanline , winding_number , i , from , to ) ;
2020-05-10 10:58:33 +00:00
draw_line ( from , to , color , 1 ) ;
2020-05-06 07:25:12 +00:00
}
auto is_passing_through_maxima = scanline = = previous . maximum_y
| | scanline = = previous . minimum_y
| | scanline = = current . maximum_y
| | scanline = = current . minimum_y ;
auto is_passing_through_vertex = false ;
if ( is_passing_through_maxima ) {
is_passing_through_vertex = previous . x = = current . x ;
}
if ( ! is_passing_through_vertex | | previous . inverse_slope * current . inverse_slope < 0 )
2020-12-12 17:34:39 +00:00
increment_winding ( winding_number , truncated_from , truncated_to ) ;
2020-05-06 07:25:12 +00:00
// update the x coord
active_list [ i - 1 ] . x - = active_list [ i - 1 ] . inverse_slope ;
}
active_list . last ( ) . x - = active_list . last ( ) . inverse_slope ;
} else {
2020-06-10 08:57:59 +00:00
auto point = IntPoint ( active_list [ 0 ] . x , scanline ) ;
2020-05-06 07:25:12 +00:00
draw_line ( point , point , color ) ;
// update the x coord
active_list . first ( ) . x - = active_list . first ( ) . inverse_slope ;
}
}
- - scanline ;
// remove any edge that goes out of bound from the active list
for ( size_t i = 0 , count = active_list . size ( ) ; i < count ; + + i ) {
if ( scanline < = active_list [ i ] . minimum_y ) {
active_list . remove ( i ) ;
- - count ;
- - i ;
}
}
for ( size_t j = last_active_segment ; j < segments . size ( ) ; + + j , + + last_active_segment ) {
auto & segment = segments [ j ] ;
if ( segment . maximum_y < scanline )
break ;
if ( segment . minimum_y > = scanline )
continue ;
active_list . append ( segment ) ;
}
}
2021-05-01 19:10:08 +00:00
if constexpr ( FILL_PATH_DEBUG ) {
size_t i { 0 } ;
for ( auto & segment : segments ) {
draw_line ( Point < int > ( segment . from ) , Point < int > ( segment . to ) , Color : : from_hsv ( i + + * 360.0 / segments . size ( ) , 1.0 , 1.0 ) , 1 ) ;
}
2020-08-26 22:34:59 +00:00
}
2020-05-06 07:25:12 +00:00
}
2020-10-27 20:17:50 +00:00
void Painter : : blit_disabled ( const IntPoint & location , const Gfx : : Bitmap & bitmap , const IntRect & rect , const Palette & palette )
{
auto bright_color = palette . threed_highlight ( ) ;
auto dark_color = palette . threed_shadow1 ( ) ;
blit_filtered ( location . translated ( 1 , 1 ) , bitmap , rect , [ & ] ( auto ) {
return bright_color ;
} ) ;
blit_filtered ( location , bitmap , rect , [ & ] ( Color src ) {
int gray = src . to_grayscale ( ) . red ( ) ;
if ( gray > 160 )
return bright_color ;
return dark_color ;
} ) ;
}
2021-03-06 00:42:50 +00:00
void Painter : : blit_tiled ( const IntRect & dst_rect , const Gfx : : Bitmap & bitmap , const IntRect & rect )
{
auto tile_width = rect . width ( ) ;
auto tile_height = rect . height ( ) ;
auto dst_right = dst_rect . right ( ) ;
auto dst_bottom = dst_rect . bottom ( ) ;
for ( int tile_y = dst_rect . top ( ) ; tile_y < dst_bottom ; tile_y + = tile_height ) {
for ( int tile_x = dst_rect . left ( ) ; tile_x < dst_right ; tile_x + = tile_width ) {
IntRect tile_src_rect = rect ;
auto tile_x_overflow = tile_x + tile_width - dst_right ;
if ( tile_x_overflow > 0 ) {
tile_src_rect . set_width ( tile_width - tile_x_overflow ) ;
}
auto tile_y_overflow = tile_y + tile_height - dst_bottom ;
if ( tile_y_overflow > 0 ) {
tile_src_rect . set_height ( tile_height - tile_y_overflow ) ;
}
blit ( IntPoint ( tile_x , tile_y ) , bitmap , tile_src_rect ) ;
}
}
}
2021-04-10 23:09:44 +00:00
String parse_ampersand_string ( const StringView & raw_text , Optional < size_t > * underline_offset )
2021-04-05 20:40:07 +00:00
{
2021-04-10 23:09:44 +00:00
if ( raw_text . is_empty ( ) )
return String : : empty ( ) ;
2021-04-05 20:40:07 +00:00
2021-04-10 23:09:44 +00:00
StringBuilder builder ;
2021-04-05 20:40:07 +00:00
2021-04-10 23:09:44 +00:00
for ( size_t i = 0 ; i < raw_text . length ( ) ; + + i ) {
if ( raw_text [ i ] = = ' & ' ) {
2021-07-14 15:10:05 +00:00
if ( i ! = ( raw_text . length ( ) - 1 ) & & raw_text [ i + 1 ] = = ' & ' ) {
2021-04-10 23:09:44 +00:00
builder . append ( raw_text [ i ] ) ;
2021-07-14 15:10:05 +00:00
+ + i ;
} else if ( underline_offset & & ! ( * underline_offset ) . has_value ( ) ) {
2021-04-10 23:09:44 +00:00
* underline_offset = i ;
2021-07-14 15:10:05 +00:00
}
2021-04-10 23:09:44 +00:00
continue ;
2021-04-05 20:40:07 +00:00
}
2021-04-10 23:09:44 +00:00
builder . append ( raw_text [ i ] ) ;
}
return builder . to_string ( ) ;
}
2021-04-05 20:40:07 +00:00
2021-04-10 23:09:44 +00:00
void Gfx : : Painter : : draw_ui_text ( const Gfx : : IntRect & rect , const StringView & text , const Gfx : : Font & font , Gfx : : TextAlignment text_alignment , Gfx : : Color color )
{
2021-04-05 20:40:07 +00:00
Optional < size_t > underline_offset ;
2021-04-10 23:09:44 +00:00
auto name_to_draw = parse_ampersand_string ( text , & underline_offset ) ;
2021-04-05 20:40:07 +00:00
Gfx : : IntRect text_rect { 0 , 0 , font . width ( name_to_draw ) , font . glyph_height ( ) } ;
2021-04-09 09:15:41 +00:00
text_rect . align_within ( rect , text_alignment ) ;
2021-04-05 20:40:07 +00:00
2021-04-09 09:15:41 +00:00
draw_text ( text_rect , name_to_draw , font , text_alignment , color ) ;
2021-04-05 20:40:07 +00:00
if ( underline_offset . has_value ( ) ) {
Utf8View utf8_view { name_to_draw } ;
int width = 0 ;
for ( auto it = utf8_view . begin ( ) ; it ! = utf8_view . end ( ) ; + + it ) {
if ( utf8_view . byte_offset_of ( it ) > = underline_offset . value ( ) ) {
2021-04-05 21:15:09 +00:00
int y = text_rect . bottom ( ) + 1 ;
2021-04-05 20:40:07 +00:00
int x1 = text_rect . left ( ) + width ;
int x2 = x1 + font . glyph_or_emoji_width ( * it ) ;
2021-04-10 23:11:36 +00:00
draw_line ( { x1 , y } , { x2 , y } , color ) ;
2021-04-05 20:40:07 +00:00
break ;
}
2021-04-09 13:44:04 +00:00
width + = font . glyph_or_emoji_width ( * it ) + font . glyph_spacing ( ) ;
2021-04-05 20:40:07 +00:00
}
}
}
2020-02-06 10:56:38 +00:00
}