]> source.dussan.org Git - poi.git/commitdiff
bug 58043: provide some compatibility between HSSF and XSSF in regards to values...
authorDominik Stadler <centic@apache.org>
Sun, 3 Jan 2016 13:28:17 +0000 (13:28 +0000)
committerDominik Stadler <centic@apache.org>
Sun, 3 Jan 2016 13:28:17 +0000 (13:28 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1722716 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/hssf/record/ExtendedFormatRecord.java
src/java/org/apache/poi/hssf/usermodel/HSSFCellStyle.java
src/java/org/apache/poi/ss/usermodel/CellStyle.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java
src/ooxml/java/org/apache/poi/xssf/usermodel/extensions/XSSFCellAlignment.java
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCellStyle.java
src/testcases/org/apache/poi/hssf/record/TestExtendedFormatRecord.java
src/testcases/org/apache/poi/hssf/usermodel/TestCellStyle.java

index 261cfd8786cf7eb9ff5908cdf9ea486f39af4579..6f64d46ec101069a8c8908e14e1ffcb6fb5cee68 100644 (file)
@@ -410,7 +410,7 @@ public final class ExtendedFormatRecord
     }
 
     /**
-     * set the degree of rotation.  (I've not actually seen this used anywhere)
+     * set the degree of rotation.
      *
      *
      * @param rotation the degree of rotation
@@ -1145,7 +1145,7 @@ public final class ExtendedFormatRecord
     }
 
     /**
-     * get the degree of rotation.  (I've not actually seen this used anywhere)
+     * get the degree of rotation.
      *
      *
      * @return rotation - the degree of rotation
index b6d366c50689ab654bd5490af9352610dc0f9527..25e3b371c0a87b38d6b0b6cee141cc6b358164e5 100644 (file)
@@ -324,6 +324,12 @@ public final class HSSFCellStyle implements CellStyle {
 
     /**
      * set the degree of rotation for the text in the cell
+     *
+     * Note: HSSF uses values from -90 to 90 degrees, whereas XSSF 
+     * uses values from 0 to 180 degrees. The implementations of this method will map between these two value-ranges 
+     * accordingly, however the corresponding getter is returning values in the range mandated by the current type
+     * of Excel file-format that this CellStyle is applied to.
+     *
      * @param rotation degrees (between -90 and 90 degrees, of 0xff for vertical)
      */
     @Override
@@ -337,7 +343,11 @@ public final class HSSFCellStyle implements CellStyle {
         //The 4th quadrant (-1 to -90) is stored as (91 to 180)
         rotation = (short)(90 - rotation);
       }
-      else if ((rotation < -90)  ||(rotation > 90)) {
+      else if (rotation > 90 && rotation <= 180) {
+          // stay compatible with the range used by XSSF, map from ]90..180] to ]0..-90]
+          // we actually don't need to do anything here as the internal value is stored in [0-180] anyway!
+      }
+      else if ((rotation < -90)  || (rotation > 90)) {
         //Do not allow an incorrect rotation to be set
         throw new IllegalArgumentException("The rotation must be between -90 and 90 degrees, or 0xff");
       }
index be5a43e93a39dbd5a526b7a0b3f615d07e3da440..e91d19d0f3f52d2b4703e65e1fe027f413b1c34a 100644 (file)
@@ -363,17 +363,26 @@ public interface CellStyle {
     short getVerticalAlignment();
 
     /**
-     * set the degree of rotation for the text in the cell
-     * @param rotation degrees (between -90 and 90 degrees)
+     * set the degree of rotation for the text in the cell.
+     *
+     * Note: HSSF uses values from -90 to 90 degrees, whereas XSSF 
+     * uses values from 0 to 180 degrees. The implementations of this method will map between these two value-ranges 
+     * accordingly, however the corresponding getter is returning values in the range mandated by the current type
+     * of Excel file-format that this CellStyle is applied to.
+     *
+     * @param rotation degrees (see note above)
      */
-
     void setRotation(short rotation);
 
     /**
-     * get the degree of rotation for the text in the cell
-     * @return rotation degrees (between -90 and 90 degrees)
+     * get the degree of rotation for the text in the cell.
+     *
+     * Note: HSSF uses values from -90 to 90 degrees, whereas XSSF 
+     * uses values from 0 to 180 degrees. The implementations of this method will map between these two value-ranges 
+     * value-range as used by the type of Excel file-format that this CellStyle is applied to.
+     *
+     * @return rotation degrees (see note above)
      */
-
     short getRotation();
 
     /**
index a2c265f1bff6cbdef3405017d1d7509343a73310..ea355756ef52c6f2cbe2f3ceb9f4285131e6f17f 100644 (file)
@@ -1364,6 +1364,11 @@ public void setFillPattern(short fp) {
      * <code>[degrees below horizon] = 90 - textRotation.</code>
      * </p>
      *
+     * Note: HSSF uses values from -90 to 90 degrees, whereas XSSF 
+     * uses values from 0 to 180 degrees. The implementations of this method will map between these two value-ranges 
+     * accordingly, however the corresponding getter is returning values in the range mandated by the current type
+     * of Excel file-format that this CellStyle is applied to.
+     *
      * @param rotation - the rotation degrees (between 0 and 180 degrees)
      */
     @Override
index 233616c83368931397ef259ca77285817db31ea0..06e5d0bf66b8695e07f62f05a089109f19fc13a9 100644 (file)
@@ -133,9 +133,17 @@ public class XSSFCellAlignment {
      * <code>[degrees below horizon] = 90 - textRotation.</code>
      * </p>
      *
+     * Note: HSSF uses values from -90 to 90 degrees, whereas XSSF 
+     * uses values from 0 to 180 degrees. The implementations of this method will map between these two value-ranges 
+     * accordingly, however the corresponding getter is returning values in the range mandated by the current type
+     * of Excel file-format that this CellStyle is applied to.
+     *
      * @param rotation - the rotation degrees (between 0 and 180 degrees)
      */
     public void setTextRotation(long rotation) {
+        if(rotation < 0 && rotation >= -90) {
+            rotation = 90 + ((-1)*rotation);
+        }
         cellAlignement.setTextRotation(rotation);
     }
 
index 56eff44578346b4e21be1ed6a2201e531e8903ee..92235c43994c23dd3ca5bbbc58e9a86af0292c57 100644 (file)
@@ -2906,4 +2906,52 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues {
         
         wb.close();
     }
+
+    private void createXls() throws IOException
+    {
+        Workbook workbook = new HSSFWorkbook();
+        FileOutputStream fileOut = new FileOutputStream("/tmp/rotated.xls");
+        Sheet sheet1 = workbook.createSheet();
+        Row row1 = sheet1.createRow((short) 0);
+
+        Cell cell1 = row1.createCell(0);
+
+        cell1.setCellValue("Successful rotated text.");
+
+        CellStyle style = workbook.createCellStyle();
+        style.setRotation((short) -90);
+
+        cell1.setCellStyle(style);
+
+        workbook.write(fileOut);
+        fileOut.close();
+        workbook.close();
+    }
+
+    private void createXlsx() throws IOException {
+        Workbook workbook = new XSSFWorkbook();
+        FileOutputStream fileOut = new FileOutputStream("/tmp/rotated.xlsx");
+        Sheet sheet1 = workbook.createSheet();
+        Row row1 = sheet1.createRow((short) 0);
+
+        Cell cell1 = row1.createCell(0);
+
+        cell1.setCellValue("Unsuccessful rotated text.");
+
+        CellStyle style = workbook.createCellStyle();
+        style.setRotation((short) -90);
+
+        cell1.setCellStyle(style);
+
+        workbook.write(fileOut);
+        fileOut.close();
+        workbook.close();
+    }
+
+    @Ignore("Creates files for checking results manually, actual values are tested in Test*CellStyle")
+    @Test
+    public void test58043() throws Exception {
+        createXls();
+        createXlsx();
+    }
 }
index 1b33f28a4ffef606dd3c1c7ca0563d2008a15fd4..21f14bb5d6a15719cd83d91c85f744dfa0521739 100644 (file)
@@ -1059,4 +1059,31 @@ public class TestXSSFCellStyle {
         target.close();
         reference.close();
     }
+
+    @Test
+    public void test58043() {
+        assertEquals(0, cellStyle.getRotation());
+
+        cellStyle.setRotation((short)89);
+        assertEquals(89, cellStyle.getRotation());
+        
+        cellStyle.setRotation((short)90);
+        assertEquals(90, cellStyle.getRotation());
+        
+        cellStyle.setRotation((short)179);
+        assertEquals(179, cellStyle.getRotation());
+        
+        cellStyle.setRotation((short)180);
+        assertEquals(180, cellStyle.getRotation());
+        
+        // negative values are mapped to the correct values for compatibility between HSSF and XSSF
+        cellStyle.setRotation((short)-1);
+        assertEquals(91, cellStyle.getRotation());
+        
+        cellStyle.setRotation((short)-89);
+        assertEquals(179, cellStyle.getRotation());
+        
+        cellStyle.setRotation((short)-90);
+        assertEquals(180, cellStyle.getRotation());
+    }
 }
index 6aaa509a559f5b2de20cb0e9b0062563b366b477..5ed16ad0f467af225a27e52d194f4bc2c42784e5 100644 (file)
@@ -129,4 +129,28 @@ public final class TestExtendedFormatRecord extends TestCase {
                for (int i = 0; i < data.length; i++)
                        assertEquals("At offset " + i, data[i], recordBytes[i + 4]);
        }
+       
+       public void testRotation() {
+        ExtendedFormatRecord record = createEFR();
+        assertEquals(0, record.getRotation());
+
+        record.setRotation((short)1);
+        assertEquals(1, record.getRotation());
+
+        record.setRotation((short)89);
+        assertEquals(89, record.getRotation());
+
+        record.setRotation((short)90);
+        assertEquals(90, record.getRotation());
+
+        // internally values below zero are stored differently
+        record.setRotation((short)-1);
+        assertEquals(255, record.getRotation());
+
+        record.setRotation((short)-89);
+        assertEquals(-77, 90-record.getRotation());
+
+        record.setRotation((short)-90);
+        assertEquals(-76, 90-record.getRotation());
+       }
 }
index 002803486483924f66f4c6a71b43e1b5c4a7b357..8d7fd012a62c5f5b018271842ac701c66178a0ea 100644 (file)
@@ -23,8 +23,6 @@ import java.io.IOException;
 import java.util.Calendar;
 import java.util.Date;
 
-import junit.framework.TestCase;
-
 import org.apache.poi.hssf.HSSFTestDataSamples;
 import org.apache.poi.ss.usermodel.Cell;
 import org.apache.poi.ss.usermodel.CellStyle;
@@ -34,6 +32,9 @@ import org.apache.poi.ss.usermodel.Sheet;
 import org.apache.poi.ss.usermodel.Workbook;
 import org.apache.poi.util.LocaleUtil;
 import org.apache.poi.util.TempFile;
+import org.junit.Test;
+
+import junit.framework.TestCase;
 
 /**
  * Class to test cell styling functionality
@@ -507,4 +508,40 @@ public final class TestCellStyle extends TestCase {
 //            out.close();
 //        }
     }
+
+
+    @Test
+    public void test58043() throws IOException {
+        HSSFWorkbook     wb   = new HSSFWorkbook();
+        HSSFCellStyle    cellStyle   = wb.createCellStyle();
+
+        assertEquals(0, cellStyle.getRotation());
+
+        cellStyle.setRotation((short)89);
+        assertEquals(89, cellStyle.getRotation());
+        
+        cellStyle.setRotation((short)90);
+        assertEquals(90, cellStyle.getRotation());
+        
+        cellStyle.setRotation((short)-1);
+        assertEquals(-1, cellStyle.getRotation());
+        
+        cellStyle.setRotation((short)-89);
+        assertEquals(-89, cellStyle.getRotation());
+        
+        cellStyle.setRotation((short)-90);
+        assertEquals(-90, cellStyle.getRotation());
+        
+        cellStyle.setRotation((short)-89);
+        assertEquals(-89, cellStyle.getRotation());
+
+        // values above 90 are mapped to the correct values for compatibility between HSSF and XSSF
+        cellStyle.setRotation((short)179);
+        assertEquals(-89, cellStyle.getRotation());
+        
+        cellStyle.setRotation((short)180);
+        assertEquals(-90, cellStyle.getRotation());
+        
+        wb.close();
+    }
 }