Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add render child element #162

Merged
merged 1 commit into from
Apr 8, 2024

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
Copy link
Contributor Author

@sammycage, done

@sammycage sammycage merged commit 1c989d7 into sammycage:master Apr 8, 2024
3 checks passed
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