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.

TestFontRendering.java 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.hslf.usermodel;
  16. import static org.junit.Assert.*;
  17. import static org.junit.Assume.assumeTrue;
  18. import java.awt.*;
  19. import java.awt.geom.AffineTransform;
  20. import java.awt.geom.Rectangle2D;
  21. import java.awt.image.BufferedImage;
  22. import java.awt.image.DataBufferByte;
  23. import java.io.File;
  24. import java.io.InputStream;
  25. import java.util.*;
  26. import javax.imageio.ImageIO;
  27. import org.apache.poi.POIDataSamples;
  28. import org.apache.poi.sl.draw.Drawable;
  29. import org.apache.poi.util.TempFile;
  30. import org.junit.Ignore;
  31. import org.junit.Test;
  32. /**
  33. * Test font rendering of alternative and fallback fonts
  34. */
  35. public class TestFontRendering {
  36. private static POIDataSamples slTests = POIDataSamples.getSlideShowInstance();
  37. // @Ignore2("This fails on some systems because fonts are rendered slightly different")
  38. @Test
  39. public void bug55902mixedFontWithChineseCharacters() throws Exception {
  40. // font files need to be downloaded first via
  41. // ant test-scratchpad-download-resources
  42. String fontFiles[][] = {
  43. // Calibri is not available on *nix systems, so we need to use another similar free font
  44. { "build/scratchpad-test-resources/Cabin-Regular.ttf", "mapped", "Calibri" },
  45. // use "MS PGothic" if available (Windows only) ...
  46. // for the junit test not all chars are rendered
  47. { "build/scratchpad-test-resources/mona.ttf", "fallback", "Cabin" }
  48. };
  49. // setup fonts (especially needed, when run under *nix systems)
  50. GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
  51. Map<String,String> fontMap = new HashMap<String,String>();
  52. Map<String,String> fallbackMap = new HashMap<String,String>();
  53. for (String fontFile[] : fontFiles) {
  54. File f = new File(fontFile[0]);
  55. assumeTrue("necessary font file "+f.getName()+" not downloaded.", f.exists());
  56. Font font = Font.createFont(Font.TRUETYPE_FONT, f);
  57. ge.registerFont(font);
  58. Map<String,String> map = ("mapped".equals(fontFile[1]) ? fontMap : fallbackMap);
  59. map.put(fontFile[2], font.getFamily());
  60. }
  61. InputStream is = slTests.openResourceAsStream("bug55902-mixedFontChineseCharacters.ppt");
  62. HSLFSlideShow ss = new HSLFSlideShow(is);
  63. is.close();
  64. Dimension pgsize = ss.getPageSize();
  65. HSLFSlide slide = ss.getSlides().get(0);
  66. // render it
  67. double zoom = 1;
  68. AffineTransform at = new AffineTransform();
  69. at.setToScale(zoom, zoom);
  70. BufferedImage imgActual = new BufferedImage((int)Math.ceil(pgsize.width*zoom), (int)Math.ceil(pgsize.height*zoom), BufferedImage.TYPE_3BYTE_BGR);
  71. Graphics2D graphics = imgActual.createGraphics();
  72. graphics.setRenderingHint(Drawable.FONT_FALLBACK, fallbackMap);
  73. graphics.setRenderingHint(Drawable.FONT_MAP, fontMap);
  74. graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  75. graphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
  76. graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
  77. graphics.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
  78. graphics.setTransform(at);
  79. graphics.setPaint(Color.white);
  80. graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));
  81. slide.draw(graphics);
  82. BufferedImage imgExpected = ImageIO.read(slTests.getFile("bug55902-mixedChars.png"));
  83. DataBufferByte expectedDB = (DataBufferByte)imgExpected.getRaster().getDataBuffer();
  84. DataBufferByte actualDB = (DataBufferByte)imgActual.getRaster().getDataBuffer();
  85. byte[] expectedData = expectedDB.getData(0);
  86. byte[] actualData = actualDB.getData(0);
  87. // allow to find out what the actual difference is in CI where this fails currently
  88. if(!Arrays.equals(expectedData, actualData)) {
  89. ImageIO.write(imgActual, "PNG", TempFile.createTempFile("TestFontRendering", ".png"));
  90. }
  91. assertArrayEquals("Expected to have matching raster-arrays, but found differences", expectedData, actualData);
  92. }
  93. }