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.

XSSFFontFormatting.java 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * ====================================================================
  3. * Licensed to the Apache Software Foundation (ASF) under one or more
  4. * contributor license agreements. See the NOTICE file distributed with
  5. * this work for additional information regarding copyright ownership.
  6. * The ASF licenses this file to You under the Apache License, Version 2.0
  7. * (the "License"); you may not use this file except in compliance with
  8. * the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. * ====================================================================
  18. */
  19. package org.apache.poi.xssf.usermodel;
  20. import org.apache.poi.ss.usermodel.*;
  21. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFont;
  22. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTUnderlineProperty;
  23. import org.openxmlformats.schemas.spreadsheetml.x2006.main.STUnderlineValues;
  24. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBooleanProperty;
  25. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor;
  26. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFontSize;
  27. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTVerticalAlignFontProperty;
  28. import org.openxmlformats.schemas.spreadsheetml.x2006.main.STVerticalAlignRun;
  29. /**
  30. * @author Yegor Kozlov
  31. */
  32. public class XSSFFontFormatting implements FontFormatting {
  33. private IndexedColorMap _colorMap;
  34. private CTFont _font;
  35. /*package*/ XSSFFontFormatting(CTFont font, IndexedColorMap colorMap) {
  36. _font = font;
  37. _colorMap = colorMap;
  38. }
  39. /**
  40. * get the type of super or subscript for the font
  41. *
  42. * @return super or subscript option
  43. * @see #SS_NONE
  44. * @see #SS_SUPER
  45. * @see #SS_SUB
  46. */
  47. @Override
  48. public short getEscapementType(){
  49. if(_font.sizeOfVertAlignArray() == 0) return SS_NONE;
  50. CTVerticalAlignFontProperty prop = _font.getVertAlignArray(0);
  51. return (short)(prop.getVal().intValue() - 1);
  52. }
  53. /**
  54. * set the escapement type for the font
  55. *
  56. * @param escapementType super or subscript option
  57. * @see #SS_NONE
  58. * @see #SS_SUPER
  59. * @see #SS_SUB
  60. */
  61. @Override
  62. public void setEscapementType(short escapementType){
  63. _font.setVertAlignArray(null);
  64. if(escapementType != SS_NONE){
  65. _font.addNewVertAlign().setVal(STVerticalAlignRun.Enum.forInt(escapementType + 1));
  66. }
  67. }
  68. /**
  69. * XMLBeans and the XSD make this look like it can have multiple values, but it is maxOccurrs=1.
  70. * Use get*Array(), it is much faster than get*List().
  71. *
  72. * @see org.apache.poi.ss.usermodel.FontFormatting#isStruckout()
  73. */
  74. @Override
  75. public boolean isStruckout() {
  76. for (CTBooleanProperty bProp : _font.getStrikeArray()) return bProp.getVal();
  77. return false;
  78. }
  79. /**
  80. * @return font color index
  81. */
  82. @Override
  83. public short getFontColorIndex(){
  84. if(_font.sizeOfColorArray() == 0) return -1;
  85. int idx = 0;
  86. CTColor color = _font.getColorArray(0);
  87. if(color.isSetIndexed()) idx = (int)color.getIndexed();
  88. return (short)idx;
  89. }
  90. /**
  91. * @param color font color index
  92. */
  93. @Override
  94. public void setFontColorIndex(short color){
  95. _font.setColorArray(null);
  96. if(color != -1){
  97. _font.addNewColor().setIndexed(color);
  98. }
  99. }
  100. @Override
  101. public XSSFColor getFontColor() {
  102. if(_font.sizeOfColorArray() == 0) return null;
  103. return new XSSFColor(_font.getColorArray(0), _colorMap);
  104. }
  105. @Override
  106. public void setFontColor(Color color) {
  107. XSSFColor xcolor = XSSFColor.toXSSFColor(color);
  108. if (xcolor == null) {
  109. _font.getColorList().clear();
  110. } else {
  111. _font.setColorArray(0, xcolor.getCTColor());
  112. }
  113. }
  114. /**
  115. * gets the height of the font in 1/20th point units
  116. *
  117. * @return fontheight (in points/20); or -1 if not modified
  118. */
  119. @Override
  120. public int getFontHeight(){
  121. if(_font.sizeOfSzArray() == 0) return -1;
  122. CTFontSize sz = _font.getSzArray(0);
  123. return (short)(20*sz.getVal());
  124. }
  125. /**
  126. * Sets the height of the font in 1/20th point units
  127. *
  128. * @param height the height in twips (in points/20)
  129. */
  130. @Override
  131. public void setFontHeight(int height){
  132. _font.setSzArray(null);
  133. if(height != -1){
  134. _font.addNewSz().setVal((double)height / 20);
  135. }
  136. }
  137. /**
  138. * get the type of underlining for the font
  139. *
  140. * @return font underlining type
  141. *
  142. * @see #U_NONE
  143. * @see #U_SINGLE
  144. * @see #U_DOUBLE
  145. * @see #U_SINGLE_ACCOUNTING
  146. * @see #U_DOUBLE_ACCOUNTING
  147. */
  148. @Override
  149. public short getUnderlineType(){
  150. if(_font.sizeOfUArray() == 0) return U_NONE;
  151. CTUnderlineProperty u = _font.getUArray(0);
  152. switch(u.getVal().intValue()){
  153. case STUnderlineValues.INT_SINGLE: return U_SINGLE;
  154. case STUnderlineValues.INT_DOUBLE: return U_DOUBLE;
  155. case STUnderlineValues.INT_SINGLE_ACCOUNTING: return U_SINGLE_ACCOUNTING;
  156. case STUnderlineValues.INT_DOUBLE_ACCOUNTING: return U_DOUBLE_ACCOUNTING;
  157. default: return U_NONE;
  158. }
  159. }
  160. /**
  161. * set the type of underlining type for the font
  162. *
  163. * @param underlineType super or subscript option
  164. *
  165. * @see #U_NONE
  166. * @see #U_SINGLE
  167. * @see #U_DOUBLE
  168. * @see #U_SINGLE_ACCOUNTING
  169. * @see #U_DOUBLE_ACCOUNTING
  170. */
  171. @Override
  172. public void setUnderlineType(short underlineType){
  173. _font.setUArray(null);
  174. if(underlineType != U_NONE){
  175. FontUnderline fenum = FontUnderline.valueOf(underlineType);
  176. STUnderlineValues.Enum val = STUnderlineValues.Enum.forInt(fenum.getValue());
  177. _font.addNewU().setVal(val);
  178. }
  179. }
  180. /**
  181. * get whether the font weight is set to bold or not
  182. *
  183. * @return bold - whether the font is bold or not
  184. */
  185. @Override
  186. public boolean isBold(){
  187. return _font.sizeOfBArray() == 1 && _font.getBArray(0).getVal();
  188. }
  189. /**
  190. * @return true if font style was set to <i>italic</i>
  191. */
  192. @Override
  193. public boolean isItalic(){
  194. return _font.sizeOfIArray() == 1 && _font.getIArray(0).getVal();
  195. }
  196. /**
  197. * set font style options.
  198. *
  199. * @param italic - if true, set posture style to italic, otherwise to normal
  200. * @param bold if true, set font weight to bold, otherwise to normal
  201. */
  202. @Override
  203. public void setFontStyle(boolean italic, boolean bold){
  204. _font.setIArray(null);
  205. _font.setBArray(null);
  206. if(italic) _font.addNewI().setVal(true);
  207. if(bold) _font.addNewB().setVal(true);
  208. }
  209. /**
  210. * set font style options to default values (non-italic, non-bold)
  211. */
  212. @Override
  213. public void resetFontStyle(){
  214. _font.set(CTFont.Factory.newInstance());
  215. }
  216. }