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.

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