From 4a173780dd793aed02c54ba0a9c816ce1b1f8991 Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Thu, 16 Jul 2020 12:50:40 +0000 Subject: [PATCH] [bug-64605] add support for non-integer font sizes on character runs (use double instead of float) git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1879950 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/wp/usermodel/CharacterRun.java | 4 ++-- .../xwpf/usermodel/XWPFDefaultRunStyle.java | 19 ++++++++++++++++--- .../apache/poi/xwpf/usermodel/XWPFRun.java | 14 ++++++++------ .../poi/xwpf/usermodel/TestXWPFRun.java | 4 ++-- .../poi/xwpf/usermodel/TestXWPFStyles.java | 1 + .../poi/hwpf/usermodel/CharacterRun.java | 6 +++--- 6 files changed, 32 insertions(+), 16 deletions(-) diff --git a/src/java/org/apache/poi/wp/usermodel/CharacterRun.java b/src/java/org/apache/poi/wp/usermodel/CharacterRun.java index 81117c2bd2..28dc89016e 100644 --- a/src/java/org/apache/poi/wp/usermodel/CharacterRun.java +++ b/src/java/org/apache/poi/wp/usermodel/CharacterRun.java @@ -48,9 +48,9 @@ public interface CharacterRun { void setImprinted(boolean imprint); int getFontSize(); - float getFontSizeAsFloat(); + Double getFontSizeAsDouble(); void setFontSize(int halfPoints); - void setFontSize(float halfPoints); + void setFontSize(double halfPoints); int getCharacterSpacing(); void setCharacterSpacing(int twips); diff --git a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDefaultRunStyle.java b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDefaultRunStyle.java index 60c2f272dc..884949d6d7 100644 --- a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDefaultRunStyle.java +++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDefaultRunStyle.java @@ -19,6 +19,9 @@ package org.apache.poi.xwpf.usermodel; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRPr; +import java.math.BigDecimal; +import java.math.RoundingMode; + /** * Default Character Run style, from which other styles will override * TODO Share logic with {@link XWPFRun} which also uses CTRPr @@ -35,8 +38,18 @@ public class XWPFDefaultRunStyle { } public int getFontSize() { - if (rpr.isSetSz()) - return rpr.getSz().getVal().intValue() / 2; - return -1; + BigDecimal bd = getFontSizeAsBigDecimal(0); + return bd == null ? -1 : bd.intValue(); + } + + public Double getFontSizeAsDouble() { + BigDecimal bd = getFontSizeAsBigDecimal(1); + return bd == null ? null : bd.doubleValue(); + } + + private BigDecimal getFontSizeAsBigDecimal(int scale) { + return (rpr != null && rpr.isSetSz()) ? + new BigDecimal(rpr.getSz().getVal()).divide(BigDecimal.valueOf(2)).setScale(scale, RoundingMode.HALF_UP) : + null; } } 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 7ff89ee11c..b35f9a3105 100644 --- a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRun.java +++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRun.java @@ -868,13 +868,14 @@ public class XWPFRun implements ISDTContents, IRunElement, CharacterRun { * characters in the contents of this run when displayed. * * @return value representing the font size (non-integer size will be rounded with half rounding up) - * @deprecated use {@link #getFontSizeAsFloat()} + * @deprecated use {@link #getFontSizeAsDouble()} */ @Deprecated @Removal(version = "6.0.0") @Override public int getFontSize() { - return getFontSizeAsBigDecimal(0).intValue(); + BigDecimal bd = getFontSizeAsBigDecimal(0); + return bd == null ? -1 : bd.intValue(); } /** @@ -885,15 +886,16 @@ public class XWPFRun implements ISDTContents, IRunElement, CharacterRun { * @since POI 5.0.0 */ @Override - public float getFontSizeAsFloat() { - return getFontSizeAsBigDecimal(1).floatValue(); + public Double getFontSizeAsDouble() { + BigDecimal bd = getFontSizeAsBigDecimal(1); + return bd == null ? null : bd.doubleValue(); } private BigDecimal getFontSizeAsBigDecimal(int scale) { CTRPr pr = getRunProperties(false); return (pr != null && pr.isSetSz()) ? new BigDecimal(pr.getSz().getVal()).divide(BigDecimal.valueOf(2)).setScale(scale, RoundingMode.HALF_UP) : - BigDecimal.valueOf(-1); + null; } /** @@ -930,7 +932,7 @@ public class XWPFRun implements ISDTContents, IRunElement, CharacterRun { * @since POI 5.0.0 */ @Override - public void setFontSize(float size) { + public void setFontSize(double size) { BigDecimal bd = BigDecimal.valueOf(size); CTRPr pr = getRunProperties(true); CTHpsMeasure ctSize = pr.isSetSz() ? pr.getSz() : pr.addNewSz(); 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 c361b5337c..b91e0a5891 100644 --- a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFRun.java +++ b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFRun.java @@ -183,7 +183,7 @@ public class TestXWPFRun { XWPFRun run = new XWPFRun(ctRun, irb); assertEquals(7, run.getFontSize()); - assertEquals(7.0f, run.getFontSizeAsFloat(), 0.01); + assertEquals(7.0, run.getFontSizeAsDouble(), 0.01); run.setFontSize(24); assertEquals(48, rpr.getSz().getVal().longValue()); @@ -191,7 +191,7 @@ public class TestXWPFRun { run.setFontSize(24.5f); assertEquals(49, rpr.getSz().getVal().longValue()); assertEquals(25, run.getFontSize()); - assertEquals(24.5f, run.getFontSizeAsFloat(), 0.01); + assertEquals(24.5, run.getFontSizeAsDouble(), 0.01); } @Test diff --git a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFStyles.java b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFStyles.java index d03a13b958..c429c7434e 100644 --- a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFStyles.java +++ b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFStyles.java @@ -202,6 +202,7 @@ public final class TestXWPFStyles { assertNotNull(styles.getDefaultParagraphStyle()); assertEquals(11, styles.getDefaultRunStyle().getFontSize()); + assertEquals(11.0, styles.getDefaultRunStyle().getFontSizeAsDouble(), 0.01); assertEquals(200, styles.getDefaultParagraphStyle().getSpacingAfter()); } } diff --git a/src/scratchpad/src/org/apache/poi/hwpf/usermodel/CharacterRun.java b/src/scratchpad/src/org/apache/poi/hwpf/usermodel/CharacterRun.java index 530196eb26..4e51c747de 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/usermodel/CharacterRun.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/usermodel/CharacterRun.java @@ -352,9 +352,9 @@ public final class CharacterRun extends Range implements Duplicatable, org.apach return _props.getHps(); } - public float getFontSizeAsFloat() + public Double getFontSizeAsDouble() { - return (float)getFontSize(); + return (double)getFontSize(); } public void setFontSize(int halfPoints) @@ -365,7 +365,7 @@ public final class CharacterRun extends Range implements Duplicatable, org.apach } - public void setFontSize(float halfPoints) + public void setFontSize(double halfPoints) { setFontSize(BigDecimal.valueOf(halfPoints).setScale(0, RoundingMode.HALF_UP).intValue()); } -- 2.39.5