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.

XSLFTheme.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 static org.apache.poi.ooxml.POIXMLTypeLoader.DEFAULT_XML_OPTIONS;
  17. import java.io.IOException;
  18. import java.io.OutputStream;
  19. import javax.xml.namespace.QName;
  20. import org.apache.poi.ooxml.POIXMLDocumentPart;
  21. import org.apache.poi.openxml4j.opc.PackagePart;
  22. import org.apache.poi.util.Beta;
  23. import org.apache.poi.util.Internal;
  24. import org.apache.xmlbeans.XmlException;
  25. import org.apache.xmlbeans.XmlOptions;
  26. import org.openxmlformats.schemas.drawingml.x2006.main.CTBaseStyles;
  27. import org.openxmlformats.schemas.drawingml.x2006.main.CTColor;
  28. import org.openxmlformats.schemas.drawingml.x2006.main.CTColorScheme;
  29. import org.openxmlformats.schemas.drawingml.x2006.main.CTOfficeStyleSheet;
  30. import org.openxmlformats.schemas.drawingml.x2006.main.ThemeDocument;
  31. /**
  32. * A shared style sheet in a .pptx slide show
  33. */
  34. @Beta
  35. public class XSLFTheme extends POIXMLDocumentPart {
  36. private CTOfficeStyleSheet _theme;
  37. XSLFTheme() {
  38. _theme = CTOfficeStyleSheet.Factory.newInstance();
  39. }
  40. /**
  41. * @since POI 3.14-Beta1
  42. */
  43. public XSLFTheme(PackagePart part) throws IOException, XmlException {
  44. super(part);
  45. ThemeDocument doc =
  46. ThemeDocument.Factory.parse(getPackagePart().getInputStream(), DEFAULT_XML_OPTIONS);
  47. _theme = doc.getTheme();
  48. }
  49. @SuppressWarnings("WeakerAccess")
  50. public void importTheme(XSLFTheme theme) {
  51. _theme = theme.getXmlObject();
  52. }
  53. /**
  54. *
  55. * @return name of this theme, e.g. "Office Theme"
  56. */
  57. public String getName(){
  58. return _theme.getName();
  59. }
  60. /**
  61. * Set name of this theme
  62. *
  63. * @param name name of this theme
  64. */
  65. public void setName(String name){
  66. _theme.setName(name);
  67. }
  68. /**
  69. * Get a color from the theme's color scheme by name
  70. *
  71. * @return a theme color or <code>null</code> if not found
  72. */
  73. @Internal
  74. public CTColor getCTColor(String name) {
  75. CTBaseStyles elems = _theme.getThemeElements();
  76. CTColorScheme scheme = (elems == null) ? null : elems.getClrScheme();
  77. return getMapColor(name, scheme);
  78. }
  79. private static CTColor getMapColor(String mapName, CTColorScheme scheme) {
  80. if (mapName == null || scheme == null) {
  81. return null;
  82. }
  83. switch (mapName) {
  84. case "accent1":
  85. return scheme.getAccent1();
  86. case "accent2":
  87. return scheme.getAccent2();
  88. case "accent3":
  89. return scheme.getAccent3();
  90. case "accent4":
  91. return scheme.getAccent4();
  92. case "accent5":
  93. return scheme.getAccent5();
  94. case "accent6":
  95. return scheme.getAccent6();
  96. case "dk1":
  97. return scheme.getDk1();
  98. case "dk2":
  99. return scheme.getDk2();
  100. case "folHlink":
  101. return scheme.getFolHlink();
  102. case "hlink":
  103. return scheme.getHlink();
  104. case "lt1":
  105. return scheme.getLt1();
  106. case "lt2":
  107. return scheme.getLt2();
  108. default:
  109. return null;
  110. }
  111. }
  112. /**
  113. * While developing only!
  114. */
  115. @Internal
  116. public CTOfficeStyleSheet getXmlObject() {
  117. return _theme;
  118. }
  119. @Override
  120. protected final void commit() throws IOException {
  121. XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS);
  122. xmlOptions.setSaveSyntheticDocumentElement(
  123. new QName(XSLFRelation.NS_DRAWINGML, "theme"));
  124. PackagePart part = getPackagePart();
  125. OutputStream out = part.getOutputStream();
  126. getXmlObject().save(out, xmlOptions);
  127. out.close();
  128. }
  129. /**
  130. * @return typeface of the major font to use in a document.
  131. * Typically the major font is used for heading areas of a document.
  132. *
  133. */
  134. @SuppressWarnings("WeakerAccess")
  135. public String getMajorFont(){
  136. return _theme.getThemeElements().getFontScheme().getMajorFont().getLatin().getTypeface();
  137. }
  138. /**
  139. * @return typeface of the minor font to use in a document.
  140. * Typically the monor font is used for normal text or paragraph areas.
  141. *
  142. */
  143. @SuppressWarnings("WeakerAccess")
  144. public String getMinorFont(){
  145. return _theme.getThemeElements().getFontScheme().getMinorFont().getLatin().getTypeface();
  146. }
  147. }