Bug 48415 - HSSFPicture.resize() works incorrectly if default font size changed
Summary: HSSFPicture.resize() works incorrectly if default font size changed
Status: RESOLVED WONTFIX
Alias: None
Product: POI
Classification: Unclassified
Component: HSSF (show other bugs)
Version: 3.5-FINAL
Hardware: PC Windows Server 2003
: P2 normal (vote)
Target Milestone: ---
Assignee: POI Developers List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2009-12-20 07:07 UTC by george.wei
Modified: 2009-12-21 08:30 UTC (History)
0 users



Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description george.wei 2009-12-20 07:07:45 UTC
Test steps:

1.1 Use Excel to new an Excel workbook
1.2 Save and close the workbook as Book1.xls
1.3 Use POI to open Book1.xls and insert an image
1.4 Call HSSFPicture.resize()
1.5 Save and close Book1.xls

2.1 Use Excel to new an Excel workbook
2.2 Modify the default font size (For example, from 10 to 20)
2.3 Save and close the workbook as Book2.xls
2.4 Use POI to open Book2.xls and insert the same image used in the previous test
2.5 Call HSSFPicture.resize()
2.6 Save and close Book2.xls

Then use Excel to open these two workbooks, you can see the same image in these workbooks do NOT have the same size! The later one stretchs horizontally!
Comment 1 Yegor Kozlov 2009-12-21 08:30:03 UTC
I'm re-posting my reply from poi-user:

HSSFPicture.resize() as well as XSSFPicture.resize() indeed work only for the default font which is Arial 10pt for .xls and Calibri 11pt for .xlsx.

Excel uses a funny coordinate system to position graphic objects. X and Y axes  are measured not in pixels but in units proportional to a character width of the default font. To properly position and size an image POI needs to translate the actual dimensions measured in pixels to columns and rows. This is the key of the problem. At the moment POI performs this translation using hardcoded constants obtained empirically for Arial 10pt and Calibri 11pt. It works OK in most cases and also explains why HSSFPicture.resize() is 'off' if the default font is changed.

Unfortunately it can not be easily fixed. To measure a custom font POI needs to load it (i.e. the font must be physically available) and get the font metrics using java.awt.* utilities. It will make the result machine dependent and impossible to test.

I updated the javadocs on HSSFPicture.resize()  to reflect it. 

Yegor