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 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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.CTSchemeColor;
  25. import org.openxmlformats.schemas.drawingml.x2006.main.CTBaseStyles;
  26. import org.openxmlformats.schemas.drawingml.x2006.main.CTColor;
  27. import org.openxmlformats.schemas.drawingml.x2006.main.CTColorScheme;
  28. import org.openxmlformats.schemas.drawingml.x2006.main.ThemeDocument;
  29. import org.openxmlformats.schemas.drawingml.x2006.main.CTOfficeStyleSheet;
  30. import org.openxmlformats.schemas.drawingml.x2006.main.CTSRgbColor;
  31. import org.openxmlformats.schemas.drawingml.x2006.main.CTSolidColorFillProperties;
  32. import org.openxmlformats.schemas.drawingml.x2006.main.CTPresetColor;
  33. import javax.xml.namespace.QName;
  34. import java.awt.Color;
  35. import java.io.IOException;
  36. import java.io.OutputStream;
  37. import java.util.Map;
  38. import java.util.HashMap;
  39. @Beta
  40. public class XSLFTheme extends POIXMLDocumentPart {
  41. private CTOfficeStyleSheet _theme;
  42. private Map<String, XSLFColor> _schemeColors;
  43. XSLFTheme() {
  44. super();
  45. _theme = CTOfficeStyleSheet.Factory.newInstance();
  46. }
  47. public XSLFTheme(PackagePart part, PackageRelationship rel) throws IOException, XmlException {
  48. super(part, rel);
  49. ThemeDocument doc =
  50. ThemeDocument.Factory.parse(getPackagePart().getInputStream());
  51. _theme = doc.getTheme();
  52. initialize();
  53. }
  54. private void initialize(){
  55. CTBaseStyles elems = _theme.getThemeElements();
  56. CTColorScheme scheme = elems.getClrScheme();
  57. // The color scheme is responsible for defining a list of twelve colors.
  58. _schemeColors = new HashMap<String, XSLFColor>(12);
  59. for(XmlObject o : scheme.selectPath("*")){
  60. CTColor c = (CTColor)o;
  61. String name = c.getDomNode().getLocalName();
  62. _schemeColors.put(name, new XSLFColor(c));
  63. }
  64. _schemeColors.put("bg1", _schemeColors.get("lt1"));
  65. _schemeColors.put("bg2", _schemeColors.get("lt2"));
  66. _schemeColors.put("tx1", _schemeColors.get("dk1"));
  67. _schemeColors.put("tx2", _schemeColors.get("dk2"));
  68. }
  69. public String getName(){
  70. return _theme.getName();
  71. }
  72. public void setName(String name){
  73. _theme.setName(name);
  74. }
  75. /**
  76. * Get a color from the theme's color scheme by name
  77. *
  78. * @return a theme color or <code>null</code> if not found
  79. */
  80. public XSLFColor getColor(String name){
  81. return _schemeColors.get(name);
  82. }
  83. Color getSchemeColor(CTSchemeColor schemeColor){
  84. String colorRef = schemeColor.getVal().toString();
  85. int alpha = 0xFF;
  86. if(schemeColor.sizeOfAlphaArray() > 0){
  87. int aval = schemeColor.getAlphaArray(0).getVal();
  88. alpha = Math.round(255 * aval / 100000f);
  89. }
  90. Color themeColor = _schemeColors.get(colorRef).getColor(alpha);
  91. int lumMod = 100, lumOff = 0;
  92. if (schemeColor.sizeOfLumModArray() > 0) {
  93. lumMod = schemeColor.getLumModArray(0).getVal() / 1000;
  94. }
  95. if (schemeColor.sizeOfLumOffArray() > 0) {
  96. lumOff = schemeColor.getLumOffArray(0).getVal() / 1000;
  97. }
  98. if(schemeColor.sizeOfShadeArray() > 0) {
  99. lumMod = schemeColor.getShadeArray(0).getVal() / 1000;
  100. }
  101. Color color = modulateLuminanace(themeColor, lumMod, lumOff);
  102. if(schemeColor.sizeOfTintArray() > 0) {
  103. float tint = schemeColor.getTintArray(0).getVal() / 100000f;
  104. int red = Math.round(tint * themeColor.getRed() + (1 - tint) * 255);
  105. int green = Math.round(tint * themeColor.getGreen() + (1 - tint) * 255);
  106. int blue = Math.round(tint * themeColor.getBlue() + (1 - tint) * 255);
  107. color = new Color(red, green, blue);
  108. }
  109. return color;
  110. }
  111. /**
  112. * TODO get rid of code duplication. Re-write to use xpath instead of beans
  113. */
  114. Color getPresetColor(CTPresetColor presetColor){
  115. String colorName = presetColor.getVal().toString();
  116. Color color;
  117. try {
  118. color = (Color)Color.class.getField(colorName).get(null);
  119. } catch (Exception e){
  120. color = Color.black;
  121. }
  122. if(presetColor.sizeOfAlphaArray() > 0){
  123. int aval = presetColor.getAlphaArray(0).getVal();
  124. int alpha = Math.round(255 * aval / 100000f);
  125. color = new Color(color.getRed(), color.getGreen(), color.getBlue(), alpha);
  126. }
  127. int lumMod = 100, lumOff = 0;
  128. if (presetColor.sizeOfLumModArray() > 0) {
  129. lumMod = presetColor.getLumModArray(0).getVal() / 1000;
  130. }
  131. if (presetColor.sizeOfLumOffArray() > 0) {
  132. lumOff = presetColor.getLumOffArray(0).getVal() / 1000;
  133. }
  134. if(presetColor.sizeOfShadeArray() > 0) {
  135. lumMod = presetColor.getShadeArray(0).getVal() / 1000;
  136. }
  137. color = modulateLuminanace(color, lumMod, lumOff);
  138. if(presetColor.sizeOfTintArray() > 0) {
  139. float tint = presetColor.getTintArray(0).getVal() / 100000f;
  140. int red = Math.round(tint * color.getRed() + (1 - tint) * 255);
  141. int green = Math.round(tint * color.getGreen() + (1 - tint) * 255);
  142. int blue = Math.round(tint * color.getBlue() + (1 - tint) * 255);
  143. color = new Color(red, green, blue);
  144. }
  145. return color;
  146. }
  147. public Color brighter(Color color, double tint) {
  148. int r = color.getRed();
  149. int g = color.getGreen();
  150. int b = color.getBlue();
  151. /* From 2D group:
  152. * 1. black.brighter() should return grey
  153. * 2. applying brighter to blue will always return blue, brighter
  154. * 3. non pure color (non zero rgb) will eventually return white
  155. */
  156. int i = (int)(1.0/(1.0-tint));
  157. if ( r == 0 && g == 0 && b == 0) {
  158. return new Color(i, i, i);
  159. }
  160. if ( r > 0 && r < i ) r = i;
  161. if ( g > 0 && g < i ) g = i;
  162. if ( b > 0 && b < i ) b = i;
  163. return new Color(Math.min((int)(r/tint), 255),
  164. Math.min((int)(g/tint), 255),
  165. Math.min((int)(b/tint), 255));
  166. }
  167. Color getSrgbColor(CTSRgbColor srgb){
  168. byte[] val = srgb.getVal();
  169. int alpha = 0xFF;
  170. if(srgb.sizeOfAlphaArray() > 0){
  171. int aval = srgb.getAlphaArray(0).getVal();
  172. alpha = Math.round(255 * aval / 100000f);
  173. }
  174. return new Color(0xFF & val[0], 0xFF & val[1], 0xFF & val[2], alpha);
  175. }
  176. Color getSolidFillColor(CTSolidColorFillProperties solidFill){
  177. Color color;
  178. if (solidFill.isSetSrgbClr()) {
  179. color = getSrgbColor(solidFill.getSrgbClr());
  180. } else if (solidFill.isSetSchemeClr()) {
  181. color = getSchemeColor(solidFill.getSchemeClr());
  182. } else {
  183. // TODO support other types
  184. color = Color.black;
  185. }
  186. return color;
  187. }
  188. Color getColor(CTColor solidFill){
  189. Color color;
  190. if (solidFill.isSetSrgbClr()) {
  191. color = getSrgbColor(solidFill.getSrgbClr());
  192. } else if (solidFill.isSetSchemeClr()) {
  193. color = getSchemeColor(solidFill.getSchemeClr());
  194. } else {
  195. // TODO support other types
  196. color = Color.black;
  197. }
  198. return color;
  199. }
  200. /**
  201. * While developing only!
  202. */
  203. @Internal
  204. public CTOfficeStyleSheet getXmlObject() {
  205. return _theme;
  206. }
  207. protected final void commit() throws IOException {
  208. XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS);
  209. Map<String, String> map = new HashMap<String, String>();
  210. map.put("http://schemas.openxmlformats.org/drawingml/2006/main", "a");
  211. xmlOptions.setSaveSuggestedPrefixes(map);
  212. xmlOptions.setSaveSyntheticDocumentElement(
  213. new QName("http://schemas.openxmlformats.org/drawingml/2006/main", "theme"));
  214. PackagePart part = getPackagePart();
  215. OutputStream out = part.getOutputStream();
  216. getXmlObject().save(out, xmlOptions);
  217. out.close();
  218. }
  219. public static Color modulateLuminanace(Color c, int lumMod, int lumOff) {
  220. Color color;
  221. if (lumOff > 0) {
  222. color = new Color(
  223. (int) (Math.round((255 - c.getRed()) * (100.0 - lumMod) / 100.0 + c.getRed())),
  224. (int) (Math.round((255 - c.getGreen()) * lumOff / 100.0 + c.getGreen())),
  225. (int) (Math.round((255 - c.getBlue()) * lumOff / 100.0 + c.getBlue())),
  226. c.getAlpha()
  227. );
  228. } else {
  229. color = new Color(
  230. (int) (Math.round(c.getRed() * lumMod / 100.0)),
  231. (int) (Math.round(c.getGreen() * lumMod / 100.0)),
  232. (int) (Math.round(c.getBlue() * lumMod / 100.0)),
  233. c.getAlpha()
  234. );
  235. }
  236. return color;
  237. }
  238. public String getMajorFont(){
  239. return _theme.getThemeElements().getFontScheme().getMajorFont().getLatin().getTypeface();
  240. }
  241. public String getMinorFont(){
  242. return _theme.getThemeElements().getFontScheme().getMinorFont().getLatin().getTypeface();
  243. }
  244. }