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 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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.Rectangle;
  23. import java.io.ByteArrayOutputStream;
  24. import java.io.File;
  25. import java.io.IOException;
  26. import java.net.URI;
  27. import java.net.URISyntaxException;
  28. import javax.xml.transform.stream.StreamResult;
  29. import org.junit.Assert;
  30. import org.junit.Test;
  31. import org.apache.fop.apps.FOUserAgent;
  32. import org.apache.fop.apps.FopFactory;
  33. import org.apache.fop.fonts.EmbeddingMode;
  34. import org.apache.fop.fonts.FontInfo;
  35. import org.apache.fop.fonts.FontType;
  36. import org.apache.fop.fonts.MultiByteFont;
  37. import org.apache.fop.render.intermediate.IFContext;
  38. import org.apache.fop.render.intermediate.IFException;
  39. import org.apache.fop.render.java2d.CustomFontMetricsMapper;
  40. public class PCLPainterTestCase {
  41. private FOUserAgent ua = FopFactory.newInstance(new File(".").toURI()).newFOUserAgent();
  42. @Test
  43. public void testFillRect() throws IFException {
  44. Rectangle size = new Rectangle(1, 1);
  45. PCLPageDefinition pclPageDef = new PCLPageDefinition("", 0, new Dimension(), size, true);
  46. PCLDocumentHandler documentHandler = new PCLDocumentHandler(new IFContext(ua));
  47. ByteArrayOutputStream output = new ByteArrayOutputStream();
  48. documentHandler.setResult(new StreamResult(output));
  49. documentHandler.startDocument();
  50. PCLPainter pclPainter = new PCLPainter(documentHandler, pclPageDef);
  51. pclPainter.fillRect(size, Color.RED);
  52. Assert.assertTrue(output.toString().contains("*c4Q\u001B*c0.01h0.01V\u001B*c32G\u001B*c4P"));
  53. output.reset();
  54. pclPainter.getPCLUtil().setColorEnabled(true);
  55. pclPainter.fillRect(size, Color.RED);
  56. Assert.assertFalse(output.toString().contains("*c4P"));
  57. Assert.assertTrue(output.toString().contains("*v255a0b0c0I\u001B*v0S\u001B*c0.01h0.01V\u001B*c0P"));
  58. }
  59. @Test
  60. public void testTruetype() throws IFException, IOException, FontFormatException, URISyntaxException {
  61. String optimizeResources = getPCL(true).toString();
  62. String notOptimizeResources = getPCL(false).toString();
  63. Assert.assertTrue(notOptimizeResources.contains("DejaVu"));
  64. Assert.assertFalse(optimizeResources.contains("DejaVu"));
  65. Assert.assertTrue(optimizeResources.length() > 900);
  66. }
  67. private ByteArrayOutputStream getPCL(boolean optimizeResources)
  68. throws IFException, URISyntaxException, IOException, FontFormatException {
  69. Rectangle size = new Rectangle(1, 1);
  70. PCLPageDefinition pclPageDef = new PCLPageDefinition("", 0, new Dimension(), size, true);
  71. PCLDocumentHandler documentHandler = new PCLDocumentHandler(new IFContext(ua));
  72. ByteArrayOutputStream output = new ByteArrayOutputStream();
  73. documentHandler.setResult(new StreamResult(output));
  74. documentHandler.startDocument();
  75. PCLPainter pclPainter = new PCLPainter(documentHandler, pclPageDef);
  76. pclPainter.getPCLUtil().setOptimizeResources(optimizeResources);
  77. FontInfo fi = new FontInfo();
  78. fi.addFontProperties("", "", "", 0);
  79. MultiByteFont mbf = new MultiByteFont(ua.getResourceResolver(), EmbeddingMode.AUTO);
  80. mbf.setEmbedURI(new URI("test/resources/fonts/ttf/DejaVuLGCSerif.ttf"));
  81. mbf.setFontType(FontType.TRUETYPE);
  82. fi.addMetrics("", new CustomFontMetricsMapper(mbf));
  83. documentHandler.setFontInfo(fi);
  84. pclPainter.setFont("", "", 0, "", 0, Color.BLACK);
  85. pclPainter.drawText(0, 0, 0, 0, null, "test");
  86. return output;
  87. }
  88. }