poi-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Eliot Kimber <ekim...@contrext.com>
Subject Re: General Support for Percentage Values?
Date Wed, 15 Aug 2018 22:39:50 GMT
(This is in the context of setting the width on XWPFTable.)

I decided that the best (and simplest) approach would be to use strings to set the width value
for "auto" and percentages, rather than creating a separate class that would just end up creating
a percentage string anyway. This also matches the way the API is likely to be used, e.g.,
getting percentage value strings from some incoming source.

So I added a new setWidth(String widthValue) method to XWPFTable() which validates that it's
a good value ("auto", an integer, or a percentage) and then if it's good, sets the value appropriately.

I also created a new method, getWidthDecimal(), to return the width value as a decimal (because
percentages can be decimal values).

Using this test case:

        XWPFDocument doc = new XWPFDocument();

        XWPFTable xtab = doc.createTable();

        assertEquals(0, xtab.getWidth());
        assertEquals(STTblWidth.AUTO, xtab.getWidthType());
        
        xtab.setWidth(1000);
        assertEquals(STTblWidth.DXA, xtab.getWidthType());
        assertEquals(1000, xtab.getWidth());
        
        xtab.setWidth("auto");
        assertEquals(STTblWidth.AUTO, xtab.getWidthType());
        assertEquals(0, xtab.getWidth());
        assertEquals(0.0, xtab.getWidthDecimal(), 0.01);
        
        xtab.setWidth("999");
        assertEquals(STTblWidth.DXA, xtab.getWidthType());                
        assertEquals(999, xtab.getWidth());
        
        xtab.setWidth("50.5%");
        assertEquals(STTblWidth.PCT, xtab.getWidthType());        
        assertEquals(50.5, xtab.getWidthDecimal(), 0.01);
        assertEquals(50.5, xtab.getCTTbl().getTblPr().getTblW().xgetW().getBigDecimalValue().doubleValue(),
0.01);

Everything passes until the last test, where the value returned by getWidthDecimal() is 50.0,
not 50.5.

It looks like the underlying CT class methods always set the value as an integer, even when
using CTTblWidth.setBigDecimalValue():

        } else if (widthValue.matches("[0-9]+(\\.[0-9]+)?%")) {
            String numericPart = widthValue.substring(0, widthValue.length() - 1);
            STDecimalNumber number = STDecimalNumber.Factory.newInstance();
            number.setBigDecimalValue(new BigDecimal(numericPart));
            ctWidth.xsetW(number);
            ctWidth.setType(STTblWidth.PCT);

Debugging through this code I see that the @w:val attribute is "50" rather than "50.5" after
the xsetW() method call.

This appears to be happening at a very low level.

Is there a way to fix this so that percentages can be decimals or have I misunderstood the
specs and percentages should actually always be integers (although I'm pretty sure the spec
allows fractional percentages).

Thanks,

Eliot
--
Eliot Kimber
http://contrext.com
 

On 8/15/18, 3:40 PM, "Eliot Kimber" <ekimber@contrext.com> wrote:

    I need to extend the options for setting table widths.
    
    Per the spec, a table's width can be an absolute measurement, a percentage value, or the
keyword "auto".
    
    Right now XWPFTable.setWidth() only takes an integer.
    
    I'd like to extend it to take in addition a percentage value or the value "auto" (which
I suppose conceptually is an enumeration with one possible value).
    
    There doesn't seem to be a class that represents a percentage value (at least I couldn't
find any declarations with the string "perc" in them.
    
    Am I missing something for working with percentage values in the XWPF API?
    
    Thanks,
    
    E.
    
    --
    Eliot Kimber
    http://contrext.com
     
    
    
    
    ---------------------------------------------------------------------
    To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
    For additional commands, e-mail: user-help@poi.apache.org
    
    
    



---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


Mime
View raw message