You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

HSLFTextParagraph.java 53KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.hslf.usermodel;
  16. import static org.apache.poi.hslf.record.RecordTypes.OutlineTextRefAtom;
  17. import java.awt.Color;
  18. import java.io.IOException;
  19. import java.util.ArrayList;
  20. import java.util.Arrays;
  21. import java.util.Iterator;
  22. import java.util.List;
  23. import org.apache.poi.hslf.exceptions.HSLFException;
  24. import org.apache.poi.hslf.model.PPFont;
  25. import org.apache.poi.hslf.model.textproperties.BitMaskTextProp;
  26. import org.apache.poi.hslf.model.textproperties.FontAlignmentProp;
  27. import org.apache.poi.hslf.model.textproperties.IndentProp;
  28. import org.apache.poi.hslf.model.textproperties.ParagraphFlagsTextProp;
  29. import org.apache.poi.hslf.model.textproperties.TextAlignmentProp;
  30. import org.apache.poi.hslf.model.textproperties.TextPFException9;
  31. import org.apache.poi.hslf.model.textproperties.TextProp;
  32. import org.apache.poi.hslf.model.textproperties.TextPropCollection;
  33. import org.apache.poi.hslf.model.textproperties.TextPropCollection.TextPropType;
  34. import org.apache.poi.hslf.record.ColorSchemeAtom;
  35. import org.apache.poi.hslf.record.EscherTextboxWrapper;
  36. import org.apache.poi.hslf.record.FontCollection;
  37. import org.apache.poi.hslf.record.MasterTextPropAtom;
  38. import org.apache.poi.hslf.record.OutlineTextRefAtom;
  39. import org.apache.poi.hslf.record.PPDrawing;
  40. import org.apache.poi.hslf.record.Record;
  41. import org.apache.poi.hslf.record.RecordContainer;
  42. import org.apache.poi.hslf.record.RecordTypes;
  43. import org.apache.poi.hslf.record.SlideListWithText;
  44. import org.apache.poi.hslf.record.SlidePersistAtom;
  45. import org.apache.poi.hslf.record.StyleTextProp9Atom;
  46. import org.apache.poi.hslf.record.StyleTextPropAtom;
  47. import org.apache.poi.hslf.record.TextBytesAtom;
  48. import org.apache.poi.hslf.record.TextCharsAtom;
  49. import org.apache.poi.hslf.record.TextHeaderAtom;
  50. import org.apache.poi.hslf.record.TextRulerAtom;
  51. import org.apache.poi.hslf.record.TextSpecInfoAtom;
  52. import org.apache.poi.sl.draw.DrawPaint;
  53. import org.apache.poi.sl.usermodel.AutoNumberingScheme;
  54. import org.apache.poi.sl.usermodel.PaintStyle;
  55. import org.apache.poi.sl.usermodel.PaintStyle.SolidPaint;
  56. import org.apache.poi.sl.usermodel.TextParagraph;
  57. import org.apache.poi.util.Internal;
  58. import org.apache.poi.util.LocaleUtil;
  59. import org.apache.poi.util.POILogFactory;
  60. import org.apache.poi.util.POILogger;
  61. import org.apache.poi.util.StringUtil;
  62. import org.apache.poi.util.Units;
  63. /**
  64. * This class represents a run of text in a powerpoint document. That
  65. * run could be text on a sheet, or text in a note.
  66. * It is only a very basic class for now
  67. *
  68. * @author Nick Burch
  69. */
  70. public final class HSLFTextParagraph implements TextParagraph<HSLFShape,HSLFTextParagraph,HSLFTextRun> {
  71. protected static final POILogger logger = POILogFactory.getLogger(HSLFTextParagraph.class);
  72. /**
  73. * How to align the text
  74. */
  75. /* package */static final int AlignLeft = 0;
  76. /* package */static final int AlignCenter = 1;
  77. /* package */static final int AlignRight = 2;
  78. /* package */static final int AlignJustify = 3;
  79. // Note: These fields are protected to help with unit testing
  80. // Other classes shouldn't really go playing with them!
  81. private final TextHeaderAtom _headerAtom;
  82. private TextBytesAtom _byteAtom;
  83. private TextCharsAtom _charAtom;
  84. private TextPropCollection _paragraphStyle = new TextPropCollection(1, TextPropType.paragraph);
  85. protected TextRulerAtom _ruler;
  86. protected final List<HSLFTextRun> _runs = new ArrayList<HSLFTextRun>();
  87. protected HSLFTextShape _parentShape;
  88. private HSLFSheet _sheet;
  89. private int shapeId;
  90. private StyleTextProp9Atom styleTextProp9Atom;
  91. private boolean _dirty = false;
  92. private final List<HSLFTextParagraph> parentList;
  93. /**
  94. * Constructs a Text Run from a Unicode text block.
  95. * Either a {@link TextCharsAtom} or a {@link TextBytesAtom} needs to be provided.
  96. *
  97. * @param tha the TextHeaderAtom that defines what's what
  98. * @param tba the TextBytesAtom containing the text or null if {@link TextCharsAtom} is provided
  99. * @param tca the TextCharsAtom containing the text or null if {@link TextBytesAtom} is provided
  100. * @param parentList the list which contains this paragraph
  101. */
  102. /* package */ HSLFTextParagraph(
  103. TextHeaderAtom tha,
  104. TextBytesAtom tba,
  105. TextCharsAtom tca,
  106. List<HSLFTextParagraph> parentList
  107. ) {
  108. if (tha == null) {
  109. throw new IllegalArgumentException("TextHeaderAtom must be set.");
  110. }
  111. _headerAtom = tha;
  112. _byteAtom = tba;
  113. _charAtom = tca;
  114. this.parentList = parentList;
  115. }
  116. /* package */HSLFTextParagraph(HSLFTextParagraph other) {
  117. _headerAtom = other._headerAtom;
  118. _byteAtom = other._byteAtom;
  119. _charAtom = other._charAtom;
  120. _parentShape = other._parentShape;
  121. _sheet = other._sheet;
  122. _ruler = other._ruler;
  123. shapeId = other.shapeId;
  124. _paragraphStyle.copy(other._paragraphStyle);
  125. parentList = other.parentList;
  126. }
  127. public void addTextRun(HSLFTextRun run) {
  128. _runs.add(run);
  129. }
  130. @Override
  131. public List<HSLFTextRun> getTextRuns() {
  132. return _runs;
  133. }
  134. public TextPropCollection getParagraphStyle() {
  135. return _paragraphStyle;
  136. }
  137. public void setParagraphStyle(TextPropCollection paragraphStyle) {
  138. _paragraphStyle.copy(paragraphStyle);
  139. }
  140. /**
  141. * Setting a master style reference
  142. *
  143. * @param paragraphStyle the master style reference
  144. *
  145. * @since POI 3.14-Beta1
  146. */
  147. @Internal
  148. /* package */ void setMasterStyleReference(TextPropCollection paragraphStyle) {
  149. _paragraphStyle = paragraphStyle;
  150. }
  151. /**
  152. * Supply the Sheet we belong to, which might have an assigned SlideShow
  153. * Also passes it on to our child RichTextRuns
  154. */
  155. public static void supplySheet(List<HSLFTextParagraph> paragraphs, HSLFSheet sheet) {
  156. if (paragraphs == null) {
  157. return;
  158. }
  159. for (HSLFTextParagraph p : paragraphs) {
  160. p.supplySheet(sheet);
  161. }
  162. assert(sheet.getSlideShow() != null);
  163. applyHyperlinks(paragraphs);
  164. }
  165. /**
  166. * Supply the Sheet we belong to, which might have an assigned SlideShow
  167. * Also passes it on to our child RichTextRuns
  168. */
  169. private void supplySheet(HSLFSheet sheet) {
  170. this._sheet = sheet;
  171. if (_runs == null) return;
  172. for (HSLFTextRun rt : _runs) {
  173. rt.updateSheet();
  174. }
  175. }
  176. public HSLFSheet getSheet() {
  177. return this._sheet;
  178. }
  179. /**
  180. * @return Shape ID
  181. */
  182. protected int getShapeId() {
  183. return shapeId;
  184. }
  185. /**
  186. * @param id Shape ID
  187. */
  188. protected void setShapeId(int id) {
  189. shapeId = id;
  190. }
  191. /**
  192. * @return 0-based index of the text run in the SLWT container
  193. */
  194. protected int getIndex() {
  195. return (_headerAtom != null) ? _headerAtom.getIndex() : -1;
  196. }
  197. /**
  198. * Sets the index of the paragraph in the SLWT container
  199. *
  200. * @param index
  201. */
  202. protected void setIndex(int index) {
  203. if (_headerAtom != null) _headerAtom.setIndex(index);
  204. }
  205. /**
  206. * Returns the type of the text, from the TextHeaderAtom.
  207. * Possible values can be seen from TextHeaderAtom
  208. * @see org.apache.poi.hslf.record.TextHeaderAtom
  209. */
  210. public int getRunType() {
  211. return (_headerAtom != null) ? _headerAtom.getTextType() : -1;
  212. }
  213. public void setRunType(int runType) {
  214. if (_headerAtom != null) _headerAtom.setTextType(runType);
  215. }
  216. /**
  217. * Is this Text Run one from a {@link PPDrawing}, or is it
  218. * one from the {@link SlideListWithText}?
  219. */
  220. public boolean isDrawingBased() {
  221. return (getIndex() == -1);
  222. }
  223. public TextRulerAtom getTextRuler() {
  224. return _ruler;
  225. }
  226. public TextRulerAtom createTextRuler() {
  227. _ruler = getTextRuler();
  228. if (_ruler == null) {
  229. _ruler = TextRulerAtom.getParagraphInstance();
  230. Record childAfter = _byteAtom;
  231. if (childAfter == null) childAfter = _charAtom;
  232. if (childAfter == null) childAfter = _headerAtom;
  233. _headerAtom.getParentRecord().addChildAfter(_ruler, childAfter);
  234. }
  235. return _ruler;
  236. }
  237. /**
  238. * Returns records that make up the list of text paragraphs
  239. * (there can be misc InteractiveInfo, TxInteractiveInfo and other records)
  240. *
  241. * @return text run records
  242. */
  243. public Record[] getRecords() {
  244. Record r[] = _headerAtom.getParentRecord().getChildRecords();
  245. return getRecords(r, new int[] { 0 }, _headerAtom);
  246. }
  247. private static Record[] getRecords(Record[] records, int[] startIdx, TextHeaderAtom headerAtom) {
  248. if (records == null) {
  249. throw new NullPointerException("records need to be set.");
  250. }
  251. for (; startIdx[0] < records.length; startIdx[0]++) {
  252. Record r = records[startIdx[0]];
  253. if (r instanceof TextHeaderAtom && (headerAtom == null || r == headerAtom)) break;
  254. }
  255. if (startIdx[0] >= records.length) {
  256. logger.log(POILogger.INFO, "header atom wasn't found - container might contain only an OutlineTextRefAtom");
  257. return new Record[0];
  258. }
  259. int length;
  260. for (length = 1; startIdx[0] + length < records.length; length++) {
  261. Record r = records[startIdx[0]+length];
  262. if (r instanceof TextHeaderAtom || r instanceof SlidePersistAtom) break;
  263. }
  264. Record result[] = new Record[length];
  265. System.arraycopy(records, startIdx[0], result, 0, length);
  266. startIdx[0] += length;
  267. return result;
  268. }
  269. /** Numbered List info */
  270. public void setStyleTextProp9Atom(final StyleTextProp9Atom styleTextProp9Atom) {
  271. this.styleTextProp9Atom = styleTextProp9Atom;
  272. }
  273. /** Numbered List info */
  274. public StyleTextProp9Atom getStyleTextProp9Atom() {
  275. return this.styleTextProp9Atom;
  276. }
  277. @Override
  278. public Iterator<HSLFTextRun> iterator() {
  279. return _runs.iterator();
  280. }
  281. @Override
  282. public Double getLeftMargin() {
  283. TextProp val = getPropVal(_paragraphStyle, "text.offset", this);
  284. return (val == null) ? null : Units.masterToPoints(val.getValue());
  285. }
  286. @Override
  287. public void setLeftMargin(Double leftMargin) {
  288. Integer val = (leftMargin == null) ? null : Units.pointsToMaster(leftMargin);
  289. setParagraphTextPropVal("text.offset", val);
  290. }
  291. @Override
  292. public Double getRightMargin() {
  293. // TODO: find out, how to determine this value
  294. return null;
  295. }
  296. @Override
  297. public void setRightMargin(Double rightMargin) {
  298. // TODO: find out, how to set this value
  299. }
  300. @Override
  301. public Double getIndent() {
  302. TextProp val = getPropVal(_paragraphStyle, "bullet.offset", this);
  303. return (val == null) ? null : Units.masterToPoints(val.getValue());
  304. }
  305. @Override
  306. public void setIndent(Double indent) {
  307. Integer val = (indent == null) ? null : Units.pointsToMaster(indent);
  308. setParagraphTextPropVal("bullet.offset", val);
  309. }
  310. @Override
  311. public String getDefaultFontFamily() {
  312. String typeface = null;
  313. if (!_runs.isEmpty()) {
  314. typeface = _runs.get(0).getFontFamily();
  315. }
  316. return (typeface != null) ? typeface : "Arial";
  317. }
  318. @Override
  319. public Double getDefaultFontSize() {
  320. Double d = null;
  321. if (!_runs.isEmpty()) {
  322. d = _runs.get(0).getFontSize();
  323. }
  324. return (d != null) ? d : 12d;
  325. }
  326. @Override
  327. public void setTextAlign(TextAlign align) {
  328. Integer alignInt = null;
  329. if (align != null) switch (align) {
  330. default:
  331. case LEFT: alignInt = TextAlignmentProp.LEFT;break;
  332. case CENTER: alignInt = TextAlignmentProp.CENTER; break;
  333. case RIGHT: alignInt = TextAlignmentProp.RIGHT; break;
  334. case DIST: alignInt = TextAlignmentProp.DISTRIBUTED; break;
  335. case JUSTIFY: alignInt = TextAlignmentProp.JUSTIFY; break;
  336. case JUSTIFY_LOW: alignInt = TextAlignmentProp.JUSTIFYLOW; break;
  337. case THAI_DIST: alignInt = TextAlignmentProp.THAIDISTRIBUTED; break;
  338. }
  339. setParagraphTextPropVal("alignment", alignInt);
  340. }
  341. @Override
  342. public TextAlign getTextAlign() {
  343. TextProp tp = getPropVal(_paragraphStyle, "alignment", this);
  344. if (tp == null) return null;
  345. switch (tp.getValue()) {
  346. default:
  347. case TextAlignmentProp.LEFT: return TextAlign.LEFT;
  348. case TextAlignmentProp.CENTER: return TextAlign.CENTER;
  349. case TextAlignmentProp.RIGHT: return TextAlign.RIGHT;
  350. case TextAlignmentProp.JUSTIFY: return TextAlign.JUSTIFY;
  351. case TextAlignmentProp.JUSTIFYLOW: return TextAlign.JUSTIFY_LOW;
  352. case TextAlignmentProp.DISTRIBUTED: return TextAlign.DIST;
  353. case TextAlignmentProp.THAIDISTRIBUTED: return TextAlign.THAI_DIST;
  354. }
  355. }
  356. @Override
  357. public FontAlign getFontAlign() {
  358. TextProp tp = getPropVal(_paragraphStyle, FontAlignmentProp.NAME, this);
  359. if (tp == null) return null;
  360. switch (tp.getValue()) {
  361. case FontAlignmentProp.BASELINE: return FontAlign.BASELINE;
  362. case FontAlignmentProp.TOP: return FontAlign.TOP;
  363. case FontAlignmentProp.CENTER: return FontAlign.CENTER;
  364. case FontAlignmentProp.BOTTOM: return FontAlign.BOTTOM;
  365. default: return FontAlign.AUTO;
  366. }
  367. }
  368. public AutoNumberingScheme getAutoNumberingScheme() {
  369. if (styleTextProp9Atom == null) return null;
  370. TextPFException9[] ant = styleTextProp9Atom.getAutoNumberTypes();
  371. int level = getIndentLevel();
  372. if (ant == null || level == -1 || level >= ant.length) return null;
  373. return ant[level].getAutoNumberScheme();
  374. }
  375. public Integer getAutoNumberingStartAt() {
  376. if (styleTextProp9Atom == null) return null;
  377. TextPFException9[] ant = styleTextProp9Atom.getAutoNumberTypes();
  378. int level = getIndentLevel();
  379. if (ant == null || level >= ant.length) return null;
  380. Short startAt = ant[level].getAutoNumberStartNumber();
  381. assert(startAt != null);
  382. return startAt.intValue();
  383. }
  384. @Override
  385. public BulletStyle getBulletStyle() {
  386. if (!isBullet() && getAutoNumberingScheme() == null) return null;
  387. return new BulletStyle() {
  388. @Override
  389. public String getBulletCharacter() {
  390. Character chr = HSLFTextParagraph.this.getBulletChar();
  391. return (chr == null || chr == 0) ? "" : "" + chr;
  392. }
  393. @Override
  394. public String getBulletFont() {
  395. return HSLFTextParagraph.this.getBulletFont();
  396. }
  397. @Override
  398. public Double getBulletFontSize() {
  399. return HSLFTextParagraph.this.getBulletSize();
  400. }
  401. @Override
  402. public void setBulletFontColor(Color color) {
  403. setBulletFontColor(DrawPaint.createSolidPaint(color));
  404. }
  405. @Override
  406. public void setBulletFontColor(PaintStyle color) {
  407. if (!(color instanceof SolidPaint)) {
  408. throw new IllegalArgumentException("HSLF only supports SolidPaint");
  409. }
  410. SolidPaint sp = (SolidPaint)color;
  411. Color col = DrawPaint.applyColorTransform(sp.getSolidColor());
  412. HSLFTextParagraph.this.setBulletColor(col);
  413. }
  414. @Override
  415. public PaintStyle getBulletFontColor() {
  416. Color col = HSLFTextParagraph.this.getBulletColor();
  417. return DrawPaint.createSolidPaint(col);
  418. }
  419. @Override
  420. public AutoNumberingScheme getAutoNumberingScheme() {
  421. return HSLFTextParagraph.this.getAutoNumberingScheme();
  422. }
  423. @Override
  424. public Integer getAutoNumberingStartAt() {
  425. return HSLFTextParagraph.this.getAutoNumberingStartAt();
  426. }
  427. };
  428. }
  429. @Override
  430. public void setBulletStyle(Object... styles) {
  431. if (styles.length == 0) {
  432. setBullet(false);
  433. } else {
  434. setBullet(true);
  435. for (Object ostyle : styles) {
  436. if (ostyle instanceof Number) {
  437. setBulletSize(((Number)ostyle).doubleValue());
  438. } else if (ostyle instanceof Color) {
  439. setBulletColor((Color)ostyle);
  440. } else if (ostyle instanceof Character) {
  441. setBulletChar((Character)ostyle);
  442. } else if (ostyle instanceof String) {
  443. setBulletFont((String)ostyle);
  444. } else if (ostyle instanceof AutoNumberingScheme) {
  445. throw new HSLFException("setting bullet auto-numberin scheme for HSLF not supported ... yet");
  446. }
  447. }
  448. }
  449. }
  450. @Override
  451. public HSLFTextShape getParentShape() {
  452. return _parentShape;
  453. }
  454. public void setParentShape(HSLFTextShape parentShape) {
  455. _parentShape = parentShape;
  456. }
  457. @Override
  458. public int getIndentLevel() {
  459. return _paragraphStyle == null ? 0 : _paragraphStyle.getIndentLevel();
  460. }
  461. @Override
  462. public void setIndentLevel(int level) {
  463. if( _paragraphStyle != null ) _paragraphStyle.setIndentLevel((short)level);
  464. }
  465. /**
  466. * Sets whether this rich text run has bullets
  467. */
  468. public void setBullet(boolean flag) {
  469. setFlag(ParagraphFlagsTextProp.BULLET_IDX, flag);
  470. }
  471. /**
  472. * Returns whether this rich text run has bullets
  473. */
  474. public boolean isBullet() {
  475. return getFlag(ParagraphFlagsTextProp.BULLET_IDX);
  476. }
  477. /**
  478. * Sets the bullet character
  479. */
  480. public void setBulletChar(Character c) {
  481. Integer val = (c == null) ? null : (int)c.charValue();
  482. setParagraphTextPropVal("bullet.char", val);
  483. }
  484. /**
  485. * Returns the bullet character
  486. */
  487. public Character getBulletChar() {
  488. TextProp tp = getPropVal(_paragraphStyle, "bullet.char", this);
  489. return (tp == null) ? null : (char)tp.getValue();
  490. }
  491. /**
  492. * Sets the bullet size
  493. */
  494. public void setBulletSize(Double size) {
  495. setPctOrPoints("bullet.size", size);
  496. }
  497. /**
  498. * Returns the bullet size, null if unset
  499. */
  500. public Double getBulletSize() {
  501. return getPctOrPoints("bullet.size");
  502. }
  503. /**
  504. * Sets the bullet color
  505. */
  506. public void setBulletColor(Color color) {
  507. Integer val = (color == null) ? null : new Color(color.getBlue(), color.getGreen(), color.getRed(), 254).getRGB();
  508. setParagraphTextPropVal("bullet.color", val);
  509. setFlag(ParagraphFlagsTextProp.BULLET_HARDCOLOR_IDX, (color != null));
  510. }
  511. /**
  512. * Returns the bullet color
  513. */
  514. public Color getBulletColor() {
  515. TextProp tp = getPropVal(_paragraphStyle, "bullet.color", this);
  516. boolean hasColor = getFlag(ParagraphFlagsTextProp.BULLET_HARDCOLOR_IDX);
  517. if (tp == null || !hasColor) {
  518. // if bullet color is undefined, return color of first run
  519. if (_runs.isEmpty()) {
  520. return null;
  521. }
  522. SolidPaint sp = _runs.get(0).getFontColor();
  523. if(sp == null) {
  524. return null;
  525. }
  526. return DrawPaint.applyColorTransform(sp.getSolidColor());
  527. }
  528. return getColorFromColorIndexStruct(tp.getValue(), _sheet);
  529. }
  530. /**
  531. * Sets the bullet font
  532. */
  533. public void setBulletFont(String typeface) {
  534. if (typeface == null) {
  535. setPropVal(_paragraphStyle, "bullet.font", null);
  536. setFlag(ParagraphFlagsTextProp.BULLET_HARDFONT_IDX, false);
  537. }
  538. FontCollection fc = getSheet().getSlideShow().getFontCollection();
  539. int idx = fc.addFont(typeface);
  540. setParagraphTextPropVal("bullet.font", idx);
  541. setFlag(ParagraphFlagsTextProp.BULLET_HARDFONT_IDX, true);
  542. }
  543. /**
  544. * Returns the bullet font
  545. */
  546. public String getBulletFont() {
  547. TextProp tp = getPropVal(_paragraphStyle, "bullet.font", this);
  548. boolean hasFont = getFlag(ParagraphFlagsTextProp.BULLET_HARDFONT_IDX);
  549. if (tp == null || !hasFont) return getDefaultFontFamily();
  550. PPFont ppFont = getSheet().getSlideShow().getFont(tp.getValue());
  551. assert(ppFont != null);
  552. return ppFont.getFontName();
  553. }
  554. @Override
  555. public void setLineSpacing(Double lineSpacing) {
  556. setPctOrPoints("linespacing", lineSpacing);
  557. }
  558. @Override
  559. public Double getLineSpacing() {
  560. return getPctOrPoints("linespacing");
  561. }
  562. @Override
  563. public void setSpaceBefore(Double spaceBefore) {
  564. setPctOrPoints("spacebefore", spaceBefore);
  565. }
  566. @Override
  567. public Double getSpaceBefore() {
  568. return getPctOrPoints("spacebefore");
  569. }
  570. @Override
  571. public void setSpaceAfter(Double spaceAfter) {
  572. setPctOrPoints("spaceafter", spaceAfter);
  573. }
  574. @Override
  575. public Double getSpaceAfter() {
  576. return getPctOrPoints("spaceafter");
  577. }
  578. @Override
  579. public Double getDefaultTabSize() {
  580. // TODO: implement
  581. return null;
  582. }
  583. private Double getPctOrPoints(String propName) {
  584. TextProp tp = getPropVal(_paragraphStyle, propName, this);
  585. if (tp == null) return null;
  586. int val = tp.getValue();
  587. return (val < 0) ? Units.masterToPoints(val) : val;
  588. }
  589. private void setPctOrPoints(String propName, Double dval) {
  590. Integer ival = null;
  591. if (dval != null) {
  592. ival = (dval < 0) ? Units.pointsToMaster(dval) : dval.intValue();
  593. }
  594. setParagraphTextPropVal(propName, ival);
  595. }
  596. private boolean getFlag(int index) {
  597. BitMaskTextProp tp = (BitMaskTextProp)getPropVal(_paragraphStyle, ParagraphFlagsTextProp.NAME, this);
  598. return (tp == null) ? false : tp.getSubValue(index);
  599. }
  600. private void setFlag(int index, boolean value) {
  601. BitMaskTextProp tp = (BitMaskTextProp)_paragraphStyle.addWithName(ParagraphFlagsTextProp.NAME);
  602. tp.setSubValue(value, index);
  603. setDirty();
  604. }
  605. /**
  606. * Fetch the value of the given Paragraph related TextProp. Returns null if
  607. * that TextProp isn't present. If the TextProp isn't present, the value
  608. * from the appropriate Master Sheet will apply.
  609. *
  610. * The propName can be a comma-separated list, in case multiple equivalent values
  611. * are queried
  612. */
  613. protected static TextProp getPropVal(TextPropCollection props, String propName, HSLFTextParagraph paragraph) {
  614. String propNames[] = propName.split(",");
  615. for (String pn : propNames) {
  616. TextProp prop = props.findByName(pn);
  617. if (prop != null) return prop;
  618. }
  619. BitMaskTextProp maskProp = (BitMaskTextProp) props.findByName(ParagraphFlagsTextProp.NAME);
  620. boolean hardAttribute = (maskProp != null && maskProp.getValue() == 0);
  621. if (hardAttribute) return null;
  622. HSLFSheet sheet = paragraph.getSheet();
  623. int txtype = paragraph.getRunType();
  624. HSLFMasterSheet master = sheet.getMasterSheet();
  625. if (master == null) {
  626. logger.log(POILogger.WARN, "MasterSheet is not available");
  627. return null;
  628. }
  629. boolean isChar = props.getTextPropType() == TextPropType.character;
  630. for (String pn : propNames) {
  631. TextProp prop = master.getStyleAttribute(txtype, paragraph.getIndentLevel(), pn, isChar);
  632. if (prop != null) return prop;
  633. }
  634. return null;
  635. }
  636. /**
  637. * Returns the named TextProp, either by fetching it (if it exists) or
  638. * adding it (if it didn't)
  639. *
  640. * @param props the TextPropCollection to fetch from / add into
  641. * @param name the name of the TextProp to fetch/add
  642. * @param val the value, null if unset
  643. */
  644. protected static void setPropVal(TextPropCollection props, String name, Integer val) {
  645. if (val == null) {
  646. props.removeByName(name);
  647. return;
  648. }
  649. // Fetch / Add the TextProp
  650. TextProp tp = props.addWithName(name);
  651. tp.setValue(val);
  652. }
  653. /**
  654. * Check and add linebreaks to text runs leading other paragraphs
  655. *
  656. * @param paragraphs
  657. */
  658. protected static void fixLineEndings(List<HSLFTextParagraph> paragraphs) {
  659. HSLFTextRun lastRun = null;
  660. for (HSLFTextParagraph p : paragraphs) {
  661. if (lastRun != null && !lastRun.getRawText().endsWith("\r")) {
  662. lastRun.setText(lastRun.getRawText() + "\r");
  663. }
  664. List<HSLFTextRun> ltr = p.getTextRuns();
  665. if (ltr.isEmpty()) {
  666. throw new RuntimeException("paragraph without textruns found");
  667. }
  668. lastRun = ltr.get(ltr.size() - 1);
  669. assert (lastRun.getRawText() != null);
  670. }
  671. }
  672. /**
  673. * Search for a StyleTextPropAtom is for this text header (list of paragraphs)
  674. *
  675. * @param header the header
  676. * @param textLen the length of the rawtext, or -1 if the length is not known
  677. */
  678. private static StyleTextPropAtom findStyleAtomPresent(TextHeaderAtom header, int textLen) {
  679. boolean afterHeader = false;
  680. StyleTextPropAtom style = null;
  681. for (Record record : header.getParentRecord().getChildRecords()) {
  682. long rt = record.getRecordType();
  683. if (afterHeader && rt == RecordTypes.TextHeaderAtom.typeID) {
  684. // already on the next header, quit searching
  685. break;
  686. }
  687. afterHeader |= (header == record);
  688. if (afterHeader && rt == RecordTypes.StyleTextPropAtom.typeID) {
  689. // found it
  690. style = (StyleTextPropAtom) record;
  691. }
  692. }
  693. if (style == null) {
  694. logger.log(POILogger.INFO, "styles atom doesn't exist. Creating dummy record for later saving.");
  695. style = new StyleTextPropAtom((textLen < 0) ? 1 : textLen);
  696. } else {
  697. if (textLen >= 0) {
  698. style.setParentTextSize(textLen);
  699. }
  700. }
  701. return style;
  702. }
  703. /**
  704. * Saves the modified paragraphs/textrun to the records.
  705. * Also updates the styles to the correct text length.
  706. */
  707. protected static void storeText(List<HSLFTextParagraph> paragraphs) {
  708. fixLineEndings(paragraphs);
  709. String rawText = toInternalString(getRawText(paragraphs));
  710. // Will it fit in a 8 bit atom?
  711. boolean isUnicode = StringUtil.hasMultibyte(rawText);
  712. // isUnicode = true;
  713. TextHeaderAtom headerAtom = paragraphs.get(0)._headerAtom;
  714. TextBytesAtom byteAtom = paragraphs.get(0)._byteAtom;
  715. TextCharsAtom charAtom = paragraphs.get(0)._charAtom;
  716. StyleTextPropAtom styleAtom = findStyleAtomPresent(headerAtom, rawText.length());
  717. // Store in the appropriate record
  718. Record oldRecord = null, newRecord = null;
  719. if (isUnicode) {
  720. if (byteAtom != null || charAtom == null) {
  721. oldRecord = byteAtom;
  722. charAtom = new TextCharsAtom();
  723. }
  724. newRecord = charAtom;
  725. charAtom.setText(rawText);
  726. } else {
  727. if (charAtom != null || byteAtom == null) {
  728. oldRecord = charAtom;
  729. byteAtom = new TextBytesAtom();
  730. }
  731. newRecord = byteAtom;
  732. byte[] byteText = new byte[rawText.length()];
  733. StringUtil.putCompressedUnicode(rawText, byteText, 0);
  734. byteAtom.setText(byteText);
  735. }
  736. assert (newRecord != null);
  737. RecordContainer _txtbox = headerAtom.getParentRecord();
  738. Record[] cr = _txtbox.getChildRecords();
  739. int /* headerIdx = -1, */ textIdx = -1, styleIdx = -1;
  740. for (int i = 0; i < cr.length; i++) {
  741. Record r = cr[i];
  742. if (r == headerAtom) ; // headerIdx = i;
  743. else if (r == oldRecord || r == newRecord) textIdx = i;
  744. else if (r == styleAtom) styleIdx = i;
  745. }
  746. if (textIdx == -1) {
  747. // the old record was never registered, ignore it
  748. _txtbox.addChildAfter(newRecord, headerAtom);
  749. // textIdx = headerIdx + 1;
  750. } else {
  751. // swap not appropriated records - noop if unchanged
  752. cr[textIdx] = newRecord;
  753. }
  754. if (styleIdx == -1) {
  755. // Add the new StyleTextPropAtom after the TextCharsAtom / TextBytesAtom
  756. _txtbox.addChildAfter(styleAtom, newRecord);
  757. }
  758. for (HSLFTextParagraph p : paragraphs) {
  759. if (newRecord == byteAtom) {
  760. p._byteAtom = byteAtom;
  761. p._charAtom = null;
  762. } else {
  763. p._byteAtom = null;
  764. p._charAtom = charAtom;
  765. }
  766. }
  767. // Update the text length for its Paragraph and Character stylings
  768. // * reset the length, to the new string's length
  769. // * add on +1 if the last block
  770. styleAtom.clearStyles();
  771. TextPropCollection lastPTPC = null, lastRTPC = null, ptpc = null, rtpc = null;
  772. for (HSLFTextParagraph para : paragraphs) {
  773. ptpc = para.getParagraphStyle();
  774. ptpc.updateTextSize(0);
  775. if (!ptpc.equals(lastPTPC)) {
  776. lastPTPC = styleAtom.addParagraphTextPropCollection(0);
  777. lastPTPC.copy(ptpc);
  778. }
  779. for (HSLFTextRun tr : para.getTextRuns()) {
  780. rtpc = tr.getCharacterStyle();
  781. rtpc.updateTextSize(0);
  782. if (!rtpc.equals(lastRTPC)) {
  783. lastRTPC = styleAtom.addCharacterTextPropCollection(0);
  784. lastRTPC.copy(rtpc);
  785. }
  786. int len = tr.getLength();
  787. ptpc.updateTextSize(ptpc.getCharactersCovered() + len);
  788. rtpc.updateTextSize(len);
  789. lastPTPC.updateTextSize(lastPTPC.getCharactersCovered() + len);
  790. lastRTPC.updateTextSize(lastRTPC.getCharactersCovered() + len);
  791. }
  792. }
  793. assert (lastPTPC != null && lastRTPC != null && ptpc != null && rtpc != null);
  794. ptpc.updateTextSize(ptpc.getCharactersCovered() + 1);
  795. rtpc.updateTextSize(rtpc.getCharactersCovered() + 1);
  796. lastPTPC.updateTextSize(lastPTPC.getCharactersCovered() + 1);
  797. lastRTPC.updateTextSize(lastRTPC.getCharactersCovered() + 1);
  798. /**
  799. * If TextSpecInfoAtom is present, we must update the text size in it,
  800. * otherwise the ppt will be corrupted
  801. */
  802. for (Record r : paragraphs.get(0).getRecords()) {
  803. if (r instanceof TextSpecInfoAtom) {
  804. ((TextSpecInfoAtom) r).setParentSize(rawText.length() + 1);
  805. break;
  806. }
  807. }
  808. if (_txtbox instanceof EscherTextboxWrapper) {
  809. try {
  810. ((EscherTextboxWrapper) _txtbox).writeOut(null);
  811. } catch (IOException e) {
  812. throw new RuntimeException("failed dummy write", e);
  813. }
  814. }
  815. for (HSLFTextParagraph p : paragraphs) {
  816. p._dirty = false;
  817. }
  818. }
  819. /**
  820. * Adds the supplied text onto the end of the TextParagraphs,
  821. * creating a new RichTextRun for it to sit in.
  822. *
  823. * @param text the text string used by this object.
  824. */
  825. protected static HSLFTextRun appendText(List<HSLFTextParagraph> paragraphs, String text, boolean newParagraph) {
  826. text = toInternalString(text);
  827. // check paragraphs
  828. assert(!paragraphs.isEmpty() && !paragraphs.get(0).getTextRuns().isEmpty());
  829. HSLFTextParagraph htp = paragraphs.get(paragraphs.size() - 1);
  830. HSLFTextRun htr = htp.getTextRuns().get(htp.getTextRuns().size() - 1);
  831. boolean addParagraph = newParagraph;
  832. for (String rawText : text.split("(?<=\r)")) {
  833. // special case, if last text paragraph or run is empty, we will reuse it
  834. boolean lastRunEmpty = (htr.getLength() == 0);
  835. boolean lastParaEmpty = lastRunEmpty && (htp.getTextRuns().size() == 1);
  836. if (addParagraph && !lastParaEmpty) {
  837. TextPropCollection tpc = htp.getParagraphStyle();
  838. HSLFTextParagraph prevHtp = htp;
  839. htp = new HSLFTextParagraph(htp._headerAtom, htp._byteAtom, htp._charAtom, paragraphs);
  840. htp.getParagraphStyle().copy(tpc);
  841. htp.setParentShape(prevHtp.getParentShape());
  842. htp.setShapeId(prevHtp.getShapeId());
  843. htp.supplySheet(prevHtp.getSheet());
  844. paragraphs.add(htp);
  845. }
  846. addParagraph = true;
  847. if (!lastRunEmpty) {
  848. TextPropCollection tpc = htr.getCharacterStyle();
  849. htr = new HSLFTextRun(htp);
  850. htr.getCharacterStyle().copy(tpc);
  851. htp.addTextRun(htr);
  852. }
  853. htr.setText(rawText);
  854. }
  855. storeText(paragraphs);
  856. return htr;
  857. }
  858. /**
  859. * Sets (overwrites) the current text.
  860. * Uses the properties of the first paragraph / textrun
  861. *
  862. * @param text the text string used by this object.
  863. */
  864. public static HSLFTextRun setText(List<HSLFTextParagraph> paragraphs, String text) {
  865. // check paragraphs
  866. assert(!paragraphs.isEmpty() && !paragraphs.get(0).getTextRuns().isEmpty());
  867. Iterator<HSLFTextParagraph> paraIter = paragraphs.iterator();
  868. HSLFTextParagraph htp = paraIter.next(); // keep first
  869. assert (htp != null);
  870. while (paraIter.hasNext()) {
  871. paraIter.next();
  872. paraIter.remove();
  873. }
  874. Iterator<HSLFTextRun> runIter = htp.getTextRuns().iterator();
  875. if (runIter.hasNext()) {
  876. HSLFTextRun htr = runIter.next();
  877. htr.setText("");
  878. while (runIter.hasNext()) {
  879. runIter.next();
  880. runIter.remove();
  881. }
  882. } else {
  883. HSLFTextRun trun = new HSLFTextRun(htp);
  884. htp.addTextRun(trun);
  885. }
  886. return appendText(paragraphs, text, false);
  887. }
  888. public static String getText(List<HSLFTextParagraph> paragraphs) {
  889. assert (!paragraphs.isEmpty());
  890. String rawText = getRawText(paragraphs);
  891. return toExternalString(rawText, paragraphs.get(0).getRunType());
  892. }
  893. public static String getRawText(List<HSLFTextParagraph> paragraphs) {
  894. StringBuilder sb = new StringBuilder();
  895. for (HSLFTextParagraph p : paragraphs) {
  896. for (HSLFTextRun r : p.getTextRuns()) {
  897. sb.append(r.getRawText());
  898. }
  899. }
  900. return sb.toString();
  901. }
  902. @Override
  903. public String toString() {
  904. StringBuilder sb = new StringBuilder();
  905. for (HSLFTextRun r : getTextRuns()) {
  906. sb.append(r.getRawText());
  907. }
  908. return toExternalString(sb.toString(), getRunType());
  909. }
  910. /**
  911. * Returns a new string with line breaks converted into internal ppt
  912. * representation
  913. */
  914. protected static String toInternalString(String s) {
  915. String ns = s.replaceAll("\\r?\\n", "\r");
  916. return ns;
  917. }
  918. /**
  919. * Converts raw text from the text paragraphs to a formatted string,
  920. * i.e. it converts certain control characters used in the raw txt
  921. *
  922. * @param rawText the raw text
  923. * @param runType the run type of the shape, paragraph or headerAtom.
  924. * use -1 if unknown
  925. * @return the formatted string
  926. */
  927. public static String toExternalString(String rawText, int runType) {
  928. // PowerPoint seems to store files with \r as the line break
  929. // The messes things up on everything but a Mac, so translate
  930. // them to \n
  931. String text = rawText.replace('\r', '\n');
  932. switch (runType) {
  933. // 0xB acts like cariage return in page titles and like blank in the
  934. // others
  935. case -1:
  936. case org.apache.poi.hslf.record.TextHeaderAtom.TITLE_TYPE:
  937. case org.apache.poi.hslf.record.TextHeaderAtom.CENTER_TITLE_TYPE:
  938. text = text.replace((char) 0x0B, '\n');
  939. break;
  940. default:
  941. text = text.replace((char) 0x0B, ' ');
  942. break;
  943. }
  944. return text;
  945. }
  946. /**
  947. * For a given PPDrawing, grab all the TextRuns
  948. */
  949. public static List<List<HSLFTextParagraph>> findTextParagraphs(PPDrawing ppdrawing, HSLFSheet sheet) {
  950. List<List<HSLFTextParagraph>> runsV = new ArrayList<List<HSLFTextParagraph>>();
  951. for (EscherTextboxWrapper wrapper : ppdrawing.getTextboxWrappers()) {
  952. List<HSLFTextParagraph> p = findTextParagraphs(wrapper, sheet);
  953. if (p != null) runsV.add(p);
  954. }
  955. return runsV;
  956. }
  957. /**
  958. * Scans through the supplied record array, looking for
  959. * a TextHeaderAtom followed by one of a TextBytesAtom or
  960. * a TextCharsAtom. Builds up TextRuns from these
  961. *
  962. * @param wrapper an EscherTextboxWrapper
  963. */
  964. protected static List<HSLFTextParagraph> findTextParagraphs(EscherTextboxWrapper wrapper, HSLFSheet sheet) {
  965. // propagate parents to parent-aware records
  966. RecordContainer.handleParentAwareRecords(wrapper);
  967. int shapeId = wrapper.getShapeId();
  968. List<HSLFTextParagraph> rv = null;
  969. OutlineTextRefAtom ota = (OutlineTextRefAtom)wrapper.findFirstOfType(OutlineTextRefAtom.typeID);
  970. if (ota != null) {
  971. // if we are based on an outline, there are no further records to be parsed from the wrapper
  972. if (sheet == null) {
  973. throw new RuntimeException("Outline atom reference can't be solved without a sheet record");
  974. }
  975. List<List<HSLFTextParagraph>> sheetRuns = sheet.getTextParagraphs();
  976. assert (sheetRuns != null);
  977. int idx = ota.getTextIndex();
  978. for (List<HSLFTextParagraph> r : sheetRuns) {
  979. if (r.isEmpty()) continue;
  980. int ridx = r.get(0).getIndex();
  981. if (ridx > idx) break;
  982. if (ridx == idx) {
  983. if (rv == null) {
  984. rv = r;
  985. } else {
  986. // create a new container
  987. // TODO: ... is this case really happening?
  988. rv = new ArrayList<HSLFTextParagraph>(rv);
  989. rv.addAll(r);
  990. }
  991. }
  992. }
  993. if (rv == null || rv.isEmpty()) {
  994. logger.log(POILogger.WARN, "text run not found for OutlineTextRefAtom.TextIndex=" + idx);
  995. }
  996. } else {
  997. if (sheet != null) {
  998. // check sheet runs first, so we get exactly the same paragraph list
  999. List<List<HSLFTextParagraph>> sheetRuns = sheet.getTextParagraphs();
  1000. assert (sheetRuns != null);
  1001. for (List<HSLFTextParagraph> paras : sheetRuns) {
  1002. if (!paras.isEmpty() && paras.get(0)._headerAtom.getParentRecord() == wrapper) {
  1003. rv = paras;
  1004. break;
  1005. }
  1006. }
  1007. }
  1008. if (rv == null) {
  1009. // if we haven't found the wrapper in the sheet runs, create a new paragraph list from its record
  1010. List<List<HSLFTextParagraph>> rvl = findTextParagraphs(wrapper.getChildRecords());
  1011. switch (rvl.size()) {
  1012. case 0: break; // nothing found
  1013. case 1: rv = rvl.get(0); break; // normal case
  1014. default:
  1015. throw new RuntimeException("TextBox contains more than one list of paragraphs.");
  1016. }
  1017. }
  1018. }
  1019. if (rv != null) {
  1020. StyleTextProp9Atom styleTextProp9Atom = wrapper.getStyleTextProp9Atom();
  1021. for (HSLFTextParagraph htp : rv) {
  1022. htp.setShapeId(shapeId);
  1023. htp.setStyleTextProp9Atom(styleTextProp9Atom);
  1024. }
  1025. }
  1026. return rv;
  1027. }
  1028. /**
  1029. * Scans through the supplied record array, looking for
  1030. * a TextHeaderAtom followed by one of a TextBytesAtom or
  1031. * a TextCharsAtom. Builds up TextRuns from these
  1032. *
  1033. * @param records the records to build from
  1034. */
  1035. protected static List<List<HSLFTextParagraph>> findTextParagraphs(Record[] records) {
  1036. List<List<HSLFTextParagraph>> paragraphCollection = new ArrayList<List<HSLFTextParagraph>>();
  1037. int[] recordIdx = { 0 };
  1038. for (int slwtIndex = 0; recordIdx[0] < records.length; slwtIndex++) {
  1039. TextHeaderAtom header = null;
  1040. TextBytesAtom tbytes = null;
  1041. TextCharsAtom tchars = null;
  1042. TextRulerAtom ruler = null;
  1043. MasterTextPropAtom indents = null;
  1044. for (Record r : getRecords(records, recordIdx, null)) {
  1045. long rt = r.getRecordType();
  1046. if (RecordTypes.TextHeaderAtom.typeID == rt) {
  1047. header = (TextHeaderAtom) r;
  1048. } else if (RecordTypes.TextBytesAtom.typeID == rt) {
  1049. tbytes = (TextBytesAtom) r;
  1050. } else if (RecordTypes.TextCharsAtom.typeID == rt) {
  1051. tchars = (TextCharsAtom) r;
  1052. } else if (RecordTypes.TextRulerAtom.typeID == rt) {
  1053. ruler = (TextRulerAtom) r;
  1054. } else if (RecordTypes.MasterTextPropAtom.typeID == rt) {
  1055. indents = (MasterTextPropAtom) r;
  1056. }
  1057. // don't search for RecordTypes.StyleTextPropAtom.typeID here ... see findStyleAtomPresent below
  1058. }
  1059. if (header == null) break;
  1060. if (header.getParentRecord() instanceof SlideListWithText) {
  1061. // runs found in PPDrawing are not linked with SlideListWithTexts
  1062. header.setIndex(slwtIndex);
  1063. }
  1064. if (tbytes == null && tchars == null) {
  1065. tbytes = new TextBytesAtom();
  1066. // don't add record yet - set it in storeText
  1067. logger.log(POILogger.INFO, "bytes nor chars atom doesn't exist. Creating dummy record for later saving.");
  1068. }
  1069. String rawText = (tchars != null) ? tchars.getText() : tbytes.getText();
  1070. StyleTextPropAtom styles = findStyleAtomPresent(header, rawText.length());
  1071. List<HSLFTextParagraph> paragraphs = new ArrayList<HSLFTextParagraph>();
  1072. paragraphCollection.add(paragraphs);
  1073. // split, but keep delimiter
  1074. for (String para : rawText.split("(?<=\r)")) {
  1075. HSLFTextParagraph tpara = new HSLFTextParagraph(header, tbytes, tchars, paragraphs);
  1076. paragraphs.add(tpara);
  1077. tpara._ruler = ruler;
  1078. tpara.getParagraphStyle().updateTextSize(para.length());
  1079. HSLFTextRun trun = new HSLFTextRun(tpara);
  1080. tpara.addTextRun(trun);
  1081. trun.setText(para);
  1082. }
  1083. applyCharacterStyles(paragraphs, styles.getCharacterStyles());
  1084. applyParagraphStyles(paragraphs, styles.getParagraphStyles());
  1085. if (indents != null) {
  1086. applyParagraphIndents(paragraphs, indents.getIndents());
  1087. }
  1088. }
  1089. if (paragraphCollection.isEmpty()) {
  1090. logger.log(POILogger.DEBUG, "No text records found.");
  1091. }
  1092. return paragraphCollection;
  1093. }
  1094. protected static void applyHyperlinks(List<HSLFTextParagraph> paragraphs) {
  1095. List<HSLFHyperlink> links = HSLFHyperlink.find(paragraphs);
  1096. for (HSLFHyperlink h : links) {
  1097. int csIdx = 0;
  1098. for (HSLFTextParagraph p : paragraphs) {
  1099. for (HSLFTextRun r : p) {
  1100. if (h.getStartIndex() <= csIdx && csIdx < h.getEndIndex()) {
  1101. r.setHyperlinkId(h.getId());
  1102. }
  1103. csIdx += r.getLength();
  1104. }
  1105. }
  1106. }
  1107. }
  1108. protected static void applyCharacterStyles(List<HSLFTextParagraph> paragraphs, List<TextPropCollection> charStyles) {
  1109. int paraIdx = 0, runIdx = 0;
  1110. HSLFTextRun trun;
  1111. for (int csIdx = 0; csIdx < charStyles.size(); csIdx++) {
  1112. TextPropCollection p = charStyles.get(csIdx);
  1113. for (int ccRun = 0, ccStyle = p.getCharactersCovered(); ccRun < ccStyle;) {
  1114. HSLFTextParagraph para = paragraphs.get(paraIdx);
  1115. List<HSLFTextRun> runs = para.getTextRuns();
  1116. trun = runs.get(runIdx);
  1117. final int len = trun.getLength();
  1118. if (ccRun + len <= ccStyle) {
  1119. ccRun += len;
  1120. } else {
  1121. String text = trun.getRawText();
  1122. trun.setText(text.substring(0, ccStyle - ccRun));
  1123. HSLFTextRun nextRun = new HSLFTextRun(para);
  1124. nextRun.setText(text.substring(ccStyle - ccRun));
  1125. runs.add(runIdx + 1, nextRun);
  1126. ccRun += ccStyle - ccRun;
  1127. }
  1128. trun.setCharacterStyle(p);
  1129. if (paraIdx == paragraphs.size()-1 && runIdx == runs.size()-1) {
  1130. if (csIdx < charStyles.size() - 1) {
  1131. // special case, empty trailing text run
  1132. HSLFTextRun nextRun = new HSLFTextRun(para);
  1133. nextRun.setText("");
  1134. runs.add(nextRun);
  1135. ccRun++;
  1136. } else {
  1137. // need to add +1 to the last run of the last paragraph
  1138. trun.getCharacterStyle().updateTextSize(trun.getLength()+1);
  1139. ccRun++;
  1140. }
  1141. }
  1142. // need to compare it again, in case a run has been added after
  1143. if (++runIdx == runs.size()) {
  1144. paraIdx++;
  1145. runIdx = 0;
  1146. }
  1147. }
  1148. }
  1149. }
  1150. protected static void applyParagraphStyles(List<HSLFTextParagraph> paragraphs, List<TextPropCollection> paraStyles) {
  1151. int paraIdx = 0;
  1152. for (TextPropCollection p : paraStyles) {
  1153. for (int ccPara = 0, ccStyle = p.getCharactersCovered(); ccPara < ccStyle; paraIdx++) {
  1154. if (paraIdx >= paragraphs.size()) return;
  1155. HSLFTextParagraph htp = paragraphs.get(paraIdx);
  1156. TextPropCollection pCopy = new TextPropCollection(0, TextPropType.paragraph);
  1157. pCopy.copy(p);
  1158. htp.setParagraphStyle(pCopy);
  1159. int len = 0;
  1160. for (HSLFTextRun trun : htp.getTextRuns()) {
  1161. len += trun.getLength();
  1162. }
  1163. if (paraIdx == paragraphs.size()-1) len++;
  1164. pCopy.updateTextSize(len);
  1165. ccPara += len;
  1166. }
  1167. }
  1168. }
  1169. protected static void applyParagraphIndents(List<HSLFTextParagraph> paragraphs, List<IndentProp> paraStyles) {
  1170. int paraIdx = 0;
  1171. for (IndentProp p : paraStyles) {
  1172. for (int ccPara = 0, ccStyle = p.getCharactersCovered(); ccPara < ccStyle; paraIdx++) {
  1173. if (paraIdx >= paragraphs.size() || ccPara >= ccStyle-1) return;
  1174. HSLFTextParagraph para = paragraphs.get(paraIdx);
  1175. int len = 0;
  1176. for (HSLFTextRun trun : para.getTextRuns()) {
  1177. len += trun.getLength();
  1178. }
  1179. para.setIndentLevel(p.getIndentLevel());
  1180. ccPara += len + 1;
  1181. }
  1182. }
  1183. }
  1184. protected static List<HSLFTextParagraph> createEmptyParagraph() {
  1185. EscherTextboxWrapper wrapper = new EscherTextboxWrapper();
  1186. return createEmptyParagraph(wrapper);
  1187. }
  1188. protected static List<HSLFTextParagraph> createEmptyParagraph(EscherTextboxWrapper wrapper) {
  1189. TextHeaderAtom tha = new TextHeaderAtom();
  1190. tha.setParentRecord(wrapper);
  1191. wrapper.appendChildRecord(tha);
  1192. TextBytesAtom tba = new TextBytesAtom();
  1193. tba.setText("".getBytes(LocaleUtil.CHARSET_1252));
  1194. wrapper.appendChildRecord(tba);
  1195. StyleTextPropAtom sta = new StyleTextPropAtom(1);
  1196. TextPropCollection paraStyle = sta.addParagraphTextPropCollection(1);
  1197. TextPropCollection charStyle = sta.addCharacterTextPropCollection(1);
  1198. wrapper.appendChildRecord(sta);
  1199. List<HSLFTextParagraph> paragraphs = new ArrayList<HSLFTextParagraph>(1);
  1200. HSLFTextParagraph htp = new HSLFTextParagraph(tha, tba, null, paragraphs);
  1201. htp.setParagraphStyle(paraStyle);
  1202. paragraphs.add(htp);
  1203. HSLFTextRun htr = new HSLFTextRun(htp);
  1204. htr.setCharacterStyle(charStyle);
  1205. htr.setText("");
  1206. htp.addTextRun(htr);
  1207. return paragraphs;
  1208. }
  1209. public EscherTextboxWrapper getTextboxWrapper() {
  1210. return (EscherTextboxWrapper) _headerAtom.getParentRecord();
  1211. }
  1212. protected static Color getColorFromColorIndexStruct(int rgb, HSLFSheet sheet) {
  1213. int cidx = rgb >>> 24;
  1214. Color tmp;
  1215. switch (cidx) {
  1216. // Background ... Accent 3 color
  1217. case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
  1218. if (sheet == null) return null;
  1219. ColorSchemeAtom ca = sheet.getColorScheme();
  1220. tmp = new Color(ca.getColor(cidx), true);
  1221. break;
  1222. // Color is an sRGB value specified by red, green, and blue fields.
  1223. case 0xFE:
  1224. tmp = new Color(rgb, true);
  1225. break;
  1226. // Color is undefined.
  1227. default:
  1228. case 0xFF:
  1229. return null;
  1230. }
  1231. return new Color(tmp.getBlue(), tmp.getGreen(), tmp.getRed());
  1232. }
  1233. /**
  1234. * Sets the value of the given Paragraph TextProp, add if required
  1235. * @param propName The name of the Paragraph TextProp
  1236. * @param val The value to set for the TextProp
  1237. */
  1238. public void setParagraphTextPropVal(String propName, Integer val) {
  1239. setPropVal(_paragraphStyle, propName, val);
  1240. setDirty();
  1241. }
  1242. /**
  1243. * marks this paragraph dirty, so its records will be renewed on save
  1244. */
  1245. public void setDirty() {
  1246. _dirty = true;
  1247. }
  1248. public boolean isDirty() {
  1249. return _dirty;
  1250. }
  1251. /**
  1252. * Calculates the start index of the given text run
  1253. *
  1254. * @param textrun the text run to search for
  1255. * @return the start index with the paragraph collection or -1 if not found
  1256. */
  1257. /* package */ int getStartIdxOfTextRun(HSLFTextRun textrun) {
  1258. int idx = 0;
  1259. for (HSLFTextParagraph p : parentList) {
  1260. for (HSLFTextRun r : p) {
  1261. if (r == textrun) {
  1262. return idx;
  1263. }
  1264. idx += r.getLength();
  1265. }
  1266. }
  1267. return -1;
  1268. }
  1269. }