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.

TestFonts.java 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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.sl;
  16. import static org.apache.poi.sl.SLCommonUtils.xslfOnly;
  17. import static org.junit.Assert.assertTrue;
  18. import static org.junit.Assume.assumeFalse;
  19. import java.awt.Color;
  20. import java.awt.Dimension;
  21. import java.awt.Font;
  22. import java.awt.FontFormatException;
  23. import java.awt.Graphics2D;
  24. import java.awt.GraphicsEnvironment;
  25. import java.awt.Rectangle;
  26. import java.awt.RenderingHints;
  27. import java.awt.geom.Rectangle2D;
  28. import java.awt.image.BufferedImage;
  29. import java.io.IOException;
  30. import java.util.Arrays;
  31. import java.util.HashMap;
  32. import java.util.Map;
  33. import org.apache.poi.POIDataSamples;
  34. import org.apache.poi.common.usermodel.fonts.FontGroup;
  35. import org.apache.poi.sl.draw.DrawFactory;
  36. import org.apache.poi.sl.draw.Drawable;
  37. import org.apache.poi.sl.usermodel.Slide;
  38. import org.apache.poi.sl.usermodel.SlideShow;
  39. import org.apache.poi.sl.usermodel.StrokeStyle.LineDash;
  40. import org.apache.poi.sl.usermodel.TextBox;
  41. import org.apache.poi.sl.usermodel.TextParagraph;
  42. import org.apache.poi.sl.usermodel.TextRun;
  43. import org.apache.poi.xslf.usermodel.XMLSlideShow;
  44. import org.junit.BeforeClass;
  45. import org.junit.Test;
  46. /**
  47. * Test rendering - specific to font handling
  48. */
  49. public class TestFonts {
  50. private static POIDataSamples _slTests = POIDataSamples.getSlideShowInstance();
  51. private static final String JPTEXT =
  52. "\u3061\u3087\u3063\u3068\u65E9\u3044\u3051\u3069T\u30B7\u30E3\u30C4\u304C\u7740\u305F\u304F\u306A" +
  53. "\u308B\u5B63\u7BC0\u2661\u304A\u6BCD\u3055\u3093\u306E\u5F71\u97FF\u304B\u3001\u975E\u5E38\u306B" +
  54. "\u6050\u7ADC\u304C\u5927\u597D\u304D\u3067\u3059\u3002\u3082\u3046\u98FC\u3044\u305F\u3044\u304F" +
  55. "\u3089\u3044\u5927\u597D\u304D\u3067\u3059\u3002#\u30B8\u30E5\u30E9\u30B7\u30C3\u30AF\u30EF\u30FC" +
  56. "\u30EB\u30C9 \u306E\u30E9\u30D7\u30C8\u30EB4\u59C9\u59B9\u3068\u304B\u6FC0\u7684\u306B\u53EF\u611B" +
  57. "\u304F\u3066\u53EF\u611B\u304F\u3066\u53EF\u611B\u304F\u3066\u53EF\u611B\u3044\u3067\u3059\u3002" +
  58. "\u3081\u308D\u3081\u308D\u3001\u5927\u597D\u304D\u2661\u304A\u6BCD\u3055\u3093\u3082\u6050\u7ADC" +
  59. "\u304C\u597D\u304D\u3067\u3001\u5C0F\u3055\u3044\u9803\u3001\u53E4\u4EE3\u751F\u7269\u306E\u56F3" +
  60. "\u9451\u3092\u4E00\u7DD2\u306B\u898B\u3066\u305F\u306E\u601D\u3044\u51FA\u3059\u301C\u3068\u3044";
  61. private static final String INIT_FONTS[] = { "mona.ttf" };
  62. // currently linux and mac return quite different values
  63. private static final int[] expected_sizes = {
  64. 304, // windows 10, 1080p, MS Office 2016, system text scaling 100% instead of default 125%
  65. 306, 308,// Windows 10, 15.6" 3840x2160
  66. 311, 312, 313, 318,
  67. 348, // Windows 10, 15.6" 3840x2160
  68. 362, // Windows 10, 13.3" 1080p high-dpi
  69. 372, // Ubuntu Xenial, 15", 1680x1050
  70. 377, 391, 398, 399, // Mac
  71. 406 // Ubuntu Xenial, 15", 1680x1050
  72. };
  73. @BeforeClass
  74. public static void initGE() throws FontFormatException, IOException {
  75. GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
  76. for (String s : INIT_FONTS) {
  77. Font font = Font.createFont(Font.TRUETYPE_FONT, _slTests.getFile(s));
  78. ge.registerFont(font);
  79. }
  80. }
  81. @Test
  82. public void resizeToFitTextHSLF() throws IOException, ReflectiveOperationException {
  83. assumeFalse(xslfOnly());
  84. SlideShow<?,?> ppt = (SlideShow<?,?>)Class.forName("org.apache.poi.hslf.usermodel.HSLFSlideShow").newInstance();
  85. resizeToFitText(ppt);
  86. ppt.close();
  87. }
  88. @Test
  89. public void resizeToFitTextXSLF() throws IOException {
  90. SlideShow<?,?> ppt = new XMLSlideShow();
  91. resizeToFitText(ppt);
  92. ppt.close();
  93. }
  94. private void resizeToFitText(SlideShow<?,?> slideshow) throws IOException {
  95. Slide<?,?> sld = slideshow.createSlide();
  96. TextBox<?,?> tb = sld.createTextBox();
  97. tb.setAnchor(new Rectangle(50, 50, 200, 50));
  98. tb.setStrokeStyle(Color.black, LineDash.SOLID, 3);
  99. tb.setText(JPTEXT);
  100. setFont(tb, "NoSuchFont", FontGroup.LATIN);
  101. Dimension pgsize = slideshow.getPageSize();
  102. int width = (int)pgsize.getWidth();
  103. int height = (int)pgsize.getHeight();
  104. BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
  105. Graphics2D graphics = img.createGraphics();
  106. Map<String,String> fallbackMap = new HashMap<>();
  107. fallbackMap.put("NoSuchFont", "Mona");
  108. // in XSLF the fonts default to the theme fonts (Calibri), if the font group is not overridden
  109. // see XSLFTextRun.XSLFTextInfo.getCTTextFont
  110. fallbackMap.put("Calibri", "Mona");
  111. graphics.setRenderingHint(Drawable.FONT_FALLBACK, fallbackMap);
  112. graphics.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
  113. tb.resizeToFitText(graphics);
  114. graphics.dispose();
  115. Rectangle2D anc = tb.getAnchor();
  116. // ignore font metrics differences on windows / linux (... hopefully ...)
  117. int tbHeight = (int)anc.getHeight();
  118. boolean found = Arrays.binarySearch(expected_sizes, tbHeight) > -1;
  119. assertTrue(tbHeight+" wasn't within the expected sizes: "+Arrays.toString(expected_sizes), found);
  120. }
  121. private void setFont(TextBox<?,?> tb, String fontFamily, FontGroup fontGroup) {
  122. // TODO: set east asian font family - MS Office uses "MS Mincho" or "MS Gothic" as a fallback
  123. // see https://stackoverflow.com/questions/26063828 for good explanation about the font metrics
  124. // differences on different environments
  125. for (TextParagraph<?,?,? extends TextRun> p : tb.getTextParagraphs()) {
  126. for (TextRun r : p.getTextRuns()) {
  127. r.setFontFamily(fontFamily, fontGroup);
  128. }
  129. }
  130. }
  131. }