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.

XSSFSimpleShape.java 30KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  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.xssf.usermodel;
  16. import java.util.ArrayList;
  17. import java.util.Iterator;
  18. import java.util.List;
  19. import org.apache.poi.hssf.util.HSSFColor;
  20. import org.apache.poi.ss.usermodel.VerticalAlignment;
  21. import org.apache.poi.util.Internal;
  22. import org.apache.poi.util.Units;
  23. import org.openxmlformats.schemas.drawingml.x2006.main.*;
  24. import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTShape;
  25. import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTShapeNonVisual;
  26. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRElt;
  27. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRPrElt;
  28. import org.openxmlformats.schemas.spreadsheetml.x2006.main.STUnderlineValues;
  29. /**
  30. * Represents a shape with a predefined geometry in a SpreadsheetML drawing.
  31. * Possible shape types are defined in {@link org.apache.poi.ss.usermodel.ShapeTypes}
  32. */
  33. public class XSSFSimpleShape extends XSSFShape implements Iterable<XSSFTextParagraph> { // TODO - instantiable superclass
  34. /**
  35. * List of the paragraphs that make up the text in this shape
  36. */
  37. private final List<XSSFTextParagraph> _paragraphs;
  38. /**
  39. * A default instance of CTShape used for creating new shapes.
  40. */
  41. private static CTShape prototype = null;
  42. /**
  43. * Xml bean that stores properties of this shape
  44. */
  45. private CTShape ctShape;
  46. protected XSSFSimpleShape(XSSFDrawing drawing, CTShape ctShape) {
  47. this.drawing = drawing;
  48. this.ctShape = ctShape;
  49. _paragraphs = new ArrayList<XSSFTextParagraph>();
  50. // initialize any existing paragraphs - this will be the default body paragraph in a new shape,
  51. // or existing paragraphs that have been loaded from the file
  52. CTTextBody body = ctShape.getTxBody();
  53. if(body != null) {
  54. for(int i = 0; i < body.sizeOfPArray(); i++) {
  55. _paragraphs.add(new XSSFTextParagraph(body.getPArray(i), ctShape));
  56. }
  57. }
  58. }
  59. /**
  60. * Prototype with the default structure of a new auto-shape.
  61. */
  62. protected static CTShape prototype() {
  63. if(prototype == null) {
  64. CTShape shape = CTShape.Factory.newInstance();
  65. CTShapeNonVisual nv = shape.addNewNvSpPr();
  66. CTNonVisualDrawingProps nvp = nv.addNewCNvPr();
  67. nvp.setId(1);
  68. nvp.setName("Shape 1");
  69. nv.addNewCNvSpPr();
  70. CTShapeProperties sp = shape.addNewSpPr();
  71. CTTransform2D t2d = sp.addNewXfrm();
  72. CTPositiveSize2D p1 = t2d.addNewExt();
  73. p1.setCx(0);
  74. p1.setCy(0);
  75. CTPoint2D p2 = t2d.addNewOff();
  76. p2.setX(0);
  77. p2.setY(0);
  78. CTPresetGeometry2D geom = sp.addNewPrstGeom();
  79. geom.setPrst(STShapeType.RECT);
  80. geom.addNewAvLst();
  81. CTTextBody body = shape.addNewTxBody();
  82. CTTextBodyProperties bodypr = body.addNewBodyPr();
  83. bodypr.setAnchor(STTextAnchoringType.T);
  84. bodypr.setRtlCol(false);
  85. CTTextParagraph p = body.addNewP();
  86. p.addNewPPr().setAlgn(STTextAlignType.L);
  87. CTTextCharacterProperties endPr = p.addNewEndParaRPr();
  88. endPr.setLang("en-US");
  89. endPr.setSz(1100);
  90. CTSolidColorFillProperties scfpr = endPr.addNewSolidFill();
  91. scfpr.addNewSrgbClr().setVal(new byte[] { 0, 0, 0 });
  92. body.addNewLstStyle();
  93. prototype = shape;
  94. }
  95. return prototype;
  96. }
  97. @Internal
  98. public CTShape getCTShape(){
  99. return ctShape;
  100. }
  101. public Iterator<XSSFTextParagraph> iterator(){
  102. return _paragraphs.iterator();
  103. }
  104. /**
  105. * Returns the text from all paragraphs in the shape. Paragraphs are separated by new lines.
  106. *
  107. * @return text contained within this shape or empty string
  108. */
  109. public String getText() {
  110. final int MAX_LEVELS = 9;
  111. StringBuilder out = new StringBuilder();
  112. List<Integer> levelCount = new ArrayList<Integer>(MAX_LEVELS); // maximum 9 levels
  113. XSSFTextParagraph p = null;
  114. // initialise the levelCount array - this maintains a record of the numbering to be used at each level
  115. for (int k = 0; k < MAX_LEVELS; k++){
  116. levelCount.add(0);
  117. }
  118. for(int i = 0; i < _paragraphs.size(); i++) {
  119. if (out.length() > 0) out.append('\n');
  120. p = _paragraphs.get(i);
  121. if(p.isBullet() && p.getText().length() > 0){
  122. int level = Math.min(p.getLevel(), MAX_LEVELS - 1);
  123. if(p.isBulletAutoNumber()){
  124. i = processAutoNumGroup(i, level, levelCount, out);
  125. } else {
  126. // indent appropriately for the level
  127. for(int j = 0; j < level; j++){
  128. out.append('\t');
  129. }
  130. String character = p.getBulletCharacter();
  131. out.append(character.length() > 0 ? character + " " : "- ");
  132. out.append(p.getText());
  133. }
  134. } else {
  135. out.append(p.getText());
  136. // this paragraph is not a bullet, so reset the count array
  137. for (int k = 0; k < MAX_LEVELS; k++){
  138. levelCount.set(k, 0);
  139. }
  140. }
  141. }
  142. return out.toString();
  143. }
  144. /**
  145. *
  146. */
  147. private int processAutoNumGroup(int index, int level, List<Integer> levelCount, StringBuilder out){
  148. XSSFTextParagraph p = null;
  149. XSSFTextParagraph nextp = null;
  150. ListAutoNumber scheme, nextScheme;
  151. int startAt, nextStartAt;
  152. p = _paragraphs.get(index);
  153. // The rules for generating the auto numbers are as follows. If the following paragraph is also
  154. // an auto-number, has the same type/scheme (and startAt if defined on this paragraph) then they are
  155. // considered part of the same group. An empty bullet paragraph is counted as part of the same
  156. // group but does not increment the count for the group. A change of type, startAt or the paragraph
  157. // not being a bullet resets the count for that level to 1.
  158. // first auto-number paragraph so initialise to 1 or the bullets startAt if present
  159. startAt = p.getBulletAutoNumberStart();
  160. scheme = p.getBulletAutoNumberScheme();
  161. if(levelCount.get(level) == 0) {
  162. levelCount.set(level, startAt == 0 ? 1 : startAt);
  163. }
  164. // indent appropriately for the level
  165. for(int j = 0; j < level; j++){
  166. out.append('\t');
  167. }
  168. if (p.getText().length() > 0){
  169. out.append(getBulletPrefix(scheme, levelCount.get(level)));
  170. out.append(p.getText());
  171. }
  172. while(true) {
  173. nextp = (index + 1) == _paragraphs.size() ? null : _paragraphs.get(index + 1);
  174. if(nextp == null) break; // out of paragraphs
  175. if(!(nextp.isBullet() && p.isBulletAutoNumber())) break; // not an auto-number bullet
  176. if(nextp.getLevel() > level) {
  177. // recurse into the new level group
  178. if (out.length() > 0) out.append('\n');
  179. index = processAutoNumGroup(index + 1, nextp.getLevel(), levelCount, out);
  180. continue; // restart the loop given the new index
  181. } else if(nextp.getLevel() < level) {
  182. break; // changed level
  183. }
  184. nextScheme = nextp.getBulletAutoNumberScheme();
  185. nextStartAt = nextp.getBulletAutoNumberStart();
  186. if(nextScheme == scheme && nextStartAt == startAt) {
  187. // bullet is valid, so increment i
  188. ++index;
  189. if (out.length() > 0) out.append('\n');
  190. // indent for the level
  191. for(int j = 0; j < level; j++){
  192. out.append('\t');
  193. }
  194. // check for empty text - only output a bullet if there is text, but it is still part of the group
  195. if(nextp.getText().length() > 0) {
  196. // increment the count for this level
  197. levelCount.set(level, levelCount.get(level) + 1);
  198. out.append(getBulletPrefix(nextScheme, levelCount.get(level)));
  199. out.append(nextp.getText());
  200. }
  201. } else {
  202. // something doesn't match so stop
  203. break;
  204. }
  205. }
  206. // end of the group so reset the count for this level
  207. levelCount.set(level, 0);
  208. return index;
  209. }
  210. /**
  211. * Returns a string containing an appropriate prefix for an auto-numbering bullet
  212. * @param scheme the auto-numbering scheme used by the bullet
  213. * @param value the value of the bullet
  214. * @return appropriate prefix for an auto-numbering bullet
  215. */
  216. private String getBulletPrefix(ListAutoNumber scheme, int value){
  217. StringBuilder out = new StringBuilder();
  218. switch(scheme) {
  219. case ALPHA_LC_PARENT_BOTH:
  220. case ALPHA_LC_PARENT_R:
  221. if(scheme == ListAutoNumber.ALPHA_LC_PARENT_BOTH) out.append('(');
  222. out.append(valueToAlpha(value).toLowerCase());
  223. out.append(')');
  224. break;
  225. case ALPHA_UC_PARENT_BOTH:
  226. case ALPHA_UC_PARENT_R:
  227. if(scheme == ListAutoNumber.ALPHA_UC_PARENT_BOTH) out.append('(');
  228. out.append(valueToAlpha(value));
  229. out.append(')');
  230. break;
  231. case ALPHA_LC_PERIOD:
  232. out.append(valueToAlpha(value).toLowerCase());
  233. out.append('.');
  234. break;
  235. case ALPHA_UC_PERIOD:
  236. out.append(valueToAlpha(value));
  237. out.append('.');
  238. break;
  239. case ARABIC_PARENT_BOTH:
  240. case ARABIC_PARENT_R:
  241. if(scheme == ListAutoNumber.ARABIC_PARENT_BOTH) out.append('(');
  242. out.append(value);
  243. out.append(')');
  244. break;
  245. case ARABIC_PERIOD:
  246. out.append(value);
  247. out.append('.');
  248. break;
  249. case ARABIC_PLAIN:
  250. out.append(value);
  251. break;
  252. case ROMAN_LC_PARENT_BOTH:
  253. case ROMAN_LC_PARENT_R:
  254. if(scheme == ListAutoNumber.ROMAN_LC_PARENT_BOTH) out.append('(');
  255. out.append(valueToRoman(value).toLowerCase());
  256. out.append(')');
  257. break;
  258. case ROMAN_UC_PARENT_BOTH:
  259. case ROMAN_UC_PARENT_R:
  260. if(scheme == ListAutoNumber.ROMAN_UC_PARENT_BOTH) out.append('(');
  261. out.append(valueToRoman(value));
  262. out.append(')');
  263. break;
  264. case ROMAN_LC_PERIOD:
  265. out.append(valueToRoman(value).toLowerCase());
  266. out.append('.');
  267. break;
  268. case ROMAN_UC_PERIOD:
  269. out.append(valueToRoman(value));
  270. out.append('.');
  271. break;
  272. default:
  273. out.append('\u2022'); // can't set the font to wingdings so use the default bullet character
  274. break;
  275. }
  276. out.append(" ");
  277. return out.toString();
  278. }
  279. /**
  280. * Convert an integer to its alpha equivalent e.g. 1 = A, 2 = B, 27 = AA etc
  281. */
  282. private String valueToAlpha(int value) {
  283. String alpha = "";
  284. int modulo;
  285. while (value > 0) {
  286. modulo = (value - 1) % 26;
  287. alpha = (char)(65 + modulo) + alpha;
  288. value = (int)((value - modulo) / 26);
  289. }
  290. return alpha;
  291. }
  292. private static String[] _romanChars = new String[] { "M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I" };
  293. private static int[] _romanAlphaValues = new int[] { 1000,900,500,400,100,90,50,40,10,9,5,4,1 };
  294. /**
  295. * Convert an integer to its roman equivalent e.g. 1 = I, 9 = IX etc
  296. */
  297. private String valueToRoman(int value) {
  298. StringBuilder out = new StringBuilder();
  299. for(int i = 0; value > 0 && i < _romanChars.length; i++) {
  300. while(_romanAlphaValues[i] <= value) {
  301. out.append(_romanChars[i]);
  302. value -= _romanAlphaValues[i];
  303. }
  304. }
  305. return out.toString();
  306. }
  307. /**
  308. * Clear all text from this shape
  309. */
  310. public void clearText(){
  311. _paragraphs.clear();
  312. CTTextBody txBody = ctShape.getTxBody();
  313. txBody.setPArray(null); // remove any existing paragraphs
  314. }
  315. /**
  316. * Set a single paragraph of text on the shape. Note this will replace all existing paragraphs created on the shape.
  317. * @param text string representing the paragraph text
  318. */
  319. public void setText(String text){
  320. clearText();
  321. addNewTextParagraph().addNewTextRun().setText(text);
  322. }
  323. /**
  324. * Set a single paragraph of text on the shape. Note this will replace all existing paragraphs created on the shape.
  325. * @param str rich text string representing the paragraph text
  326. */
  327. public void setText(XSSFRichTextString str){
  328. XSSFWorkbook wb = (XSSFWorkbook)getDrawing().getParent().getParent();
  329. str.setStylesTableReference(wb.getStylesSource());
  330. CTTextParagraph p = CTTextParagraph.Factory.newInstance();
  331. if(str.numFormattingRuns() == 0){
  332. CTRegularTextRun r = p.addNewR();
  333. CTTextCharacterProperties rPr = r.addNewRPr();
  334. rPr.setLang("en-US");
  335. rPr.setSz(1100);
  336. r.setT(str.getString());
  337. } else {
  338. for (int i = 0; i < str.getCTRst().sizeOfRArray(); i++) {
  339. CTRElt lt = str.getCTRst().getRArray(i);
  340. CTRPrElt ltPr = lt.getRPr();
  341. if(ltPr == null) ltPr = lt.addNewRPr();
  342. CTRegularTextRun r = p.addNewR();
  343. CTTextCharacterProperties rPr = r.addNewRPr();
  344. rPr.setLang("en-US");
  345. applyAttributes(ltPr, rPr);
  346. r.setT(lt.getT());
  347. }
  348. }
  349. clearText();
  350. ctShape.getTxBody().setPArray(new CTTextParagraph[]{p});
  351. _paragraphs.add(new XSSFTextParagraph(ctShape.getTxBody().getPArray(0), ctShape));
  352. }
  353. /**
  354. * Returns a collection of the XSSFTextParagraphs that are attached to this shape
  355. *
  356. * @return text paragraphs in this shape
  357. */
  358. public List<XSSFTextParagraph> getTextParagraphs() {
  359. return _paragraphs;
  360. }
  361. /**
  362. * Add a new paragraph run to this shape
  363. *
  364. * @return created paragraph run
  365. */
  366. public XSSFTextParagraph addNewTextParagraph() {
  367. CTTextBody txBody = ctShape.getTxBody();
  368. CTTextParagraph p = txBody.addNewP();
  369. XSSFTextParagraph paragraph = new XSSFTextParagraph(p, ctShape);
  370. _paragraphs.add(paragraph);
  371. return paragraph;
  372. }
  373. /**
  374. * Add a new paragraph run to this shape, set to the provided string
  375. *
  376. * @return created paragraph run
  377. */
  378. public XSSFTextParagraph addNewTextParagraph(String text) {
  379. XSSFTextParagraph paragraph = addNewTextParagraph();
  380. paragraph.addNewTextRun().setText(text);
  381. return paragraph;
  382. }
  383. /**
  384. * Add a new paragraph run to this shape, set to the provided rich text string
  385. *
  386. * @return created paragraph run
  387. */
  388. public XSSFTextParagraph addNewTextParagraph(XSSFRichTextString str) {
  389. CTTextBody txBody = ctShape.getTxBody();
  390. CTTextParagraph p = txBody.addNewP();
  391. if(str.numFormattingRuns() == 0){
  392. CTRegularTextRun r = p.addNewR();
  393. CTTextCharacterProperties rPr = r.addNewRPr();
  394. rPr.setLang("en-US");
  395. rPr.setSz(1100);
  396. r.setT(str.getString());
  397. } else {
  398. for (int i = 0; i < str.getCTRst().sizeOfRArray(); i++) {
  399. CTRElt lt = str.getCTRst().getRArray(i);
  400. CTRPrElt ltPr = lt.getRPr();
  401. if(ltPr == null) ltPr = lt.addNewRPr();
  402. CTRegularTextRun r = p.addNewR();
  403. CTTextCharacterProperties rPr = r.addNewRPr();
  404. rPr.setLang("en-US");
  405. applyAttributes(ltPr, rPr);
  406. r.setT(lt.getT());
  407. }
  408. }
  409. // Note: the XSSFTextParagraph constructor will create its required XSSFTextRuns from the provided CTTextParagraph
  410. XSSFTextParagraph paragraph = new XSSFTextParagraph(p, ctShape);
  411. _paragraphs.add(paragraph);
  412. return paragraph;
  413. }
  414. /**
  415. * Sets the type of horizontal overflow for the text.
  416. *
  417. * @param overflow - the type of horizontal overflow.
  418. * A <code>null</code> values unsets this property.
  419. */
  420. public void setTextHorizontalOverflow(TextHorizontalOverflow overflow){
  421. CTTextBodyProperties bodyPr = ctShape.getTxBody().getBodyPr();
  422. if (bodyPr != null) {
  423. if(anchor == null) {
  424. if(bodyPr.isSetHorzOverflow()) bodyPr.unsetHorzOverflow();
  425. } else {
  426. bodyPr.setHorzOverflow(STTextHorzOverflowType.Enum.forInt(overflow.ordinal() + 1));
  427. }
  428. }
  429. }
  430. /**
  431. * Returns the type of horizontal overflow for the text.
  432. *
  433. * @return the type of horizontal overflow
  434. */
  435. public TextHorizontalOverflow getTextHorizontalOverflow(){
  436. CTTextBodyProperties bodyPr = ctShape.getTxBody().getBodyPr();
  437. if(bodyPr != null) {
  438. if(bodyPr.isSetHorzOverflow()){
  439. return TextHorizontalOverflow.values()[bodyPr.getVertOverflow().intValue() - 1];
  440. }
  441. }
  442. return TextHorizontalOverflow.OVERFLOW;
  443. }
  444. /**
  445. * Sets the type of vertical overflow for the text.
  446. *
  447. * @param overflow - the type of vertical overflow.
  448. * A <code>null</code> values unsets this property.
  449. */
  450. public void setTextVerticalOverflow(TextVerticalOverflow overflow){
  451. CTTextBodyProperties bodyPr = ctShape.getTxBody().getBodyPr();
  452. if (bodyPr != null) {
  453. if(anchor == null) {
  454. if(bodyPr.isSetVertOverflow()) bodyPr.unsetVertOverflow();
  455. } else {
  456. bodyPr.setVertOverflow(STTextVertOverflowType.Enum.forInt(overflow.ordinal() + 1));
  457. }
  458. }
  459. }
  460. /**
  461. * Returns the type of vertical overflow for the text.
  462. *
  463. * @return the type of vertical overflow
  464. */
  465. public TextVerticalOverflow getTextVerticalOverflow(){
  466. CTTextBodyProperties bodyPr = ctShape.getTxBody().getBodyPr();
  467. if(bodyPr != null) {
  468. if(bodyPr.isSetVertOverflow()){
  469. return TextVerticalOverflow.values()[bodyPr.getVertOverflow().intValue() - 1];
  470. }
  471. }
  472. return TextVerticalOverflow.OVERFLOW;
  473. }
  474. /**
  475. * Sets the type of vertical alignment for the text within the shape.
  476. *
  477. * @param anchor - the type of alignment.
  478. * A <code>null</code> values unsets this property.
  479. */
  480. public void setVerticalAlignment(VerticalAlignment anchor){
  481. CTTextBodyProperties bodyPr = ctShape.getTxBody().getBodyPr();
  482. if (bodyPr != null) {
  483. if(anchor == null) {
  484. if(bodyPr.isSetAnchor()) bodyPr.unsetAnchor();
  485. } else {
  486. bodyPr.setAnchor(STTextAnchoringType.Enum.forInt(anchor.ordinal() + 1));
  487. }
  488. }
  489. }
  490. /**
  491. * Returns the type of vertical alignment for the text within the shape.
  492. *
  493. * @return the type of vertical alignment
  494. */
  495. public VerticalAlignment getVerticalAlignment(){
  496. CTTextBodyProperties bodyPr = ctShape.getTxBody().getBodyPr();
  497. if(bodyPr != null) {
  498. if(bodyPr.isSetAnchor()){
  499. return VerticalAlignment.values()[bodyPr.getAnchor().intValue() - 1];
  500. }
  501. }
  502. return VerticalAlignment.TOP;
  503. }
  504. /**
  505. * Sets the vertical orientation of the text
  506. *
  507. * @param orientation vertical orientation of the text
  508. * A <code>null</code> values unsets this property.
  509. */
  510. public void setTextDirection(TextDirection orientation){
  511. CTTextBodyProperties bodyPr = ctShape.getTxBody().getBodyPr();
  512. if (bodyPr != null) {
  513. if(orientation == null) {
  514. if(bodyPr.isSetVert()) bodyPr.unsetVert();
  515. } else {
  516. bodyPr.setVert(STTextVerticalType.Enum.forInt(orientation.ordinal() + 1));
  517. }
  518. }
  519. }
  520. /**
  521. * Gets the vertical orientation of the text
  522. *
  523. * @return vertical orientation of the text
  524. */
  525. public TextDirection getTextDirection(){
  526. CTTextBodyProperties bodyPr = ctShape.getTxBody().getBodyPr();
  527. if (bodyPr != null) {
  528. STTextVerticalType.Enum val = bodyPr.getVert();
  529. if(val != null){
  530. return TextDirection.values()[val.intValue() - 1];
  531. }
  532. }
  533. return TextDirection.HORIZONTAL;
  534. }
  535. /**
  536. * Returns the distance (in points) between the bottom of the text frame
  537. * and the bottom of the inscribed rectangle of the shape that contains the text.
  538. *
  539. * @return the bottom inset in points
  540. */
  541. public double getBottomInset(){
  542. CTTextBodyProperties bodyPr = ctShape.getTxBody().getBodyPr();
  543. if (bodyPr != null) {
  544. if(bodyPr.isSetBIns()){
  545. return Units.toPoints(bodyPr.getBIns());
  546. }
  547. }
  548. // If this attribute is omitted, then a value of 0.05 inches is implied
  549. return 3.6;
  550. }
  551. /**
  552. * Returns the distance (in points) between the left edge of the text frame
  553. * and the left edge of the inscribed rectangle of the shape that contains
  554. * the text.
  555. *
  556. * @return the left inset in points
  557. */
  558. public double getLeftInset(){
  559. CTTextBodyProperties bodyPr = ctShape.getTxBody().getBodyPr();
  560. if (bodyPr != null) {
  561. if(bodyPr.isSetLIns()){
  562. return Units.toPoints(bodyPr.getLIns());
  563. }
  564. }
  565. // If this attribute is omitted, then a value of 0.05 inches is implied
  566. return 3.6;
  567. }
  568. /**
  569. * Returns the distance (in points) between the right edge of the
  570. * text frame and the right edge of the inscribed rectangle of the shape
  571. * that contains the text.
  572. *
  573. * @return the right inset in points
  574. */
  575. public double getRightInset(){
  576. CTTextBodyProperties bodyPr = ctShape.getTxBody().getBodyPr();
  577. if (bodyPr != null) {
  578. if(bodyPr.isSetRIns()){
  579. return Units.toPoints(bodyPr.getRIns());
  580. }
  581. }
  582. // If this attribute is omitted, then a value of 0.05 inches is implied
  583. return 3.6;
  584. }
  585. /**
  586. * Returns the distance (in points) between the top of the text frame
  587. * and the top of the inscribed rectangle of the shape that contains the text.
  588. *
  589. * @return the top inset in points
  590. */
  591. public double getTopInset(){
  592. CTTextBodyProperties bodyPr = ctShape.getTxBody().getBodyPr();
  593. if (bodyPr != null) {
  594. if(bodyPr.isSetTIns()){
  595. return Units.toPoints(bodyPr.getTIns());
  596. }
  597. }
  598. // If this attribute is omitted, then a value of 0.05 inches is implied
  599. return 3.6;
  600. }
  601. /**
  602. * Sets the bottom inset.
  603. * @see #getBottomInset()
  604. *
  605. * @param margin the bottom margin
  606. */
  607. public void setBottomInset(double margin){
  608. CTTextBodyProperties bodyPr = ctShape.getTxBody().getBodyPr();
  609. if (bodyPr != null) {
  610. if(margin == -1) bodyPr.unsetBIns();
  611. else bodyPr.setBIns(Units.toEMU(margin));
  612. }
  613. }
  614. /**
  615. * Sets the left inset.
  616. * @see #getLeftInset()
  617. *
  618. * @param margin the left margin
  619. */
  620. public void setLeftInset(double margin){
  621. CTTextBodyProperties bodyPr = ctShape.getTxBody().getBodyPr();
  622. if (bodyPr != null) {
  623. if(margin == -1) bodyPr.unsetLIns();
  624. else bodyPr.setLIns(Units.toEMU(margin));
  625. }
  626. }
  627. /**
  628. * Sets the right inset.
  629. * @see #getRightInset()
  630. *
  631. * @param margin the right margin
  632. */
  633. public void setRightInset(double margin){
  634. CTTextBodyProperties bodyPr = ctShape.getTxBody().getBodyPr();
  635. if (bodyPr != null) {
  636. if(margin == -1) bodyPr.unsetRIns();
  637. else bodyPr.setRIns(Units.toEMU(margin));
  638. }
  639. }
  640. /**
  641. * Sets the top inset.
  642. * @see #getTopInset()
  643. *
  644. * @param margin the top margin
  645. */
  646. public void setTopInset(double margin){
  647. CTTextBodyProperties bodyPr = ctShape.getTxBody().getBodyPr();
  648. if (bodyPr != null) {
  649. if(margin == -1) bodyPr.unsetTIns();
  650. else bodyPr.setTIns(Units.toEMU(margin));
  651. }
  652. }
  653. /**
  654. * @return whether to wrap words within the bounding rectangle
  655. */
  656. public boolean getWordWrap(){
  657. CTTextBodyProperties bodyPr = ctShape.getTxBody().getBodyPr();
  658. if (bodyPr != null) {
  659. if(bodyPr.isSetWrap()){
  660. return bodyPr.getWrap() == STTextWrappingType.SQUARE;
  661. }
  662. }
  663. return true;
  664. }
  665. /**
  666. *
  667. * @param wrap whether to wrap words within the bounding rectangle
  668. */
  669. public void setWordWrap(boolean wrap){
  670. CTTextBodyProperties bodyPr = ctShape.getTxBody().getBodyPr();
  671. if (bodyPr != null) {
  672. bodyPr.setWrap(wrap ? STTextWrappingType.SQUARE : STTextWrappingType.NONE);
  673. }
  674. }
  675. /**
  676. *
  677. * Specifies that a shape should be auto-fit to fully contain the text described within it.
  678. * Auto-fitting is when text within a shape is scaled in order to contain all the text inside
  679. *
  680. * @param value type of autofit
  681. */
  682. public void setTextAutofit(TextAutofit value){
  683. CTTextBodyProperties bodyPr = ctShape.getTxBody().getBodyPr();
  684. if (bodyPr != null) {
  685. if(bodyPr.isSetSpAutoFit()) bodyPr.unsetSpAutoFit();
  686. if(bodyPr.isSetNoAutofit()) bodyPr.unsetNoAutofit();
  687. if(bodyPr.isSetNormAutofit()) bodyPr.unsetNormAutofit();
  688. switch(value){
  689. case NONE: bodyPr.addNewNoAutofit(); break;
  690. case NORMAL: bodyPr.addNewNormAutofit(); break;
  691. case SHAPE: bodyPr.addNewSpAutoFit(); break;
  692. }
  693. }
  694. }
  695. /**
  696. *
  697. * @return type of autofit
  698. */
  699. public TextAutofit getTextAutofit(){
  700. CTTextBodyProperties bodyPr = ctShape.getTxBody().getBodyPr();
  701. if (bodyPr != null) {
  702. if(bodyPr.isSetNoAutofit()) return TextAutofit.NONE;
  703. else if (bodyPr.isSetNormAutofit()) return TextAutofit.NORMAL;
  704. else if (bodyPr.isSetSpAutoFit()) return TextAutofit.SHAPE;
  705. }
  706. return TextAutofit.NORMAL;
  707. }
  708. /**
  709. * Gets the shape type, one of the constants defined in {@link org.apache.poi.ss.usermodel.ShapeTypes}.
  710. *
  711. * @return the shape type
  712. * @see org.apache.poi.ss.usermodel.ShapeTypes
  713. */
  714. public int getShapeType() {
  715. return ctShape.getSpPr().getPrstGeom().getPrst().intValue();
  716. }
  717. /**
  718. * Sets the shape types.
  719. *
  720. * @param type the shape type, one of the constants defined in {@link org.apache.poi.ss.usermodel.ShapeTypes}.
  721. * @see org.apache.poi.ss.usermodel.ShapeTypes
  722. */
  723. public void setShapeType(int type) {
  724. ctShape.getSpPr().getPrstGeom().setPrst(STShapeType.Enum.forInt(type));
  725. }
  726. protected CTShapeProperties getShapeProperties(){
  727. return ctShape.getSpPr();
  728. }
  729. /**
  730. * org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRPrElt to
  731. * org.openxmlformats.schemas.drawingml.x2006.main.CTFont adapter
  732. */
  733. private static void applyAttributes(CTRPrElt pr, CTTextCharacterProperties rPr){
  734. if(pr.sizeOfBArray() > 0) rPr.setB(pr.getBArray(0).getVal());
  735. if(pr.sizeOfUArray() > 0) {
  736. STUnderlineValues.Enum u1 = pr.getUArray(0).getVal();
  737. if(u1 == STUnderlineValues.SINGLE) rPr.setU(STTextUnderlineType.SNG);
  738. else if(u1 == STUnderlineValues.DOUBLE) rPr.setU(STTextUnderlineType.DBL);
  739. else if(u1 == STUnderlineValues.NONE) rPr.setU(STTextUnderlineType.NONE);
  740. }
  741. if(pr.sizeOfIArray() > 0) rPr.setI(pr.getIArray(0).getVal());
  742. if(pr.sizeOfRFontArray() > 0) {
  743. CTTextFont rFont = rPr.isSetLatin() ? rPr.getLatin() : rPr.addNewLatin();
  744. rFont.setTypeface(pr.getRFontArray(0).getVal());
  745. }
  746. if(pr.sizeOfSzArray() > 0) {
  747. int sz = (int)(pr.getSzArray(0).getVal()*100);
  748. rPr.setSz(sz);
  749. }
  750. if(pr.sizeOfColorArray() > 0) {
  751. CTSolidColorFillProperties fill = rPr.isSetSolidFill() ? rPr.getSolidFill() : rPr.addNewSolidFill();
  752. org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor xlsColor = pr.getColorArray(0);
  753. if(xlsColor.isSetRgb()) {
  754. CTSRgbColor clr = fill.isSetSrgbClr() ? fill.getSrgbClr() : fill.addNewSrgbClr();
  755. clr.setVal(xlsColor.getRgb());
  756. }
  757. else if(xlsColor.isSetIndexed()) {
  758. HSSFColor indexed = HSSFColor.getIndexHash().get((int) xlsColor.getIndexed());
  759. if (indexed != null) {
  760. byte[] rgb = new byte[3];
  761. rgb[0] = (byte) indexed.getTriplet()[0];
  762. rgb[1] = (byte) indexed.getTriplet()[1];
  763. rgb[2] = (byte) indexed.getTriplet()[2];
  764. CTSRgbColor clr = fill.isSetSrgbClr() ? fill.getSrgbClr() : fill.addNewSrgbClr();
  765. clr.setVal(rgb);
  766. }
  767. }
  768. }
  769. }
  770. }