From: PJ Fanning Date: Thu, 16 Jul 2020 12:30:36 +0000 (+0000) Subject: [bug-64605] add support for non-integer font sizes on character runs X-Git-Tag: before_ooxml_3rd_edition~178 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=d928ac58a82b243138282ea69ee53b318b8a052c;p=poi.git [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 --- 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); } /** @@ -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. + *

+ * 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. + *

+ * + * @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 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