Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

Merged
merged 27 commits into from
May 7, 2025
Merged

Fixed drawingResize dimensions calculation #2123

merged 27 commits into from
May 7, 2025

Conversation

R3dByt3
Copy link
Contributor

@R3dByt3 R3dByt3 commented Apr 28, 2025

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:

  • The conversion from pt to px was incorrect (row height).
  • The default conversion from MDW to px were incorrect (column width - there is no margin) - also this calculation should depend on the used font; To be honest I have not checked if and how this could be achieved within the project.
  • The aspect ratio calculation has been simplified
  • The offset values were subtracted from the image dimensions which is simply not correct

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

  • Docs change / refactoring / dependency upgrade
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

Copy link
Member

@xuri xuri left a 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.

@xuri xuri added the size/S Denotes a PR that changes 10-29 lines, ignoring generated files. label Apr 28, 2025
@R3dByt3 R3dByt3 requested a review from xuri April 28, 2025 14:44
Copy link
Member

@xuri xuri left a 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.
Image position has been affect by the pull request 2123

Copy link
Member

@xuri xuri left a 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.
changes

@R3dByt3
Copy link
Contributor Author

R3dByt3 commented Apr 29, 2025

The image will overflow Sheet1!B30 cell after this changes, the image with AutoFit inserted by this unit test. Image position has been affect by the pull request 2123

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:

  1. If the same image is added twice with the same settings except the offset, it might be possible, that both images end up with different sizes. Should varying offsets really affect the image size?
  2. Consider the case of adding multiple images or dynamically calculating cell sizes and images positions - Would it really be the expected behaviour, if there are only a few pixels left from the start anchor, that an image is added with for example 5x5, 1x1 pixels?
  3. When adding multiple images dynamically, maybe even with varying native sizes, this may be not the desired behaviour like in my case and can only be fixed by calculating the required col / row size for all rows / cols in advance.

Otherwise another option like "ResizeToCell" would be required or even target size fields for the picture struct.

What are your thoughts about this?

@xuri
Copy link
Member

xuri commented Apr 29, 2025

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.

@xuri
Copy link
Member

xuri commented Apr 30, 2025

Any update for other remaining code review issues?

@R3dByt3
Copy link
Contributor Author

R3dByt3 commented Apr 30, 2025

Im working on it hoping to complete it today, from what I have noticed the anchoring wasnt fully correct and confused me a bit.

@R3dByt3 R3dByt3 requested a review from xuri April 30, 2025 12:08
@R3dByt3
Copy link
Contributor Author

R3dByt3 commented Apr 30, 2025

@xuri I think its finally done

Some explanations:

  • oneCellAnchors (<=> InsertionMode Picture shall move with cell) were not fully implemented, which could lead to images not beeing shown - fixed

  • oneCellAnchors have not been adjusted on col / row insertion - fixed

  • insertionPositionCalculation does not modify anymore the targeted starting cell => from my point of view the targeted starting cell must not be modified, since the resulting behaviour will otherwise not be equal to the requested one:

grafik
(Images get seperated by cell overflows)

  • updated some excel default values and unit conversion formulas => at least for me (a german office 365 user) these numbers seem to work a little better, but the best case would be to include some font based calculations

  • row and col functions to retrieve the height / width did seem to handle the hidden row / col case incorrectly - fixed

Please notice regarding the anchoring behaviour:
Using a twoCellAnchor (default) the starting cell and its size are the most top left point of the area of insertion; A twoCellAnchor with offsets will cause headaches, since the image will start scaling based on its own size and cell sizes etc. Also it is not possible to have an offset size > the area size of the starting cell.

@xuri xuri added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Apr 30, 2025
Copy link
Member

@xuri xuri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After these changes, add 60px x 60px image to test issue #2001, the size of the last 3 images is incorrect.

You can download this image attachment:

pic.jpg

The result:

@R3dByt3
Copy link
Contributor Author

R3dByt3 commented May 2, 2025

After these changes, add 60px x 60px image to test issue #2001, the size of the last 3 images is incorrect.

You can download this image attachment:

pic.jpg

The result:

I will try to reproduce this issue and add a test case for it 👍 (Propably on monday)

@xuri
Copy link
Member

xuri commented May 5, 2025

Please set the target branch to master instead of v2. Create branch from master.

@R3dByt3
Copy link
Contributor Author

R3dByt3 commented May 5, 2025

I fixed the absolute anchoring issue - the issue arised from updating the anchors since oneCellAnchors have absolute sizes.

@R3dByt3 R3dByt3 requested a review from xuri May 5, 2025 07:42
@R3dByt3 R3dByt3 changed the base branch from v2 to master May 5, 2025 07:44
Copy link
Member

@xuri xuri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your update. I test issue #2001, all the size of the images is correct, but the position of the last image is incorrect. If the value of OffsetX and OffsetY is over than anchor cell column width or height, it won't work.

image

Expected:

image

@R3dByt3
Copy link
Contributor Author

R3dByt3 commented May 6, 2025

Hopefully this covers all your questions or issues: If we are talking about oneCellAnchors, Excel starts overlapping images from last to first if the images do not fit into the column. This can be observed by resizing the column. Implementing an overflow by hand creates other issues like I have mentioned earlier (image separation; incorrect anchor cell, which causes unexpected movements on column resizing/hiding). At least currently I do not know about a property or similar to control or override this behavior of excel regarding too many images for column size X.

Does this answer your question or did I miss something?

- Shorten parameters for the positionObjectPixels function
Copy link
Member

@xuri xuri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The positionObjectPixels function has too many parameters, I pushed a commit on your branch, combine some parameters to made it shorten and simplicity.

Copy link

codecov bot commented May 6, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.23%. Comparing base (3650e5c) to head (7178a11).
Report is 10 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff            @@
##           master    #2123    +/-   ##
========================================
  Coverage   99.22%   99.23%            
========================================
  Files          32       32            
  Lines       30114    30222   +108     
========================================
+ Hits        29882    29991   +109     
+ Misses        154      153     -1     
  Partials       78       78            
Flag Coverage Δ
unittests 99.23% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@xuri xuri merged commit 8fdc26e into qax-os:master May 7, 2025
17 checks passed
Copy link
Member

@xuri xuri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks for your contribution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
Status: Bugfix
Development

Successfully merging this pull request may close these issues.

Excelize floating images support editAs="oneCell" attribute Possible bugs in AddPictureFromBytes method.
2 participants