您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

CharacterPropertyFetcher.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * ====================================================================
  3. * Licensed to the Apache Software Foundation (ASF) under one or more
  4. * contributor license agreements. See the NOTICE file distributed with
  5. * this work for additional information regarding copyright ownership.
  6. * The ASF licenses this file to You under the Apache License, Version 2.0
  7. * (the "License"); you may not use this file except in compliance with
  8. * the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. * ====================================================================
  18. */
  19. package org.apache.poi.xslf.model;
  20. import static org.apache.poi.xslf.model.ParagraphPropertyFetcher.getThemeProps;
  21. import static org.apache.poi.xslf.model.ParagraphPropertyFetcher.select;
  22. import java.util.function.Consumer;
  23. import org.apache.poi.util.Internal;
  24. import org.apache.poi.xslf.usermodel.XSLFShape;
  25. import org.apache.poi.xslf.usermodel.XSLFSheet;
  26. import org.apache.poi.xslf.usermodel.XSLFSlideMaster;
  27. import org.apache.poi.xslf.usermodel.XSLFTextRun;
  28. import org.apache.xmlbeans.XmlException;
  29. import org.openxmlformats.schemas.drawingml.x2006.main.CTTextCharacterProperties;
  30. import org.openxmlformats.schemas.drawingml.x2006.main.CTTextParagraphProperties;
  31. @Internal
  32. public final class CharacterPropertyFetcher<T> extends PropertyFetcher<T> {
  33. public interface CharPropFetcher<S> {
  34. void fetch (CTTextCharacterProperties props, Consumer<S> val);
  35. }
  36. private final XSLFTextRun run;
  37. int _level;
  38. private final CharPropFetcher<T> fetcher;
  39. public CharacterPropertyFetcher(XSLFTextRun run, CharPropFetcher<T> fetcher) {
  40. _level = run.getParagraph().getIndentLevel();
  41. this.fetcher = fetcher;
  42. this.run = run;
  43. }
  44. public boolean fetch(XSLFShape shape) {
  45. // this is only called when propagating to parent styles
  46. try {
  47. fetchProp(select(shape, _level));
  48. } catch (XmlException ignored) {
  49. }
  50. return isSet();
  51. }
  52. public T fetchProperty(XSLFShape shape) {
  53. final XSLFSheet sheet = shape.getSheet();
  54. if (!(sheet instanceof XSLFSlideMaster)) {
  55. fetchRunProp();
  56. fetchParagraphDefaultRunProp();
  57. fetchShapeProp(shape);
  58. fetchThemeProp(shape);
  59. }
  60. fetchMasterProp();
  61. return isSet() ? getValue() : null;
  62. }
  63. private void fetchRunProp() {
  64. fetchProp(run.getRPr(false));
  65. }
  66. private void fetchParagraphDefaultRunProp() {
  67. if (!isSet()) {
  68. CTTextParagraphProperties pr = run.getParagraph().getXmlObject().getPPr();
  69. if (pr != null) {
  70. fetchProp(pr.getDefRPr());
  71. }
  72. }
  73. }
  74. private void fetchShapeProp(XSLFShape shape) {
  75. if (!isSet()) {
  76. shape.fetchShapeProperty(this);
  77. }
  78. }
  79. private void fetchThemeProp(XSLFShape shape) {
  80. if (!isSet()) {
  81. fetchProp(getThemeProps(shape, _level));
  82. }
  83. }
  84. private void fetchMasterProp() {
  85. // defaults for placeholders are defined in the slide master
  86. // TODO: determine master shape
  87. if (!isSet()) {
  88. fetchProp(run.getParagraph().getDefaultMasterStyle());
  89. }
  90. }
  91. private void fetchProp(CTTextParagraphProperties props) {
  92. if (props != null) {
  93. fetchProp(props.getDefRPr());
  94. }
  95. }
  96. private void fetchProp(CTTextCharacterProperties props) {
  97. if (props != null) {
  98. fetcher.fetch(props, this::setValue);
  99. }
  100. }
  101. }