Skip to content

Conversation

lolpie244
Copy link
Contributor

No description provided.

@lolpie244 lolpie244 mentioned this pull request Apr 8, 2024
@sammycage
Copy link
Owner

Incredible!! But please add some nullptr checks and zero division checks like this.

Bitmap DomElement::renderToBitmap(uint32_t width, uint32_t height, uint32_t backgroundColor) const
{
    if(m_element == nullptr || !m_element->box())
        return Bitmap();
    auto elementBounds = m_element->box()->map(m_element->box()->strokeBoundingBox());
    if(elementBounds.empty())
        return Bitmap();
    if(width == 0 && height == 0) {
        width = static_cast<std::uint32_t>(std::ceil(elementBounds.w));
        height = static_cast<std::uint32_t>(std::ceil(elementBounds.h));
    } else if(width != 0 && height == 0) {
        height = static_cast<std::uint32_t>(std::ceil(width * elementBounds.h / elementBounds.w));
    } else if(height != 0 && width == 0) {
        width = static_cast<std::uint32_t>(std::ceil(height * elementBounds.w / elementBounds.h));
    }

    const auto xScale = width / elementBounds.w;
    const auto yScale = height / elementBounds.h;

    Matrix matrix(xScale, 0, 0, yScale, -elementBounds.x * xScale, -elementBounds.y * yScale);
    Bitmap bitmap(width, height);
    bitmap.clear(backgroundColor);
    RenderState state(nullptr, RenderMode::Display);
    state.canvas = Canvas::create(bitmap.data(), bitmap.width(), bitmap.height(), bitmap.stride());
    state.transform = Transform(matrix);
    m_element->box()->render(state);
    return bitmap;
}

@lolpie244 lolpie244 force-pushed the Add-child-element-render branch from e03f683 to df5bbe3 Compare April 8, 2024 20:37
@lolpie244
Copy link
Contributor Author

@sammycage, done

@sammycage sammycage merged commit 1c989d7 into sammycage:master Apr 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants