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 34KB

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