[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
This commit is contained in:
PJ Fanning 2020-07-16 12:50:40 +00:00
parent cb1d9a0061
commit 4a173780dd
6 changed files with 32 additions and 16 deletions

View File

@ -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);

View File

@ -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;
}
}

View File

@ -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();

View File

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

View File

@ -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());
}
}

View File

@ -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());
}