1 #include "thorvg_renderer.hpp"
2 #include "math/vec2d.hpp"
3 #include "shapes/paint/color.hpp"
7 TvgRenderPath::TvgRenderPath()
9 this->m_Shape = tvg::Shape::gen().release();
12 void TvgRenderPath::fillRule(FillRule value)
16 case FillRule::evenOdd:
17 m_Shape->fill(tvg::FillRule::EvenOdd);
19 case FillRule::nonZero:
24 Vec2D applyTransform(const Vec2D &vec, const Mat2D &mat)
26 tvg::Matrix m = {1, 0, 0, 0, 1, 0, 0, 0, 1};
35 ret[0] = round(vec[0] * m.e11 + vec[1] * m.e12 + m.e13);
36 ret[1] = round(vec[0] * m.e21 + vec[1] * m.e22 + m.e23);
41 void TvgRenderPath::addRenderPath(RenderPath* path, const Mat2D& transform)
43 auto m_PathType = reinterpret_cast<TvgRenderPath*>(path)->m_PathType;
44 auto m_PathPoints = reinterpret_cast<TvgRenderPath*>(path)->m_PathPoints;
45 Vec2D vec1, vec2, vec3, vecOut1, vecOut2, vecOut3;
48 for (size_t i = 0; i < m_PathType.size(); i++)
50 PathCommand type = m_PathType[i];
53 case PathCommand::MoveTo:
54 vecOut1 = applyTransform(m_PathPoints[index], transform);
55 m_Shape->moveTo(vecOut1[0], vecOut1[1]);
58 case PathCommand::LineTo:
59 vecOut1 = applyTransform(m_PathPoints[index], transform);
60 m_Shape->lineTo(vecOut1[0], vecOut1[1]);
63 case PathCommand::CubicTo:
64 vecOut1 = applyTransform(m_PathPoints[index], transform);
65 vecOut2 = applyTransform(m_PathPoints[index + 1], transform);
66 vecOut3 = applyTransform(m_PathPoints[index + 2], transform);
67 m_Shape->cubicTo(vecOut1[0], vecOut1[1],
68 vecOut2[0], vecOut2[1],
69 vecOut3[0], vecOut3[1]);
72 case PathCommand::Close:
79 void TvgRenderPath::reset()
86 void TvgRenderPath::moveTo(float x, float y)
88 m_PathType.push_back(PathCommand::MoveTo);
89 m_PathPoints.push_back({x, y});
92 void TvgRenderPath::lineTo(float x, float y)
94 m_PathType.push_back(PathCommand::LineTo);
95 m_PathPoints.push_back({x, y});
98 void TvgRenderPath::cubicTo(float ox, float oy, float ix, float iy, float x, float y)
100 m_PathType.push_back(PathCommand::CubicTo);
101 m_PathPoints.push_back({ox, oy});
102 m_PathPoints.push_back({ix, iy});
103 m_PathPoints.push_back({x, y});
106 void TvgRenderPath::close()
108 m_PathType.push_back(PathCommand::Close);
111 void TvgRenderer::save()
115 void TvgRenderer::restore()
119 void TvgRenderer::transform(const Mat2D& transform)
121 m_Transform = transform;
124 void TvgRenderer::drawPath(RenderPath* path, RenderPaint* paint)
126 Matrix m = {m_Transform[0], 0, m_Transform[4], 0, m_Transform[3], m_Transform[5], 0, 0, 1};
128 auto renderPath = reinterpret_cast<TvgRenderPath*>(path);
129 auto shape = reinterpret_cast<TvgRenderPath*>(path)->shape();
132 auto tvgPaint = reinterpret_cast<TvgRenderPaint*>(paint)->paint();
134 if (tvgPaint->isFill)
136 shape->fill(tvgPaint->fillColor[0], tvgPaint->fillColor[1], tvgPaint->fillColor[2], tvgPaint->fillColor[3]);
139 if (tvgPaint->isStroke)
141 shape->stroke(tvgPaint->strokeColor[0], tvgPaint->strokeColor[1], tvgPaint->strokeColor[2], tvgPaint->strokeColor[3]);
142 shape->stroke(tvgPaint->cap);
143 shape->stroke(tvgPaint->join);
144 if (tvgPaint->thickness != 0)
146 shape->stroke(tvgPaint->thickness);
150 if (!renderPath->getPushed())
152 m_Canvas->push(unique_ptr<Shape>(shape));
153 renderPath->setPushed(true);
157 m_Canvas->update(shape);
161 void TvgRenderer::clipPath(RenderPath* path)
166 TvgRenderPaint::TvgRenderPaint()
171 void TvgRenderPaint::style(RenderPaintStyle style)
175 case RenderPaintStyle::fill:
176 m_Paint.style = RenderPaintStyle::fill;
177 m_Paint.isFill = true;
179 case RenderPaintStyle::stroke:
180 m_Paint.style = RenderPaintStyle::stroke;
181 m_Paint.isStroke = true;
185 void TvgRenderPaint::color(unsigned int value)
187 int b = value >> 0 & 255;
188 int g = value >> 8 & 255;
189 int r = value >> 16 & 255;
190 int a = value >> 24 & 255;
192 if (m_Paint.style == RenderPaintStyle::fill)
194 m_Paint.fillColor[0] = r;
195 m_Paint.fillColor[1] = g;
196 m_Paint.fillColor[2] = b;
197 m_Paint.fillColor[3] = a;
199 else if (m_Paint.style == RenderPaintStyle::stroke)
201 m_Paint.strokeColor[0] = r;
202 m_Paint.strokeColor[1] = g;
203 m_Paint.strokeColor[2] = b;
204 m_Paint.strokeColor[3] = a;
208 void TvgRenderPaint::thickness(float value)
210 m_Paint.thickness = value;
213 void TvgRenderPaint::join(StrokeJoin value)
217 case rive::StrokeJoin::round:
218 m_Paint.join = tvg::StrokeJoin::Round;
220 case rive::StrokeJoin::bevel:
221 m_Paint.join = tvg::StrokeJoin::Bevel;
223 case rive::StrokeJoin::miter:
224 m_Paint.join = tvg::StrokeJoin::Miter;
229 void TvgRenderPaint::cap(StrokeCap value)
233 case rive::StrokeCap::butt:
234 m_Paint.cap = tvg::StrokeCap::Butt;
236 case rive::StrokeCap::round:
237 m_Paint.cap = tvg::StrokeCap::Round;
239 case rive::StrokeCap::square:
240 m_Paint.cap = tvg::StrokeCap::Square;
245 void TvgRenderPaint::linearGradient(float sx, float sy, float ex, float ey)
250 void TvgRenderPaint::radialGradient(float sx, float sy, float ex, float ey)
254 void TvgRenderPaint::addStop(unsigned int color, float stop)
258 void TvgRenderPaint::completeGradient()
263 void TvgRenderPaint::blendMode(BlendMode value)
270 RenderPath* makeRenderPath() { return new TvgRenderPath();}
271 RenderPaint* makeRenderPaint() { return new TvgRenderPaint();}