Skip to content

Commit

Permalink
Refactor Matrix methods
Browse files Browse the repository at this point in the history
  • Loading branch information
sammycage committed Sep 13, 2024
1 parent cbfbaf8 commit 1c77e86
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 58 deletions.
18 changes: 2 additions & 16 deletions include/lunasvg.h
Original file line number Diff line number Diff line change
Expand Up @@ -333,21 +333,14 @@ class LUNASVG_API Matrix {
*/
Matrix& scale(float sx, float sy);

/**
* @brief Pre-rotates this matrix by the specified angle.
* @param angle The rotation angle in degrees.
* @return A reference to this matrix after rotation.
*/
Matrix& rotate(float angle);

/**
* @brief Pre-rotates this matrix by the specified angle around a point.
* @param angle The rotation angle in degrees.
* @param cx The x-coordinate of the center of rotation.
* @param cy The y-coordinate of the center of rotation.
* @return A reference to this matrix after rotation.
*/
Matrix& rotate(float angle, float cx, float cy);
Matrix& rotate(float angle, float cx = 0.f, float cy = 0.f);

/**
* @brief Pre-shears this matrix by the specified factors.
Expand Down Expand Up @@ -390,21 +383,14 @@ class LUNASVG_API Matrix {
*/
static Matrix scaled(float sx, float sy);

/**
* @brief Creates a rotation matrix with the specified angle.
* @param angle The rotation angle in degrees.
* @return A new rotation matrix.
*/
static Matrix rotated(float angle);

/**
* @brief Creates a rotation matrix with the specified angle around a point.
* @param angle The rotation angle in degrees.
* @param cx The x-coordinate of the center of rotation.
* @param cy The y-coordinate of the center of rotation.
* @return A new rotation matrix.
*/
static Matrix rotated(float angle, float cx, float cy);
static Matrix rotated(float angle, float cx = 0.f, float cy = 0.f);

/**
* @brief Creates a shearing matrix with the specified factors.
Expand Down
28 changes: 8 additions & 20 deletions source/graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,6 @@ Transform& Transform::scale(float sx, float sy)
return multiply(scaled(sx, sy));
}

Transform& Transform::rotate(float angle)
{
return multiply(rotated(angle));
}

Transform& Transform::rotate(float angle, float cx, float cy)
{
return multiply(rotated(angle, cx, cy));
Expand All @@ -93,11 +88,6 @@ Transform& Transform::postScale(float sx, float sy)
return postMultiply(scaled(sx, sy));
}

Transform& Transform::postRotate(float angle)
{
return postMultiply(rotated(angle));
}

Transform& Transform::postRotate(float angle, float cx, float cy)
{
return postMultiply(rotated(angle, cx, cy));
Expand Down Expand Up @@ -163,19 +153,17 @@ bool Transform::parse(const char* data, size_t length)
return plutovg_matrix_parse(&m_matrix, data, length);
}

Transform Transform::rotated(float angle)
{
plutovg_matrix_t matrix;
plutovg_matrix_init_rotate(&matrix, PLUTOVG_DEG2RAD(angle));
return matrix;
}

Transform Transform::rotated(float angle, float cx, float cy)
{
plutovg_matrix_t matrix;
plutovg_matrix_init_translate(&matrix, cx, cy);
plutovg_matrix_rotate(&matrix, PLUTOVG_DEG2RAD(angle));
plutovg_matrix_translate(&matrix, -cx, -cy);
if(cx == 0.f && cy == 0.f) {
plutovg_matrix_init_rotate(&matrix, PLUTOVG_DEG2RAD(angle));
} else {
plutovg_matrix_init_translate(&matrix, cx, cy);
plutovg_matrix_rotate(&matrix, PLUTOVG_DEG2RAD(angle));
plutovg_matrix_translate(&matrix, -cx, -cy);
}

return matrix;
}

Expand Down
7 changes: 2 additions & 5 deletions source/graphics.h
Original file line number Diff line number Diff line change
Expand Up @@ -281,15 +281,13 @@ class Transform {
Transform& multiply(const Transform& transform);
Transform& translate(float tx, float ty);
Transform& scale(float sx, float sy);
Transform& rotate(float angle);
Transform& rotate(float angle, float cx, float cy);
Transform& rotate(float angle, float cx = 0.f, float cy = 0.f);
Transform& shear(float shx, float shy);

Transform& postMultiply(const Transform& transform);
Transform& postTranslate(float tx, float ty);
Transform& postScale(float sx, float sy);
Transform& postRotate(float angle);
Transform& postRotate(float angle, float cx, float cy);
Transform& postRotate(float angle, float cx = 0.f, float cy = 0.f);
Transform& postShear(float shx, float shy);

Transform inverse() const;
Expand All @@ -310,7 +308,6 @@ class Transform {

static Transform translated(float tx, float ty);
static Transform scaled(float sx, float sy);
static Transform rotated(float angle);
static Transform rotated(float angle, float cx, float cy);
static Transform sheared(float shx, float shy);

Expand Down
10 changes: 0 additions & 10 deletions source/lunasvg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,6 @@ Matrix& Matrix::translate(float tx, float ty)
return multiply(translated(tx, ty));
}

Matrix& Matrix::rotate(float angle)
{
return multiply(rotated(angle));
}

Matrix& Matrix::rotate(float angle, float cx, float cy)
{
return multiply(rotated(angle, cx, cy));
Expand Down Expand Up @@ -222,11 +217,6 @@ Matrix Matrix::scaled(float sx, float sy)
return Transform::scaled(sx, sy);
}

Matrix Matrix::rotated(float angle)
{
return Transform::rotated(angle);
}

Matrix Matrix::rotated(float angle, float cx, float cy)
{
return Transform::rotated(angle, cx, cy);
Expand Down
9 changes: 2 additions & 7 deletions source/svgtextelement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -320,12 +320,9 @@ void SVGTextElement::render(SVGRenderState& state) const

std::u32string_view wholeText(m_text);
for(const auto& fragment : m_fragments) {
auto transform = newState.currentTransform() * Transform::rotated(fragment.angle, fragment.x, fragment.y);
auto text = wholeText.substr(fragment.offset, fragment.length);
auto origin = Point(fragment.x, fragment.y);
auto transform = newState.currentTransform();
transform.translate(origin.x, origin.y);
transform.rotate(fragment.angle);
transform.translate(-origin.x, -origin.y);

const auto& font = fragment.element->font();
if(newState.mode() == SVGRenderMode::Clipping) {
Expand All @@ -351,12 +348,10 @@ Rect SVGTextElement::boundingBox(bool includeStroke) const
for(const auto& fragment : m_fragments) {
const auto& font = fragment.element->font();
const auto& stroke = fragment.element->stroke();
auto fragmentTranform = Transform::rotated(fragment.angle, fragment.x, fragment.y);
auto fragmentRect = Rect(fragment.x, fragment.y - font.ascent(), fragment.width, fragment.element->font_size());
if(includeStroke && stroke.isRenderable())
fragmentRect.inflate(fragment.element->stroke_width() / 2.f);
auto fragmentTranform = Transform::translated(fragment.x, fragment.y);
fragmentTranform.rotate(fragment.angle);
fragmentTranform.translate(-fragment.x, -fragment.y);
boundingBox.unite(fragmentTranform.mapRect(fragmentRect));
}

Expand Down

0 comments on commit 1c77e86

Please sign in to comment.