Skip to content

LaTeX: sync pdftex engine default imageresolution with pxunit - #8760

Merged
tk0miya merged 1 commit into
sphinx-doc:masterfrom
jfbu:pdftex_8253
Jan 25, 2021
Merged

tk0miya merged 1 commit into
sphinx-doc:masterfrom
jfbu:pdftex_8253

Conversation

@jfbu

@jfbu jfbu commented Jan 24, 2021

Copy link
Copy Markdown
Contributor

Closes: #8253

The 'pxunit' key from latex_elements instructs how to handle image
dimensions specified in px units.

But pdftex has \pdfimageresolution which is used when an image file does
not provide readable or legit values for the x and/or y resolution.

This commit syncs them: from 'pxunit' the default image resolution in
pixels per inch (an integer) is computed.

This way an image will behave the same if:

  • it is loaded with no explicit size set, and no readable image
    resolution data is readable from the file (or that data matches the
    'pxunit' setting)

  • or a size is set in figure directive using px units and equal to the
    natural pixel size of the image,

This also with 'lualatex' but is ignored by with 'xelatex' and
'uplatex'.

Bugfix

Relates

It is hard to provide unit test, because one would have to examine logs of a latex build, and the data inthere might change according to latex upgrades.

This will change the size of images loaded with no explicit dimension set, if the graphics file has no x-resolution or no such resolution understood by pdftex. For this reason I make this PR for merge into master.

@jfbu jfbu added this to the 4.0.0 milestone Jan 24, 2021
Closes: sphinx-doc#8253

The 'pxunit' key from latex_elements instructs how to handle image
dimensions specified in px units.

But pdftex has \pdfimageresolution which is used when an image file does
not provide readable or legit values for the x and/or y resolution.

This commit syncs them: from 'pxunit' the default image resolution in
pixels per inch (an integer) is computed.

This way an image will behave the same if:

- it is loaded with no explicit size set, _and_ no readable image
  resolution data is readable from the file (or that data matches the
  'pxunit' setting)

- or a size is set in figure directive using px units and equal to the
  natural pixel size of the image,

This also with 'lualatex' but is ignored by with 'xelatex' and
'uplatex'.
@jfbu

jfbu commented Jan 25, 2021

Copy link
Copy Markdown
Contributor Author

For testing this change, one can use this in index.rst:

.. figure:: image.png

   Caption

.. figure:: image.png
   :width: 223px

   Caption

where 223px stands for the natural width of image.png. I used this one
render_shader-nodes_input_volume-info_node
If testing with another image.png, replace 223px by its natural width.

After make latexpdf the two images should be same size in pdf, whatever the value of latex_elements['pxunit'].

@jfbu

jfbu commented Jan 25, 2021 •

Copy link
Copy Markdown
Contributor Author

Notice that this is breaking change for png images not providing the x-resolution/y-resolution data (which is in pHYs data chunk although the latter may serve only to specify a deviation of pixel shape from square one, and only optionally uses the meter absolute unit)

When included without any size specification and assuming 'pxunit' is not changed, they will be rendered at 96ppi compared to previously 72ppi, i.e. they are rendered smaller. This applies for example to more.png file in Sphinx own docs (getting started section). In this specific case, this looks like a bugfix, the image size fits better with the text baseline spread in the pdf output.

Using ImageMagick convert utility on unixen, one can set the image resolution in a png file this way:

convert -units PixelsPerInch image.png -density 192 image192dpi.png 

allowing to check that when a png file does have the pixels per inch (or rather pixels per meter in the pHYs data chunk), using the figure directive with no size spec will have pdftex insert the image in accordance to the pixel resolution set in the file, independently of the setting of \pdfimageresolution from Sphinx (according to 'pxunit').

@jfbu

jfbu commented Jan 25, 2021 •

Copy link
Copy Markdown
Contributor Author

To recap issue #8253 is caused by a number of factors:

  • the png file has no xresolution data understood by pdftex; pdftex then uses a default that there is 72 pixels to an inch
  • when one specifies explicitly a width in px, Sphinx uses a default of 96 pixels to an inch
  • the actual sample image provided had a pixel width of 223 not 200 (but this is an unrelated error)

It is possible, at least for pdflatex and lualatex, to set the default it uses for images whose data chunks contain no resolution indication. This pull request does this in a manner that makes 1px used in a width or height specification match with the default dpi used for images with no resolution data.

This means that from then on an image file will behave the same, if it has no resolution data, whether included with no size specification or included with a target width expressed in px and matching its natural x-size in px. This will work independently of the latex_elements['pxunit'] setting but the image width in pdf will depend on this setting (because this setting also will help tell pdftex what to use as unspecified pixel per inch resolution).

Nothing changes for

  • images loaded with specific target size, whether using or not px unit, whether or not their file data contains a pixel per inch (or per meter) spec,
  • images loaded with no specific target size, but having among their data a specified resolution which is understood by pdftex; the inclusion will then match that specified resolution and the resulting width is independent on how Sphinx is configured to handle px units; an image of natural width 300px and resolution of 100ppi ends up using 3in of horizontal space in pdf, if it is loaded with no specified target width, and this is independent of 'pxunit' setting. It behave that way prior to that PR and will still behave this way.

I don't know how to achieve this with xelatex and uplatex

@tk0miya
tk0miya merged commit 77fad1e into sphinx-doc:master Jan 25, 2021
@jfbu
jfbu deleted the pdftex_8253 branch January 31, 2021 15:17
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Jul 16, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants