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.

HSSFFontFormatting.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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.CFRuleBase;
  17. import org.apache.poi.hssf.record.cf.FontFormatting;
  18. import org.apache.poi.hssf.util.HSSFColor;
  19. import org.apache.poi.ss.usermodel.Color;
  20. import org.apache.poi.util.Removal;
  21. /**
  22. * High level representation for Font Formatting component
  23. * of Conditional Formatting settings
  24. */
  25. @SuppressWarnings("unused")
  26. public final class HSSFFontFormatting implements org.apache.poi.ss.usermodel.FontFormatting {
  27. /**
  28. * Underline type - None
  29. * @deprecated use {@link org.apache.poi.ss.usermodel.Font#U_NONE}
  30. */
  31. @Deprecated
  32. @Removal(version = "5.0.0")
  33. public final static byte U_NONE = org.apache.poi.ss.usermodel.Font.U_NONE;
  34. /**
  35. * Underline type - Single
  36. * @deprecated use {@link org.apache.poi.ss.usermodel.Font#U_SINGLE}
  37. */
  38. @Deprecated
  39. @Removal(version = "5.0.0")
  40. public final static byte U_SINGLE = org.apache.poi.ss.usermodel.Font.U_SINGLE;
  41. /**
  42. * Underline type - Double
  43. * @deprecated use {@link org.apache.poi.ss.usermodel.Font#U_DOUBLE}
  44. */
  45. @Deprecated
  46. @Removal(version = "5.0.0")
  47. public final static byte U_DOUBLE = org.apache.poi.ss.usermodel.Font.U_DOUBLE;
  48. /**
  49. * Underline type - Single Accounting
  50. * @deprecated use {@link org.apache.poi.ss.usermodel.Font#U_SINGLE_ACCOUNTING}
  51. */
  52. @Deprecated
  53. @Removal(version = "5.0.0")
  54. public final static byte U_SINGLE_ACCOUNTING = org.apache.poi.ss.usermodel.Font.U_SINGLE_ACCOUNTING;
  55. /**
  56. * Underline type - Double Accounting
  57. * @deprecated use {@link org.apache.poi.ss.usermodel.Font#U_DOUBLE_ACCOUNTING}
  58. */
  59. @Deprecated
  60. @Removal(version = "5.0.0")
  61. public final static byte U_DOUBLE_ACCOUNTING = org.apache.poi.ss.usermodel.Font.U_DOUBLE_ACCOUNTING;
  62. private final FontFormatting fontFormatting;
  63. private final HSSFWorkbook workbook;
  64. HSSFFontFormatting(CFRuleBase cfRuleRecord, HSSFWorkbook workbook) {
  65. this.fontFormatting = cfRuleRecord.getFontFormatting();
  66. this.workbook = workbook;
  67. }
  68. FontFormatting getFontFormattingBlock() {
  69. return fontFormatting;
  70. }
  71. /**
  72. * get the type of super or subscript for the font
  73. *
  74. * @return super or subscript option
  75. * @see org.apache.poi.ss.usermodel.Font#SS_NONE
  76. * @see org.apache.poi.ss.usermodel.Font#SS_SUPER
  77. * @see org.apache.poi.ss.usermodel.Font#SS_SUB
  78. */
  79. public short getEscapementType()
  80. {
  81. return fontFormatting.getEscapementType();
  82. }
  83. /**
  84. * @return font color index
  85. */
  86. public short getFontColorIndex()
  87. {
  88. return fontFormatting.getFontColorIndex();
  89. }
  90. public HSSFColor getFontColor() {
  91. return workbook.getCustomPalette().getColor(
  92. getFontColorIndex()
  93. );
  94. }
  95. public void setFontColor(Color color) {
  96. HSSFColor hcolor = HSSFColor.toHSSFColor(color);
  97. if (hcolor == null) {
  98. fontFormatting.setFontColorIndex((short)0);
  99. } else {
  100. fontFormatting.setFontColorIndex(hcolor.getIndex());
  101. }
  102. }
  103. /**
  104. * gets the height of the font in 1/20th point units
  105. *
  106. * @return fontheight (in points/20); or -1 if not modified
  107. */
  108. public int getFontHeight() {
  109. return fontFormatting.getFontHeight();
  110. }
  111. /**
  112. * get the font weight for this font (100-1000dec or 0x64-0x3e8). Default is
  113. * 0x190 for normal and 0x2bc for bold
  114. *
  115. * @return bw - a number between 100-1000 for the fonts "boldness"
  116. */
  117. public short getFontWeight() {
  118. return fontFormatting.getFontWeight();
  119. }
  120. /**
  121. * @see org.apache.poi.hssf.record.cf.FontFormatting#getRawRecord()
  122. */
  123. byte[] getRawRecord() {
  124. return fontFormatting.getRawRecord();
  125. }
  126. /**
  127. * get the type of underlining for the font
  128. *
  129. * @return font underlining type
  130. *
  131. * @see org.apache.poi.ss.usermodel.Font#U_NONE
  132. * @see org.apache.poi.ss.usermodel.Font#U_SINGLE
  133. * @see org.apache.poi.ss.usermodel.Font#U_DOUBLE
  134. * @see org.apache.poi.ss.usermodel.Font#U_SINGLE_ACCOUNTING
  135. * @see org.apache.poi.ss.usermodel.Font#U_DOUBLE_ACCOUNTING
  136. */
  137. public short getUnderlineType()
  138. {
  139. return fontFormatting.getUnderlineType();
  140. }
  141. /**
  142. * get whether the font weight is set to bold or not
  143. *
  144. * @return bold - whether the font is bold or not
  145. */
  146. public boolean isBold()
  147. {
  148. return fontFormatting.isFontWeightModified() && fontFormatting.isBold();
  149. }
  150. /**
  151. * @return true if escapement type was modified from default
  152. */
  153. public boolean isEscapementTypeModified()
  154. {
  155. return fontFormatting.isEscapementTypeModified();
  156. }
  157. /**
  158. * @return true if font cancellation was modified from default
  159. */
  160. public boolean isFontCancellationModified()
  161. {
  162. return fontFormatting.isFontCancellationModified();
  163. }
  164. /**
  165. * @return true if font outline type was modified from default
  166. */
  167. public boolean isFontOutlineModified()
  168. {
  169. return fontFormatting.isFontOutlineModified();
  170. }
  171. /**
  172. * @return true if font shadow type was modified from default
  173. */
  174. public boolean isFontShadowModified()
  175. {
  176. return fontFormatting.isFontShadowModified();
  177. }
  178. /**
  179. * @return true if font style was modified from default
  180. */
  181. public boolean isFontStyleModified()
  182. {
  183. return fontFormatting.isFontStyleModified();
  184. }
  185. /**
  186. * @return true if font style was set to <i>italic</i>
  187. */
  188. public boolean isItalic()
  189. {
  190. return fontFormatting.isFontStyleModified() && fontFormatting.isItalic();
  191. }
  192. /**
  193. * @return true if font outline is on
  194. */
  195. public boolean isOutlineOn()
  196. {
  197. return fontFormatting.isFontOutlineModified() && fontFormatting.isOutlineOn();
  198. }
  199. /**
  200. * @return true if font shadow is on
  201. */
  202. public boolean isShadowOn()
  203. {
  204. return fontFormatting.isFontOutlineModified() && fontFormatting.isShadowOn();
  205. }
  206. /**
  207. * @return true if font strikeout is on
  208. */
  209. public boolean isStruckout()
  210. {
  211. return fontFormatting.isFontCancellationModified() && fontFormatting.isStruckout();
  212. }
  213. /**
  214. * @return true if font underline type was modified from default
  215. */
  216. public boolean isUnderlineTypeModified()
  217. {
  218. return fontFormatting.isUnderlineTypeModified();
  219. }
  220. /**
  221. * @return true if font weight was modified from default
  222. */
  223. public boolean isFontWeightModified()
  224. {
  225. return fontFormatting.isFontWeightModified();
  226. }
  227. /**
  228. * set font style options.
  229. *
  230. * @param italic - if true, set posture style to italic, otherwise to normal
  231. * @param bold if true, set font weight to bold, otherwise to normal
  232. */
  233. public void setFontStyle(boolean italic, boolean bold)
  234. {
  235. boolean modified = italic || bold;
  236. fontFormatting.setItalic(italic);
  237. fontFormatting.setBold(bold);
  238. fontFormatting.setFontStyleModified(modified);
  239. fontFormatting.setFontWieghtModified(modified);
  240. }
  241. /**
  242. * set font style options to default values (non-italic, non-bold)
  243. */
  244. public void resetFontStyle()
  245. {
  246. setFontStyle(false,false);
  247. }
  248. /**
  249. * set the escapement type for the font
  250. *
  251. * @param escapementType super or subscript option
  252. * @see org.apache.poi.ss.usermodel.Font#SS_NONE
  253. * @see org.apache.poi.ss.usermodel.Font#SS_SUPER
  254. * @see org.apache.poi.ss.usermodel.Font#SS_SUB
  255. */
  256. public void setEscapementType(short escapementType) {
  257. switch(escapementType) {
  258. case org.apache.poi.ss.usermodel.Font.SS_SUB:
  259. case org.apache.poi.ss.usermodel.Font.SS_SUPER:
  260. fontFormatting.setEscapementType(escapementType);
  261. fontFormatting.setEscapementTypeModified(true);
  262. break;
  263. case org.apache.poi.ss.usermodel.Font.SS_NONE:
  264. fontFormatting.setEscapementType(escapementType);
  265. fontFormatting.setEscapementTypeModified(false);
  266. break;
  267. default:
  268. }
  269. }
  270. /**
  271. * @param modified flag, if escapement type is modified
  272. * @see org.apache.poi.hssf.record.cf.FontFormatting#setEscapementTypeModified(boolean)
  273. */
  274. public void setEscapementTypeModified(boolean modified) {
  275. fontFormatting.setEscapementTypeModified(modified);
  276. }
  277. /**
  278. * @param modified flag, if font cancellation is modified
  279. * @see org.apache.poi.hssf.record.cf.FontFormatting#setFontCancellationModified(boolean)
  280. */
  281. public void setFontCancellationModified(boolean modified)
  282. {
  283. fontFormatting.setFontCancellationModified(modified);
  284. }
  285. /**
  286. * @param fci the font color index
  287. * @see org.apache.poi.hssf.record.cf.FontFormatting#setFontColorIndex(short)
  288. */
  289. public void setFontColorIndex(short fci)
  290. {
  291. fontFormatting.setFontColorIndex(fci);
  292. }
  293. /**
  294. * @param height the font height
  295. * @see org.apache.poi.hssf.record.cf.FontFormatting#setFontHeight(int)
  296. */
  297. public void setFontHeight(int height)
  298. {
  299. fontFormatting.setFontHeight(height);
  300. }
  301. /**
  302. * @param modified flag, if font outline is modified
  303. * @see org.apache.poi.hssf.record.cf.FontFormatting#setFontOutlineModified(boolean)
  304. */
  305. public void setFontOutlineModified(boolean modified)
  306. {
  307. fontFormatting.setFontOutlineModified(modified);
  308. }
  309. /**
  310. * @param modified flag, if font shadow is modified
  311. * @see org.apache.poi.hssf.record.cf.FontFormatting#setFontShadowModified(boolean)
  312. */
  313. public void setFontShadowModified(boolean modified)
  314. {
  315. fontFormatting.setFontShadowModified(modified);
  316. }
  317. /**
  318. * @param modified flag, if font style is modified
  319. * @see org.apache.poi.hssf.record.cf.FontFormatting#setFontStyleModified(boolean)
  320. */
  321. public void setFontStyleModified(boolean modified)
  322. {
  323. fontFormatting.setFontStyleModified(modified);
  324. }
  325. /**
  326. * @param on flag, if outline is set
  327. * @see org.apache.poi.hssf.record.cf.FontFormatting#setOutline(boolean)
  328. */
  329. public void setOutline(boolean on)
  330. {
  331. fontFormatting.setOutline(on);
  332. fontFormatting.setFontOutlineModified(on);
  333. }
  334. /**
  335. * @param on flag, if shadow is set
  336. * @see org.apache.poi.hssf.record.cf.FontFormatting#setShadow(boolean)
  337. */
  338. public void setShadow(boolean on)
  339. {
  340. fontFormatting.setShadow(on);
  341. fontFormatting.setFontShadowModified(on);
  342. }
  343. /**
  344. * @param strike flag, if strikeout is set
  345. * @see org.apache.poi.hssf.record.cf.FontFormatting#setStrikeout(boolean)
  346. */
  347. public void setStrikeout(boolean strike)
  348. {
  349. fontFormatting.setStrikeout(strike);
  350. fontFormatting.setFontCancellationModified(strike);
  351. }
  352. /**
  353. * set the type of underlining type for the font
  354. *
  355. * @param underlineType super or subscript option
  356. *
  357. * @see org.apache.poi.ss.usermodel.Font#U_NONE
  358. * @see org.apache.poi.ss.usermodel.Font#U_SINGLE
  359. * @see org.apache.poi.ss.usermodel.Font#U_DOUBLE
  360. * @see org.apache.poi.ss.usermodel.Font#U_SINGLE_ACCOUNTING
  361. * @see org.apache.poi.ss.usermodel.Font#U_DOUBLE_ACCOUNTING
  362. */
  363. public void setUnderlineType(short underlineType) {
  364. switch(underlineType) {
  365. case org.apache.poi.ss.usermodel.Font.U_SINGLE:
  366. case org.apache.poi.ss.usermodel.Font.U_DOUBLE:
  367. case org.apache.poi.ss.usermodel.Font.U_SINGLE_ACCOUNTING:
  368. case org.apache.poi.ss.usermodel.Font.U_DOUBLE_ACCOUNTING:
  369. fontFormatting.setUnderlineType(underlineType);
  370. setUnderlineTypeModified(true);
  371. break;
  372. case org.apache.poi.ss.usermodel.Font.U_NONE:
  373. fontFormatting.setUnderlineType(underlineType);
  374. setUnderlineTypeModified(false);
  375. break;
  376. default:
  377. }
  378. }
  379. /**
  380. * @param modified flag, if underline type is modified
  381. * @see org.apache.poi.hssf.record.cf.FontFormatting#setUnderlineTypeModified(boolean)
  382. */
  383. public void setUnderlineTypeModified(boolean modified) {
  384. fontFormatting.setUnderlineTypeModified(modified);
  385. }
  386. }