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

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