aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPJ Fanning <fanningpj@apache.org>2020-07-16 12:30:36 +0000
committerPJ Fanning <fanningpj@apache.org>2020-07-16 12:30:36 +0000
commitd928ac58a82b243138282ea69ee53b318b8a052c (patch)
tree895aed2f0e4bac98c8b6ba40e8e5b1e84b3be1ac
parenta5cf0f0d3fff77668f5b62db7850f400ae970269 (diff)
downloadpoi-d928ac58a82b243138282ea69ee53b318b8a052c.tar.gz
poi-d928ac58a82b243138282ea69ee53b318b8a052c.zip
[bug-64605] add support for non-integer font sizes on character runs
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1879948 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r--src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRun.java48
-rw-r--r--src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFRun.java6
2 files changed, 52 insertions, 2 deletions
diff --git a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRun.java b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRun.java
index c53d7699e2..7ff89ee11c 100644
--- a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRun.java
+++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRun.java
@@ -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);
}
/**
@@ -894,6 +917,27 @@ 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.
+ * <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
* surrounding non-positioned text. This allows the text to be repositioned
diff --git a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFRun.java b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFRun.java
index 72013f8985..c361b5337c 100644
--- a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFRun.java
+++ b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFRun.java
@@ -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