From 587cfe2d2f8a35b214edf287b26bd62618d8dc4d Mon Sep 17 00:00:00 2001 From: Yegor Kozlov Date: Mon, 19 Dec 2011 08:17:46 +0000 Subject: [PATCH] Bugzilla 52219: fixed XSSFSimpleShape to set rich text attributes from XSSFRichTextString git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1220642 13f79535-47bb-0310-9956-ffa450edef68 --- src/documentation/content/xdocs/status.xml | 1 + .../poi/xssf/usermodel/XSSFSimpleShape.java | 61 +++++++++++-------- .../poi/xssf/usermodel/TestXSSFDrawing.java | 45 ++++++++++++++ 3 files changed, 82 insertions(+), 25 deletions(-) diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index 7a7a8f3145..f9609297ca 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -34,6 +34,7 @@ + 52219 - fixed XSSFSimpleShape to set rich text attributes from XSSFRichtextString 52314 - enhanced SheetUtil.getColumnWidth diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSimpleShape.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSimpleShape.java index 7900d8452e..0de7b9c064 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSimpleShape.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSimpleShape.java @@ -17,32 +17,15 @@ 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); + } + } + } } } diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFDrawing.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFDrawing.java index ced478e3ea..1213256071 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFDrawing.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFDrawing.java @@ -16,15 +16,24 @@ ==================================================================== */ 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())); + + } } -- 2.39.5