Re: [NTG-context] checking whether file is landscape or portrait
Hello Pablo, also Lua might be useful for you - some code like the one bellow, which is based on an answer in this forum years ago: assert(not figures.getinfo2) figures.getinfo2 = function(name, page) -- [ [NTG-context] Pdf info with Lua/Ctx API ] if type(name) == "string" then name = { name = name, page = page } end if name.name then local data = figures.push(name) local info = figures.identify() if info.status.status ~= 0 then figures.check() -- !Counts pages here! end figures.pop() return --data info end end 'info' might return not only width and height of the image, but also its orientation/rotation. And it would be up to you whether to swap width/height depending on orientation. Hope this helps. Best regards, Lukas On 2020-01-29 16:44, Pablo Rodriguez wrote: On 1/29/20 2:31 PM, Arthur Reutenauer wrote: On Tue, Jan 28, 2020 at 05:43:36PM +0100, Pablo Rodriguez wrote: I’m afraid this is the second time I’m aware of the existence of \ifdim (the first time was when reading previous message from Taco 😅). It’s a TeX primitive. From the TeXbook, chapter 20: * \ifdim (compares two dimensions) This is like \ifnum, but it compares two values. For example, to test whether the value of \hsize exceeds 100pt, you can say ‘\ifdim\hsize>100pt’. The definition of \ifnum, right above it, states: * \ifnum (compares two integers) The must be either ‘<’ or ‘=’ or ‘>’. The two integer numbers are compared to each other in the usual way, and the result is true or false accordingly. Many thanks for your reply, Arthur. The explanation is extremely clear. I think I should try to read the TeXbook (but I need time for this first). Many thanks for your help, Pablo ___ If your question is of interest to others as well, please add an entry to the Wiki! maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context webpage : http://www.pragma-ade.nl / http://context.aanhet.net archive : https://bitbucket.org/phg/context-mirror/commits/ wiki : http://contextgarden.net ___
Re: [NTG-context] checking whether file is landscape or portrait
On 1/29/20 2:31 PM, Arthur Reutenauer wrote: > On Tue, Jan 28, 2020 at 05:43:36PM +0100, Pablo Rodriguez wrote: >> I’m afraid this is the second time I’m aware of the existence of \ifdim >> (the first time was when reading previous message from Taco 😅). > > It’s a TeX primitive. From the TeXbook, chapter 20: > > * \ifdim (compares two dimensions) > This is like \ifnum, but it compares two values. For > example, to test whether the value of \hsize exceeds 100pt, you > can say ‘\ifdim\hsize>100pt’. > > The definition of \ifnum, right above it, states: > > * \ifnum (compares two integers) > The must be either ‘<’ or ‘=’ or ‘>’. The two > integer numbers are compared to each other in the usual way, and > the result is true or false accordingly. Many thanks for your reply, Arthur. The explanation is extremely clear. I think I should try to read the TeXbook (but I need time for this first). Many thanks for your help, Pablo -- http://www.ousia.tk ___ If your question is of interest to others as well, please add an entry to the Wiki! maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context webpage : http://www.pragma-ade.nl / http://context.aanhet.net archive : https://bitbucket.org/phg/context-mirror/commits/ wiki : http://contextgarden.net ___
Re: [NTG-context] checking whether file is landscape or portrait
On Tue, Jan 28, 2020 at 05:43:36PM +0100, Pablo Rodriguez wrote: > I’m afraid this is the second time I’m aware of the existence of \ifdim > (the first time was when reading previous message from Taco 😅). It’s a TeX primitive. From the TeXbook, chapter 20: * \ifdim (compares two dimensions) This is like \ifnum, but it compares two values. For example, to test whether the value of \hsize exceeds 100pt, you can say ‘\ifdim\hsize>100pt’. The definition of \ifnum, right above it, states: * \ifnum (compares two integers) The must be either ‘<’ or ‘=’ or ‘>’. The two integer numbers are compared to each other in the usual way, and the result is true or false accordingly. Best, Arthur ___ If your question is of interest to others as well, please add an entry to the Wiki! maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context webpage : http://www.pragma-ade.nl / http://context.aanhet.net archive : https://bitbucket.org/phg/context-mirror/commits/ wiki : http://contextgarden.net ___
Re: [NTG-context] checking whether file is landscape or portrait
On Tue, 28 Jan 2020 17:04:52 +0100 Taco Hoekwater 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 ___ If your question is of interest to others as well, please add an entry to the Wiki! maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context webpage : http://www.pragma-ade.nl / http://context.aanhet.net archive : https://bitbucket.org/phg/context-mirror/commits/ wiki : http://contextgarden.net ___
Re: [NTG-context] checking whether file is landscape or portrait
On 1/28/20 5:15 PM, Wolfgang Schuster wrote: > On Tue, 28 Jan 2020 16:51:35 +0100 Pablo Rodriguez wrote: >> [...] >> At least, I would like to know how to get image dimension numbers >> suitable to be deployed with \ifnum. > > Use \ifdim to compare dimensions. Many thanks for your reply, Wolfgang. I’m afraid this is the second time I’m aware of the existence of \ifdim (the first time was when reading previous message from Taco 😅). Many thanks for your help, Pablo -- http://www.ousia.tk ___ If your question is of interest to others as well, please add an entry to the Wiki! maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context webpage : http://www.pragma-ade.nl / http://context.aanhet.net archive : https://bitbucket.org/phg/context-mirror/commits/ wiki : http://contextgarden.net ___
Re: [NTG-context] checking whether file is landscape or portrait
Many thanks for your reply, Taco. It helps me a lot to manipulate the images (which are actually PDF documents). I have another question about an issue that I experience at work. Some of the PDF documents I have to deal with are poorly generated. There are misplaced A4 pages inside legal paper sizes. I thought there were malformed in such a way: \showframe \setuppapersize[A4][legal] \starttext \dorecurse{10}{\input{zapf}} \stoptext So my approach was: \setuplayout[page] \starttext \getfiguredimensions[a-test.pdf] \page[right]\dorecurse{\noffigurepages} {\externalfigure[a-test.pdf][page=\recurselevel]} \stoptext But after a closer inspection, the docuemnts are properly malformed. They include some top and left extra margins. Is there any way to remote those extra margins? (Offset doesn’t seem to work here.) Many thanks for your help again, Pablo On 1/28/20 5:04 PM, Taco Hoekwater 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. > > Best wishes, > Taco > >> On 28 Jan 2020, at 16:51, Pablo Rodriguez wrote: >> >> Dear list, >> >> I have to check whether an external PDF document is portrait or >> landscape (in order to manipulate it in different ways). >> >> From what I understand of grph-inc.mkiv, I came with the following sample: >> >>\unprotect >>\def\figureXSize{\clf_figurestatus{width}{}} >>\def\figureYSize{\clf_figurestatus{height}{}} >>\protect >> >>\setupexternalfigures[location=default] >>\starttext >>\getfiguredimensions[cow.pdf] >>\figuresize\\ >>\figurelabel\\ >>\figureXSize\\ >>\figureYSize\\ >>\ifnum\figureXSize<\figureYSize portrait\else landscape\fi >>\stoptext >> >> I feel bad of abusing the ConTeXt source in such a miserable way. But >> I’m afraid this is the best I can. >> >> Is there no standard way to check portrait or landscape on a given >> figure? A conditional for that would be useful. >> >> At least, I would like to know how to get image dimension numbers >> suitable to be deployed with \ifnum. >> >> Many thanks for your help, >> >> Pablo -- http://www.ousia.tk ___ If your question is of interest to others as well, please add an entry to the Wiki! maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context webpage : http://www.pragma-ade.nl / http://context.aanhet.net archive : https://bitbucket.org/phg/context-mirror/commits/ wiki : http://contextgarden.net ___
Re: [NTG-context] checking whether file is landscape or portrait
On Tue, 28 Jan 2020 16:51:35 +0100 Pablo Rodriguez wrote: > Dear list, > > I have to check whether an external PDF document is portrait or > landscape (in order to manipulate it in different ways). > > From what I understand of grph-inc.mkiv, I came with the following sample: > > \unprotect > \def\figureXSize{\clf_figurestatus{width}{}} > \def\figureYSize{\clf_figurestatus{height}{}} > \protect > > \setupexternalfigures[location=default] > \starttext > \getfiguredimensions[cow.pdf] > \figuresize\\ > \figurelabel\\ > \figureXSize\\ > \figureYSize\\ > \ifnum\figureXSize<\figureYSize portrait\else landscape\fi > \stoptext > > I feel bad of abusing the ConTeXt source in such a miserable way. But > I’m afraid this is the best I can. > > Is there no standard way to check portrait or landscape on a given > figure? A conditional for that would be useful. > > At least, I would like to know how to get image dimension numbers > suitable to be deployed with \ifnum. Use \ifdim to compare dimensions. \setupexternalfigures[location=default] \starttext \getfiguredimensions[mill.png] \ifdim\figurewidth>\figureheight landscape% \else portrait% \fi \getfiguredimensions[cow.pdf] \ifdim\figurewidth>\figureheight landscape% \else portrait% \fi \stoptext Wolfgang ___ If your question is of interest to others as well, please add an entry to the Wiki! maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context webpage : http://www.pragma-ade.nl / http://context.aanhet.net archive : https://bitbucket.org/phg/context-mirror/commits/ wiki : http://contextgarden.net ___
Re: [NTG-context] checking whether file is landscape or portrait
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. Best wishes, Taco > On 28 Jan 2020, at 16:51, Pablo Rodriguez wrote: > > Dear list, > > I have to check whether an external PDF document is portrait or > landscape (in order to manipulate it in different ways). > > From what I understand of grph-inc.mkiv, I came with the following sample: > >\unprotect >\def\figureXSize{\clf_figurestatus{width}{}} >\def\figureYSize{\clf_figurestatus{height}{}} >\protect > >\setupexternalfigures[location=default] >\starttext >\getfiguredimensions[cow.pdf] >\figuresize\\ >\figurelabel\\ >\figureXSize\\ >\figureYSize\\ >\ifnum\figureXSize<\figureYSize portrait\else landscape\fi >\stoptext > > I feel bad of abusing the ConTeXt source in such a miserable way. But > I’m afraid this is the best I can. > > Is there no standard way to check portrait or landscape on a given > figure? A conditional for that would be useful. > > At least, I would like to know how to get image dimension numbers > suitable to be deployed with \ifnum. > > Many thanks for your help, > > Pablo > -- > http://www.ousia.tk > ___ > If your question is of interest to others as well, please add an entry to the > Wiki! > > maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context > webpage : http://www.pragma-ade.nl / http://context.aanhet.net > archive : https://bitbucket.org/phg/context-mirror/commits/ > wiki : http://contextgarden.net > ___ Taco Hoekwater Elvenkind BV ___ If your question is of interest to others as well, please add an entry to the Wiki! maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context webpage : http://www.pragma-ade.nl / http://context.aanhet.net archive : https://bitbucket.org/phg/context-mirror/commits/ wiki : http://contextgarden.net ___
[NTG-context] checking whether file is landscape or portrait
Dear list, I have to check whether an external PDF document is portrait or landscape (in order to manipulate it in different ways). From what I understand of grph-inc.mkiv, I came with the following sample: \unprotect \def\figureXSize{\clf_figurestatus{width}{}} \def\figureYSize{\clf_figurestatus{height}{}} \protect \setupexternalfigures[location=default] \starttext \getfiguredimensions[cow.pdf] \figuresize\\ \figurelabel\\ \figureXSize\\ \figureYSize\\ \ifnum\figureXSize<\figureYSize portrait\else landscape\fi \stoptext I feel bad of abusing the ConTeXt source in such a miserable way. But I’m afraid this is the best I can. Is there no standard way to check portrait or landscape on a given figure? A conditional for that would be useful. At least, I would like to know how to get image dimension numbers suitable to be deployed with \ifnum. Many thanks for your help, Pablo -- http://www.ousia.tk ___ If your question is of interest to others as well, please add an entry to the Wiki! maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context webpage : http://www.pragma-ade.nl / http://context.aanhet.net archive : https://bitbucket.org/phg/context-mirror/commits/ wiki : http://contextgarden.net ___