From f038906105abccd33ef16fb361c98571214198f0 Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Tue, 19 Sep 2006 22:47:44 +0000 Subject: [PATCH] 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 --- .../content/xdocs/hslf/how-to-shapes.xml | 20 ++++---- .../org/apache/poi/hslf/model/TextBox.java | 38 ++++++++++----- .../poi/hslf/record/StyleTextPropAtom.java | 8 ++++ .../poi/hslf/usermodel/RichTextRun.java | 47 +++++++++++++++++++ .../poi/hslf/usermodel/TestRichTextRun.java | 17 +++++++ 5 files changed, 107 insertions(+), 23 deletions(-) diff --git a/src/documentation/content/xdocs/hslf/how-to-shapes.xml b/src/documentation/content/xdocs/hslf/how-to-shapes.xml index fb8229304..8c90d2b8f 100644 --- a/src/documentation/content/xdocs/hslf/how-to-shapes.xml +++ b/src/documentation/content/xdocs/hslf/how-to-shapes.xml @@ -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); diff --git a/src/scratchpad/src/org/apache/poi/hslf/model/TextBox.java b/src/scratchpad/src/org/apache/poi/hslf/model/TextBox.java index 760fa1b21..755dd4d98 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/model/TextBox.java +++ b/src/scratchpad/src/org/apache/poi/hslf/model/TextBox.java @@ -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 Font object for this text frame * * @param size the size of the font - */ + * + * @deprecated Use RichTextRun to work with the text format. + *

This method will be permanently removed in a future version of the POI HSLF API.

+ */ 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 RichTextRun to work with the text format. + *

This method will be permanently removed in a future version of the POI HSLF API.

*/ 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 RichTextRun to work with the text format. + *

This method will be permanently removed in a future version of the POI HSLF API.

*/ 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 true if the text should be bold, false otherwise + * + * @deprecated Use RichTextRun to work with the text format. + *

This method will be permanently removed in a future version of the POI HSLF API.

*/ 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 true if the text should be italic, false otherwise + * + * @deprecated Use RichTextRun to work with the text format. + *

This method will be permanently removed in a future version of the POI HSLF API.

*/ 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 true if the text should be underlined, false otherwise + * + * @deprecated Use RichTextRun to work with the text format. + *

This method will be permanently removed in a future version of the POI HSLF API.

*/ 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 RichTextRun to work with the text format. + *

This method will be permanently removed in a future version of the POI HSLF API.

*/ 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 RichTextRun to work with the text format. + *

This method will be permanently removed in a future version of the POI HSLF API.

*/ 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(){ diff --git a/src/scratchpad/src/org/apache/poi/hslf/record/StyleTextPropAtom.java b/src/scratchpad/src/org/apache/poi/hslf/record/StyleTextPropAtom.java index 17d2101f0..8ab88c69e 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/record/StyleTextPropAtom.java +++ b/src/scratchpad/src/org/apache/poi/hslf/record/StyleTextPropAtom.java @@ -490,6 +490,14 @@ public class StyleTextPropAtom extends RecordAtom } } } + + public short getReservedField(){ + return reservedField; + } + + public void setReservedField(short val){ + reservedField = val; + } } diff --git a/src/scratchpad/src/org/apache/poi/hslf/usermodel/RichTextRun.java b/src/scratchpad/src/org/apache/poi/hslf/usermodel/RichTextRun.java index 5f4cd2422..082b03f33 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/usermodel/RichTextRun.java +++ b/src/scratchpad/src/org/apache/poi/hslf/usermodel/RichTextRun.java @@ -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 Align* constants defined in the TextBox 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 Align* constants defined in the TextBox 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! ------- diff --git a/src/scratchpad/testcases/org/apache/poi/hslf/usermodel/TestRichTextRun.java b/src/scratchpad/testcases/org/apache/poi/hslf/usermodel/TestRichTextRun.java index 599f39d4a..d5f788349 100644 --- a/src/scratchpad/testcases/org/apache/poi/hslf/usermodel/TestRichTextRun.java +++ b/src/scratchpad/testcases/org/apache/poi/hslf/usermodel/TestRichTextRun.java @@ -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 ); + } + + } + } + } }