Skip to content

Commit

Permalink
Fix crash in drawing separators
Browse files Browse the repository at this point in the history
Separators with 0 start or end fractions were crashing in `LinearGradientPaint` implementation that doesn't support identical consecutive fractions.

This was happening in the ribbon - for displayed contextual tasks, as well as after resizing the ribbon to kick in the scroll mode for regular task tab buttons.
  • Loading branch information
kirill-grouchnikov committed Mar 17, 2021
1 parent a4dc020 commit 4288406
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 8 deletions.
Binary file modified drop/4.0-SNAPSHOT/core/radiance-substance-4.0-SNAPSHOT.jar
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -249,18 +249,58 @@ public static void paintSeparator(Component c, Graphics g, SubstanceColorScheme
float gradEndFraction = (float) (height - gradEnd) / height;
float regularX = Math.max(0, width / 2.0f - borderStrokeWidth);

// Dynamically create the array of stop fractions and corresponding
// colors. If the start fraction is 0, we need to skip the 0-opacity
// first stop, as identical fractions in LinearGradientPaint lead to
// a crash. The same applies to end fraction 0.
int stopsCount = ((gradStartFraction > 0.0f) ? 1 : 0) + 2 +
((gradEndFraction < 1.0f) ? 1 : 0);
float[] stops = new float[stopsCount];
Color[] colors = new Color[stopsCount];
int stopIndex = 0;
if (gradStartFraction > 0.0f) {
stops[stopIndex] = 0.0f;
colors[stopIndex] = primaryZero;
stopIndex++;
}
stops[stopIndex] = gradStartFraction;
colors[stopIndex] = primary;
stopIndex++;
stops[stopIndex] = gradEndFraction;
colors[stopIndex] = primary;
stopIndex++;
if (gradEndFraction < 1.0f) {
stops[stopIndex] = 1.0f;
colors[stopIndex] = primaryZero;
}

LinearGradientPaint primaryPaint = new LinearGradientPaint(0, 0, 0, height,
new float[] { 0.0f, gradStartFraction, gradEndFraction, 1.0f },
new Color[] { primaryZero, primary, primary, primaryZero });
stops, colors);
graphics.setPaint(primaryPaint);
graphics.draw(new Line2D.Float(regularX, 0, regularX, height));

if (hasShadow) {
float shadowX = regularX + borderStrokeWidth;

stopIndex = 0;
if (gradStartFraction > 0.0f) {
stops[stopIndex] = 0.0f;
colors[stopIndex] = secondaryZero;
stopIndex++;
}
stops[stopIndex] = gradStartFraction;
colors[stopIndex] = secondary;
stopIndex++;
stops[stopIndex] = gradEndFraction;
colors[stopIndex] = secondary;
stopIndex++;
if (gradEndFraction < 1.0f) {
stops[stopIndex] = 1.0f;
colors[stopIndex] = secondaryZero;
}

LinearGradientPaint secondaryPaint = new LinearGradientPaint(0, 0, 0, height,
new float[] { 0.0f, gradStartFraction, gradEndFraction, 1.0f },
new Color[] { secondaryZero, secondary, secondary, secondaryZero });
stops, colors);
graphics.setPaint(secondaryPaint);
graphics.draw(new Line2D.Float(shadowX, 0, shadowX, height));
}
Expand All @@ -274,18 +314,58 @@ public static void paintSeparator(Component c, Graphics g, SubstanceColorScheme
float gradEndFraction = (float) (width - gradEnd) / width;
float regularY = Math.max(0, height / 2.0f - borderStrokeWidth);

// Dynamically create the array of stop fractions and corresponding
// colors. If the start fraction is 0, we need to skip the 0-opacity
// first stop, as identical fractions in LinearGradientPaint lead to
// a crash. The same applies to end fraction 0.
int stopsCount = ((gradStartFraction > 0.0f) ? 1 : 0) + 2 +
((gradEndFraction < 1.0f) ? 1 : 0);
float[] stops = new float[stopsCount];
Color[] colors = new Color[stopsCount];
int stopIndex = 0;
if (gradStartFraction > 0.0f) {
stops[stopIndex] = 0.0f;
colors[stopIndex] = primaryZero;
stopIndex++;
}
stops[stopIndex] = gradStartFraction;
colors[stopIndex] = primary;
stopIndex++;
stops[stopIndex] = gradEndFraction;
colors[stopIndex] = primary;
stopIndex++;
if (gradEndFraction < 1.0f) {
stops[stopIndex] = 1.0f;
colors[stopIndex] = primaryZero;
}

LinearGradientPaint primaryPaint = new LinearGradientPaint(0, 0, width, 0,
new float[] { 0.0f, gradStartFraction, gradEndFraction, 1.0f },
new Color[] { primaryZero, primary, primary, primaryZero });
stops, colors);
graphics.setPaint(primaryPaint);
graphics.draw(new Line2D.Float(0, regularY, width, regularY));

if (hasShadow) {
float shadowY = regularY + borderStrokeWidth;

stopIndex = 0;
if (gradStartFraction > 0.0f) {
stops[stopIndex] = 0.0f;
colors[stopIndex] = secondaryZero;
stopIndex++;
}
stops[stopIndex] = gradStartFraction;
colors[stopIndex] = secondary;
stopIndex++;
stops[stopIndex] = gradEndFraction;
colors[stopIndex] = secondary;
stopIndex++;
if (gradEndFraction < 1.0f) {
stops[stopIndex] = 1.0f;
colors[stopIndex] = secondaryZero;
}

LinearGradientPaint secondaryPaint = new LinearGradientPaint(0, 0, width, 0,
new float[] { 0.0f, gradStartFraction, gradEndFraction, 1.0f },
new Color[] { secondaryZero, secondary, secondary, secondaryZero });
stops, colors);
graphics.setPaint(secondaryPaint);
graphics.draw(new Line2D.Float(0, shadowY, width, shadowY));
}
Expand Down

0 comments on commit 4288406

Please sign in to comment.