aboutsummaryrefslogtreecommitdiffstats
path: root/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFFont.java
blob: 3fcd77b38113aabe34662afdb595acfad1ea3903 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
package org.apache.poi.xssf.usermodel;

import junit.framework.TestCase;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.XSSFTestDataSamples;
import org.apache.poi.xssf.usermodel.extensions.XSSFColor;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBooleanProperty;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFont;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFontName;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFontScheme;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFontSize;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTIntProperty;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTUnderlineProperty;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTVerticalAlignFontProperty;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STFontScheme;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STUnderlineValues;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STVerticalAlignRun;

public final class TestXSSFFont extends TestCase{

	public void testConstructor() {
		XSSFFont xssfFont=new XSSFFont();
		assertNotNull(xssfFont.getCTFont());
	}

	public void testBoldweight() {
		CTFont ctFont=CTFont.Factory.newInstance();
		CTBooleanProperty bool=ctFont.addNewB();
		bool.setVal(false);
		ctFont.setBArray(0,bool);
		XSSFFont xssfFont=new XSSFFont(ctFont);
		assertEquals(false, xssfFont.getBold());


		xssfFont.setBold(true);
		assertEquals(ctFont.getBArray().length,1);
		assertEquals(true, ctFont.getBArray(0).getVal());
	}

	public void testCharSet() {
		CTFont ctFont=CTFont.Factory.newInstance();
		CTIntProperty prop=ctFont.addNewCharset();
		prop.setVal(FontCharset.ANSI.getValue());

		ctFont.setCharsetArray(0,prop);
		XSSFFont xssfFont=new XSSFFont(ctFont);
		assertEquals(Font.ANSI_CHARSET,xssfFont.getCharSet());

		xssfFont.setCharSet(FontCharset.DEFAULT);
		assertEquals(FontCharset.DEFAULT.getValue(),ctFont.getCharsetArray(0).getVal());
	}

	public void testFontName() {
		CTFont ctFont=CTFont.Factory.newInstance();
		CTFontName fname=ctFont.addNewName();
		fname.setVal("Arial");
		ctFont.setNameArray(0,fname);

		XSSFFont xssfFont=new XSSFFont(ctFont);
		assertEquals("Arial", xssfFont.getFontName());

		xssfFont.setFontName("Courier");
		assertEquals("Courier",ctFont.getNameArray(0).getVal());
	}

	public void testItalic() {
		CTFont ctFont=CTFont.Factory.newInstance();
		CTBooleanProperty bool=ctFont.addNewI();
		bool.setVal(false);
		ctFont.setIArray(0,bool);

		XSSFFont xssfFont=new XSSFFont(ctFont);
		assertEquals(false, xssfFont.getItalic());

		xssfFont.setItalic(true);
		assertEquals(ctFont.getIArray().length,1);
		assertEquals(true, ctFont.getIArray(0).getVal());
		assertEquals(true,ctFont.getIArray(0).getVal());
	}

	public void testStrikeout() {
		CTFont ctFont=CTFont.Factory.newInstance();
		CTBooleanProperty bool=ctFont.addNewStrike();
		bool.setVal(false);
		ctFont.setStrikeArray(0,bool);

		XSSFFont xssfFont=new XSSFFont(ctFont);
		assertEquals(false, xssfFont.getStrikeout());

		xssfFont.setStrikeout(true);
		assertEquals(ctFont.getStrikeArray().length,1);
		assertEquals(true, ctFont.getStrikeArray(0).getVal());
		assertEquals(true,ctFont.getStrikeArray(0).getVal());
	}

	public void testFontHeight() {
		CTFont ctFont=CTFont.Factory.newInstance();
		CTFontSize size=ctFont.addNewSz();
		size.setVal(11);
		ctFont.setSzArray(0,size);

		XSSFFont xssfFont=new XSSFFont(ctFont);
		assertEquals(11,xssfFont.getFontHeight());

		xssfFont.setFontHeight((short)20);
		assertEquals(new Double(20).doubleValue(),ctFont.getSzArray(0).getVal());
	}

	public void testFontHeightInPoint() {
		CTFont ctFont=CTFont.Factory.newInstance();
		CTFontSize size=ctFont.addNewSz();
		size.setVal(14);
		ctFont.setSzArray(0,size);

		XSSFFont xssfFont=new XSSFFont(ctFont);
		assertEquals(14,xssfFont.getFontHeightInPoints());

		xssfFont.setFontHeightInPoints((short)20);
		assertEquals(new Double(20).doubleValue(),ctFont.getSzArray(0).getVal());
	}

	public void testUnderline() {
		CTFont ctFont=CTFont.Factory.newInstance();
		CTUnderlineProperty underlinePropr=ctFont.addNewU();
		underlinePropr.setVal(STUnderlineValues.SINGLE);
		ctFont.setUArray(0,underlinePropr);

		XSSFFont xssfFont=new XSSFFont(ctFont);
		assertEquals(Font.U_SINGLE, xssfFont.getUnderline());

		xssfFont.setUnderline(Font.U_DOUBLE);
		assertEquals(ctFont.getUArray().length,1);
		assertEquals(STUnderlineValues.DOUBLE,ctFont.getUArray(0).getVal());
		
		xssfFont.setUnderline(FontUnderline.DOUBLE_ACCOUNTING);
		assertEquals(ctFont.getUArray().length,1);
		assertEquals(STUnderlineValues.DOUBLE_ACCOUNTING,ctFont.getUArray(0).getVal());
	}

	public void testColor() {
		CTFont ctFont=CTFont.Factory.newInstance();
		CTColor color=ctFont.addNewColor();
		color.setIndexed(XSSFFont.DEFAULT_FONT_COLOR);
		ctFont.setColorArray(0,color);

		XSSFFont xssfFont=new XSSFFont(ctFont);
		assertEquals(IndexedColors.BLACK.getIndex(),xssfFont.getColor());

		xssfFont.setColor(IndexedColors.RED.getIndex());
		assertEquals(IndexedColors.RED.getIndex(), ctFont.getColorArray(0).getIndexed());
	}

	public void testRgbColor() {
		CTFont ctFont=CTFont.Factory.newInstance();
		CTColor color=ctFont.addNewColor();

		color.setRgb(Integer.toHexString(0xFFFFFF).getBytes());
		ctFont.setColorArray(0,color);

		XSSFFont xssfFont=new XSSFFont(ctFont);
		assertEquals(ctFont.getColorArray(0).getRgb()[0],xssfFont.getRgbColor().getRgb()[0]);
		assertEquals(ctFont.getColorArray(0).getRgb()[1],xssfFont.getRgbColor().getRgb()[1]);
		assertEquals(ctFont.getColorArray(0).getRgb()[2],xssfFont.getRgbColor().getRgb()[2]);
		assertEquals(ctFont.getColorArray(0).getRgb()[3],xssfFont.getRgbColor().getRgb()[3]);

		color.setRgb(Integer.toHexString(0xF1F1F1).getBytes());
		XSSFColor newColor=new XSSFColor(color);
		xssfFont.setRgbColor(newColor);
		assertEquals(ctFont.getColorArray(0).getRgb()[2],newColor.getRgb()[2]);
	}

	public void testThemeColor() {
		CTFont ctFont=CTFont.Factory.newInstance();
		CTColor color=ctFont.addNewColor();
		color.setTheme(1);
		ctFont.setColorArray(0,color);

		XSSFFont xssfFont=new XSSFFont(ctFont);
		assertEquals(ctFont.getColorArray(0).getTheme(),xssfFont.getThemeColor());

		xssfFont.setThemeColor(IndexedColors.RED.getIndex());
		assertEquals(IndexedColors.RED.getIndex(),ctFont.getColorArray(0).getTheme());
	}

	public void testFamily() {
		CTFont ctFont=CTFont.Factory.newInstance();
		CTIntProperty family=ctFont.addNewFamily();
		family.setVal(FontFamily.MODERN.getValue());
		ctFont.setFamilyArray(0,family);

		XSSFFont xssfFont=new XSSFFont(ctFont);
		assertEquals(FontFamily.MODERN.getValue(),xssfFont.getFamily());
	}

	public void testScheme() {
		CTFont ctFont=CTFont.Factory.newInstance();
		CTFontScheme scheme=ctFont.addNewScheme();
		scheme.setVal(STFontScheme.MAJOR);
		ctFont.setSchemeArray(0,scheme);

		XSSFFont font=new XSSFFont(ctFont);
		assertEquals(FontScheme.MAJOR,font.getScheme());

		font.setScheme(FontScheme.NONE);
		assertEquals(STFontScheme.NONE,ctFont.getSchemeArray(0).getVal());
	}

	public void testTypeOffset() {
		CTFont ctFont=CTFont.Factory.newInstance();
		CTVerticalAlignFontProperty valign=ctFont.addNewVertAlign();
		valign.setVal(STVerticalAlignRun.BASELINE);
		ctFont.setVertAlignArray(0,valign);

		XSSFFont font=new XSSFFont(ctFont);
		assertEquals(Font.SS_NONE,font.getTypeOffset());

		font.setTypeOffset(XSSFFont.SS_SUPER);
		assertEquals(STVerticalAlignRun.SUPERSCRIPT,ctFont.getVertAlignArray(0).getVal());
	}

	/**
	 * Tests that we can define fonts to a new
	 *  file, save, load, and still see them
	 * @throws Exception
	 */
	public void testCreateSave() {
		XSSFWorkbook wb = new XSSFWorkbook();
		XSSFSheet s1 = wb.createSheet();
		Row r1 = s1.createRow(0);
		Cell r1c1 = r1.createCell(0);
		r1c1.setCellValue(2.2);

		assertEquals(1, wb.getNumberOfFonts());

		XSSFFont font=wb.createFont();
		font.setBold(true);
		font.setStrikeout(true);
		font.setColor(IndexedColors.YELLOW.getIndex());
		font.setFontName("Courier");
		wb.createCellStyle().setFont(font);
		assertEquals(2, wb.getNumberOfFonts());

		CellStyle cellStyleTitle=wb.createCellStyle();
		cellStyleTitle.setFont(font);
		r1c1.setCellStyle(cellStyleTitle);

		// Save and re-load
		wb = XSSFTestDataSamples.writeOutAndReadBack(wb);
		s1 = wb.getSheetAt(0);

		assertEquals(2, wb.getNumberOfFonts());
        short idx = s1.getRow(0).getCell(0).getCellStyle().getFontIndex();
        Font fnt = wb.getFontAt(idx);
        assertNotNull(fnt);
		assertEquals(IndexedColors.YELLOW.getIndex(), fnt.getColor());
		assertEquals("Courier", fnt.getFontName());

		// Now add an orphaned one
		XSSFFont font2 = wb.createFont();
		font2.setItalic(true);
		font2.setFontHeightInPoints((short)15);
		wb.createCellStyle().setFont(font2);
		assertEquals(3, wb.getNumberOfFonts());

		// Save and re-load
		wb = XSSFTestDataSamples.writeOutAndReadBack(wb);
		s1 = wb.getSheetAt(0);

		assertEquals(3, wb.getNumberOfFonts());
		assertNotNull(wb.getFontAt((short)1));
		assertNotNull(wb.getFontAt((short)2));

		assertEquals(15, wb.getFontAt((short)2).getFontHeightInPoints());
		assertEquals(true, wb.getFontAt((short)2).getItalic());
	}

	public void testXSSFFont() {
		XSSFWorkbook workbook=new XSSFWorkbook();
		//Font font1=workbook.createFont();

		Sheet sheet=workbook.createSheet("sheet 1 - test font");


		Row row=sheet.createRow(0);
		Cell cell=row.createCell(0);
		cell.setCellValue(new XSSFRichTextString("XSSFFont test example file"));
		XSSFFont font=new XSSFFont();
		font.setBold(true);
		font.setFontHeightInPoints((short)22);
		font.setColor(IndexedColors.BLUE.getIndex());
		font.setFontName("Verdana");
		CellStyle cellStyleTitle=workbook.createCellStyle();
		cellStyleTitle.setFont(font);
		cell.setCellStyle(cellStyleTitle);


		row=sheet.createRow(3);
		XSSFFont font1=new XSSFFont();
		font1.setBold(true);
		font1.setItalic(true);
		font1.setFontHeightInPoints((short)18);
		font1.setColor(IndexedColors.RED.getIndex());
		font1.setFontName("Arial");
		CellStyle cellStyle1=workbook.createCellStyle();
		cellStyle1.setFont(font1);

		Cell cell1=row.createCell(0);
		cell1.setCellValue(new XSSFRichTextString("red bold 18pt italic Arial"));
		cell1.setCellStyle(cellStyle1);

		
		row=sheet.createRow(4);
		Font font2=new XSSFFont();
		font2.setFontHeight((short)1);
		font2.setFontName("Courier");
		font2.setColor(IndexedColors.BLACK.getIndex());
		font2.setUnderline(Font.U_DOUBLE);
		CellStyle cellStyle2=workbook.createCellStyle();
		cellStyle2.setFont(font2);

		Cell cell2=row.createCell(0);
		cell2.setCellValue(new XSSFRichTextString("Something in courier underlined"));
		cell2.setCellStyle(cellStyle2);


		row=sheet.createRow(5);
		cell1=row.createCell(0);
		Font font3=new XSSFFont();
		font3.setFontHeightInPoints((short)9);
		font3.setFontName("Times");
		font3.setStrikeout(true);
		font3.setColor(IndexedColors.PINK.getIndex());
		CellStyle cellStyle3=workbook.createCellStyle();
		cellStyle3.setFont(font3);

		cell1.setCellValue(new XSSFRichTextString("pink italic Times 9pt strikeout!!!"));
		cell1.setCellStyle(cellStyle3);

		XSSFTestDataSamples.writeOutAndReadBack(workbook);
	}

    /**
     * Test that fonts get added properly
     * 
     * @see org.apache.poi.hssf.usermodel.TestBugs#test45338()
     */
    public void test45338() {
        XSSFWorkbook wb = new XSSFWorkbook();
        assertEquals(1, wb.getNumberOfFonts());

        XSSFSheet s = wb.createSheet();
        s.createRow(0);
        s.createRow(1);
        XSSFCell c1 = s.getRow(0).createCell(0);
        XSSFCell c2 = s.getRow(1).createCell(0);

        assertEquals(1, wb.getNumberOfFonts());

        XSSFFont f1 = wb.getFontAt((short)0);
        assertEquals(XSSFFont.BOLDWEIGHT_NORMAL, f1.getBoldweight());

        // Check that asking for the same font
        //  multiple times gives you the same thing.
        // Otherwise, our tests wouldn't work!
        assertEquals(
                wb.getFontAt((short)0),
                wb.getFontAt((short)0)
        );

        // Look for a new font we have
        //  yet to add
        assertNull(
            wb.findFont(
                (short)11, (short)123, (short)22,
                "Thingy", false, true, (short)2, (byte)2
            )
        );

        XSSFFont nf = wb.createFont();
        assertEquals(2, wb.getNumberOfFonts());

        assertEquals(1, nf.getIndex());
        assertEquals(nf, wb.getFontAt((short)1));

        nf.setBoldweight((short)11);
        nf.setColor((short)123);
        nf.setFontHeight((short)22);
        nf.setFontName("Thingy");
        nf.setItalic(false);
        nf.setStrikeout(true);
        nf.setTypeOffset((short)2);
        nf.setUnderline((byte)2);

        assertEquals(2, wb.getNumberOfFonts());
        assertEquals(nf, wb.getFontAt((short)1));

        assertEquals(
                wb.getFontAt((short)1),
                wb.getFontAt((short)1)
        );
        assertTrue(
                wb.getFontAt((short)0)
                !=
                wb.getFontAt((short)1)
        );

        // Find it now
        assertNotNull(
            wb.findFont(
                (short)11, (short)123, (short)22,
                "Thingy", false, true, (short)2, (byte)2
            )
        );
        assertEquals(
            1,
            wb.findFont(
                   (short)11, (short)123, (short)22,
                   "Thingy", false, true, (short)2, (byte)2
               ).getIndex()
        );
        assertEquals(nf,
               wb.findFont(
                   (short)11, (short)123, (short)22,
                   "Thingy", false, true, (short)2, (byte)2
               )
        );
    }

}