Skip to content

Commit d6c39a3

Browse files
ganomirototor
authored andcommitted
Fixing out of bounds exception when there is an artifical key frame for the first fraction and for the last fraction at the same time. Fixed by using the same approach as if there was only an artificial key frame for the last fraction, which reuses the alpha of the last color.
Adjustment for the starting alpha of an artificial key frame for the first fraction. Gradients should only be visible between two stop elements, not between start and stop or stop and end. To achieve this, the alpha needs to be the same for start and first stop.
1 parent adc28f8 commit d6c39a3

File tree

3 files changed

+279
-5
lines changed

3 files changed

+279
-5
lines changed

graphics2d/src/main/java/de/rototor/pdfbox/graphics2d/PdfBoxGraphics2DPaintApplier.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1305,23 +1305,28 @@ private int patchFunction(int colorIdx, COSDictionary cosBase)
13051305
final float alpha1;
13061306
if (needBoundsKeyFrameEntry(fractions))
13071307
{
1308+
PdfBoxGraphics2DColor clr;
13081309
if (0 == colorIdx)
13091310
{
1310-
alpha0 = 1f;
1311+
// Use first colors alpha for the inserted keyframe instead of 1f.
1312+
// Gradients should only happen between explicitly defined stops.
1313+
clr = alphaGrayscaleColors[0];
1314+
alpha0 = clr.toPDColor().getComponents()[0];
13111315
}
13121316
else
13131317
{
1314-
PdfBoxGraphics2DColor clr = alphaGrayscaleColors[colorIdx - 1];
1318+
clr = alphaGrayscaleColors[colorIdx - 1];
13151319
alpha0 = clr.toPDColor().getComponents()[0];
13161320
}
1317-
1318-
PdfBoxGraphics2DColor clr = alphaGrayscaleColors[colorIdx];
1321+
// In case we have an inserted exit keyframe in addition to an inserted entry keyframe,
1322+
// we are already out of colors and will reuse the last color's alpha value.
1323+
if (colorIdx < alphaGrayscaleColors.length)
1324+
clr = alphaGrayscaleColors[colorIdx];
13191325
alpha1 = clr.toPDColor().getComponents()[0];
13201326
colorIdx++;
13211327
}
13221328
else
13231329
{
1324-
13251330
PdfBoxGraphics2DColor clr = alphaGrayscaleColors[colorIdx];
13261331
alpha0 = clr.toPDColor().getComponents()[0];
13271332
if (colorIdx + 1 < alphaGrayscaleColors.length)

graphics2d/src/test/java/de/rototor/pdfbox/graphics2d/RenderSVGsTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public void testSVGs() throws IOException
3333
renderSVG("focalpoint_radial_sample.svg", 100);
3434
renderSVG("tux_colored.svg", 0.3);
3535
renderSVG("tux.svg", 0.3);
36+
renderSVG("gradients.svg", 0.3);
3637
renderSVG("barChart.svg", 0.45);
3738
renderSVG("gump-bench.svg", 1);
3839
renderSVG("json.svg", 150);
Lines changed: 268 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)