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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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.spreadsheetml.x2006.main.CTColor;
  25. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFont;
  26. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFontSize;
  27. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTUnderlineProperty;
  28. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTVerticalAlignFontProperty;
  29. import org.openxmlformats.schemas.spreadsheetml.x2006.main.STUnderlineValues;
  30. import org.openxmlformats.schemas.spreadsheetml.x2006.main.STVerticalAlignRun;
  31. /**
  32. * @author Yegor Kozlov
  33. */
  34. public class XSSFFontFormatting implements FontFormatting {
  35. private IndexedColorMap _colorMap;
  36. private CTFont _font;
  37. /*package*/ XSSFFontFormatting(CTFont font, IndexedColorMap colorMap) {
  38. _font = font;
  39. _colorMap = colorMap;
  40. }
  41. /**
  42. * get the type of super or subscript for the font
  43. *
  44. * @return super or subscript option
  45. * @see org.apache.poi.ss.usermodel.Font#SS_NONE
  46. * @see org.apache.poi.ss.usermodel.Font#SS_SUPER
  47. * @see org.apache.poi.ss.usermodel.Font#SS_SUB
  48. */
  49. @Override
  50. public short getEscapementType(){
  51. if(_font.sizeOfVertAlignArray() == 0) return org.apache.poi.ss.usermodel.Font.SS_NONE;
  52. CTVerticalAlignFontProperty prop = _font.getVertAlignArray(0);
  53. return (short)(prop.getVal().intValue() - 1);
  54. }
  55. /**
  56. * set the escapement type for the font
  57. *
  58. * @param escapementType super or subscript option
  59. * @see org.apache.poi.ss.usermodel.Font#SS_NONE
  60. * @see org.apache.poi.ss.usermodel.Font#SS_SUPER
  61. * @see org.apache.poi.ss.usermodel.Font#SS_SUB
  62. */
  63. @Override
  64. public void setEscapementType(short escapementType){
  65. _font.setVertAlignArray(null);
  66. if(escapementType != org.apache.poi.ss.usermodel.Font.SS_NONE){
  67. _font.addNewVertAlign().setVal(STVerticalAlignRun.Enum.forInt(escapementType + 1));
  68. }
  69. }
  70. /**
  71. * XMLBeans and the XSD make this look like it can have multiple values, but it is maxOccurrs=1.
  72. * Use get*Array(), it is much faster than get*List().
  73. *
  74. * @see org.apache.poi.ss.usermodel.FontFormatting#isStruckout()
  75. */
  76. @Override
  77. public boolean isStruckout() {
  78. return _font.sizeOfStrikeArray() > 0 && _font.getStrikeArray(0).getVal();
  79. }
  80. /**
  81. * @return font color index
  82. */
  83. @Override
  84. public short getFontColorIndex(){
  85. if(_font.sizeOfColorArray() == 0) return -1;
  86. int idx = 0;
  87. CTColor color = _font.getColorArray(0);
  88. if(color.isSetIndexed()) idx = (int)color.getIndexed();
  89. return (short)idx;
  90. }
  91. /**
  92. * @param color font color index
  93. */
  94. @Override
  95. public void setFontColorIndex(short color){
  96. _font.setColorArray(null);
  97. if(color != -1){
  98. _font.addNewColor().setIndexed(color);
  99. }
  100. }
  101. @Override
  102. public XSSFColor getFontColor() {
  103. if(_font.sizeOfColorArray() == 0) return null;
  104. return XSSFColor.from(_font.getColorArray(0), _colorMap);
  105. }
  106. @Override
  107. public void setFontColor(Color color) {
  108. XSSFColor xcolor = XSSFColor.toXSSFColor(color);
  109. if (xcolor == null) {
  110. _font.getColorList().clear();
  111. } else if(_font.sizeOfColorArray() == 0) {
  112. _font.addNewColor().setRgb(xcolor.getRGB());
  113. } else {
  114. _font.setColorArray(0, xcolor.getCTColor());
  115. }
  116. }
  117. /**
  118. * gets the height of the font in 1/20th point units
  119. *
  120. * @return fontheight (in points/20); or -1 if not modified
  121. */
  122. @Override
  123. public int getFontHeight(){
  124. if(_font.sizeOfSzArray() == 0) return -1;
  125. CTFontSize sz = _font.getSzArray(0);
  126. return (int)(20*sz.getVal());
  127. }
  128. /**
  129. * Sets the height of the font in 1/20th point units
  130. *
  131. * @param height the height in twips (in points/20)
  132. */
  133. @Override
  134. public void setFontHeight(int height){
  135. _font.setSzArray(null);
  136. if(height != -1){
  137. _font.addNewSz().setVal((double)height / 20);
  138. }
  139. }
  140. /**
  141. * get the type of underlining for the font
  142. *
  143. * @return font underlining type
  144. *
  145. * @see Font#U_NONE
  146. * @see Font#U_SINGLE
  147. * @see Font#U_DOUBLE
  148. * @see Font#U_SINGLE_ACCOUNTING
  149. * @see Font#U_DOUBLE_ACCOUNTING
  150. */
  151. @Override
  152. public short getUnderlineType(){
  153. if(_font.sizeOfUArray() == 0) return Font.U_NONE;
  154. CTUnderlineProperty u = _font.getUArray(0);
  155. switch(u.getVal().intValue()){
  156. case STUnderlineValues.INT_SINGLE: return Font.U_SINGLE;
  157. case STUnderlineValues.INT_DOUBLE: return Font.U_DOUBLE;
  158. case STUnderlineValues.INT_SINGLE_ACCOUNTING: return Font.U_SINGLE_ACCOUNTING;
  159. case STUnderlineValues.INT_DOUBLE_ACCOUNTING: return Font.U_DOUBLE_ACCOUNTING;
  160. default: return Font.U_NONE;
  161. }
  162. }
  163. /**
  164. * set the type of underlining type for the font
  165. *
  166. * @param underlineType super or subscript option
  167. *
  168. * @see Font#U_NONE
  169. * @see Font#U_SINGLE
  170. * @see Font#U_DOUBLE
  171. * @see Font#U_SINGLE_ACCOUNTING
  172. * @see Font#U_DOUBLE_ACCOUNTING
  173. */
  174. @Override
  175. public void setUnderlineType(short underlineType){
  176. _font.setUArray(null);
  177. if(underlineType != Font.U_NONE){
  178. FontUnderline fenum = FontUnderline.valueOf(underlineType);
  179. STUnderlineValues.Enum val = STUnderlineValues.Enum.forInt(fenum.getValue());
  180. _font.addNewU().setVal(val);
  181. }
  182. }
  183. /**
  184. * get whether the font weight is set to bold or not
  185. *
  186. * @return bold - whether the font is bold or not
  187. */
  188. @Override
  189. public boolean isBold(){
  190. return _font.sizeOfBArray() == 1 && _font.getBArray(0).getVal();
  191. }
  192. /**
  193. * @return true if font style was set to <i>italic</i>
  194. */
  195. @Override
  196. public boolean isItalic(){
  197. return _font.sizeOfIArray() == 1 && _font.getIArray(0).getVal();
  198. }
  199. /**
  200. * set font style options.
  201. *
  202. * @param italic - if true, set posture style to italic, otherwise to normal
  203. * @param bold if true, set font weight to bold, otherwise to normal
  204. */
  205. @Override
  206. public void setFontStyle(boolean italic, boolean bold){
  207. _font.setIArray(null);
  208. _font.setBArray(null);
  209. if(italic) _font.addNewI().setVal(true);
  210. if(bold) _font.addNewB().setVal(true);
  211. }
  212. /**
  213. * set font style options to default values (non-italic, non-bold)
  214. */
  215. @Override
  216. public void resetFontStyle(){
  217. _font.set(CTFont.Factory.newInstance());
  218. }
  219. }