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.

ThemesTable.java 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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.xssf.model;
  16. import static org.apache.poi.POIXMLTypeLoader.DEFAULT_XML_OPTIONS;
  17. import java.io.IOException;
  18. import java.io.OutputStream;
  19. import org.apache.poi.POIXMLDocumentPart;
  20. import org.apache.poi.openxml4j.opc.PackagePart;
  21. import org.apache.poi.xssf.usermodel.IndexedColorMap;
  22. import org.apache.poi.xssf.usermodel.XSSFColor;
  23. import org.apache.xmlbeans.XmlException;
  24. import org.openxmlformats.schemas.drawingml.x2006.main.CTColor;
  25. import org.openxmlformats.schemas.drawingml.x2006.main.CTColorScheme;
  26. import org.openxmlformats.schemas.drawingml.x2006.main.ThemeDocument;
  27. /**
  28. * Class that represents theme of XLSX document. The theme includes specific
  29. * colors and fonts.
  30. */
  31. public class ThemesTable extends POIXMLDocumentPart {
  32. public enum ThemeElement {
  33. LT1(0, "Lt1"),
  34. DK1(1,"Dk1"),
  35. LT2(2,"Lt2"),
  36. DK2(3,"Dk2"),
  37. ACCENT1(4,"Accent1"),
  38. ACCENT2(5,"Accent2"),
  39. ACCENT3(6,"Accent3"),
  40. ACCENT4(7,"Accent4"),
  41. ACCENT5(8,"Accent5"),
  42. ACCENT6(9,"Accent6"),
  43. HLINK(10,"Hlink"),
  44. FOLHLINK(11,"FolHlink"),
  45. UNKNOWN(-1,null);
  46. public static ThemeElement byId(int idx) {
  47. if (idx >= values().length || idx < 0) return UNKNOWN;
  48. return values()[idx];
  49. }
  50. private ThemeElement(int idx, String name) {
  51. this.idx = idx; this.name = name;
  52. }
  53. public final int idx;
  54. public final String name;
  55. }
  56. private IndexedColorMap colorMap;
  57. private ThemeDocument theme;
  58. /**
  59. * Create a new, empty ThemesTable
  60. */
  61. public ThemesTable() {
  62. super();
  63. theme = ThemeDocument.Factory.newInstance();
  64. theme.addNewTheme().addNewThemeElements();
  65. }
  66. /**
  67. * Construct a ThemesTable.
  68. * @param part A PackagePart.
  69. *
  70. * @since POI 3.14-Beta1
  71. */
  72. public ThemesTable(PackagePart part) throws IOException {
  73. super(part);
  74. try {
  75. theme = ThemeDocument.Factory.parse(part.getInputStream(), DEFAULT_XML_OPTIONS);
  76. } catch(XmlException e) {
  77. throw new IOException(e.getLocalizedMessage(), e);
  78. }
  79. }
  80. /**
  81. * Construct a ThemesTable from an existing ThemeDocument.
  82. * @param theme A ThemeDocument.
  83. */
  84. public ThemesTable(ThemeDocument theme) {
  85. this.theme = theme;
  86. }
  87. /**
  88. * called from {@link StylesTable} when setting theme, used to adjust colors if a custom indexed mapping is defined
  89. * @param colorMap
  90. */
  91. protected void setColorMap(IndexedColorMap colorMap) {
  92. this.colorMap = colorMap;
  93. }
  94. /**
  95. * Convert a theme "index" (as used by fonts etc) into a color.
  96. * @param idx A theme "index"
  97. * @return The mapped XSSFColor, or null if not mapped.
  98. */
  99. public XSSFColor getThemeColor(int idx) {
  100. // Theme color references are NOT positional indices into the color scheme,
  101. // i.e. these keys are NOT the same as the order in which theme colors appear
  102. // in theme1.xml. They are keys to a mapped color.
  103. CTColorScheme colorScheme = theme.getTheme().getThemeElements().getClrScheme();
  104. CTColor ctColor;
  105. switch (ThemeElement.byId(idx)) {
  106. case LT1: ctColor = colorScheme.getLt1(); break;
  107. case DK1: ctColor = colorScheme.getDk1(); break;
  108. case LT2: ctColor = colorScheme.getLt2(); break;
  109. case DK2: ctColor = colorScheme.getDk2(); break;
  110. case ACCENT1: ctColor = colorScheme.getAccent1(); break;
  111. case ACCENT2: ctColor = colorScheme.getAccent2(); break;
  112. case ACCENT3: ctColor = colorScheme.getAccent3(); break;
  113. case ACCENT4: ctColor = colorScheme.getAccent4(); break;
  114. case ACCENT5: ctColor = colorScheme.getAccent5(); break;
  115. case ACCENT6: ctColor = colorScheme.getAccent6(); break;
  116. case HLINK: ctColor = colorScheme.getHlink(); break;
  117. case FOLHLINK:ctColor = colorScheme.getFolHlink();break;
  118. default: return null;
  119. }
  120. byte[] rgb = null;
  121. if (ctColor.isSetSrgbClr()) {
  122. // Color is a regular one
  123. rgb = ctColor.getSrgbClr().getVal();
  124. } else if (ctColor.isSetSysClr()) {
  125. // Color is a tint of white or black
  126. rgb = ctColor.getSysClr().getLastClr();
  127. } else {
  128. return null;
  129. }
  130. return new XSSFColor(rgb, colorMap);
  131. }
  132. /**
  133. * If the colour is based on a theme, then inherit
  134. * information (currently just colours) from it as
  135. * required.
  136. */
  137. public void inheritFromThemeAsRequired(XSSFColor color) {
  138. if(color == null) {
  139. // Nothing for us to do
  140. return;
  141. }
  142. if(! color.getCTColor().isSetTheme()) {
  143. // No theme set, nothing to do
  144. return;
  145. }
  146. // Get the theme colour
  147. XSSFColor themeColor = getThemeColor(color.getTheme());
  148. // Set the raw colour, not the adjusted one
  149. // Do a raw set, no adjusting at the XSSFColor layer either
  150. color.getCTColor().setRgb(themeColor.getCTColor().getRgb());
  151. // All done
  152. }
  153. /**
  154. * Write this table out as XML.
  155. *
  156. * @param out The stream to write to.
  157. * @throws IOException if an error occurs while writing.
  158. */
  159. public void writeTo(OutputStream out) throws IOException {
  160. theme.save(out, DEFAULT_XML_OPTIONS);
  161. }
  162. @Override
  163. protected void commit() throws IOException {
  164. PackagePart part = getPackagePart();
  165. OutputStream out = part.getOutputStream();
  166. writeTo(out);
  167. out.close();
  168. }
  169. }