#59327 - Setting text direction on a table cell has no effect

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1742338 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Andreas Beeker 2016-05-04 22:09:10 +00:00
parent ac29f3105a
commit f688ed96c6
4 changed files with 150 additions and 8 deletions

View File

@ -17,7 +17,6 @@
package org.apache.poi.sl.usermodel;
import java.awt.Graphics2D;
import java.util.List;
public interface TextShape<
@ -44,11 +43,13 @@ public interface TextShape<
* (each line is 270 degrees rotated clockwise, so it goes
* from bottom to top; each next line is to the right from
* the previous one).
* For HSLF: always interpreted by Powerpoint as HORIZONTAL.
*/
VERTICAL_270,
/**
* Determines if all of the text is vertical
* ("one letter on top of another").
* For HSLF: not supported
*/
STACKED
}

View File

@ -45,6 +45,7 @@ import org.openxmlformats.schemas.drawingml.x2006.main.STLineEndWidth;
import org.openxmlformats.schemas.drawingml.x2006.main.STPenAlignment;
import org.openxmlformats.schemas.drawingml.x2006.main.STPresetLineDashVal;
import org.openxmlformats.schemas.drawingml.x2006.main.STTextAnchoringType;
import org.openxmlformats.schemas.drawingml.x2006.main.STTextVerticalType;
/**
* Represents a cell of a table in a .pptx presentation
@ -429,4 +430,65 @@ public class XSLFTableCell extends XSLFTextShape implements TableCell<XSLFShape,
return align;
}
/**
* @since POI 3.15-beta2
*/
@Override
public void setTextDirection(TextDirection orientation) {
CTTableCellProperties cellProps = getCellProperties(true);
if(orientation == null) {
if (cellProps.isSetVert()) {
cellProps.unsetVert();
}
} else {
STTextVerticalType.Enum vt;
switch (orientation) {
default:
case HORIZONTAL:
vt = STTextVerticalType.HORZ;
break;
case VERTICAL:
vt = STTextVerticalType.VERT;
break;
case VERTICAL_270:
vt = STTextVerticalType.VERT_270;
break;
case STACKED:
vt = STTextVerticalType.WORD_ART_VERT;
break;
}
cellProps.setVert(vt);
}
}
/**
* @since POI 3.15-beta2
*/
@Override
public TextDirection getTextDirection() {
CTTableCellProperties cellProps = getCellProperties(false);
STTextVerticalType.Enum orientation;
if (cellProps != null && cellProps.isSetVert()) {
orientation = cellProps.getVert();
} else {
orientation = STTextVerticalType.HORZ;
}
switch (orientation.intValue()) {
default:
case STTextVerticalType.INT_HORZ:
return TextDirection.HORIZONTAL;
case STTextVerticalType.INT_VERT:
case STTextVerticalType.INT_EA_VERT:
case STTextVerticalType.INT_MONGOLIAN_VERT:
return TextDirection.VERTICAL;
case STTextVerticalType.INT_VERT_270:
return TextDirection.VERTICAL_270;
case STTextVerticalType.INT_WORD_ART_VERT:
case STTextVerticalType.INT_WORD_ART_VERT_RTL:
return TextDirection.STACKED;
}
}
}

View File

@ -21,14 +21,19 @@ package org.apache.poi.sl;
import static org.junit.Assert.assertEquals;
import java.awt.geom.Rectangle2D;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.poi.POIDataSamples;
import org.apache.poi.hslf.usermodel.HSLFSlideShow;
import org.apache.poi.sl.usermodel.SlideShow;
import org.apache.poi.sl.usermodel.SlideShowFactory;
import org.apache.poi.sl.usermodel.TableCell;
import org.apache.poi.sl.usermodel.TableShape;
import org.apache.poi.xslf.XSLFTestDataSamples;
import org.apache.poi.sl.usermodel.TextShape.TextDirection;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.junit.Test;
@ -89,4 +94,42 @@ public class TestTable {
}
}
@Test
public void testTextDirection() throws IOException {
TextDirection tds[] = {
TextDirection.HORIZONTAL,
TextDirection.VERTICAL,
TextDirection.VERTICAL_270,
// TextDirection.STACKED is not supported on HSLF
};
for (int i=0; i<2; i++) {
SlideShow<?,?> ppt1 = (i == 0) ? new HSLFSlideShow() : new XMLSlideShow();
TableShape<?,?> tbl1 = ppt1.createSlide().createTable(1, 3);
tbl1.setAnchor(new Rectangle2D.Double(50, 50, 200, 200));
int col = 0;
for (TextDirection td : tds) {
TableCell<?,?> c = tbl1.getCell(0, col++);
c.setTextDirection(td);
c.setText("bla");
}
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ppt1.write(bos);
ppt1.close();
InputStream is = new ByteArrayInputStream(bos.toByteArray());
SlideShow<?,?> ppt2 = SlideShowFactory.create(is);
TableShape<?,?> tbl2 = (TableShape<?,?>)ppt2.getSlides().get(0).getShapes().get(0);
col = 0;
for (TextDirection td : tds) {
TableCell<?,?> c = tbl2.getCell(0, col++);
assertEquals(td, c.getTextDirection());
}
ppt2.close();
}
}
}

View File

@ -660,15 +660,51 @@ implements TextShape<HSLFShape,HSLFTextParagraph> {
@Override
public TextDirection getTextDirection() {
// TODO: determine vertical text setting
// see 2.3.22.10 Geometry Text Boolean Properties
return TextDirection.HORIZONTAL;
// see 2.4.5 MSOTXFL
AbstractEscherOptRecord opt = getEscherOptRecord();
EscherSimpleProperty prop = getEscherProperty(opt, EscherProperties.TEXT__TEXTFLOW);
int msotxfl = (prop == null) ? 0 : prop.getPropertyValue();
switch (msotxfl) {
default:
case 0: // msotxflHorzN
case 4: // msotxflHorzA
return TextDirection.HORIZONTAL;
case 1: // msotxflTtoBA
case 3: // msotxflTtoBN
case 5: // msotxflVertN
return TextDirection.VERTICAL;
case 2: // msotxflBtoT
return TextDirection.VERTICAL_270;
// TextDirection.STACKED is not supported
}
}
@Override
public void setTextDirection(TextDirection orientation) {
// TODO: determine vertical text setting
// see 2.3.22.10 Geometry Text Boolean Properties / gtextFVertical [MS-ODRAW]
AbstractEscherOptRecord opt = getEscherOptRecord();
int msotxfl;
if (orientation == null) {
msotxfl = -1;
} else {
switch (orientation) {
default:
case STACKED:
// not supported -> remove
msotxfl = -1;
break;
case HORIZONTAL:
msotxfl = 0;
break;
case VERTICAL:
msotxfl = 1;
break;
case VERTICAL_270:
// always interpreted as horizontal
msotxfl = 2;
break;
}
}
setEscherProperty(opt, EscherProperties.TEXT__TEXTFLOW, msotxfl);
}
@Override