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.6KB

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