Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

0.6.0 / src / web / painting / device_pixel_converter.lucb

#==============================================================================================
#
#   device_pixel_converter - Painting::DevicePixelConverter: CSS pixels to device pixels
#
#   DESCRIPTION:
#       Port of Libraries/LibWeb/Painting/DevicePixelConverter.h (lines 1-94). Each conversion
#       scales by the device pixels per CSS pixel in double precision and rounds, floors or ceils
#       with the C library (the C++ calls the global ::round, ::floor and ::ceil of <math.h>
#       on doubles), then converts to DevicePixels (int).
#
#       Copyright (c) 2018-2022, Andreas Kling <andreas@ladybird.org>
#       Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
#       Copyright (c) 2025, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

## Ported from Libraries/LibWeb/Painting/DevicePixelConverter.h:18 Web::Painting::DevicePixelConverter::rounded_device_pixels.
pub func paint_device_pixel_converter_rounded_device_pixels(this: const PaintDevicePixelConverter*, css_pixels: gfx.CssPixels) -> i32:
    return (i32)math.round(css_pixels.to_double() * this.m_device_pixels_per_css_pixel)

## Ported from Libraries/LibWeb/Painting/DevicePixelConverter.h:23 Web::Painting::DevicePixelConverter::enclosing_device_pixels.
pub func paint_device_pixel_converter_enclosing_device_pixels(this: const PaintDevicePixelConverter*, css_pixels: gfx.CssPixels) -> i32:
    return (i32)math.ceil(css_pixels.to_double() * this.m_device_pixels_per_css_pixel)

## Ported from Libraries/LibWeb/Painting/DevicePixelConverter.h:28 Web::Painting::DevicePixelConverter::floored_device_pixels.
pub func paint_device_pixel_converter_floored_device_pixels(this: const PaintDevicePixelConverter*, css_pixels: gfx.CssPixels) -> i32:
    return (i32)math.floor(css_pixels.to_double() * this.m_device_pixels_per_css_pixel)

## Ported from Libraries/LibWeb/Painting/DevicePixelConverter.h:33 Web::Painting::DevicePixelConverter::rounded_device_point.
pub func paint_device_pixel_converter_rounded_device_point(this: const PaintDevicePixelConverter*, point: gfx.CssPixelPoint) -> gfx.IntPoint:
    return gfx.IntPoint(m_x = (i32)math.round(point.m_x.to_double() * this.m_device_pixels_per_css_pixel),
                        m_y = (i32)math.round(point.m_y.to_double() * this.m_device_pixels_per_css_pixel))

## Ported from Libraries/LibWeb/Painting/DevicePixelConverter.h:41 Web::Painting::DevicePixelConverter::floored_device_point.
pub func paint_device_pixel_converter_floored_device_point(this: const PaintDevicePixelConverter*, point: gfx.CssPixelPoint) -> gfx.IntPoint:
    return gfx.IntPoint(m_x = (i32)math.floor(point.m_x.to_double() * this.m_device_pixels_per_css_pixel),
                        m_y = (i32)math.floor(point.m_y.to_double() * this.m_device_pixels_per_css_pixel))

## Ported from Libraries/LibWeb/Painting/DevicePixelConverter.h:49 Web::Painting::DevicePixelConverter::enclosing_device_rect.
pub func paint_device_pixel_converter_enclosing_device_rect(this: const PaintDevicePixelConverter*, rect: gfx.CssPixelRect) -> gfx.DevicePixelRect:
    var result: gfx.DevicePixelRect = ---
    gfx.device_pixel_rect_construct_i32(&result,
                                        (i32)math.floor(rect.m_location.m_x.to_double() * this.m_device_pixels_per_css_pixel),
                                        (i32)math.floor(rect.m_location.m_y.to_double() * this.m_device_pixels_per_css_pixel),
                                        (i32)math.ceil(rect.m_size.m_width.to_double() * this.m_device_pixels_per_css_pixel),
                                        (i32)math.ceil(rect.m_size.m_height.to_double() * this.m_device_pixels_per_css_pixel))
    return result

## Ported from Libraries/LibWeb/Painting/DevicePixelConverter.h:59 Web::Painting::DevicePixelConverter::rounded_device_rect.
pub func paint_device_pixel_converter_rounded_device_rect(this: const PaintDevicePixelConverter*, rect: gfx.CssPixelRect) -> gfx.DevicePixelRect:
    let s = this.m_device_pixels_per_css_pixel
    let x = math.round(rect.m_location.m_x.to_double() * s)
    let y = math.round(rect.m_location.m_y.to_double() * s)
    # The braced initializer converts each double to DevicePixels (int).
    var result: gfx.DevicePixelRect = ---
    gfx.device_pixel_rect_construct_i32(&result, (i32)x, (i32)y,
                                        (i32)(math.round((rect.m_location.m_x.to_double() + rect.m_size.m_width.to_double()) * s) - x),
                                        (i32)(math.round((rect.m_location.m_y.to_double() + rect.m_size.m_height.to_double()) * s) - y))
    return result

## Ported from Libraries/LibWeb/Painting/DevicePixelConverter.h:67 Web::Painting::DevicePixelConverter::enclosing_device_size.
pub func paint_device_pixel_converter_enclosing_device_size(this: const PaintDevicePixelConverter*, size: gfx.CssPixelSize) -> gfx.IntSize:
    return gfx.IntSize(m_width = (i32)math.ceil(size.m_width.to_double() * this.m_device_pixels_per_css_pixel),
                       m_height = (i32)math.ceil(size.m_height.to_double() * this.m_device_pixels_per_css_pixel))

## Ported from Libraries/LibWeb/Painting/DevicePixelConverter.h:75 Web::Painting::DevicePixelConverter::rounded_device_size.
pub func paint_device_pixel_converter_rounded_device_size(this: const PaintDevicePixelConverter*, size: gfx.CssPixelSize) -> gfx.IntSize:
    return gfx.IntSize(m_width = (i32)math.round(size.m_width.to_double() * this.m_device_pixels_per_css_pixel),
                       m_height = (i32)math.round(size.m_height.to_double() * this.m_device_pixels_per_css_pixel))

## Ported from Libraries/LibWeb/Painting/DevicePixelConverter.h:83 Web::Painting::DevicePixelConverter::device_pixels_per_css_pixel.
pub func paint_device_pixel_converter_device_pixels_per_css_pixel(this: const PaintDevicePixelConverter*) -> f64:
    return this.m_device_pixels_per_css_pixel

## Ported from Libraries/LibWeb/Painting/DevicePixelConverter.h:85 Web::Painting::DevicePixelConverter::DevicePixelConverter.
pub func paint_device_pixel_converter_construct(this: PaintDevicePixelConverter*, device_pixels_per_css_pixel: f64):
    this.m_device_pixels_per_css_pixel = device_pixels_per_css_pixel