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.

ImagePreloaderTestCase.java 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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.image.loader.batik;
  19. import java.io.File;
  20. import java.io.IOException;
  21. import org.junit.Ignore;
  22. import org.junit.Test;
  23. import static org.junit.Assert.assertEquals;
  24. import static org.junit.Assert.assertNotNull;
  25. import org.apache.xmlgraphics.image.loader.ImageException;
  26. import org.apache.xmlgraphics.image.loader.ImageInfo;
  27. import org.apache.xmlgraphics.image.loader.ImageManager;
  28. import org.apache.fop.apps.FOUserAgent;
  29. import org.apache.fop.apps.FopFactory;
  30. import org.apache.fop.apps.FopFactoryBuilder;
  31. import org.apache.fop.apps.MimeConstants;
  32. /**
  33. * Tests for bundled image preloader implementations.
  34. */
  35. public class ImagePreloaderTestCase {
  36. private FopFactory fopFactory;
  37. public ImagePreloaderTestCase() {
  38. FopFactoryBuilder builder = new FopFactoryBuilder(new File(".").toURI());
  39. builder.setSourceResolution(72);
  40. builder.setTargetResolution(300);
  41. fopFactory = builder.build();
  42. }
  43. @Test
  44. public void testSVG() throws Exception {
  45. String uri = "test/resources/images/img-w-size.svg";
  46. checkSVGFile(uri);
  47. }
  48. @Test
  49. public void testSVGZ() throws Exception {
  50. String uri = "test/resources/images/img-w-size.svgz";
  51. checkSVGFile(uri);
  52. }
  53. private void checkSVGFile(String uri) throws ImageException, IOException {
  54. FOUserAgent userAgent = fopFactory.newFOUserAgent();
  55. ImageManager manager = fopFactory.getImageManager();
  56. ImageInfo info = manager.preloadImage(uri, userAgent.getImageSessionContext());
  57. assertNotNull("ImageInfo must not be null", info);
  58. assertEquals(MimeConstants.MIME_SVG, info.getMimeType());
  59. assertEquals(uri, info.getOriginalURI());
  60. assertEquals(16, info.getSize().getWidthPx());
  61. assertEquals(16, info.getSize().getHeightPx());
  62. assertEquals(userAgent.getSourceResolution(), info.getSize().getDpiHorizontal(), 0.1);
  63. assertEquals(16000, info.getSize().getWidthMpt());
  64. assertEquals(16000, info.getSize().getHeightMpt());
  65. }
  66. @Test
  67. public void testSVGNoSize() throws Exception {
  68. String uri = "test/resources/images/img.svg";
  69. FOUserAgent userAgent = fopFactory.newFOUserAgent();
  70. ImageManager manager = fopFactory.getImageManager();
  71. ImageInfo info = manager.preloadImage(uri, userAgent.getImageSessionContext());
  72. assertNotNull("ImageInfo must not be null", info);
  73. assertEquals(MimeConstants.MIME_SVG, info.getMimeType());
  74. assertEquals(uri, info.getOriginalURI());
  75. assertEquals(100, info.getSize().getWidthPx()); //100 = default viewport size
  76. assertEquals(100, info.getSize().getHeightPx());
  77. assertEquals(userAgent.getSourceResolution(), info.getSize().getDpiHorizontal(), 0.1);
  78. assertEquals(100000, info.getSize().getWidthMpt());
  79. assertEquals(100000, info.getSize().getHeightMpt());
  80. }
  81. @Test
  82. @Ignore("Batik has not yet been handled")
  83. public void testSVGWithDOM() throws Exception {
  84. String uri = "my:SVGImage";
  85. FOUserAgent userAgent = fopFactory.newFOUserAgent();
  86. // TODO: SORT THIS OUT!!
  87. // userAgent.setURIResolver(new URIResolver() {
  88. //
  89. // public Source resolve(String href, String base) throws TransformerException {
  90. // if (href.startsWith("my:")) {
  91. // DOMImplementation impl = SVGDOMImplementation.getDOMImplementation();
  92. // String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
  93. // Document doc = impl.createDocument(svgNS, "svg", null);
  94. // Element element = doc.getDocumentElement();
  95. // element.setAttribute("viewBox", "0 0 20 20");
  96. // element.setAttribute("width", "20pt");
  97. // element.setAttribute("height", "20pt");
  98. //
  99. // Element rect = doc.createElementNS(svgNS, "rect");
  100. // rect.setAttribute("x", "5");
  101. // rect.setAttribute("y", "5");
  102. // rect.setAttribute("width", "10");
  103. // rect.setAttribute("height", "10");
  104. // element.appendChild(rect);
  105. //
  106. // DOMSource src = new DOMSource(doc);
  107. // return src;
  108. // } else {
  109. // return null;
  110. // }
  111. // }
  112. //
  113. // });
  114. ImageManager manager = fopFactory.getImageManager();
  115. ImageInfo info = manager.preloadImage(uri, userAgent.getImageSessionContext());
  116. assertNotNull("ImageInfo must not be null", info);
  117. assertEquals(MimeConstants.MIME_SVG, info.getMimeType());
  118. assertEquals(uri, info.getOriginalURI());
  119. assertEquals(20, info.getSize().getWidthPx()); //100 = default viewport size
  120. assertEquals(20, info.getSize().getHeightPx());
  121. assertEquals(userAgent.getSourceResolution(), info.getSize().getDpiHorizontal(), 0.1);
  122. assertEquals(20000, info.getSize().getWidthMpt());
  123. assertEquals(20000, info.getSize().getHeightMpt());
  124. }
  125. @Test
  126. public void testWMF() throws Exception {
  127. String uri = "test/resources/images/testChart.wmf";
  128. FOUserAgent userAgent = fopFactory.newFOUserAgent();
  129. ImageManager manager = fopFactory.getImageManager();
  130. ImageInfo info = manager.preloadImage(uri, userAgent.getImageSessionContext());
  131. assertNotNull("ImageInfo must not be null", info);
  132. assertEquals(ImageWMF.MIME_WMF, info.getMimeType());
  133. assertEquals(uri, info.getOriginalURI());
  134. assertEquals(27940, info.getSize().getWidthPx());
  135. assertEquals(21590, info.getSize().getHeightPx());
  136. assertEquals(2540, info.getSize().getDpiHorizontal(), 0.1);
  137. assertEquals(792000, info.getSize().getWidthMpt());
  138. assertEquals(612000, info.getSize().getHeightMpt());
  139. }
  140. }