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.

PCLPainterTestCase.java 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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.pcl;
  19. import java.awt.Color;
  20. import java.awt.Dimension;
  21. import java.awt.FontFormatException;
  22. import java.awt.Graphics2D;
  23. import java.awt.Rectangle;
  24. import java.awt.geom.AffineTransform;
  25. import java.awt.geom.Rectangle2D;
  26. import java.io.ByteArrayOutputStream;
  27. import java.io.File;
  28. import java.io.IOException;
  29. import java.net.URI;
  30. import java.net.URISyntaxException;
  31. import javax.xml.transform.stream.StreamResult;
  32. import org.junit.Assert;
  33. import org.junit.Test;
  34. import org.apache.xmlgraphics.image.loader.ImageInfo;
  35. import org.apache.xmlgraphics.image.loader.ImageSize;
  36. import org.apache.xmlgraphics.image.loader.impl.ImageGraphics2D;
  37. import org.apache.xmlgraphics.java2d.Graphics2DImagePainter;
  38. import org.apache.fop.apps.FOUserAgent;
  39. import org.apache.fop.apps.FopFactory;
  40. import org.apache.fop.fonts.EmbeddingMode;
  41. import org.apache.fop.fonts.FontInfo;
  42. import org.apache.fop.fonts.FontType;
  43. import org.apache.fop.fonts.MultiByteFont;
  44. import org.apache.fop.render.intermediate.IFContext;
  45. import org.apache.fop.render.intermediate.IFException;
  46. import org.apache.fop.render.java2d.CustomFontMetricsMapper;
  47. public class PCLPainterTestCase {
  48. private FOUserAgent ua = FopFactory.newInstance(new File(".").toURI()).newFOUserAgent();
  49. @Test
  50. public void testFillRect() throws IFException {
  51. Rectangle size = new Rectangle(1, 1);
  52. PCLPageDefinition pclPageDef = new PCLPageDefinition("", 0, new Dimension(), size, true);
  53. PCLDocumentHandler documentHandler = new PCLDocumentHandler(new IFContext(ua));
  54. ByteArrayOutputStream output = new ByteArrayOutputStream();
  55. documentHandler.setResult(new StreamResult(output));
  56. documentHandler.startDocument();
  57. PCLPainter pclPainter = new PCLPainter(documentHandler, pclPageDef);
  58. pclPainter.fillRect(size, Color.RED);
  59. Assert.assertTrue(output.toString().contains("*c4Q\u001B*c0.01h0.01V\u001B*c32G\u001B*c4P"));
  60. output.reset();
  61. pclPainter.getPCLUtil().setColorEnabled(true);
  62. pclPainter.fillRect(size, Color.RED);
  63. Assert.assertFalse(output.toString().contains("*c4P"));
  64. Assert.assertTrue(output.toString().contains("*v255a0b0c0I\u001B*v0S\u001B*c0.01h0.01V\u001B*c0P"));
  65. }
  66. @Test
  67. public void testDrawImage() throws IFException {
  68. Rectangle size = new Rectangle(1, 1);
  69. PCLPageDefinition pclPageDef = new PCLPageDefinition("", 0, new Dimension(), size, true);
  70. PCLDocumentHandler documentHandler = new PCLDocumentHandler(new IFContext(ua));
  71. ByteArrayOutputStream output = new ByteArrayOutputStream();
  72. documentHandler.setResult(new StreamResult(output));
  73. documentHandler.startDocument();
  74. PCLPainter pclPainter = new PCLPainter(documentHandler, pclPageDef);
  75. pclPainter.getPCLUtil().setColorEnabled(true);
  76. pclPainter.drawImage("test/resources/images/fop-logo-color-24bit.png", new Rectangle(100, 100));
  77. Assert.assertTrue(output.toString(), output.toString().contains("*r0f1t1S"));
  78. }
  79. @Test
  80. public void testDrawGraphics() throws IOException, IFException {
  81. Rectangle size = new Rectangle(1, 1);
  82. PCLPageDefinition pclPageDef = new PCLPageDefinition("", 0, new Dimension(), size, true);
  83. PCLDocumentHandler documentHandler = new PCLDocumentHandler(new IFContext(ua));
  84. ByteArrayOutputStream output = new ByteArrayOutputStream();
  85. documentHandler.setResult(new StreamResult(output));
  86. documentHandler.startDocument();
  87. PCLPainter pclPainter = new PCLPainter(documentHandler, pclPageDef);
  88. PCLImageHandlerGraphics2D graphics2D = new PCLImageHandlerGraphics2D();
  89. ImageInfo info = new ImageInfo(null, null);
  90. info.setSize(new ImageSize());
  91. ImageGraphics2D imageGraphics2D = new ImageGraphics2D(info, new MyGraphics2DImagePainter());
  92. graphics2D.handleImage(pclPainter.createRenderingContext(), imageGraphics2D, new Rectangle(50, 100));
  93. Assert.assertTrue(output.toString().contains("*c0.5x1Y"));
  94. output.reset();
  95. pclPainter.startGroup(AffineTransform.getRotateInstance(-Math.PI / 2), null);
  96. graphics2D.handleImage(pclPainter.createRenderingContext(), imageGraphics2D, new Rectangle(50, 100));
  97. Assert.assertTrue(output.toString().contains("*c1x0.5Y"));
  98. }
  99. static class MyGraphics2DImagePainter implements Graphics2DImagePainter {
  100. public void paint(Graphics2D g2d, Rectangle2D area) {
  101. }
  102. public Dimension getImageSize() {
  103. return null;
  104. }
  105. }
  106. @Test
  107. public void testTruetype() throws IFException, IOException, FontFormatException, URISyntaxException {
  108. String optimizeResources = getPCL(true).toString();
  109. String notOptimizeResources = getPCL(false).toString();
  110. Assert.assertTrue(notOptimizeResources.contains("DejaVu"));
  111. Assert.assertFalse(optimizeResources.contains("DejaVu"));
  112. Assert.assertTrue(optimizeResources.length() > 900);
  113. }
  114. private ByteArrayOutputStream getPCL(boolean optimizeResources)
  115. throws IFException, URISyntaxException, IOException, FontFormatException {
  116. Rectangle size = new Rectangle(1, 1);
  117. PCLPageDefinition pclPageDef = new PCLPageDefinition("", 0, new Dimension(), size, true);
  118. PCLDocumentHandler documentHandler = new PCLDocumentHandler(new IFContext(ua));
  119. ByteArrayOutputStream output = new ByteArrayOutputStream();
  120. documentHandler.setResult(new StreamResult(output));
  121. documentHandler.startDocument();
  122. PCLPainter pclPainter = new PCLPainter(documentHandler, pclPageDef);
  123. pclPainter.getPCLUtil().setOptimizeResources(optimizeResources);
  124. FontInfo fi = new FontInfo();
  125. fi.addFontProperties("", "", "", 0);
  126. MultiByteFont mbf = new MultiByteFont(ua.getResourceResolver(), EmbeddingMode.AUTO);
  127. mbf.setEmbedURI(new URI("test/resources/fonts/ttf/DejaVuLGCSerif.ttf"));
  128. mbf.setFontType(FontType.TRUETYPE);
  129. fi.addMetrics("", new CustomFontMetricsMapper(mbf));
  130. documentHandler.setFontInfo(fi);
  131. pclPainter.setFont("", "", 0, "", 0, Color.BLACK);
  132. pclPainter.drawText(0, 0, 0, 0, null, "test");
  133. return output;
  134. }
  135. }