]> source.dussan.org Git - poi.git/commitdiff
Bugzilla 52219: fixed XSSFSimpleShape to set rich text attributes from XSSFRichTextString
authorYegor Kozlov <yegor@apache.org>
Mon, 19 Dec 2011 08:17:46 +0000 (08:17 +0000)
committerYegor Kozlov <yegor@apache.org>
Mon, 19 Dec 2011 08:17:46 +0000 (08:17 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1220642 13f79535-47bb-0310-9956-ffa450edef68

src/documentation/content/xdocs/status.xml
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSimpleShape.java
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFDrawing.java

index 7a7a8f314553959622d4a44f4799fec8f47d20a1..f9609297ca4e4981f0611c3ceab4c5ddc2a03604 100644 (file)
@@ -34,6 +34,7 @@
 
     <changes>
         <release version="3.8-beta6" date="2012-??-??">
+           <action dev="poi-developers" type="fix">52219 - fixed XSSFSimpleShape to set rich text attributes from XSSFRichtextString</action>
            <action dev="poi-developers" type="fix">52314 - enhanced SheetUtil.getColumnWidth</action>
         </release>
         <release version="3.8-beta5" date="2011-12-17">
index 7900d8452e2bf344c08c701578ef15b9946e07f6..0de7b9c064da551d6b1c7f7681d0375ab9fc8374 100644 (file)
 
 package org.apache.poi.xssf.usermodel;
 
-import org.openxmlformats.schemas.drawingml.x2006.main.CTFontReference;
-import org.openxmlformats.schemas.drawingml.x2006.main.CTNonVisualDrawingProps;
-import org.openxmlformats.schemas.drawingml.x2006.main.CTPoint2D;
-import org.openxmlformats.schemas.drawingml.x2006.main.CTPositiveSize2D;
-import org.openxmlformats.schemas.drawingml.x2006.main.CTPresetGeometry2D;
-import org.openxmlformats.schemas.drawingml.x2006.main.CTRegularTextRun;
-import org.openxmlformats.schemas.drawingml.x2006.main.CTSchemeColor;
-import org.openxmlformats.schemas.drawingml.x2006.main.CTShapeProperties;
-import org.openxmlformats.schemas.drawingml.x2006.main.CTShapeStyle;
-import org.openxmlformats.schemas.drawingml.x2006.main.CTStyleMatrixReference;
-import org.openxmlformats.schemas.drawingml.x2006.main.CTTextBody;
-import org.openxmlformats.schemas.drawingml.x2006.main.CTTextBodyProperties;
-import org.openxmlformats.schemas.drawingml.x2006.main.CTTextCharacterProperties;
-import org.openxmlformats.schemas.drawingml.x2006.main.CTTextFont;
-import org.openxmlformats.schemas.drawingml.x2006.main.CTTextParagraph;
-import org.openxmlformats.schemas.drawingml.x2006.main.CTTransform2D;
-import org.openxmlformats.schemas.drawingml.x2006.main.STFontCollectionIndex;
-import org.openxmlformats.schemas.drawingml.x2006.main.STSchemeColorVal;
-import org.openxmlformats.schemas.drawingml.x2006.main.STShapeType;
-import org.openxmlformats.schemas.drawingml.x2006.main.STTextAlignType;
-import org.openxmlformats.schemas.drawingml.x2006.main.STTextAnchoringType;
+import org.apache.poi.hssf.util.HSSFColor;
+import org.apache.poi.ss.usermodel.IndexedColors;
+import org.openxmlformats.schemas.drawingml.x2006.main.*;
 import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTShape;
 import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTShapeNonVisual;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRElt;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRPrElt;
 import org.apache.poi.util.Internal;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.STUnderlineValues;
 
 /**
  * Represents a shape with a predefined geometry in a SpreadsheetML drawing.
@@ -190,15 +173,43 @@ public class XSSFSimpleShape extends XSSFShape { // TODO - instantiable supercla
 
     /**
      *
-     * CTRPrElt --> CTFont adapter
+     * org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRPrElt to
+     * org.openxmlformats.schemas.drawingml.x2006.main.CTFont adapter
      */
     private static void applyAttributes(CTRPrElt pr, CTTextCharacterProperties rPr){
 
         if(pr.sizeOfBArray() > 0) rPr.setB(pr.getBArray(0).getVal());
-        //if(pr.sizeOfUArray() > 0) rPr.setU(pr.getUArray(0).getVal());
+        if(pr.sizeOfUArray() > 0) {
+            STUnderlineValues.Enum u1 = pr.getUArray(0).getVal();
+            if(u1 == STUnderlineValues.SINGLE) rPr.setU(STTextUnderlineType.SNG);
+            else if(u1 == STUnderlineValues.DOUBLE) rPr.setU(STTextUnderlineType.DBL);
+            else if(u1 == STUnderlineValues.NONE) rPr.setU(STTextUnderlineType.NONE);
+        }
         if(pr.sizeOfIArray() > 0) rPr.setI(pr.getIArray(0).getVal());
 
-        CTTextFont rFont = rPr.addNewLatin();
-        rFont.setTypeface(pr.sizeOfRFontArray() > 0 ? pr.getRFontArray(0).getVal() : "Arial");
+        if(pr.sizeOfFamilyArray() > 0) {
+            CTTextFont rFont = rPr.addNewLatin();
+            rFont.setTypeface(pr.getRFontArray(0).getVal());
+        }
+        
+        if(pr.sizeOfColorArray() > 0) {
+            CTSolidColorFillProperties fill = rPr.isSetSolidFill() ? rPr.getSolidFill() : rPr.addNewSolidFill();
+            org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor xlsColor = pr.getColorArray(0);
+            if(xlsColor.isSetRgb()) {
+                CTSRgbColor clr = fill.isSetSrgbClr() ? fill.getSrgbClr() : fill.addNewSrgbClr();
+                clr.setVal(xlsColor.getRgb());
+            }
+            else if(xlsColor.isSetIndexed()) {
+                HSSFColor indexed = HSSFColor.getIndexHash().get((int) xlsColor.getIndexed());
+                if (indexed != null) {
+                    byte[] rgb = new byte[3];
+                    rgb[0] = (byte) indexed.getTriplet()[0];
+                    rgb[1] = (byte) indexed.getTriplet()[1];
+                    rgb[2] = (byte) indexed.getTriplet()[2];
+                    CTSRgbColor clr = fill.isSetSrgbClr() ? fill.getSrgbClr() : fill.addNewSrgbClr();
+                    clr.setVal(rgb);
+                }
+            }
+        }
     }
 }
index ced478e3ea64776e7aa9dd14af956c09c6e3688b..1213256071145365b796e77a6a3e0442c850d531 100644 (file)
 ==================================================================== */
 package org.apache.poi.xssf.usermodel;
 
+import java.awt.*;
+import java.util.Arrays;
 import java.util.List;
 
 import junit.framework.TestCase;
 
 import org.apache.poi.POIXMLDocumentPart;
 import org.apache.poi.openxml4j.opc.OPCPackage;
+import org.apache.poi.ss.usermodel.FontUnderline;
+import org.apache.poi.ss.usermodel.IndexedColors;
 import org.apache.poi.xssf.XSSFTestDataSamples;
 import org.apache.poi.xssf.dev.XSSFDump;
+import org.openxmlformats.schemas.drawingml.x2006.main.CTRegularTextRun;
+import org.openxmlformats.schemas.drawingml.x2006.main.CTTextCharacterProperties;
+import org.openxmlformats.schemas.drawingml.x2006.main.CTTextParagraph;
+import org.openxmlformats.schemas.drawingml.x2006.main.STTextUnderlineType;
 import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTDrawing;
+import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTShape;
 
 /**
  * @author Yegor Kozlov
@@ -171,4 +180,40 @@ public class TestXSSFDrawing extends TestCase {
             assertEquals(sh1.getShapeProperties().toString(), sh2.getShapeProperties().toString());
         }
     }
+
+    /**
+     * ensure that rich text attributes defined in a XSSFRichTextString
+     * are passed to XSSFSimpleShape.
+     *
+     * See Bugzilla 52219.
+     */
+    public void testRichText(){
+        XSSFWorkbook wb = new XSSFWorkbook();
+        XSSFSheet sheet = wb.createSheet();
+        XSSFDrawing drawing = sheet.createDrawingPatriarch();
+
+        XSSFTextBox shape = drawing.createTextbox(new XSSFClientAnchor(0, 0, 0, 0, 2, 2, 3, 4));
+        XSSFRichTextString rt = new XSSFRichTextString("Test String");
+
+        XSSFFont font = wb.createFont();
+        font.setColor(new XSSFColor(new Color(0, 128, 128)));
+        font.setItalic(true);
+        font.setBold(true);
+        font.setUnderline(FontUnderline.SINGLE);
+        rt.applyFont(font);
+
+        shape.setText(rt);
+
+        CTTextParagraph pr = shape.getCTShape().getTxBody().getPArray(0);
+        assertEquals(1, pr.sizeOfRArray());
+
+        CTTextCharacterProperties rPr = pr.getRArray(0).getRPr();
+        assertEquals(true, rPr.getB());
+        assertEquals(true, rPr.getI());
+        assertEquals(STTextUnderlineType.SNG, rPr.getU());
+        assertTrue(Arrays.equals(
+                new byte[]{0, (byte)128, (byte)128} ,
+                rPr.getSolidFill().getSrgbClr().getVal()));
+
+    }
 }