-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Fixed drawingResize dimensions calculation #2123
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: v2
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your pull request. I've left some comments.
pixels = (width*maxDigitWidth + 0.5) + padding | ||
return math.Ceil(pixels) | ||
pixels = (width*maxDigitWidth + 0.5) | ||
return float64(int(pixels)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After this change, the math
import no longer required. Unit tests TestChartSize
and TestColWidth
should be update.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@xuri Fixed - thanks for the notice, I just pasted my local changes to the browser, sorry about this one
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The unit tests TestChartSize
and TestColWidth
test fail after this changes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry somehow I missed that there are affected tests - will be fixed
@@ -947,5 +947,5 @@ func convertRowHeightToPixels(height float64) float64 { | |||
if height == 0 { | |||
return 0 | |||
} | |||
return math.Ceil(4.0 / 3.4 * height) | |||
return height * 4.0 / 3.0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The math
import of this file also no longer required. Please run test locally by go test .
after changes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will do this from now on and this will be fixed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The image will overflow Sheet1!B30
cell after this changes, the image with AutoFit
inserted by this unit test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Sheet2!I9
cell image has been affected (the ratio of width to height increases), the image is widened after this changes. The image inserted by this unit test.
if float64(cellWidth) < width || float64(cellHeight) < height { | ||
aspWidth := float64(cellWidth) / width | ||
aspHeight := float64(cellHeight) / height | ||
var asp float64 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be simplify with:
aspWidth := float64(cellWidth) / width
aspHeight := float64(cellHeight) / height
asp := min(aspWidth, aspHeight)
width, height = width*asp, height*asp
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will be changed, thanks for the info
I fear that this is a very complex subject which needs to be discussed, please hear me out. First of all I just realized another issue of the current implementation and fork (at least from my point of view). If I understand you correctly you suggest, that AutoFit should add pictures into the target cell and resize the image in a way, that it is not rendered beyond the cell borders. However the current implementation changes the from anchor if the offset leaves the area of the cell. This means that a cell insertion targeting A1 can end up starting in A2. While the initial visible result seems to be correct there is an issue: The image will be moved if Cell A1 is being enlarged. This is not correct and I think that I should fix this as well. The starting anchor point can be outside of the cell dimensions which leads to correct anchoring and simply hides the image as long as the cell is not expanded enough. In the end this will still break your expectation, but just in another way. AutoFit would resize the image respecting the aspect ratio to the current cell dimensions and if an offset is used which causes the image to overflow the cell, it would look cut off, but this could be changed by the user, by expading the row / col size. However, from my understanding your expected behaviour comes along with various problematic edge cases and this is why I would like to suggest considering to review this feature:
Otherwise another option like "ResizeToCell" would be required or even target size fields for the picture struct. What are your thoughts about this? |
Yes, I agree with you, offsets shouldn't affect the image size. Users using AutoFit with Offset will be affected after these changes, like user case in #1560, I accept this, we can make these changes as a breaking change and notice user in release notes. |
PR Details
The current implementation does not seem to be in accordance to the specification:
https://github.com/closedxml/closedxml/wiki/Cell-Dimensions
https://github.com/closedxml/closedxml/wiki/Cell-Dimensions#mdw
https://github.com/closedxml/closedxml/wiki/Cell-Dimensions#skiasharp-code
Also there seem to be logical issues as well. This commit solves an identical issue as linked below.
Description
Looking at the C# ClosedXML Excel file Wiki:
Related Issue
#2001
Motivation and Context
In my use case I encountered the problems mentioned in the issue #2001 and these changes applied locally fixed my issues.
How Has This Been Tested
Try to add multiple images into a single cell using offsets as mentioned in #2001. Currently this will cause problems. Like mentioned earlier these changes solve these issues.
Types of changes
Checklist