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.

AFPTrueTypeTestCase.java 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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.afp;
  19. import java.awt.Color;
  20. import java.io.ByteArrayInputStream;
  21. import java.io.ByteArrayOutputStream;
  22. import java.io.File;
  23. import java.io.IOException;
  24. import java.io.InputStream;
  25. import javax.xml.transform.Result;
  26. import javax.xml.transform.Source;
  27. import javax.xml.transform.Transformer;
  28. import javax.xml.transform.TransformerException;
  29. import javax.xml.transform.TransformerFactory;
  30. import javax.xml.transform.sax.SAXResult;
  31. import javax.xml.transform.stream.StreamSource;
  32. import org.junit.Test;
  33. import org.xml.sax.SAXException;
  34. import static org.mockito.Mockito.mock;
  35. import static org.mockito.Mockito.when;
  36. import org.apache.fop.afp.AFPPaintingState;
  37. import org.apache.fop.afp.AFPResourceManager;
  38. import org.apache.fop.afp.DataStream;
  39. import org.apache.fop.afp.Factory;
  40. import org.apache.fop.afp.fonts.FopCharacterSet;
  41. import org.apache.fop.afp.modca.PageObject;
  42. import org.apache.fop.apps.FOUserAgent;
  43. import org.apache.fop.apps.Fop;
  44. import org.apache.fop.apps.FopConfParser;
  45. import org.apache.fop.apps.FopFactory;
  46. import org.apache.fop.apps.FopFactoryBuilder;
  47. import org.apache.fop.fonts.EmbeddingMode;
  48. import org.apache.fop.fonts.FontInfo;
  49. import org.apache.fop.fonts.FontTriplet;
  50. import org.apache.fop.fonts.MultiByteFont;
  51. import org.apache.fop.render.intermediate.IFException;
  52. import junit.framework.Assert;
  53. public class AFPTrueTypeTestCase {
  54. @Test
  55. public void testAFPTrueType() throws IOException, SAXException, TransformerException {
  56. String fopxconf = "<fop version=\"1.0\">\n"
  57. + " <renderers>\n"
  58. + " <renderer mime=\"application/x-afp\">\n"
  59. + " <fonts>\n"
  60. + " <font name=\"Univers\" embed-url=\"test/resources/fonts/ttf/DejaVuLGCSerif.ttf\">\n"
  61. + " <font-triplet name=\"Univers\" style=\"normal\" weight=\"normal\"/>\n"
  62. + " <font-triplet name=\"any\" style=\"normal\" weight=\"normal\"/>\n"
  63. + " </font>\n"
  64. + " </fonts>\n"
  65. + " </renderer>\n"
  66. + " </renderers>\n"
  67. + "</fop>";
  68. String fo = "<fo:root xmlns:fo=\"http://www.w3.org/1999/XSL/Format\">\n"
  69. + " <fo:layout-master-set>\n"
  70. + " <fo:simple-page-master master-name=\"simple\">\n"
  71. + " <fo:region-body />\n"
  72. + " </fo:simple-page-master>\n"
  73. + " </fo:layout-master-set>\n"
  74. + " <fo:page-sequence master-reference=\"simple\">\n"
  75. + " <fo:flow flow-name=\"xsl-region-body\">\n"
  76. + " <fo:block font-family=\"Univers\">Univers</fo:block>\n"
  77. + " </fo:flow>\n"
  78. + " </fo:page-sequence>\n"
  79. + "</fo:root>";
  80. FopFactoryBuilder confBuilder = new FopConfParser(
  81. new ByteArrayInputStream(fopxconf.getBytes()), new File(".").toURI()).getFopFactoryBuilder();
  82. FopFactory fopFactory = confBuilder.build();
  83. FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
  84. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  85. Fop fop = fopFactory.newFop("application/x-afp", foUserAgent, bos);
  86. TransformerFactory factory = TransformerFactory.newInstance();
  87. Transformer transformer = factory.newTransformer();
  88. Source src = new StreamSource(new ByteArrayInputStream(fo.getBytes()));
  89. Result res = new SAXResult(fop.getDefaultHandler());
  90. transformer.transform(src, res);
  91. bos.close();
  92. StringBuilder sb = new StringBuilder();
  93. InputStream bis = new ByteArrayInputStream(bos.toByteArray());
  94. new AFPParser(false).read(bis, sb);
  95. String format = "BEGIN RESOURCE_GROUP RG000001\n"
  96. + "BEGIN NAME_RESOURCE RES00001 Triplets: OBJECT_FUNCTION_SET_SPECIFICATION"
  97. + ",OBJECT_CLASSIFICATION,0x01,FULLY_QUALIFIED_NAME,\n"
  98. + "BEGIN OBJECT_CONTAINER OC000001 Triplets: 0x41,0x00,0x00,\n";
  99. for (int i = 0; i < 29; i++) {
  100. format += "DATA OBJECT_CONTAINER\n";
  101. }
  102. format += "END OBJECT_CONTAINER OC000001\n"
  103. + "END NAME_RESOURCE RES00001\n"
  104. + "END RESOURCE_GROUP RG000001\n"
  105. + "BEGIN DOCUMENT DOC00001\n"
  106. + "BEGIN PAGE_GROUP PGP00001\n"
  107. + "BEGIN PAGE PGN00001\n"
  108. + "BEGIN ACTIVE_ENVIRONMENT_GROUP AEG00001\n"
  109. + "MAP DATA_RESOURCE Triplets: 0x01,FULLY_QUALIFIED_NAME,FULLY_QUALIFIED_NAME"
  110. + ",OBJECT_CLASSIFICATION,DATA_OBJECT_FONT_DESCRIPTOR,\n"
  111. + "DESCRIPTOR PAGE\n"
  112. + "MIGRATION PRESENTATION_TEXT\n"
  113. + "END ACTIVE_ENVIRONMENT_GROUP AEG00001\n"
  114. + "BEGIN PRESENTATION_TEXT PT000001\n"
  115. + "DATA PRESENTATION_TEXT\n"
  116. + "END PRESENTATION_TEXT PT000001\n"
  117. + "END PAGE PGN00001\n"
  118. + "END PAGE_GROUP PGP00001\n"
  119. + "END DOCUMENT DOC00001\n";
  120. Assert.assertEquals(sb.toString(), format);
  121. }
  122. @Test
  123. public void testAFPPainter() throws IFException, IOException {
  124. AFPDocumentHandler afpDocumentHandler = mock(AFPDocumentHandler.class);
  125. when(afpDocumentHandler.getPaintingState()).thenReturn(new AFPPaintingState());
  126. when(afpDocumentHandler.getResourceManager()).thenReturn(new AFPResourceManager(null));
  127. DataStream ds = mock(DataStream.class);
  128. when(afpDocumentHandler.getDataStream()).thenReturn(ds);
  129. PageObject po = new PageObject(new Factory(), "PAGE0001", 0, 0, 0, 0, 0);
  130. when(ds.getCurrentPage()).thenReturn(po);
  131. AFPPainter afpPainter = new MyAFPPainter(afpDocumentHandler);
  132. afpPainter.setFont("any", "normal", 400, null, null, Color.BLACK);
  133. afpPainter.drawText(0, 0, 0, 0, null, "test");
  134. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  135. po.writeToStream(bos);
  136. InputStream bis = new ByteArrayInputStream(bos.toByteArray());
  137. StringBuilder sb = new StringBuilder();
  138. new AFPParser(true).read(bis, sb);
  139. Assert.assertTrue(sb.toString(),
  140. sb.toString().contains("DATA PRESENTATION_TEXT AMB AMI SCFL TRN t TRN e TRN s TRN t"));
  141. }
  142. class MyAFPPainter extends AFPPainter {
  143. public MyAFPPainter(AFPDocumentHandler documentHandler) {
  144. super(documentHandler);
  145. }
  146. protected FOUserAgent getUserAgent() {
  147. FopFactory fopFactory = FopFactory.newInstance(new File(".").toURI());
  148. return fopFactory.newFOUserAgent();
  149. }
  150. protected FontInfo getFontInfo() {
  151. FontInfo f = new FontInfo();
  152. f.addFontProperties("any", FontTriplet.DEFAULT_FONT_TRIPLET);
  153. MultiByteFont font = new MultiByteFont(null, EmbeddingMode.AUTO);
  154. font.setWidthArray(new int[100]);
  155. f.addMetrics("any", new AFPFontConfig.AFPTrueTypeFont("", true,
  156. new FopCharacterSet("", "UTF-16BE", "", font, null, null), null, null));
  157. return f;
  158. }
  159. }
  160. }