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