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.

PDFDocumentGraphics2DTestCase.java 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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.pdf;
  19. import java.awt.Color;
  20. import java.awt.Dimension;
  21. import java.awt.Font;
  22. import java.awt.Graphics2D;
  23. import org.junit.Test;
  24. import static org.junit.Assert.assertEquals;
  25. import org.apache.commons.io.output.ByteArrayOutputStream;
  26. import org.apache.xmlgraphics.util.UnitConv;
  27. import org.apache.fop.svg.PDFDocumentGraphics2D;
  28. /**
  29. * Tests for {@link PDFDocumentGraphics2D}.
  30. */
  31. public class PDFDocumentGraphics2DTestCase {
  32. /**
  33. * Does a smoke test on PDFDocumentGraphics2D making sure that nobody accidentally broke
  34. * anything serious. It does not check the correctness of the produced PDF.
  35. * @throws Exception if an error occurs
  36. */
  37. @Test
  38. public void smokeTest() throws Exception {
  39. ByteArrayOutputStream baout = new ByteArrayOutputStream();
  40. PDFDocumentGraphics2D g2d = new PDFDocumentGraphics2D(false);
  41. g2d.setGraphicContext(new org.apache.xmlgraphics.java2d.GraphicContext());
  42. //Set up the document size
  43. Dimension pageSize = new Dimension(
  44. (int)Math.ceil(UnitConv.mm2pt(210)),
  45. (int)Math.ceil(UnitConv.mm2pt(297))); //page size A4 (in pt)
  46. g2d.setupDocument(baout, pageSize.width, pageSize.height);
  47. //A few rectangles rotated and with different color
  48. Graphics2D copy = (Graphics2D)g2d.create();
  49. int c = 12;
  50. for (int i = 0; i < c; i++) {
  51. float f = ((i + 1) / (float)c);
  52. Color col = new Color(0.0f, 1 - f, 0.0f);
  53. copy.setColor(col);
  54. copy.fillRect(70, 90, 50, 50);
  55. copy.rotate(-2 * Math.PI / c, 70, 90);
  56. }
  57. copy.dispose();
  58. //Some text
  59. g2d.rotate(-0.25);
  60. g2d.setColor(Color.RED);
  61. g2d.setFont(new Font("sans-serif", Font.PLAIN, 36));
  62. g2d.drawString("Hello world!", 140, 140);
  63. g2d.setColor(Color.RED.darker());
  64. g2d.setFont(new Font("serif", Font.PLAIN, 36));
  65. g2d.drawString("Hello world!", 140, 180);
  66. g2d.nextPage(); //Move to next page
  67. g2d.setFont(new Font("sans-serif", Font.PLAIN, 36));
  68. g2d.drawString("Welcome to page 2!", 140, 140);
  69. //Cleanup
  70. g2d.finish();
  71. String pdfString = baout.toString("ISO-8859-1");
  72. assertEquals("%%EOF not found",
  73. pdfString.substring(pdfString.length() - 6), "%%EOF\n");
  74. }
  75. }