]> source.dussan.org Git - poi.git/commitdiff
Fix bug #49273 - Correct handling for Font Character Sets with indicies greater than 127
authorNick Burch <nick@apache.org>
Tue, 25 May 2010 16:25:34 +0000 (16:25 +0000)
committerNick Burch <nick@apache.org>
Tue, 25 May 2010 16:25:34 +0000 (16:25 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@948089 13f79535-47bb-0310-9956-ffa450edef68

src/documentation/content/xdocs/status.xml
src/java/org/apache/poi/hssf/usermodel/HSSFFont.java
src/java/org/apache/poi/ss/usermodel/Font.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFFont.java
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFFont.java
test-data/spreadsheet/49273.xlsx [new file with mode: 0644]

index 068ab153af25563a9ca22b7c693684cbf9a923a0..826937bee5e7250420506b238fd6bde029c82bbe 100644 (file)
@@ -34,6 +34,7 @@
 
     <changes>
         <release version="3.7-SNAPSHOT" date="2010-??-??">
+           <action dev="POI-DEVELOPERS" type="fix">49273 - Correct handling for Font Character Sets with indicies greater than 127</action>
            <action dev="POI-DEVELOPERS" type="add">49334 - Track the ValueRangeRecords of charts in HSSFChart, to allow the basic axis operations</action>
            <action dev="POI-DEVELOPERS" type="add">49242 - Track the LinkDataRecords of charts in HSSFChart</action>
            <action dev="POI-DEVELOPERS" type="add">Improved performance of XSSFWorkbook.write </action>
index 58621bdc9e2e26286bac30a32b7669130de86097..4e2a2fe8987e142d81256b489bd4e14ec4be6204 100644 (file)
@@ -280,9 +280,29 @@ public final class HSSFFont implements Font {
      * @see #DEFAULT_CHARSET
      * @see #SYMBOL_CHARSET
      */
-    public byte getCharSet()
+    public int getCharSet()
     {
-        return font.getCharset();
+        byte charset = font.getCharset();
+        if(charset >= 0) {
+           return (int)charset;
+        } else {
+           return charset + 256;
+        }
+    }
+
+    /**
+     * set character-set to use.
+     * @see #ANSI_CHARSET
+     * @see #DEFAULT_CHARSET
+     * @see #SYMBOL_CHARSET
+     */
+    public void setCharSet(int charset)
+    {
+        byte cs = (byte)charset;
+        if(charset > 127) {
+           cs = (byte)(charset-256);
+        }
+        setCharSet(cs);
     }
 
     /**
index b9c10a8b153c4cf2822a79b3be9e1be51d12bdcd..ee0d8add8b88a18f1a7207580e2dabfea84e2be9 100644 (file)
@@ -251,7 +251,7 @@ public interface Font {
      * @see #DEFAULT_CHARSET
      * @see #SYMBOL_CHARSET
      */
-    byte getCharSet();
+    int getCharSet();
 
     /**
      * set character-set to use.
@@ -260,6 +260,13 @@ public interface Font {
      * @see #SYMBOL_CHARSET
      */
     void setCharSet(byte charset);
+    /**
+     * set character-set to use.
+     * @see #ANSI_CHARSET
+     * @see #DEFAULT_CHARSET
+     * @see #SYMBOL_CHARSET
+     */
+    void setCharSet(int charset);
 
     /**
      * get the index within the XSSFWorkbook (sequence within the collection of Font objects)
index 7fbbee79516c1fe3fc192fcbe8d83cc77869fd05..3b92c6fd3563535f90568e1eb3a1a896065cb5db 100644 (file)
@@ -107,13 +107,13 @@ public class XSSFFont implements Font {
     /**
      * get character-set to use.
      *
-     * @return byte - character-set
+     * @return int - character-set (0-255)
      * @see org.apache.poi.ss.usermodel.FontCharset
      */
-    public byte getCharSet() {
+    public int getCharSet() {
         CTIntProperty charset = _ctFont.sizeOfCharsetArray() == 0 ? null : _ctFont.getCharsetArray(0);
         int val = charset == null ? FontCharset.ANSI.getValue() : FontCharset.valueOf(charset.getVal()).getValue();
-        return (byte)val;
+        return val;
     }
 
 
@@ -293,6 +293,19 @@ public class XSSFFont implements Font {
      * @see FontCharset
      */
     public void setCharSet(byte charset) {
+       int cs = (int)charset;
+       if(cs < 0) {
+          cs += 256;
+       }
+       setCharSet(cs);
+    }
+    /**
+     * set character-set to use.
+     *
+     * @param charset - charset
+     * @see FontCharset
+     */
+    public void setCharSet(int charset) {
         CTIntProperty charsetProperty = _ctFont.sizeOfCharsetArray() == 0 ? _ctFont.addNewCharset() : _ctFont.getCharsetArray(0);
         switch (charset) {
             case Font.ANSI_CHARSET:
index 1bcc98149e9add8ef781dd89bfb1ad4ba2c90ede..fe8d3a588bef73cdd9223426cf81e1256f8ef831 100644 (file)
@@ -19,6 +19,7 @@ package org.apache.poi.xssf.usermodel;
 
 import org.apache.poi.ss.usermodel.*;
 import org.apache.poi.xssf.XSSFITestDataProvider;
+import org.apache.poi.xssf.XSSFTestDataSamples;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBooleanProperty;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFont;
@@ -72,6 +73,21 @@ public final class TestXSSFFont extends BaseTestFont{
 
                xssfFont.setCharSet(FontCharset.DEFAULT);
                assertEquals(FontCharset.DEFAULT.getValue(),ctFont.getCharsetArray(0).getVal());
+               
+               
+               // Now try with a few sample files
+               
+               // Normal charset
+      XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook("Formatting.xlsx");
+      assertEquals(0, 
+            workbook.getSheetAt(0).getRow(0).getCell(0).getCellStyle().getFont().getCharSet()
+      );
+               
+               // GB2312 charact set
+      workbook = XSSFTestDataSamples.openSampleWorkbook("49273.xlsx");
+      assertEquals(134, 
+            workbook.getSheetAt(0).getRow(0).getCell(0).getCellStyle().getFont().getCharSet()
+      );
        }
 
        public void testFontName() {
diff --git a/test-data/spreadsheet/49273.xlsx b/test-data/spreadsheet/49273.xlsx
new file mode 100644 (file)
index 0000000..ad1ad27
Binary files /dev/null and b/test-data/spreadsheet/49273.xlsx differ