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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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.assertTrue;
  17. import static org.junit.Assume.assumeTrue;
  18. import java.awt.Color;
  19. import java.awt.Dimension;
  20. import java.awt.Font;
  21. import java.awt.Graphics2D;
  22. import java.awt.GraphicsEnvironment;
  23. import java.awt.RenderingHints;
  24. import java.awt.geom.AffineTransform;
  25. import java.awt.geom.Rectangle2D;
  26. import java.awt.image.BufferedImage;
  27. import java.awt.image.DataBufferByte;
  28. import java.io.File;
  29. import java.io.InputStream;
  30. import java.util.Arrays;
  31. import java.util.HashMap;
  32. import java.util.Map;
  33. import javax.imageio.ImageIO;
  34. import org.apache.poi.POIDataSamples;
  35. import org.apache.poi.hslf.model.Slide;
  36. import org.apache.poi.hslf.model.TextPainter;
  37. import org.junit.Test;
  38. /**
  39. * Test font rendering of alternative and fallback fonts
  40. */
  41. public class TestFontRendering {
  42. private static POIDataSamples slTests = POIDataSamples.getSlideShowInstance();
  43. @Test
  44. public void bug55902mixedFontWithChineseCharacters() throws Exception {
  45. // font files need to be downloaded first via
  46. // ant test-scratchpad-download-resources
  47. String fontFiles[][] = {
  48. // Calibri is not available on *nix systems, so we need to use another similar free font
  49. { "build/scratchpad-test-resources/Cabin-Regular.ttf", "mapped", "Calibri" },
  50. // use "MS PGothic" if available (Windows only) ...
  51. // for the junit test not all chars are rendered
  52. { "build/scratchpad-test-resources/mona.ttf", "fallback", "Cabin" }
  53. };
  54. // setup fonts (especially needed, when run under *nix systems)
  55. GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
  56. Map<String,String> fontMap = new HashMap<String,String>();
  57. Map<String,String> fallbackMap = new HashMap<String,String>();
  58. for (String fontFile[] : fontFiles) {
  59. File f = new File(fontFile[0]);
  60. assumeTrue("necessary font file "+f.getName()+" not downloaded.", f.exists());
  61. Font font = Font.createFont(Font.TRUETYPE_FONT, f);
  62. ge.registerFont(font);
  63. Map<String,String> map = ("mapped".equals(fontFile[1]) ? fontMap : fallbackMap);
  64. map.put(fontFile[2], font.getFamily());
  65. }
  66. InputStream is = slTests.openResourceAsStream("bug55902-mixedFontChineseCharacters.ppt");
  67. SlideShow ss = new SlideShow(is);
  68. is.close();
  69. Dimension pgsize = ss.getPageSize();
  70. Slide slide = ss.getSlides()[0];
  71. // render it
  72. double zoom = 1;
  73. AffineTransform at = new AffineTransform();
  74. at.setToScale(zoom, zoom);
  75. BufferedImage imgActual = new BufferedImage((int)Math.ceil(pgsize.width*zoom), (int)Math.ceil(pgsize.height*zoom), BufferedImage.TYPE_3BYTE_BGR);
  76. Graphics2D graphics = imgActual.createGraphics();
  77. graphics.setRenderingHint(TextPainter.KEY_FONTFALLBACK, fallbackMap);
  78. graphics.setRenderingHint(TextPainter.KEY_FONTMAP, fontMap);
  79. graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  80. graphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
  81. graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
  82. graphics.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
  83. graphics.setTransform(at);
  84. graphics.setPaint(Color.white);
  85. graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));
  86. slide.draw(graphics);
  87. BufferedImage imgExpected = ImageIO.read(slTests.getFile("bug55902-mixedChars.png"));
  88. DataBufferByte expectedDB = (DataBufferByte)imgExpected.getRaster().getDataBuffer();
  89. DataBufferByte actualDB = (DataBufferByte)imgActual.getRaster().getDataBuffer();
  90. assertTrue(Arrays.equals(expectedDB.getData(0), actualDB.getData(0)));
  91. }
  92. }