-
-
Notifications
You must be signed in to change notification settings - Fork 98
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
Open
pdesaulniers
wants to merge
26
commits into
DISTRHO:develop
Choose a base branch
from
wolf-plugins:svg
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
fa450e2
Initial work on SVG support
pdesaulniers 23cc7da
Finish basic implementation of SVG support + MidiKeyboard example
pdesaulniers 7505c9b
Update MidiKeyboard's README
pdesaulniers cc70a9d
Avoid constexpr and add leak detector
pdesaulniers 9b1867b
Avoid comparison between signed and unsigned integers
pdesaulniers 9308d52
Avoid potential memory leak
pdesaulniers b901c49
Get rid of bufferSize variable
pdesaulniers f82df26
Bring back the comment mentioning the need for a temporary buffer
pdesaulniers 60ab304
Cleanup
pdesaulniers 54013df
Use static vars for UI width/height
pdesaulniers 0b05f45
SVG cleanup and subtle UI polish
pdesaulniers a27c8d8
Allow playing notes using the computer keyboard
pdesaulniers abbc504
Use regular int for keyboardDeltaWidth
pdesaulniers d83aaf2
Add keycodes support
pdesaulniers 21f3696
Better documentation for PuglKeyCodes; change keycode acronym to KC
pdesaulniers 9510c10
Merge branch 'keycodes-support' of https://github.com/pdesaulniers/DP…
pdesaulniers 2d9b13d
Update piano widget to use keycodes
pdesaulniers 6ea0cc3
Remove superfluous notes & fix build
pdesaulniers 5bffb9f
Fix comment
pdesaulniers cb5517b
Add some comments
pdesaulniers 07c6585
Adjustments to keycode support
pdesaulniers 86c40bc
Tweak indentation
pdesaulniers b34b29c
Expose keycodes in Base.hpp
pdesaulniers 116aeff
Merge branch 'keycodes-support' into svg
pdesaulniers d304efb
Use new KeyCode enum, instead of PuglKeyCodes
pdesaulniers ab8b5f1
Don't include pugl.h
pdesaulniers File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* DISTRHO Plugin Framework (DPF) | ||
* Copyright (C) 2012-2019 Filipe Coelho <[email protected]> | ||
* | ||
* 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 "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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
* DISTRHO Plugin Framework (DPF) | ||
* Copyright (C) 2012-2019 Filipe Coelho <[email protected]> | ||
* | ||
* 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, ) | ||
|
||
if (fRGBAData != nullptr) | ||
{ | ||
free(fRGBAData); | ||
fRGBAData = nullptr; | ||
} | ||
|
||
// nsvgParse modifies the input data, so we must use a temporary buffer | ||
char* tmpBuffer = (char*)malloc(dataSize); | ||
|
||
DISTRHO_SAFE_ASSERT_RETURN(tmpBuffer != nullptr, ) | ||
|
||
strncpy(tmpBuffer, rawData, dataSize); | ||
tmpBuffer[dataSize - 1] = '\0'; | ||
|
||
const float dpi = 96; | ||
|
||
NSVGimage* image = nsvgParse(tmpBuffer, "px", dpi); | ||
|
||
free(tmpBuffer); | ||
|
||
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); | ||
|
||
NSVGrasterizer* rasterizer = nsvgCreateRasterizer(); | ||
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 | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 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 comment
The 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 comment
The 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.