您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ImageLoaderTestCase.java 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 junit.framework.TestCase;
  21. import org.apache.xmlgraphics.image.loader.Image;
  22. import org.apache.xmlgraphics.image.loader.ImageFlavor;
  23. import org.apache.xmlgraphics.image.loader.ImageInfo;
  24. import org.apache.xmlgraphics.image.loader.ImageManager;
  25. import org.apache.xmlgraphics.image.loader.impl.ImageRendered;
  26. import org.apache.xmlgraphics.image.loader.impl.ImageXMLDOM;
  27. import org.apache.xmlgraphics.image.writer.ImageWriterUtil;
  28. import org.apache.fop.apps.FOUserAgent;
  29. import org.apache.fop.apps.FopFactory;
  30. /**
  31. * Tests for bundled ImageLoader implementations.
  32. */
  33. public class ImageLoaderTestCase extends TestCase {
  34. private static final File DEBUG_TARGET_DIR = null; //new File("D:/");
  35. private FopFactory fopFactory;
  36. public ImageLoaderTestCase(String name) {
  37. super(name);
  38. fopFactory = FopFactory.newInstance();
  39. fopFactory.setSourceResolution(72);
  40. fopFactory.setTargetResolution(300);
  41. }
  42. public void testSVG() throws Exception {
  43. String uri = "test/resources/images/img-w-size.svg";
  44. FOUserAgent userAgent = fopFactory.newFOUserAgent();
  45. ImageManager manager = fopFactory.getImageManager();
  46. ImageInfo info = manager.preloadImage(uri, userAgent.getImageSessionContext());
  47. assertNotNull("ImageInfo must not be null", info);
  48. Image img = manager.getImage(info, ImageFlavor.XML_DOM,
  49. userAgent.getImageSessionContext());
  50. assertNotNull("Image must not be null", img);
  51. assertEquals(ImageFlavor.XML_DOM, img.getFlavor());
  52. ImageXMLDOM imgDom = (ImageXMLDOM)img;
  53. assertNotNull(imgDom.getDocument());
  54. assertEquals("http://www.w3.org/2000/svg", imgDom.getRootNamespace());
  55. info = imgDom.getInfo(); //Switch to the ImageInfo returned by the image
  56. assertEquals(16000, info.getSize().getWidthMpt());
  57. assertEquals(16000, info.getSize().getHeightMpt());
  58. img = manager.getImage(info, ImageFlavor.RENDERED_IMAGE,
  59. userAgent.getImageSessionContext());
  60. assertNotNull("Image must not be null", img);
  61. assertEquals(ImageFlavor.RENDERED_IMAGE, img.getFlavor());
  62. ImageRendered imgRed = (ImageRendered)img;
  63. assertNotNull(imgRed.getRenderedImage());
  64. if (DEBUG_TARGET_DIR != null) {
  65. ImageWriterUtil.saveAsPNG(imgRed.getRenderedImage(),
  66. (int)userAgent.getTargetResolution(),
  67. new File(DEBUG_TARGET_DIR, "out.svg.png"));
  68. }
  69. assertEquals(67, imgRed.getRenderedImage().getWidth());
  70. assertEquals(67, imgRed.getRenderedImage().getHeight());
  71. info = imgRed.getInfo(); //Switch to the ImageInfo returned by the image
  72. assertEquals(16000, info.getSize().getWidthMpt());
  73. assertEquals(16000, info.getSize().getHeightMpt());
  74. }
  75. public void testSVGNoViewbox() throws Exception {
  76. String uri = "test/resources/images/circles.svg";
  77. FopFactory ff = FopFactory.newInstance();
  78. ff.setSourceResolution(96);
  79. ff.setTargetResolution(300);
  80. FOUserAgent userAgent = ff.newFOUserAgent();
  81. ImageManager manager = ff.getImageManager();
  82. ImageInfo info = manager.preloadImage(uri, userAgent.getImageSessionContext());
  83. assertNotNull("ImageInfo must not be null", info);
  84. Image img = manager.getImage(info, ImageFlavor.XML_DOM,
  85. userAgent.getImageSessionContext());
  86. assertNotNull("Image must not be null", img);
  87. assertEquals(ImageFlavor.XML_DOM, img.getFlavor());
  88. ImageXMLDOM imgDom = (ImageXMLDOM)img;
  89. assertNotNull(imgDom.getDocument());
  90. assertEquals("http://www.w3.org/2000/svg", imgDom.getRootNamespace());
  91. info = imgDom.getInfo(); //Switch to the ImageInfo returned by the image
  92. assertEquals(96, info.getSize().getDpiHorizontal(), 0);
  93. assertEquals(340158, info.getSize().getWidthMpt());
  94. assertEquals(340158, info.getSize().getHeightMpt());
  95. assertEquals(454, info.getSize().getWidthPx());
  96. assertEquals(454, info.getSize().getHeightPx());
  97. img = manager.getImage(info, ImageFlavor.RENDERED_IMAGE,
  98. userAgent.getImageSessionContext());
  99. assertNotNull("Image must not be null", img);
  100. assertEquals(ImageFlavor.RENDERED_IMAGE, img.getFlavor());
  101. ImageRendered imgRed = (ImageRendered)img;
  102. assertNotNull(imgRed.getRenderedImage());
  103. if (DEBUG_TARGET_DIR != null) {
  104. ImageWriterUtil.saveAsPNG(imgRed.getRenderedImage(),
  105. (int)userAgent.getTargetResolution(),
  106. new File(DEBUG_TARGET_DIR, "circles.svg.png"));
  107. }
  108. assertEquals(1418, imgRed.getRenderedImage().getWidth());
  109. assertEquals(1418, imgRed.getRenderedImage().getHeight());
  110. info = imgRed.getInfo(); //Switch to the ImageInfo returned by the image
  111. assertEquals(340158, info.getSize().getWidthMpt());
  112. assertEquals(340158, info.getSize().getHeightMpt());
  113. }
  114. public void testWMF() throws Exception {
  115. String uri = "test/resources/images/testChart.wmf";
  116. FOUserAgent userAgent = fopFactory.newFOUserAgent();
  117. ImageManager manager = fopFactory.getImageManager();
  118. ImageInfo info = manager.preloadImage(uri, userAgent.getImageSessionContext());
  119. assertNotNull("ImageInfo must not be null", info);
  120. Image img = manager.getImage(info, ImageFlavor.RENDERED_IMAGE,
  121. userAgent.getImageSessionContext());
  122. assertNotNull("Image must not be null", img);
  123. assertEquals(ImageFlavor.RENDERED_IMAGE, img.getFlavor());
  124. ImageRendered imgRed = (ImageRendered)img;
  125. assertNotNull(imgRed.getRenderedImage());
  126. if (DEBUG_TARGET_DIR != null) {
  127. ImageWriterUtil.saveAsPNG(imgRed.getRenderedImage(),
  128. (int)userAgent.getTargetResolution(),
  129. new File(DEBUG_TARGET_DIR, "out.wmf.png"));
  130. }
  131. assertEquals(3300, imgRed.getRenderedImage().getWidth());
  132. assertEquals(2550, imgRed.getRenderedImage().getHeight());
  133. info = imgRed.getInfo(); //Switch to the ImageInfo returned by the image
  134. assertEquals(792000, info.getSize().getWidthMpt());
  135. assertEquals(612000, info.getSize().getHeightMpt());
  136. }
  137. }