Skip to content

Commit

Permalink
Change API to always use .render and update README
Browse files Browse the repository at this point in the history
  • Loading branch information
sudara committed Dec 23, 2023
1 parent 50080ce commit 713b3e1
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 73 deletions.
156 changes: 98 additions & 58 deletions README.md

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions melatonin/blur_demo_component.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,19 +128,19 @@ namespace melatonin
g.fillPath (innerShadowedPath);
innerShadow.render (g, innerShadowedPath);

strokedDropShadow.renderStroked (g, strokedDropPath, juce::PathStrokeType (6));
strokedDropShadow.render (g, strokedDropPath, juce::PathStrokeType (6));
g.strokePath (strokedDropPath, juce::PathStrokeType (6));

g.strokePath (strokedInnerPath, juce::PathStrokeType (6));
strokedInnerShadow.renderStroked (g, strokedInnerPath, juce::PathStrokeType (6));
strokedInnerShadow.render (g, strokedInnerPath, juce::PathStrokeType (6));

g.setColour (juce::Colours::white);
g.setFont (juce::Font (50).boldened());
textDropShadow.renderText (g, "drop", textBounds.toFloat(), juce::Justification::left);
textDropShadow.render (g, "drop", textBounds, juce::Justification::left);
g.drawText ("drop", textBounds, juce::Justification::left);

g.drawText ("inner", textBounds, juce::Justification::centredRight);
textInnerShadow.renderText (g, "inner", textBounds.toFloat(), juce::Justification::centredRight);
textInnerShadow.render (g, "inner", textBounds.toFloat(), juce::Justification::centredRight);

g.setFont (juce::Font (16));
auto labels = juce::StringArray ("radius", "spread", "offsetX", "offsetY", "opacity");
Expand Down
14 changes: 12 additions & 2 deletions melatonin/internal/cached_shadows.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ namespace melatonin::internal
renderInternal (g);
}

void renderStroked (juce::Graphics& g, const juce::Path& newPath, const juce::PathStrokeType& newType, bool lowQuality = false)
void render (juce::Graphics& g, const juce::Path& newPath, const juce::PathStrokeType& newType, bool lowQuality = false)
{
stroked = true;
setScale (g, lowQuality);
Expand All @@ -62,7 +62,7 @@ namespace melatonin::internal
renderInternal (g);
}

void renderText (juce::Graphics& g, const juce::String& text, const juce::Rectangle<float>& area, juce::Justification justification)
void render (juce::Graphics& g, const juce::String& text, const juce::Rectangle<float>& area, juce::Justification justification)
{
setScale (g, false);

Expand All @@ -85,6 +85,16 @@ namespace melatonin::internal
// need to still render a path here, which path?
}

void render (juce::Graphics& g, const juce::String& text, const juce::Rectangle<int>& area, juce::Justification justification)
{
render (g, text, area.toFloat(), justification);
}

void render (juce::Graphics& g, const juce::String& text, int x, int y, int width, int height, juce::Justification justification)
{
render (g, text, juce::Rectangle<int> (x, y, width, height).toFloat(), justification);
}

void setRadius (size_t radius, size_t index = 0)
{
if (index < renderedSingleChannelShadows.size())
Expand Down
2 changes: 1 addition & 1 deletion melatonin_blur.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ BEGIN_JUCE_MODULE_DECLARATION
ID: melatonin_blur
vendor: Sudara
version: 1.2.0
version: 1.3.0
name: Optimized CPU vector blurring and JUCE drop shadowing with tests and benchmarks
description: Blurry Life
license: MIT
Expand Down
10 changes: 5 additions & 5 deletions tests/stroked_path.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ TEST_CASE ("Melatonin Blur Stroked Path")
g.fillAll (juce::Colours::white);
melatonin::DropShadow shadow (juce::Colours::black, 2);
auto strokeType = juce::PathStrokeType (3.0f);
shadow.renderStroked (g, p, strokeType);
shadow.render (g, p, strokeType);
g.strokePath (p, strokeType);

save_test_image (result, "stroked_path.png");
Expand All @@ -69,20 +69,20 @@ TEST_CASE ("Melatonin Blur Stroked Path")
g.fillAll (juce::Colours::white);
melatonin::DropShadow shadow (juce::Colours::black, 2);
auto strokeType = juce::PathStrokeType (3.0f);
shadow.renderStroked (g, p, strokeType);
shadow.render (g, p, strokeType);

// erase the image, render the shadow again
g.fillAll (juce::Colours::white);
strokeType = juce::PathStrokeType (0.0f);
shadow.renderStroked (g, p, strokeType);
shadow.render (g, p, strokeType);

// there should be no more shadow (and we didn't render the path, so pure white)
CHECK (filledBounds (result).isEmpty());

// erase the image, render the shadow again
g.fillAll (juce::Colours::white);
strokeType = juce::PathStrokeType (1.0f);
shadow.renderStroked (g, p, strokeType);
shadow.render (g, p, strokeType);

CHECK (!filledBounds (result).isEmpty());
}
Expand All @@ -96,7 +96,7 @@ TEST_CASE ("Melatonin Blur Stroked Path")
CHECK (getPixel (result, 2, 6) == "FF000000");
CHECK (getPixel (result, 3, 5) == "FF000000");
CHECK (getPixel (result, 4, 4) == "FF000000");
shadow.renderStroked (g, p, strokeType);
shadow.render (g, p, strokeType);
CHECK (getPixel (result, 2, 6) != "FF000000");
CHECK (getPixel (result, 3, 5) != "FF000000");
CHECK (getPixel (result, 4, 4) != "FF000000");
Expand Down
16 changes: 13 additions & 3 deletions tests/text_shadow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ TEST_CASE ("Melatonin Blur Text Shadow")

// top middle is close to black
auto topMiddle = result.getPixelAt (4, 4);
CHECK (getPixel (result, 4, 0) == "FF191919");
CHECK (getPixel (result, 4, 0) != "FFFFFFFF");
CHECK (topMiddle.getRed() == topMiddle.getGreen());
}

Expand All @@ -50,7 +50,7 @@ TEST_CASE ("Melatonin Blur Text Shadow")
g.fillAll (juce::Colours::white);
melatonin::DropShadow shadow (juce::Colours::black, 3);
g.setColour (juce::Colours::black);
shadow.renderText (g, "O", { 0, 0, 9, 9 }, juce::Justification::centred);
shadow.render (g, "O", 0, 0, 9, 9, juce::Justification::centred);
g.drawText ("O", 0, 0, 9, 9, juce::Justification::centred, false);

// middle of image is no longer white
Expand All @@ -64,11 +64,21 @@ TEST_CASE ("Melatonin Blur Text Shadow")
melatonin::InnerShadow shadow (juce::Colours::red, 1);
g.setColour (juce::Colours::black);
g.drawText ("O", 0, 0, 9, 9, juce::Justification::centred, false);
shadow.renderText (g, "O", { 0, 0, 9, 9 }, juce::Justification::centred);
shadow.render (g, "O", 0, 0, 9, 9, juce::Justification::centred);

// top middle has more red than green
auto topMiddle = result.getPixelAt (4, 0);
CHECK (topMiddle.getRed() > topMiddle.getGreen());
save_test_image (result, "text_inner_shadow.png");
}

SECTION ("accepts permutations of rectangle")
{
melatonin::InnerShadow shadow (juce::Colours::red, 1);
shadow.render (g, "O", 0, 0, 9, 9, juce::Justification::centred);
juce::Rectangle<int> intRect (0, 0, 9, 9);
shadow.render (g, "O", intRect, juce::Justification::centred);
juce::Rectangle<float> floatRect (0, 0, 9, 9);
shadow.render (g, "O", floatRect, juce::Justification::centred);
}
}

0 comments on commit 713b3e1

Please sign in to comment.