Skip to content

Fix notebook plots scaling on non-zero pixel ratio devices, remove margins - #12324

Closed
netique wants to merge 0 commit into
rstudio:cherry-blossomfrom
netique:main
Closed

netique wants to merge 0 commit into
rstudio:cherry-blossomfrom
netique:main

Conversation

@netique

@netique netique commented Nov 11, 2022 •

Copy link
Copy Markdown
Contributor

Intent

The PR attempts to resolve several issues pertaining wrong scaling of inline plots preview (also known as notebook plots). A tentative list of issues is as follows: #4521, #2098, #10825, #7028, #4913, rstudio/rmarkdown#2262, rstudio/rmarkdown#1827.

Approach

In NotebookPlots.cpp, a temporary device with a set of arguments is created with setNotebookGraphicsOption function living in the R module NotebookPlots.R. The device itself is created by another function defined herein – createNotebookGraphicsDevice. There are two situations to consider:

  1. fig.width and/or fig.height chunk options are not set and the preview resizes to fit the editor (automatic size behaviour in the source code lingo). In this case, the width is provided from ace_content getOffsetWidth and comes in pixels (height is calculated using the golden ratio, but the width is cut by 22 pixels to account for padding beforehand). So, the dimensions and theres is multiplied by a device pixel ratio to look the same on different DPIs (the PR does not touch this).
  2. Chunk options tied with plot dimensions are set by the user and we have to reflect that and show the plot with the exact properties. This is a central part of the PR. As opposed to situation 1, we get dimensions in inches. But using the same logic as in the situation 1, the current code doubles the user-demanded dimensions. The PR prohibits this and I believe that solves the issue. Note that the actual DPI is still affected by device pixel ratio deliberately to provide a smoother feel for high DPI users but not to compromise the physical dimensions!

To illustrate, here are two png plots with the same dimensions in inches, but vastly different DPI. Note that the appearance and physical dimensions do not change:

lowres         highres

# left plot
png("plot.png", width = 7, height = 5, units = "in", res = 96 * 0.5)
plot(pressure)
dev.off()

# right plot
png("plot.png", width = 7, height = 5, units = "in", res = 96 * 2.5)
plot(pressure)
dev.off()

Note that in this PR I made additional edit concerning figure margins. These were manually edited by setNotebookMargins function "called" in setNotebookGraphicsOption. I believe this margin manipulation is unwanted because it makes the actual plot different from the same plot in the rendered document. However, it may be retained for the situation 1.

Automated Tests

There are no tests provided.

QA Notes

As the changes introduced affect the very end of the "pipeline" creating the inline plot preview, I believe there is only this very thing to check. However, there is replayNotebookPlots function that calls the edited portions of code.

Checklist

  • If this PR adds a new feature, or fixes a bug in a previously released version, it includes an entry in NEWS.md
  • If this PR adds or changes UI, the updated UI meets accessibility standards
  • A reviewer is assigned to this PR (if unsure who to assign, check Area Owners list)
  • This PR passes all local unit tests

@netique

netique commented Nov 12, 2022

Copy link
Copy Markdown
Contributor Author

Another issue tightly connected with inline plot display is that with PlotSizeAutomatic, the display width is limited, but in case of PlotSizeManual it's not:

Snímek obrazovky 2022-11-12 v 17 04 36

This has to be changed on java/cpp side. Unfortunately, I cannot build RStudio from source on macOS Ventura (I really cannot replicate the build environment, neither wikis nor INSTALL helped me, so that holds me back...).

@timtmok timtmok added this to the Cherry Blossom milestone Nov 15, 2022
@timtmok
timtmok self-requested a review November 15, 2022 18:37
@timtmok

timtmok commented Nov 15, 2022

Copy link
Copy Markdown
Contributor

Thank you for the contribution! I'll take a look at the changes this week. The current release has reached a point where only critical fixes will be merged. After review, it will be merged once there's a branch for next release.

@jgutman
jgutman changed the base branch from main to cherry-blossom November 15, 2022 18:58
@jgutman
jgutman changed the base branch from cherry-blossom to main November 15, 2022 18:59
@jgutman
jgutman changed the base branch from main to cherry-blossom November 17, 2022 17:41
@jgutman

jgutman commented Nov 17, 2022

Copy link
Copy Markdown
Contributor

For testing purposes, we will probably want to choose one or more of the referenced issues to move into test, once this is merged.

@timtmok

timtmok commented Nov 17, 2022

Copy link
Copy Markdown
Contributor

Jackie has created PR #12350 to get this change rebased on cherry-blossom. I'll get this merged through that PR once the cherry-blossom branch is ready for this. I'll create another commit to get the NEWS update in the correct file. Just have to git move the NEWS file and have this change mentioned in the cherry-blossom NEWS.

timtmok added a commit that referenced this pull request Nov 28, 2022
Reopen Fix notebook plots scaling on non-zero pixel ratio devices, remove margins (#12324)
@jgutman

jgutman commented Nov 29, 2022

Copy link
Copy Markdown
Contributor

Thanks @netique, this has been merged.

@netique

netique commented Dec 2, 2022

Copy link
Copy Markdown
Contributor Author

@jgutman, @timtmok maybe you can close all of these now: #4521, #2098, #10825, #7028, #4913, rstudio/rmarkdown#2262, rstudio/rmarkdown#1827. But maybe I would be happy if the people commenting on those could try out the fix first – it's now available in the daily builds.

@ronblum

ronblum commented Dec 2, 2022

Copy link
Copy Markdown
Contributor

@netique Thank you so much for fixing this! I moved all of the issues that this resolves to Cherry Blossom so that we can verify that all of the issues are fixed. If any aren't, we'll switch them back to the former milestone, but hopefully they can all be closed. Thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants