Skip to content
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

Wrong text sizes from strheight and strwidth #739

Open
dvs opened this issue May 11, 2023 · 4 comments · May be fixed by #740
Open

Wrong text sizes from strheight and strwidth #739

dvs opened this issue May 11, 2023 · 4 comments · May be fixed by #740

Comments

@dvs
Copy link

dvs commented May 11, 2023

Hi,

It seems that graphics::strheight and graphics::strwidth return correct values only for the default plot size options(repr.plot.width = 7, repr.plot.height = 7.

Here is the code cell which should plot a text label inside a box:

library("graphics")

# Set plot's dimensions
options(repr.plot.width = 3, repr.plot.height = 3)      # strwidth and strheight are smaller than the text sizes
# options(repr.plot.width = 7, repr.plot.height = 7)    # OK
# options(repr.plot.width = 15, repr.plot.height = 15)  # strwidth and strheight are larger than the text sizes

# Set a string and font size
plot.new()
text_string <- "Hello, world!"
font_size <- 1.0

# Calculate height and width of the string
height <- strheight(text_string, cex=font_size)
width <-   strwidth(text_string, cex=font_size)

# Print the result
cat("The height of the string is:", height, "\n")
cat("The width of the string is:", height, "\n")

# Draw the string inside a rectangular box
x <- 0.7
y <- 0.5
text(x=x, y=y, pos=2, label=text_string, cex=font_size)
rect(x - width, y - 0.5 * height, x, y + 0.5 *height, border=TRUE)
# See that the box matches the text only for options(repr.plot.width = 7, repr.plot.height = 7)

Below are the output and the drawing:

The height of the string is: 0.02796961 
The width of the string is: 0.02796961

image

For PDF and PNG devices the same code draws the box properly for any plot size.
Perhaps, I didn't set some scaling option for IRkernel that should had prevented this scale discrepancy?
Thanks for your help!

@flying-sheep
Copy link
Member

Hi! I assume that’s because the default null device is 7ʺ×7ʺ:

init_null_device()

this could be fixed by initializing that device with the right size, I think.

@flying-sheep flying-sheep linked a pull request Jun 13, 2023 that will close this issue
@flying-sheep
Copy link
Member

Can you check if #740 solves your problem?

@dvs
Copy link
Author

dvs commented Jun 13, 2023

Can you check if #740 solves your problem?

Can I fix it in my Jupyter notebook's cell by setting some options or calling some methods?
Or you are suggesting forking IRKernel and changing init_null_device() code?

@dvs
Copy link
Author

dvs commented Jun 14, 2023

Sorry, please ignore my questions above, I overlooked the pull request.

I've installed this branch and the kernel dies with this message on console:

Error in .geometry(width, height, units, res) : 
  'res' must be specified unless 'units = "px"'
Calls: <Anonymous> ... dev.new -> do.call -> <Anonymous> -> ok_device -> .geometry
Execution halted

I hope you can make out what went wrong here.
Unfortunately, I have zero experience in R debugging.

Also in the commit I see a typo, repr.plot.width value in height assignment:

null_device <- function(
filename = null_filename,
width = getOption('repr.plot.width'),
height = getOption('repr.plot.width'),
units = 'in',
...
)

Thanks for your efforts!

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 a pull request may close this issue.

2 participants