PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/ppt/poi/org/apache/poi/hssf/usermodel/HSSFFont.java

https://github.com/minstrelsy/POI-Android
Java | 360 lines | 145 code | 54 blank | 161 comment | 17 complexity | 35899c7f55dd9c9576f58aaede24eb00 MD5 | raw file
  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.hssf.usermodel;
  16. import org.apache.poi.hssf.record.FontRecord;
  17. import org.apache.poi.hssf.util.HSSFColor;
  18. import org.apache.poi.ss.usermodel.Font;
  19. /**
  20. * Represents a Font used in a workbook.
  21. *
  22. *
  23. * @author Andrew C. Oliver
  24. * @see org.apache.poi.hssf.usermodel.HSSFWorkbook#createFont()
  25. * @see org.apache.poi.hssf.usermodel.HSSFWorkbook#getFontAt(short)
  26. * @see org.apache.poi.hssf.usermodel.HSSFCellStyle#setFont(HSSFFont)
  27. */
  28. public final class HSSFFont implements Font {
  29. /**
  30. * Arial font
  31. */
  32. public final static String FONT_ARIAL = "Arial";
  33. private FontRecord font;
  34. private short index;
  35. /** Creates a new instance of HSSFFont */
  36. protected HSSFFont(short index, FontRecord rec)
  37. {
  38. font = rec;
  39. this.index = index;
  40. }
  41. /**
  42. * set the name for the font (i.e. Arial)
  43. * @param name String representing the name of the font to use
  44. * @see #FONT_ARIAL
  45. */
  46. public void setFontName(String name)
  47. {
  48. font.setFontName(name);
  49. }
  50. /**
  51. * get the name for the font (i.e. Arial)
  52. * @return String representing the name of the font to use
  53. * @see #FONT_ARIAL
  54. */
  55. public String getFontName()
  56. {
  57. return font.getFontName();
  58. }
  59. /**
  60. * get the index within the HSSFWorkbook (sequence within the collection of Font objects)
  61. * @return unique index number of the underlying record this Font represents (probably you don't care
  62. * unless you're comparing which one is which)
  63. */
  64. public short getIndex()
  65. {
  66. return index;
  67. }
  68. /**
  69. * set the font height in unit's of 1/20th of a point. Maybe you might want to
  70. * use the setFontHeightInPoints which matches to the familiar 10, 12, 14 etc..
  71. * @param height height in 1/20ths of a point
  72. * @see #setFontHeightInPoints(short)
  73. */
  74. public void setFontHeight(short height)
  75. {
  76. font.setFontHeight(height);
  77. }
  78. /**
  79. * set the font height
  80. * @param height height in the familiar unit of measure - points
  81. * @see #setFontHeight(short)
  82. */
  83. public void setFontHeightInPoints(short height)
  84. {
  85. font.setFontHeight(( short ) (height * 20));
  86. }
  87. /**
  88. * get the font height in unit's of 1/20th of a point. Maybe you might want to
  89. * use the getFontHeightInPoints which matches to the familiar 10, 12, 14 etc..
  90. * @return short - height in 1/20ths of a point
  91. * @see #getFontHeightInPoints()
  92. */
  93. public short getFontHeight()
  94. {
  95. return font.getFontHeight();
  96. }
  97. /**
  98. * get the font height
  99. * @return short - height in the familiar unit of measure - points
  100. * @see #getFontHeight()
  101. */
  102. public short getFontHeightInPoints()
  103. {
  104. return ( short ) (font.getFontHeight() / 20);
  105. }
  106. /**
  107. * set whether to use italics or not
  108. * @param italic italics or not
  109. */
  110. public void setItalic(boolean italic)
  111. {
  112. font.setItalic(italic);
  113. }
  114. /**
  115. * get whether to use italics or not
  116. * @return italics or not
  117. */
  118. public boolean getItalic()
  119. {
  120. return font.isItalic();
  121. }
  122. /**
  123. * set whether to use a strikeout horizontal line through the text or not
  124. * @param strikeout or not
  125. */
  126. public void setStrikeout(boolean strikeout)
  127. {
  128. font.setStrikeout(strikeout);
  129. }
  130. /**
  131. * get whether to use a strikeout horizontal line through the text or not
  132. * @return strikeout or not
  133. */
  134. public boolean getStrikeout()
  135. {
  136. return font.isStruckout();
  137. }
  138. /**
  139. * set the color for the font
  140. * @param color to use
  141. * @see #COLOR_NORMAL Note: Use this rather than HSSFColor.AUTOMATIC for default font color
  142. * @see #COLOR_RED
  143. */
  144. public void setColor(short color)
  145. {
  146. font.setColorPaletteIndex(color);
  147. }
  148. /**
  149. * get the color for the font
  150. * @return color to use
  151. * @see #COLOR_NORMAL
  152. * @see #COLOR_RED
  153. * @see org.apache.poi.hssf.usermodel.HSSFPalette#getColor(short)
  154. */
  155. public short getColor()
  156. {
  157. return font.getColorPaletteIndex();
  158. }
  159. /**
  160. * get the color value for the font
  161. */
  162. public HSSFColor getHSSFColor(HSSFWorkbook wb)
  163. {
  164. HSSFPalette pallette = wb.getCustomPalette();
  165. return pallette.getColor( getColor() );
  166. }
  167. /**
  168. * set the boldness to use
  169. * @param boldweight
  170. * @see #BOLDWEIGHT_NORMAL
  171. * @see #BOLDWEIGHT_BOLD
  172. */
  173. public void setBoldweight(short boldweight)
  174. {
  175. font.setBoldWeight(boldweight);
  176. }
  177. /**
  178. * get the boldness to use
  179. * @return boldweight
  180. * @see #BOLDWEIGHT_NORMAL
  181. * @see #BOLDWEIGHT_BOLD
  182. */
  183. public short getBoldweight()
  184. {
  185. return font.getBoldWeight();
  186. }
  187. /**
  188. * set normal,super or subscript.
  189. * @param offset type to use (none,super,sub)
  190. * @see #SS_NONE
  191. * @see #SS_SUPER
  192. * @see #SS_SUB
  193. */
  194. public void setTypeOffset(short offset)
  195. {
  196. font.setSuperSubScript(offset);
  197. }
  198. /**
  199. * get normal,super or subscript.
  200. * @return offset type to use (none,super,sub)
  201. * @see #SS_NONE
  202. * @see #SS_SUPER
  203. * @see #SS_SUB
  204. */
  205. public short getTypeOffset()
  206. {
  207. return font.getSuperSubScript();
  208. }
  209. /**
  210. * set type of text underlining to use
  211. * @param underline type
  212. * @see #U_NONE
  213. * @see #U_SINGLE
  214. * @see #U_DOUBLE
  215. * @see #U_SINGLE_ACCOUNTING
  216. * @see #U_DOUBLE_ACCOUNTING
  217. */
  218. public void setUnderline(byte underline)
  219. {
  220. font.setUnderline(underline);
  221. }
  222. /**
  223. * get type of text underlining to use
  224. * @return underlining type
  225. * @see #U_NONE
  226. * @see #U_SINGLE
  227. * @see #U_DOUBLE
  228. * @see #U_SINGLE_ACCOUNTING
  229. * @see #U_DOUBLE_ACCOUNTING
  230. */
  231. public byte getUnderline()
  232. {
  233. return font.getUnderline();
  234. }
  235. /**
  236. * get character-set to use.
  237. * @return character-set
  238. * @see #ANSI_CHARSET
  239. * @see #DEFAULT_CHARSET
  240. * @see #SYMBOL_CHARSET
  241. */
  242. public int getCharSet()
  243. {
  244. byte charset = font.getCharset();
  245. if(charset >= 0) {
  246. return (int)charset;
  247. } else {
  248. return charset + 256;
  249. }
  250. }
  251. /**
  252. * set character-set to use.
  253. * @see #ANSI_CHARSET
  254. * @see #DEFAULT_CHARSET
  255. * @see #SYMBOL_CHARSET
  256. */
  257. public void setCharSet(int charset)
  258. {
  259. byte cs = (byte)charset;
  260. if(charset > 127) {
  261. cs = (byte)(charset-256);
  262. }
  263. setCharSet(cs);
  264. }
  265. /**
  266. * set character-set to use.
  267. * @see #ANSI_CHARSET
  268. * @see #DEFAULT_CHARSET
  269. * @see #SYMBOL_CHARSET
  270. */
  271. public void setCharSet(byte charset)
  272. {
  273. font.setCharset(charset);
  274. }
  275. public String toString()
  276. {
  277. return "org.apache.poi.hssf.usermodel.HSSFFont{" +
  278. font +
  279. "}";
  280. }
  281. public int hashCode() {
  282. final int prime = 31;
  283. int result = 1;
  284. result = prime * result + ((font == null) ? 0 : font.hashCode());
  285. result = prime * result + index;
  286. return result;
  287. }
  288. public boolean equals(Object obj) {
  289. if (this == obj) return true;
  290. if (obj == null) return false;
  291. if (obj instanceof HSSFFont) {
  292. final HSSFFont other = (HSSFFont) obj;
  293. if (font == null) {
  294. if (other.font != null)
  295. return false;
  296. } else if (!font.equals(other.font))
  297. return false;
  298. if (index != other.index)
  299. return false;
  300. return true;
  301. }
  302. return false;
  303. }
  304. }