dymokomi/luce-browser-engine
Source of release 0.6.0
0.6.0 / src / web / painting / shadow_painting.lucb
#==============================================================================================
#
# shadow_painting - Painting::ShadowPainting: box shadows and text shadows
#
# DESCRIPTION:
# Port of Libraries/LibWeb/Painting/ShadowPainting.cpp (lines 1-151): the outer and inner
# box shadow commands of each shadow layer (back to front, clipped to the outside or the
# inside of the border radii), and the text shadow command of each layer of a fragment
# span (its glyph run sliced to the span).
#
# Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.org>
# Copyright (c) 2021-2022, Sam Atkins <atkinssj@serenityos.org>
# Copyright (c) 2022, MacDue <macdue@dueutil.tech>
# SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================
## Ported from Libraries/LibWeb/Painting/ShadowPainting.cpp:17 Web::Painting::paint_box_shadow.
pub func paint_paint_box_shadow(context: DisplayListRecordingContext*, bordered_content_rect: gfx.CssPixelRect, borderless_content_rect: gfx.CssPixelRect, borders_data: display_list.BordersData, border_radii: display_list.BorderRadiiData, box_shadow_layers: ak.Vector[PaintShadowData]):
let corner_radii = border_radii_data_as_corners(&border_radii, display_list_recording_context_device_pixel_converter(context))
# Note: Box-shadow layers are ordered front-to-back, so we paint them in reverse
var layer_index = ak.vector_size(&box_shadow_layers)
while layer_index > 0:
layer_index -= 1
let box_shadow_data = *ak.vector_at_const(&box_shadow_layers, layer_index)
let offset_x = display_list_recording_context_rounded_device_pixels(context, box_shadow_data.offset_x)
let offset_y = display_list_recording_context_rounded_device_pixels(context, box_shadow_data.offset_y)
let blur_radius = display_list_recording_context_rounded_device_pixels(context, box_shadow_data.blur_radius)
let spread_distance = display_list_recording_context_rounded_device_pixels(context, box_shadow_data.spread_distance)
var device_content_rect: gfx.DevicePixelRect = ---
if box_shadow_data.placement == PaintShadowPlacement.inner:
device_content_rect = display_list_recording_context_rounded_device_rect(context, borderless_content_rect)
else:
device_content_rect = display_list_recording_context_rounded_device_rect(context, bordered_content_rect)
let device_content_rect_int = device_content_rect
if box_shadow_data.placement == PaintShadowPlacement.inner:
var outer_shadow_rect = gfx.device_pixel_rect_translated_int_point(&device_content_rect_int, gfx.IntPoint(m_x = offset_x, m_y = offset_y))
let inner_shadow_rect = gfx.device_pixel_rect_inflated_i32_i32_i32(&outer_shadow_rect, -spread_distance, -spread_distance, -spread_distance, -spread_distance)
gfx.device_pixel_rect_inflate_i32_i32_i32(&outer_shadow_rect,
blur_radius + offset_y,
blur_radius + gfx.abs_i32(offset_x),
blur_radius + gfx.abs_i32(offset_y),
blur_radius + offset_x)
var inner_shadow_corner_radii = corner_radii
display_list.corner_radii_adjust_corners_for_spread_distance(&inner_shadow_corner_radii, -spread_distance)
var shrinked_border_radii = border_radii
display_list.border_radii_data_shrink(&shrinked_border_radii, borders_data.top.width, borders_data.right.width, borders_data.bottom.width, borders_data.left.width)
var corner_clipper = paint_scoped_corner_radius_clip_new(context, device_content_rect, shrinked_border_radii, display_list.CornerClip.outside)
display_list.display_list_recorder_paint_inner_box_shadow(display_list_recording_context_display_list_recorder(context), display_list.PaintInnerBoxShadow(
color = box_shadow_data.color,
blur_radius = blur_radius,
device_content_rect = device_content_rect_int,
content_corner_radii = corner_radii,
outer_shadow_rect = outer_shadow_rect,
inner_shadow_rect = inner_shadow_rect,
inner_shadow_corner_radii = inner_shadow_corner_radii))
paint_scoped_corner_radius_clip_destruct(&corner_clipper)
else:
var shadow_rect = device_content_rect_int
gfx.device_pixel_rect_inflate_i32_i32_i32(&shadow_rect, spread_distance, spread_distance, spread_distance, spread_distance)
gfx.device_pixel_rect_translate_by_i32_i32(&shadow_rect, offset_x, offset_y)
var shadow_corner_radii = corner_radii
display_list.corner_radii_adjust_corners_for_spread_distance(&shadow_corner_radii, spread_distance)
var corner_clipper = paint_scoped_corner_radius_clip_new(context, device_content_rect, border_radii, display_list.CornerClip.inside)
display_list.display_list_recorder_paint_outer_box_shadow(display_list_recording_context_display_list_recorder(context), display_list.PaintOuterBoxShadow(
color = box_shadow_data.color,
blur_radius = blur_radius,
device_content_rect = device_content_rect_int,
content_corner_radii = corner_radii,
shadow_rect = shadow_rect,
shadow_corner_radii = shadow_corner_radii))
paint_scoped_corner_radius_clip_destruct(&corner_clipper)
## Ported from Libraries/LibWeb/Painting/ShadowPainting.cpp:87 Web::Painting::paint_text_shadow.
pub func paint_paint_text_shadow(context: DisplayListRecordingContext*, span: PaintPaintableFragmentFragmentSpan):
let fragment = span.fragment
let shadow_layers = span.shadow_layers else paint_paintable_fragment_shadows(fragment)
if ak.vector_is_empty(&shadow_layers):
return
var glyph_run = paint_paintable_fragment_glyph_run(fragment) else return
let glyphs = web_fonts.glyph_run_glyphs_const(glyph_run)
if ak.vector_is_empty(&glyphs):
return
# If this is a partial span, slice the glyph run to only include the relevant glyphs.
if span.start_code_unit != 0 or span.end_code_unit != paint_paintable_fragment_length_in_code_units(fragment):
var start_glyph: usize = 0
var glyph_count: usize = 0
var code_unit_offset: usize = 0
for i in 0..<ak.vector_size(&glyphs):
if code_unit_offset == span.start_code_unit:
start_glyph = i
code_unit_offset += ak.vector_at_const(&glyphs, i).length_in_code_units
if code_unit_offset == span.end_code_unit:
glyph_count = i - start_glyph + 1
break
if glyph_count > 0:
glyph_run = web_fonts.glyph_run_slice(glyph_run, start_glyph, glyph_count)
let fragment_width = display_list_recording_context_enclosing_device_pixels(context, paint_paintable_fragment_width(fragment))
let fragment_height = display_list_recording_context_enclosing_device_pixels(context, paint_paintable_fragment_height(fragment))
let fragment_baseline = display_list_recording_context_rounded_device_pixels(context, paint_paintable_fragment_baseline(fragment))
let fragment_absolute_rect = paint_paintable_fragment_absolute_rect(fragment)
# Note: Box-shadow layers are ordered front-to-back, so we paint them in reverse
var layer_index = ak.vector_size(&shadow_layers)
while layer_index > 0:
layer_index -= 1
let layer = *ak.vector_at_const(&shadow_layers, layer_index)
let blur_radius = display_list_recording_context_rounded_device_pixels(context, layer.blur_radius)
# Space around the painted text to allow it to blur.
let margin = blur_radius * 2
var text_rect: gfx.DevicePixelRect = ---
gfx.device_pixel_rect_construct_i32(&text_rect, margin, margin, fragment_width, fragment_height)
var bounding_rect: gfx.DevicePixelRect = ---
gfx.device_pixel_rect_construct_i32(&bounding_rect, 0, 0, gfx.device_pixel_rect_width(&text_rect) + margin + margin, gfx.device_pixel_rect_height(&text_rect) + margin + margin)
# FIXME: this is close but not quite perfect. non integer scale values can be offset by tiny amounts.
let css_margin = gfx.mul_css_pixels_int(layer.blur_radius, 2)
let scale = display_list_recording_context_device_pixels_per_css_pixel(context)
let location = gfx.FloatPoint(m_x = gfx.css_pixel_rect_x(&fragment_absolute_rect).add(layer.offset_x).sub(css_margin).to_float(),
m_y = gfx.css_pixel_rect_y(&fragment_absolute_rect).add(layer.offset_y).sub(css_margin).to_float())
let draw_location = gfx.float_point_mul(&location, (f32)scale)
display_list.display_list_recorder_paint_text_shadow(display_list_recording_context_display_list_recorder(context), blur_radius, bounding_rect, gfx.device_pixel_rect_translated_i32_i32(&text_rect, 0, fragment_baseline), glyph_run, scale, layer.color, draw_location)