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

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.Color;
  21. import org.apache.poi.ss.usermodel.Font;
  22. import org.apache.poi.ss.usermodel.FontFormatting;
  23. import org.apache.poi.ss.usermodel.FontUnderline;
  24. import org.openxmlformats.schemas.officeDocument.x2006.sharedTypes.STVerticalAlignRun;
  25. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor;
  26. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFont;
  27. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFontSize;
  28. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTUnderlineProperty;
  29. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTVerticalAlignFontProperty;
  30. import org.openxmlformats.schemas.spreadsheetml.x2006.main.STUnderlineValues;
  31. public class XSSFFontFormatting implements FontFormatting {
  32. private IndexedColorMap _colorMap;
  33. private CTFont _font;
  34. /*package*/ XSSFFontFormatting(CTFont font, IndexedColorMap colorMap) {
  35. _font = font;
  36. _colorMap = colorMap;
  37. }
  38. /**
  39. * get the type of super or subscript for the font
  40. *
  41. * @return super or subscript option
  42. * @see org.apache.poi.ss.usermodel.Font#SS_NONE
  43. * @see org.apache.poi.ss.usermodel.Font#SS_SUPER
  44. * @see org.apache.poi.ss.usermodel.Font#SS_SUB
  45. */
  46. @Override
  47. public short getEscapementType(){
  48. if(_font.sizeOfVertAlignArray() == 0) return org.apache.poi.ss.usermodel.Font.SS_NONE;
  49. CTVerticalAlignFontProperty prop = _font.getVertAlignArray(0);
  50. return (short)(prop.getVal().intValue() - 1);
  51. }
  52. /**
  53. * set the escapement type for the font
  54. *
  55. * @param escapementType super or subscript option
  56. * @see org.apache.poi.ss.usermodel.Font#SS_NONE
  57. * @see org.apache.poi.ss.usermodel.Font#SS_SUPER
  58. * @see org.apache.poi.ss.usermodel.Font#SS_SUB
  59. */
  60. @Override
  61. public void setEscapementType(short escapementType){
  62. _font.setVertAlignArray(null);
  63. if(escapementType != org.apache.poi.ss.usermodel.Font.SS_NONE){
  64. _font.addNewVertAlign().setVal(STVerticalAlignRun.Enum.forInt(escapementType + 1));
  65. }
  66. }
  67. /**
  68. * XMLBeans and the XSD make this look like it can have multiple values, but it is maxOccurrs=1.
  69. * Use get*Array(), it is much faster than get*List().
  70. *
  71. * @see org.apache.poi.ss.usermodel.FontFormatting#isStruckout()
  72. */
  73. @Override
  74. public boolean isStruckout() {
  75. return _font.sizeOfStrikeArray() > 0 && _font.getStrikeArray(0).getVal();
  76. }
  77. /**
  78. * @return font color index
  79. */
  80. @Override
  81. public short getFontColorIndex(){
  82. if(_font.sizeOfColorArray() == 0) return -1;
  83. int idx = 0;
  84. CTColor color = _font.getColorArray(0);
  85. if(color.isSetIndexed()) idx = (int)color.getIndexed();
  86. return (short)idx;
  87. }
  88. /**
  89. * @param color font color index
  90. */
  91. @Override
  92. public void setFontColorIndex(short color){
  93. _font.setColorArray(null);
  94. if(color != -1){
  95. _font.addNewColor().setIndexed(color);
  96. }
  97. }
  98. @Override
  99. public XSSFColor getFontColor() {
  100. if(_font.sizeOfColorArray() == 0) return null;
  101. return XSSFColor.from(_font.getColorArray(0), _colorMap);
  102. }
  103. @Override
  104. public void setFontColor(Color color) {
  105. XSSFColor xcolor = XSSFColor.toXSSFColor(color);
  106. if (xcolor == null) {
  107. _font.getColorList().clear();
  108. } else if(_font.sizeOfColorArray() == 0) {
  109. _font.addNewColor().setRgb(xcolor.getRGB());
  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 (int)(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 Font#U_NONE
  143. * @see Font#U_SINGLE
  144. * @see Font#U_DOUBLE
  145. * @see Font#U_SINGLE_ACCOUNTING
  146. * @see Font#U_DOUBLE_ACCOUNTING
  147. */
  148. @Override
  149. public short getUnderlineType(){
  150. if(_font.sizeOfUArray() == 0) return Font.U_NONE;
  151. CTUnderlineProperty u = _font.getUArray(0);
  152. switch(u.getVal().intValue()){
  153. case STUnderlineValues.INT_SINGLE: return Font.U_SINGLE;
  154. case STUnderlineValues.INT_DOUBLE: return Font.U_DOUBLE;
  155. case STUnderlineValues.INT_SINGLE_ACCOUNTING: return Font.U_SINGLE_ACCOUNTING;
  156. case STUnderlineValues.INT_DOUBLE_ACCOUNTING: return Font.U_DOUBLE_ACCOUNTING;
  157. default: return Font.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 Font#U_NONE
  166. * @see Font#U_SINGLE
  167. * @see Font#U_DOUBLE
  168. * @see Font#U_SINGLE_ACCOUNTING
  169. * @see Font#U_DOUBLE_ACCOUNTING
  170. */
  171. @Override
  172. public void setUnderlineType(short underlineType){
  173. _font.setUArray(null);
  174. if(underlineType != Font.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. }