[NTG-context] image file resolver in Lua?

Rudolf Bahr quasi at quasi.de
Sat Feb 3 07:57:45 CET 2018


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!

Regards, Rudolf



More information about the ntg-context mailing list