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.

AFPPainterTestCase.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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.awt.Dimension;
  21. import java.awt.Rectangle;
  22. import java.io.ByteArrayInputStream;
  23. import java.io.ByteArrayOutputStream;
  24. import java.io.File;
  25. import java.io.IOException;
  26. import java.io.InputStream;
  27. import java.net.URI;
  28. import java.net.URISyntaxException;
  29. import java.util.ArrayList;
  30. import java.util.List;
  31. import java.util.Map;
  32. import javax.xml.transform.stream.StreamResult;
  33. import org.junit.Assert;
  34. import org.junit.Test;
  35. import static org.junit.Assert.fail;
  36. import static org.mockito.Matchers.any;
  37. import static org.mockito.Matchers.anyString;
  38. import static org.mockito.Mockito.mock;
  39. import static org.mockito.Mockito.verify;
  40. import static org.mockito.Mockito.when;
  41. import org.apache.xmlgraphics.image.loader.Image;
  42. import org.apache.xmlgraphics.image.loader.ImageFlavor;
  43. import org.apache.xmlgraphics.image.loader.ImageManager;
  44. import org.apache.xmlgraphics.image.loader.impl.DefaultImageContext;
  45. import org.apache.xmlgraphics.image.loader.impl.DefaultImageSessionContext;
  46. import org.apache.xmlgraphics.image.loader.impl.ImageBuffered;
  47. import org.apache.fop.afp.AFPEventProducer;
  48. import org.apache.fop.afp.AFPPaintingState;
  49. import org.apache.fop.afp.AFPResourceManager;
  50. import org.apache.fop.afp.fonts.CharacterSet;
  51. import org.apache.fop.afp.fonts.CharactersetEncoder;
  52. import org.apache.fop.afp.fonts.RasterFont;
  53. import org.apache.fop.apps.FOUserAgent;
  54. import org.apache.fop.apps.FopFactory;
  55. import org.apache.fop.events.EventBroadcaster;
  56. import org.apache.fop.fo.Constants;
  57. import org.apache.fop.fo.expr.PropertyException;
  58. import org.apache.fop.fonts.Font;
  59. import org.apache.fop.fonts.FontInfo;
  60. import org.apache.fop.render.ImageHandlerRegistry;
  61. import org.apache.fop.render.intermediate.IFContext;
  62. import org.apache.fop.render.intermediate.IFException;
  63. import org.apache.fop.traits.BorderProps;
  64. import org.apache.fop.util.ColorUtil;
  65. public class AFPPainterTestCase {
  66. @Test
  67. public void testDrawBorderRect() {
  68. // the goal of this test is to check that the drawing of rounded corners in AFP uses a bitmap of the
  69. // rounded corners (in fact the whole rectangle with rounded corners). the check is done by verifying
  70. // that the AFPImageHandlerRenderedImage.handleImage() method is called
  71. // mock
  72. AFPPaintingState afpPaintingState = mock(AFPPaintingState.class);
  73. when(afpPaintingState.getResolution()).thenReturn(72);
  74. // mock
  75. EventBroadcaster eventBroadcaster = mock(EventBroadcaster.class);
  76. // mock
  77. DefaultImageContext defaultImageContext = mock(DefaultImageContext.class);
  78. when(defaultImageContext.getSourceResolution()).thenReturn(72000f);
  79. // mock
  80. DefaultImageSessionContext defaultImageSessionContxt = mock(DefaultImageSessionContext.class);
  81. when(defaultImageSessionContxt.getParentContext()).thenReturn(defaultImageContext);
  82. when(defaultImageSessionContxt.getTargetResolution()).thenReturn(72000f);
  83. // mock
  84. ImageBuffered imageBuffered = mock(ImageBuffered.class);
  85. // mock
  86. ImageManager imageManager = mock(ImageManager.class);
  87. // mock
  88. AFPImageHandlerRenderedImage afpImageHandlerRenderedImage = mock(AFPImageHandlerRenderedImage.class);
  89. // mock
  90. ImageHandlerRegistry imageHandlerRegistry = mock(ImageHandlerRegistry.class);
  91. when(imageHandlerRegistry.getHandler(any(AFPRenderingContext.class), any(Image.class))).thenReturn(
  92. afpImageHandlerRenderedImage);
  93. // mock
  94. FOUserAgent foUserAgent = mock(FOUserAgent.class);
  95. when(foUserAgent.getEventBroadcaster()).thenReturn(eventBroadcaster);
  96. when(foUserAgent.getImageSessionContext()).thenReturn(defaultImageSessionContxt);
  97. when(foUserAgent.getImageManager()).thenReturn(imageManager);
  98. when(foUserAgent.getImageHandlerRegistry()).thenReturn(imageHandlerRegistry);
  99. // mock
  100. AFPEventProducer afpEventProducer = mock(AFPEventProducer.class);
  101. when(AFPEventProducer.Provider.get(eventBroadcaster)).thenReturn(afpEventProducer);
  102. // mock
  103. AFPResourceManager afpResourceManager = mock(AFPResourceManager.class);
  104. when(afpResourceManager.isObjectCached(null)).thenReturn(false);
  105. // mock
  106. IFContext ifContext = mock(IFContext.class);
  107. when(ifContext.getUserAgent()).thenReturn(foUserAgent);
  108. // mock
  109. AFPDocumentHandler afpDocumentHandler = mock(AFPDocumentHandler.class);
  110. when(afpDocumentHandler.getPaintingState()).thenReturn(afpPaintingState);
  111. when(afpDocumentHandler.getContext()).thenReturn(ifContext);
  112. when(afpDocumentHandler.getResourceManager()).thenReturn(afpResourceManager);
  113. when(afpDocumentHandler.cacheRoundedCorner("a2a48964ba2d")).thenReturn("RC000000");
  114. // real instance, no mock
  115. AFPPainter afpPainter = new AFPPainter(afpDocumentHandler);
  116. // build rectangle 200 x 50 (points, which are converted to millipoints)
  117. Rectangle rectangle = new Rectangle(0, 0, 200000, 50000);
  118. // build border properties
  119. int style = Constants.EN_SOLID;
  120. BorderProps.Mode mode = BorderProps.Mode.SEPARATE;
  121. Color color = Color.BLACK;
  122. int borderWidth = 4000;
  123. int radiusStart = 30000;
  124. int radiusEnd = 30000;
  125. BorderProps border1 = new BorderProps(style, borderWidth, radiusStart, radiusEnd, color, mode);
  126. BorderProps border2 = new BorderProps(style, borderWidth, radiusStart, radiusEnd, color, mode);
  127. BorderProps border3 = new BorderProps(style, borderWidth, radiusStart, radiusEnd, color, mode);
  128. BorderProps border4 = new BorderProps(style, borderWidth, radiusStart, radiusEnd, color, mode);
  129. try {
  130. when(imageManager.convertImage(any(Image.class), any(ImageFlavor[].class), any(Map.class)))
  131. .thenReturn(imageBuffered);
  132. afpPainter.drawBorderRect(rectangle, border1, border2, border3, border4, Color.WHITE);
  133. // note: here we would really like to verify that the second and third arguments passed to
  134. // handleImage() are the instances ib and rect declared above but that causes mockito to throw
  135. // an exception, probably because we cannot declare the AFPRenderingContext and are forced to
  136. // use any(), which forces the use of any() for all arguments
  137. verify(afpImageHandlerRenderedImage).handleImage(any(AFPRenderingContext.class),
  138. any(Image.class), any(Rectangle.class));
  139. } catch (Exception e) {
  140. fail("something broke...");
  141. }
  142. }
  143. @Test
  144. public void testPresentationText() throws URISyntaxException, IFException, IOException {
  145. List<String> strings = new ArrayList<String>();
  146. strings.add("test");
  147. Assert.assertEquals(writeText(strings), "BEGIN DOCUMENT DOC00001\n"
  148. + "BEGIN PAGE PGN00001\n"
  149. + "BEGIN ACTIVE_ENVIRONMENT_GROUP AEG00001\n"
  150. + "DESCRIPTOR PAGE\n"
  151. + "MIGRATION PRESENTATION_TEXT\n"
  152. + "END ACTIVE_ENVIRONMENT_GROUP AEG00001\n"
  153. + "BEGIN PRESENTATION_TEXT PT000001\n"
  154. + "DATA PRESENTATION_TEXT\n"
  155. + "END PRESENTATION_TEXT PT000001\n"
  156. + "END PAGE PGN00001\n"
  157. + "END DOCUMENT DOC00001\n");
  158. for (int i = 0; i < 5000; i++) {
  159. strings.add("test");
  160. }
  161. Assert.assertEquals(writeText(strings), "BEGIN DOCUMENT DOC00001\n"
  162. + "BEGIN PAGE PGN00001\n"
  163. + "BEGIN ACTIVE_ENVIRONMENT_GROUP AEG00001\n"
  164. + "DESCRIPTOR PAGE\n"
  165. + "MIGRATION PRESENTATION_TEXT\n"
  166. + "END ACTIVE_ENVIRONMENT_GROUP AEG00001\n"
  167. + "BEGIN PRESENTATION_TEXT PT000001\n"
  168. + "DATA PRESENTATION_TEXT\n"
  169. + "END PRESENTATION_TEXT PT000001\n"
  170. + "BEGIN PRESENTATION_TEXT PT000002\n"
  171. + "DATA PRESENTATION_TEXT\n"
  172. + "END PRESENTATION_TEXT PT000002\n"
  173. + "END PAGE PGN00001\n"
  174. + "END DOCUMENT DOC00001\n");
  175. }
  176. @Test
  177. public void testPresentationText2() throws URISyntaxException, IFException, IOException {
  178. List<String> strings = new ArrayList<String>();
  179. for (int i = 0; i < 5000; i++) {
  180. strings.add("tes");
  181. }
  182. Assert.assertEquals(writeText(strings), "BEGIN DOCUMENT DOC00001\n"
  183. + "BEGIN PAGE PGN00001\n"
  184. + "BEGIN ACTIVE_ENVIRONMENT_GROUP AEG00001\n"
  185. + "DESCRIPTOR PAGE\n"
  186. + "MIGRATION PRESENTATION_TEXT\n"
  187. + "END ACTIVE_ENVIRONMENT_GROUP AEG00001\n"
  188. + "BEGIN PRESENTATION_TEXT PT000001\n"
  189. + "DATA PRESENTATION_TEXT\n"
  190. + "END PRESENTATION_TEXT PT000001\n"
  191. + "BEGIN PRESENTATION_TEXT PT000002\n"
  192. + "DATA PRESENTATION_TEXT\n"
  193. + "END PRESENTATION_TEXT PT000002\n"
  194. + "END PAGE PGN00001\n"
  195. + "END DOCUMENT DOC00001\n");
  196. }
  197. private String writeText(List<String> text) throws URISyntaxException, IOException, IFException {
  198. FOUserAgent agent = FopFactory.newInstance(new URI(".")).newFOUserAgent();
  199. IFContext context = new IFContext(agent);
  200. AFPDocumentHandler doc = new AFPDocumentHandler(context);
  201. AFPPainter afpPainter = new AFPPainter(doc);
  202. FontInfo fi = new FontInfo();
  203. fi.addFontProperties("", Font.DEFAULT_FONT);
  204. RasterFont rf = new RasterFont("", true);
  205. CharacterSet cs = mock(CharacterSet.class);
  206. CharactersetEncoder.EncodedChars encoder = mock(CharactersetEncoder.EncodedChars.class);
  207. when(cs.encodeChars(anyString())).thenReturn(encoder);
  208. when(encoder.getLength()).thenReturn(text.get(0).length());
  209. rf.addCharacterSet(12000, cs);
  210. fi.addMetrics("", rf);
  211. doc.setFontInfo(fi);
  212. afpPainter.setFont("any", "normal", 400, "", 12000, Color.BLACK);
  213. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  214. doc.setResult(new StreamResult(outputStream));
  215. doc.startDocument();
  216. doc.startPage(0, "", "", new Dimension());
  217. for (String s : text) {
  218. afpPainter.drawText(0, 0, 0, 0, null, s);
  219. }
  220. doc.endDocument();
  221. InputStream bis = new ByteArrayInputStream(outputStream.toByteArray());
  222. StringBuilder sb = new StringBuilder();
  223. new AFPParser(false).read(bis, sb);
  224. return sb.toString();
  225. }
  226. @Test
  227. public void testDrawBorderRect3() throws IFException, PropertyException, IOException {
  228. FOUserAgent ua = FopFactory.newInstance(new File(".").toURI()).newFOUserAgent();
  229. AFPDocumentHandler documentHandler = new AFPDocumentHandler(new IFContext(ua));
  230. ByteArrayOutputStream os = new ByteArrayOutputStream();
  231. documentHandler.setResult(new StreamResult(os));
  232. documentHandler.startDocument();
  233. documentHandler.startPage(0, "", "", new Dimension());
  234. AFPPainter afpPainter = new AFPPainter(documentHandler);
  235. int style = Constants.EN_DOTTED;
  236. BorderProps.Mode mode = BorderProps.Mode.COLLAPSE_OUTER;
  237. Color color = ColorUtil.parseColorString(ua, "fop-rgb-icc(0.5019608,0.5019608,0.5019608,#CMYK,,0,0,0,0.5)");
  238. int borderWidth = 500;
  239. int radiusStart = 0;
  240. int radiusEnd = 0;
  241. BorderProps border1 = new BorderProps(style, borderWidth, radiusStart, radiusEnd, color, mode);
  242. afpPainter.drawBorderRect(new Rectangle(0, 0, 552755, 16090), null, border1, null, null, Color.WHITE);
  243. documentHandler.endDocument();
  244. InputStream bis = new ByteArrayInputStream(os.toByteArray());
  245. StringBuilder sb = new StringBuilder();
  246. new AFPParser(false).read(bis, sb);
  247. Assert.assertEquals(sb.toString(), "BEGIN DOCUMENT DOC00001\n"
  248. + "BEGIN PAGE PGN00001\n"
  249. + "BEGIN ACTIVE_ENVIRONMENT_GROUP AEG00001\n"
  250. + "DESCRIPTOR PAGE\n"
  251. + "MIGRATION PRESENTATION_TEXT\n"
  252. + "END ACTIVE_ENVIRONMENT_GROUP AEG00001\n"
  253. + "BEGIN PRESENTATION_TEXT PT000001\n"
  254. + "DATA PRESENTATION_TEXT\n"
  255. + "END PRESENTATION_TEXT PT000001\n"
  256. + "BEGIN PRESENTATION_TEXT PT000002\n"
  257. + "DATA PRESENTATION_TEXT\n"
  258. + "END PRESENTATION_TEXT PT000002\n"
  259. + "END PAGE PGN00001\n"
  260. + "END DOCUMENT DOC00001\n");
  261. }
  262. }