* checking whether file is landscape or portrait @ 2020-01-28 15:51 Pablo Rodriguez 2020-01-28 16:04 ` Taco Hoekwater 2020-01-28 16:15 ` Wolfgang Schuster 0 siblings, 2 replies; 9+ messages in thread From: Pablo Rodriguez @ 2020-01-28 15:51 UTC (permalink / raw) To: mailing list for ConTeXt users 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 ___________________________________________________________________________________ ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: checking whether file is landscape or portrait 2020-01-28 15:51 checking whether file is landscape or portrait Pablo Rodriguez @ 2020-01-28 16:04 ` Taco Hoekwater 2020-01-28 16:39 ` Pablo Rodriguez 2020-01-28 16:46 ` Wolfgang Schuster 2020-01-28 16:15 ` Wolfgang Schuster 1 sibling, 2 replies; 9+ messages in thread From: Taco Hoekwater @ 2020-01-28 16:04 UTC (permalink / raw) To: mailing list for ConTeXt users 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 <oinos@gmx.es> 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 ___________________________________________________________________________________ ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: checking whether file is landscape or portrait 2020-01-28 16:04 ` Taco Hoekwater @ 2020-01-28 16:39 ` Pablo Rodriguez 2020-01-28 16:46 ` Wolfgang Schuster 1 sibling, 0 replies; 9+ messages in thread From: Pablo Rodriguez @ 2020-01-28 16:39 UTC (permalink / raw) To: ntg-context 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 <oinos@gmx.es> 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 ___________________________________________________________________________________ ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: checking whether file is landscape or portrait 2020-01-28 16:04 ` Taco Hoekwater 2020-01-28 16:39 ` Pablo Rodriguez @ 2020-01-28 16:46 ` Wolfgang Schuster 1 sibling, 0 replies; 9+ messages in thread From: Wolfgang Schuster @ 2020-01-28 16:46 UTC (permalink / raw) To: ntg-context On Tue, 28 Jan 2020 17:04:52 +0100 Taco Hoekwater <taco@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 ___________________________________________________________________________________ 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 ___________________________________________________________________________________ ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: checking whether file is landscape or portrait 2020-01-28 15:51 checking whether file is landscape or portrait Pablo Rodriguez 2020-01-28 16:04 ` Taco Hoekwater @ 2020-01-28 16:15 ` Wolfgang Schuster 2020-01-28 16:43 ` Pablo Rodriguez 1 sibling, 1 reply; 9+ messages in thread From: Wolfgang Schuster @ 2020-01-28 16:15 UTC (permalink / raw) To: ntg-context On Tue, 28 Jan 2020 16:51:35 +0100 Pablo Rodriguez <oinos@gmx.es> 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 ___________________________________________________________________________________ ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: checking whether file is landscape or portrait 2020-01-28 16:15 ` Wolfgang Schuster @ 2020-01-28 16:43 ` Pablo Rodriguez 2020-01-29 13:31 ` Arthur Reutenauer 0 siblings, 1 reply; 9+ messages in thread From: Pablo Rodriguez @ 2020-01-28 16:43 UTC (permalink / raw) To: ntg-context 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 ___________________________________________________________________________________ ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: checking whether file is landscape or portrait 2020-01-28 16:43 ` Pablo Rodriguez @ 2020-01-29 13:31 ` Arthur Reutenauer 2020-01-29 15:44 ` Pablo Rodriguez 0 siblings, 1 reply; 9+ messages in thread From: Arthur Reutenauer @ 2020-01-29 13:31 UTC (permalink / raw) To: Mailing list for ConTeXt users 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<dimen1><relation><dimen2> (compares two dimensions) This is like \ifnum, but it compares two <dimen> 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<number1><relation><number2> (compares two integers) The <relation> 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 ___________________________________________________________________________________ ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: checking whether file is landscape or portrait 2020-01-29 13:31 ` Arthur Reutenauer @ 2020-01-29 15:44 ` Pablo Rodriguez 2020-01-30 12:08 ` context 0 siblings, 1 reply; 9+ messages in thread From: Pablo Rodriguez @ 2020-01-29 15:44 UTC (permalink / raw) To: ntg-context 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<dimen1><relation><dimen2> (compares two dimensions) > This is like \ifnum, but it compares two <dimen> 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<number1><relation><number2> (compares two integers) > The <relation> 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 ___________________________________________________________________________________ ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: checking whether file is landscape or portrait 2020-01-29 15:44 ` Pablo Rodriguez @ 2020-01-30 12:08 ` context 0 siblings, 0 replies; 9+ messages in thread From: context @ 2020-01-30 12:08 UTC (permalink / raw) To: mailing list for ConTeXt users 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<dimen1><relation><dimen2> (compares two dimensions) >> This is like \ifnum, but it compares two <dimen> 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<number1><relation><number2> (compares two integers) >> The <relation> 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 ___________________________________________________________________________________ ^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2020-01-30 12:08 UTC | newest] Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2020-01-28 15:51 checking whether file is landscape or portrait Pablo Rodriguez 2020-01-28 16:04 ` Taco Hoekwater 2020-01-28 16:39 ` Pablo Rodriguez 2020-01-28 16:46 ` Wolfgang Schuster 2020-01-28 16:15 ` Wolfgang Schuster 2020-01-28 16:43 ` Pablo Rodriguez 2020-01-29 13:31 ` Arthur Reutenauer 2020-01-29 15:44 ` Pablo Rodriguez 2020-01-30 12:08 ` context
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for NNTP newsgroup(s).