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
This commit is contained in:
Yegor Kozlov 2011-12-19 08:17:46 +00:00
parent dde2d28102
commit 587cfe2d2f
3 changed files with 82 additions and 25 deletions

View 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">

View File

@ -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);
}
}
}
}
}

View File

@ -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()));
}
}