Repository: SerenityOS/serenity
File: Userland/Libraries/LibPDF/Renderer.cpp (C++)

Code before:
{
    if (m_rendering_preferences.show_clipping_paths)
        m_clip_paths_to_show_for_debugging.append(m_current_path);

    if (!m_rendering_preferences.apply_clip)
        return {};

    // If the clip is a rectangle that is larger than the current clip's bounding box, we can ignore it.
    // This is important for performance, because many PDFs add a clip for the whole page before other clips,
    // and we don't want to allocate a whole-page bitmap for those.
    if (auto maybe_rect = m_current_path.as_rect(); maybe_rect.has_value()) {
        auto rect = Gfx::enclosing_int_rect(maybe_rect.value());
        if (rect.contains(state().clipping_state.clip_bounding_box))
            return {};
    }

    // About to set new clip; flush old one if needed.
    copy_current_clip_path_content_to_output();

    auto next_clipping_bbox = Gfx::enclosing_int_rect(m_current_path.bounding_box());
    next_clipping_bbox.intersect(state().clipping_state.clip_bounding_box);

    auto clip_path_alpha = MUST(Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, next_clipping_bbox.is_empty() ? Gfx::IntSize { 1, 1 } : next_clipping_bbox.size()));

    m_current_path.close_all_subpaths();
    Gfx::Painter clip_painter(*clip_path_alpha);
    clip_painter.translate(-next_clipping_bbox.location());
    clip_painter.fill_path(m_current_path, Color::Black, winding_rule);

    if (state().clipping_state.clip_path_alpha)
        apply_clip(*clip_path_alpha, next_clipping_bbox, *state().clipping_state.clip_path_alpha, state().clipping_state.clip_bounding_box);

    state().clipping_state.clip_path_alpha = move(clip_path_alpha);
    state().clipping_state.clip_bounding_box = next_clipping_bbox;
    state().clipping_state.has_own_clip = true;

    TRY(prepare_clipped_bitmap_painter());
    return {};
}

void Renderer::finalize_clip_before_graphics_state_restore()
{
    copy_current_clip_path_content_to_output();
}

PDFErrorOr<void> Renderer::restore_previous_clip_after_graphics_state_restore()
{
    if (state().clipping_state.clip_path_alpha)
        TRY(prepare_clipped_bitmap_painter());
    return {};
}

Review comment (suggestion):
Is there a reason not to use the AA painter here? It'd be consistent with the other uses of `.fill_path()` in LibPDF and given the alpha is the only thing you care about here it probably doesn't hurt to bump the samples a little.  

Code after:
{
    if (m_rendering_preferences.show_clipping_paths)
        m_clip_paths_to_show_for_debugging.append(m_current_path);

    if (!m_rendering_preferences.apply_clip)
        return {};

    // If the clip is a rectangle that is larger than the current clip's bounding box, we can ignore it.
    // This is important for performance, because many PDFs add a clip for the whole page before other clips,
    // and we don't want to allocate a whole-page bitmap for those.
    if (auto maybe_rect = m_current_path.as_rect(); maybe_rect.has_value()) {
        auto rect = Gfx::enclosing_int_rect(maybe_rect.value());
        if (rect.contains(state().clipping_state.clip_bounding_box))
            return {};
    }

    // About to set new clip; flush old one if needed.
    copy_current_clip_path_content_to_output();

    auto next_clipping_bbox = Gfx::enclosing_int_rect(m_current_path.bounding_box());
    next_clipping_bbox.intersect(state().clipping_state.clip_bounding_box);

    auto clip_path_alpha = TRY(Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, next_clipping_bbox.is_empty() ? Gfx::IntSize { 1, 1 } : next_clipping_bbox.size()));

    m_current_path.close_all_subpaths();
    Gfx::Painter clip_painter(*clip_path_alpha);
    Gfx::AntiAliasingPainter aa_clip_painter(clip_painter);
    clip_painter.translate(-next_clipping_bbox.location());
    aa_clip_painter.fill_path(m_current_path, Color::Black, winding_rule);

    if (state().clipping_state.clip_path_alpha)
        apply_clip(*clip_path_alpha, next_clipping_bbox, *state().clipping_state.clip_path_alpha, state().clipping_state.clip_bounding_box);

    state().clipping_state.clip_path_alpha = move(clip_path_alpha);
    state().clipping_state.clip_bounding_box = next_clipping_bbox;
    state().clipping_state.has_own_clip = true;

    TRY(prepare_clipped_bitmap_painter());
    return {};
}

void Renderer::finalize_clip_before_graphics_state_restore()
{
    copy_current_clip_path_content_to_output();
}

PDFErrorOr<void> Renderer::restore_previous_clip_after_graphics_state_restore()
{
    if (state().clipping_state.clip_path_alpha)
        TRY(prepare_clipped_bitmap_painter());
    return {};