From 9b09cb683ab24180411f033a8ba9ed2d6073ebca Mon Sep 17 00:00:00 2001 From: Andreas Beeker Date: Sat, 18 Jul 2015 20:35:28 +0000 Subject: [PATCH] Introduce dirty flag for paragraphs and store them to records on save git-svn-id: https://svn.apache.org/repos/asf/poi/branches/common_sl@1691774 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/hslf/usermodel/HSLFSlideShow.java | 14 +++++++ .../poi/hslf/usermodel/HSLFTextParagraph.java | 42 +++++++++++++++---- .../poi/hslf/usermodel/HSLFTextRun.java | 11 +++-- .../poi/hslf/usermodel/TestTextRun.java | 2 +- 4 files changed, 57 insertions(+), 12 deletions(-) diff --git a/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFSlideShow.java b/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFSlideShow.java index 5cc313e91f..ec7de6635c 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFSlideShow.java +++ b/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFSlideShow.java @@ -442,6 +442,20 @@ public final class HSLFSlideShow implements SlideShow { * OutputStream */ public void write(OutputStream out) throws IOException { + // check for text paragraph modifications + for (HSLFSlide sl : getSlides()) { + for (HSLFShape sh : sl.getShapes()) { + if (!(sh instanceof HSLFTextShape)) continue; + HSLFTextShape hts = (HSLFTextShape)sh; + boolean isDirty = false; + for (HSLFTextParagraph p : hts.getTextParagraphs()) { + isDirty |= p.isDirty(); + } + if (isDirty) hts.storeText(); + } + } + + _hslfSlideShow.write(out); } diff --git a/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFTextParagraph.java b/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFTextParagraph.java index 386447328b..b07a8f8b67 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFTextParagraph.java +++ b/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFTextParagraph.java @@ -64,6 +64,8 @@ public final class HSLFTextParagraph implements TextParagraph { private int shapeId; private StyleTextProp9Atom styleTextProp9Atom; + + private boolean _dirty = false; /** * Constructs a Text Run from a Unicode text block. @@ -265,7 +267,7 @@ public final class HSLFTextParagraph implements TextParagraph { @Override public void setLeftMargin(Double leftMargin) { Integer val = (leftMargin == null) ? null : Units.pointsToMaster(leftMargin); - setPropVal(_paragraphStyle, "text.offset", val); + setParagraphTextPropVal("text.offset", val); } @Override @@ -288,7 +290,7 @@ public final class HSLFTextParagraph implements TextParagraph { @Override public void setIndent(Double indent) { Integer val = (indent == null) ? null : Units.pointsToMaster(indent); - setPropVal(_paragraphStyle, "bullet.offset", val); + setParagraphTextPropVal("bullet.offset", val); } @Override @@ -327,7 +329,7 @@ public final class HSLFTextParagraph implements TextParagraph { case JUSTIFY_LOW: alignInt = TextAlignmentProp.JUSTIFYLOW; break; case THAI_DIST: alignInt = TextAlignmentProp.THAIDISTRIBUTED; break; } - setPropVal(_paragraphStyle, "alignment", alignInt); + setParagraphTextPropVal("alignment", alignInt); } @Override @@ -455,7 +457,7 @@ public final class HSLFTextParagraph implements TextParagraph { */ public void setBulletChar(Character c) { Integer val = (c == null) ? null : (int)c.charValue(); - setPropVal(_paragraphStyle, "bullet.char", val); + setParagraphTextPropVal("bullet.char", val); } /** @@ -485,7 +487,7 @@ public final class HSLFTextParagraph implements TextParagraph { */ public void setBulletColor(Color color) { Integer val = (color == null) ? null : new Color(color.getBlue(), color.getGreen(), color.getRed(), 254).getRGB(); - setPropVal(_paragraphStyle, "bullet.color", val); + setParagraphTextPropVal("bullet.color", val); } /** @@ -513,7 +515,7 @@ public final class HSLFTextParagraph implements TextParagraph { FontCollection fc = getSheet().getSlideShow().getFontCollection(); int idx = fc.addFont(typeface); - setPropVal(_paragraphStyle, "bullet.font", idx); + setParagraphTextPropVal("bullet.font", idx); setFlag(ParagraphFlagsTextProp.BULLET_HARDFONT_IDX, true); } @@ -576,7 +578,7 @@ public final class HSLFTextParagraph implements TextParagraph { if (dval != null) { ival = (dval < 0) ? Units.pointsToMaster(dval) : dval.intValue(); } - setPropVal(_paragraphStyle, propName, ival); + setParagraphTextPropVal(propName, ival); } private boolean getFlag(int index) { @@ -587,6 +589,7 @@ public final class HSLFTextParagraph implements TextParagraph { private void setFlag(int index, boolean value) { BitMaskTextProp tp = (BitMaskTextProp)_paragraphStyle.addWithName(ParagraphFlagsTextProp.NAME); tp.setSubValue(value, index); + setDirty(); } /** @@ -813,6 +816,10 @@ public final class HSLFTextParagraph implements TextParagraph { throw new RuntimeException("failed dummy write", e); } } + + for (HSLFTextParagraph p : paragraphs) { + p._dirty = false; + } } /** @@ -1260,4 +1267,25 @@ public final class HSLFTextParagraph implements TextParagraph { } return new Color(tmp.getBlue(), tmp.getGreen(), tmp.getRed()); } + + /** + * Sets the value of the given Paragraph TextProp, add if required + * @param propName The name of the Paragraph TextProp + * @param val The value to set for the TextProp + */ + public void setParagraphTextPropVal(String propName, Integer val) { + setPropVal(_paragraphStyle, propName, val); + setDirty(); + } + + /** + * marks this paragraph dirty, so its records will be renewed on save + */ + public void setDirty() { + _dirty = true; + } + + public boolean isDirty() { + return _dirty; + } } diff --git a/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFTextRun.java b/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFTextRun.java index d12bfe3de8..8728eabb6b 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFTextRun.java +++ b/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFTextRun.java @@ -18,7 +18,6 @@ package org.apache.poi.hslf.usermodel; import static org.apache.poi.hslf.usermodel.HSLFTextParagraph.getPropVal; -import static org.apache.poi.hslf.usermodel.HSLFTextParagraph.setPropVal; import java.awt.Color; @@ -133,7 +132,10 @@ public final class HSLFTextRun implements TextRun { */ private void setCharFlagsTextPropVal(int index, boolean value) { // TODO: check if paragraph/chars can be handled the same ... - if (getFlag(index) != value) setFlag(index, value); + if (getFlag(index) != value) { + setFlag(index, value); + parentParagraph.setDirty(); + } } /** @@ -142,7 +144,8 @@ public final class HSLFTextRun implements TextRun { * @param val The value to set for the TextProp */ public void setCharTextPropVal(String propName, Integer val) { - setPropVal(characterStyle, propName, val); + HSLFTextParagraph.setPropVal(characterStyle, propName, val); + parentParagraph.setDirty(); } @@ -248,7 +251,7 @@ public final class HSLFTextRun implements TextRun { * @param val the percentage of the font size. If the value is positive, it is superscript, otherwise it is subscript */ public void setSuperscript(int val) { - setPropVal(characterStyle, "superscript", val); + setCharTextPropVal("superscript", val); } @Override diff --git a/src/scratchpad/testcases/org/apache/poi/hslf/usermodel/TestTextRun.java b/src/scratchpad/testcases/org/apache/poi/hslf/usermodel/TestTextRun.java index c2f7ac9d08..aa0c307f93 100644 --- a/src/scratchpad/testcases/org/apache/poi/hslf/usermodel/TestTextRun.java +++ b/src/scratchpad/testcases/org/apache/poi/hslf/usermodel/TestTextRun.java @@ -525,7 +525,7 @@ public final class TestTextRun { rt.setFontColor(Color.RED); } } - tx.storeText(); + // tx.storeText(); } } ByteArrayOutputStream out = new ByteArrayOutputStream();