Bug 54210

Summary: When saving PPT to PNG, some text is rendered backwards
Product: POI Reporter: vincent.kirsch
Component: HSLFAssignee: POI Developers List <dev>
Status: RESOLVED FIXED    
Severity: major    
Priority: P2    
Version: 3.8-FINAL   
Target Milestone: ---   
Hardware: PC   
OS: All   
Attachments: This zip contains the PPT file and the PNG image resulting from the code above
Proposed patch generated with patch.xml
Text patch
A PPT test case and the result of the conversion

Description vincent.kirsch 2012-11-27 09:30:47 UTC
Created attachment 29643 [details]
This zip contains the PPT file and the PNG image resulting from the code above

When converting the attached PPT file with HSLF, and the code below, some of the text (but not all of it) is rendered as if seen in a mirror (see the PNG file attached).

Code snippet: 

InputStream is = getInputStream(); //A FileInputStream on the ppt file
SlideShow ppt = new SlideShow(is);

Dimension pgsize = ppt.getPageSize();
double scaleW = (double)TARGET_WIDTH / (double)pgsize.width;
double scaleH = (double)TARGET_HEIGHT / (double)pgsize.height;
double scale = Math.min(scaleW, scaleH);
int width = (int) (pgsize.width * scale);
int height = (int) (pgsize.height * scale);

Slide[] slide = ppt.getSlides();
for (int i = 0; i < slide.length; i++) 
{
	BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
	Graphics2D graphics = img.createGraphics();
	graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
	graphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
	graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
	graphics.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);

	graphics.setPaint(Color.white);
	graphics.clearRect(0, 0, width, height);

	graphics.scale((double)width/pgsize.width, (double)height/pgsize.height);

	slide[i].draw(graphics);

	// save the result
	File image = new File(root, "slide"+Integer.toString(i)+".png");
	ImageIO.write(img, "png", image);
}
Comment 1 Yegor Kozlov 2012-11-27 14:26:12 UTC
The trouble is with rendering text in flipped shapes. It appears that text in shapes follows the rotation transform, but does not follow the flip transform. That is, if a shape is rotated then any text inside this shape is rotated too, but if a shape is flipped then the text follows the original (not flipped) transform. In current implementation it is not so and the text in flipped shapes is painted upside down. 
PPTX2PNG has the same problem.
Comment 2 vincent.kirsch 2012-11-29 10:02:28 UTC
Here's a proposed patch that takes horizontal and vertic
Comment 3 vincent.kirsch 2012-11-29 10:05:45 UTC
Created attachment 29654 [details]
Proposed patch generated with patch.xml

Here's a proposed patch that takes horizontal and vertical flips, as well as rotation for TextPainter
Comment 4 Yegor Kozlov 2012-11-29 10:59:25 UTC
The patch is empty. Please try again.

(In reply to comment #3)
> Created attachment 29654 [details]
> Proposed patch generated with patch.xml
> 
> Here's a proposed patch that takes horizontal and vertical flips, as well as
> rotation for TextPainter
Comment 5 vincent.kirsch 2012-11-29 12:22:25 UTC
Created attachment 29656 [details]
Text patch

The gerenated gz patch does not seem to contain the patch; however, a patch.txt file is generated with the changes in it, so I attached that one instead, hope it'll be ok.
Comment 6 Yegor Kozlov 2012-12-02 12:27:01 UTC
Thanks for the patch, applied in r1416165 with some tweaks.

Yegor
Comment 7 vincent.kirsch 2012-12-03 06:51:57 UTC
You're welcome! Any idea ni what release it would be included and when that would be released?

(In reply to comment #6)
> Thanks for the patch, applied in r1416165 with some tweaks.
> 
> Yegor
Comment 8 Yegor Kozlov 2012-12-03 08:06:10 UTC
The fix will be included in POI-4.0 which is expected in 2013. I recommend to use the latest build from trunk which can be downloaded from https://builds.apache.org/job/POI/lastSuccessfulBuild/artifact/build/dist/

Yegor

(In reply to comment #7)
> You're welcome! Any idea ni what release it would be included and when that
> would be released?
> 
> (In reply to comment #6)
> > Thanks for the patch, applied in r1416165 with some tweaks.
> > 
> > Yegor
Comment 9 Yegor Kozlov 2012-12-03 08:20:22 UTC
The fix will be included in POI-4.0 which is expected in 2013. I recommend to use the latest build from trunk which can be downloaded from https://builds.apache.org/job/POI/lastSuccessfulBuild/artifact/build/dist/

Yegor

(In reply to comment #7)
> You're welcome! Any idea ni what release it would be included and when that
> would be released?
> 
> (In reply to comment #6)
> > Thanks for the patch, applied in r1416165 with some tweaks.
> > 
> > Yegor
Comment 10 vincent.kirsch 2013-04-18 14:42:45 UTC
Hello, do you have any idea of when version 4.0 will be released?

Alternatively, would it be possible to include the fix for this bug in in 3.9.X release?

Thanks.
Comment 11 Nick Burch 2013-04-18 15:06:39 UTC
It's unlikely to be very soon, as most of the committers to Apache POI are very busy, and there are a lot of contributed patches that need to be applied first

for now, you can either use a nightly build, or checkout from svn and build yourself
Comment 12 vincent.kirsch 2014-02-11 15:10:43 UTC
Hello,

I just did a test with POI 3.10-FINAL which according to http://poi.apache.org/changes.html should contain the fix (it's included since the beta-1), but I still have the image mirrored from the ppt in the attachemnt I sent when I opened the issue.

Note that to be able to test, the ppt must first be edited and the title's font changed from Calibri to anything else, because there seems to be a JDK bug that gives an ArrayOutOfBoundException when using the Calibri font. I changed to Arial and after that I still get the mirrored version. Would it be possible to have another look? 

Thanks,
Vincent.
Comment 13 Nick Burch 2014-02-13 12:05:14 UTC
It'd be helpful if you could create a test file with various flipped / rotated / flipped and rotated bits of text in it. Ideally with a few different fonts and sizes for each one. Maybe a few directional shapes too, while we're at it.

With that done, please attach the file, try rendering it with POI, and report back which ones work and which ones don't.

With that info, it'll be much easier to hone in on what's still a problem, and which bits we've already fixed
Comment 14 vincent.kirsch 2014-02-14 08:05:02 UTC
Well I attached a ZIP file when I created the issue; it contains a .ppt file which shows the issue when used with the code I mentioned in my first comment. 

To be honest I have no idea how the PPT was created as it came from a customer of mine (I just changed the text). But that file definitely causes the issue.

I re-attached it with the font changed as I previously explained that the JJVM can't seem to handle Calibri anymore, and put the result PNG I get after calling the aforementioned code.

Is that sufficient for you?
Comment 15 vincent.kirsch 2014-02-14 08:06:03 UTC
Created attachment 31312 [details]
A PPT test case and the result of the conversion
Comment 16 Andreas Beeker 2014-02-14 21:47:24 UTC
The backward.ppt fails in JDK 1.6.0_45, but works in JDK 1.7.0_45 (Windows) with the current POI trunk and the text is also displayed the same way as the MS PPT Viewer would do.
There's a quite recent bug report on IBM JDK [1], which is probably the same for Oracle/Sun JDK 1.6.

Is it really necessary to workaround the linebreak measurer stuff in POI?
(... maybe skip JDK 1.6 support and continue with 1.7 directly :) ...)

[1] http://www-01.ibm.com/support/docview.wss?uid=swg1IV47787
Comment 17 vincent.kirsch 2014-09-12 12:00:08 UTC
Hello, do you have any news about this?

Cheers
Vincent
Comment 18 Andreas Beeker 2014-09-15 16:52:08 UTC
Is this related to #55644, i.e. is this affected by the following JDK bug?

https://bugs.openjdk.java.net/browse/JDK-8013716
Comment 19 vincent.kirsch 2014-09-16 06:57:59 UTC
Hi,

thanks for your reply.

I only discovered what is described in the linked issue by trying to prepare a test case for the original issue, which is that some text was mirrored (inverted). 

I submitted a patch that was adapted and integrated a relatively long time ago (a few months); then I downloaded a version of POI that integrated that patch, but I still had the issue, and I created a test case, where I noticed the font issue.

So in short, no, it's not linked, and the original issue was still there last time I tried. That being said, I haven't tried since February, maybe it's been fixed in the meantime, but then I would have expected this issue to be updated :)

Cheers
Vincent
Comment 20 Andreas Beeker 2014-09-16 19:32:02 UTC
I've just validated the rendering.

The expected output is non-mirrored texts, right!?

On my Win7/64 machine with the 32-bit version of 1.6.45 but with the old fontmanager.dll of 1.6.43 it works. with the 1.6.45-fontmanager.dll I get the AIOOBE. with 1.7.45 it works.
Comment 21 Nick Burch 2014-09-17 06:48:54 UTC
I wonder if it's worth putting something on the site to list "known broken" JVMs for Apache POI? IIRC Apache Lucene do something similar for ones they have found to not work
Comment 22 vincent.kirsch 2015-08-31 15:37:56 UTC
Hi,

What is the status of this issue? 

I see it's marked as 'NEEDINFO', however I don't see what more I can provide.

The last comments brought some confusion to me;

The goal is indeed to not have flipped text, no matter the font used in the document.

Thanks!
Vincent
Comment 23 Andreas Beeker 2015-11-14 02:48:47 UTC
I've changed two issues as part of r1714290
- the default alignment of text is now top
- the old flipping code contained an unnecessary rotation, instead of it the 
code now traverses the parent group for flipping and supports text rotation, 
i.e. an additional rotation to the shape rotation
Comment 24 vincent.kirsch 2015-11-30 13:01:29 UTC
Hi,

Thanks very much.

Do you know in what version this will be available, and when it will be released?

Thanks,
Vincent.
Comment 25 Andreas Beeker 2015-11-30 14:25:35 UTC
We are working towards 3.14-Beta1 now - which will be available in 1-2 weeks.
I think most of the changes were already in 3.13 with the common-sl upgrade,
but the next beta contains the fixes mentioned in comment 23
Comment 26 vincent.kirsch 2015-11-30 14:31:47 UTC
OK thanks for the info. I'll try to find some time to test the 3.14 beta when it's out.