選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

TestXSSFFont.java 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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.jupiter.api.Assertions.*;
  17. import java.io.IOException;
  18. import java.util.stream.Stream;
  19. import org.apache.poi.common.usermodel.fonts.FontCharset;
  20. import org.apache.poi.ooxml.POIXMLException;
  21. import org.apache.poi.ss.usermodel.BaseTestFont;
  22. import org.apache.poi.ss.usermodel.Font;
  23. import org.apache.poi.ss.usermodel.FontFamily;
  24. import org.apache.poi.ss.usermodel.FontScheme;
  25. import org.apache.poi.ss.usermodel.FontUnderline;
  26. import org.apache.poi.ss.usermodel.IndexedColors;
  27. import org.apache.poi.ss.usermodel.Workbook;
  28. import org.apache.poi.ss.util.SheetUtil;
  29. import org.apache.poi.util.LocaleUtil;
  30. import org.apache.poi.xssf.XSSFITestDataProvider;
  31. import org.apache.poi.xssf.XSSFTestDataSamples;
  32. import org.junit.jupiter.api.Test;
  33. import org.junit.jupiter.params.provider.Arguments;
  34. import org.openxmlformats.schemas.officeDocument.x2006.sharedTypes.STVerticalAlignRun;
  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.CTFontFamily;
  39. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFontName;
  40. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFontScheme;
  41. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFontSize;
  42. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTIntProperty;
  43. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTUnderlineProperty;
  44. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTVerticalAlignFontProperty;
  45. import org.openxmlformats.schemas.spreadsheetml.x2006.main.STFontScheme;
  46. import org.openxmlformats.schemas.spreadsheetml.x2006.main.STUnderlineValues;
  47. public final class TestXSSFFont extends BaseTestFont{
  48. public TestXSSFFont() {
  49. super(XSSFITestDataProvider.instance);
  50. }
  51. @SuppressWarnings("unused")
  52. public static Stream<Arguments> defaultFont() {
  53. return Stream.of(Arguments.of("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. assertFalse(xssfFont.getBold());
  68. xssfFont.setBold(true);
  69. assertEquals(ctFont.sizeOfBArray(),1);
  70. assertTrue(ctFont.getBArray(0).getVal());
  71. }
  72. @SuppressWarnings("deprecation")
  73. @Test
  74. public void testCharSetWithDeprecatedFontCharset() throws IOException {
  75. CTFont ctFont=CTFont.Factory.newInstance();
  76. CTIntProperty prop=ctFont.addNewCharset();
  77. prop.setVal(org.apache.poi.ss.usermodel.FontCharset.ANSI.getValue());
  78. ctFont.setCharsetArray(0,prop);
  79. XSSFFont xssfFont=new XSSFFont(ctFont);
  80. assertEquals(Font.ANSI_CHARSET,xssfFont.getCharSet());
  81. xssfFont.setCharSet(org.apache.poi.ss.usermodel.FontCharset.DEFAULT);
  82. assertEquals(org.apache.poi.ss.usermodel.FontCharset.DEFAULT.getValue(),ctFont.getCharsetArray(0).getVal());
  83. // Try with a few less usual ones:
  84. // Set with the Charset itself
  85. xssfFont.setCharSet(org.apache.poi.ss.usermodel.FontCharset.RUSSIAN);
  86. assertEquals(org.apache.poi.ss.usermodel.FontCharset.RUSSIAN.getValue(), xssfFont.getCharSet());
  87. // And set with the Charset index
  88. xssfFont.setCharSet(org.apache.poi.ss.usermodel.FontCharset.ARABIC.getValue());
  89. assertEquals(org.apache.poi.ss.usermodel.FontCharset.ARABIC.getValue(), xssfFont.getCharSet());
  90. xssfFont.setCharSet((byte)(org.apache.poi.ss.usermodel.FontCharset.ARABIC.getValue()));
  91. assertEquals(org.apache.poi.ss.usermodel.FontCharset.ARABIC.getValue(), xssfFont.getCharSet());
  92. // This one isn't allowed
  93. assertNull(org.apache.poi.ss.usermodel.FontCharset.valueOf(9999));
  94. assertThrows(POIXMLException.class, () -> xssfFont.setCharSet(9999),
  95. "Shouldn't be able to set an invalid charset");
  96. // Now try with a few sample files
  97. // Normal charset
  98. try (XSSFWorkbook wb1 = XSSFTestDataSamples.openSampleWorkbook("Formatting.xlsx")) {
  99. assertEquals(0,
  100. wb1.getSheetAt(0).getRow(0).getCell(0).getCellStyle().getFont().getCharSet()
  101. );
  102. }
  103. // GB2312 charset
  104. try (XSSFWorkbook wb2 = XSSFTestDataSamples.openSampleWorkbook("49273.xlsx")) {
  105. assertEquals(134,
  106. wb2.getSheetAt(0).getRow(0).getCell(0).getCellStyle().getFont().getCharSet()
  107. );
  108. }
  109. }
  110. @Test
  111. public void testCharSet() throws IOException {
  112. CTFont ctFont=CTFont.Factory.newInstance();
  113. CTIntProperty prop=ctFont.addNewCharset();
  114. prop.setVal(FontCharset.ANSI.getNativeId());
  115. ctFont.setCharsetArray(0,prop);
  116. XSSFFont xssfFont=new XSSFFont(ctFont);
  117. assertEquals(Font.ANSI_CHARSET,xssfFont.getCharSet());
  118. xssfFont.setCharSet(FontCharset.DEFAULT);
  119. assertEquals(FontCharset.DEFAULT.getNativeId(),ctFont.getCharsetArray(0).getVal());
  120. // Try with a few less usual ones:
  121. // Set with the Charset itself
  122. xssfFont.setCharSet(FontCharset.RUSSIAN);
  123. assertEquals(FontCharset.RUSSIAN.getNativeId(), xssfFont.getCharSet());
  124. // And set with the Charset index
  125. xssfFont.setCharSet(FontCharset.ARABIC.getNativeId());
  126. assertEquals(FontCharset.ARABIC.getNativeId(), xssfFont.getCharSet());
  127. xssfFont.setCharSet((byte)(FontCharset.ARABIC.getNativeId()));
  128. assertEquals(FontCharset.ARABIC.getNativeId(), xssfFont.getCharSet());
  129. // This one isn't allowed
  130. assertNull(FontCharset.valueOf(9999));
  131. assertThrows(POIXMLException.class, () -> xssfFont.setCharSet(9999), "Shouldn't be able to set an invalid charset");
  132. // Now try with a few sample files
  133. // Normal charset
  134. try (XSSFWorkbook wb1 = XSSFTestDataSamples.openSampleWorkbook("Formatting.xlsx")) {
  135. assertEquals(0,
  136. wb1.getSheetAt(0).getRow(0).getCell(0).getCellStyle().getFont().getCharSet()
  137. );
  138. }
  139. // GB2312 charset
  140. try (XSSFWorkbook wb2 = XSSFTestDataSamples.openSampleWorkbook("49273.xlsx")) {
  141. assertEquals(134,
  142. wb2.getSheetAt(0).getRow(0).getCell(0).getCellStyle().getFont().getCharSet()
  143. );
  144. }
  145. }
  146. @Test
  147. public void testFontName() {
  148. CTFont ctFont=CTFont.Factory.newInstance();
  149. CTFontName fname=ctFont.addNewName();
  150. fname.setVal("Arial");
  151. ctFont.setNameArray(0,fname);
  152. XSSFFont xssfFont=new XSSFFont(ctFont);
  153. assertEquals("Arial", xssfFont.getFontName());
  154. xssfFont.setFontName("Courier");
  155. assertEquals("Courier",ctFont.getNameArray(0).getVal());
  156. }
  157. @Test
  158. public void testItalic() {
  159. CTFont ctFont=CTFont.Factory.newInstance();
  160. CTBooleanProperty bool=ctFont.addNewI();
  161. bool.setVal(false);
  162. ctFont.setIArray(0,bool);
  163. XSSFFont xssfFont=new XSSFFont(ctFont);
  164. assertFalse(xssfFont.getItalic());
  165. xssfFont.setItalic(true);
  166. assertEquals(ctFont.sizeOfIArray(),1);
  167. assertTrue(ctFont.getIArray(0).getVal());
  168. assertTrue(ctFont.getIArray(0).getVal());
  169. }
  170. @Test
  171. public void testStrikeout() {
  172. CTFont ctFont=CTFont.Factory.newInstance();
  173. CTBooleanProperty bool=ctFont.addNewStrike();
  174. bool.setVal(false);
  175. ctFont.setStrikeArray(0,bool);
  176. XSSFFont xssfFont=new XSSFFont(ctFont);
  177. assertFalse(xssfFont.getStrikeout());
  178. xssfFont.setStrikeout(true);
  179. assertEquals(ctFont.sizeOfStrikeArray(),1);
  180. assertTrue(ctFont.getStrikeArray(0).getVal());
  181. assertTrue(ctFont.getStrikeArray(0).getVal());
  182. }
  183. @Test
  184. public void testFontHeight() {
  185. CTFont ctFont=CTFont.Factory.newInstance();
  186. CTFontSize size=ctFont.addNewSz();
  187. size.setVal(11);
  188. ctFont.setSzArray(0,size);
  189. XSSFFont xssfFont=new XSSFFont(ctFont);
  190. assertEquals(11,xssfFont.getFontHeightInPoints());
  191. xssfFont.setFontHeight(20);
  192. assertEquals(20.0, ctFont.getSzArray(0).getVal(), 0.0);
  193. }
  194. @Test
  195. public void testFontHeightInPoint() {
  196. CTFont ctFont=CTFont.Factory.newInstance();
  197. CTFontSize size=ctFont.addNewSz();
  198. size.setVal(14);
  199. ctFont.setSzArray(0,size);
  200. XSSFFont xssfFont=new XSSFFont(ctFont);
  201. assertEquals(14,xssfFont.getFontHeightInPoints());
  202. xssfFont.setFontHeightInPoints((short)20);
  203. assertEquals(20.0, ctFont.getSzArray(0).getVal(), 0.0);
  204. }
  205. @Test
  206. public void testUnderline() {
  207. CTFont ctFont=CTFont.Factory.newInstance();
  208. CTUnderlineProperty underlinePropr=ctFont.addNewU();
  209. underlinePropr.setVal(STUnderlineValues.SINGLE);
  210. ctFont.setUArray(0,underlinePropr);
  211. XSSFFont xssfFont=new XSSFFont(ctFont);
  212. assertEquals(Font.U_SINGLE, xssfFont.getUnderline());
  213. xssfFont.setUnderline(Font.U_DOUBLE);
  214. assertEquals(ctFont.sizeOfUArray(),1);
  215. assertEquals(STUnderlineValues.DOUBLE,ctFont.getUArray(0).getVal());
  216. xssfFont.setUnderline(FontUnderline.DOUBLE_ACCOUNTING);
  217. assertEquals(ctFont.sizeOfUArray(),1);
  218. assertEquals(STUnderlineValues.DOUBLE_ACCOUNTING,ctFont.getUArray(0).getVal());
  219. }
  220. @Test
  221. public void testColor() {
  222. CTFont ctFont=CTFont.Factory.newInstance();
  223. CTColor color=ctFont.addNewColor();
  224. color.setIndexed(XSSFFont.DEFAULT_FONT_COLOR);
  225. ctFont.setColorArray(0,color);
  226. XSSFFont xssfFont=new XSSFFont(ctFont);
  227. assertEquals(IndexedColors.BLACK.getIndex(),xssfFont.getColor());
  228. xssfFont.setColor(IndexedColors.RED.getIndex());
  229. assertEquals(IndexedColors.RED.getIndex(), ctFont.getColorArray(0).getIndexed());
  230. }
  231. @Test
  232. public void testRgbColor() {
  233. CTFont ctFont=CTFont.Factory.newInstance();
  234. CTColor color=ctFont.addNewColor();
  235. color.setRgb(Integer.toHexString(0xFFFFFF).getBytes(LocaleUtil.CHARSET_1252));
  236. ctFont.setColorArray(0,color);
  237. XSSFFont xssfFont=new XSSFFont(ctFont);
  238. assertEquals(ctFont.getColorArray(0).getRgb()[0],xssfFont.getXSSFColor().getRGB()[0]);
  239. assertEquals(ctFont.getColorArray(0).getRgb()[1],xssfFont.getXSSFColor().getRGB()[1]);
  240. assertEquals(ctFont.getColorArray(0).getRgb()[2],xssfFont.getXSSFColor().getRGB()[2]);
  241. assertEquals(ctFont.getColorArray(0).getRgb()[3],xssfFont.getXSSFColor().getRGB()[3]);
  242. xssfFont.setColor((short)23);
  243. byte[] bytes = Integer.toHexString(0xF1F1F1).getBytes(LocaleUtil.CHARSET_1252);
  244. color.setRgb(bytes);
  245. XSSFColor newColor=XSSFColor.from(color, null);
  246. xssfFont.setColor(newColor);
  247. assertEquals(ctFont.getColorArray(0).getRgb()[2],newColor.getRGB()[2]);
  248. assertArrayEquals(bytes, xssfFont.getXSSFColor().getRGB());
  249. assertEquals(0, xssfFont.getColor());
  250. }
  251. @Test
  252. public void testThemeColor() {
  253. CTFont ctFont=CTFont.Factory.newInstance();
  254. CTColor color=ctFont.addNewColor();
  255. color.setTheme(1);
  256. ctFont.setColorArray(0,color);
  257. XSSFFont xssfFont=new XSSFFont(ctFont);
  258. assertEquals(ctFont.getColorArray(0).getTheme(),xssfFont.getThemeColor());
  259. xssfFont.setThemeColor(IndexedColors.RED.getIndex());
  260. assertEquals(IndexedColors.RED.getIndex(),ctFont.getColorArray(0).getTheme());
  261. }
  262. @Test
  263. public void testFamily() {
  264. CTFont ctFont=CTFont.Factory.newInstance();
  265. CTFontFamily family=ctFont.addNewFamily();
  266. family.setVal(FontFamily.MODERN.getValue());
  267. ctFont.setFamilyArray(0,family);
  268. XSSFFont xssfFont=new XSSFFont(ctFont);
  269. assertEquals(FontFamily.MODERN.getValue(),xssfFont.getFamily());
  270. }
  271. @Test
  272. public void testScheme() {
  273. CTFont ctFont=CTFont.Factory.newInstance();
  274. CTFontScheme scheme=ctFont.addNewScheme();
  275. scheme.setVal(STFontScheme.MAJOR);
  276. ctFont.setSchemeArray(0,scheme);
  277. XSSFFont font=new XSSFFont(ctFont);
  278. assertEquals(FontScheme.MAJOR,font.getScheme());
  279. font.setScheme(FontScheme.NONE);
  280. assertEquals(STFontScheme.NONE,ctFont.getSchemeArray(0).getVal());
  281. }
  282. @Test
  283. public void testTypeOffset() {
  284. CTFont ctFont=CTFont.Factory.newInstance();
  285. CTVerticalAlignFontProperty valign=ctFont.addNewVertAlign();
  286. valign.setVal(STVerticalAlignRun.BASELINE);
  287. ctFont.setVertAlignArray(0,valign);
  288. XSSFFont font=new XSSFFont(ctFont);
  289. assertEquals(Font.SS_NONE,font.getTypeOffset());
  290. font.setTypeOffset(XSSFFont.SS_SUPER);
  291. assertEquals(STVerticalAlignRun.SUPERSCRIPT,ctFont.getVertAlignArray(0).getVal());
  292. }
  293. // store test from TestSheetUtil here as it uses XSSF
  294. @Test
  295. public void testCanComputeWidthXSSF() throws IOException {
  296. try (Workbook wb = new XSSFWorkbook()) {
  297. // cannot check on result because on some machines we get back false here!
  298. SheetUtil.canComputeColumnWidth(wb.getFontAt(0));
  299. }
  300. }
  301. // store test from TestSheetUtil here as it uses XSSF
  302. @Test
  303. public void testCanComputeWidthInvalidFont() {
  304. Font font = new XSSFFont(CTFont.Factory.newInstance());
  305. font.setFontName("some non existing font name");
  306. // Even with invalid fonts we still get back useful data most of the time...
  307. SheetUtil.canComputeColumnWidth(font);
  308. }
  309. /**
  310. * Test that fonts get added properly
  311. */
  312. @Test
  313. public void testFindFont() throws IOException {
  314. try (XSSFWorkbook wb = new XSSFWorkbook()) {
  315. assertEquals(1, wb.getNumberOfFonts());
  316. XSSFSheet s = wb.createSheet();
  317. s.createRow(0);
  318. s.createRow(1);
  319. s.getRow(0).createCell(0);
  320. s.getRow(1).createCell(0);
  321. assertEquals(1, wb.getNumberOfFonts());
  322. XSSFFont f1 = wb.getFontAt(0);
  323. assertFalse(f1.getBold());
  324. // Check that asking for the same font
  325. // multiple times gives you the same thing.
  326. // Otherwise, our tests wouldn't work!
  327. assertSame(wb.getFontAt(0), wb.getFontAt(0));
  328. assertEquals(
  329. wb.getFontAt(0),
  330. wb.getFontAt(0)
  331. );
  332. // Look for a new font we have
  333. // yet to add
  334. assertNull(
  335. wb.findFont(
  336. false, IndexedColors.INDIGO.getIndex(), (short) 22,
  337. "Thingy", false, true, (short) 2, (byte) 2
  338. )
  339. );
  340. assertNull(
  341. wb.getStylesSource().findFont(
  342. false, new XSSFColor(IndexedColors.INDIGO, new DefaultIndexedColorMap()), (short) 22,
  343. "Thingy", false, true, (short) 2, (byte) 2
  344. )
  345. );
  346. XSSFFont nf = wb.createFont();
  347. assertEquals(2, wb.getNumberOfFonts());
  348. assertEquals(1, nf.getIndex());
  349. assertEquals(nf, wb.getFontAt(1));
  350. nf.setBold(false);
  351. nf.setColor(IndexedColors.INDIGO.getIndex());
  352. nf.setFontHeight((short) 22);
  353. nf.setFontName("Thingy");
  354. nf.setItalic(false);
  355. nf.setStrikeout(true);
  356. nf.setTypeOffset((short) 2);
  357. nf.setUnderline((byte) 2);
  358. assertEquals(2, wb.getNumberOfFonts());
  359. assertEquals(nf, wb.getFontAt(1));
  360. assertNotSame(wb.getFontAt(0), wb.getFontAt(1));
  361. // Find it now
  362. assertNotNull(
  363. wb.findFont(
  364. false, IndexedColors.INDIGO.getIndex(), (short) 22,
  365. "Thingy", false, true, (short) 2, (byte) 2
  366. )
  367. );
  368. assertNotNull(
  369. wb.getStylesSource().findFont(
  370. false, new XSSFColor(IndexedColors.INDIGO, new DefaultIndexedColorMap()), (short) 22,
  371. "Thingy", false, true, (short) 2, (byte) 2
  372. )
  373. );
  374. XSSFFont font = wb.findFont(
  375. false, IndexedColors.INDIGO.getIndex(), (short) 22,
  376. "Thingy", false, true, (short) 2, (byte) 2
  377. );
  378. assertNotNull(font);
  379. assertEquals(
  380. 1,
  381. font.getIndex()
  382. );
  383. assertEquals(nf,
  384. wb.findFont(
  385. false, IndexedColors.INDIGO.getIndex(), (short) 22,
  386. "Thingy", false, true, (short) 2, (byte) 2
  387. )
  388. );
  389. assertEquals(nf,
  390. wb.getStylesSource().findFont(
  391. false, new XSSFColor(IndexedColors.INDIGO, new DefaultIndexedColorMap()), (short) 22,
  392. "Thingy", false, true, (short) 2, (byte) 2
  393. )
  394. );
  395. }
  396. }
  397. @Test
  398. public void testEquals() {
  399. XSSFFont font = new XSSFFont();
  400. XSSFFont equ = new XSSFFont();
  401. XSSFFont notequ = new XSSFFont();
  402. notequ.setItalic(true);
  403. assertEquals(equ, font);
  404. assertNotEquals(font, notequ);
  405. notequ.setItalic(false);
  406. notequ.setThemeColor((short)123);
  407. assertNotEquals(font, notequ);
  408. }
  409. }