[NTG-context] image file resolver in Lua?

Rudolf Bahr quasi at quasi.de
Mon Feb 5 15:12:12 CET 2018


On Sun, Feb 04, 2018 at 01:06:08PM +0100, Hans Hagen wrote:
> On 2/3/2018 7:57 AM, Rudolf Bahr wrote:
> > On Fri, Feb 02, 2018 at 04:17:32PM +0100, Henning Hraban Ramm wrote:
> > > Am 2018-02-02 um 12:08 schrieb Rudolf Bahr <quasi at quasi.de>:
> > > 
> > > > On Fri, Feb 02, 2018 at 08:40:08AM +0100, Henning Hraban Ramm wrote:
> > > > > Ahoi,
> > > > > 
> > > > > I’m using a Lua function to calculate image sizes, cropping etc.
> > > > > This snippet:
> > > > > 
> > > > > local filename = figures.current().status.fullname
> > > > > local pic = img.scan{filename = filename}
> > > > > 
> > > > > ... works most of the time, but sometimes the "current" image is the previous one, might be because I’m using layers and postponing.
> > > > > So I’d like to use just the same filename as I give to \externalfigure, i.e. without path and extension.
> > > > > How can I address the same file path resolver in my Lua function?
> > > > > 
> > > > 
> > > > Moin, Hraban,
> > > > 
> > > > where could I find something about those lua functions?
> > > 
> > > Maybe here: http://source.contextgarden.net/tex/context/base/mkiv/grph-inc.lua
> > > 
> > > The image placement functions I’m using are something Hans and others helped me accomplish during the last meeting; AFAIK there’s no real documentation.
> > > 
> > > Find attached the lua part. It’s probably more complicated than necessary
> > > 
> > > 
> > > I could work around my original problem: I’m using my image with \externalfigure in an invisible place first, so it’s current, and figures.current().status is the right one. :)
> > > 
> > > Here’s my (convoluted) placement macro:
> > > 
> > > % image on empty page, caption in margin
> > > % usage:
> > > % \startpostponing[pagenumber]
> > > % \pagefig[reference]{caption}{filename}
> > > % \stoppostponing
> > > % postponing is important, otherwise the page numbering gets wrong
> > > \def\pagefig{\dosingleempty\doPagefig}
> > > \def\doPagefig[#1]#2#3{
> > > % calculate image size
> > > \startfullpagemakeup
> > >    \externalfigure[#3][width=1mm] % dummy to set image as current
> > >    \def\myClipHeight{\ctxlua{ctxClipHeight("#3", \Resolution)}}
> > >    \def\mystartY{\ctxlua{ctxStartY("#3", \Resolution)}}
> > >    % image
> > >    \setlayerframed[bglayer][%
> > >      frame=off,
> > >      offset=overlay,
> > >      x=21mm,
> > >      y=\mystartY,
> > >      height=\myClipHeight,
> > >      width=\textwidth,
> > >      align=flushleft,
> > >      ]{%
> > >      \clip[
> > >        width=\textwidth,
> > >        height=\myClipHeight,
> > >        hoffset=0mm,
> > >        voffset=0mm, % i.e. clip bottom! TODO: options!
> > >        ]{\externalfigure[#3][width=\textwidth]}
> > > 	}
> > >    \textreference[pre#1]{\myImgHeight}
> > >    % caption
> > >    \setlayerframed[bglayer][%
> > >    frame=off,
> > >    offset=overlay,
> > >    linecorrection=on,
> > >    x=143mm,y=\dimexpr\mystartY + 3pt\relax,
> > >    height=\myClipHeight,
> > >    width=\outermarginwidth,
> > >    align={flushleft,low},
> > >    valign=bottom,
> > >    ]{%
> > >    \vbox{
> > >      \textreference[#1]{\strut #2}\vfill #2
> > >    }}
> > > \stopfullpagemakeup
> > > }
> > > 
> > > 
> > > usage example:
> > > 
> > > \startpostponing[82]
> > > \pagefig[alinari-stelzen]{Gebrüder Alinari (Florenz, 19. Jhd.)}{ka34/img/Gebr-Alinari_Kinder_auf_Stelzen}
> > > \stoppostponing
> > > 
> > > 
> > > 
> > > Greetlings, Hraban
> > > ---
> > > http://www.fiee.net
> > > http://wiki.contextgarden.net
> > > GPG Key ID 1C9B22FD
> > > 
> > 
> > Moin Hraban!
> > 
> > It's very interesting for me to see, how exactly you calculate
> > the size of images and texts with the help of lua!
> > 
> > Up to now I used to use python and a independend lua to provide some
> > necessary data for context. For instance python invokes graphicsmagick
> > in order to get the size of images. Because it's provided in px and not
> > in pt, so often some further manual work is unavoidable.
> > 
> > Now, since lua and metafun are well integrated into context it suggests
> > itself to use luatex instead of python, though I like this language
> > very much.
> > 
> > Thank you very much, Hraban, for putting your macros and links
> > at my disposal!
> Not sure what you want top know but a lot of image properties can be checked
> by prerolling:
> 
> \starttext
> 
> \getfiguredimensions[t:/sources/cow.pdf]
> 
> \starttabulate[|T|T|]
> \NC \string\figurewidth         \NC \figurewidth         \NC \NR
> \NC \string\figureheight        \NC \figureheight        \NC \NR
> \NC \string\figurexscale        \NC \figurexscale        \NC \NR
> \NC \string\figureyscale        \NC \figureyscale        \NC \NR
> \TB
> \NC \string\figuresize          \NC \figuresize          \NC \NR
> \NC \string\figurelabel         \NC \figurelabel         \NC \NR
> \NC \string\figurefileoriginal  \NC \figurefileoriginal  \NC \NR
> \NC \string\figurefilepage      \NC \figurefilepage      \NC \NR
> \NC \string\figurefileoptions   \NC \figurefileoptions   \NC \NR
> \NC \string\figurefileconversion\NC \figurefileconversion\NC \NR
> \NC \string\figurefileresolution\NC \figurefileresolution\NC \NR
> \NC \string\figurefilecolor     \NC \figurefilecolor     \NC \NR
> \NC \string\figurefilearguments \NC \figurefilearguments \NC \NR
> \NC \string\figurefilecache     \NC \figurefilecache     \NC \NR
> \NC \string\figurefileprefix    \NC \figurefileprefix    \NC \NR
> \TB
> \NC \string\figurenaturalwidth  \NC \figurenaturalwidth  \NC \NR
> \NC \string\figurenaturalheight \NC \figurenaturalheight \NC \NR
> \NC \string\figurexresolution   \NC \figurexresolution   \NC \NR
> \NC \string\figureyresolution   \NC \figureyresolution   \NC \NR
> \NC \string\figureorientation   \NC \figureorientation   \NC \NR
> \NC \string\figurerotation      \NC \figurerotation      \NC \NR
> \NC \string\figurexsize         \NC \figurexsize         \NC \NR
> \NC \string\figureysize         \NC \figureysize         \NC \NR
> \NC \string\figurecolordepth    \NC \figurecolordepth    \NC \NR
> \NC \string\figuredepth         \NC \figuredepth         \NC \NR
> \TB
> \NC \string\figurefullname      \NC \figurefullname      \NC \NR
> \NC \string\noffigurepages      \NC \noffigurepages      \NC \NR
> \TB
> \NC \string\figurefilepath      \NC \figurefilepath      \NC \NR
> \NC \string\figurefilename      \NC \figurefilename      \NC \NR
> \NC \string\figurefiletype      \NC \figurefiletype      \NC \NR
> \stoptabulate
> 
> \stoptext
> 
> 

This is very good and useful, thank you, Hans!

I need this to set a bunch of family history books with plenty of 
photos, texts, family trees, journey diaries, scans, index of persons etc. 
With the help of context+luatex+metafun I'd like to set the pages hopefully 
without the need of too much manual corrections, i.e in a as far as possible 
automated way. There is a lot of material, especially nowadays the 
flood of digital pictures. At the moment I succeed in setting only 2 or 3
such year-books per year.

Thanks again for your help,

Rudolf



More information about the ntg-context mailing list