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.8KB

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 org.apache.poi.POIXMLDocumentPart;
  17. import org.apache.poi.openxml4j.opc.PackagePart;
  18. import org.apache.poi.openxml4j.opc.PackageRelationship;
  19. import org.apache.poi.util.Beta;
  20. import org.apache.poi.util.Internal;
  21. import org.apache.xmlbeans.XmlException;
  22. import org.apache.xmlbeans.XmlObject;
  23. import org.apache.xmlbeans.XmlOptions;
  24. import org.openxmlformats.schemas.drawingml.x2006.main.CTBaseStyles;
  25. import org.openxmlformats.schemas.drawingml.x2006.main.CTColor;
  26. import org.openxmlformats.schemas.drawingml.x2006.main.CTColorMapping;
  27. import org.openxmlformats.schemas.drawingml.x2006.main.CTColorScheme;
  28. import org.openxmlformats.schemas.drawingml.x2006.main.CTOfficeStyleSheet;
  29. import org.openxmlformats.schemas.drawingml.x2006.main.CTTextParagraphProperties;
  30. import org.openxmlformats.schemas.drawingml.x2006.main.ThemeDocument;
  31. import javax.xml.namespace.QName;
  32. import java.io.IOException;
  33. import java.io.OutputStream;
  34. import java.util.HashMap;
  35. import java.util.Map;
  36. /**
  37. * A shared style sheet in a .pptx slide show
  38. *
  39. * @author Yegor Kozlov
  40. */
  41. @Beta
  42. public class XSLFTheme extends POIXMLDocumentPart {
  43. private CTOfficeStyleSheet _theme;
  44. private Map<String, CTColor> _schemeColors;
  45. XSLFTheme() {
  46. super();
  47. _theme = CTOfficeStyleSheet.Factory.newInstance();
  48. }
  49. public XSLFTheme(PackagePart part, PackageRelationship rel) throws IOException, XmlException {
  50. super(part, rel);
  51. ThemeDocument doc =
  52. ThemeDocument.Factory.parse(getPackagePart().getInputStream());
  53. _theme = doc.getTheme();
  54. initialize();
  55. }
  56. private void initialize(){
  57. CTBaseStyles elems = _theme.getThemeElements();
  58. CTColorScheme scheme = elems.getClrScheme();
  59. // The color scheme is responsible for defining a list of twelve colors.
  60. _schemeColors = new HashMap<String, CTColor>(12);
  61. for(XmlObject o : scheme.selectPath("*")){
  62. CTColor c = (CTColor)o;
  63. String name = c.getDomNode().getLocalName();
  64. _schemeColors.put(name, c);
  65. }
  66. }
  67. /**
  68. * re-map colors
  69. *
  70. * @param cmap color map defined in the master slide referencing this theme
  71. */
  72. void initColorMap(CTColorMapping cmap) {
  73. _schemeColors.put("bg1", _schemeColors.get(cmap.getBg1().toString()));
  74. _schemeColors.put("bg2", _schemeColors.get(cmap.getBg2().toString()));
  75. _schemeColors.put("tx1", _schemeColors.get(cmap.getTx1().toString()));
  76. _schemeColors.put("tx2", _schemeColors.get(cmap.getTx2().toString()));
  77. }
  78. /**
  79. *
  80. * @return name of this theme, e.g. "Office Theme"
  81. */
  82. public String getName(){
  83. return _theme.getName();
  84. }
  85. /**
  86. * Set name of this theme
  87. *
  88. * @param name name of this theme
  89. */
  90. public void setName(String name){
  91. _theme.setName(name);
  92. }
  93. /**
  94. * Get a color from the theme's color scheme by name
  95. *
  96. * @return a theme color or <code>null</code> if not found
  97. */
  98. CTColor getCTColor(String name){
  99. return _schemeColors.get(name);
  100. }
  101. /**
  102. * While developing only!
  103. */
  104. @Internal
  105. public CTOfficeStyleSheet getXmlObject() {
  106. return _theme;
  107. }
  108. protected final void commit() throws IOException {
  109. XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS);
  110. Map<String, String> map = new HashMap<String, String>();
  111. map.put("http://schemas.openxmlformats.org/drawingml/2006/main", "a");
  112. xmlOptions.setSaveSuggestedPrefixes(map);
  113. xmlOptions.setSaveSyntheticDocumentElement(
  114. new QName("http://schemas.openxmlformats.org/drawingml/2006/main", "theme"));
  115. PackagePart part = getPackagePart();
  116. OutputStream out = part.getOutputStream();
  117. getXmlObject().save(out, xmlOptions);
  118. out.close();
  119. }
  120. /**
  121. * @return typeface of the major font to use in a document.
  122. * Typically the major font is used for heading areas of a document.
  123. *
  124. */
  125. public String getMajorFont(){
  126. return _theme.getThemeElements().getFontScheme().getMajorFont().getLatin().getTypeface();
  127. }
  128. /**
  129. * @return typeface of the minor font to use in a document.
  130. * Typically the monor font is used for normal text or paragraph areas.
  131. *
  132. */
  133. public String getMinorFont(){
  134. return _theme.getThemeElements().getFontScheme().getMinorFont().getLatin().getTypeface();
  135. }
  136. CTTextParagraphProperties getDefaultParagraphStyle(){
  137. XmlObject[] o = _theme.selectPath(
  138. "declare namespace p='http://schemas.openxmlformats.org/presentationml/2006/main' " +
  139. "declare namespace a='http://schemas.openxmlformats.org/drawingml/2006/main' " +
  140. ".//a:objectDefaults/a:spDef/a:lstStyle/a:defPPr");
  141. if(o.length == 1){
  142. return (CTTextParagraphProperties)o[0];
  143. }
  144. return null;
  145. }
  146. }