Bug using TextFrame.Height when used with different fonts

Hi,

I think we may have found a bug in TextFrame.Height when used with different fonts.

The code below should create a slide with a single TextFrame. We add lots of text and try to remove one character at a time until it it is under a certain height.

This works when the font is the default Arial. However, if you change the font (as below) the number of characters removed remains constant.

This means that for Arial Narrow too many characters are removed, so you end up with half of the last line emtpy. With Verdana not enough characters are removed so the text overfows the rectangle.

Any help would be much appreciated.

Thanks,

Mike

protected void Page_Load(object sender, EventArgs e)

{

Response.Clear();

Response.ClearContent();

Response.ClearHeaders();

Response.ContentType = "application/vnd.ms-powerpoint";

Response.AddHeader("Content-Disposition", "attachment; filename=output.ppt");

string sContent = "Vivamus tincidunt tortor non diam euismod iaculis. Ut justo nulla, porta a iaculis a, elementum ac mauris. Pellentesque pulvinar, massa non aliquet dignissim, massa ligula tempus libero, non egestas nunc dolor rhoncus dui. Nam ipsum eros, ultricies et lobortis a, rutrum at turpis. Phasellus sollicitudin felis vitae erat adipiscing nec accumsan ante egestas. Vivamus consectetur nisi nec magna tincidunt quis condimentum massa molestie. Sed non lorem eget turpis venenatis vestibulum eget a nisl. Quisque vitae justo mi, in elementum lacus. Vestibulum condimentum leo in nibh gravida fermentum. Mauris venenatis orci vel sapien mollis non ullamcorper justo commodo. Integer imperdiet, sapien sit amet scelerisque tempor, nibh diam eleifend neque, a lobortis nunc eros et justo. Nam turpis magna, facilisis vel tincidunt at, aliquet vitae purus. Aliquam mauris justo, blandit euismod convallis condimentum, facilisis ac tortor. Vivamus tincidunt tortor non diam euismod iaculis. Ut justo nulla, porta a iaculis a, elementum ac mauris. Pellentesque pulvinar, massa non aliquet dignissim, massa ligula tempus libero, non egestas nunc dolor rhoncus dui. Nam ipsum eros, ultricies et lobortis a, rutrum at turpis. Phasellus sollicitudin felis vitae erat adipiscing nec accumsan ante egestas. Vivamus consectetur nisi nec magna tincidunt quis condimentum massa molestie. Sed non lorem eget turpis venenatis vestibulum eget a nisl. Quisque vitae justo mi, in elementum lacus. Vestibulum condimentum leo in nibh gravida fermentum. Mauris venenatis orci vel sapien mollis non ullamcorper justo commodo. Integer imperdiet, sapien sit amet scelerisque tempor, nibh diam eleifend neque, a lobortis nunc eros et justo. Nam turpis magna, facilisis vel tincidunt at, aliquet vitae purus. Aliquam mauris justo, blandit euismod convallis condimentum, facilisis ac tortor. Vivamus tincidunt tortor non diam euismod iaculis. Ut justo nulla, porta a iaculis a, elementum ac mauris. Pellentesque pulvinar, massa non aliquet dignissim, massa ligula tempus libero, non egestas nunc dolor rhoncus dui. Nam ipsum eros, ultricies et lobortis a, rutrum at turpis. Phasellus sollicitudin felis vitae erat adipiscing nec accumsan ante egestas. Vivamus consectetur nisi nec magna tincidunt quis condimentum massa molestie. Sed non lorem eget turpis venenatis vestibulum eget a nisl. Quisque vitae justo mi, in elementum lacus. Vestibulum condimentum leo in nibh gravida fermentum. Mauris venenatis orci vel sapien mollis non ullamcorper justo commodo. Integer imperdiet, sapien sit amet scelerisque tempor, nibh diam eleifend neque, a lobortis nunc eros et justo. Nam turpis magna, facilisis vel tincidunt at, aliquet vitae purus. Aliquam mauris justo, blandit euismod convallis condimentum, facilisis ac tortor. Vivamus tincidunt tortor non diam euismod iaculis. Ut justo nulla, porta a iaculis a, elementum ac mauris. Pellentesque pulvinar, massa non aliquet dignissim, massa ligula tempus libero, non egestas nunc dolor rhoncus dui. Nam ipsum eros, ultricies et lobortis a, rutrum at turpis. Phasellus sollicitudin felis vitae erat adipiscing nec accumsan ante egestas. Vivamus consectetur nisi nec magna tincidunt quis condimentum massa molestie. Sed non lorem eget turpis venenatis vestibulum eget a nisl. Quisque vitae justo mi, in elementum lacus. Vestibulum condimentum leo in nibh gravida fermentum. Mauris venenatis orci vel sapien mollis non ullamcorper justo commodo. Integer imperdiet, sapien sit amet scelerisque tempor, nibh diam eleifend neque, a lobortis nunc eros et justo. Nam turpis magna, facilisis vel tincidunt at, aliquet vitae purus. Aliquam mauris justo, blandit euismod convallis condimentum, facilisis ac tortor. ";

Presentation oPres = new Presentation();

oPres.Fonts[0].FontName = "Verdana";

Rectangle oRect = oPres.Slides[0].Shapes.AddRectangle(1000, 10, 4000, 1000);

TextFrame oFrame = oRect.AddTextFrame(sContent);

oFrame.AnchorText = AnchorText.TopBaseline;

oFrame.WrapText = true;

oFrame.FitShapeToText = true;

Portion oPortion = oFrame.Paragraphs[0].Portions[0];

oPortion.FontHeight = 10;

while (oRect.Height > 1500)

{

string sCurrentContent = oPortion.Text;

oPortion.Text = sCurrentContent.Remove(sCurrentContent.Length - 2);

}

oPres.Write(Response.OutputStream);

}

Hi,

I don't think there is such bug with font height. Different fonts require different space according to their structure. If you change the font of the text in rectangle produced with the code provided by you in MS Power Point, you can notice that the rectangle changes its height to different values according to the font selected. So, it is the font behavior that is reponsible for this, not the height.

Hi again,

Your response hasn't really helped me to fix my code. I think I might not have explained my problem clearly enough so I will try again now.

I want to be able to truncate the text in a rectangle so that the rectangle will end up with a height less than 1500.

I find that my truncate code works perfectly for Arial font, but other fonts are less reliable.

I have a new example of my problem, below. It creates three rectangles and places them side by side. All three rectangles are then treated exactly the same, except for the fonts they are assigned. Finally, the heights of the rectangles are written to the bottom of the slide.

I have attached the .PPT file that is created so you can see the problem for yourself. You should see that the Arial Narrow rectangle has too few characters. The Verdana rectangle has too many characters and spills out.

Here is the code I used:

protected void Page_Load(object sender, EventArgs e)

{

Response.Clear();

Response.ClearContent();

Response.ClearHeaders();

Response.ContentType = "application/vnd.ms-powerpoint";

Response.AddHeader("Content-Disposition", "attachment; filename=output.ppt");

string sContent = "Vivamus tincidunt tortor non diam euismod iaculis. Ut justo nulla, porta a iaculis a, elementum ac mauris. Pellentesque pulvinar, massa non aliquet dignissim, massa ligula tempus libero, non egestas nunc dolor rhoncus dui. Nam ipsum eros, ultricies et lobortis a, rutrum at turpis. Phasellus sollicitudin felis vitae erat adipiscing nec accumsan ante egestas. Vivamus consectetur nisi nec magna tincidunt quis condimentum massa molestie. Sed non lorem eget turpis venenatis vestibulum eget a nisl. Quisque vitae justo mi, in elementum lacus. Vestibulum condimentum leo in nibh gravida fermentum. Mauris venenatis orci vel sapien mollis non ullamcorper justo commodo. Integer imperdiet, sapien sit amet scelerisque tempor, nibh diam eleifend neque, a lobortis nunc eros et justo. Nam turpis magna, facilisis vel tincidunt at, aliquet vitae purus. Aliquam mauris justo, blandit euismod convallis condimentum, facilisis ac tortor. Vivamus tincidunt tortor non diam euismod iaculis. Ut justo nulla, porta a iaculis a, elementum ac mauris. Pellentesque pulvinar, massa non aliquet dignissim, massa ligula tempus libero, non egestas nunc dolor rhoncus dui. Nam ipsum eros, ultricies et lobortis a, rutrum at turpis. Phasellus sollicitudin felis vitae erat adipiscing nec accumsan ante egestas. Vivamus consectetur nisi nec magna tincidunt quis condimentum massa molestie. Sed non lorem eget turpis venenatis vestibulum eget a nisl. Quisque vitae justo mi, in elementum lacus. Vestibulum condimentum leo in nibh gravida fermentum. Mauris venenatis orci vel sapien mollis non ullamcorper justo commodo. Integer imperdiet, sapien sit amet scelerisque tempor, nibh diam eleifend neque, a lobortis nunc eros et justo. Nam turpis magna, facilisis vel tincidunt at, aliquet vitae purus. Aliquam mauris justo, blandit euismod convallis condimentum, facilisis ac tortor. Vivamus tincidunt tortor non diam euismod iaculis. Ut justo nulla, porta a iaculis a, elementum ac mauris. Pellentesque pulvinar, massa non aliquet dignissim, massa ligula tempus libero, non egestas nunc dolor rhoncus dui. Nam ipsum eros, ultricies et lobortis a, rutrum at turpis. Phas END OF STRING";

Presentation oPres = new Presentation();

Rectangle[] oRectangles = new Rectangle[3];

oRectangles[0] = oPres.Slides[0].Shapes.AddRectangle(0, 0, 4000, 1000);

oRectangles[1] = oPres.Slides[0].Shapes.AddRectangle(4100, 0, 4000, 1000);

oRectangles[2] = oPres.Slides[0].Shapes.AddRectangle(8200, 0, 4000, 1000);

string[] sFonts = {"Arial", "Arial Narrow", "Verdana"};

for(int ii = 0; ii<3; ii++)

{

Rectangle oRect = oRectangles[ii];

TextFrame oFrame = oRect.AddTextFrame(sContent);

oFrame.WrapText = true;

oFrame.FitShapeToText = true;

oFrame.AnchorText = AnchorText.Top;

Portion oPortion = oFrame.Paragraphs[0].Portions[0];

oPortion.FontHeight = 10;

//Set font:

FontEntity oFontEntityNew = new FontEntity(oPres, oPres.Fonts[0]);

oFontEntityNew.FontName = sFonts[ii];

oPortion.FontIndex = oPres.Fonts.Add(oFontEntityNew);

//Truncate:

while (oRect.Height > 1500)

{

//Remove one character:

oPortion.Text = oPortion.Text.Remove(oPortion.Text.Length - 1);

}

}

//Output the heights:

string sOutput = string.Format("Rectangle Heights: {0} | {1} | {2} ", oRectangles[0].Height, oRectangles[1].Height, oRectangles[2].Height);

sOutput += string.Format("\vTextFrame Heights: {0} | {1} | {2} ", oRectangles[0].TextFrame.Height, oRectangles[1].TextFrame.Height, oRectangles[2].TextFrame.Height);

Rectangle oRectOutput = oPres.Slides[0].Shapes.AddRectangle(0, 3000, 2000, 600);

oRectOutput.AddTextFrame(sOutput);

oRectOutput.TextFrame.WrapText = true;

oRectOutput.TextFrame.Paragraphs[0].Portions[0].FontHeight = 10;

oPres.Write(Response.OutputStream);

}

Hi,

I shall investigate the point raised by you and infrom you about the findings in a couple of days.

Hi,

I have investigated the issue with different scenarios. It seems that TextFrame.FitShapeToText resize the height and width of the text based on Arial / Default font instead of the font of the text in the shape. An issue with issue id 11874 has been created and you will be informed as soon as this issue is fixed by our product development team.

Thanks for the code examples to identify the issue.

Awesome! Thanks for doing this.

Is there anywhere on your site where I can track the progress of the issue, or could you give me a rough idea of timescale which I can pass on to my client?

Thanks again.

Hi,

We have an Issue Tracking System, but that is private for our internal processing. However, I shall discuss with our product development team lead and will inform you accordingly about the scheduled time scale.

Hi,

Just checking for an update as I havn't heard anything for a week.

Also, will the fix only be for version 4? As ask as we are currently using 3.1.1. It was the latest version when we started development.

Thanks

Hi,

Here are the findings and the estimated timescale on this issue as provided by the product development team:

"This happens when GDI and .NET Framework return little different value for the metrics of some fonts (for example "arial narrow", "arial black" and other "narrow", "black", "medium", "condensed" and etc. fonts) but not for all of them. As a result calculated height and width of a text is not absolutely correct. Each such font should be investigated separately and it takes a lot of time. This particular font will be investigated more carefully in the next 2-3 weeks."

Hi Muhammad,

Has there been any progress with Arial Narrow yet?

Thanks

Hi,

I have forwarded your request to the product development team and will respond to you as soon as i get information about the progress on this issue from the development team.

Hi,

This issue has been investigated and it has been found that it requires more time for fix. So, most probably it will be fixed by January release.

Hi again,

Could we please have an update as to the progress of this issue?

Are we still looking at a January release?

Thanks

Hi,

Thanks for your interest in Aspose.Slides.

Unfortunately, this issue (11874) is still under investigation and is yet to fix. For an estimated timeline for the fix of this issue, you will be updated her in a couple of days. Sorry for the inconvenience.

Thanks and Best Regards

Hi,

Thanks for your interest in Aspose.Slides.

Well, after detailed investigations, the exact source of problem has been identified. Unfortunately, MS Office uses standard GDI, whereas .NET Framework uses GDI+ for text rendering. Further, there is difference of text rendering by GDI and GDI+ as explained by Microsoft in this article:
http://support.microsoft.com/kb/307208
The best practice to address this difference has already been implemented in Aspose.Slides for .NET. It works perfectly for smaller text with larger font size, however, larger text with smaller font increases the probability of measurement differences. The comparison of the text as provided by you in the previous posts as used through MS PowerPoint and Aspose.Slides with Arial font has been attached. Due to this rendering difference, one wrapping error per 15 lines of text with Arial font having font size 10 has been observed. With other fonts, there will be more difference.

Hope this will be helpful to understand the limitations due to these text rendering differences.

Thanks and Best Regards