sammycage / lunasvg

lunasvg is a standalone SVG rendering library in C++
MIT License
818 stars 115 forks source link

Add render child element #162

Closed lolpie244 closed 3 months ago

sammycage commented 3 months ago

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 commented 3 months ago

@sammycage, done