View | Details | Raw Unified | Return to bug 60521
Collapse All | Expand All

(-)src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFShapeGroup.java (-3 / +44 lines)
Lines 17-23 Link Here
17
17
18
package org.apache.poi.xssf.usermodel;
18
package org.apache.poi.xssf.usermodel;
19
19
20
import java.util.Iterator;
21
20
import org.apache.poi.openxml4j.opc.PackageRelationship;
22
import org.apache.poi.openxml4j.opc.PackageRelationship;
23
import org.apache.poi.ss.usermodel.ShapeContainer;
21
import org.apache.poi.util.Internal;
24
import org.apache.poi.util.Internal;
22
import org.openxmlformats.schemas.drawingml.x2006.main.CTGroupShapeProperties;
25
import org.openxmlformats.schemas.drawingml.x2006.main.CTGroupShapeProperties;
23
import org.openxmlformats.schemas.drawingml.x2006.main.CTGroupTransform2D;
26
import org.openxmlformats.schemas.drawingml.x2006.main.CTGroupTransform2D;
Lines 24-30 Link Here
24
import org.openxmlformats.schemas.drawingml.x2006.main.CTNonVisualDrawingProps;
27
import org.openxmlformats.schemas.drawingml.x2006.main.CTNonVisualDrawingProps;
25
import org.openxmlformats.schemas.drawingml.x2006.main.CTPoint2D;
28
import org.openxmlformats.schemas.drawingml.x2006.main.CTPoint2D;
26
import org.openxmlformats.schemas.drawingml.x2006.main.CTPositiveSize2D;
29
import org.openxmlformats.schemas.drawingml.x2006.main.CTPositiveSize2D;
30
import org.openxmlformats.schemas.drawingml.x2006.main.CTSRgbColor;
27
import org.openxmlformats.schemas.drawingml.x2006.main.CTShapeProperties;
31
import org.openxmlformats.schemas.drawingml.x2006.main.CTShapeProperties;
32
import org.openxmlformats.schemas.drawingml.x2006.main.CTSolidColorFillProperties;
33
import org.openxmlformats.schemas.drawingml.x2006.main.CTTransform2D;
28
import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTConnector;
34
import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTConnector;
29
import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTGroupShape;
35
import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTGroupShape;
30
import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTGroupShapeNonVisual;
36
import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTGroupShapeNonVisual;
Lines 36-45 Link Here
36
 * just as if it were a regular shape but instead of being described by a single geometry it is made up of all the
42
 * just as if it were a regular shape but instead of being described by a single geometry it is made up of all the
37
 * shape geometries encompassed within it. Within a group shape each of the shapes that make up the group are
43
 * shape geometries encompassed within it. Within a group shape each of the shapes that make up the group are
38
 * specified just as they normally would.
44
 * specified just as they normally would.
39
 *
40
 * @author Yegor Kozlov
41
 */
45
 */
42
public final class XSSFShapeGroup extends XSSFShape {
46
public final class XSSFShapeGroup extends XSSFShape implements ShapeContainer<XSSFShape> {
43
    private static CTGroupShape prototype = null;
47
    private static CTGroupShape prototype = null;
44
48
45
    private CTGroupShape ctGroup;
49
    private CTGroupShape ctGroup;
Lines 164-169 Link Here
164
        return shape;
168
        return shape;
165
    }
169
    }
166
170
171
    /**
172
     * Creates a group shape.
173
     *
174
     * @param anchor       the client anchor describes how this group is attached to the group.
175
     * @return the newly created group shape.
176
     */
177
    public XSSFShapeGroup createGroup(XSSFChildAnchor anchor) {
178
        CTGroupShape ctShape = ctGroup.addNewGrpSp();
179
        ctShape.set(prototype());
180
181
        XSSFShapeGroup shape = new XSSFShapeGroup(getDrawing(), ctShape);
182
        shape.parent = this;
183
        shape.anchor = anchor;
184
        
185
        // TODO: calculate bounding rectangle on anchor and set off/ext correctly
186
        
187
        CTGroupTransform2D xfrm = shape.getCTGroupShape().getGrpSpPr().getXfrm();
188
        CTTransform2D t2 = anchor.getCTTransform2D();
189
        xfrm.setOff(t2.getOff());
190
        xfrm.setExt(t2.getExt());
191
        // child offset is left to 0,0
192
        xfrm.setChExt(t2.getExt());
193
        xfrm.setFlipH(t2.getFlipH());
194
        xfrm.setFlipV(t2.getFlipV());
195
        
196
        return shape;
197
    }
198
167
    @Internal
199
    @Internal
168
    public CTGroupShape getCTGroupShape() {
200
    public CTGroupShape getCTGroupShape() {
169
        return ctGroup;
201
        return ctGroup;
Lines 194-197 Link Here
194
        throw new IllegalStateException("Not supported for shape group");
226
        throw new IllegalStateException("Not supported for shape group");
195
    }
227
    }
196
228
229
    @Override
230
    public Iterator<XSSFShape> iterator() {
231
        return getDrawing().getShapes(this).iterator();
232
    }
233
234
    @Override
235
    public String getShapeName() {
236
        return ctGroup.getNvGrpSpPr().getCNvPr().getName();
237
    }
197
}
238
}
(-)src/java/org/apache/poi/ss/util/ImageUtils.java (-1 / +1 lines)
Lines 277-283 Link Here
277
    }
277
    }
278
    
278
    
279
    
279
    
280
    private static double getRowHeightInPixels(Sheet sheet, int rowNum) {
280
    public static double getRowHeightInPixels(Sheet sheet, int rowNum) {
281
        Row r = sheet.getRow(rowNum);
281
        Row r = sheet.getRow(rowNum);
282
        double points = (r == null) ? sheet.getDefaultRowHeightInPoints() : r.getHeightInPoints();
282
        double points = (r == null) ? sheet.getDefaultRowHeightInPoints() : r.getHeightInPoints();
283
        return Units.toEMU(points)/(double)EMU_PER_PIXEL;
283
        return Units.toEMU(points)/(double)EMU_PER_PIXEL;
(-)src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFDrawing.java (-90 / +156 lines)
Lines 24-29 Link Here
24
import static org.junit.Assert.assertTrue;
24
import static org.junit.Assert.assertTrue;
25
25
26
import java.awt.Color;
26
import java.awt.Color;
27
import java.io.FileOutputStream;
27
import java.io.IOException;
28
import java.io.IOException;
28
import java.util.List;
29
import java.util.List;
29
30
Lines 30-39 Link Here
30
import org.apache.poi.POIXMLDocumentPart;
31
import org.apache.poi.POIXMLDocumentPart;
31
import org.apache.poi.POIXMLDocumentPart.RelationPart;
32
import org.apache.poi.POIXMLDocumentPart.RelationPart;
32
import org.apache.poi.openxml4j.opc.OPCPackage;
33
import org.apache.poi.openxml4j.opc.OPCPackage;
34
import org.apache.poi.openxml4j.opc.PackagePart;
35
import org.apache.poi.openxml4j.opc.PackagingURIHelper;
33
import org.apache.poi.ss.usermodel.ClientAnchor;
36
import org.apache.poi.ss.usermodel.ClientAnchor;
34
import org.apache.poi.ss.usermodel.FontUnderline;
37
import org.apache.poi.ss.usermodel.FontUnderline;
38
import org.apache.poi.ss.usermodel.ShapeTypes;
39
import org.apache.poi.util.IOUtils;
40
import org.apache.poi.util.Units;
35
import org.apache.poi.xssf.XSSFTestDataSamples;
41
import org.apache.poi.xssf.XSSFTestDataSamples;
36
import org.junit.Test;
42
import org.junit.Test;
43
import org.openxmlformats.schemas.drawingml.x2006.main.CTGroupTransform2D;
37
import org.openxmlformats.schemas.drawingml.x2006.main.CTTextCharacterProperties;
44
import org.openxmlformats.schemas.drawingml.x2006.main.CTTextCharacterProperties;
38
import org.openxmlformats.schemas.drawingml.x2006.main.CTTextParagraph;
45
import org.openxmlformats.schemas.drawingml.x2006.main.CTTextParagraph;
39
import org.openxmlformats.schemas.drawingml.x2006.main.STTextUnderlineType;
46
import org.openxmlformats.schemas.drawingml.x2006.main.STTextUnderlineType;
Lines 131-137 Link Here
131
        // Check
138
        // Check
132
        dr1 = sheet.createDrawingPatriarch();
139
        dr1 = sheet.createDrawingPatriarch();
133
        CTDrawing ctDrawing = dr1.getCTDrawing();
140
        CTDrawing ctDrawing = dr1.getCTDrawing();
134
        
141
135
        // Connector, shapes and text boxes are all two cell anchors
142
        // Connector, shapes and text boxes are all two cell anchors
136
        assertEquals(0, ctDrawing.sizeOfAbsoluteAnchorArray());
143
        assertEquals(0, ctDrawing.sizeOfAbsoluteAnchorArray());
137
        assertEquals(0, ctDrawing.sizeOfOneCellAnchorArray());
144
        assertEquals(0, ctDrawing.sizeOfOneCellAnchorArray());
Lines 148-158 Link Here
148
        String xml = ctDrawing.toString();
155
        String xml = ctDrawing.toString();
149
        assertTrue(xml.contains("xmlns:xdr=\"http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing\""));
156
        assertTrue(xml.contains("xmlns:xdr=\"http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing\""));
150
        assertTrue(xml.contains("xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\""));
157
        assertTrue(xml.contains("xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\""));
151
        
158
152
        checkRewrite(wb2);
159
        checkRewrite(wb2);
153
        wb2.close();
160
        wb2.close();
154
    }
161
    }
155
    
162
156
    @Test
163
    @Test
157
    public void testMultipleDrawings() throws IOException{
164
    public void testMultipleDrawings() throws IOException{
158
        XSSFWorkbook wb = new XSSFWorkbook();
165
        XSSFWorkbook wb = new XSSFWorkbook();
Lines 177-183 Link Here
177
        XSSFSheet sheet1 = wb.getSheetAt(0);
184
        XSSFSheet sheet1 = wb.getSheetAt(0);
178
185
179
        XSSFSheet sheet2 = wb.cloneSheet(0);
186
        XSSFSheet sheet2 = wb.cloneSheet(0);
180
        
187
181
        //the source sheet has one relationship and it is XSSFDrawing
188
        //the source sheet has one relationship and it is XSSFDrawing
182
        List<POIXMLDocumentPart> rels1 = sheet1.getRelations();
189
        List<POIXMLDocumentPart> rels1 = sheet1.getRelations();
183
        assertEquals(1, rels1.size());
190
        assertEquals(1, rels1.size());
Lines 202-208 Link Here
202
            assertTrue(sh1.getClass() == sh2.getClass());
209
            assertTrue(sh1.getClass() == sh2.getClass());
203
            assertEquals(sh1.getShapeProperties().toString(), sh2.getShapeProperties().toString());
210
            assertEquals(sh1.getShapeProperties().toString(), sh2.getShapeProperties().toString());
204
        }
211
        }
205
        
212
206
        checkRewrite(wb);
213
        checkRewrite(wb);
207
        wb.close();
214
        wb.close();
208
    }
215
    }
Lines 241-247 Link Here
241
        assertArrayEquals(
248
        assertArrayEquals(
242
                new byte[]{0, (byte)128, (byte)128} ,
249
                new byte[]{0, (byte)128, (byte)128} ,
243
                rPr.getSolidFill().getSrgbClr().getVal());
250
                rPr.getSolidFill().getSrgbClr().getVal());
244
        
251
245
        checkRewrite(wb);
252
        checkRewrite(wb);
246
        wb.close();
253
        wb.close();
247
    }
254
    }
Lines 276-286 Link Here
276
        assertEquals(shapes.get(0).getAnchor(), anchor1);
283
        assertEquals(shapes.get(0).getAnchor(), anchor1);
277
        assertEquals(shapes.get(1).getAnchor(), anchor2);
284
        assertEquals(shapes.get(1).getAnchor(), anchor2);
278
        assertEquals(shapes.get(2).getAnchor(), anchor3);
285
        assertEquals(shapes.get(2).getAnchor(), anchor3);
279
        
286
280
        checkRewrite(wb2);
287
        checkRewrite(wb2);
281
        wb2.close();
288
        wb2.close();
282
    }
289
    }
283
    
290
284
    /**
291
    /**
285
     * ensure that font and color rich text attributes defined in a XSSFRichTextString
292
     * ensure that font and color rich text attributes defined in a XSSFRichTextString
286
     * are passed to XSSFSimpleShape.
293
     * are passed to XSSFSimpleShape.
Lines 333-358 Link Here
333
        rt.applyFont(font);
340
        rt.applyFont(font);
334
341
335
        shape.setText(rt);
342
        shape.setText(rt);
336
        
343
337
        List<XSSFTextParagraph> paras = shape.getTextParagraphs();
344
        List<XSSFTextParagraph> paras = shape.getTextParagraphs();
338
        assertEquals(1, paras.size());
345
        assertEquals(1, paras.size());
339
        assertEquals("Test String", paras.get(0).getText());
346
        assertEquals("Test String", paras.get(0).getText());
340
        
347
341
        List<XSSFTextRun> runs = paras.get(0).getTextRuns();
348
        List<XSSFTextRun> runs = paras.get(0).getTextRuns();
342
        assertEquals(1, runs.size());
349
        assertEquals(1, runs.size());
343
        assertEquals("Arial", runs.get(0).getFontFamily());
350
        assertEquals("Arial", runs.get(0).getFontFamily());
344
        
351
345
        Color clr = runs.get(0).getFontColor(); 
352
        Color clr = runs.get(0).getFontColor();
346
        assertArrayEquals(
353
        assertArrayEquals(
347
                new int[] { 0, 255, 255 } ,
354
                new int[] { 0, 255, 255 } ,
348
                new int[] { clr.getRed(), clr.getGreen(), clr.getBlue() });
355
                new int[] { clr.getRed(), clr.getGreen(), clr.getBlue() });
349
        
356
350
        checkRewrite(wb);
357
        checkRewrite(wb);
351
        wb.close();
358
        wb.close();
352
    }
359
    }
353
    
360
354
    /**
361
    /**
355
     * Test addNewTextParagraph 
362
     * Test addNewTextParagraph
356
     */
363
     */
357
    @Test
364
    @Test
358
    public void testAddNewTextParagraph() throws IOException {
365
    public void testAddNewTextParagraph() throws IOException {
Lines 361-377 Link Here
361
        XSSFDrawing drawing = sheet.createDrawingPatriarch();
368
        XSSFDrawing drawing = sheet.createDrawingPatriarch();
362
369
363
        XSSFTextBox shape = drawing.createTextbox(new XSSFClientAnchor(0, 0, 0, 0, 2, 2, 3, 4));
370
        XSSFTextBox shape = drawing.createTextbox(new XSSFClientAnchor(0, 0, 0, 0, 2, 2, 3, 4));
364
        
371
365
        XSSFTextParagraph para = shape.addNewTextParagraph();
372
        XSSFTextParagraph para = shape.addNewTextParagraph();
366
        para.addNewTextRun().setText("Line 1");
373
        para.addNewTextRun().setText("Line 1");
367
                
374
368
        List<XSSFTextParagraph> paras = shape.getTextParagraphs();
375
        List<XSSFTextParagraph> paras = shape.getTextParagraphs();
369
        assertEquals(2, paras.size());	// this should be 2 as XSSFSimpleShape creates a default paragraph (no text), and then we add a string to that.
376
        assertEquals(2, paras.size());	// this should be 2 as XSSFSimpleShape creates a default paragraph (no text), and then we add a string to that.
370
        
377
371
        List<XSSFTextRun> runs = para.getTextRuns();
378
        List<XSSFTextRun> runs = para.getTextRuns();
372
        assertEquals(1, runs.size());
379
        assertEquals(1, runs.size());
373
        assertEquals("Line 1", runs.get(0).getText());
380
        assertEquals("Line 1", runs.get(0).getText());
374
        
381
375
        checkRewrite(wb);
382
        checkRewrite(wb);
376
        wb.close();
383
        wb.close();
377
    }
384
    }
Lines 388-404 Link Here
388
        XSSFTextBox shape = drawing.createTextbox(new XSSFClientAnchor(0, 0, 0, 0, 2, 2, 3, 4));
395
        XSSFTextBox shape = drawing.createTextbox(new XSSFClientAnchor(0, 0, 0, 0, 2, 2, 3, 4));
389
        XSSFRichTextString rt = new XSSFRichTextString("Test Rich Text String");
396
        XSSFRichTextString rt = new XSSFRichTextString("Test Rich Text String");
390
397
391
        XSSFFont font = wb1.createFont();        
398
        XSSFFont font = wb1.createFont();
392
        font.setColor(new XSSFColor(new Color(0, 255, 255)));
399
        font.setColor(new XSSFColor(new Color(0, 255, 255)));
393
        font.setFontName("Arial");
400
        font.setFontName("Arial");
394
        rt.applyFont(font);
401
        rt.applyFont(font);
395
        
402
396
        XSSFFont midfont = wb1.createFont();
403
        XSSFFont midfont = wb1.createFont();
397
        midfont.setColor(new XSSFColor(new Color(0, 255, 0)));
404
        midfont.setColor(new XSSFColor(new Color(0, 255, 0)));
398
        rt.applyFont(5, 14, midfont);	// set the text "Rich Text" to be green and the default font
405
        rt.applyFont(5, 14, midfont);	// set the text "Rich Text" to be green and the default font
399
        
406
400
        XSSFTextParagraph para = shape.addNewTextParagraph(rt);
407
        XSSFTextParagraph para = shape.addNewTextParagraph(rt);
401
        
408
402
        // Save and re-load it
409
        // Save and re-load it
403
        XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1);
410
        XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1);
404
        wb1.close();
411
        wb1.close();
Lines 406-454 Link Here
406
413
407
        // Check
414
        // Check
408
        drawing = sheet.createDrawingPatriarch();
415
        drawing = sheet.createDrawingPatriarch();
409
        
416
410
        List<XSSFShape> shapes = drawing.getShapes();
417
        List<XSSFShape> shapes = drawing.getShapes();
411
        assertEquals(1, shapes.size());
418
        assertEquals(1, shapes.size());
412
        assertTrue(shapes.get(0) instanceof XSSFSimpleShape); 
419
        assertTrue(shapes.get(0) instanceof XSSFSimpleShape);
413
        
420
414
        XSSFSimpleShape sshape = (XSSFSimpleShape) shapes.get(0);
421
        XSSFSimpleShape sshape = (XSSFSimpleShape) shapes.get(0);
415
        
422
416
        List<XSSFTextParagraph> paras = sshape.getTextParagraphs();
423
        List<XSSFTextParagraph> paras = sshape.getTextParagraphs();
417
        assertEquals(2, paras.size());	// this should be 2 as XSSFSimpleShape creates a default paragraph (no text), and then we add a string to that.  
424
        assertEquals(2, paras.size());	// this should be 2 as XSSFSimpleShape creates a default paragraph (no text), and then we add a string to that.
418
        
425
419
        List<XSSFTextRun> runs = para.getTextRuns();
426
        List<XSSFTextRun> runs = para.getTextRuns();
420
        assertEquals(3, runs.size());
427
        assertEquals(3, runs.size());
421
        
428
422
        // first run properties
429
        // first run properties
423
        assertEquals("Test ", runs.get(0).getText());
430
        assertEquals("Test ", runs.get(0).getText());
424
        assertEquals("Arial", runs.get(0).getFontFamily());
431
        assertEquals("Arial", runs.get(0).getFontFamily());
425
432
426
        Color clr = runs.get(0).getFontColor(); 
433
        Color clr = runs.get(0).getFontColor();
427
        assertArrayEquals(
434
        assertArrayEquals(
428
                new int[] { 0, 255, 255 } ,
435
                new int[] { 0, 255, 255 } ,
429
                new int[] { clr.getRed(), clr.getGreen(), clr.getBlue() });
436
                new int[] { clr.getRed(), clr.getGreen(), clr.getBlue() });
430
437
431
        // second run properties        
438
        // second run properties
432
        assertEquals("Rich Text", runs.get(1).getText());
439
        assertEquals("Rich Text", runs.get(1).getText());
433
        assertEquals(XSSFFont.DEFAULT_FONT_NAME, runs.get(1).getFontFamily());
440
        assertEquals(XSSFFont.DEFAULT_FONT_NAME, runs.get(1).getFontFamily());
434
441
435
        clr = runs.get(1).getFontColor(); 
442
        clr = runs.get(1).getFontColor();
436
        assertArrayEquals(
443
        assertArrayEquals(
437
                new int[] { 0, 255, 0 } ,
444
                new int[] { 0, 255, 0 } ,
438
                new int[] { clr.getRed(), clr.getGreen(), clr.getBlue() });        
445
                new int[] { clr.getRed(), clr.getGreen(), clr.getBlue() });
439
        
446
440
        // third run properties
447
        // third run properties
441
        assertEquals(" String", runs.get(2).getText());
448
        assertEquals(" String", runs.get(2).getText());
442
        assertEquals("Arial", runs.get(2).getFontFamily());
449
        assertEquals("Arial", runs.get(2).getFontFamily());
443
        clr = runs.get(2).getFontColor(); 
450
        clr = runs.get(2).getFontColor();
444
        assertArrayEquals(
451
        assertArrayEquals(
445
                new int[] { 0, 255, 255 } ,
452
                new int[] { 0, 255, 255 } ,
446
                new int[] { clr.getRed(), clr.getGreen(), clr.getBlue() });
453
                new int[] { clr.getRed(), clr.getGreen(), clr.getBlue() });
447
        
454
448
        checkRewrite(wb2);
455
        checkRewrite(wb2);
449
        wb2.close();
456
        wb2.close();
450
    }    
457
    }
451
    
458
452
    /**
459
    /**
453
     * Test add multiple paragraphs and retrieve text
460
     * Test add multiple paragraphs and retrieve text
454
     */
461
     */
Lines 459-482 Link Here
459
        XSSFDrawing drawing = sheet.createDrawingPatriarch();
466
        XSSFDrawing drawing = sheet.createDrawingPatriarch();
460
467
461
        XSSFTextBox shape = drawing.createTextbox(new XSSFClientAnchor(0, 0, 0, 0, 2, 2, 3, 4));
468
        XSSFTextBox shape = drawing.createTextbox(new XSSFClientAnchor(0, 0, 0, 0, 2, 2, 3, 4));
462
        
469
463
        XSSFTextParagraph para = shape.addNewTextParagraph();
470
        XSSFTextParagraph para = shape.addNewTextParagraph();
464
        para.addNewTextRun().setText("Line 1");
471
        para.addNewTextRun().setText("Line 1");
465
                
472
466
        para = shape.addNewTextParagraph();
473
        para = shape.addNewTextParagraph();
467
        para.addNewTextRun().setText("Line 2");
474
        para.addNewTextRun().setText("Line 2");
468
        
475
469
        para = shape.addNewTextParagraph();
476
        para = shape.addNewTextParagraph();
470
        para.addNewTextRun().setText("Line 3");
477
        para.addNewTextRun().setText("Line 3");
471
        
478
472
        List<XSSFTextParagraph> paras = shape.getTextParagraphs();
479
        List<XSSFTextParagraph> paras = shape.getTextParagraphs();
473
        assertEquals(4, paras.size());	// this should be 4 as XSSFSimpleShape creates a default paragraph (no text), and then we added 3 paragraphs
480
        assertEquals(4, paras.size());	// this should be 4 as XSSFSimpleShape creates a default paragraph (no text), and then we added 3 paragraphs
474
        assertEquals("Line 1\nLine 2\nLine 3", shape.getText());           
481
        assertEquals("Line 1\nLine 2\nLine 3", shape.getText());
475
        
482
476
        checkRewrite(wb);
483
        checkRewrite(wb);
477
        wb.close();
484
        wb.close();
478
    }
485
    }
479
    
486
480
    /**
487
    /**
481
     * Test setting the text, then adding multiple paragraphs and retrieve text
488
     * Test setting the text, then adding multiple paragraphs and retrieve text
482
     */
489
     */
Lines 487-509 Link Here
487
        XSSFDrawing drawing = sheet.createDrawingPatriarch();
494
        XSSFDrawing drawing = sheet.createDrawingPatriarch();
488
495
489
        XSSFTextBox shape = drawing.createTextbox(new XSSFClientAnchor(0, 0, 0, 0, 2, 2, 3, 4));
496
        XSSFTextBox shape = drawing.createTextbox(new XSSFClientAnchor(0, 0, 0, 0, 2, 2, 3, 4));
490
        
497
491
        shape.setText("Line 1");
498
        shape.setText("Line 1");
492
                
499
493
        XSSFTextParagraph para = shape.addNewTextParagraph();
500
        XSSFTextParagraph para = shape.addNewTextParagraph();
494
        para.addNewTextRun().setText("Line 2");
501
        para.addNewTextRun().setText("Line 2");
495
        
502
496
        para = shape.addNewTextParagraph();
503
        para = shape.addNewTextParagraph();
497
        para.addNewTextRun().setText("Line 3");
504
        para.addNewTextRun().setText("Line 3");
498
        
505
499
        List<XSSFTextParagraph> paras = shape.getTextParagraphs();
506
        List<XSSFTextParagraph> paras = shape.getTextParagraphs();
500
        assertEquals(3, paras.size());	// this should be 3 as we overwrote the default paragraph with setText, then added 2 new paragraphs
507
        assertEquals(3, paras.size());	// this should be 3 as we overwrote the default paragraph with setText, then added 2 new paragraphs
501
        assertEquals("Line 1\nLine 2\nLine 3", shape.getText());
508
        assertEquals("Line 1\nLine 2\nLine 3", shape.getText());
502
        
509
503
        checkRewrite(wb);
510
        checkRewrite(wb);
504
        wb.close();
511
        wb.close();
505
    }
512
    }
506
    
513
507
    /**
514
    /**
508
     * Test reading text from a textbox in an existing file
515
     * Test reading text from a textbox in an existing file
509
     */
516
     */
Lines 531-544 Link Here
531
538
532
        assertTrue(shapes.get(4) instanceof XSSFSimpleShape);
539
        assertTrue(shapes.get(4) instanceof XSSFSimpleShape);
533
540
534
        XSSFSimpleShape textbox = (XSSFSimpleShape) shapes.get(4); 
541
        XSSFSimpleShape textbox = (XSSFSimpleShape) shapes.get(4);
535
        assertEquals("Sheet with various pictures\n(jpeg, png, wmf, emf and pict)", textbox.getText());
542
        assertEquals("Sheet with various pictures\n(jpeg, png, wmf, emf and pict)", textbox.getText());
536
        
543
537
        checkRewrite(wb);
544
        checkRewrite(wb);
538
        wb.close();
545
        wb.close();
539
    }
546
    }
540
547
541
    
548
542
    /**
549
    /**
543
     * Test reading multiple paragraphs from a textbox in an existing file
550
     * Test reading multiple paragraphs from a textbox in an existing file
544
     */
551
     */
Lines 550-560 Link Here
550
        List<RelationPart> rels = sheet.getRelationParts();
557
        List<RelationPart> rels = sheet.getRelationParts();
551
        assertEquals(1, rels.size());
558
        assertEquals(1, rels.size());
552
        RelationPart rp = rels.get(0);
559
        RelationPart rp = rels.get(0);
553
       
560
554
        assertTrue(rp.getDocumentPart() instanceof XSSFDrawing);
561
        assertTrue(rp.getDocumentPart() instanceof XSSFDrawing);
555
562
556
        XSSFDrawing drawing = rp.getDocumentPart();
563
        XSSFDrawing drawing = rp.getDocumentPart();
557
        
564
558
        //sheet.createDrawingPatriarch() should return the same instance of XSSFDrawing
565
        //sheet.createDrawingPatriarch() should return the same instance of XSSFDrawing
559
        assertSame(drawing, sheet.createDrawingPatriarch());
566
        assertSame(drawing, sheet.createDrawingPatriarch());
560
        String drawingId = rp.getRelationship().getId();
567
        String drawingId = rp.getRelationship().getId();
Lines 569-602 Link Here
569
        assertTrue(shapes.get(0) instanceof XSSFSimpleShape);
576
        assertTrue(shapes.get(0) instanceof XSSFSimpleShape);
570
577
571
        XSSFSimpleShape textbox = (XSSFSimpleShape) shapes.get(0);
578
        XSSFSimpleShape textbox = (XSSFSimpleShape) shapes.get(0);
572
        
579
573
        List<XSSFTextParagraph> paras = textbox.getTextParagraphs();
580
        List<XSSFTextParagraph> paras = textbox.getTextParagraphs();
574
        assertEquals(3, paras.size());
581
        assertEquals(3, paras.size());
575
        
582
576
        assertEquals("Line 2", paras.get(1).getText());	// check content of second paragraph
583
        assertEquals("Line 2", paras.get(1).getText());	// check content of second paragraph
577
584
578
        assertEquals("Line 1\nLine 2\nLine 3", textbox.getText());	// check content of entire textbox
585
        assertEquals("Line 1\nLine 2\nLine 3", textbox.getText());	// check content of entire textbox
579
        
586
580
        // check attributes of paragraphs
587
        // check attributes of paragraphs
581
        assertEquals(TextAlign.LEFT, paras.get(0).getTextAlign());
588
        assertEquals(TextAlign.LEFT, paras.get(0).getTextAlign());
582
        assertEquals(TextAlign.CENTER, paras.get(1).getTextAlign());
589
        assertEquals(TextAlign.CENTER, paras.get(1).getTextAlign());
583
        assertEquals(TextAlign.RIGHT, paras.get(2).getTextAlign());
590
        assertEquals(TextAlign.RIGHT, paras.get(2).getTextAlign());
584
        
591
585
        Color clr = paras.get(0).getTextRuns().get(0).getFontColor(); 
592
        Color clr = paras.get(0).getTextRuns().get(0).getFontColor();
586
        assertArrayEquals(
593
        assertArrayEquals(
587
                new int[] { 255, 0, 0 } ,
594
                new int[] { 255, 0, 0 } ,
588
                new int[] { clr.getRed(), clr.getGreen(), clr.getBlue() });
595
                new int[] { clr.getRed(), clr.getGreen(), clr.getBlue() });
589
        
596
590
        clr = paras.get(1).getTextRuns().get(0).getFontColor(); 
597
        clr = paras.get(1).getTextRuns().get(0).getFontColor();
591
        assertArrayEquals(
598
        assertArrayEquals(
592
                new int[] { 0, 255, 0 } ,
599
                new int[] { 0, 255, 0 } ,
593
                new int[] { clr.getRed(), clr.getGreen(), clr.getBlue() });
600
                new int[] { clr.getRed(), clr.getGreen(), clr.getBlue() });
594
        
601
595
        clr = paras.get(2).getTextRuns().get(0).getFontColor(); 
602
        clr = paras.get(2).getTextRuns().get(0).getFontColor();
596
        assertArrayEquals(
603
        assertArrayEquals(
597
                new int[] { 0, 0, 255 } ,
604
                new int[] { 0, 0, 255 } ,
598
                new int[] { clr.getRed(), clr.getGreen(), clr.getBlue() });
605
                new int[] { clr.getRed(), clr.getGreen(), clr.getBlue() });
599
        
606
600
        checkRewrite(wb);
607
        checkRewrite(wb);
601
        wb.close();
608
        wb.close();
602
    }
609
    }
Lines 606-618 Link Here
606
     */
613
     */
607
    @Test
614
    @Test
608
    public void testAddBulletParagraphs() throws IOException {
615
    public void testAddBulletParagraphs() throws IOException {
609
    
616
610
        XSSFWorkbook wb1 = new XSSFWorkbook();
617
        XSSFWorkbook wb1 = new XSSFWorkbook();
611
        XSSFSheet sheet = wb1.createSheet();
618
        XSSFSheet sheet = wb1.createSheet();
612
        XSSFDrawing drawing = sheet.createDrawingPatriarch();
619
        XSSFDrawing drawing = sheet.createDrawingPatriarch();
613
620
614
        XSSFTextBox shape = drawing.createTextbox(new XSSFClientAnchor(0, 0, 0, 0, 2, 2, 10, 20));
621
        XSSFTextBox shape = drawing.createTextbox(new XSSFClientAnchor(0, 0, 0, 0, 2, 2, 10, 20));
615
        
622
616
        String paraString1 = "A normal paragraph";
623
        String paraString1 = "A normal paragraph";
617
        String paraString2 = "First bullet";
624
        String paraString2 = "First bullet";
618
        String paraString3 = "Second bullet (level 1)";
625
        String paraString3 = "Second bullet (level 1)";
Lines 623-629 Link Here
623
        String paraString8 = "Third bullet (level 1)";
630
        String paraString8 = "Third bullet (level 1)";
624
        String paraString9 = "Fourth bullet (level 1)";
631
        String paraString9 = "Fourth bullet (level 1)";
625
        String paraString10 = "Fifth Bullet";
632
        String paraString10 = "Fifth Bullet";
626
        
633
627
        XSSFTextParagraph para = shape.addNewTextParagraph(paraString1);
634
        XSSFTextParagraph para = shape.addNewTextParagraph(paraString1);
628
        para = shape.addNewTextParagraph(paraString2);
635
        para = shape.addNewTextParagraph(paraString2);
629
        para.setBullet(true);
636
        para.setBullet(true);
Lines 634-640 Link Here
634
641
635
        para = shape.addNewTextParagraph(paraString4);
642
        para = shape.addNewTextParagraph(paraString4);
636
        para.setBullet(true);
643
        para.setBullet(true);
637
        
644
638
        para = shape.addNewTextParagraph(paraString5);
645
        para = shape.addNewTextParagraph(paraString5);
639
        para = shape.addNewTextParagraph(paraString6);
646
        para = shape.addNewTextParagraph(paraString6);
640
        para.setBullet(ListAutoNumber.ARABIC_PERIOD);
647
        para.setBullet(ListAutoNumber.ARABIC_PERIOD);
Lines 642-648 Link Here
642
        para = shape.addNewTextParagraph(paraString7);
649
        para = shape.addNewTextParagraph(paraString7);
643
        para.setBullet(ListAutoNumber.ARABIC_PERIOD, 3);
650
        para.setBullet(ListAutoNumber.ARABIC_PERIOD, 3);
644
        para.setLevel(1);
651
        para.setLevel(1);
645
        
652
646
        para = shape.addNewTextParagraph(paraString8);
653
        para = shape.addNewTextParagraph(paraString8);
647
        para.setBullet(ListAutoNumber.ARABIC_PERIOD, 3);
654
        para.setBullet(ListAutoNumber.ARABIC_PERIOD, 3);
648
        para.setLevel(1);
655
        para.setLevel(1);
Lines 650-663 Link Here
650
        para = shape.addNewTextParagraph("");
657
        para = shape.addNewTextParagraph("");
651
        para.setBullet(ListAutoNumber.ARABIC_PERIOD, 3);
658
        para.setBullet(ListAutoNumber.ARABIC_PERIOD, 3);
652
        para.setLevel(1);
659
        para.setLevel(1);
653
        
660
654
        para = shape.addNewTextParagraph(paraString9);
661
        para = shape.addNewTextParagraph(paraString9);
655
        para.setBullet(ListAutoNumber.ARABIC_PERIOD, 3);
662
        para.setBullet(ListAutoNumber.ARABIC_PERIOD, 3);
656
        para.setLevel(1);
663
        para.setLevel(1);
657
        
664
658
        para = shape.addNewTextParagraph(paraString10);
665
        para = shape.addNewTextParagraph(paraString10);
659
        para.setBullet(ListAutoNumber.ARABIC_PERIOD);
666
        para.setBullet(ListAutoNumber.ARABIC_PERIOD);
660
        
667
661
        // Save and re-load it
668
        // Save and re-load it
662
        XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1);
669
        XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1);
663
        wb1.close();
670
        wb1.close();
Lines 665-682 Link Here
665
672
666
        // Check
673
        // Check
667
        drawing = sheet.createDrawingPatriarch();
674
        drawing = sheet.createDrawingPatriarch();
668
        
675
669
        List<XSSFShape> shapes = drawing.getShapes();
676
        List<XSSFShape> shapes = drawing.getShapes();
670
        assertEquals(1, shapes.size());
677
        assertEquals(1, shapes.size());
671
        assertTrue(shapes.get(0) instanceof XSSFSimpleShape); 
678
        assertTrue(shapes.get(0) instanceof XSSFSimpleShape);
672
        
679
673
        XSSFSimpleShape sshape = (XSSFSimpleShape) shapes.get(0);
680
        XSSFSimpleShape sshape = (XSSFSimpleShape) shapes.get(0);
674
        
681
675
        List<XSSFTextParagraph> paras = sshape.getTextParagraphs();
682
        List<XSSFTextParagraph> paras = sshape.getTextParagraphs();
676
        assertEquals(12, paras.size());  // this should be 12 as XSSFSimpleShape creates a default paragraph (no text), and then we added to that
683
        assertEquals(12, paras.size());  // this should be 12 as XSSFSimpleShape creates a default paragraph (no text), and then we added to that
677
        
684
678
        StringBuilder builder = new StringBuilder();
685
        StringBuilder builder = new StringBuilder();
679
        
686
680
        builder.append(paraString1);
687
        builder.append(paraString1);
681
        builder.append("\n");
688
        builder.append("\n");
682
        builder.append("\u2022 ");
689
        builder.append("\u2022 ");
Lines 706-718 Link Here
706
        builder.append("\n");
713
        builder.append("\n");
707
        builder.append("2. ");
714
        builder.append("2. ");
708
        builder.append(paraString10);
715
        builder.append(paraString10);
709
        
716
710
        assertEquals(builder.toString(), sshape.getText());
717
        assertEquals(builder.toString(), sshape.getText());
711
        
718
712
        checkRewrite(wb2);
719
        checkRewrite(wb2);
713
        wb2.close();
720
        wb2.close();
714
    }  
721
    }
715
    
722
716
    /**
723
    /**
717
     * Test reading bullet numbering from a textbox in an existing file
724
     * Test reading bullet numbering from a textbox in an existing file
718
     */
725
     */
Lines 747-762 Link Here
747
        XSSFDrawing drawing = sheet.createDrawingPatriarch();
754
        XSSFDrawing drawing = sheet.createDrawingPatriarch();
748
        List<XSSFShape> shapes = drawing.getShapes();
755
        List<XSSFShape> shapes = drawing.getShapes();
749
        assertEquals(4, shapes.size());
756
        assertEquals(4, shapes.size());
750
        
757
751
        XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1);
758
        XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1);
752
        wb1.close();
759
        wb1.close();
753
        sheet = wb2.getSheetAt(0);
760
        sheet = wb2.getSheetAt(0);
754
        drawing = sheet.createDrawingPatriarch();
761
        drawing = sheet.createDrawingPatriarch();
755
        
762
756
        shapes = drawing.getShapes();
763
        shapes = drawing.getShapes();
757
        assertEquals(4, shapes.size());
764
        assertEquals(4, shapes.size());
758
        wb2.close();
765
        wb2.close();
759
        
766
760
    }
767
    }
761
768
762
    @Test(expected=IllegalArgumentException.class)
769
    @Test(expected=IllegalArgumentException.class)
Lines 764-775 Link Here
764
        XSSFWorkbook wb = new XSSFWorkbook();
771
        XSSFWorkbook wb = new XSSFWorkbook();
765
        XSSFSheet sheet = wb.createSheet();
772
        XSSFSheet sheet = wb.createSheet();
766
        XSSFDrawing drawing = sheet.createDrawingPatriarch();
773
        XSSFDrawing drawing = sheet.createDrawingPatriarch();
767
        
774
768
        // first comment works
775
        // first comment works
769
        ClientAnchor anchor = new XSSFClientAnchor(1, 1, 2, 2, 3, 3, 4, 4);
776
        ClientAnchor anchor = new XSSFClientAnchor(1, 1, 2, 2, 3, 3, 4, 4);
770
        XSSFComment comment = drawing.createCellComment(anchor);
777
        XSSFComment comment = drawing.createCellComment(anchor);
771
        assertNotNull(comment);
778
        assertNotNull(comment);
772
        
779
773
        // Should fail if we try to add the same comment for the same cell
780
        // Should fail if we try to add the same comment for the same cell
774
        try {
781
        try {
775
            drawing.createCellComment(anchor);
782
            drawing.createCellComment(anchor);
Lines 778-783 Link Here
778
        }
785
        }
779
    }
786
    }
780
787
788
    @Test
789
    public void testGroupShape() throws Exception {
790
        XSSFWorkbook wb1 = new XSSFWorkbook();
791
        XSSFSheet sheet = wb1.createSheet();
792
        XSSFDrawing drawing = sheet.createDrawingPatriarch();
793
794
        XSSFSimpleShape s0 = drawing.createSimpleShape(drawing.createAnchor(0, 0, Units.pixelToEMU(30), Units.pixelToEMU(30), 1, 1, 10, 10));
795
        s0.setShapeType(ShapeTypes.RECT);
796
        s0.setLineStyleColor(100, 0, 0);
797
798
        XSSFShapeGroup g1 = drawing.createGroup(drawing.createAnchor(0, 0, 300, 300, 1, 1, 10, 10));
799
        CTGroupTransform2D xfrmG1 = g1.getCTGroupShape().getGrpSpPr().getXfrm();
800
801
        XSSFSimpleShape s1 = g1.createSimpleShape(new XSSFChildAnchor(
802
            (int)(xfrmG1.getChExt().getCx()*0.1),
803
            (int)(xfrmG1.getChExt().getCy()*0.1),
804
            (int)(xfrmG1.getChExt().getCx()*0.9),
805
            (int)(xfrmG1.getChExt().getCy()*0.9)
806
        ));
807
        s1.setShapeType(ShapeTypes.RECT);
808
        s1.setLineStyleColor(0, 100, 0);
809
810
        XSSFShapeGroup g2 = g1.createGroup(new XSSFChildAnchor(
811
            (int)(xfrmG1.getChExt().getCx()*0.2),
812
            (int)(xfrmG1.getChExt().getCy()*0.2),
813
            (int)(xfrmG1.getChExt().getCx()*0.8),
814
            (int)(xfrmG1.getChExt().getCy()*0.8)
815
        ));
816
        CTGroupTransform2D xfrmG2 = g2.getCTGroupShape().getGrpSpPr().getXfrm();
817
        
818
        XSSFSimpleShape s2 = g2.createSimpleShape(new XSSFChildAnchor(
819
            (int)(xfrmG2.getChExt().getCx()*0.1),
820
            (int)(xfrmG2.getChExt().getCy()*0.1),
821
            (int)(xfrmG2.getChExt().getCx()*0.9),
822
            (int)(xfrmG2.getChExt().getCy()*0.9)
823
        ));
824
        s2.setShapeType(ShapeTypes.RECT);
825
        s2.setLineStyleColor(0, 0, 100);
826
827
        XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1);
828
        wb1.close();
829
        
830
        XSSFDrawing draw = wb2.getSheetAt(0).getDrawingPatriarch();
831
        List<XSSFShape> shapes = draw.getShapes();
832
        assertEquals(2, shapes.size());
833
        assertTrue(shapes.get(0) instanceof XSSFSimpleShape);
834
        assertTrue(shapes.get(1) instanceof XSSFShapeGroup);
835
        shapes = draw.getShapes((XSSFShapeGroup)shapes.get(1));
836
        assertEquals(2, shapes.size());
837
        assertTrue(shapes.get(0) instanceof XSSFSimpleShape);
838
        assertTrue(shapes.get(1) instanceof XSSFShapeGroup);
839
        shapes = draw.getShapes((XSSFShapeGroup)shapes.get(1));
840
        assertEquals(1, shapes.size());
841
        assertTrue(shapes.get(0) instanceof XSSFSimpleShape);
842
843
        wb2.close();
844
    }
845
846
781
    private static void checkRewrite(XSSFWorkbook wb) throws IOException {
847
    private static void checkRewrite(XSSFWorkbook wb) throws IOException {
782
        XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb);
848
        XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb);
783
        assertNotNull(wb2);
849
        assertNotNull(wb2);

Return to bug 60521