]> source.dussan.org Git - poi.git/commitdiff
Support alignment and indent levels for HSLF - patch from Yegor in bug #40324
authorNick Burch <nick@apache.org>
Tue, 19 Sep 2006 22:47:44 +0000 (22:47 +0000)
committerNick Burch <nick@apache.org>
Tue, 19 Sep 2006 22:47:44 +0000 (22:47 +0000)
git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@448004 13f79535-47bb-0310-9956-ffa450edef68

src/documentation/content/xdocs/hslf/how-to-shapes.xml
src/scratchpad/src/org/apache/poi/hslf/model/TextBox.java
src/scratchpad/src/org/apache/poi/hslf/record/StyleTextPropAtom.java
src/scratchpad/src/org/apache/poi/hslf/usermodel/RichTextRun.java
src/scratchpad/testcases/org/apache/poi/hslf/usermodel/TestRichTextRun.java

index fb8229304b2a499dfdc1e0cdf0695f5735457483..8c90d2b8f0a289eac0c71bb462aa44bf2f4068f5 100644 (file)
   //TextBox\r
   TextBox txt = new TextBox();\r
   txt.setText("Hello, World!");\r
-        txt.setAnchor(new java.awt.Rectangle(300, 100, 300, 50));\r
+  txt.setAnchor(new java.awt.Rectangle(300, 100, 300, 50));\r
 \r
-        //use RichTextRun to work with the text format\r
-        RichTextRun rt = txt.getRichTextRuns()[0];\r
-        rt.setFontSize(32);\r
-        rt.setFontName("Arial");\r
-        rt.setBold(true);\r
-        rt.setItalic(true);\r
-        rt.setUnderlined(true);\r
-        rt.setFontColor(Color.red);\r
-        rt.setAlignment(TextBox.AlignRight);\r
+  //use RichTextRun to work with the text format\r
+  RichTextRun rt = txt.getRichTextRuns()[0];\r
+  rt.setFontSize(32);\r
+  rt.setFontName("Arial");\r
+  rt.setBold(true);\r
+  rt.setItalic(true);\r
+  rt.setUnderlined(true);\r
+  rt.setFontColor(Color.red);\r
+  rt.setAlignment(TextBox.AlignRight);\r
 \r
   slide.addShape(txt);\r
 \r
index 760fa1b215cbfa380f42eb936e245225645d843d..755dd4d988514191d0f896b844968ac1361cac90 100644 (file)
@@ -85,8 +85,6 @@ public class TextBox extends SimpleShape {
      */\r
     protected EscherTextboxWrapper _txtbox;\r
 \r
-    private String _fontname;\r
-\r
     /**\r
      * Create a TextBox object and initialize it from the supplied Record container.\r
      * \r
@@ -432,7 +430,10 @@ public class TextBox extends SimpleShape {
      * Sets the <code>Font</code> object for this text frame\r
      *\r
      * @param size  the size of the font\r
-     */\r
+        *\r
+        * @deprecated Use <code>RichTextRun</code> to work with the text format.\r
+        * <p>This method will be permanently removed in a future version of the POI HSLF API.</p>\r
+        */\r
     public void setFontSize(int size){\r
         RichTextRun rt = _txtrun.getRichTextRuns()[0];\r
         rt.setFontSize(size);\r
@@ -441,6 +442,9 @@ public class TextBox extends SimpleShape {
     /**\r
      *\r
      * @return  the size of the font applied to this text shape\r
+     *\r
+        * @deprecated Use <code>RichTextRun</code> to work with the text format.\r
+        * <p>This method will be permanently removed in a future version of the POI HSLF API.</p>\r
      */\r
     public int getFontSize(){\r
         RichTextRun rt = _txtrun.getRichTextRuns()[0];\r
@@ -450,6 +454,9 @@ public class TextBox extends SimpleShape {
     /**\r
      *\r
      * @return  the size of the font applied to this text shape\r
+     *\r
+        * @deprecated Use <code>RichTextRun</code> to work with the text format.\r
+        * <p>This method will be permanently removed in a future version of the POI HSLF API.</p>\r
      */\r
     public Color getFontColor(){\r
         RichTextRun rt = _txtrun.getRichTextRuns()[0];\r
@@ -462,6 +469,9 @@ public class TextBox extends SimpleShape {
      * Set whether to use bold or not\r
      *\r
      * @param bold  <code>true</code>   if the text should be bold, <code>false</code>  otherwise\r
+     *\r
+        * @deprecated Use <code>RichTextRun</code> to work with the text format.\r
+        * <p>This method will be permanently removed in a future version of the POI HSLF API.</p>\r
      */\r
     public void setBold(boolean bold){\r
         RichTextRun rt = _txtrun.getRichTextRuns()[0];\r
@@ -472,6 +482,9 @@ public class TextBox extends SimpleShape {
      * Set whether to use italic or not\r
      *\r
      * @param italic  <code>true</code>   if the text should be italic, <code>false</code>  otherwise\r
+     *\r
+        * @deprecated Use <code>RichTextRun</code> to work with the text format.\r
+        * <p>This method will be permanently removed in a future version of the POI HSLF API.</p>\r
      */\r
     public void setItalic(boolean italic){\r
         RichTextRun rt = _txtrun.getRichTextRuns()[0];\r
@@ -482,6 +495,9 @@ public class TextBox extends SimpleShape {
      * Set whether to use underline or not\r
      *\r
      * @param underline  <code>true</code>   if the text should be underlined, <code>false</code>  otherwise\r
+     *\r
+        * @deprecated Use <code>RichTextRun</code> to work with the text format.\r
+        * <p>This method will be permanently removed in a future version of the POI HSLF API.</p>\r
      */\r
     public void setUnderline(boolean underline){\r
         RichTextRun rt = _txtrun.getRichTextRuns()[0];\r
@@ -492,20 +508,21 @@ public class TextBox extends SimpleShape {
      *  Sets the font of this text shape\r
      *\r
      * @param name  the name of the font to be applied to this text shape\r
+     *\r
+        * @deprecated Use <code>RichTextRun</code> to work with the text format.\r
+        * <p>This method will be permanently removed in a future version of the POI HSLF API.</p>\r
      */\r
     public void setFontName(String name){\r
-        if (_sheet == null) {\r
-            //we can't set font since slideshow is not assigned yet\r
-            _fontname = name;\r
-        } else{\r
         RichTextRun rt = _txtrun.getRichTextRuns()[0];\r
         rt.setFontName(name);\r
     }\r
-    }\r
 \r
     /**\r
      * Sets the font color\r
      * @param color  the font color\r
+     *\r
+        * @deprecated Use <code>RichTextRun</code> to work with the text format.\r
+        * <p>This method will be permanently removed in a future version of the POI HSLF API.</p>\r
      */\r
     public void setFontColor(Color color){\r
         //in PowerPont RGB bytes are swapped,\r
@@ -535,11 +552,6 @@ public class TextBox extends SimpleShape {
         for (int i = 0; i < rt.length; i++) {\r
             rt[i].supplySlideShow(_sheet.getSlideShow());\r
         }\r
-        if (_fontname != null) {\r
-            setFontName(_fontname);\r
-            _fontname = null;\r
-        }\r
-\r
     }\r
 \r
     private void initTextRun(){\r
index 17d2101f04c10e8a30b48f8d9a09247f7044b334..8ab88c69e377d8e54201d08016e4ef29e84f45ab 100644 (file)
@@ -490,6 +490,14 @@ public class StyleTextPropAtom extends RecordAtom
                                }
                        }
                }
+
+        public short getReservedField(){
+            return reservedField;
+        }
+
+        public void setReservedField(short val){
+            reservedField = val;
+        }
        }
 
 
index 5f4cd2422fdbc5643f1c81035fb2e52b30de6690..082b03f337554647fae93fe569528201c3b6f477 100644 (file)
@@ -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! -------
        
index 599f39d4aa654f1a23bc5cf6816a0b6d3e9553ae..d5f788349ec9f4f8bbc73822ba67694fd693c075 100644 (file)
@@ -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 );
+                }
+
+            }
+        }
+    }
 }