Support alignment and indent levels for HSLF - patch from Yegor in bug #40324
git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@448004 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
6e1236c74a
commit
f038906105
@ -120,17 +120,17 @@
|
||||
//TextBox
|
||||
TextBox txt = new TextBox();
|
||||
txt.setText("Hello, World!");
|
||||
txt.setAnchor(new java.awt.Rectangle(300, 100, 300, 50));
|
||||
txt.setAnchor(new java.awt.Rectangle(300, 100, 300, 50));
|
||||
|
||||
//use RichTextRun to work with the text format
|
||||
RichTextRun rt = txt.getRichTextRuns()[0];
|
||||
rt.setFontSize(32);
|
||||
rt.setFontName("Arial");
|
||||
rt.setBold(true);
|
||||
rt.setItalic(true);
|
||||
rt.setUnderlined(true);
|
||||
rt.setFontColor(Color.red);
|
||||
rt.setAlignment(TextBox.AlignRight);
|
||||
//use RichTextRun to work with the text format
|
||||
RichTextRun rt = txt.getRichTextRuns()[0];
|
||||
rt.setFontSize(32);
|
||||
rt.setFontName("Arial");
|
||||
rt.setBold(true);
|
||||
rt.setItalic(true);
|
||||
rt.setUnderlined(true);
|
||||
rt.setFontColor(Color.red);
|
||||
rt.setAlignment(TextBox.AlignRight);
|
||||
|
||||
slide.addShape(txt);
|
||||
|
||||
|
@ -85,8 +85,6 @@ public class TextBox extends SimpleShape {
|
||||
*/
|
||||
protected EscherTextboxWrapper _txtbox;
|
||||
|
||||
private String _fontname;
|
||||
|
||||
/**
|
||||
* Create a TextBox object and initialize it from the supplied Record container.
|
||||
*
|
||||
@ -432,7 +430,10 @@ public class TextBox extends SimpleShape {
|
||||
* Sets the <code>Font</code> object for this text frame
|
||||
*
|
||||
* @param size the size of the font
|
||||
*/
|
||||
*
|
||||
* @deprecated Use <code>RichTextRun</code> to work with the text format.
|
||||
* <p>This method will be permanently removed in a future version of the POI HSLF API.</p>
|
||||
*/
|
||||
public void setFontSize(int size){
|
||||
RichTextRun rt = _txtrun.getRichTextRuns()[0];
|
||||
rt.setFontSize(size);
|
||||
@ -441,6 +442,9 @@ public class TextBox extends SimpleShape {
|
||||
/**
|
||||
*
|
||||
* @return the size of the font applied to this text shape
|
||||
*
|
||||
* @deprecated Use <code>RichTextRun</code> to work with the text format.
|
||||
* <p>This method will be permanently removed in a future version of the POI HSLF API.</p>
|
||||
*/
|
||||
public int getFontSize(){
|
||||
RichTextRun rt = _txtrun.getRichTextRuns()[0];
|
||||
@ -450,6 +454,9 @@ public class TextBox extends SimpleShape {
|
||||
/**
|
||||
*
|
||||
* @return the size of the font applied to this text shape
|
||||
*
|
||||
* @deprecated Use <code>RichTextRun</code> to work with the text format.
|
||||
* <p>This method will be permanently removed in a future version of the POI HSLF API.</p>
|
||||
*/
|
||||
public Color getFontColor(){
|
||||
RichTextRun rt = _txtrun.getRichTextRuns()[0];
|
||||
@ -462,6 +469,9 @@ public class TextBox extends SimpleShape {
|
||||
* Set whether to use bold or not
|
||||
*
|
||||
* @param bold <code>true</code> if the text should be bold, <code>false</code> otherwise
|
||||
*
|
||||
* @deprecated Use <code>RichTextRun</code> to work with the text format.
|
||||
* <p>This method will be permanently removed in a future version of the POI HSLF API.</p>
|
||||
*/
|
||||
public void setBold(boolean bold){
|
||||
RichTextRun rt = _txtrun.getRichTextRuns()[0];
|
||||
@ -472,6 +482,9 @@ public class TextBox extends SimpleShape {
|
||||
* Set whether to use italic or not
|
||||
*
|
||||
* @param italic <code>true</code> if the text should be italic, <code>false</code> otherwise
|
||||
*
|
||||
* @deprecated Use <code>RichTextRun</code> to work with the text format.
|
||||
* <p>This method will be permanently removed in a future version of the POI HSLF API.</p>
|
||||
*/
|
||||
public void setItalic(boolean italic){
|
||||
RichTextRun rt = _txtrun.getRichTextRuns()[0];
|
||||
@ -482,6 +495,9 @@ public class TextBox extends SimpleShape {
|
||||
* Set whether to use underline or not
|
||||
*
|
||||
* @param underline <code>true</code> if the text should be underlined, <code>false</code> otherwise
|
||||
*
|
||||
* @deprecated Use <code>RichTextRun</code> to work with the text format.
|
||||
* <p>This method will be permanently removed in a future version of the POI HSLF API.</p>
|
||||
*/
|
||||
public void setUnderline(boolean underline){
|
||||
RichTextRun rt = _txtrun.getRichTextRuns()[0];
|
||||
@ -492,20 +508,21 @@ public class TextBox extends SimpleShape {
|
||||
* Sets the font of this text shape
|
||||
*
|
||||
* @param name the name of the font to be applied to this text shape
|
||||
*
|
||||
* @deprecated Use <code>RichTextRun</code> to work with the text format.
|
||||
* <p>This method will be permanently removed in a future version of the POI HSLF API.</p>
|
||||
*/
|
||||
public void setFontName(String name){
|
||||
if (_sheet == null) {
|
||||
//we can't set font since slideshow is not assigned yet
|
||||
_fontname = name;
|
||||
} else{
|
||||
RichTextRun rt = _txtrun.getRichTextRuns()[0];
|
||||
rt.setFontName(name);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the font color
|
||||
* @param color the font color
|
||||
*
|
||||
* @deprecated Use <code>RichTextRun</code> to work with the text format.
|
||||
* <p>This method will be permanently removed in a future version of the POI HSLF API.</p>
|
||||
*/
|
||||
public void setFontColor(Color color){
|
||||
//in PowerPont RGB bytes are swapped,
|
||||
@ -535,11 +552,6 @@ public class TextBox extends SimpleShape {
|
||||
for (int i = 0; i < rt.length; i++) {
|
||||
rt[i].supplySlideShow(_sheet.getSlideShow());
|
||||
}
|
||||
if (_fontname != null) {
|
||||
setFontName(_fontname);
|
||||
_fontname = null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void initTextRun(){
|
||||
|
@ -490,6 +490,14 @@ public class StyleTextPropAtom extends RecordAtom
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public short getReservedField(){
|
||||
return reservedField;
|
||||
}
|
||||
|
||||
public void setReservedField(short val){
|
||||
reservedField = val;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -24,6 +24,8 @@ import org.apache.poi.hslf.record.StyleTextPropAtom.CharFlagsTextProp;
|
||||
import org.apache.poi.hslf.record.StyleTextPropAtom.TextProp;
|
||||
import org.apache.poi.hslf.record.StyleTextPropAtom.TextPropCollection;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
/**
|
||||
* Represents a run of text, all with the same style
|
||||
*
|
||||
@ -54,6 +56,7 @@ public class RichTextRun
|
||||
private boolean sharingParagraphStyle;
|
||||
private boolean sharingCharacterStyle;
|
||||
|
||||
private String _fontname;
|
||||
/**
|
||||
* Create a new wrapper around a (currently not)
|
||||
* rich text string
|
||||
@ -104,6 +107,10 @@ public class RichTextRun
|
||||
*/
|
||||
public void supplySlideShow(SlideShow ss) {
|
||||
slideShow = ss;
|
||||
if (_fontname != null) {
|
||||
setFontName(_fontname);
|
||||
_fontname = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -280,10 +287,15 @@ public class RichTextRun
|
||||
}
|
||||
|
||||
public void setFontName(String fontName) {
|
||||
if (slideShow == null) {
|
||||
//we can't set font since slideshow is not assigned yet
|
||||
_fontname = fontName;
|
||||
} else{
|
||||
// Get the index for this font (adding if needed)
|
||||
int fontIdx = slideShow.getFontCollection().addFont(fontName);
|
||||
setCharTextPropVal("font.index", fontIdx);
|
||||
}
|
||||
}
|
||||
public String getFontName() {
|
||||
int fontIdx = getCharTextPropVal("font.index");
|
||||
if(fontIdx == -1) { return null; }
|
||||
@ -305,6 +317,41 @@ public class RichTextRun
|
||||
setCharTextPropVal("font.color", rgb);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets color of the text, as a java.awt.Color
|
||||
*/
|
||||
public void setFontColor(Color color) {
|
||||
//in PowerPont RGB bytes are swapped,
|
||||
int rgb = new Color(color.getBlue(), color.getGreen(), color.getRed(), 254).getRGB();
|
||||
setFontColor(rgb);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the type of horizontal alignment for the text.
|
||||
* One of the <code>Align*</code> constants defined in the <code>TextBox</code> class.
|
||||
*
|
||||
* @param align - the type of alignment
|
||||
*/
|
||||
public void setAlignment(int align) {
|
||||
setParaTextPropVal("alignment", align);
|
||||
}
|
||||
/**
|
||||
* Returns the type of horizontal alignment for the text.
|
||||
* One of the <code>Align*</code> constants defined in the <code>TextBox</class> class.
|
||||
*
|
||||
* @return the type of alignment
|
||||
*/
|
||||
public int getAlignment() {
|
||||
return getParaTextPropVal("alignment");
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return indentation level
|
||||
*/
|
||||
public int getIndentLevel() {
|
||||
return paragraphStyle == null ? 0 : paragraphStyle.getReservedField();
|
||||
}
|
||||
|
||||
// --------------- Internal HSLF methods, not intended for end-user use! -------
|
||||
|
||||
|
@ -3,6 +3,7 @@ package org.apache.poi.hslf.usermodel;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.File;
|
||||
|
||||
import org.apache.poi.hslf.HSLFSlideShow;
|
||||
import org.apache.poi.hslf.model.Slide;
|
||||
@ -420,4 +421,20 @@ if(false) {
|
||||
r.writeOut(baos);
|
||||
return baos.toByteArray();
|
||||
}
|
||||
|
||||
public void testIndentationLevel() throws Exception {
|
||||
SlideShow ppt = new SlideShow(new HSLFSlideShow(new File(System.getProperty("HSLF.testdata.path"), "ParagraphStylesShorterThanCharStyles.ppt").getPath()));
|
||||
Slide[] sl = ppt.getSlides();
|
||||
for (int i = 0; i < sl.length; i++) {
|
||||
TextRun[] txt = sl[i].getTextRuns();
|
||||
for (int j = 0; j < txt.length; j++) {
|
||||
RichTextRun[] rt = txt[j].getRichTextRuns();
|
||||
for (int k = 0; k < rt.length; k++) {
|
||||
int indent = rt[k].getIndentLevel();
|
||||
assertTrue(indent >= 0 && indent <= 4 );
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user