12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400 |
- /* ====================================================================
- Licensed to the Apache Software Foundation (ASF) under one or more
- contributor license agreements. See the NOTICE file distributed with
- this work for additional information regarding copyright ownership.
- The ASF licenses this file to You under the Apache License, Version 2.0
- (the "License"); you may not use this file except in compliance with
- the License. You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- ==================================================================== */
-
- package org.apache.poi.hslf.usermodel;
-
- import static org.apache.poi.hslf.record.RecordTypes.OutlineTextRefAtom;
-
- import java.awt.Color;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Iterator;
- import java.util.List;
-
- import org.apache.poi.hslf.exceptions.HSLFException;
- import org.apache.poi.hslf.model.PPFont;
- import org.apache.poi.hslf.model.textproperties.BitMaskTextProp;
- import org.apache.poi.hslf.model.textproperties.FontAlignmentProp;
- import org.apache.poi.hslf.model.textproperties.IndentProp;
- import org.apache.poi.hslf.model.textproperties.ParagraphFlagsTextProp;
- import org.apache.poi.hslf.model.textproperties.TextAlignmentProp;
- import org.apache.poi.hslf.model.textproperties.TextPFException9;
- import org.apache.poi.hslf.model.textproperties.TextProp;
- import org.apache.poi.hslf.model.textproperties.TextPropCollection;
- import org.apache.poi.hslf.model.textproperties.TextPropCollection.TextPropType;
- import org.apache.poi.hslf.record.ColorSchemeAtom;
- import org.apache.poi.hslf.record.EscherTextboxWrapper;
- import org.apache.poi.hslf.record.FontCollection;
- import org.apache.poi.hslf.record.MasterTextPropAtom;
- import org.apache.poi.hslf.record.OutlineTextRefAtom;
- import org.apache.poi.hslf.record.PPDrawing;
- import org.apache.poi.hslf.record.Record;
- import org.apache.poi.hslf.record.RecordContainer;
- import org.apache.poi.hslf.record.RecordTypes;
- import org.apache.poi.hslf.record.SlideListWithText;
- import org.apache.poi.hslf.record.SlidePersistAtom;
- import org.apache.poi.hslf.record.StyleTextProp9Atom;
- import org.apache.poi.hslf.record.StyleTextPropAtom;
- import org.apache.poi.hslf.record.TextBytesAtom;
- import org.apache.poi.hslf.record.TextCharsAtom;
- import org.apache.poi.hslf.record.TextHeaderAtom;
- import org.apache.poi.hslf.record.TextRulerAtom;
- import org.apache.poi.hslf.record.TextSpecInfoAtom;
- import org.apache.poi.sl.draw.DrawPaint;
- import org.apache.poi.sl.usermodel.AutoNumberingScheme;
- import org.apache.poi.sl.usermodel.PaintStyle;
- import org.apache.poi.sl.usermodel.PaintStyle.SolidPaint;
- import org.apache.poi.sl.usermodel.TextParagraph;
- import org.apache.poi.util.Internal;
- import org.apache.poi.util.LocaleUtil;
- import org.apache.poi.util.POILogFactory;
- import org.apache.poi.util.POILogger;
- import org.apache.poi.util.StringUtil;
- import org.apache.poi.util.Units;
-
- /**
- * This class represents a run of text in a powerpoint document. That
- * run could be text on a sheet, or text in a note.
- * It is only a very basic class for now
- *
- * @author Nick Burch
- */
-
- public final class HSLFTextParagraph implements TextParagraph<HSLFShape,HSLFTextParagraph,HSLFTextRun> {
- protected static final POILogger logger = POILogFactory.getLogger(HSLFTextParagraph.class);
-
- /**
- * How to align the text
- */
- /* package */static final int AlignLeft = 0;
- /* package */static final int AlignCenter = 1;
- /* package */static final int AlignRight = 2;
- /* package */static final int AlignJustify = 3;
-
- // Note: These fields are protected to help with unit testing
- // Other classes shouldn't really go playing with them!
- private final TextHeaderAtom _headerAtom;
- private TextBytesAtom _byteAtom;
- private TextCharsAtom _charAtom;
- private TextPropCollection _paragraphStyle = new TextPropCollection(1, TextPropType.paragraph);
-
- protected TextRulerAtom _ruler;
- protected final List<HSLFTextRun> _runs = new ArrayList<HSLFTextRun>();
- protected HSLFTextShape _parentShape;
- private HSLFSheet _sheet;
- private int shapeId;
-
- private StyleTextProp9Atom styleTextProp9Atom;
-
- private boolean _dirty = false;
-
- /**
- * Constructs a Text Run from a Unicode text block.
- * Either a {@link TextCharsAtom} or a {@link TextBytesAtom} needs to be provided.
- *
- * @param tha the TextHeaderAtom that defines what's what
- * @param tba the TextBytesAtom containing the text or null if {@link TextCharsAtom} is provided
- * @param tca the TextCharsAtom containing the text or null if {@link TextBytesAtom} is provided
- */
- /* package */ HSLFTextParagraph(
- TextHeaderAtom tha,
- TextBytesAtom tba,
- TextCharsAtom tca
- ) {
- if (tha == null) {
- throw new IllegalArgumentException("TextHeaderAtom must be set.");
- }
- _headerAtom = tha;
- _byteAtom = tba;
- _charAtom = tca;
- }
-
- /* package */HSLFTextParagraph(HSLFTextParagraph other) {
- _headerAtom = other._headerAtom;
- _byteAtom = other._byteAtom;
- _charAtom = other._charAtom;
- _parentShape = other._parentShape;
- _sheet = other._sheet;
- _ruler = other._ruler;
- shapeId = other.shapeId;
- _paragraphStyle.copy(other._paragraphStyle);
- }
-
- public void addTextRun(HSLFTextRun run) {
- _runs.add(run);
- }
-
- @Override
- public List<HSLFTextRun> getTextRuns() {
- return _runs;
- }
-
- public TextPropCollection getParagraphStyle() {
- return _paragraphStyle;
- }
-
- public void setParagraphStyle(TextPropCollection paragraphStyle) {
- _paragraphStyle.copy(paragraphStyle);
- }
-
- /**
- * Setting a master style reference
- *
- * @param paragraphStyle the master style reference
- *
- * @since POI 3.14-Beta1
- */
- @Internal
- /* package */ void setMasterStyleReference(TextPropCollection paragraphStyle) {
- _paragraphStyle = paragraphStyle;
- }
-
- /**
- * Supply the Sheet we belong to, which might have an assigned SlideShow
- * Also passes it on to our child RichTextRuns
- */
- public void supplySheet(HSLFSheet sheet) {
- this._sheet = sheet;
-
- if (_runs == null) return;
- for (HSLFTextRun rt : _runs) {
- rt.updateSheet();
- }
- }
-
- public HSLFSheet getSheet() {
- return this._sheet;
- }
-
- /**
- * @return Shape ID
- */
- protected int getShapeId() {
- return shapeId;
- }
-
- /**
- * @param id Shape ID
- */
- protected void setShapeId(int id) {
- shapeId = id;
- }
-
- /**
- * @return 0-based index of the text run in the SLWT container
- */
- protected int getIndex() {
- return (_headerAtom != null) ? _headerAtom.getIndex() : -1;
- }
-
- /**
- * Sets the index of the paragraph in the SLWT container
- *
- * @param index
- */
- protected void setIndex(int index) {
- if (_headerAtom != null) _headerAtom.setIndex(index);
- }
-
- /**
- * Returns the type of the text, from the TextHeaderAtom.
- * Possible values can be seen from TextHeaderAtom
- * @see org.apache.poi.hslf.record.TextHeaderAtom
- */
- public int getRunType() {
- return (_headerAtom != null) ? _headerAtom.getTextType() : -1;
- }
-
- public void setRunType(int runType) {
- if (_headerAtom != null) _headerAtom.setTextType(runType);
- }
-
- /**
- * Is this Text Run one from a {@link PPDrawing}, or is it
- * one from the {@link SlideListWithText}?
- */
- public boolean isDrawingBased() {
- return (getIndex() == -1);
- }
-
- public TextRulerAtom getTextRuler() {
- return _ruler;
-
- }
-
- public TextRulerAtom createTextRuler() {
- _ruler = getTextRuler();
- if (_ruler == null) {
- _ruler = TextRulerAtom.getParagraphInstance();
- Record childAfter = _byteAtom;
- if (childAfter == null) childAfter = _charAtom;
- if (childAfter == null) childAfter = _headerAtom;
- _headerAtom.getParentRecord().addChildAfter(_ruler, childAfter);
- }
- return _ruler;
- }
-
- /**
- * Returns records that make up the list of text paragraphs
- * (there can be misc InteractiveInfo, TxInteractiveInfo and other records)
- *
- * @return text run records
- */
- public Record[] getRecords() {
- Record r[] = _headerAtom.getParentRecord().getChildRecords();
- return getRecords(r, new int[] { 0 }, _headerAtom);
- }
-
- private static Record[] getRecords(Record[] records, int[] startIdx, TextHeaderAtom headerAtom) {
- if (records == null) {
- throw new NullPointerException("records need to be set.");
- }
-
- for (; startIdx[0] < records.length; startIdx[0]++) {
- Record r = records[startIdx[0]];
- if (r instanceof TextHeaderAtom && (headerAtom == null || r == headerAtom)) break;
- }
-
- if (startIdx[0] >= records.length) {
- logger.log(POILogger.INFO, "header atom wasn't found - container might contain only an OutlineTextRefAtom");
- return new Record[0];
- }
-
- int length;
- for (length = 1; startIdx[0] + length < records.length; length++) {
- Record r = records[startIdx[0]+length];
- if (r instanceof TextHeaderAtom || r instanceof SlidePersistAtom) break;
- }
-
- Record result[] = new Record[length];
- System.arraycopy(records, startIdx[0], result, 0, length);
- startIdx[0] += length;
-
- return result;
- }
-
- /** Numbered List info */
- public void setStyleTextProp9Atom(final StyleTextProp9Atom styleTextProp9Atom) {
- this.styleTextProp9Atom = styleTextProp9Atom;
- }
-
- /** Numbered List info */
- public StyleTextProp9Atom getStyleTextProp9Atom() {
- return this.styleTextProp9Atom;
- }
-
- @Override
- public Iterator<HSLFTextRun> iterator() {
- return _runs.iterator();
- }
-
- @Override
- public Double getLeftMargin() {
- TextProp val = getPropVal(_paragraphStyle, "text.offset", this);
- return (val == null) ? null : Units.masterToPoints(val.getValue());
- }
-
- @Override
- public void setLeftMargin(Double leftMargin) {
- Integer val = (leftMargin == null) ? null : Units.pointsToMaster(leftMargin);
- setParagraphTextPropVal("text.offset", val);
- }
-
- @Override
- public Double getRightMargin() {
- // TODO: find out, how to determine this value
- return null;
- }
-
- @Override
- public void setRightMargin(Double rightMargin) {
- // TODO: find out, how to set this value
- }
-
- @Override
- public Double getIndent() {
- TextProp val = getPropVal(_paragraphStyle, "bullet.offset", this);
- return (val == null) ? null : Units.masterToPoints(val.getValue());
- }
-
- @Override
- public void setIndent(Double indent) {
- Integer val = (indent == null) ? null : Units.pointsToMaster(indent);
- setParagraphTextPropVal("bullet.offset", val);
- }
-
- @Override
- public String getDefaultFontFamily() {
- String typeface = null;
- if (!_runs.isEmpty()) {
- typeface = _runs.get(0).getFontFamily();
- }
- return (typeface != null) ? typeface : "Arial";
- }
-
- @Override
- public Double getDefaultFontSize() {
- Double d = null;
- if (!_runs.isEmpty()) {
- d = _runs.get(0).getFontSize();
- }
-
- return (d != null) ? d : 12d;
- }
-
- @Override
- public void setTextAlign(TextAlign align) {
- Integer alignInt = null;
- if (align != null) switch (align) {
- default:
- case LEFT: alignInt = TextAlignmentProp.LEFT;break;
- case CENTER: alignInt = TextAlignmentProp.CENTER; break;
- case RIGHT: alignInt = TextAlignmentProp.RIGHT; break;
- case DIST: alignInt = TextAlignmentProp.DISTRIBUTED; break;
- case JUSTIFY: alignInt = TextAlignmentProp.JUSTIFY; break;
- case JUSTIFY_LOW: alignInt = TextAlignmentProp.JUSTIFYLOW; break;
- case THAI_DIST: alignInt = TextAlignmentProp.THAIDISTRIBUTED; break;
- }
- setParagraphTextPropVal("alignment", alignInt);
- }
-
- @Override
- public TextAlign getTextAlign() {
- TextProp tp = getPropVal(_paragraphStyle, "alignment", this);
- if (tp == null) return null;
- switch (tp.getValue()) {
- default:
- case TextAlignmentProp.LEFT: return TextAlign.LEFT;
- case TextAlignmentProp.CENTER: return TextAlign.CENTER;
- case TextAlignmentProp.RIGHT: return TextAlign.RIGHT;
- case TextAlignmentProp.JUSTIFY: return TextAlign.JUSTIFY;
- case TextAlignmentProp.JUSTIFYLOW: return TextAlign.JUSTIFY_LOW;
- case TextAlignmentProp.DISTRIBUTED: return TextAlign.DIST;
- case TextAlignmentProp.THAIDISTRIBUTED: return TextAlign.THAI_DIST;
- }
- }
-
- @Override
- public FontAlign getFontAlign() {
- TextProp tp = getPropVal(_paragraphStyle, FontAlignmentProp.NAME, this);
- if (tp == null) return null;
-
- switch (tp.getValue()) {
- case FontAlignmentProp.BASELINE: return FontAlign.BASELINE;
- case FontAlignmentProp.TOP: return FontAlign.TOP;
- case FontAlignmentProp.CENTER: return FontAlign.CENTER;
- case FontAlignmentProp.BOTTOM: return FontAlign.BOTTOM;
- default: return FontAlign.AUTO;
- }
- }
-
- public AutoNumberingScheme getAutoNumberingScheme() {
- if (styleTextProp9Atom == null) return null;
- TextPFException9[] ant = styleTextProp9Atom.getAutoNumberTypes();
- int level = getIndentLevel();
- if (ant == null || level == -1 || level >= ant.length) return null;
- return ant[level].getAutoNumberScheme();
- }
-
- public Integer getAutoNumberingStartAt() {
- if (styleTextProp9Atom == null) return null;
- TextPFException9[] ant = styleTextProp9Atom.getAutoNumberTypes();
- int level = getIndentLevel();
- if (ant == null || level >= ant.length) return null;
- Short startAt = ant[level].getAutoNumberStartNumber();
- assert(startAt != null);
- return startAt.intValue();
- }
-
-
- @Override
- public BulletStyle getBulletStyle() {
- if (!isBullet() && getAutoNumberingScheme() == null) return null;
-
- return new BulletStyle() {
- @Override
- public String getBulletCharacter() {
- Character chr = HSLFTextParagraph.this.getBulletChar();
- return (chr == null || chr == 0) ? "" : "" + chr;
- }
-
- @Override
- public String getBulletFont() {
- return HSLFTextParagraph.this.getBulletFont();
- }
-
- @Override
- public Double getBulletFontSize() {
- return HSLFTextParagraph.this.getBulletSize();
- }
-
- @Override
- public void setBulletFontColor(Color color) {
- setBulletFontColor(DrawPaint.createSolidPaint(color));
- }
-
- @Override
- public void setBulletFontColor(PaintStyle color) {
- if (!(color instanceof SolidPaint)) {
- throw new IllegalArgumentException("HSLF only supports SolidPaint");
- }
- SolidPaint sp = (SolidPaint)color;
- Color col = DrawPaint.applyColorTransform(sp.getSolidColor());
- HSLFTextParagraph.this.setBulletColor(col);
- }
-
- @Override
- public PaintStyle getBulletFontColor() {
- Color col = HSLFTextParagraph.this.getBulletColor();
- return DrawPaint.createSolidPaint(col);
- }
-
- @Override
- public AutoNumberingScheme getAutoNumberingScheme() {
- return HSLFTextParagraph.this.getAutoNumberingScheme();
- }
-
- @Override
- public Integer getAutoNumberingStartAt() {
- return HSLFTextParagraph.this.getAutoNumberingStartAt();
- }
- };
- }
-
- @Override
- public void setBulletStyle(Object... styles) {
- if (styles.length == 0) {
- setBullet(false);
- } else {
- setBullet(true);
- for (Object ostyle : styles) {
- if (ostyle instanceof Number) {
- setBulletSize(((Number)ostyle).doubleValue());
- } else if (ostyle instanceof Color) {
- setBulletColor((Color)ostyle);
- } else if (ostyle instanceof Character) {
- setBulletChar((Character)ostyle);
- } else if (ostyle instanceof String) {
- setBulletFont((String)ostyle);
- } else if (ostyle instanceof AutoNumberingScheme) {
- throw new HSLFException("setting bullet auto-numberin scheme for HSLF not supported ... yet");
- }
- }
- }
- }
-
- @Override
- public HSLFTextShape getParentShape() {
- return _parentShape;
- }
-
- public void setParentShape(HSLFTextShape parentShape) {
- _parentShape = parentShape;
- }
-
- @Override
- public int getIndentLevel() {
- return _paragraphStyle == null ? 0 : _paragraphStyle.getIndentLevel();
- }
-
- @Override
- public void setIndentLevel(int level) {
- if( _paragraphStyle != null ) _paragraphStyle.setIndentLevel((short)level);
- }
-
- /**
- * Sets whether this rich text run has bullets
- */
- public void setBullet(boolean flag) {
- setFlag(ParagraphFlagsTextProp.BULLET_IDX, flag);
- }
-
- /**
- * Returns whether this rich text run has bullets
- */
- public boolean isBullet() {
- return getFlag(ParagraphFlagsTextProp.BULLET_IDX);
- }
-
- /**
- * Sets the bullet character
- */
- public void setBulletChar(Character c) {
- Integer val = (c == null) ? null : (int)c.charValue();
- setParagraphTextPropVal("bullet.char", val);
- }
-
- /**
- * Returns the bullet character
- */
- public Character getBulletChar() {
- TextProp tp = getPropVal(_paragraphStyle, "bullet.char", this);
- return (tp == null) ? null : (char)tp.getValue();
- }
-
- /**
- * Sets the bullet size
- */
- public void setBulletSize(Double size) {
- setPctOrPoints("bullet.size", size);
- }
-
- /**
- * Returns the bullet size, null if unset
- */
- public Double getBulletSize() {
- return getPctOrPoints("bullet.size");
- }
-
- /**
- * Sets the bullet color
- */
- public void setBulletColor(Color color) {
- Integer val = (color == null) ? null : new Color(color.getBlue(), color.getGreen(), color.getRed(), 254).getRGB();
- setParagraphTextPropVal("bullet.color", val);
- setFlag(ParagraphFlagsTextProp.BULLET_HARDCOLOR_IDX, (color != null));
- }
-
- /**
- * Returns the bullet color
- */
- public Color getBulletColor() {
- TextProp tp = getPropVal(_paragraphStyle, "bullet.color", this);
- boolean hasColor = getFlag(ParagraphFlagsTextProp.BULLET_HARDCOLOR_IDX);
- if (tp == null || !hasColor) {
- // if bullet color is undefined, return color of first run
- if (_runs.isEmpty()) return null;
- SolidPaint sp = _runs.get(0).getFontColor();
- return DrawPaint.applyColorTransform(sp.getSolidColor());
- }
-
- return getColorFromColorIndexStruct(tp.getValue(), _sheet);
- }
-
- /**
- * Sets the bullet font
- */
- public void setBulletFont(String typeface) {
- if (typeface == null) {
- setPropVal(_paragraphStyle, "bullet.font", null);
- setFlag(ParagraphFlagsTextProp.BULLET_HARDFONT_IDX, false);
- }
-
- FontCollection fc = getSheet().getSlideShow().getFontCollection();
- int idx = fc.addFont(typeface);
-
- setParagraphTextPropVal("bullet.font", idx);
- setFlag(ParagraphFlagsTextProp.BULLET_HARDFONT_IDX, true);
- }
-
- /**
- * Returns the bullet font
- */
- public String getBulletFont() {
- TextProp tp = getPropVal(_paragraphStyle, "bullet.font", this);
- boolean hasFont = getFlag(ParagraphFlagsTextProp.BULLET_HARDFONT_IDX);
- if (tp == null || !hasFont) return getDefaultFontFamily();
- PPFont ppFont = getSheet().getSlideShow().getFont(tp.getValue());
- assert(ppFont != null);
- return ppFont.getFontName();
- }
-
- @Override
- public void setLineSpacing(Double lineSpacing) {
- setPctOrPoints("linespacing", lineSpacing);
- }
-
- @Override
- public Double getLineSpacing() {
- return getPctOrPoints("linespacing");
- }
-
- @Override
- public void setSpaceBefore(Double spaceBefore) {
- setPctOrPoints("spacebefore", spaceBefore);
- }
-
- @Override
- public Double getSpaceBefore() {
- return getPctOrPoints("spacebefore");
- }
-
- @Override
- public void setSpaceAfter(Double spaceAfter) {
- setPctOrPoints("spaceafter", spaceAfter);
- }
-
- @Override
- public Double getSpaceAfter() {
- return getPctOrPoints("spaceafter");
- }
-
- @Override
- public Double getDefaultTabSize() {
- // TODO: implement
- return null;
- }
-
- private Double getPctOrPoints(String propName) {
- TextProp tp = getPropVal(_paragraphStyle, propName, this);
- if (tp == null) return null;
- int val = tp.getValue();
- return (val < 0) ? Units.masterToPoints(val) : val;
- }
-
- private void setPctOrPoints(String propName, Double dval) {
- Integer ival = null;
- if (dval != null) {
- ival = (dval < 0) ? Units.pointsToMaster(dval) : dval.intValue();
- }
- setParagraphTextPropVal(propName, ival);
- }
-
- private boolean getFlag(int index) {
- BitMaskTextProp tp = (BitMaskTextProp)getPropVal(_paragraphStyle, ParagraphFlagsTextProp.NAME, this);
- return (tp == null) ? false : tp.getSubValue(index);
- }
-
- private void setFlag(int index, boolean value) {
- BitMaskTextProp tp = (BitMaskTextProp)_paragraphStyle.addWithName(ParagraphFlagsTextProp.NAME);
- tp.setSubValue(value, index);
- setDirty();
- }
-
- /**
- * Fetch the value of the given Paragraph related TextProp. Returns null if
- * that TextProp isn't present. If the TextProp isn't present, the value
- * from the appropriate Master Sheet will apply.
- *
- * The propName can be a comma-separated list, in case multiple equivalent values
- * are queried
- */
- protected static TextProp getPropVal(TextPropCollection props, String propName, HSLFTextParagraph paragraph) {
- String propNames[] = propName.split(",");
- for (String pn : propNames) {
- TextProp prop = props.findByName(pn);
- if (prop != null) return prop;
- }
-
- BitMaskTextProp maskProp = (BitMaskTextProp) props.findByName(ParagraphFlagsTextProp.NAME);
- boolean hardAttribute = (maskProp != null && maskProp.getValue() == 0);
- if (hardAttribute) return null;
-
- HSLFSheet sheet = paragraph.getSheet();
- int txtype = paragraph.getRunType();
- HSLFMasterSheet master = sheet.getMasterSheet();
- if (master == null) {
- logger.log(POILogger.WARN, "MasterSheet is not available");
- return null;
- }
-
- boolean isChar = props.getTextPropType() == TextPropType.character;
-
- for (String pn : propNames) {
- TextProp prop = master.getStyleAttribute(txtype, paragraph.getIndentLevel(), pn, isChar);
- if (prop != null) return prop;
- }
-
- return null;
- }
-
- /**
- * Returns the named TextProp, either by fetching it (if it exists) or
- * adding it (if it didn't)
- *
- * @param props the TextPropCollection to fetch from / add into
- * @param name the name of the TextProp to fetch/add
- * @param val the value, null if unset
- */
- protected static void setPropVal(TextPropCollection props, String name, Integer val) {
- if (val == null) {
- props.removeByName(name);
- return;
- }
-
- // Fetch / Add the TextProp
- TextProp tp = props.addWithName(name);
- tp.setValue(val);
- }
-
- /**
- * Check and add linebreaks to text runs leading other paragraphs
- *
- * @param paragraphs
- */
- protected static void fixLineEndings(List<HSLFTextParagraph> paragraphs) {
- HSLFTextRun lastRun = null;
- for (HSLFTextParagraph p : paragraphs) {
- if (lastRun != null && !lastRun.getRawText().endsWith("\r")) {
- lastRun.setText(lastRun.getRawText() + "\r");
- }
- List<HSLFTextRun> ltr = p.getTextRuns();
- if (ltr.isEmpty()) {
- throw new RuntimeException("paragraph without textruns found");
- }
- lastRun = ltr.get(ltr.size() - 1);
- assert (lastRun.getRawText() != null);
- }
- }
-
- /**
- * Search for a StyleTextPropAtom is for this text header (list of paragraphs)
- *
- * @param header the header
- * @param textLen the length of the rawtext, or -1 if the length is not known
- */
- private static StyleTextPropAtom findStyleAtomPresent(TextHeaderAtom header, int textLen) {
- boolean afterHeader = false;
- StyleTextPropAtom style = null;
- for (Record record : header.getParentRecord().getChildRecords()) {
- long rt = record.getRecordType();
- if (afterHeader && rt == RecordTypes.TextHeaderAtom.typeID) {
- // already on the next header, quit searching
- break;
- }
- afterHeader |= (header == record);
- if (afterHeader && rt == RecordTypes.StyleTextPropAtom.typeID) {
- // found it
- style = (StyleTextPropAtom) record;
- }
- }
-
- if (style == null) {
- logger.log(POILogger.INFO, "styles atom doesn't exist. Creating dummy record for later saving.");
- style = new StyleTextPropAtom((textLen < 0) ? 1 : textLen);
- } else {
- if (textLen >= 0) {
- style.setParentTextSize(textLen);
- }
- }
-
- return style;
- }
-
- /**
- * Saves the modified paragraphs/textrun to the records.
- * Also updates the styles to the correct text length.
- */
- protected static void storeText(List<HSLFTextParagraph> paragraphs) {
- fixLineEndings(paragraphs);
-
- String rawText = toInternalString(getRawText(paragraphs));
-
- // Will it fit in a 8 bit atom?
- boolean isUnicode = StringUtil.hasMultibyte(rawText);
- // isUnicode = true;
-
- TextHeaderAtom headerAtom = paragraphs.get(0)._headerAtom;
- TextBytesAtom byteAtom = paragraphs.get(0)._byteAtom;
- TextCharsAtom charAtom = paragraphs.get(0)._charAtom;
- StyleTextPropAtom styleAtom = findStyleAtomPresent(headerAtom, rawText.length());
-
- // Store in the appropriate record
- Record oldRecord = null, newRecord = null;
- if (isUnicode) {
- if (byteAtom != null || charAtom == null) {
- oldRecord = byteAtom;
- charAtom = new TextCharsAtom();
- }
- newRecord = charAtom;
- charAtom.setText(rawText);
- } else {
- if (charAtom != null || byteAtom == null) {
- oldRecord = charAtom;
- byteAtom = new TextBytesAtom();
- }
- newRecord = byteAtom;
- byte[] byteText = new byte[rawText.length()];
- StringUtil.putCompressedUnicode(rawText, byteText, 0);
- byteAtom.setText(byteText);
- }
- assert (newRecord != null);
-
- RecordContainer _txtbox = headerAtom.getParentRecord();
- Record[] cr = _txtbox.getChildRecords();
- int /* headerIdx = -1, */ textIdx = -1, styleIdx = -1;
- for (int i = 0; i < cr.length; i++) {
- Record r = cr[i];
- if (r == headerAtom) ; // headerIdx = i;
- else if (r == oldRecord || r == newRecord) textIdx = i;
- else if (r == styleAtom) styleIdx = i;
- }
-
- if (textIdx == -1) {
- // the old record was never registered, ignore it
- _txtbox.addChildAfter(newRecord, headerAtom);
- // textIdx = headerIdx + 1;
- } else {
- // swap not appropriated records - noop if unchanged
- cr[textIdx] = newRecord;
- }
-
- if (styleIdx == -1) {
- // Add the new StyleTextPropAtom after the TextCharsAtom / TextBytesAtom
- _txtbox.addChildAfter(styleAtom, newRecord);
- }
-
- for (HSLFTextParagraph p : paragraphs) {
- if (newRecord == byteAtom) {
- p._byteAtom = byteAtom;
- p._charAtom = null;
- } else {
- p._byteAtom = null;
- p._charAtom = charAtom;
- }
- }
-
- // Update the text length for its Paragraph and Character stylings
- // * reset the length, to the new string's length
- // * add on +1 if the last block
-
- styleAtom.clearStyles();
-
- TextPropCollection lastPTPC = null, lastRTPC = null, ptpc = null, rtpc = null;
- for (HSLFTextParagraph para : paragraphs) {
- ptpc = para.getParagraphStyle();
- ptpc.updateTextSize(0);
- if (!ptpc.equals(lastPTPC)) {
- lastPTPC = styleAtom.addParagraphTextPropCollection(0);
- lastPTPC.copy(ptpc);
- }
- for (HSLFTextRun tr : para.getTextRuns()) {
- rtpc = tr.getCharacterStyle();
- rtpc.updateTextSize(0);
- if (!rtpc.equals(lastRTPC)) {
- lastRTPC = styleAtom.addCharacterTextPropCollection(0);
- lastRTPC.copy(rtpc);
- }
- int len = tr.getLength();
- ptpc.updateTextSize(ptpc.getCharactersCovered() + len);
- rtpc.updateTextSize(len);
- lastPTPC.updateTextSize(lastPTPC.getCharactersCovered() + len);
- lastRTPC.updateTextSize(lastRTPC.getCharactersCovered() + len);
- }
- }
-
- assert (lastPTPC != null && lastRTPC != null && ptpc != null && rtpc != null);
- ptpc.updateTextSize(ptpc.getCharactersCovered() + 1);
- rtpc.updateTextSize(rtpc.getCharactersCovered() + 1);
- lastPTPC.updateTextSize(lastPTPC.getCharactersCovered() + 1);
- lastRTPC.updateTextSize(lastRTPC.getCharactersCovered() + 1);
-
- /**
- * If TextSpecInfoAtom is present, we must update the text size in it,
- * otherwise the ppt will be corrupted
- */
- for (Record r : paragraphs.get(0).getRecords()) {
- if (r instanceof TextSpecInfoAtom) {
- ((TextSpecInfoAtom) r).setParentSize(rawText.length() + 1);
- break;
- }
- }
-
- if (_txtbox instanceof EscherTextboxWrapper) {
- try {
- ((EscherTextboxWrapper) _txtbox).writeOut(null);
- } catch (IOException e) {
- throw new RuntimeException("failed dummy write", e);
- }
- }
-
- for (HSLFTextParagraph p : paragraphs) {
- p._dirty = false;
- }
- }
-
- /**
- * Adds the supplied text onto the end of the TextParagraphs,
- * creating a new RichTextRun for it to sit in.
- *
- * @param text the text string used by this object.
- */
- protected static HSLFTextRun appendText(List<HSLFTextParagraph> paragraphs, String text, boolean newParagraph) {
- text = toInternalString(text);
-
- // check paragraphs
- assert(!paragraphs.isEmpty() && !paragraphs.get(0).getTextRuns().isEmpty());
-
- HSLFTextParagraph htp = paragraphs.get(paragraphs.size() - 1);
- HSLFTextRun htr = htp.getTextRuns().get(htp.getTextRuns().size() - 1);
-
- boolean addParagraph = newParagraph;
- for (String rawText : text.split("(?<=\r)")) {
- // special case, if last text paragraph or run is empty, we will reuse it
- boolean lastRunEmpty = (htr.getLength() == 0);
- boolean lastParaEmpty = lastRunEmpty && (htp.getTextRuns().size() == 1);
-
- if (addParagraph && !lastParaEmpty) {
- TextPropCollection tpc = htp.getParagraphStyle();
- HSLFTextParagraph prevHtp = htp;
- htp = new HSLFTextParagraph(htp._headerAtom, htp._byteAtom, htp._charAtom);
- htp.getParagraphStyle().copy(tpc);
- htp.setParentShape(prevHtp.getParentShape());
- htp.setShapeId(prevHtp.getShapeId());
- htp.supplySheet(prevHtp.getSheet());
- paragraphs.add(htp);
- }
- addParagraph = true;
-
- if (!lastRunEmpty) {
- TextPropCollection tpc = htr.getCharacterStyle();
- htr = new HSLFTextRun(htp);
- htr.getCharacterStyle().copy(tpc);
- htp.addTextRun(htr);
- }
- htr.setText(rawText);
- }
-
- storeText(paragraphs);
-
- return htr;
- }
-
- /**
- * Sets (overwrites) the current text.
- * Uses the properties of the first paragraph / textrun
- *
- * @param text the text string used by this object.
- */
- public static HSLFTextRun setText(List<HSLFTextParagraph> paragraphs, String text) {
- // check paragraphs
- assert(!paragraphs.isEmpty() && !paragraphs.get(0).getTextRuns().isEmpty());
-
- Iterator<HSLFTextParagraph> paraIter = paragraphs.iterator();
- HSLFTextParagraph htp = paraIter.next(); // keep first
- assert (htp != null);
- while (paraIter.hasNext()) {
- paraIter.next();
- paraIter.remove();
- }
-
- Iterator<HSLFTextRun> runIter = htp.getTextRuns().iterator();
- if (runIter.hasNext()) {
- HSLFTextRun htr = runIter.next();
- htr.setText("");
- while (runIter.hasNext()) {
- runIter.next();
- runIter.remove();
- }
- } else {
- HSLFTextRun trun = new HSLFTextRun(htp);
- htp.addTextRun(trun);
- }
-
- return appendText(paragraphs, text, false);
- }
-
- public static String getText(List<HSLFTextParagraph> paragraphs) {
- assert (!paragraphs.isEmpty());
- String rawText = getRawText(paragraphs);
- return toExternalString(rawText, paragraphs.get(0).getRunType());
- }
-
- public static String getRawText(List<HSLFTextParagraph> paragraphs) {
- StringBuilder sb = new StringBuilder();
- for (HSLFTextParagraph p : paragraphs) {
- for (HSLFTextRun r : p.getTextRuns()) {
- sb.append(r.getRawText());
- }
- }
- return sb.toString();
- }
-
- @Override
- public String toString() {
- StringBuilder sb = new StringBuilder();
- for (HSLFTextRun r : getTextRuns()) {
- sb.append(r.getRawText());
- }
- return toExternalString(sb.toString(), getRunType());
- }
-
- /**
- * Returns a new string with line breaks converted into internal ppt
- * representation
- */
- protected static String toInternalString(String s) {
- String ns = s.replaceAll("\\r?\\n", "\r");
- return ns;
- }
-
- /**
- * Converts raw text from the text paragraphs to a formatted string,
- * i.e. it converts certain control characters used in the raw txt
- *
- * @param rawText the raw text
- * @param runType the run type of the shape, paragraph or headerAtom.
- * use -1 if unknown
- * @return the formatted string
- */
- public static String toExternalString(String rawText, int runType) {
- // PowerPoint seems to store files with \r as the line break
- // The messes things up on everything but a Mac, so translate
- // them to \n
- String text = rawText.replace('\r', '\n');
-
- switch (runType) {
- // 0xB acts like cariage return in page titles and like blank in the
- // others
- case -1:
- case org.apache.poi.hslf.record.TextHeaderAtom.TITLE_TYPE:
- case org.apache.poi.hslf.record.TextHeaderAtom.CENTER_TITLE_TYPE:
- text = text.replace((char) 0x0B, '\n');
- break;
- default:
- text = text.replace((char) 0x0B, ' ');
- break;
- }
-
- return text;
- }
-
- /**
- * For a given PPDrawing, grab all the TextRuns
- */
- public static List<List<HSLFTextParagraph>> findTextParagraphs(PPDrawing ppdrawing, HSLFSheet sheet) {
- List<List<HSLFTextParagraph>> runsV = new ArrayList<List<HSLFTextParagraph>>();
- for (EscherTextboxWrapper wrapper : ppdrawing.getTextboxWrappers()) {
- List<HSLFTextParagraph> p = findTextParagraphs(wrapper, sheet);
- if (p != null) runsV.add(p);
- }
- return runsV;
- }
-
- /**
- * Scans through the supplied record array, looking for
- * a TextHeaderAtom followed by one of a TextBytesAtom or
- * a TextCharsAtom. Builds up TextRuns from these
- *
- * @param wrapper an EscherTextboxWrapper
- */
- protected static List<HSLFTextParagraph> findTextParagraphs(EscherTextboxWrapper wrapper, HSLFSheet sheet) {
- // propagate parents to parent-aware records
- RecordContainer.handleParentAwareRecords(wrapper);
- int shapeId = wrapper.getShapeId();
- List<HSLFTextParagraph> rv = null;
-
- OutlineTextRefAtom ota = (OutlineTextRefAtom)wrapper.findFirstOfType(OutlineTextRefAtom.typeID);
- if (ota != null) {
- // if we are based on an outline, there are no further records to be parsed from the wrapper
- if (sheet == null) {
- throw new RuntimeException("Outline atom reference can't be solved without a sheet record");
- }
-
- List<List<HSLFTextParagraph>> sheetRuns = sheet.getTextParagraphs();
- assert (sheetRuns != null);
-
- int idx = ota.getTextIndex();
- for (List<HSLFTextParagraph> r : sheetRuns) {
- if (r.isEmpty()) continue;
- int ridx = r.get(0).getIndex();
- if (ridx > idx) break;
- if (ridx == idx) {
- if (rv == null) {
- rv = r;
- } else {
- // create a new container
- // TODO: ... is this case really happening?
- rv = new ArrayList<HSLFTextParagraph>(rv);
- rv.addAll(r);
- }
- }
- }
- if (rv == null || rv.isEmpty()) {
- logger.log(POILogger.WARN, "text run not found for OutlineTextRefAtom.TextIndex=" + idx);
- }
- } else {
- if (sheet != null) {
- // check sheet runs first, so we get exactly the same paragraph list
- List<List<HSLFTextParagraph>> sheetRuns = sheet.getTextParagraphs();
- assert (sheetRuns != null);
-
- for (List<HSLFTextParagraph> paras : sheetRuns) {
- if (!paras.isEmpty() && paras.get(0)._headerAtom.getParentRecord() == wrapper) {
- rv = paras;
- break;
- }
- }
- }
-
- if (rv == null) {
- // if we haven't found the wrapper in the sheet runs, create a new paragraph list from its record
- List<List<HSLFTextParagraph>> rvl = findTextParagraphs(wrapper.getChildRecords());
- switch (rvl.size()) {
- case 0: break; // nothing found
- case 1: rv = rvl.get(0); break; // normal case
- default:
- throw new RuntimeException("TextBox contains more than one list of paragraphs.");
- }
- }
- }
-
- if (rv != null) {
- StyleTextProp9Atom styleTextProp9Atom = wrapper.getStyleTextProp9Atom();
-
- for (HSLFTextParagraph htp : rv) {
- htp.setShapeId(shapeId);
- htp.setStyleTextProp9Atom(styleTextProp9Atom);
- }
- }
- return rv;
- }
-
- /**
- * Scans through the supplied record array, looking for
- * a TextHeaderAtom followed by one of a TextBytesAtom or
- * a TextCharsAtom. Builds up TextRuns from these
- *
- * @param records the records to build from
- */
- protected static List<List<HSLFTextParagraph>> findTextParagraphs(Record[] records) {
- List<List<HSLFTextParagraph>> paragraphCollection = new ArrayList<List<HSLFTextParagraph>>();
-
- int[] recordIdx = { 0 };
-
- for (int slwtIndex = 0; recordIdx[0] < records.length; slwtIndex++) {
- TextHeaderAtom header = null;
- TextBytesAtom tbytes = null;
- TextCharsAtom tchars = null;
- TextRulerAtom ruler = null;
- MasterTextPropAtom indents = null;
-
- for (Record r : getRecords(records, recordIdx, null)) {
- long rt = r.getRecordType();
- if (RecordTypes.TextHeaderAtom.typeID == rt) {
- header = (TextHeaderAtom) r;
- } else if (RecordTypes.TextBytesAtom.typeID == rt) {
- tbytes = (TextBytesAtom) r;
- } else if (RecordTypes.TextCharsAtom.typeID == rt) {
- tchars = (TextCharsAtom) r;
- } else if (RecordTypes.TextRulerAtom.typeID == rt) {
- ruler = (TextRulerAtom) r;
- } else if (RecordTypes.MasterTextPropAtom.typeID == rt) {
- indents = (MasterTextPropAtom) r;
- }
- // don't search for RecordTypes.StyleTextPropAtom.typeID here ... see findStyleAtomPresent below
- }
-
- if (header == null) break;
-
- if (header.getParentRecord() instanceof SlideListWithText) {
- // runs found in PPDrawing are not linked with SlideListWithTexts
- header.setIndex(slwtIndex);
- }
-
- if (tbytes == null && tchars == null) {
- tbytes = new TextBytesAtom();
- // don't add record yet - set it in storeText
- logger.log(POILogger.INFO, "bytes nor chars atom doesn't exist. Creating dummy record for later saving.");
- }
-
- String rawText = (tchars != null) ? tchars.getText() : tbytes.getText();
- StyleTextPropAtom styles = findStyleAtomPresent(header, rawText.length());
-
- List<HSLFTextParagraph> paragraphs = new ArrayList<HSLFTextParagraph>();
- paragraphCollection.add(paragraphs);
-
- // split, but keep delimiter
- for (String para : rawText.split("(?<=\r)")) {
- HSLFTextParagraph tpara = new HSLFTextParagraph(header, tbytes, tchars);
- paragraphs.add(tpara);
- tpara._ruler = ruler;
- tpara.getParagraphStyle().updateTextSize(para.length());
-
- HSLFTextRun trun = new HSLFTextRun(tpara);
- tpara.addTextRun(trun);
- trun.setText(para);
- }
-
- applyCharacterStyles(paragraphs, styles.getCharacterStyles());
- applyParagraphStyles(paragraphs, styles.getParagraphStyles());
- if (indents != null) {
- applyParagraphIndents(paragraphs, indents.getIndents());
- }
- }
-
- if (paragraphCollection.isEmpty()) {
- logger.log(POILogger.DEBUG, "No text records found.");
- }
-
- return paragraphCollection;
- }
-
- protected static void applyCharacterStyles(List<HSLFTextParagraph> paragraphs, List<TextPropCollection> charStyles) {
- int paraIdx = 0, runIdx = 0;
- HSLFTextRun trun;
-
- for (int csIdx = 0; csIdx < charStyles.size(); csIdx++) {
- TextPropCollection p = charStyles.get(csIdx);
- for (int ccRun = 0, ccStyle = p.getCharactersCovered(); ccRun < ccStyle;) {
- HSLFTextParagraph para = paragraphs.get(paraIdx);
- List<HSLFTextRun> runs = para.getTextRuns();
- trun = runs.get(runIdx);
- final int len = trun.getLength();
-
- if (ccRun + len <= ccStyle) {
- ccRun += len;
- } else {
- String text = trun.getRawText();
- trun.setText(text.substring(0, ccStyle - ccRun));
-
- HSLFTextRun nextRun = new HSLFTextRun(para);
- nextRun.setText(text.substring(ccStyle - ccRun));
- runs.add(runIdx + 1, nextRun);
-
- ccRun += ccStyle - ccRun;
- }
-
- trun.setCharacterStyle(p);
-
- if (paraIdx == paragraphs.size()-1 && runIdx == runs.size()-1) {
- if (csIdx < charStyles.size() - 1) {
- // special case, empty trailing text run
- HSLFTextRun nextRun = new HSLFTextRun(para);
- nextRun.setText("");
- runs.add(nextRun);
- ccRun++;
- } else {
- // need to add +1 to the last run of the last paragraph
- trun.getCharacterStyle().updateTextSize(trun.getLength()+1);
- ccRun++;
- }
- }
-
- // need to compare it again, in case a run has been added after
- if (++runIdx == runs.size()) {
- paraIdx++;
- runIdx = 0;
- }
- }
- }
- }
-
- protected static void applyParagraphStyles(List<HSLFTextParagraph> paragraphs, List<TextPropCollection> paraStyles) {
- int paraIdx = 0;
- for (TextPropCollection p : paraStyles) {
- for (int ccPara = 0, ccStyle = p.getCharactersCovered(); ccPara < ccStyle; paraIdx++) {
- if (paraIdx >= paragraphs.size()) return;
- HSLFTextParagraph htp = paragraphs.get(paraIdx);
- TextPropCollection pCopy = new TextPropCollection(0, TextPropType.paragraph);
- pCopy.copy(p);
- htp.setParagraphStyle(pCopy);
- int len = 0;
- for (HSLFTextRun trun : htp.getTextRuns()) {
- len += trun.getLength();
- }
- if (paraIdx == paragraphs.size()-1) len++;
- pCopy.updateTextSize(len);
- ccPara += len;
- }
- }
- }
-
- protected static void applyParagraphIndents(List<HSLFTextParagraph> paragraphs, List<IndentProp> paraStyles) {
- int paraIdx = 0;
- for (IndentProp p : paraStyles) {
- for (int ccPara = 0, ccStyle = p.getCharactersCovered(); ccPara < ccStyle; paraIdx++) {
- if (paraIdx >= paragraphs.size() || ccPara >= ccStyle-1) return;
- HSLFTextParagraph para = paragraphs.get(paraIdx);
- int len = 0;
- for (HSLFTextRun trun : para.getTextRuns()) {
- len += trun.getLength();
- }
- para.setIndentLevel(p.getIndentLevel());
- ccPara += len + 1;
- }
- }
- }
-
- protected static List<HSLFTextParagraph> createEmptyParagraph() {
- EscherTextboxWrapper wrapper = new EscherTextboxWrapper();
- return createEmptyParagraph(wrapper);
- }
-
- protected static List<HSLFTextParagraph> createEmptyParagraph(EscherTextboxWrapper wrapper) {
- TextHeaderAtom tha = new TextHeaderAtom();
- tha.setParentRecord(wrapper);
- wrapper.appendChildRecord(tha);
-
- TextBytesAtom tba = new TextBytesAtom();
- tba.setText("".getBytes(LocaleUtil.CHARSET_1252));
- wrapper.appendChildRecord(tba);
-
- StyleTextPropAtom sta = new StyleTextPropAtom(1);
- TextPropCollection paraStyle = sta.addParagraphTextPropCollection(1);
- TextPropCollection charStyle = sta.addCharacterTextPropCollection(1);
- wrapper.appendChildRecord(sta);
-
- HSLFTextParagraph htp = new HSLFTextParagraph(tha, tba, null);
- htp.setParagraphStyle(paraStyle);
-
- HSLFTextRun htr = new HSLFTextRun(htp);
- htr.setCharacterStyle(charStyle);
- htr.setText("");
- htp.addTextRun(htr);
-
- return Arrays.asList(htp);
- }
-
- public EscherTextboxWrapper getTextboxWrapper() {
- return (EscherTextboxWrapper) _headerAtom.getParentRecord();
- }
-
- protected static Color getColorFromColorIndexStruct(int rgb, HSLFSheet sheet) {
- int cidx = rgb >>> 24;
- Color tmp;
- switch (cidx) {
- // Background ... Accent 3 color
- case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
- if (sheet == null) return null;
- ColorSchemeAtom ca = sheet.getColorScheme();
- tmp = new Color(ca.getColor(cidx), true);
- break;
- // Color is an sRGB value specified by red, green, and blue fields.
- case 0xFE:
- tmp = new Color(rgb, true);
- break;
- // Color is undefined.
- default:
- case 0xFF:
- return null;
- }
- 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;
- }
- }
|