ruckertm / HINT

Sources and Documentation for the HINT project
http://hint.userweb.mwn.de/
Other
7 stars 1 forks source link

graphics inclusion #7

Closed davidcarlisle closed 1 year ago

davidcarlisle commented 1 year ago

in hint branches I have started to sketch a graphics.cfg to default hitex backend if latex is used , and a hitex.def backend file for latex color and graphicx packages.

https://github.com/latex3/graphics-cfg/blob/hint/graphics.cfg

https://github.com/latex3/graphics-def/blob/hint/hitex.def

Latex supports

\includegraphics[height=5cm]{example-image.png}

and

\includegraphics[scale=2]{example-image.png}

For most backends height is not supported and so latex converts height=5cm to scale=(5cm/<natural height>)

similarly if scale is not supported (as is the case with hitex) latex would convert scale=2 to height=(2*<natural height>)

Either way, latex currently assumes it can get the natural height (the natural size can be passed as an option, but such an option is not needed by currently used back ends)

Is it possible to get the natural size of an image?

Using copies of /usr/local/texlive/2023/texmf-dist/tex/latex/mwe/example-image{.jpg,.png} for testing,

these images are 400bp by 300bp.

I hoped

\setbox0\hbox{\HINTimage=example-image.jpg}

might work, but


aaa

\setbox0\hbox{\HINTimage=example-image.jpg}
\showthe\wd0

xxx

\bye

gives

> 8.75002pt.
l.5 \showthe\wd0

? 
! Missing } inserted.
<inserted text> 
                }
<to be read again> 
                   \vfill 
\bye ->\par \vfill 
                   \supereject \end 
l.9 \bye

? 

and if I explicitly terminate the \HINTimage with \relax I get no error but 0pt:

aaa

\setbox0\hbox{\HINTimage=example-image.jpg\relax}
\showthe\wd0

xxx

\bye

produces no error but

> 0.0pt.
l.5 \showthe\wd0

To avoid division by zero errors the draft hitex.def arbitrarily sets the natural size to 1in x 1in, this allows

1 \includegraphics{example-image.png}

2 \includegraphics[height=5cm]{example-image.png}

to include the image, and a scaled version, but the actual sizes are wrong.

Not directly related but the color mapping in the windows hintview seems off for png files,


aaa

\HINTimage=example-image.jpg  width 1in height 1in \relax

bbb

\HINTimage=example-image.png width 1in height 1in \relax

\bye

views as a brown png:

image

josephwright commented 1 year ago

We could use the same approach as for dvipdfmx and use extractbb - slightly slower but is works well enough

davidcarlisle commented 1 year ago

and use extractbb

I don't think shell escape is available

davidcarlisle commented 1 year ago

Incidentally pdftex, luatex and xetex/xdvipdfmx all agree on how to infer an intrinsic size of a bitmap, so it would be good if hitex could do the same so that example-image.png and example-image.jpg could both be included without specifying a size, producing a 400bp x 300bp box in both cases.

the texlive extractbb manual says:

extractbb (from dvipdfmx) uses density information if present. Otherwise, it generates bounding box with 100px = 100bp. This is what pdfTeX does.

ruckertm commented 1 year ago

Hi, the \HINTimage primitive works like an improved \input so you the file name needs to be terminated by a space or \relax. The equal sign is optional. I just changed the code so that all double quotes in the name are ignored but protect spaces in the file name. The present code allows quotes only at the beginning and the end. If the file name starts with { it can be any general text up to the closing } brace. So all this is leagal code

\hbox{\HINTimage = ei.jpg }% needs a space to terminate the name
\hbox{\HINTimage ei.jpg }% needs a space to terminate the name
\hbox{\HINTimage = ei.jpg\relax} %\relax terminates
\hbox{\HINTimage = "e i.jpg" } % needs a terminating space or \relax if quoted
\hbox{\HINTimage = {ei.jpg}} % no space if given as general text
\hbox{\HINTimage{ei.jpg}}
\hbox{\HINTimage = {prosa/ei.jpg}}
\hbox{\HINTimage = prosa/ei.jpg\relax}
ruckertm commented 1 year ago

Reading the image dimension from image files:

hitex can determine the size of an image file if the size is given. Both PNG and JPG files will always specify the dimension in pixels and may optionaly specify the resolution.

PNG may optionally specify the resolution either using pixel/meter as unit or with an undefined unit. The present code will not use the resolution if the unit is undefined. The new code will assume that the unit is pixel/inch. Because this is how it is used most of the time. If no resolution is given in the file, the new code will use the image dimension in pixel and assumes a resolution of 72.27 pixel/inch so 1pixel is 1pt

JPG may optionaly specify the resolution either using pixel/centimeter or pixel/inch as unit or again with an undefined unit. The present code will not use the resolution if the unit is undefined. The new code will ignore the undefined unit as before and proceeds as if no resolution was given. If no resolution is given in the file, the new code will use the image dimension in pixel and assumes a resolution of 72.27 pixel/inch so 1pixel is 1pt. If someone has a better idea what to do with a resolution in an undefined unit, please let me know. I will commit the new code (next week) after some more testing to the TeX Live repository and provide new executables in this repository.

ruckertm commented 1 year ago

The new code for hitex is now in the TeX Live repo. The current code for hintview (and binaries) are on https://github.com/ruckertm/HINT/ There are also four new testfiles under https://github.com/ruckertm/HINT/tree/main/hilatex/tests namely planimage1.tex plainimage2.tex lxtimage1.tex and ltximage2.tex

The sizes issue is fixed. Just remove the line \def\Gin@urx{72}\let\Gin@ury\Gin@urx from hitex.def

ruckertm commented 1 year ago

I moved the color problem to a separate issue