Skip to content

Commit

Permalink
Add DomElement::render
Browse files Browse the repository at this point in the history
  • Loading branch information
sammycage committed Aug 4, 2024
1 parent 438ca4e commit be40aed
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
16 changes: 15 additions & 1 deletion include/lunasvg.h
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,21 @@ 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;
/**
* @brief Renders the document to a bitmap
* @param matrix - the current transformation matrix
* @param bitmap - target image on which the content will be drawn
*/
void render(Bitmap bitmap, const Matrix& matrix = Matrix{}) const;

/**
* @brief renderToBitmap
* @param width
* @param height
* @param backgroundColor
* @return
*/
Bitmap renderToBitmap(std::uint32_t width, std::uint32_t height, std::uint32_t backgroundColor = 0x00000000) const;

private:
Element* m_element = nullptr;
Expand Down
15 changes: 11 additions & 4 deletions source/lunasvg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,16 @@ Matrix DomElement::getAbsoluteTransform() const
return transform;
}

void DomElement::render(Bitmap bitmap, const Matrix& matrix) const
{
if(m_element == nullptr || !m_element->box())
return;
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);
}

Bitmap DomElement::renderToBitmap(std::uint32_t width, std::uint32_t height, std::uint32_t backgroundColor) const
{
if(m_element == nullptr || !m_element->box())
Expand All @@ -366,10 +376,7 @@ Bitmap DomElement::renderToBitmap(std::uint32_t width, std::uint32_t height, std
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);
render(bitmap, matrix);
return bitmap;
}

Expand Down

0 comments on commit be40aed

Please sign in to comment.