From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.io/gmane.comp.tex.context/99998 Path: news.gmane.org!.POSTED!not-for-mail From: Henning Hraban Ramm Newsgroups: gmane.comp.tex.context Subject: Re: image file resolver in Lua? Date: Fri, 2 Feb 2018 16:17:32 +0100 Message-ID: References: <20180202110802.z5bujyqssvvi3a3s@nan> Reply-To: mailing list for ConTeXt users NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 (Mac OS X Mail 7.3 \(1878.6\)) Content-Type: multipart/mixed; boundary="Apple-Mail=_9E1DFA5E-2574-42A4-B969-752DC55F70F4" X-Trace: blaine.gmane.org 1517584611 9965 195.159.176.226 (2 Feb 2018 15:16:51 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Fri, 2 Feb 2018 15:16:51 +0000 (UTC) To: mailing list for ConTeXt users Original-X-From: ntg-context-bounces@ntg.nl Fri Feb 02 16:16:47 2018 Return-path: Envelope-to: gctc-ntg-context-518@m.gmane.org Original-Received: from zapf.boekplan.nl ([5.39.185.232] helo=zapf.ntg.nl) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1ehd4t-0001cL-5l for gctc-ntg-context-518@m.gmane.org; Fri, 02 Feb 2018 16:16:35 +0100 Original-Received: from localhost (localhost [127.0.0.1]) by zapf.ntg.nl (Postfix) with ESMTP id 7B41C42861F; Fri, 2 Feb 2018 16:18:21 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at zapf.boekplan.nl Original-Received: from zapf.ntg.nl ([127.0.0.1]) by localhost (zapf.ntg.nl [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id NlK7TDs3RP_J; Fri, 2 Feb 2018 16:18:06 +0100 (CET) Original-Received: from zapf.ntg.nl (localhost [IPv6:::1]) by zapf.ntg.nl (Postfix) with ESMTP id 45332428628; Fri, 2 Feb 2018 16:18:00 +0100 (CET) Original-Received: from localhost (localhost [127.0.0.1]) by zapf.ntg.nl (Postfix) with ESMTP id A023142862D for ; Fri, 2 Feb 2018 16:17:59 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at zapf.boekplan.nl Original-Received: from zapf.ntg.nl ([127.0.0.1]) by localhost (zapf.ntg.nl [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id FYhYJu3CWOdQ for ; Fri, 2 Feb 2018 16:17:45 +0100 (CET) Original-Received: from fiee.net (feronia.fiee.net [46.163.112.221]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by zapf.ntg.nl (Postfix) with ESMTPS id 9AF9E428628 for ; Fri, 2 Feb 2018 16:17:32 +0100 (CET) Original-Received: from [192.168.178.36] (p5B293F06.dip0.t-ipconnect.de [91.41.63.6]) by mail.fiee.net (Postfix) with ESMTPSA id 8FF5F8023A for ; Fri, 2 Feb 2018 16:17:31 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fiee.net; s=default; t=1517584651; bh=J7AzlgZ3AxUJ77LCVO23Ru3rrkaphVNcD7dLBniQzBU=; l=5916; h=From:Subject:To; b=LuabeRoHKuQNuT0wAmQH/CXiURqKqIpRcuPLG2mKHQglXOAFUx6NvvKlI7pbMARmM DrkxGXok1daiIYTTfm/AmEgycDUSG3K0qEaoM+HUBaMfav6pMQLZO0Vd/CWcaqQLgT /haaMvi/mR93UCP8GbIaLrS6yzziBOxBRsjgVuBw= Authentication-Results: feronia.fiee.net; spf=pass (sender IP is 91.41.63.6) smtp.mailfrom=texml@fiee.net smtp.helo=[192.168.178.36] Received-SPF: pass (feronia.fiee.net: connection is authenticated) In-Reply-To: <20180202110802.z5bujyqssvvi3a3s@nan> X-Mailer: Apple Mail (2.1878.6) X-BeenThere: ntg-context@ntg.nl X-Mailman-Version: 2.1.16 Precedence: list List-Id: mailing list for ConTeXt users List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: ntg-context-bounces@ntg.nl Original-Sender: "ntg-context" Xref: news.gmane.org gmane.comp.tex.context:99998 Archived-At: --Apple-Mail=_9E1DFA5E-2574-42A4-B969-752DC55F70F4 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=windows-1252 Am 2018-02-02 um 12:08 schrieb Rudolf Bahr : > On Fri, Feb 02, 2018 at 08:40:08AM +0100, Henning Hraban Ramm wrote: >> Ahoi, >>=20 >> I=92m using a Lua function to calculate image sizes, cropping etc. >> This snippet: >>=20 >> local filename =3D figures.current().status.fullname >> local pic =3D img.scan{filename =3D filename} >>=20 >> ... works most of the time, but sometimes the "current" image is the = previous one, might be because I=92m using layers and postponing. >> So I=92d 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? >>=20 >=20 > Moin, Hraban, >=20 > 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=92m using are something Hans and others = helped me accomplish during the last meeting; AFAIK there=92s no real = documentation. Find attached the lua part. It=92s probably more complicated than = necessary I could work around my original problem: I=92m using my image with = \externalfigure in an invisible place first, so it=92s current, and = figures.current().status is the right one. :) Here=92s 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=3D1mm] % dummy to set image as current \def\myClipHeight{\ctxlua{ctxClipHeight("#3", \Resolution)}} \def\mystartY{\ctxlua{ctxStartY("#3", \Resolution)}} % image \setlayerframed[bglayer][% frame=3Doff, offset=3Doverlay, x=3D21mm, y=3D\mystartY, height=3D\myClipHeight, width=3D\textwidth, align=3Dflushleft, ]{% \clip[ width=3D\textwidth, height=3D\myClipHeight, hoffset=3D0mm, voffset=3D0mm, % i.e. clip bottom! TODO: options! ]{\externalfigure[#3][width=3D\textwidth]} } \textreference[pre#1]{\myImgHeight} % caption \setlayerframed[bglayer][% frame=3Doff, offset=3Doverlay, linecorrection=3Don, x=3D143mm,y=3D\dimexpr\mystartY + 3pt\relax, height=3D\myClipHeight, width=3D\outermarginwidth, align=3D{flushleft,low}, valign=3Dbottom, ]{% \vbox{ \textreference[#1]{\strut #2}\vfill #2 }} \stopfullpagemakeup } usage example: \startpostponing[82] \pagefig[alinari-stelzen]{Gebr=FCder 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 --Apple-Mail=_9E1DFA5E-2574-42A4-B969-752DC55F70F4 Content-Disposition: attachment; filename=grph-imagesize.lua Content-Type: application/octet-stream; name="grph-imagesize.lua" Content-Transfer-Encoding: 7bit TEXpt = 65536 -- sp per pt TEXptpi = 72.27 -- pt per inch function sp2mm(n) -- convert sp into mm return math.floor(n * 35.28 / TEXpt)/100 end function glue2num(glue) -- convert TeX glue (e.g. skips) into dimensions(? or numbers) return glue.width + (glue.stretch * glue.stretch_order) - (glue.shrink * glue.shrink_order) end function getImgHeight(filename, resolution) -- filename, target resolution [dpi] -- filename is not used ATM, just current image print("LUA getRegImgHeight(" .. filename .. ", " .. resolution .. ")") local targetwidth = tex.getdimen("textwidth") filename = figures.current().status.fullname local pic = img.scan{filename = filename} local picH = pic.ysize * TEXptpi * TEXpt / resolution -- picture height in sp local picW = pic.xsize * TEXptpi * TEXpt / resolution -- picture width in sp print("LUA picH: " .. picH .. " / ysize: " .. pic.ysize) local scaling = targetwidth / picW -- scaling factor picH = picH * scaling -- scaled image height return picH end function ctxImgHeight(filename, resolution) context(getImgHeight(filename, resolution) .. "sp") end function getClipHeight(picH) -- image should sit on a grid line and occupy n whole lines plus -- 2/3 of a line to "start" with middle length of type local lineH = tex.getdimen("lineheight") local picLY = math.floor(picH / lineH) -- image height in whole lines local clipY = lineH * picLY -- clipped image height local picTop = picH % lineH -- rest of image height local firstSkip = 2/3 * lineH -- first line if picTop >= firstSkip then clipY = clipY + firstSkip -- add first line else clipY = clipY - (1/2 * lineH) -- clip to first line end --context(clipY .. "sp") return clipY end function ctxClipHeight(filename, resolution) context(getClipHeight(getImgHeight(filename, resolution)) .. "sp") end function getStartY(picH) -- I need an absolute y coordinate for image placement local Ycorr = 2 * TEXpt -- y correction local topOffset = tex.getdimen("topspace") + tex.getdimen("headerheight") + tex.getdimen("headerdistance") + tex.getdimen("topdistance") local topskip = glue2num(tex.topskip) local textH = tex.getdimen("textheight") local lineH = tex.getdimen("lineheight") local skipH = (textH - picH) / 3 -- spread space 2:3 local skipL = math.floor(skipH / lineH) skipH = skipL * lineH + 1/3 * lineH -- within type area skipH = skipH + topOffset + topskip + Ycorr return skipH end function ctxStartY(filename, resolution) context(getStartY(getClipHeight(getImgHeight(filename, resolution))) .. "sp") end --Apple-Mail=_9E1DFA5E-2574-42A4-B969-752DC55F70F4 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: base64 Content-Disposition: inline X19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19f X19fX19fX19fX19fX19fX19fX19fX19fX18KSWYgeW91ciBxdWVzdGlvbiBpcyBvZiBpbnRlcmVz dCB0byBvdGhlcnMgYXMgd2VsbCwgcGxlYXNlIGFkZCBhbiBlbnRyeSB0byB0aGUgV2lraSEKCm1h aWxsaXN0IDogbnRnLWNvbnRleHRAbnRnLm5sIC8gaHR0cDovL3d3dy5udGcubmwvbWFpbG1hbi9s aXN0aW5mby9udGctY29udGV4dAp3ZWJwYWdlICA6IGh0dHA6Ly93d3cucHJhZ21hLWFkZS5ubCAv IGh0dHA6Ly9jb250ZXh0LmFhbmhldC5uZXQKYXJjaGl2ZSAgOiBodHRwczovL2JpdGJ1Y2tldC5v cmcvcGhnL2NvbnRleHQtbWlycm9yL2NvbW1pdHMvCndpa2kgICAgIDogaHR0cDovL2NvbnRleHRn YXJkZW4ubmV0Cl9fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19f X19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19f --Apple-Mail=_9E1DFA5E-2574-42A4-B969-752DC55F70F4--