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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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.HashMap;
  31. import java.util.List;
  32. import java.util.Map;
  33. import javax.xml.transform.stream.StreamResult;
  34. import org.junit.Assert;
  35. import org.junit.Test;
  36. import static org.mockito.ArgumentMatchers.any;
  37. import static org.mockito.ArgumentMatchers.nullable;
  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.xmlgraphics.util.QName;
  48. import org.apache.fop.afp.AFPEventProducer;
  49. import org.apache.fop.afp.AFPPaintingState;
  50. import org.apache.fop.afp.AFPResourceManager;
  51. import org.apache.fop.afp.fonts.CharacterSet;
  52. import org.apache.fop.afp.fonts.CharactersetEncoder;
  53. import org.apache.fop.afp.fonts.OutlineFontTestCase;
  54. import org.apache.fop.afp.fonts.RasterFont;
  55. import org.apache.fop.apps.FOUserAgent;
  56. import org.apache.fop.apps.FopFactory;
  57. import org.apache.fop.events.EventBroadcaster;
  58. import org.apache.fop.fo.Constants;
  59. import org.apache.fop.fo.expr.PropertyException;
  60. import org.apache.fop.fonts.Font;
  61. import org.apache.fop.fonts.FontInfo;
  62. import org.apache.fop.render.ImageHandlerRegistry;
  63. import org.apache.fop.render.afp.extensions.AFPElementMapping;
  64. import org.apache.fop.render.intermediate.IFContext;
  65. import org.apache.fop.render.intermediate.IFException;
  66. import org.apache.fop.traits.BorderProps;
  67. import org.apache.fop.util.ColorUtil;
  68. public class AFPPainterTestCase {
  69. @Test
  70. public void testDrawBorderRect() throws Exception {
  71. // the goal of this test is to check that the drawing of rounded corners in AFP uses a bitmap of the
  72. // rounded corners (in fact the whole rectangle with rounded corners). the check is done by verifying
  73. // that the AFPImageHandlerRenderedImage.handleImage() method is called
  74. // mock
  75. AFPPaintingState afpPaintingState = mock(AFPPaintingState.class);
  76. when(afpPaintingState.getResolution()).thenReturn(72);
  77. // mock
  78. EventBroadcaster eventBroadcaster = mock(EventBroadcaster.class);
  79. // mock
  80. DefaultImageContext defaultImageContext = mock(DefaultImageContext.class);
  81. when(defaultImageContext.getSourceResolution()).thenReturn(72000f);
  82. // mock
  83. DefaultImageSessionContext defaultImageSessionContxt = mock(DefaultImageSessionContext.class);
  84. when(defaultImageSessionContxt.getParentContext()).thenReturn(defaultImageContext);
  85. when(defaultImageSessionContxt.getTargetResolution()).thenReturn(72000f);
  86. // mock
  87. ImageBuffered imageBuffered = mock(ImageBuffered.class);
  88. // mock
  89. ImageManager imageManager = mock(ImageManager.class);
  90. // mock
  91. AFPImageHandlerRenderedImage afpImageHandlerRenderedImage = mock(AFPImageHandlerRenderedImage.class);
  92. // mock
  93. ImageHandlerRegistry imageHandlerRegistry = mock(ImageHandlerRegistry.class);
  94. when(imageHandlerRegistry.getHandler(any(AFPRenderingContext.class), nullable(Image.class))).thenReturn(
  95. afpImageHandlerRenderedImage);
  96. // mock
  97. FOUserAgent foUserAgent = mock(FOUserAgent.class);
  98. when(foUserAgent.getEventBroadcaster()).thenReturn(eventBroadcaster);
  99. when(foUserAgent.getImageSessionContext()).thenReturn(defaultImageSessionContxt);
  100. when(foUserAgent.getImageManager()).thenReturn(imageManager);
  101. when(foUserAgent.getImageHandlerRegistry()).thenReturn(imageHandlerRegistry);
  102. // mock
  103. AFPEventProducer afpEventProducer = mock(AFPEventProducer.class);
  104. when(AFPEventProducer.Provider.get(eventBroadcaster)).thenReturn(afpEventProducer);
  105. // mock
  106. AFPResourceManager afpResourceManager = mock(AFPResourceManager.class);
  107. when(afpResourceManager.isObjectCached(null)).thenReturn(false);
  108. // mock
  109. IFContext ifContext = mock(IFContext.class);
  110. when(ifContext.getUserAgent()).thenReturn(foUserAgent);
  111. // mock
  112. AFPDocumentHandler afpDocumentHandler = mock(AFPDocumentHandler.class);
  113. when(afpDocumentHandler.getPaintingState()).thenReturn(afpPaintingState);
  114. when(afpDocumentHandler.getContext()).thenReturn(ifContext);
  115. when(afpDocumentHandler.getResourceManager()).thenReturn(afpResourceManager);
  116. when(afpDocumentHandler.cacheRoundedCorner("a2a48964ba2d")).thenReturn("RC000000");
  117. // real instance, no mock
  118. AFPPainter afpPainter = new AFPPainter(afpDocumentHandler);
  119. // build rectangle 200 x 50 (points, which are converted to millipoints)
  120. Rectangle rectangle = new Rectangle(0, 0, 200000, 50000);
  121. // build border properties
  122. int style = Constants.EN_SOLID;
  123. BorderProps.Mode mode = BorderProps.Mode.SEPARATE;
  124. Color color = Color.BLACK;
  125. int borderWidth = 4000;
  126. int radiusStart = 30000;
  127. int radiusEnd = 30000;
  128. BorderProps border1 = new BorderProps(style, borderWidth, radiusStart, radiusEnd, color, mode);
  129. BorderProps border2 = new BorderProps(style, borderWidth, radiusStart, radiusEnd, color, mode);
  130. BorderProps border3 = new BorderProps(style, borderWidth, radiusStart, radiusEnd, color, mode);
  131. BorderProps border4 = new BorderProps(style, borderWidth, radiusStart, radiusEnd, color, mode);
  132. when(imageManager.convertImage(any(Image.class), any(ImageFlavor[].class), any(Map.class)))
  133. .thenReturn(imageBuffered);
  134. afpPainter.drawBorderRect(rectangle, border1, border2, border3, border4, Color.WHITE);
  135. // note: here we would really like to verify that the second and third arguments passed to
  136. // handleImage() are the instances ib and rect declared above but that causes mockito to throw
  137. // an exception, probably because we cannot declare the AFPRenderingContext and are forced to
  138. // use any(), which forces the use of any() for all arguments
  139. verify(afpImageHandlerRenderedImage).handleImage(any(AFPRenderingContext.class),
  140. nullable(Image.class), any(Rectangle.class));
  141. }
  142. @Test
  143. public void testPresentationText() throws URISyntaxException, IFException, IOException {
  144. List<String> strings = new ArrayList<String>();
  145. strings.add("test");
  146. Assert.assertEquals(writeText(strings), "BEGIN DOCUMENT DOC00001\n"
  147. + "BEGIN PAGE PGN00001\n"
  148. + "BEGIN ACTIVE_ENVIRONMENT_GROUP AEG00001\n"
  149. + "DESCRIPTOR PAGE\n"
  150. + "MIGRATION PRESENTATION_TEXT\n"
  151. + "END ACTIVE_ENVIRONMENT_GROUP AEG00001\n"
  152. + "BEGIN PRESENTATION_TEXT PT000001\n"
  153. + "DATA PRESENTATION_TEXT\n"
  154. + "END PRESENTATION_TEXT PT000001\n"
  155. + "END PAGE PGN00001\n"
  156. + "END DOCUMENT DOC00001\n");
  157. for (int i = 0; i < 5000; i++) {
  158. strings.add("test");
  159. }
  160. Assert.assertEquals(writeText(strings), "BEGIN DOCUMENT DOC00001\n"
  161. + "BEGIN PAGE PGN00001\n"
  162. + "BEGIN ACTIVE_ENVIRONMENT_GROUP AEG00001\n"
  163. + "DESCRIPTOR PAGE\n"
  164. + "MIGRATION PRESENTATION_TEXT\n"
  165. + "END ACTIVE_ENVIRONMENT_GROUP AEG00001\n"
  166. + "BEGIN PRESENTATION_TEXT PT000001\n"
  167. + "DATA PRESENTATION_TEXT\n"
  168. + "END PRESENTATION_TEXT PT000001\n"
  169. + "BEGIN PRESENTATION_TEXT PT000002\n"
  170. + "DATA PRESENTATION_TEXT\n"
  171. + "END PRESENTATION_TEXT PT000002\n"
  172. + "END PAGE PGN00001\n"
  173. + "END DOCUMENT DOC00001\n");
  174. }
  175. @Test
  176. public void testPresentationText2() throws URISyntaxException, IFException, IOException {
  177. List<String> strings = new ArrayList<String>();
  178. for (int i = 0; i < 5000; i++) {
  179. strings.add("tes");
  180. }
  181. Assert.assertEquals(writeText(strings), "BEGIN DOCUMENT DOC00001\n"
  182. + "BEGIN PAGE PGN00001\n"
  183. + "BEGIN ACTIVE_ENVIRONMENT_GROUP AEG00001\n"
  184. + "DESCRIPTOR PAGE\n"
  185. + "MIGRATION PRESENTATION_TEXT\n"
  186. + "END ACTIVE_ENVIRONMENT_GROUP AEG00001\n"
  187. + "BEGIN PRESENTATION_TEXT PT000001\n"
  188. + "DATA PRESENTATION_TEXT\n"
  189. + "END PRESENTATION_TEXT PT000001\n"
  190. + "BEGIN PRESENTATION_TEXT PT000002\n"
  191. + "DATA PRESENTATION_TEXT\n"
  192. + "END PRESENTATION_TEXT PT000002\n"
  193. + "END PAGE PGN00001\n"
  194. + "END DOCUMENT DOC00001\n");
  195. }
  196. private String writeText(List<String> text) throws URISyntaxException, IOException, IFException {
  197. FOUserAgent agent = FopFactory.newInstance(new URI(".")).newFOUserAgent();
  198. IFContext context = new IFContext(agent);
  199. AFPDocumentHandler doc = new AFPDocumentHandler(context);
  200. AFPPainter afpPainter = new AFPPainter(doc);
  201. FontInfo fi = new FontInfo();
  202. fi.addFontProperties("", Font.DEFAULT_FONT);
  203. RasterFont rf = new RasterFont("", true);
  204. CharacterSet cs = mock(CharacterSet.class);
  205. CharactersetEncoder.EncodedChars encoder = mock(CharactersetEncoder.EncodedChars.class);
  206. when(cs.encodeChars(any(CharSequence.class))).thenReturn(encoder);
  207. when(encoder.getLength()).thenReturn(text.get(0).length());
  208. rf.addCharacterSet(12000, cs);
  209. fi.addMetrics("", rf);
  210. doc.setFontInfo(fi);
  211. afpPainter.setFont("any", "normal", 400, "", 12000, Color.BLACK);
  212. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  213. doc.setResult(new StreamResult(outputStream));
  214. doc.startDocument();
  215. doc.startPage(0, "", "", new Dimension());
  216. for (String s : text) {
  217. afpPainter.drawText(0, 0, 0, 0, null, s);
  218. }
  219. doc.endDocument();
  220. InputStream bis = new ByteArrayInputStream(outputStream.toByteArray());
  221. StringBuilder sb = new StringBuilder();
  222. new AFPParser(false).read(bis, sb);
  223. return sb.toString();
  224. }
  225. @Test
  226. public void testDrawBorderRect3() throws IFException, PropertyException, IOException {
  227. FOUserAgent ua = FopFactory.newInstance(new File(".").toURI()).newFOUserAgent();
  228. AFPDocumentHandler documentHandler = new AFPDocumentHandler(new IFContext(ua));
  229. ByteArrayOutputStream os = new ByteArrayOutputStream();
  230. documentHandler.setResult(new StreamResult(os));
  231. documentHandler.startDocument();
  232. documentHandler.startPage(0, "", "", new Dimension());
  233. AFPPainter afpPainter = new AFPPainter(documentHandler);
  234. drawBorder(afpPainter, ua);
  235. documentHandler.endDocument();
  236. InputStream bis = new ByteArrayInputStream(os.toByteArray());
  237. StringBuilder sb = new StringBuilder();
  238. new AFPParser(false).read(bis, sb);
  239. Assert.assertEquals(sb.toString(), "BEGIN DOCUMENT DOC00001\n"
  240. + "BEGIN PAGE PGN00001\n"
  241. + "BEGIN ACTIVE_ENVIRONMENT_GROUP AEG00001\n"
  242. + "DESCRIPTOR PAGE\n"
  243. + "MIGRATION PRESENTATION_TEXT\n"
  244. + "END ACTIVE_ENVIRONMENT_GROUP AEG00001\n"
  245. + "BEGIN PRESENTATION_TEXT PT000001\n"
  246. + "DATA PRESENTATION_TEXT\n"
  247. + "END PRESENTATION_TEXT PT000001\n"
  248. + "BEGIN PRESENTATION_TEXT PT000002\n"
  249. + "DATA PRESENTATION_TEXT\n"
  250. + "END PRESENTATION_TEXT PT000002\n"
  251. + "END PAGE PGN00001\n"
  252. + "END DOCUMENT DOC00001\n");
  253. }
  254. @Test
  255. public void testDrawBorderRectAndText() throws IFException, PropertyException, IOException {
  256. FOUserAgent ua = FopFactory.newInstance(new File(".").toURI()).newFOUserAgent();
  257. AFPDocumentHandler documentHandler = new AFPDocumentHandler(new IFContext(ua));
  258. documentHandler.setResolution(480);
  259. ByteArrayOutputStream os = new ByteArrayOutputStream();
  260. documentHandler.setResult(new StreamResult(os));
  261. documentHandler.startDocument();
  262. documentHandler.startPage(0, "", "", new Dimension());
  263. AFPPainter afpPainter = new AFPPainter(documentHandler);
  264. setFont(documentHandler, afpPainter);
  265. drawBorder(afpPainter, ua);
  266. StringBuilder text = new StringBuilder();
  267. for (int i = 0; i < 4770; i++) {
  268. text.append("a");
  269. }
  270. afpPainter.drawText(0, 0, 0, 0, null, text.toString());
  271. documentHandler.endDocument();
  272. InputStream bis = new ByteArrayInputStream(os.toByteArray());
  273. StringBuilder sb = new StringBuilder();
  274. new AFPParser(false).read(bis, sb);
  275. Assert.assertEquals(sb.toString(), "BEGIN DOCUMENT DOC00001\n"
  276. + "BEGIN PAGE PGN00001\n"
  277. + "BEGIN ACTIVE_ENVIRONMENT_GROUP AEG00001\n"
  278. + "DESCRIPTOR PAGE\n"
  279. + "MIGRATION PRESENTATION_TEXT\n"
  280. + "END ACTIVE_ENVIRONMENT_GROUP AEG00001\n"
  281. + "BEGIN PRESENTATION_TEXT PT000001\n"
  282. + "DATA PRESENTATION_TEXT\n"
  283. + "END PRESENTATION_TEXT PT000001\n"
  284. + "BEGIN PRESENTATION_TEXT PT000002\n"
  285. + "DATA PRESENTATION_TEXT\n"
  286. + "END PRESENTATION_TEXT PT000002\n"
  287. + "BEGIN PRESENTATION_TEXT PT000003\n"
  288. + "DATA PRESENTATION_TEXT\n"
  289. + "END PRESENTATION_TEXT PT000003\n"
  290. + "END PAGE PGN00001\n"
  291. + "END DOCUMENT DOC00001\n");
  292. }
  293. private void setFont(AFPDocumentHandler doc, AFPPainter afpPainter) throws IFException {
  294. FontInfo fi = new FontInfo();
  295. fi.addFontProperties("", Font.DEFAULT_FONT);
  296. RasterFont rf = new RasterFont("", true);
  297. CharacterSet cs = OutlineFontTestCase.getCharacterSet();
  298. rf.addCharacterSet(12000, cs);
  299. fi.addMetrics("", rf);
  300. doc.setFontInfo(fi);
  301. afpPainter.setFont("any", "normal", 400, "", 12000, Color.BLACK);
  302. }
  303. private void drawBorder(AFPPainter afpPainter, FOUserAgent ua) throws IFException, PropertyException {
  304. int style = Constants.EN_DOTTED;
  305. BorderProps.Mode mode = BorderProps.Mode.COLLAPSE_OUTER;
  306. Color color = ColorUtil.parseColorString(ua, "fop-rgb-icc(0.5019608,0.5019608,0.5019608,#CMYK,,0,0,0,0.5)");
  307. int borderWidth = 500;
  308. int radiusStart = 0;
  309. int radiusEnd = 0;
  310. BorderProps border1 = new BorderProps(style, borderWidth, radiusStart, radiusEnd, color, mode);
  311. afpPainter.drawBorderRect(new Rectangle(0, 0, 552755, 16090), null, border1, null, null, Color.WHITE);
  312. }
  313. @Test
  314. public void testPageGroup() throws IFException, IOException {
  315. FOUserAgent ua = FopFactory.newInstance(new File(".").toURI()).newFOUserAgent();
  316. AFPDocumentHandler documentHandler = new AFPDocumentHandler(new IFContext(ua));
  317. Map<QName, String> attributes = new HashMap<>();
  318. attributes.put(AFPElementMapping.PAGE_GROUP, "false");
  319. documentHandler.getContext().setForeignAttributes(attributes);
  320. ByteArrayOutputStream os = new ByteArrayOutputStream();
  321. documentHandler.setResult(new StreamResult(os));
  322. documentHandler.startDocument();
  323. documentHandler.startPageSequence(null);
  324. documentHandler.startPage(0, "", "", new Dimension());
  325. AFPPainter afpPainter = new AFPPainter(documentHandler);
  326. setFont(documentHandler, afpPainter);
  327. afpPainter.drawText(0, 0, 0, 0, null, "a");
  328. documentHandler.endPage();
  329. documentHandler.endPageSequence();
  330. attributes.clear();
  331. documentHandler.startPageSequence(null);
  332. documentHandler.startPage(0, "", "", new Dimension());
  333. afpPainter.drawText(0, 0, 0, 0, null, "a");
  334. documentHandler.endDocument();
  335. InputStream bis = new ByteArrayInputStream(os.toByteArray());
  336. StringBuilder sb = new StringBuilder();
  337. new AFPParser(false).read(bis, sb);
  338. Assert.assertEquals(sb.toString(), "BEGIN DOCUMENT DOC00001\n"
  339. + "BEGIN PAGE PGN00001\n"
  340. + "BEGIN ACTIVE_ENVIRONMENT_GROUP AEG00001\n"
  341. + "MAP CODED_FONT Triplets: "
  342. + "FULLY_QUALIFIED_NAME,FULLY_QUALIFIED_NAME,CHARACTER_ROTATION,RESOURCE_LOCAL_IDENTIFIER,\n"
  343. + "DESCRIPTOR PAGE\n"
  344. + "MIGRATION PRESENTATION_TEXT\n"
  345. + "END ACTIVE_ENVIRONMENT_GROUP AEG00001\n"
  346. + "BEGIN PRESENTATION_TEXT PT000001\n"
  347. + "DATA PRESENTATION_TEXT\n"
  348. + "END PRESENTATION_TEXT PT000001\n"
  349. + "END PAGE PGN00001\n"
  350. + "BEGIN PAGE_GROUP PGP00001\n"
  351. + "BEGIN PAGE PGN00002\n"
  352. + "BEGIN ACTIVE_ENVIRONMENT_GROUP AEG00002\n"
  353. + "DESCRIPTOR PAGE\n"
  354. + "MIGRATION PRESENTATION_TEXT\n"
  355. + "END ACTIVE_ENVIRONMENT_GROUP AEG00002\n"
  356. + "BEGIN PRESENTATION_TEXT PT000002\n"
  357. + "DATA PRESENTATION_TEXT\n"
  358. + "END PRESENTATION_TEXT PT000002\n"
  359. + "END PAGE PGN00002\n"
  360. + "END PAGE_GROUP PGP00001\n"
  361. + "END DOCUMENT DOC00001\n");
  362. }
  363. }