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 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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.tests;
  16. import static org.apache.poi.sl.tests.SLCommonUtils.xslfOnly;
  17. import static org.junit.jupiter.api.Assertions.assertTrue;
  18. import static org.junit.jupiter.api.Assumptions.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.HashMap;
  31. import java.util.Map;
  32. import org.apache.poi.POIDataSamples;
  33. import org.apache.poi.common.usermodel.fonts.FontGroup;
  34. import org.apache.poi.sl.draw.Drawable;
  35. import org.apache.poi.sl.usermodel.Slide;
  36. import org.apache.poi.sl.usermodel.SlideShow;
  37. import org.apache.poi.sl.usermodel.SlideShowFactory;
  38. import org.apache.poi.sl.usermodel.StrokeStyle.LineDash;
  39. import org.apache.poi.sl.usermodel.TextBox;
  40. import org.apache.poi.sl.usermodel.TextParagraph;
  41. import org.apache.poi.sl.usermodel.TextRun;
  42. import org.apache.poi.xslf.usermodel.XMLSlideShow;
  43. import org.junit.jupiter.api.BeforeAll;
  44. import org.junit.jupiter.api.Test;
  45. /**
  46. * Test rendering - specific to font handling
  47. */
  48. class TestFonts {
  49. private static final POIDataSamples _slTests = POIDataSamples.getSlideShowInstance();
  50. private static final String JPTEXT =
  51. "\u3061\u3087\u3063\u3068\u65E9\u3044\u3051\u3069T\u30B7\u30E3\u30C4\u304C\u7740\u305F\u304F\u306A" +
  52. "\u308B\u5B63\u7BC0\u2661\u304A\u6BCD\u3055\u3093\u306E\u5F71\u97FF\u304B\u3001\u975E\u5E38\u306B" +
  53. "\u6050\u7ADC\u304C\u5927\u597D\u304D\u3067\u3059\u3002\u3082\u3046\u98FC\u3044\u305F\u3044\u304F" +
  54. "\u3089\u3044\u5927\u597D\u304D\u3067\u3059\u3002#\u30B8\u30E5\u30E9\u30B7\u30C3\u30AF\u30EF\u30FC" +
  55. "\u30EB\u30C9 \u306E\u30E9\u30D7\u30C8\u30EB4\u59C9\u59B9\u3068\u304B\u6FC0\u7684\u306B\u53EF\u611B" +
  56. "\u304F\u3066\u53EF\u611B\u304F\u3066\u53EF\u611B\u304F\u3066\u53EF\u611B\u3044\u3067\u3059\u3002" +
  57. "\u3081\u308D\u3081\u308D\u3001\u5927\u597D\u304D\u2661\u304A\u6BCD\u3055\u3093\u3082\u6050\u7ADC" +
  58. "\u304C\u597D\u304D\u3067\u3001\u5C0F\u3055\u3044\u9803\u3001\u53E4\u4EE3\u751F\u7269\u306E\u56F3" +
  59. "\u9451\u3092\u4E00\u7DD2\u306B\u898B\u3066\u305F\u306E\u601D\u3044\u51FA\u3059\u301C\u3068\u3044";
  60. private static final String[] INIT_FONTS = {"mona.ttf"};
  61. @BeforeAll
  62. public static void initGE() throws FontFormatException, IOException {
  63. GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
  64. for (String s : INIT_FONTS) {
  65. Font font = Font.createFont(Font.TRUETYPE_FONT, _slTests.getFile(s));
  66. ge.registerFont(font);
  67. }
  68. }
  69. @Test
  70. void resizeToFitTextHSLF() throws IOException {
  71. assumeFalse(xslfOnly());
  72. SlideShow<?,?> ppt = SlideShowFactory.create(false);
  73. resizeToFitText(ppt);
  74. ppt.close();
  75. }
  76. @Test
  77. void resizeToFitTextXSLF() throws IOException {
  78. SlideShow<?,?> ppt = new XMLSlideShow();
  79. resizeToFitText(ppt);
  80. ppt.close();
  81. }
  82. private void resizeToFitText(SlideShow<?,?> slideshow) throws IOException {
  83. Slide<?,?> sld = slideshow.createSlide();
  84. TextBox<?,?> tb = sld.createTextBox();
  85. tb.setAnchor(new Rectangle(50, 50, 200, 50));
  86. tb.setStrokeStyle(Color.black, LineDash.SOLID, 3);
  87. tb.setText(JPTEXT);
  88. setFont(tb, "NoSuchFont", FontGroup.LATIN);
  89. Dimension pgsize = slideshow.getPageSize();
  90. int width = (int)pgsize.getWidth();
  91. int height = (int)pgsize.getHeight();
  92. BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
  93. Graphics2D graphics = img.createGraphics();
  94. Map<String,String> fallbackMap = new HashMap<>();
  95. fallbackMap.put("NoSuchFont", "Mona");
  96. // in XSLF the fonts default to the theme fonts (Calibri), if the font group is not overridden
  97. // see XSLFTextRun.XSLFTextInfo.getCTTextFont
  98. fallbackMap.put("Calibri", "Mona");
  99. graphics.setRenderingHint(Drawable.FONT_FALLBACK, fallbackMap);
  100. graphics.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
  101. tb.resizeToFitText(graphics);
  102. graphics.dispose();
  103. Rectangle2D anc = tb.getAnchor();
  104. // ignore font metrics differences on windows / linux (... hopefully ...)
  105. int tbHeight = (int)anc.getHeight();
  106. assertTrue(tbHeight > 100);
  107. }
  108. private void setFont(TextBox<?,?> tb, String fontFamily, FontGroup fontGroup) {
  109. // TODO: set east asian font family - MS Office uses "MS Mincho" or "MS Gothic" as a fallback
  110. for (TextParagraph<?,?,? extends TextRun> p : tb.getTextParagraphs()) {
  111. for (TextRun r : p.getTextRuns()) {
  112. r.setFontFamily(fontFamily, fontGroup);
  113. }
  114. }
  115. }
  116. }