-
-
Notifications
You must be signed in to change notification settings - Fork 99
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add SVG support + basic MidiKeyboard example #141
base: develop
Are you sure you want to change the base?
Changes from 3 commits
fa450e2
23cc7da
7505c9b
cc70a9d
9b1867b
9308d52
b901c49
f82df26
60ab304
54013df
0b05f45
a27c8d8
abbc504
d83aaf2
21f3696
9510c10
2d9b13d
6ea0cc3
5bffb9f
cb5517b
07c6585
86c40bc
b34b29c
116aeff
d304efb
ab8b5f1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
#define DGL_IMAGE_BASE_HPP_INCLUDED | ||
|
||
#include "Geometry.hpp" | ||
#include "SVG.hpp" | ||
|
||
START_NAMESPACE_DGL | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* DISTRHO Plugin Framework (DPF) | ||
* Copyright (C) 2012-2019 Filipe Coelho <falktx@falktx.com> | ||
* | ||
* Permission to use, copy, modify, and/or distribute this software for any purpose with | ||
* or without fee is hereby granted, provided that the above copyright notice and this | ||
* permission notice appear in all copies. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD | ||
* TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN | ||
* NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL | ||
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER | ||
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN | ||
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
*/ | ||
|
||
#ifndef DGL_SVG_HPP_INCLUDED | ||
#define DGL_SVG_HPP_INCLUDED | ||
|
||
#include "src/nanosvg/nanosvg.h" | ||
#include "Geometry.hpp" | ||
|
||
struct NSVGrasterizer; | ||
|
||
START_NAMESPACE_DGL | ||
|
||
/** | ||
* Utility class for loading SVGs. | ||
*/ | ||
class SVG | ||
{ | ||
public: | ||
|
||
/** | ||
Constructor for a null SVG. | ||
*/ | ||
SVG(); | ||
|
||
/** | ||
Destructor. | ||
*/ | ||
~SVG(); | ||
|
||
/** | ||
Load SVG data from memory. | ||
@note @a rawData must remain valid for the lifetime of this SVG. | ||
*/ | ||
void loadFromMemory(const char* const rawData, | ||
const uint dataSize, | ||
const float scaling = 1.0f) noexcept; | ||
|
||
/** | ||
Check if this SVG is valid. | ||
*/ | ||
const Size<uint>& getSize() const noexcept; | ||
|
||
/** | ||
Get the RGBA data of the rasterized SVG. | ||
*/ | ||
const unsigned char* getRGBAData() const noexcept; | ||
|
||
/** | ||
Check if this SVG is valid. | ||
*/ | ||
bool isValid() const noexcept; | ||
|
||
private: | ||
Size<uint> fSize; | ||
unsigned char* fRGBAData; | ||
|
||
DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(SVG) | ||
}; | ||
|
||
END_NAMESPACE_DGL | ||
|
||
#endif |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -92,6 +92,13 @@ void Image::loadFromMemory(const char* const rawData, | |
fIsReady = false; | ||
} | ||
|
||
void Image::loadFromSVG(const SVG& svg) noexcept | ||
{ | ||
DISTRHO_SAFE_ASSERT_RETURN(svg.isValid(),) | ||
|
||
loadFromMemory((const char*)svg.getRGBAData(), svg.getSize(), GL_RGBA); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is pretty bad, as the RGBA data becomes invalid when the SVG is freed. The caller needs to keep both the SVG and the Image 'alive'. What should we do instead? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add a parameter to allocate memory. if true, do malloc and copy data. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems to me like this would be a pretty wasteful copy. I guess it could be avoided if there was an object that combined both SVG and Image. |
||
} | ||
|
||
GLenum Image::getFormat() const noexcept | ||
{ | ||
return fFormat; | ||
|
@@ -114,8 +121,7 @@ Image& Image::operator=(const Image& image) noexcept | |
|
||
void Image::_drawAt(const Point<int>& pos) | ||
{ | ||
if (fTextureId == 0 || ! isValid()) | ||
return; | ||
DISTRHO_SAFE_ASSERT_RETURN(fTextureId != 0 && isValid(), ) | ||
|
||
glEnable(GL_TEXTURE_2D); | ||
glBindTexture(GL_TEXTURE_2D, fTextureId); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* DISTRHO Plugin Framework (DPF) | ||
* Copyright (C) 2012-2019 Filipe Coelho <falktx@falktx.com> | ||
* | ||
* Permission to use, copy, modify, and/or distribute this software for any purpose with | ||
* or without fee is hereby granted, provided that the above copyright notice and this | ||
* permission notice appear in all copies. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD | ||
* TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN | ||
* NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL | ||
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER | ||
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN | ||
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
*/ | ||
|
||
#include "SVG.hpp" | ||
|
||
#define NANOSVG_IMPLEMENTATION | ||
#include "nanosvg/nanosvg.h" | ||
|
||
#define NANOSVGRAST_IMPLEMENTATION | ||
#include "nanosvg/nanosvgrast.h" | ||
|
||
|
||
START_NAMESPACE_DGL | ||
|
||
SVG::SVG() | ||
: fSize(), | ||
fRGBAData(nullptr) | ||
{ | ||
} | ||
|
||
void SVG::loadFromMemory(const char* const rawData, const uint dataSize, const float scaling) noexcept | ||
{ | ||
DISTRHO_SAFE_ASSERT_RETURN(rawData != nullptr, ) | ||
DISTRHO_SAFE_ASSERT_RETURN(scaling > 0.0f, ) | ||
|
||
NSVGrasterizer* rasterizer = nsvgCreateRasterizer(); | ||
|
||
// nsvgParse modifies the input data, so we must use a temporary buffer | ||
const size_t bufferSize = dataSize * 4; | ||
|
||
char tmpBuffer[bufferSize]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this might segfault if stack size becomes too big. |
||
strncpy(tmpBuffer, rawData, bufferSize); | ||
|
||
const float dpi = 96; | ||
|
||
NSVGimage* image = nsvgParse(tmpBuffer, "px", dpi); | ||
|
||
DISTRHO_SAFE_ASSERT_RETURN(image != nullptr, ) | ||
|
||
const uint scaledWidth = image->width * scaling; | ||
const uint scaledHeight = image->height * scaling; | ||
|
||
DISTRHO_SAFE_ASSERT_RETURN(scaledWidth > 0 && scaledHeight > 0, ) | ||
|
||
fRGBAData = (unsigned char*)malloc(scaledWidth * scaledHeight * 4); | ||
|
||
nsvgRasterize(rasterizer, image, 0, 0, scaling, fRGBAData, scaledWidth, scaledHeight, scaledWidth * 4); | ||
|
||
fSize.setSize(scaledWidth, scaledHeight); | ||
|
||
nsvgDelete(image); | ||
nsvgDeleteRasterizer(rasterizer); | ||
} | ||
|
||
SVG::~SVG() | ||
{ | ||
free(fRGBAData); | ||
} | ||
|
||
const Size<uint>& SVG::getSize() const noexcept | ||
{ | ||
return fSize; | ||
} | ||
|
||
const unsigned char* SVG::getRGBAData() const noexcept | ||
{ | ||
return fRGBAData; | ||
} | ||
|
||
bool SVG::isValid() const noexcept | ||
{ | ||
return (fRGBAData != nullptr && fSize.isValid()); | ||
} | ||
|
||
END_NAMESPACE_DGL | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
Copyright (c) 2013-14 Mikko Mononen memon@inside.org | ||
|
||
This software is provided 'as-is', without any express or implied | ||
warranty. In no event will the authors be held liable for any damages | ||
arising from the use of this software. | ||
|
||
Permission is granted to anyone to use this software for any purpose, | ||
including commercial applications, and to alter it and redistribute it | ||
freely, subject to the following restrictions: | ||
|
||
1. The origin of this software must not be misrepresented; you must not | ||
claim that you wrote the original software. If you use this software | ||
in a product, an acknowledgment in the product documentation would be | ||
appreciated but is not required. | ||
2. Altered source versions must be plainly marked as such, and must not be | ||
misrepresented as being the original software. | ||
3. This notice may not be removed or altered from any source distribution. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This include is not really needed