Skip to content

Fix blurry inline notebook plots on HiDPI displays - #17100

Merged
gtritchie merged 1 commit into
mainfrom
bugfix/notebook-plot-dpi
Feb 24, 2026
Merged

gtritchie merged 1 commit into
mainfrom
bugfix/notebook-plot-dpi

Conversation

@kevinushey

Copy link
Copy Markdown
Collaborator

Intent

Addresses #16838.

Summary

  • Fixes a regression where inline notebook/Quarto plots rendered with custom figure dimensions (fig-width, fig-height, fig-asp) were blurry on HiDPI/Retina displays
  • The device pixel ratio was only being applied to DPI when using automatic (pixel-based) sizing, not when using manual (inch-based) sizing
  • Moves the dpi * pixelRatio scaling outside the units == "px" conditional so it applies to both sizing modes

Details

Commit 98bc0203b5 ("support custom DPI in chunk plots") refactored NotebookPlots.R and inadvertently moved the DPI pixel-ratio scaling inside a conditional block that only executes for automatic sizing. Previously, DPI was unconditionally scaled via res = 96 * pixelRatio.

When a user specifies custom figure dimensions, the sizing mode is set to PlotSizeManual with units = "in", which skipped the pixel ratio multiplication on DPI. This produced images at 96 DPI instead of 192 DPI on 2x Retina displays (e.g., 672x480 instead of 1344x960 for a 7x5 inch plot).

Test plan

  • Open a Quarto or R Notebook document on a HiDPI/Retina display
  • Run a chunk with default figure settings — verify the plot is crisp
  • Run a chunk with custom fig-width/fig-height — verify the plot is equally crisp
  • Run a chunk with fig-asp set — verify the plot is crisp
  • Verify plots are also crisp on a non-HiDPI display (pixel ratio = 1)

When custom figure dimensions (e.g. fig-width, fig-height, fig-asp)
were specified, the device pixel ratio was not applied to the plot
DPI, resulting in low-resolution images on HiDPI/Retina displays.

This was a regression from 98bc020, which moved the DPI scaling
inside a conditional that only applied to automatic (pixel-based)
sizing. Move the DPI scaling back outside the conditional so it
applies regardless of sizing mode.

Addresses #16838.
@kevinushey
kevinushey requested a review from gtritchie February 24, 2026 19:14
@kevinushey kevinushey added this to the Globemaster Allium milestone Feb 24, 2026
@posit-snyk-bot

posit-snyk-bot commented Feb 24, 2026 •

Copy link
Copy Markdown
Contributor

✅ Snyk checks have passed. No issues have been found so far.

Status Scanner Critical High Medium Low Total (0)
✅ Open Source Security 0 0 0 0 0 issues
✅ Licenses 0 0 0 0 0 issues

💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse.

@gtritchie gtritchie left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM!

@gtritchie
gtritchie merged commit 0765eb6 into main Feb 24, 2026
8 checks passed
@gtritchie
gtritchie deleted the bugfix/notebook-plot-dpi branch February 24, 2026 20:33
gtritchie added a commit that referenced this pull request Feb 28, 2026
- Fix TOCTOU race in buildCspHeader by loading atomic port into a local
  variable instead of reading it twice
- Fix thread-unsafe caching in loadCspDirectives by using a C++11
  static-initialized lambda instead of a manual bool guard
- Add WLOG calls for file read, JSON parse, and schema errors in
  loadCspDirectives so failures are no longer silently swallowed
- Add DLOG calls in injectThemeInfo when expected HTML tags are missing
- Replace magic number with constexpr + strlen for class attribute offset
- Fix inaccurate @return doc on handleAIChatRequest
- Fix incomplete doc on setChatBackendPort (also called on stop)
- Remove redundant inline comments covered by function-level docs

Addresses #17100.
jcheng5 pushed a commit that referenced this pull request Feb 28, 2026
* Add CSP headers and meta-tag theme injection for AI chat pane

RStudio serves the chat UI HTML directly via rsession, bypassing the
databot Express server's CSP middleware. Add CSP header support to the
static file handler so the browser enforces Content-Security-Policy in
RStudio mode.

CSP directives are read from dist/csp.json (emitted by the databot
build) so the policy stays in sync without hardcoding values in C++.
The only RStudio-specific augmentation is adding the WebSocket origin
to connect-src in desktop mode.

Replace the inline <script> theme injection with a <meta> tag carrying
theme colors as data attributes plus a class="dark" on the <html> tag.
This avoids needing a CSP nonce for inline scripts.

* Use existing string_utils::htmlEscape for attribute escaping

Replace the local htmlEscapeAttr() with the existing
string_utils::htmlEscape(str, isAttributeValue=true) from
core/StringUtils, which handles the same characters plus
carriage returns and newlines.

* Fix thread safety, add logging, and improve docs in CSP code

- Fix TOCTOU race in buildCspHeader by loading atomic port into a local
  variable instead of reading it twice
- Fix thread-unsafe caching in loadCspDirectives by using a C++11
  static-initialized lambda instead of a manual bool guard
- Add WLOG calls for file read, JSON parse, and schema errors in
  loadCspDirectives so failures are no longer silently swallowed
- Add DLOG calls in injectThemeInfo when expected HTML tags are missing
- Replace magic number with constexpr + strlen for class attribute offset
- Fix inaccurate @return doc on handleAIChatRequest
- Fix incomplete doc on setChatBackendPort (also called on stop)
- Remove redundant inline comments covered by function-level docs

Addresses #17100.

* Address PR review feedback on CSP and theme injection

- Restrict theme injection to index.html only (not all HTML files)
- Cache built CSP header string with mutex; rebuild only on port change
- Fix port comparison to use >= 0 (port 0 is valid; sentinel is -1)
- Add frame-ancestors 'self' to prevent clickjacking
- Add kChatBackendPortNone sentinel constant, replace magic -1
- Remove unused getChatBackendPort()
- Consolidate meta tag format to single raw string
- Clarify HTML parsing comment about Vite output constraints
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants