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.

TestXSSFFont.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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.xssf.usermodel;
  16. import static org.junit.Assert.assertArrayEquals;
  17. import static org.junit.Assert.assertEquals;
  18. import static org.junit.Assert.assertNotNull;
  19. import static org.junit.Assert.fail;
  20. import java.io.IOException;
  21. import org.apache.poi.POIXMLException;
  22. import org.apache.poi.ss.usermodel.BaseTestFont;
  23. import org.apache.poi.ss.usermodel.Font;
  24. import org.apache.poi.ss.usermodel.FontCharset;
  25. import org.apache.poi.ss.usermodel.FontFamily;
  26. import org.apache.poi.ss.usermodel.FontScheme;
  27. import org.apache.poi.ss.usermodel.FontUnderline;
  28. import org.apache.poi.ss.usermodel.IndexedColors;
  29. import org.apache.poi.ss.usermodel.Workbook;
  30. import org.apache.poi.ss.util.SheetUtil;
  31. import org.apache.poi.util.LocaleUtil;
  32. import org.apache.poi.xssf.XSSFITestDataProvider;
  33. import org.apache.poi.xssf.XSSFTestDataSamples;
  34. import org.junit.Test;
  35. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBooleanProperty;
  36. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor;
  37. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFont;
  38. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFontName;
  39. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFontScheme;
  40. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFontSize;
  41. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTIntProperty;
  42. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTUnderlineProperty;
  43. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTVerticalAlignFontProperty;
  44. import org.openxmlformats.schemas.spreadsheetml.x2006.main.STFontScheme;
  45. import org.openxmlformats.schemas.spreadsheetml.x2006.main.STUnderlineValues;
  46. import org.openxmlformats.schemas.spreadsheetml.x2006.main.STVerticalAlignRun;
  47. public final class TestXSSFFont extends BaseTestFont{
  48. public TestXSSFFont() {
  49. super(XSSFITestDataProvider.instance);
  50. }
  51. @Test
  52. public void testDefaultFont() throws IOException {
  53. baseTestDefaultFont("Calibri", (short) 220, IndexedColors.BLACK.getIndex());
  54. }
  55. @Test
  56. public void testConstructor() {
  57. XSSFFont xssfFont=new XSSFFont();
  58. assertNotNull(xssfFont.getCTFont());
  59. }
  60. @Test
  61. public void testBold() {
  62. CTFont ctFont=CTFont.Factory.newInstance();
  63. CTBooleanProperty bool=ctFont.addNewB();
  64. bool.setVal(false);
  65. ctFont.setBArray(0,bool);
  66. XSSFFont xssfFont=new XSSFFont(ctFont);
  67. assertEquals(false, xssfFont.getBold());
  68. xssfFont.setBold(true);
  69. assertEquals(ctFont.sizeOfBArray(),1);
  70. assertEquals(true, ctFont.getBArray(0).getVal());
  71. }
  72. @Test
  73. public void testCharSet() throws IOException {
  74. CTFont ctFont=CTFont.Factory.newInstance();
  75. CTIntProperty prop=ctFont.addNewCharset();
  76. prop.setVal(FontCharset.ANSI.getValue());
  77. ctFont.setCharsetArray(0,prop);
  78. XSSFFont xssfFont=new XSSFFont(ctFont);
  79. assertEquals(Font.ANSI_CHARSET,xssfFont.getCharSet());
  80. xssfFont.setCharSet(FontCharset.DEFAULT);
  81. assertEquals(FontCharset.DEFAULT.getValue(),ctFont.getCharsetArray(0).getVal());
  82. // Try with a few less usual ones:
  83. // Set with the Charset itself
  84. xssfFont.setCharSet(FontCharset.RUSSIAN);
  85. assertEquals(FontCharset.RUSSIAN.getValue(), xssfFont.getCharSet());
  86. // And set with the Charset index
  87. xssfFont.setCharSet(FontCharset.ARABIC.getValue());
  88. assertEquals(FontCharset.ARABIC.getValue(), xssfFont.getCharSet());
  89. xssfFont.setCharSet((byte)(FontCharset.ARABIC.getValue()));
  90. assertEquals(FontCharset.ARABIC.getValue(), xssfFont.getCharSet());
  91. // This one isn't allowed
  92. assertEquals(null, FontCharset.valueOf(9999));
  93. try {
  94. xssfFont.setCharSet(9999);
  95. fail("Shouldn't be able to set an invalid charset");
  96. } catch(POIXMLException e) {}
  97. // Now try with a few sample files
  98. // Normal charset
  99. XSSFWorkbook wb1 = XSSFTestDataSamples.openSampleWorkbook("Formatting.xlsx");
  100. assertEquals(0,
  101. wb1.getSheetAt(0).getRow(0).getCell(0).getCellStyle().getFont().getCharSet()
  102. );
  103. wb1.close();
  104. // GB2312 charact set
  105. XSSFWorkbook wb2 = XSSFTestDataSamples.openSampleWorkbook("49273.xlsx");
  106. assertEquals(134,
  107. wb2.getSheetAt(0).getRow(0).getCell(0).getCellStyle().getFont().getCharSet()
  108. );
  109. wb2.close();
  110. }
  111. @Test
  112. public void testFontName() {
  113. CTFont ctFont=CTFont.Factory.newInstance();
  114. CTFontName fname=ctFont.addNewName();
  115. fname.setVal("Arial");
  116. ctFont.setNameArray(0,fname);
  117. XSSFFont xssfFont=new XSSFFont(ctFont);
  118. assertEquals("Arial", xssfFont.getFontName());
  119. xssfFont.setFontName("Courier");
  120. assertEquals("Courier",ctFont.getNameArray(0).getVal());
  121. }
  122. @Test
  123. public void testItalic() {
  124. CTFont ctFont=CTFont.Factory.newInstance();
  125. CTBooleanProperty bool=ctFont.addNewI();
  126. bool.setVal(false);
  127. ctFont.setIArray(0,bool);
  128. XSSFFont xssfFont=new XSSFFont(ctFont);
  129. assertEquals(false, xssfFont.getItalic());
  130. xssfFont.setItalic(true);
  131. assertEquals(ctFont.sizeOfIArray(),1);
  132. assertEquals(true, ctFont.getIArray(0).getVal());
  133. assertEquals(true,ctFont.getIArray(0).getVal());
  134. }
  135. @Test
  136. public void testStrikeout() {
  137. CTFont ctFont=CTFont.Factory.newInstance();
  138. CTBooleanProperty bool=ctFont.addNewStrike();
  139. bool.setVal(false);
  140. ctFont.setStrikeArray(0,bool);
  141. XSSFFont xssfFont=new XSSFFont(ctFont);
  142. assertEquals(false, xssfFont.getStrikeout());
  143. xssfFont.setStrikeout(true);
  144. assertEquals(ctFont.sizeOfStrikeArray(),1);
  145. assertEquals(true, ctFont.getStrikeArray(0).getVal());
  146. assertEquals(true,ctFont.getStrikeArray(0).getVal());
  147. }
  148. @Test
  149. public void testFontHeight() {
  150. CTFont ctFont=CTFont.Factory.newInstance();
  151. CTFontSize size=ctFont.addNewSz();
  152. size.setVal(11);
  153. ctFont.setSzArray(0,size);
  154. XSSFFont xssfFont=new XSSFFont(ctFont);
  155. assertEquals(11,xssfFont.getFontHeightInPoints());
  156. xssfFont.setFontHeight(20);
  157. assertEquals(20.0, ctFont.getSzArray(0).getVal(), 0.0);
  158. }
  159. @Test
  160. public void testFontHeightInPoint() {
  161. CTFont ctFont=CTFont.Factory.newInstance();
  162. CTFontSize size=ctFont.addNewSz();
  163. size.setVal(14);
  164. ctFont.setSzArray(0,size);
  165. XSSFFont xssfFont=new XSSFFont(ctFont);
  166. assertEquals(14,xssfFont.getFontHeightInPoints());
  167. xssfFont.setFontHeightInPoints((short)20);
  168. assertEquals(20.0, ctFont.getSzArray(0).getVal(), 0.0);
  169. }
  170. @Test
  171. public void testUnderline() {
  172. CTFont ctFont=CTFont.Factory.newInstance();
  173. CTUnderlineProperty underlinePropr=ctFont.addNewU();
  174. underlinePropr.setVal(STUnderlineValues.SINGLE);
  175. ctFont.setUArray(0,underlinePropr);
  176. XSSFFont xssfFont=new XSSFFont(ctFont);
  177. assertEquals(Font.U_SINGLE, xssfFont.getUnderline());
  178. xssfFont.setUnderline(Font.U_DOUBLE);
  179. assertEquals(ctFont.sizeOfUArray(),1);
  180. assertEquals(STUnderlineValues.DOUBLE,ctFont.getUArray(0).getVal());
  181. xssfFont.setUnderline(FontUnderline.DOUBLE_ACCOUNTING);
  182. assertEquals(ctFont.sizeOfUArray(),1);
  183. assertEquals(STUnderlineValues.DOUBLE_ACCOUNTING,ctFont.getUArray(0).getVal());
  184. }
  185. @Test
  186. public void testColor() {
  187. CTFont ctFont=CTFont.Factory.newInstance();
  188. CTColor color=ctFont.addNewColor();
  189. color.setIndexed(XSSFFont.DEFAULT_FONT_COLOR);
  190. ctFont.setColorArray(0,color);
  191. XSSFFont xssfFont=new XSSFFont(ctFont);
  192. assertEquals(IndexedColors.BLACK.getIndex(),xssfFont.getColor());
  193. xssfFont.setColor(IndexedColors.RED.getIndex());
  194. assertEquals(IndexedColors.RED.getIndex(), ctFont.getColorArray(0).getIndexed());
  195. }
  196. @Test
  197. public void testRgbColor() {
  198. CTFont ctFont=CTFont.Factory.newInstance();
  199. CTColor color=ctFont.addNewColor();
  200. color.setRgb(Integer.toHexString(0xFFFFFF).getBytes(LocaleUtil.CHARSET_1252));
  201. ctFont.setColorArray(0,color);
  202. XSSFFont xssfFont=new XSSFFont(ctFont);
  203. assertEquals(ctFont.getColorArray(0).getRgb()[0],xssfFont.getXSSFColor().getRGB()[0]);
  204. assertEquals(ctFont.getColorArray(0).getRgb()[1],xssfFont.getXSSFColor().getRGB()[1]);
  205. assertEquals(ctFont.getColorArray(0).getRgb()[2],xssfFont.getXSSFColor().getRGB()[2]);
  206. assertEquals(ctFont.getColorArray(0).getRgb()[3],xssfFont.getXSSFColor().getRGB()[3]);
  207. xssfFont.setColor((short)23);
  208. byte[] bytes = Integer.toHexString(0xF1F1F1).getBytes(LocaleUtil.CHARSET_1252);
  209. color.setRgb(bytes);
  210. XSSFColor newColor=new XSSFColor(color, null);
  211. xssfFont.setColor(newColor);
  212. assertEquals(ctFont.getColorArray(0).getRgb()[2],newColor.getRGB()[2]);
  213. assertArrayEquals(bytes, xssfFont.getXSSFColor().getRGB());
  214. assertEquals(0, xssfFont.getColor());
  215. }
  216. @Test
  217. public void testThemeColor() {
  218. CTFont ctFont=CTFont.Factory.newInstance();
  219. CTColor color=ctFont.addNewColor();
  220. color.setTheme(1);
  221. ctFont.setColorArray(0,color);
  222. XSSFFont xssfFont=new XSSFFont(ctFont);
  223. assertEquals(ctFont.getColorArray(0).getTheme(),xssfFont.getThemeColor());
  224. xssfFont.setThemeColor(IndexedColors.RED.getIndex());
  225. assertEquals(IndexedColors.RED.getIndex(),ctFont.getColorArray(0).getTheme());
  226. }
  227. @Test
  228. public void testFamily() {
  229. CTFont ctFont=CTFont.Factory.newInstance();
  230. CTIntProperty family=ctFont.addNewFamily();
  231. family.setVal(FontFamily.MODERN.getValue());
  232. ctFont.setFamilyArray(0,family);
  233. XSSFFont xssfFont=new XSSFFont(ctFont);
  234. assertEquals(FontFamily.MODERN.getValue(),xssfFont.getFamily());
  235. }
  236. @Test
  237. public void testScheme() {
  238. CTFont ctFont=CTFont.Factory.newInstance();
  239. CTFontScheme scheme=ctFont.addNewScheme();
  240. scheme.setVal(STFontScheme.MAJOR);
  241. ctFont.setSchemeArray(0,scheme);
  242. XSSFFont font=new XSSFFont(ctFont);
  243. assertEquals(FontScheme.MAJOR,font.getScheme());
  244. font.setScheme(FontScheme.NONE);
  245. assertEquals(STFontScheme.NONE,ctFont.getSchemeArray(0).getVal());
  246. }
  247. @Test
  248. public void testTypeOffset() {
  249. CTFont ctFont=CTFont.Factory.newInstance();
  250. CTVerticalAlignFontProperty valign=ctFont.addNewVertAlign();
  251. valign.setVal(STVerticalAlignRun.BASELINE);
  252. ctFont.setVertAlignArray(0,valign);
  253. XSSFFont font=new XSSFFont(ctFont);
  254. assertEquals(Font.SS_NONE,font.getTypeOffset());
  255. font.setTypeOffset(XSSFFont.SS_SUPER);
  256. assertEquals(STVerticalAlignRun.SUPERSCRIPT,ctFont.getVertAlignArray(0).getVal());
  257. }
  258. // store test from TestSheetUtil here as it uses XSSF
  259. @Test
  260. public void testCanComputeWidthXSSF() throws IOException {
  261. Workbook wb = new XSSFWorkbook();
  262. // cannot check on result because on some machines we get back false here!
  263. SheetUtil.canComputeColumnWidth(wb.getFontAt((short)0));
  264. wb.close();
  265. }
  266. // store test from TestSheetUtil here as it uses XSSF
  267. @Test
  268. public void testCanComputeWidthInvalidFont() throws IOException {
  269. Font font = new XSSFFont(CTFont.Factory.newInstance());
  270. font.setFontName("some non existing font name");
  271. // Even with invalid fonts we still get back useful data most of the time...
  272. SheetUtil.canComputeColumnWidth(font);
  273. }
  274. }