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

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