Bug 45140

Summary: TextShape.resizeToFitText() Not Properly Sizing TextShape
Product: POI Reporter: Nathan Mische <nmische>
Component: HSLFAssignee: POI Developers List <dev>
Status: RESOLVED FIXED    
Severity: normal    
Priority: P2    
Version: unspecified   
Target Milestone: ---   
Hardware: PC   
OS: Windows Vista   
Bug Depends on: 61169    
Bug Blocks:    
Attachments: Patch for resizeToFitText
Text overflowing its textbox

Description Nathan Mische 2008-06-05 07:42:59 UTC
Created attachment 22081 [details]
Patch for resizeToFitText

TextShape.resizeToFitText() does not appear to properly size the TextShape. Take for example the following code:

SlideShow ppt = new SlideShow();
Slide slide = ppt.createSlide();
TextBox title = slide.addTitle();
title.setText("Title Line One\nTitle Line Two");
title.resizeToFitText();
	    
//save changes 
FileOutputStream out = new FileOutputStream("test.ppt");
ppt.write(out);
out.close();

The title TextBox created by this example is much wider than the text and is only the height of a single line, although the title has multiple lines of text. 

From my investigation there appear to be two issues with the size calculation. 

First, the text doesn't seem to properly split into lines. The existing code attempts to split on \r but I've found it needs to split on \n.

Second, the TextShape height is set to the highest line, not the cumulative height of all of the lines.

I've attached a patch which in my testing seems to do a better job at sizing the TextShape.
Comment 1 Yegor Kozlov 2008-06-05 09:16:08 UTC
Thanks for the patch. It will be included in 3.1-final.

Regards,
Yegor
Comment 2 Yegor Kozlov 2008-06-08 05:32:56 UTC
Patch applied in r664492. Thanks.

Yegor
Comment 3 François Beaune 2017-06-08 14:52:14 UTC
Created attachment 35038 [details]
Text overflowing its textbox
Comment 4 François Beaune 2017-06-08 14:52:56 UTC
It looks like this no longer works properly.

The following code results in the text overflowing the textbox:

XSLFTextBox textBox = slide.createTextBox();
XSLFTextParagraph paragraph = textBox.getTextParagraphs().get(0);
XSLFTextRun textRun = paragraph.addNewTextRun();
textRun.setText("TRENDALERT | Summer trends for men.\n T-shirts and shirts with tropical prints. Bomberjacks are still a musthave.");
textBox.setAnchor(new Rectangle(left, top, width, 10000));
textBox.resizeToFitText();

I've attached a screenshot of the text overflowing the textbox in PowerPoint 2016: https://bz.apache.org/bugzilla/attachment.cgi?id=35038
Comment 5 François Beaune 2017-06-08 15:42:43 UTC
Ah, it appears that the bug was initially fixed in the HSLF API and not in the XSLF API, which we are using...