]> source.dussan.org Git - poi.git/commitdiff
#45124 - inserting text or images wipes out boldness and makes everything italic
authorAndreas Beeker <kiwiwings@apache.org>
Sat, 31 Oct 2015 23:05:09 +0000 (23:05 +0000)
committerAndreas Beeker <kiwiwings@apache.org>
Sat, 31 Oct 2015 23:05:09 +0000 (23:05 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1711704 13f79535-47bb-0310-9956-ffa450edef68

src/scratchpad/src/org/apache/poi/hslf/model/textproperties/TextPropCollection.java
src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFTextParagraph.java
src/scratchpad/testcases/org/apache/poi/hslf/record/TestStyleTextPropAtom.java
src/scratchpad/testcases/org/apache/poi/hslf/usermodel/TestBugs.java
test-data/slideshow/bug45124.ppt [new file with mode: 0644]

index 84cfe9940fc35c012354ef3548d634ef98e9d568..08696b2187e8d7956e52aa547e4b671b7dff7111 100644 (file)
@@ -20,6 +20,7 @@ package org.apache.poi.hslf.model.textproperties;
 import java.io.*;
 import java.util.*;
 
+import org.apache.poi.hslf.exceptions.HSLFException;
 import org.apache.poi.hslf.record.StyleTextPropAtom;
 import org.apache.poi.util.HexDump;
 import org.apache.poi.util.LittleEndian;
@@ -32,7 +33,7 @@ import org.apache.poi.util.LittleEndian;
  */
 public class TextPropCollection {
     /** All the different kinds of paragraph properties we might handle */
-    public static final TextProp[] paragraphTextPropTypes = {
+    private static final TextProp[] paragraphTextPropTypes = {
         // TextProp order is according to 2.9.20 TextPFException,
         // bitmask order can be different
         new ParagraphFlagsTextProp(),
@@ -60,7 +61,7 @@ public class TextPropCollection {
     };
     
     /** All the different kinds of character properties we might handle */
-    public static final TextProp[] characterTextPropTypes = new TextProp[] {
+    private static final TextProp[] characterTextPropTypes = new TextProp[] {
         new TextProp(0, 0x100000, "pp10ext"),
         new TextProp(0, 0x1000000, "newAsian.font.index"), // A bit that specifies whether the newEAFontRef field of the TextCFException10 structure that contains this CFMasks exists.
         new TextProp(0, 0x2000000, "cs.font.index"), // A bit that specifies whether the csFontRef field of the TextCFException10 structure that contains this CFMasks exists.
@@ -84,7 +85,7 @@ public class TextPropCollection {
     // indentLevel is only valid for paragraph collection
     // if it's set to -1, it must be omitted - see 2.9.36 TextMasterStyleLevel
     private short indentLevel = 0;
-       private final List<TextProp> textPropList = new ArrayList<TextProp>();
+       private final Map<String,TextProp> textProps = new HashMap<String,TextProp>();
     private int maskSpecial = 0;
     private final TextPropType textPropType;
     
@@ -98,101 +99,89 @@ public class TextPropCollection {
         this.textPropType = textPropType;
     }
 
-    public int getSpecialMask() { return maskSpecial; }
+    public int getSpecialMask() {
+        return maskSpecial;
+    }
 
        /** Fetch the number of characters this styling applies to */
-       public int getCharactersCovered() { return charactersCovered; }
-       /** Fetch the TextProps that define this styling */
-       public List<TextProp> getTextPropList() { return textPropList; }
+       public int getCharactersCovered() {
+           return charactersCovered;
+    }
+
+       /** Fetch the TextProps that define this styling in the record order */
+       public List<TextProp> getTextPropList() {
+           List<TextProp> orderedList = new ArrayList<TextProp>();
+        for (TextProp potProp : getPotentialProperties()) {
+            TextProp textProp = textProps.get(potProp.getName());
+            if (textProp != null) {
+                orderedList.add(textProp);
+            }
+        }
+           return orderedList;
+    }
        
        /** Fetch the TextProp with this name, or null if it isn't present */
-       public TextProp findByName(String textPropName) {
-               for(TextProp prop : textPropList) {
-                       if(prop.getName().equals(textPropName)) {
-                               return prop;
-                       }
-               }
-               return null;
+       public final TextProp findByName(String textPropName) {
+               return textProps.get(textPropName);
        }
 
-       public TextProp removeByName(String name) {
-           Iterator<TextProp> iter = textPropList.iterator();
-           TextProp tp = null;
-           while (iter.hasNext()) {
-               tp = iter.next();
-               if (tp.getName().equals(name)){
-                   iter.remove();
-                   break;
-               }
-           }
-           return tp;
+       public final TextProp removeByName(String name) {
+           return textProps.remove(name);
        }
        
-       /** Add the TextProp with this name to the list */
-       public TextProp addWithName(String name) {
-               // Find the base TextProp to base on
-               TextProp existing = findByName(name);
-               if (existing != null) return existing;
-               
-               TextProp base = null;
-               for (TextProp tp : getPotentialProperties()) {
-                   if (tp.getName().equals(name)) {
-                       base = tp;
-                       break;
-                   }
-               }
-               
-               if(base == null) {
-                       throw new IllegalArgumentException("No TextProp with name " + name + " is defined to add from. "
-                       + "Character and paragraphs have their own properties/names.");
-               }
-               
-               // Add a copy of this property, in the right place to the list
-               TextProp textProp = base.clone();
-               addProp(textProp);
-               return textProp;
-       }
-
-       public TextPropType getTextPropType() {
+       public final TextPropType getTextPropType() {
            return textPropType;
        }
        
        private TextProp[] getPotentialProperties() {
            return (textPropType == TextPropType.paragraph) ? paragraphTextPropTypes : characterTextPropTypes;
        }
+
+       /**
+        * Checks the paragraph or character properties for the given property name.
+        * Throws a HSLFException, if the name doesn't belong into this set of properties 
+        *
+        * @param name the property name
+        * @return if found, the property template to copy from
+        */
+       private TextProp validatePropName(String name) {
+       for (TextProp tp : getPotentialProperties()) {
+            if (tp.getName().equals(name)) {
+                return tp;
+            }
+        }
+       String errStr = 
+           "No TextProp with name " + name + " is defined to add from. " +
+           "Character and paragraphs have their own properties/names.";
+       throw new HSLFException(errStr);       
+       }
        
+    /** Add the TextProp with this name to the list */
+    public final TextProp addWithName(String name) {
+        // Find the base TextProp to base on
+        TextProp existing = findByName(name);
+        if (existing != null) return existing;
+        
+        // Add a copy of this property
+        TextProp textProp = validatePropName(name).clone();
+        textProps.put(name,textProp);
+        return textProp;
+    }
+
        /**
         * Add the property at the correct position. Replaces an existing property with the same name.
         *
         * @param textProp the property to be added
         */
-       public void addProp(TextProp textProp) {
-           assert(textProp != null);
-           
-        int pos = 0;
-        boolean found = false;
-        for (TextProp curProp : getPotentialProperties()) {
-            String potName = curProp.getName();
-            if (pos == textPropList.size() || potName.equals(textProp.getName())) {
-                if (textPropList.size() > pos && potName.equals(textPropList.get(pos).getName())) {
-                    // replace existing prop (with same name)
-                    textPropList.set(pos, textProp);
-                } else {
-                    textPropList.add(pos, textProp);
-                }
-                found = true;
-                break;
-            }
-            
-            if (potName.equals(textPropList.get(pos).getName())) {
-                pos++;
-            }
-        }
+       public final void addProp(TextProp textProp) {
+           if (textProp == null) {
+               throw new HSLFException("TextProp must not be null");
+           }
 
-        if(!found) {
-            String err = "TextProp with name " + textProp.getName() + " doesn't belong to this collection.";
-            throw new IllegalArgumentException(err);
-        }
+           String propName = textProp.getName();
+           validatePropName(propName);
+           
+           textProps.put(propName, textProp);
        }
 
        /**
@@ -254,8 +243,8 @@ public class TextPropCollection {
         this.charactersCovered = other.charactersCovered;
         this.indentLevel = other.indentLevel;
         this.maskSpecial = other.maskSpecial;
-        this.textPropList.clear();
-        for (TextProp tp : other.textPropList) {
+        this.textProps.clear();
+        for (TextProp tp : other.textProps.values()) {
             TextProp tpCopy = (tp instanceof BitMaskTextProp)
                 ? ((BitMaskTextProp)tp).cloneAll()
                 : tp.clone();
@@ -285,25 +274,22 @@ public class TextPropCollection {
 
                // Then the mask field
                int mask = maskSpecial;
-               for (TextProp textProp : textPropList) {
+               for (TextProp textProp : textProps.values()) {
             mask |= textProp.getWriteMask();
         }
                StyleTextPropAtom.writeLittleEndian(mask,o);
 
                // Then the contents of all the properties
-               for (TextProp potProp : getPotentialProperties()) {
-               for(TextProp textProp : textPropList) {
-                   if (!textProp.getName().equals(potProp.getName())) continue;
-                int val = textProp.getValue();
-                if (textProp instanceof BitMaskTextProp && textProp.getWriteMask() == 0) {
-                    // don't add empty properties, as they can't be recognized while reading
-                    continue;
-                } else if (textProp.getSize() == 2) {
-                               StyleTextPropAtom.writeLittleEndian((short)val,o);
-                       } else if (textProp.getSize() == 4) {
-                               StyleTextPropAtom.writeLittleEndian(val,o);
-                       }
-               }
+               for (TextProp textProp : getTextPropList()) {
+            int val = textProp.getValue();
+            if (textProp instanceof BitMaskTextProp && textProp.getWriteMask() == 0) {
+                // don't add empty properties, as they can't be recognized while reading
+                continue;
+            } else if (textProp.getSize() == 2) {
+                StyleTextPropAtom.writeLittleEndian((short)val,o);
+            } else if (textProp.getSize() == 4) {
+                StyleTextPropAtom.writeLittleEndian(val,o);
+            }
                }
        }
 
@@ -324,7 +310,7 @@ public class TextPropCollection {
         result = prime * result + charactersCovered;
         result = prime * result + maskSpecial;
         result = prime * result + indentLevel;
-        result = prime * result + ((textPropList == null) ? 0 : textPropList.hashCode());
+        result = prime * result + ((textProps == null) ? 0 : textProps.hashCode());
         return result;
     }
     /**
@@ -340,21 +326,7 @@ public class TextPropCollection {
             return false;
         }
 
-        if (textPropList == null) {
-            return (o.textPropList == null);
-        }        
-        
-        Map<String,TextProp> m = new HashMap<String,TextProp>();
-        for (TextProp tp : o.textPropList) {
-            m.put(tp.getName(), tp);
-        }
-        
-        for (TextProp tp : this.textPropList) {
-            TextProp otp = m.get(tp.getName());
-            if (!tp.equals(otp)) return false;
-        }
-        
-        return true;
+        return textProps.equals(o.textProps);
     }
 
     public String toString() {
index 79ca35caf77052adfde432208ebe25a89f4be47d..bc8ac93c6ee308732a990cb5e5aa2c95b32876ab 100644 (file)
@@ -26,6 +26,7 @@ import java.util.Arrays;
 import java.util.Iterator;\r
 import java.util.List;\r
 \r
+import org.apache.poi.hslf.exceptions.HSLFException;\r
 import org.apache.poi.hslf.model.PPFont;\r
 import org.apache.poi.hslf.model.textproperties.BitMaskTextProp;\r
 import org.apache.poi.hslf.model.textproperties.FontAlignmentProp;\r
@@ -466,6 +467,28 @@ public final class HSLFTextParagraph implements TextParagraph<HSLFShape,HSLFText
         };\r
     }\r
 \r
+    @Override\r
+    public void setBulletStyle(Object... styles) {\r
+        if (styles.length == 0) {\r
+            setBullet(false);\r
+        } else {\r
+            setBullet(true);\r
+            for (Object ostyle : styles) {\r
+                if (ostyle instanceof Number) {\r
+                    setBulletSize(((Number)ostyle).doubleValue());\r
+                } else if (ostyle instanceof Color) {\r
+                    setBulletColor((Color)ostyle);\r
+                } else if (ostyle instanceof Character) {\r
+                    setBulletChar((Character)ostyle);\r
+                } else if (ostyle instanceof String) {\r
+                    setBulletFont((String)ostyle);\r
+                } else if (ostyle instanceof AutoNumberingScheme) {\r
+                    throw new HSLFException("setting bullet auto-numberin scheme for HSLF not supported ... yet");\r
+                }\r
+            }\r
+        }\r
+    }\r
+    \r
     @Override\r
     public HSLFTextShape getParentShape() {\r
         return _parentShape;\r
@@ -535,6 +558,7 @@ public final class HSLFTextParagraph implements TextParagraph<HSLFShape,HSLFText
     public void setBulletColor(Color color) {\r
         Integer val = (color == null) ? null : new Color(color.getBlue(), color.getGreen(), color.getRed(), 254).getRGB();\r
         setParagraphTextPropVal("bullet.color", val);\r
+        setFlag(ParagraphFlagsTextProp.BULLET_HARDCOLOR_IDX, (color != null));\r
     }\r
 \r
     /**\r
@@ -542,7 +566,8 @@ public final class HSLFTextParagraph implements TextParagraph<HSLFShape,HSLFText
      */\r
     public Color getBulletColor() {\r
         TextProp tp = getPropVal(_paragraphStyle, "bullet.color", this);\r
-        if (tp == null) {\r
+        boolean hasColor = getFlag(ParagraphFlagsTextProp.BULLET_HARDCOLOR_IDX);\r
+        if (tp == null || !hasColor) {\r
             // if bullet color is undefined, return color of first run\r
             if (_runs.isEmpty()) return null;\r
             SolidPaint sp = _runs.get(0).getFontColor();\r
@@ -573,7 +598,8 @@ public final class HSLFTextParagraph implements TextParagraph<HSLFShape,HSLFText
      */\r
     public String getBulletFont() {\r
         TextProp tp = getPropVal(_paragraphStyle, "bullet.font", this);\r
-        if (tp == null) return getDefaultFontFamily();\r
+        boolean hasFont = getFlag(ParagraphFlagsTextProp.BULLET_HARDFONT_IDX);\r
+        if (tp == null || !hasFont) return getDefaultFontFamily();\r
         PPFont ppFont = getSheet().getSlideShow().getFont(tp.getValue());\r
         assert(ppFont != null);\r
         return ppFont.getFontName();\r
index e868aa67684666f8919221d58b0b47ad403c00e9..15877fdcf34286048c0de7ec535979e76457d893 100644 (file)
@@ -23,6 +23,7 @@ import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.util.List;
 
+import org.apache.poi.hslf.exceptions.HSLFException;
 import org.apache.poi.hslf.model.textproperties.*;
 import org.apache.poi.util.HexDump;
 import org.junit.Test;
@@ -375,8 +376,7 @@ public final class TestStyleTextPropAtom {
         assertEquals(0x0003, cf_4_1.getValue());
     }
 
-    @SuppressWarnings("unused")
-    @Test
+    @Test(expected=HSLFException.class)
     public void testFindAddTextProp() {
         StyleTextPropAtom stpb = new StyleTextPropAtom(data_b,0,data_b.length);
         stpb.setParentTextSize(data_b_text_len);
@@ -392,6 +392,16 @@ public final class TestStyleTextPropAtom {
         TextPropCollection b_ch_2 = b_ch_l.get(1);
         TextPropCollection b_ch_3 = b_ch_l.get(2);
         TextPropCollection b_ch_4 = b_ch_l.get(3);
+        
+        assertNotNull(b_p_1);
+        assertNotNull(b_p_2);
+        assertNotNull(b_p_3);
+        assertNotNull(b_p_4);
+        
+        assertNotNull(b_ch_1);
+        assertNotNull(b_ch_2);
+        assertNotNull(b_ch_3);
+        assertNotNull(b_ch_4);
 
         // CharFlagsTextProp: 3 doesn't have, 4 does
         assertNull(b_ch_3.findByName("char_flags"));
@@ -419,12 +429,7 @@ public final class TestStyleTextPropAtom {
         assertEquals(new_sa, b_p_2.getTextPropList().get(2));
 
         // Check we get an error with a made up one
-        try {
-            b_p_2.addWithName("madeUpOne");
-            fail();
-        } catch(IllegalArgumentException e) {
-            // Good, as expected
-        }
+        b_p_2.addWithName("madeUpOne");
     }
 
     /**
index bb150f7fdabf19cce1f4c6d46ae2626b2ebb7731..d18e8a26d3b12b2136d7de67f12fac19e6702f54 100644 (file)
@@ -22,9 +22,11 @@ import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 
+import java.awt.Color;
 import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.FileInputStream;
+import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.ArrayList;
@@ -47,10 +49,18 @@ import org.apache.poi.hslf.record.Document;
 import org.apache.poi.hslf.record.Record;
 import org.apache.poi.hslf.record.SlideListWithText;
 import org.apache.poi.hslf.record.SlideListWithText.SlideAtomsSet;
+import org.apache.poi.hslf.record.StyleTextPropAtom;
 import org.apache.poi.hslf.record.TextHeaderAtom;
+import org.apache.poi.sl.draw.DrawPaint;
+import org.apache.poi.sl.usermodel.PaintStyle;
+import org.apache.poi.sl.usermodel.PaintStyle.SolidPaint;
 import org.apache.poi.sl.usermodel.PictureData.PictureType;
+import org.apache.poi.sl.usermodel.Slide;
 import org.apache.poi.sl.usermodel.SlideShow;
 import org.apache.poi.sl.usermodel.SlideShowFactory;
+import org.apache.poi.sl.usermodel.TextBox;
+import org.apache.poi.sl.usermodel.TextParagraph;
+import org.apache.poi.sl.usermodel.TextRun;
 import org.apache.poi.util.LittleEndian;
 import org.apache.poi.util.StringUtil;
 import org.apache.poi.util.Units;
@@ -642,6 +652,41 @@ public final class TestBugs {
     
     @Test
     public void bug58516() throws IOException {
-        SlideShowFactory.create(_slTests.getFile("bug58516.ppt"));
+        SlideShowFactory.create(_slTests.getFile("bug58516.ppt")).close();
+    }
+
+    @Test
+    public void bug45124() throws IOException {
+        SlideShow<?,?> ppt = SlideShowFactory.create(_slTests.getFile("bug45124.ppt"));
+        Slide<?,?> slide1 = ppt.getSlides().get(1);
+
+        TextBox<?,?> res = slide1.createTextBox();
+        res.setAnchor(new java.awt.Rectangle(60, 150, 700, 100));
+        res.setText("I am italic-false, bold-true inserted text");
+        
+
+        TextParagraph<?,?,?> tp = res.getTextParagraphs().get(0);
+        TextRun rt = tp.getTextRuns().get(0);
+        rt.setItalic(false);
+        assertTrue(rt.isBold());
+        
+        tp.setBulletStyle(Color.red, 'A');
+
+        SlideShow<?,?> ppt2 = HSLFTestDataSamples.writeOutAndReadBack((HSLFSlideShow)ppt);
+        ppt.close();
+        
+        res = (TextBox<?,?>)ppt2.getSlides().get(1).getShapes().get(1);
+        tp = res.getTextParagraphs().get(0);
+        rt = tp.getTextRuns().get(0);
+        
+        assertFalse(rt.isItalic());
+        assertTrue(rt.isBold());
+        PaintStyle ps = tp.getBulletStyle().getBulletFontColor();
+        assertTrue(ps instanceof SolidPaint);
+        Color actColor = DrawPaint.applyColorTransform(((SolidPaint)ps).getSolidColor());
+        assertEquals(Color.red, actColor);
+        assertEquals("A", tp.getBulletStyle().getBulletCharacter());
+        
+        ppt2.close();
     }
 }
diff --git a/test-data/slideshow/bug45124.ppt b/test-data/slideshow/bug45124.ppt
new file mode 100644 (file)
index 0000000..2fd87b1
Binary files /dev/null and b/test-data/slideshow/bug45124.ppt differ