You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GradientTestCase.java 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.render.ps.svg;
  19. import java.awt.Color;
  20. import java.io.ByteArrayOutputStream;
  21. import java.io.IOException;
  22. import org.junit.Test;
  23. import static org.junit.Assert.assertEquals;
  24. import org.apache.commons.io.IOUtils;
  25. import org.apache.batik.ext.awt.LinearGradientPaint;
  26. import org.apache.batik.ext.awt.MultipleGradientPaint;
  27. import org.apache.batik.ext.awt.RadialGradientPaint;
  28. import org.apache.xmlgraphics.java2d.GraphicContext;
  29. import org.apache.xmlgraphics.ps.PSGenerator;
  30. public class GradientTestCase {
  31. @Test
  32. public void testLinearGradient() throws IOException {
  33. float[] fractions = {0f, 1f};
  34. Color[] colors = {new Color(255, 255, 0), new Color(255, 0, 0)};
  35. LinearGradientPaint gradient = new LinearGradientPaint(115f, 285f, 15f, 15f, fractions, colors);
  36. testGradientRendering(gradient, "expected-linear-gradient.ps");
  37. }
  38. @Test
  39. public void testRadialGradient() throws IOException {
  40. float cx = 840f;
  41. float cy = 180f;
  42. float r = 16f;
  43. float[] fractions = {0.2f, 0.6f, 0.8f, 1.0f};
  44. Color[] colors = {
  45. new Color(255, 255, 255),
  46. new Color(200, 200, 200),
  47. new Color(170, 170, 170),
  48. new Color(140, 140, 140)};
  49. RadialGradientPaint gradient = new RadialGradientPaint(cx, cy, r, fractions, colors);
  50. testGradientRendering(gradient, "expected-radial-gradient.ps");
  51. }
  52. private void testGradientRendering(MultipleGradientPaint gradient, String expectedResourceName)
  53. throws IOException {
  54. ByteArrayOutputStream out = new ByteArrayOutputStream();
  55. PSSVGGraphics2D svgGraphics2D = new PSSVGGraphics2D(false, new PSGenerator(out));
  56. svgGraphics2D.setGraphicContext(new GraphicContext());
  57. svgGraphics2D.applyPaint(gradient, true);
  58. byte[] actual = out.toByteArray();
  59. byte[] expected = IOUtils.toByteArray(getClass().getResourceAsStream(expectedResourceName));
  60. assertEquals(new String(expected, "US-ASCII"), new String(actual, "US-ASCII"));
  61. }
  62. }