Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-render

Source of release 0.6.1

0.6.1 / src / raster / scan_hairline_aa.lucb

#==============================================================================================
#
#   scan_hairline_aa - anti-aliased rectangles
#
#   DESCRIPTION:
#       Port of tiny-skia scan/hairline_aa.rs (SkScan_Antihair.cpp), part 1: FixedRect,
#       fill_rect (anti-aliased rectangles in 24.8 fixed point: fill_dot8, do_scanline,
#       call_hline_blitter) and stroke_path. The hairlines themselves are Skia m144's, in
#       scan_antihair.lucb.
#
#==============================================================================================

struct FixedRect:
    var left: FDot16
    var top: FDot16
    var right: FDot16
    var bottom: FDot16

    static func from_rect(src: Rect) -> FixedRect:
        return FixedRect(left = fdot16_from_f32(src.left()), top = fdot16_from_f32(src.top()), right = fdot16_from_f32(src.right()), bottom = fdot16_from_f32(src.bottom()))

func alpha_mul(value: AlphaU8, alpha256: i32) -> u8:
    let a = ((i32)value * alpha256) >> 8
    return (u8)a

func hairline_aa_fill_rect(rect0: Rect, clip: ScreenIntRect, blitter: Blitter):
    let rect = rect0.intersect(clip.to_rect()) else return  # everything was clipped out

    let fr = FixedRect.from_rect(rect)
    fill_fixed_rect(fr, blitter)

func fill_fixed_rect(rect: FixedRect, blitter: Blitter):
    fill_dot8(fdot8_from_fdot16(rect.left), fdot8_from_fdot16(rect.top), fdot8_from_fdot16(rect.right), fdot8_from_fdot16(rect.bottom), true, blitter)

func to_alpha(a: i32) -> u8:
    return (u8)a

func fill_dot8(l: FDot8, t: FDot8, r: FDot8, b: FDot8, fill_inner: bool, blitter0: Blitter):
    var blitter = blitter0
    # check for empty now that we're in our reduced precision space
    if l >= r or t >= b:
        return

    var top = t >> 8
    if top == ((b - 1) >> 8):
        # just one scanline high
        do_scanline(l, top, r, to_alpha(b - t - 1), blitter)
        return

    if t & 0xFF != 0:
        do_scanline(l, top, r, to_alpha(256 - (t & 0xFF)), blitter)
        top += 1

    let bottom = b >> 8
    let height = bottom - top
    if height > 0:
        var left = l >> 8
        if left == ((r - 1) >> 8):
            # just 1-pixel wide
            blitter.blit_v(left, top, height, to_alpha(r - l - 1))
        else:
            if l & 0xFF != 0:
                blitter.blit_v(left, top, height, to_alpha(256 - (l & 0xFF)))

                left += 1

            let right = r >> 8
            let width = right - left
            if width > 0 and fill_inner:
                blitter.blit_rect(left, top, width, height)

            if r & 0xFF != 0:
                blitter.blit_v(right, top, height, to_alpha(r & 0xFF))

    if b & 0xFF != 0:
        do_scanline(l, bottom, r, to_alpha(b & 0xFF), blitter)

func do_scanline(l: FDot8, top: i32, r: FDot8, alpha: AlphaU8, blitter0: Blitter):
    var blitter = blitter0
    if (l >> 8) == ((r - 1) >> 8):
        # 1x1 pixel
        blitter.blit_v(l >> 8, top, 1, alpha_mul(alpha, r - l))
        return

    var left = l >> 8

    if l & 0xFF != 0:
        blitter.blit_v(left, top, 1, alpha_mul(alpha, 256 - (l & 0xFF)))
        left += 1

    let right = r >> 8
    let width = right - left
    if width > 0:
        call_hline_blitter(left, top, width, alpha, blitter)

    if r & 0xFF != 0:
        blitter.blit_v(right, top, 1, alpha_mul(alpha, r & 0xFF))

func call_hline_blitter(x0: i32, y: i32, count0: i32, alpha: AlphaU8, blitter0: Blitter):
    var blitter = blitter0
    let hline_stack_buffer: i32 = 100

    var runs: AlphaRun[101]
    var aa: u8[100]

    var x = x0
    var count = count0
    while true:
        # In theory, we should be able to just do this once (outside of the loop), since
        # aa[] and runs[] are supposed" to be const when we call the blitter. In reality,
        # some wrapper-blitters (e.g. RgnClipBlitter) cast away that constness, and modify
        # the buffers in-place. Hence the need to be defensive here and reseed the aa value.
        aa[0] = alpha

        var n = count
        if n > hline_stack_buffer:
            n = hline_stack_buffer

        runs[0] = (u16)n
        runs[(usize)n] = 0
        blitter.blit_anti_h(x, y, aa, runs)
        x += n
        count -= n
        if count <= 0:
            break

func hairline_aa_stroke_path(path: const Path*, line_cap: LineCap, clip: ScreenIntRect, blitter: Blitter):
    stroke_path_impl(path, line_cap, clip, anti_hair_line_rgn, blitter)