22
33#include < scratchcpp/iengine.h>
44#include < scratchcpp/costume.h>
5+ #include < QtSvg/QSvgRenderer>
6+ #include < qnanopainter.h>
57
68#include " renderedtarget.h"
79#include " targetpainter.h"
@@ -80,6 +82,8 @@ void RenderedTarget::loadCostume(Costume *costume)
8082 m_imageChanged = true ;
8183
8284 if (costume->dataFormat () == " svg" ) {
85+ if (costume != m_costume)
86+ m_svgRenderer.load (QByteArray::fromRawData (static_cast <const char *>(costume->data ()), costume->dataSize ()));
8387 }
8488
8589 m_costume = costume;
@@ -202,6 +206,8 @@ void RenderedTarget::doLoadCostume()
202206 Target *target = scratchTarget ();
203207
204208 if (m_costume->dataFormat () == " svg" ) {
209+ QRectF rect = m_svgRenderer.viewBoxF ();
210+ calculateSize (target, rect.width (), rect.height ());
205211 } else {
206212 m_bitmapBuffer.open (QBuffer::WriteOnly);
207213 m_bitmapBuffer.write (static_cast <const char *>(m_costume->data ()), m_costume->dataSize ());
@@ -217,6 +223,41 @@ void RenderedTarget::doLoadCostume()
217223 m_costumeMutex.unlock ();
218224}
219225
226+ void RenderedTarget::paintSvg (QNanoPainter *painter)
227+ {
228+ Q_ASSERT (painter);
229+ QOpenGLContext *context = QOpenGLContext::currentContext ();
230+ Q_ASSERT (context);
231+
232+ if (!context)
233+ return ;
234+
235+ QOffscreenSurface surface;
236+ surface.setFormat (context->format ());
237+ surface.create ();
238+ Q_ASSERT (surface.isValid ());
239+
240+ QSurface *oldSurface = context->surface ();
241+ context->makeCurrent (&surface);
242+
243+ const QRectF drawRect (0 , 0 , width (), height ());
244+ const QSize drawRectSize = drawRect.size ().toSize ();
245+
246+ /* QOpenGLFramebufferObjectFormat fboFormat;
247+ fboFormat.setSamples(16);
248+ fboFormat.setAttachment(QOpenGLFramebufferObject::CombinedDepthStencil);*/
249+
250+ QOpenGLPaintDevice device (drawRectSize);
251+ QPainter qPainter;
252+ qPainter.begin (&device);
253+ qPainter.setRenderHints (QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
254+ m_svgRenderer.render (&qPainter, drawRect);
255+ qPainter.end ();
256+
257+ context->doneCurrent ();
258+ context->makeCurrent (oldSurface);
259+ }
260+
220261void RenderedTarget::calculateSize (Target *target, double costumeWidth, double costumeHeight)
221262{
222263 if (m_costume) {
@@ -258,3 +299,11 @@ bool RenderedTarget::mirrorHorizontally() const
258299{
259300 return m_mirrorHorizontally;
260301}
302+
303+ bool RenderedTarget::isSvg () const
304+ {
305+ if (!m_costume)
306+ return false ;
307+
308+ return (m_costume->dataFormat () == " svg" );
309+ }
0 commit comments