]> source.dussan.org Git - poi.git/commitdiff
[bug-64605] add support for non-integer font sizes on character runs
authorPJ Fanning <fanningpj@apache.org>
Thu, 16 Jul 2020 12:30:36 +0000 (12:30 +0000)
committerPJ Fanning <fanningpj@apache.org>
Thu, 16 Jul 2020 12:30:36 +0000 (12:30 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1879948 13f79535-47bb-0310-9956-ffa450edef68

src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRun.java
src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFRun.java

index c53d7699e2b4c3ccc2f9333e91ebe478f41b86c4..7ff89ee11c7c8cf4da728359311e5c73ed1e76db 100644 (file)
@@ -21,7 +21,9 @@ import static org.apache.poi.ooxml.POIXMLTypeLoader.DEFAULT_XML_OPTIONS;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.StringReader;
+import java.math.BigDecimal;
 import java.math.BigInteger;
+import java.math.RoundingMode;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
@@ -865,12 +867,33 @@ public class XWPFRun implements ISDTContents, IRunElement, CharacterRun {
      * Specifies the font size which shall be applied to all non complex script
      * characters in the contents of this run when displayed.
      *
-     * @return value representing the font size
+     * @return value representing the font size (non-integer size will be rounded with half rounding up)
+     * @deprecated use {@link #getFontSizeAsFloat()}
      */
+    @Deprecated
+    @Removal(version = "6.0.0")
     @Override
     public int getFontSize() {
+        return getFontSizeAsBigDecimal(0).intValue();
+    }
+
+    /**
+     * Specifies the font size which shall be applied to all non complex script
+     * characters in the contents of this run when displayed.
+     *
+     * @return value representing the font size
+     * @since POI 5.0.0
+     */
+    @Override
+    public float getFontSizeAsFloat() {
+        return getFontSizeAsBigDecimal(1).floatValue();
+    }
+
+    private BigDecimal getFontSizeAsBigDecimal(int scale) {
         CTRPr pr = getRunProperties(false);
-        return (pr != null && pr.isSetSz()) ? pr.getSz().getVal().divide(BigInteger.valueOf(2)).intValue() : -1;
+        return (pr != null && pr.isSetSz()) ?
+                new BigDecimal(pr.getSz().getVal()).divide(BigDecimal.valueOf(2)).setScale(scale, RoundingMode.HALF_UP) :
+                BigDecimal.valueOf(-1);
     }
 
     /**
@@ -893,6 +916,27 @@ public class XWPFRun implements ISDTContents, IRunElement, CharacterRun {
         ctSize.setVal(bint.multiply(BigInteger.valueOf(2)));
     }
 
+    /**
+     * Specifies the font size which shall be applied to all non complex script
+     * characters in the contents of this run when displayed.
+     * <p>
+     * If this element is not present, the default value is to leave the value
+     * applied at previous level in the style hierarchy. If this element is
+     * never applied in the style hierarchy, then any appropriate font size may
+     * be used for non complex script characters.
+     * </p>
+     *
+     * @param size The font size as number of point measurements.
+     * @since POI 5.0.0
+     */
+    @Override
+    public void setFontSize(float size) {
+        BigDecimal bd = BigDecimal.valueOf(size);
+        CTRPr pr = getRunProperties(true);
+        CTHpsMeasure ctSize = pr.isSetSz() ? pr.getSz() : pr.addNewSz();
+        ctSize.setVal(bd.multiply(BigDecimal.valueOf(2)).setScale(0, RoundingMode.HALF_UP).toBigInteger());
+    }
+
     /**
      * This element specifies the amount by which text shall be raised or
      * lowered for this run in relation to the default baseline of the
index 72013f8985aa1e2cbb1b04f20ad7b818ab27dc35..c361b5337cc5497a7c26695ae85c4ef625f1cac3 100644 (file)
@@ -183,9 +183,15 @@ public class TestXWPFRun {
 
         XWPFRun run = new XWPFRun(ctRun, irb);
         assertEquals(7, run.getFontSize());
+        assertEquals(7.0f, run.getFontSizeAsFloat(), 0.01);
 
         run.setFontSize(24);
         assertEquals(48, rpr.getSz().getVal().longValue());
+
+        run.setFontSize(24.5f);
+        assertEquals(49, rpr.getSz().getVal().longValue());
+        assertEquals(25, run.getFontSize());
+        assertEquals(24.5f, run.getFontSizeAsFloat(), 0.01);
     }
 
     @Test