dymokomi/luce-browser-engine
Source of release 0.6.1
0.6.1 / src / web / painting / border_painting_1.lucb
#==============================================================================================
#
# border_painting_1 - Painting::BorderPainting: border colors and the double, groove and
# ridge styles
#
# DESCRIPTION:
# Port of Libraries/LibWeb/Painting/BorderPainting.cpp (lines 1-249): the inset/outset
# shades, `border_color`, and the start of `paint_border` (its `paint_double_border`
# lambda, the line style switch and the dotted and dashed lines). The solid edges, the
# points of each edge's shape and `paint_all_borders` are in border_painting_2.
#
# The C++ lambdas of `paint_border` are functions taking what they capture.
#
# Copyright (c) 2020, Andreas Kling <andreas@ladybird.org>
# Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
# Copyright (c) 2022, MacDue <macdue@dueutil.tech>
# SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================
# mark: Inset and outset shades ===================================================================
## Ported from Libraries/LibWeb/Painting/BorderPainting.cpp:17 dark_light_absolute_value_difference.
let paint_border_dark_light_absolute_value_difference: f64 = 1.0 / 3.0
## Ported from Libraries/LibWeb/Painting/BorderPainting.cpp:19 Web::Painting::light_color_for_inset_and_outset.
func paint_light_color_for_inset_and_outset(color: gfx.Color) -> gfx.Color:
let hsv = gfx.color_to_hsv(&color)
if hsv.value >= paint_border_dark_light_absolute_value_difference:
return color
var result = gfx.color_from_hsv_hsv(gfx.Hsv(hue = hsv.hue, saturation = hsv.saturation, value = hsv.value + paint_border_dark_light_absolute_value_difference))
gfx.color_set_alpha(&result, gfx.color_alpha(&color))
return result
## Ported from Libraries/LibWeb/Painting/BorderPainting.cpp:29 Web::Painting::dark_color_for_inset_and_outset.
func paint_dark_color_for_inset_and_outset(color: gfx.Color) -> gfx.Color:
let hsv = gfx.color_to_hsv(&color)
if hsv.value < paint_border_dark_light_absolute_value_difference:
return color
var result = gfx.color_from_hsv_hsv(gfx.Hsv(hue = hsv.hue, saturation = hsv.saturation, value = hsv.value - paint_border_dark_light_absolute_value_difference))
gfx.color_set_alpha(&result, gfx.color_alpha(&color))
return result
## Ported from Libraries/LibWeb/Painting/BorderPainting.cpp:39 Web::Painting::border_color.
pub func paint_border_color(edge: display_list.BorderEdge, borders_data: display_list.BordersDataDevicePixels) -> gfx.Color:
let border_data = display_list.borders_data_device_pixels_for_edge_const(&borders_data, edge)
if border_data.line_style == css_data.LineStyle.inset:
if edge == display_list.BorderEdge.left or edge == display_list.BorderEdge.top:
return paint_dark_color_for_inset_and_outset(border_data.color)
return paint_light_color_for_inset_and_outset(border_data.color)
if border_data.line_style == css_data.LineStyle.outset:
if edge == display_list.BorderEdge.left or edge == display_list.BorderEdge.top:
return paint_light_color_for_inset_and_outset(border_data.color)
return paint_dark_color_for_inset_and_outset(border_data.color)
return border_data.color
# mark: Double, groove and ridge borders ==========================================================
## `max(1, static_cast<float>(value) * proportional_line_thickness)`: AK::max deduces int from its first argument,
## so the float product is truncated to an int.
func paint_border_scaled_width(value: i32, proportional_line_thickness: f32) -> i32:
return ak.max((i32)1, (i32)((f32)value * proportional_line_thickness))
## The `paint_double_border` lambda of paint_border: the edge painted twice, an outer and an inner border each
## `proportional_line_thickness` of the width, with the inner one's rect and radii shrunk.
func paint_border_paint_double_border(painter: display_list.DisplayListRecorder*, edge: display_list.BorderEdge, rect: gfx.DevicePixelRect, radius: display_list.CornerRadius, opposite_radius: display_list.CornerRadius, borders_data: display_list.BordersDataDevicePixels, path: gfx.Path*, proportional_line_thickness: f32, outer_style: css_data.LineStyle, inner_style: css_data.LineStyle):
# AD-HOC: We clamp the individual borders to 1px thick if they're less so that they don't disappear entirely.
# This matches other browsers and is allowable per the spec, where the thickness is implementation-defined.
# FIXME: Converting to floats and back is awkward, can we somehow do all this processing using CSSPixels?
var modified_borders_data = borders_data
modified_borders_data.top.width = paint_border_scaled_width(modified_borders_data.top.width, proportional_line_thickness)
modified_borders_data.right.width = paint_border_scaled_width(modified_borders_data.right.width, proportional_line_thickness)
modified_borders_data.bottom.width = paint_border_scaled_width(modified_borders_data.bottom.width, proportional_line_thickness)
modified_borders_data.left.width = paint_border_scaled_width(modified_borders_data.left.width, proportional_line_thickness)
var modified_rect = rect
# Outer border
match edge:
.top:
gfx.device_pixel_rect_set_height(&modified_rect, paint_border_scaled_width(gfx.device_pixel_rect_height(&rect), proportional_line_thickness))
.right:
gfx.device_pixel_rect_set_width(&modified_rect, paint_border_scaled_width(gfx.device_pixel_rect_width(&rect), proportional_line_thickness))
gfx.device_pixel_rect_set_right_without_resize(&modified_rect, gfx.device_pixel_rect_right(&rect))
.bottom:
gfx.device_pixel_rect_set_height(&modified_rect, paint_border_scaled_width(gfx.device_pixel_rect_height(&rect), proportional_line_thickness))
gfx.device_pixel_rect_set_bottom_without_resize(&modified_rect, gfx.device_pixel_rect_bottom(&rect))
.left:
gfx.device_pixel_rect_set_width(&modified_rect, paint_border_scaled_width(gfx.device_pixel_rect_width(&rect), proportional_line_thickness))
_:
trap("VERIFY_NOT_REACHED")
display_list.borders_data_device_pixels_for_edge(&modified_borders_data, edge).line_style = outer_style
paint_paint_border(painter, edge, modified_rect, radius, opposite_radius, modified_borders_data, path, true)
# Inner border, with smaller rect and radii
var modified_radius = radius
var modified_opposite_radius = opposite_radius
match edge:
.top:
let top_inset = borders_data.top.width - modified_borders_data.top.width
let right_inset = borders_data.right.width - modified_borders_data.right.width
let left_inset = borders_data.left.width - modified_borders_data.left.width
modified_radius.horizontal_radius = ak.max((i32)0, modified_radius.horizontal_radius - left_inset)
modified_opposite_radius.horizontal_radius = ak.max((i32)0, modified_opposite_radius.horizontal_radius - right_inset)
modified_radius.vertical_radius = ak.max((i32)0, modified_radius.vertical_radius - top_inset)
modified_opposite_radius.vertical_radius = modified_radius.vertical_radius
gfx.device_pixel_rect_set_bottom_without_resize(&modified_rect, gfx.device_pixel_rect_bottom(&rect))
# FIXME: Figure out the correct shrink amounts. This is only correct for some cases.
if modified_radius.horizontal_radius == 0 and modified_opposite_radius.horizontal_radius == 0:
gfx.device_pixel_rect_shrink_i32_i32_i32(&modified_rect, 0, right_inset, 0, left_inset)
.right:
let top_inset = borders_data.top.width - modified_borders_data.top.width
let right_inset = borders_data.right.width - modified_borders_data.right.width
let bottom_inset = borders_data.bottom.width - modified_borders_data.bottom.width
modified_radius.horizontal_radius = ak.max((i32)0, modified_radius.horizontal_radius - right_inset)
modified_opposite_radius.horizontal_radius = modified_radius.horizontal_radius
modified_radius.vertical_radius = ak.max((i32)0, modified_radius.vertical_radius - top_inset)
modified_opposite_radius.vertical_radius = ak.max((i32)0, modified_opposite_radius.vertical_radius - bottom_inset)
gfx.device_pixel_rect_set_left(&modified_rect, gfx.device_pixel_rect_left(&rect))
# FIXME: Figure out the correct shrink amounts. This is only correct for some cases.
if modified_radius.vertical_radius == 0 and modified_opposite_radius.vertical_radius == 0:
gfx.device_pixel_rect_shrink_i32_i32_i32(&modified_rect, top_inset, 0, bottom_inset, 0)
.bottom:
let right_inset = borders_data.right.width - modified_borders_data.right.width
let bottom_inset = borders_data.bottom.width - modified_borders_data.bottom.width
let left_inset = borders_data.left.width - modified_borders_data.left.width
modified_radius.horizontal_radius = ak.max((i32)0, modified_radius.horizontal_radius - right_inset)
modified_opposite_radius.horizontal_radius = ak.max((i32)0, modified_opposite_radius.horizontal_radius - left_inset)
modified_radius.vertical_radius = ak.max((i32)0, modified_radius.vertical_radius - bottom_inset)
modified_opposite_radius.vertical_radius = modified_radius.vertical_radius
gfx.device_pixel_rect_set_top(&modified_rect, gfx.device_pixel_rect_top(&rect))
# FIXME: Figure out the correct shrink amounts. This is only correct for some cases.
if modified_radius.horizontal_radius == 0 and modified_opposite_radius.horizontal_radius == 0:
gfx.device_pixel_rect_shrink_i32_i32_i32(&modified_rect, 0, right_inset, 0, left_inset)
.left:
let top_inset = borders_data.top.width - modified_borders_data.top.width
let bottom_inset = borders_data.bottom.width - modified_borders_data.bottom.width
let left_inset = borders_data.left.width - modified_borders_data.left.width
modified_radius.horizontal_radius = ak.max((i32)0, modified_radius.horizontal_radius - left_inset)
modified_opposite_radius.horizontal_radius = modified_radius.horizontal_radius
modified_radius.vertical_radius = ak.max((i32)0, modified_radius.vertical_radius - bottom_inset)
modified_opposite_radius.vertical_radius = ak.max((i32)0, modified_opposite_radius.vertical_radius - top_inset)
gfx.device_pixel_rect_set_right_without_resize(&modified_rect, gfx.device_pixel_rect_right(&rect))
# FIXME: Figure out the correct shrink amounts. This is only correct for some cases.
if modified_radius.vertical_radius == 0 and modified_opposite_radius.vertical_radius == 0:
gfx.device_pixel_rect_shrink_i32_i32_i32(&modified_rect, top_inset, 0, bottom_inset, 0)
_:
trap("VERIFY_NOT_REACHED")
display_list.borders_data_device_pixels_for_edge(&modified_borders_data, edge).line_style = inner_style
paint_paint_border(painter, edge, modified_rect, modified_radius, modified_opposite_radius, modified_borders_data, path, true)
# mark: paint_border ==============================================================================
## The `points_for_edge` lambda of paint_border: the two end points of a dotted or dashed edge's line.
func paint_border_points_for_edge(edge: display_list.BorderEdge, rect: gfx.DevicePixelRect) -> (gfx.IntPoint, gfx.IntPoint):
match edge:
.top:
let top_right = gfx.device_pixel_rect_top_right(&rect)
return (gfx.device_pixel_rect_top_left(&rect), gfx.int_point_moved_left(&top_right, 1))
.right:
let top_right = gfx.device_pixel_rect_top_right(&rect)
let bottom_right = gfx.device_pixel_rect_bottom_right(&rect)
return (gfx.int_point_moved_left(&top_right, 1), gfx.int_point_translated_i32(&bottom_right, -1))
.bottom:
let bottom_left = gfx.device_pixel_rect_bottom_left(&rect)
let bottom_right = gfx.device_pixel_rect_bottom_right(&rect)
return (gfx.int_point_moved_up(&bottom_left, 1), gfx.int_point_translated_i32(&bottom_right, -1))
# default: // Edge::Left
_:
let bottom_left = gfx.device_pixel_rect_bottom_left(&rect)
return (gfx.device_pixel_rect_top_left(&rect), gfx.int_point_moved_up(&bottom_left, 1))
## Ported from Libraries/LibWeb/Painting/BorderPainting.cpp:57 Web::Painting::paint_border.
pub func paint_paint_border(painter: display_list.DisplayListRecorder*, edge: display_list.BorderEdge, rect: gfx.DevicePixelRect, radius: display_list.CornerRadius, opposite_radius: display_list.CornerRadius, borders_data: display_list.BordersDataDevicePixels, path: gfx.Path*, last: bool):
let border_data = *display_list.borders_data_device_pixels_for_edge_const(&borders_data, edge)
if border_data.width <= 0:
return
let color = paint_border_color(edge, borders_data)
let border_style = border_data.line_style
var gfx_line_style = gfx.LineStyle.solid
match border_style:
.none_, .hidden:
return
.dotted:
gfx_line_style = gfx.LineStyle.dotted
.dashed:
gfx_line_style = gfx.LineStyle.dashed
.solid, .inset, .outset:
# The only difference between Inset/Outset and Solid is the color, and we already handled that above.
gfx_line_style = gfx.LineStyle.solid
.double:
# Treat this as two solid borders, each 1/3 of the width.
paint_border_paint_double_border(painter, edge, rect, radius, opposite_radius, borders_data, path, 1.0 / 3.0, css_data.LineStyle.solid, css_data.LineStyle.solid)
return
.groove, .ridge:
# Two half-width borders
paint_border_paint_double_border(painter, edge, rect, radius, opposite_radius, borders_data, path, 0.5,
css_data.LineStyle.inset if border_style == css_data.LineStyle.groove else css_data.LineStyle.outset,
css_data.LineStyle.outset if border_style == css_data.LineStyle.groove else css_data.LineStyle.inset)
return
_:
trap("VERIFY_NOT_REACHED")
if gfx_line_style != gfx.LineStyle.solid:
var (p1, p2) = paint_border_points_for_edge(edge, rect)
let half_width = border_data.width // 2
match edge:
.top:
gfx.int_point_translate_by_i32_i32(&p1, half_width, half_width)
gfx.int_point_translate_by_i32_i32(&p2, -half_width, half_width)
.right:
gfx.int_point_translate_by_i32_i32(&p1, -half_width, half_width)
gfx.int_point_translate_by_i32_i32(&p2, -half_width, -half_width)
.bottom:
gfx.int_point_translate_by_i32_i32(&p1, half_width, -half_width)
gfx.int_point_translate_by_i32_i32(&p2, -half_width, -half_width)
.left:
gfx.int_point_translate_by_i32_i32(&p1, half_width, half_width)
gfx.int_point_translate_by_i32_i32(&p2, half_width, -half_width)
_:
trap("VERIFY_NOT_REACHED")
if border_style == css_data.LineStyle.dotted:
display_list.display_list_recorder_draw_line(painter, p1, p2, color, border_data.width, gfx_line_style)
return
display_list.display_list_recorder_draw_line(painter, p1, p2, color, border_data.width, gfx_line_style)
return
paint_border_paint_solid_edge(painter, edge, rect, radius, opposite_radius, borders_data, border_data, color, path, last)