Skip to content

Commit

Permalink
Add render child element
Browse files Browse the repository at this point in the history
  • Loading branch information
lolpie244 committed Apr 8, 2024
1 parent d1eec96 commit e03f683
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
2 changes: 2 additions & 0 deletions include/lunasvg.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@ class LUNASVG_API DomElement {
*/
Element* get() { return m_element; }

Bitmap renderToBitmap(std::uint32_t width, std::uint32_t height, std::uint32_t backgroundColor = 0x00000000) const;

private:
Element* m_element = nullptr;
};
Expand Down
21 changes: 21 additions & 0 deletions source/lunasvg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,27 @@ Matrix DomElement::getAbsoluteTransform() const
return getLocalTransform();
}

Bitmap DomElement::renderToBitmap(std::uint32_t width, std::uint32_t height, std::uint32_t backgroundColor) const {
auto elementBounds = m_element->box()->map(m_element->box()->strokeBoundingBox());
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));
}

Matrix matrix(width / elementBounds.w, 0, 0, height / elementBounds.h, -elementBounds.x * (width / elementBounds.w), -elementBounds.y * (height / elementBounds.h));
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;
}

std::unique_ptr<Document> Document::loadFromFile(const std::string& filename)
{
std::ifstream fs;
Expand Down

0 comments on commit e03f683

Please sign in to comment.