[NTG-context] checking whether file is landscape or portrait

Wolfgang Schuster wolfgang.schuster.lists at gmail.com
Tue Jan 28 17:46:58 CET 2020


On Tue, 28 Jan 2020 17:04:52 +0100
Taco Hoekwater <taco at elvenkind.com> wrote:

> Hi Pablo,
> 
> I have had lots of problems with getting the correct figure dimensions
> for external images. In the end, I now use the code below. It is just
> as low-level and much more inefficient than your code, but it has not 
> failed me yet.
> 
>   \newdimen\MYfigurewidth
>   \newdimen\MYfigureheight
>   \unexpanded\def\MYgetfiguredimensions
>     {\dodoubleempty\MYdogetfiguredimensions}
>   
>   \def\MYdogetfiguredimensions[#1][#2]%
>     {\setbox0=\hbox{\externalfigure[#1][#2]}%
>      \MYfigurewidth=\wd0
>      \MYfigureheight=\ht0 } 
> 
> And used like:
> 
>        \MYgetfiguredimensions[cow.pdf][page=1]
>        \ifdim\MYfigurewidth>\MYfigureheight
>>        \fi
> 
> and I only use Hans’ \getfiguredimensions when I need to know a pdf page count.
> 
> It is not that Hans’ macro is bad, but external figures (especially PDF, 
> but also PNGs) can be very misbehaved.
> 
> Actually forcing the inclusion into a box is crude but at least it will 
> always return results identical to any actual desired inclusion.


I modified your code a bit to give it the ConTeXt touch.


Alternative 1:

\setupexternalfigures[location=default]

\starttext

\unexpanded\def\doifelselandscape
  {\dowithnextbox
     {\ifdim\nextboxwd>\nextboxht
        \expandafter\firstoftwoarguments
      \else
        \expandafter\secondoftwoarguments
      \fi}
   \hbox}

\doifelselandscape{\externalfigure[cow.pdf]}{YES}{NO}

\doifelselandscape{\externalfigure[mill.png]}{YES}{NO}

\blank

\unexpanded\def\doifelselandscapefigure#1#2%
  {\doifelselandscape{\externalfigure[#1][#2]}}

\doifelselandscapefigure{cow.pdf}{}{YES}{NO}

\doifelselandscapefigure{mill.png}{}{YES}{NO}

\stoptext


Alternative 2:

\setupexternalfigures[location=default]

\newif\iflandscape

\unexpanded\def\checkiflandscape
  {\dowithnextbox
     {\ifdim\nextboxwd>\nextboxht
        \landscapetrue
      \else
        \landscapefalse
      \fi}
   \hbox}

\starttext

\checkiflandscape{\externalfigure[cow.pdf]}

\iflandscape
  YES
\else
  NO
\fi

\checkiflandscape{\externalfigure[mill.png]}

\iflandscape
  YES
\else
  NO
\fi

\stoptext


Wolfgang


More information about the ntg-context mailing list