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.

XSLFTextRun.java 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  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.xslf.usermodel;
  16. import java.awt.Color;
  17. import org.apache.poi.common.usermodel.fonts.FontCharset;
  18. import org.apache.poi.common.usermodel.fonts.FontFamily;
  19. import org.apache.poi.common.usermodel.fonts.FontGroup;
  20. import org.apache.poi.common.usermodel.fonts.FontInfo;
  21. import org.apache.poi.common.usermodel.fonts.FontPitch;
  22. import org.apache.poi.openxml4j.exceptions.OpenXML4JRuntimeException;
  23. import org.apache.poi.openxml4j.opc.PackagePart;
  24. import org.apache.poi.sl.draw.DrawPaint;
  25. import org.apache.poi.sl.usermodel.PaintStyle;
  26. import org.apache.poi.sl.usermodel.PaintStyle.SolidPaint;
  27. import org.apache.poi.sl.usermodel.TextRun;
  28. import org.apache.poi.util.Beta;
  29. import org.apache.poi.xslf.model.CharacterPropertyFetcher;
  30. import org.apache.poi.xslf.usermodel.XSLFPropertiesDelegate.XSLFFillProperties;
  31. import org.apache.xmlbeans.XmlObject;
  32. import org.openxmlformats.schemas.drawingml.x2006.main.CTFontCollection;
  33. import org.openxmlformats.schemas.drawingml.x2006.main.CTFontScheme;
  34. import org.openxmlformats.schemas.drawingml.x2006.main.CTHyperlink;
  35. import org.openxmlformats.schemas.drawingml.x2006.main.CTRegularTextRun;
  36. import org.openxmlformats.schemas.drawingml.x2006.main.CTSchemeColor;
  37. import org.openxmlformats.schemas.drawingml.x2006.main.CTShapeStyle;
  38. import org.openxmlformats.schemas.drawingml.x2006.main.CTSolidColorFillProperties;
  39. import org.openxmlformats.schemas.drawingml.x2006.main.CTTextCharacterProperties;
  40. import org.openxmlformats.schemas.drawingml.x2006.main.CTTextField;
  41. import org.openxmlformats.schemas.drawingml.x2006.main.CTTextFont;
  42. import org.openxmlformats.schemas.drawingml.x2006.main.CTTextLineBreak;
  43. import org.openxmlformats.schemas.drawingml.x2006.main.CTTextNormalAutofit;
  44. import org.openxmlformats.schemas.drawingml.x2006.main.STTextStrikeType;
  45. import org.openxmlformats.schemas.drawingml.x2006.main.STTextUnderlineType;
  46. /**
  47. * Represents a run of text within the containing text body. The run element is the
  48. * lowest level text separation mechanism within a text body.
  49. */
  50. @Beta
  51. public class XSLFTextRun implements TextRun {
  52. private final XmlObject _r;
  53. private final XSLFTextParagraph _p;
  54. protected XSLFTextRun(XmlObject r, XSLFTextParagraph p){
  55. _r = r;
  56. _p = p;
  57. if (!(r instanceof CTRegularTextRun || r instanceof CTTextLineBreak || r instanceof CTTextField)) {
  58. throw new OpenXML4JRuntimeException("unsupported text run of type "+r.getClass());
  59. }
  60. }
  61. XSLFTextParagraph getParentParagraph(){
  62. return _p;
  63. }
  64. @Override
  65. public String getRawText(){
  66. if (_r instanceof CTTextField) {
  67. return ((CTTextField)_r).getT();
  68. } else if (_r instanceof CTTextLineBreak) {
  69. return "\n";
  70. }
  71. return ((CTRegularTextRun)_r).getT();
  72. }
  73. @Override
  74. public void setText(String text){
  75. if (_r instanceof CTTextField) {
  76. ((CTTextField)_r).setT(text);
  77. } else if (!(_r instanceof CTTextLineBreak)) {
  78. ((CTRegularTextRun)_r).setT(text);
  79. }
  80. }
  81. /**
  82. * Return the text run xmlbeans object.
  83. * Depending on the type of text run, this can be {@link CTTextField},
  84. * {@link CTTextLineBreak} or usually a {@link CTRegularTextRun}
  85. *
  86. * @return the xmlbeans object
  87. */
  88. public XmlObject getXmlObject(){
  89. return _r;
  90. }
  91. @Override
  92. public void setFontColor(Color color) {
  93. setFontColor(DrawPaint.createSolidPaint(color));
  94. }
  95. @Override
  96. public void setFontColor(PaintStyle color) {
  97. if (!(color instanceof SolidPaint)) {
  98. throw new IllegalArgumentException("Currently only SolidPaint is supported!");
  99. }
  100. SolidPaint sp = (SolidPaint)color;
  101. Color c = DrawPaint.applyColorTransform(sp.getSolidColor());
  102. CTTextCharacterProperties rPr = getRPr(true);
  103. CTSolidColorFillProperties fill = rPr.isSetSolidFill() ? rPr.getSolidFill() : rPr.addNewSolidFill();
  104. XSLFColor col = new XSLFColor(fill, getParentParagraph().getParentShape().getSheet().getTheme(), fill.getSchemeClr());
  105. col.setColor(c);
  106. }
  107. @Override
  108. public PaintStyle getFontColor(){
  109. final boolean hasPlaceholder = getParentParagraph().getParentShape().getPlaceholder() != null;
  110. CharacterPropertyFetcher<PaintStyle> fetcher = new CharacterPropertyFetcher<PaintStyle>(_p.getIndentLevel()){
  111. @Override
  112. public boolean fetch(CTTextCharacterProperties props){
  113. if (props == null) {
  114. return false;
  115. }
  116. XSLFShape shape = _p.getParentShape();
  117. CTShapeStyle style = shape.getSpStyle();
  118. CTSchemeColor phClr = null;
  119. if (style != null && style.getFontRef() != null) {
  120. phClr = style.getFontRef().getSchemeClr();
  121. }
  122. XSLFFillProperties fp = XSLFPropertiesDelegate.getFillDelegate(props);
  123. XSLFSheet sheet = shape.getSheet();
  124. PackagePart pp = sheet.getPackagePart();
  125. XSLFTheme theme = sheet.getTheme();
  126. PaintStyle ps = XSLFShape.selectPaint(fp, phClr, pp, theme, hasPlaceholder);
  127. if (ps != null) {
  128. setValue(ps);
  129. return true;
  130. }
  131. return false;
  132. }
  133. };
  134. fetchCharacterProperty(fetcher);
  135. return fetcher.getValue();
  136. }
  137. @Override
  138. public void setFontSize(Double fontSize){
  139. CTTextCharacterProperties rPr = getRPr(true);
  140. if(fontSize == null) {
  141. if (rPr.isSetSz()) {
  142. rPr.unsetSz();
  143. }
  144. } else {
  145. if (fontSize < 1.0) {
  146. throw new IllegalArgumentException("Minimum font size is 1pt but was " + fontSize);
  147. }
  148. rPr.setSz((int)(100*fontSize));
  149. }
  150. }
  151. @Override
  152. public Double getFontSize(){
  153. double scale = 1;
  154. CTTextNormalAutofit afit = getParentParagraph().getParentShape().getTextBodyPr().getNormAutofit();
  155. if(afit != null) {
  156. scale = (double)afit.getFontScale() / 100000;
  157. }
  158. CharacterPropertyFetcher<Double> fetcher = new CharacterPropertyFetcher<Double>(_p.getIndentLevel()){
  159. @Override
  160. public boolean fetch(CTTextCharacterProperties props){
  161. if (props != null && props.isSetSz()) {
  162. setValue(props.getSz()*0.01);
  163. return true;
  164. }
  165. return false;
  166. }
  167. };
  168. fetchCharacterProperty(fetcher);
  169. return fetcher.getValue() == null ? null : fetcher.getValue()*scale;
  170. }
  171. /**
  172. * @return the spacing between characters within a text run,
  173. * If this attribute is omitted than a value of 0 or no adjustment is assumed.
  174. */
  175. @SuppressWarnings("WeakerAccess")
  176. public double getCharacterSpacing(){
  177. CharacterPropertyFetcher<Double> fetcher = new CharacterPropertyFetcher<Double>(_p.getIndentLevel()){
  178. @Override
  179. public boolean fetch(CTTextCharacterProperties props){
  180. if (props != null && props.isSetSpc()) {
  181. setValue(props.getSpc()*0.01);
  182. return true;
  183. }
  184. return false;
  185. }
  186. };
  187. fetchCharacterProperty(fetcher);
  188. return fetcher.getValue() == null ? 0 : fetcher.getValue();
  189. }
  190. /**
  191. * Set the spacing between characters within a text run.
  192. * <p>
  193. * The spacing is specified in points. Positive values will cause the text to expand,
  194. * negative values to condense.
  195. * </p>
  196. *
  197. * @param spc character spacing in points.
  198. */
  199. @SuppressWarnings("WeakerAccess")
  200. public void setCharacterSpacing(double spc){
  201. CTTextCharacterProperties rPr = getRPr(true);
  202. if(spc == 0.0) {
  203. if(rPr.isSetSpc()) {
  204. rPr.unsetSpc();
  205. }
  206. } else {
  207. rPr.setSpc((int)(100*spc));
  208. }
  209. }
  210. @Override
  211. public void setFontFamily(String typeface) {
  212. FontGroup fg = FontGroup.getFontGroupFirst(getRawText());
  213. new XSLFFontInfo(fg).setTypeface(typeface);
  214. }
  215. @Override
  216. public void setFontFamily(String typeface, FontGroup fontGroup) {
  217. new XSLFFontInfo(fontGroup).setTypeface(typeface);
  218. }
  219. @Override
  220. public void setFontInfo(FontInfo fontInfo, FontGroup fontGroup) {
  221. new XSLFFontInfo(fontGroup).copyFrom(fontInfo);
  222. }
  223. @Override
  224. public String getFontFamily() {
  225. FontGroup fg = FontGroup.getFontGroupFirst(getRawText());
  226. return new XSLFFontInfo(fg).getTypeface();
  227. }
  228. @Override
  229. public String getFontFamily(FontGroup fontGroup) {
  230. return new XSLFFontInfo(fontGroup).getTypeface();
  231. }
  232. @Override
  233. public FontInfo getFontInfo(FontGroup fontGroup) {
  234. XSLFFontInfo fontInfo = new XSLFFontInfo(fontGroup);
  235. return (fontInfo.getTypeface() != null) ? fontInfo : null;
  236. }
  237. @Override
  238. public byte getPitchAndFamily(){
  239. FontGroup fg = FontGroup.getFontGroupFirst(getRawText());
  240. XSLFFontInfo fontInfo = new XSLFFontInfo(fg);
  241. FontPitch pitch = fontInfo.getPitch();
  242. if (pitch == null) {
  243. pitch = FontPitch.VARIABLE;
  244. }
  245. FontFamily family = fontInfo.getFamily();
  246. if (family == null) {
  247. family = FontFamily.FF_SWISS;
  248. }
  249. return FontPitch.getNativeId(pitch, family);
  250. }
  251. @Override
  252. public void setStrikethrough(boolean strike) {
  253. getRPr(true).setStrike(strike ? STTextStrikeType.SNG_STRIKE : STTextStrikeType.NO_STRIKE);
  254. }
  255. @Override
  256. public boolean isStrikethrough() {
  257. CharacterPropertyFetcher<Boolean> fetcher = new CharacterPropertyFetcher<Boolean>(_p.getIndentLevel()){
  258. @Override
  259. public boolean fetch(CTTextCharacterProperties props){
  260. if(props != null && props.isSetStrike()) {
  261. setValue(props.getStrike() != STTextStrikeType.NO_STRIKE);
  262. return true;
  263. }
  264. return false;
  265. }
  266. };
  267. fetchCharacterProperty(fetcher);
  268. return fetcher.getValue() == null ? false : fetcher.getValue();
  269. }
  270. @Override
  271. public boolean isSuperscript() {
  272. CharacterPropertyFetcher<Boolean> fetcher = new CharacterPropertyFetcher<Boolean>(_p.getIndentLevel()){
  273. @Override
  274. public boolean fetch(CTTextCharacterProperties props){
  275. if (props != null && props.isSetBaseline()) {
  276. setValue(props.getBaseline() > 0);
  277. return true;
  278. }
  279. return false;
  280. }
  281. };
  282. fetchCharacterProperty(fetcher);
  283. return fetcher.getValue() == null ? false : fetcher.getValue();
  284. }
  285. /**
  286. * Set the baseline for both the superscript and subscript fonts.
  287. * <p>
  288. * The size is specified using a percentage.
  289. * Positive values indicate superscript, negative values indicate subscript.
  290. * </p>
  291. */
  292. @SuppressWarnings("WeakerAccess")
  293. public void setBaselineOffset(double baselineOffset){
  294. getRPr(true).setBaseline((int) baselineOffset * 1000);
  295. }
  296. /**
  297. * Set whether the text in this run is formatted as superscript.
  298. * Default base line offset is 30%
  299. *
  300. * @see #setBaselineOffset(double)
  301. */
  302. @SuppressWarnings("WeakerAccess")
  303. public void setSuperscript(boolean flag){
  304. setBaselineOffset(flag ? 30. : 0.);
  305. }
  306. /**
  307. * Set whether the text in this run is formatted as subscript.
  308. * Default base line offset is -25%.
  309. *
  310. * @see #setBaselineOffset(double)
  311. */
  312. @SuppressWarnings("WeakerAccess")
  313. public void setSubscript(boolean flag){
  314. setBaselineOffset(flag ? -25.0 : 0.);
  315. }
  316. @Override
  317. public boolean isSubscript() {
  318. CharacterPropertyFetcher<Boolean> fetcher = new CharacterPropertyFetcher<Boolean>(_p.getIndentLevel()){
  319. @Override
  320. public boolean fetch(CTTextCharacterProperties props){
  321. if (props != null && props.isSetBaseline()) {
  322. setValue(props.getBaseline() < 0);
  323. return true;
  324. }
  325. return false;
  326. }
  327. };
  328. fetchCharacterProperty(fetcher);
  329. return fetcher.getValue() == null ? false : fetcher.getValue();
  330. }
  331. /**
  332. * @return whether a run of text will be formatted as a superscript text. Default is false.
  333. */
  334. @Override
  335. public TextCap getTextCap() {
  336. CharacterPropertyFetcher<TextCap> fetcher = new CharacterPropertyFetcher<TextCap>(_p.getIndentLevel()){
  337. @Override
  338. public boolean fetch(CTTextCharacterProperties props){
  339. if (props != null && props.isSetCap()) {
  340. int idx = props.getCap().intValue() - 1;
  341. setValue(TextCap.values()[idx]);
  342. return true;
  343. }
  344. return false;
  345. }
  346. };
  347. fetchCharacterProperty(fetcher);
  348. return fetcher.getValue() == null ? TextCap.NONE : fetcher.getValue();
  349. }
  350. @Override
  351. public void setBold(boolean bold){
  352. getRPr(true).setB(bold);
  353. }
  354. @Override
  355. public boolean isBold(){
  356. CharacterPropertyFetcher<Boolean> fetcher = new CharacterPropertyFetcher<Boolean>(_p.getIndentLevel()){
  357. @Override
  358. public boolean fetch(CTTextCharacterProperties props){
  359. if (props != null && props.isSetB()) {
  360. setValue(props.getB());
  361. return true;
  362. }
  363. return false;
  364. }
  365. };
  366. fetchCharacterProperty(fetcher);
  367. return fetcher.getValue() == null ? false : fetcher.getValue();
  368. }
  369. @Override
  370. public void setItalic(boolean italic){
  371. getRPr(true).setI(italic);
  372. }
  373. @Override
  374. public boolean isItalic(){
  375. CharacterPropertyFetcher<Boolean> fetcher = new CharacterPropertyFetcher<Boolean>(_p.getIndentLevel()){
  376. @Override
  377. public boolean fetch(CTTextCharacterProperties props){
  378. if (props != null && props.isSetI()) {
  379. setValue(props.getI());
  380. return true;
  381. }
  382. return false;
  383. }
  384. };
  385. fetchCharacterProperty(fetcher);
  386. return fetcher.getValue() == null ? false : fetcher.getValue();
  387. }
  388. @Override
  389. public void setUnderlined(boolean underline) {
  390. getRPr(true).setU(underline ? STTextUnderlineType.SNG : STTextUnderlineType.NONE);
  391. }
  392. @Override
  393. public boolean isUnderlined(){
  394. CharacterPropertyFetcher<Boolean> fetcher = new CharacterPropertyFetcher<Boolean>(_p.getIndentLevel()){
  395. @Override
  396. public boolean fetch(CTTextCharacterProperties props){
  397. if (props != null && props.isSetU()) {
  398. setValue(props.getU() != STTextUnderlineType.NONE);
  399. return true;
  400. }
  401. return false;
  402. }
  403. };
  404. fetchCharacterProperty(fetcher);
  405. return fetcher.getValue() == null ? false : fetcher.getValue();
  406. }
  407. /**
  408. * Return the character properties
  409. *
  410. * @param create if true, create an empty character properties object if it doesn't exist
  411. * @return the character properties or null if create was false and the properties haven't exist
  412. */
  413. protected CTTextCharacterProperties getRPr(boolean create) {
  414. if (_r instanceof CTTextField) {
  415. CTTextField tf = (CTTextField)_r;
  416. if (tf.isSetRPr()) {
  417. return tf.getRPr();
  418. } else if (create) {
  419. return tf.addNewRPr();
  420. }
  421. } else if (_r instanceof CTTextLineBreak) {
  422. CTTextLineBreak tlb = (CTTextLineBreak)_r;
  423. if (tlb.isSetRPr()) {
  424. return tlb.getRPr();
  425. } else if (create) {
  426. return tlb.addNewRPr();
  427. }
  428. } else {
  429. CTRegularTextRun tr = (CTRegularTextRun)_r;
  430. if (tr.isSetRPr()) {
  431. return tr.getRPr();
  432. } else if (create) {
  433. return tr.addNewRPr();
  434. }
  435. }
  436. return null;
  437. }
  438. @Override
  439. public String toString(){
  440. return "[" + getClass() + "]" + getRawText();
  441. }
  442. @Override
  443. public XSLFHyperlink createHyperlink(){
  444. XSLFHyperlink hl = getHyperlink();
  445. if (hl != null) {
  446. return hl;
  447. }
  448. CTTextCharacterProperties rPr = getRPr(true);
  449. return new XSLFHyperlink(rPr.addNewHlinkClick(), _p.getParentShape().getSheet());
  450. }
  451. @Override
  452. public XSLFHyperlink getHyperlink(){
  453. CTTextCharacterProperties rPr = getRPr(false);
  454. if (rPr == null) {
  455. return null;
  456. }
  457. CTHyperlink hl = rPr.getHlinkClick();
  458. if (hl == null) {
  459. return null;
  460. }
  461. return new XSLFHyperlink(hl, _p.getParentShape().getSheet());
  462. }
  463. private void fetchCharacterProperty(final CharacterPropertyFetcher<?> visitor){
  464. XSLFTextShape shape = _p.getParentShape();
  465. CTTextCharacterProperties rPr = getRPr(false);
  466. if (rPr != null && visitor.fetch(rPr)) {
  467. return;
  468. }
  469. if (shape.fetchShapeProperty(visitor)) {
  470. return;
  471. }
  472. if (_p.fetchThemeProperty(visitor)) {
  473. return;
  474. }
  475. _p.fetchMasterProperty(visitor);
  476. }
  477. void copy(XSLFTextRun r){
  478. String srcFontFamily = r.getFontFamily();
  479. if(srcFontFamily != null && !srcFontFamily.equals(getFontFamily())){
  480. setFontFamily(srcFontFamily);
  481. }
  482. PaintStyle srcFontColor = r.getFontColor();
  483. if(srcFontColor != null && !srcFontColor.equals(getFontColor())){
  484. setFontColor(srcFontColor);
  485. }
  486. Double srcFontSize = r.getFontSize();
  487. if (srcFontSize == null) {
  488. if (getFontSize() != null) setFontSize(null);
  489. } else if(!srcFontSize.equals(getFontSize())) {
  490. setFontSize(srcFontSize);
  491. }
  492. boolean bold = r.isBold();
  493. if(bold != isBold()) {
  494. setBold(bold);
  495. }
  496. boolean italic = r.isItalic();
  497. if(italic != isItalic()) {
  498. setItalic(italic);
  499. }
  500. boolean underline = r.isUnderlined();
  501. if(underline != isUnderlined()) {
  502. setUnderlined(underline);
  503. }
  504. boolean strike = r.isStrikethrough();
  505. if(strike != isStrikethrough()) {
  506. setStrikethrough(strike);
  507. }
  508. }
  509. @Override
  510. public FieldType getFieldType() {
  511. if (_r instanceof CTTextField) {
  512. CTTextField tf = (CTTextField)_r;
  513. if ("slidenum".equals(tf.getType())) {
  514. return FieldType.SLIDE_NUMBER;
  515. }
  516. }
  517. return null;
  518. }
  519. private final class XSLFFontInfo implements FontInfo {
  520. private final FontGroup fontGroup;
  521. private XSLFFontInfo(FontGroup fontGroup) {
  522. this.fontGroup = (fontGroup != null) ? fontGroup : FontGroup.getFontGroupFirst(getRawText());
  523. }
  524. void copyFrom(FontInfo fontInfo) {
  525. CTTextFont tf = getXmlObject(true);
  526. setTypeface(fontInfo.getTypeface());
  527. setCharset(fontInfo.getCharset());
  528. FontPitch pitch = fontInfo.getPitch();
  529. FontFamily family = fontInfo.getFamily();
  530. if (pitch == null && family == null) {
  531. if (tf.isSetPitchFamily()) {
  532. tf.unsetPitchFamily();
  533. }
  534. } else {
  535. setPitch(pitch);
  536. setFamily(family);
  537. }
  538. }
  539. @Override
  540. public Integer getIndex() {
  541. return null;
  542. }
  543. @Override
  544. public void setIndex(int index) {
  545. throw new UnsupportedOperationException("setIndex not supported by XSLFFontInfo.");
  546. }
  547. @Override
  548. public String getTypeface() {
  549. CTTextFont tf = getXmlObject(false);
  550. return (tf != null && tf.isSetTypeface()) ? tf.getTypeface() : null;
  551. }
  552. @Override
  553. public void setTypeface(String typeface) {
  554. if (typeface != null) {
  555. getXmlObject(true).setTypeface(typeface);
  556. return;
  557. }
  558. CTTextCharacterProperties props = getRPr(false);
  559. if (props == null) {
  560. return;
  561. }
  562. FontGroup fg = FontGroup.getFontGroupFirst(getRawText());
  563. switch (fg) {
  564. default:
  565. case LATIN:
  566. if (props.isSetLatin()) {
  567. props.unsetLatin();
  568. }
  569. break;
  570. case EAST_ASIAN:
  571. if (props.isSetEa()) {
  572. props.unsetEa();
  573. }
  574. break;
  575. case COMPLEX_SCRIPT:
  576. if (props.isSetCs()) {
  577. props.unsetCs();
  578. }
  579. break;
  580. case SYMBOL:
  581. if (props.isSetSym()) {
  582. props.unsetSym();
  583. }
  584. break;
  585. }
  586. }
  587. @Override
  588. public FontCharset getCharset() {
  589. CTTextFont tf = getXmlObject(false);
  590. return (tf != null && tf.isSetCharset()) ? FontCharset.valueOf(tf.getCharset()&0xFF) : null;
  591. }
  592. @Override
  593. public void setCharset(FontCharset charset) {
  594. CTTextFont tf = getXmlObject(true);
  595. if (charset != null) {
  596. tf.setCharset((byte)charset.getNativeId());
  597. } else {
  598. if (tf.isSetCharset()) {
  599. tf.unsetCharset();
  600. }
  601. }
  602. }
  603. @Override
  604. public FontFamily getFamily() {
  605. CTTextFont tf = getXmlObject(false);
  606. return (tf != null && tf.isSetPitchFamily()) ? FontFamily.valueOfPitchFamily(tf.getPitchFamily()) : null;
  607. }
  608. @Override
  609. public void setFamily(FontFamily family) {
  610. CTTextFont tf = getXmlObject(true);
  611. if (family == null && !tf.isSetPitchFamily()) {
  612. return;
  613. }
  614. FontPitch pitch = (tf.isSetPitchFamily())
  615. ? FontPitch.valueOfPitchFamily(tf.getPitchFamily())
  616. : FontPitch.VARIABLE;
  617. byte pitchFamily = FontPitch.getNativeId(pitch, family != null ? family : FontFamily.FF_SWISS);
  618. tf.setPitchFamily(pitchFamily);
  619. }
  620. @Override
  621. public FontPitch getPitch() {
  622. CTTextFont tf = getXmlObject(false);
  623. return (tf != null && tf.isSetPitchFamily()) ? FontPitch.valueOfPitchFamily(tf.getPitchFamily()) : null;
  624. }
  625. @Override
  626. public void setPitch(FontPitch pitch) {
  627. CTTextFont tf = getXmlObject(true);
  628. if (pitch == null && !tf.isSetPitchFamily()) {
  629. return;
  630. }
  631. FontFamily family = (tf.isSetPitchFamily())
  632. ? FontFamily.valueOfPitchFamily(tf.getPitchFamily())
  633. : FontFamily.FF_SWISS;
  634. byte pitchFamily = FontPitch.getNativeId(pitch != null ? pitch : FontPitch.VARIABLE, family);
  635. tf.setPitchFamily(pitchFamily);
  636. }
  637. private CTTextFont getXmlObject(boolean create) {
  638. if (create) {
  639. return getCTTextFont(getRPr(true), true);
  640. }
  641. CharacterPropertyFetcher<CTTextFont> visitor = new CharacterPropertyFetcher<CTTextFont>(_p.getIndentLevel()){
  642. @Override
  643. public boolean fetch(CTTextCharacterProperties props){
  644. CTTextFont font = getCTTextFont(props, false);
  645. if (font == null) {
  646. return false;
  647. }
  648. setValue(font);
  649. return true;
  650. }
  651. };
  652. fetchCharacterProperty(visitor);
  653. return visitor.getValue();
  654. }
  655. private CTTextFont getCTTextFont(CTTextCharacterProperties props, boolean create) {
  656. if (props == null) {
  657. return null;
  658. }
  659. CTTextFont font;
  660. switch (fontGroup) {
  661. default:
  662. case LATIN:
  663. font = props.getLatin();
  664. if (font == null && create) {
  665. font = props.addNewLatin();
  666. }
  667. break;
  668. case EAST_ASIAN:
  669. font = props.getEa();
  670. if (font == null && create) {
  671. font = props.addNewEa();
  672. }
  673. break;
  674. case COMPLEX_SCRIPT:
  675. font = props.getCs();
  676. if (font == null && create) {
  677. font = props.addNewCs();
  678. }
  679. break;
  680. case SYMBOL:
  681. font = props.getSym();
  682. if (font == null && create) {
  683. font = props.addNewSym();
  684. }
  685. break;
  686. }
  687. if (font == null) {
  688. return null;
  689. }
  690. String typeface = font.isSetTypeface() ? font.getTypeface() : "";
  691. if (typeface.startsWith("+mj-") || typeface.startsWith("+mn-")) {
  692. // "+mj-lt".equals(typeface) || "+mn-lt".equals(typeface)
  693. final XSLFTheme theme = _p.getParentShape().getSheet().getTheme();
  694. CTFontScheme fontTheme = theme.getXmlObject().getThemeElements().getFontScheme();
  695. CTFontCollection coll = typeface.startsWith("+mj-")
  696. ? fontTheme.getMajorFont() : fontTheme.getMinorFont();
  697. // TODO: handle LCID codes
  698. // see https://blogs.msdn.microsoft.com/officeinteroperability/2013/04/22/office-open-xml-themes-schemes-and-fonts/
  699. String fgStr = typeface.substring(4);
  700. if ("ea".equals(fgStr)) {
  701. font = coll.getEa();
  702. } else if ("cs".equals(fgStr)) {
  703. font = coll.getCs();
  704. } else {
  705. font = coll.getLatin();
  706. }
  707. // SYMBOL is missing
  708. if (font == null || !font.isSetTypeface() || "".equals(font.getTypeface())) {
  709. font = coll.getLatin();
  710. }
  711. }
  712. return font;
  713. }
  714. }
  715. }