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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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.assertEquals;
  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.HashMap;
  31. import java.util.Map;
  32. import org.apache.poi.POIDataSamples;
  33. import org.apache.poi.hslf.usermodel.HSLFSlideShow;
  34. import org.apache.poi.sl.draw.DrawFactory;
  35. import org.apache.poi.sl.draw.Drawable;
  36. import org.apache.poi.sl.usermodel.Slide;
  37. import org.apache.poi.sl.usermodel.SlideShow;
  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.apache.poi.xslf.usermodel.XSLFTextRun;
  44. import org.junit.BeforeClass;
  45. import org.junit.Test;
  46. import org.openxmlformats.schemas.drawingml.x2006.main.CTRegularTextRun;
  47. /**
  48. * Test rendering - specific to font handling
  49. */
  50. public class TestFonts {
  51. private static POIDataSamples _slTests = POIDataSamples.getSlideShowInstance();
  52. private static final String JPTEXT =
  53. "\u3061\u3087\u3063\u3068\u65E9\u3044\u3051\u3069T\u30B7\u30E3\u30C4\u304C\u7740\u305F\u304F\u306A" +
  54. "\u308B\u5B63\u7BC0\u2661\u304A\u6BCD\u3055\u3093\u306E\u5F71\u97FF\u304B\u3001\u975E\u5E38\u306B" +
  55. "\u6050\u7ADC\u304C\u5927\u597D\u304D\u3067\u3059\u3002\u3082\u3046\u98FC\u3044\u305F\u3044\u304F" +
  56. "\u3089\u3044\u5927\u597D\u304D\u3067\u3059\u3002#\u30B8\u30E5\u30E9\u30B7\u30C3\u30AF\u30EF\u30FC" +
  57. "\u30EB\u30C9 \u306E\u30E9\u30D7\u30C8\u30EB4\u59C9\u59B9\u3068\u304B\u6FC0\u7684\u306B\u53EF\u611B" +
  58. "\u304F\u3066\u53EF\u611B\u304F\u3066\u53EF\u611B\u304F\u3066\u53EF\u611B\u3044\u3067\u3059\u3002" +
  59. "\u3081\u308D\u3081\u308D\u3001\u5927\u597D\u304D\u2661\u304A\u6BCD\u3055\u3093\u3082\u6050\u7ADC" +
  60. "\u304C\u597D\u304D\u3067\u3001\u5C0F\u3055\u3044\u9803\u3001\u53E4\u4EE3\u751F\u7269\u306E\u56F3" +
  61. "\u9451\u3092\u4E00\u7DD2\u306B\u898B\u3066\u305F\u306E\u601D\u3044\u51FA\u3059\u301C\u3068\u3044";
  62. private static final String INIT_FONTS[] = { "mona.ttf" };
  63. @BeforeClass
  64. public static void initGE() throws FontFormatException, IOException {
  65. GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
  66. for (String s : INIT_FONTS) {
  67. Font font = Font.createFont(Font.TRUETYPE_FONT, _slTests.getFile(s));
  68. ge.registerFont(font);
  69. }
  70. }
  71. @Test
  72. public void resizeToFitTextHSLF() throws IOException {
  73. assumeFalse(xslfOnly());
  74. SlideShow<?,?> ppt = new HSLFSlideShow();
  75. TextBox<?,?> tb = resizeToFitText(ppt);
  76. Rectangle2D anc = tb.getAnchor();
  77. // ignore font metrics differences on windows / linux (... hopefully ...)
  78. assertEquals(anc.getHeight(), 312d, 5);
  79. // setFont(tb, "Mona");
  80. // FileOutputStream fos = new FileOutputStream("bla-hslf.ppt");
  81. // ppt.write(fos);
  82. // fos.close();
  83. ppt.close();
  84. }
  85. @Test
  86. public void resizeToFitTextXSLF() throws IOException {
  87. SlideShow<?,?> ppt = new XMLSlideShow();
  88. TextBox<?,?> tb = resizeToFitText(ppt);
  89. Rectangle2D anc = tb.getAnchor();
  90. // ignore font metrics differences on windows / linux (... hopefully ...)
  91. assertEquals(anc.getHeight(), 312d, 5);
  92. // setFont(tb, "Mona");
  93. // FileOutputStream fos = new FileOutputStream("bla-xslf.ppt");
  94. // ppt.write(fos);
  95. // fos.close();
  96. ppt.close();
  97. }
  98. private TextBox<?,?> resizeToFitText(SlideShow<?,?> slideshow) throws IOException {
  99. Slide<?,?> sld = slideshow.createSlide();
  100. TextBox<?,?> tb = sld.createTextBox();
  101. tb.setAnchor(new Rectangle(50, 50, 200, 50));
  102. tb.setStrokeStyle(Color.black, LineDash.SOLID, 3);
  103. tb.setText(JPTEXT);
  104. setFont(tb, "NoSuchFont");
  105. Dimension pgsize = slideshow.getPageSize();
  106. int width = (int)pgsize.getWidth();
  107. int height = (int)pgsize.getHeight();
  108. BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
  109. Graphics2D graphics = img.createGraphics();
  110. Map<String,String> fallbackMap = new HashMap<String,String>();
  111. fallbackMap.put("NoSuchFont", "Mona");
  112. graphics.setRenderingHint(Drawable.FONT_FALLBACK, fallbackMap);
  113. graphics.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
  114. DrawFactory.getInstance(graphics).fixFonts(graphics);
  115. tb.resizeToFitText(graphics);
  116. graphics.dispose();
  117. return tb;
  118. }
  119. private void setFont(TextBox<?,?> tb, String fontFamily) {
  120. // TODO: set east asian font family - MS Office uses "MS Mincho" or "MS Gothic" as a fallback
  121. // see https://stackoverflow.com/questions/26063828 for good explanation about the font metrics
  122. // differences on different environments
  123. for (TextParagraph<?,?,? extends TextRun> p : tb.getTextParagraphs()) {
  124. for (TextRun r : p.getTextRuns()) {
  125. r.setFontFamily(fontFamily);
  126. if (r instanceof XSLFTextRun) {
  127. // TODO: provide API for HSLF
  128. XSLFTextRun xr = (XSLFTextRun)r;
  129. CTRegularTextRun tr = (CTRegularTextRun)xr.getXmlObject();
  130. tr.getRPr().addNewEa().setTypeface(fontFamily);
  131. }
  132. }
  133. }
  134. }
  135. }