Number of pages by Lua of a figure to be inserted?
Hello, I need to identify number of pages by Lua of an image which is to be inserted. The image can be of all various types supported by Ctx - e.g. .png, .pdf. I've been using the following code so far (based on a mailing thread in the past): ---- function nOfPages(fn) local fig = figures.push{name = fn} local info = figures.identify() figures.check() local n = fig.used.pages figures.pop() return n end ---- But the code above break Ctx in the case that the image doesn't exist. I'd need to modify the solution somehow so to return 'nil' or 'false' (or a error message) in the case the file was not found. How to do it? TIA. Best regards, Lukas (NB: Still "ConTeXt ver: 2012.11.26 13:31 MKIV fmt: 2012.11.27 int: english/english".) -- Ing. Lukáš Procházka [mailto:LPr@pontex.cz] Pontex s. r. o. [mailto:pontex@pontex.cz] [http://www.pontex.cz] Bezová 1658 147 14 Praha 4 Tel: +420 244 062 238 Fax: +420 244 461 038
On 4/10/2013 4:22 PM, Procházka Lukáš Ing. - Pontex s. r. o. wrote:
Hello,
I need to identify number of pages by Lua of an image which is to be inserted.
The image can be of all various types supported by Ctx - e.g. .png, .pdf.
I've been using the following code so far (based on a mailing thread in the past):
---- function nOfPages(fn) local fig = figures.push{name = fn} local info = figures.identify()
figures.check()
local n = fig.used.pages
inspect(fig)
figures.pop()
return n end ----
But the code above break Ctx in the case that the image doesn't exist.
I'd need to modify the solution somehow so to return 'nil' or 'false' (or a error message) in the case the file was not found.
How to do it?
there's a status table in fig Hans ----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | voip: 087 875 68 74 | www.pragma-ade.com | www.pragma-pod.nl -----------------------------------------------------------------
Hello, thanks for the hint. Could you please check my code? The function is able to detect number of pages (tested with a .pdf, which works; I believe it will work with other figure formats, too). When the file is not found, it doesn't break Ctx, which is good. The problem is that once a file is not found, later calls to the function fail (means they return 'false'), too. It seems like a figure being "hanging" somewhere or what; but I'm not able to find the bad place iin the code. Note that the directory structure is: t2.mkiv - file to be tested T/Kn.pdf - file to be inserted (2 pages) ---- t2.mkiv \startluacode nOfPages = function(fn) local n do local fig = figures.push{name = fn} local info = figures.identify() --inspect(info) if info.status.status ~= 0 then figures.check() -- This may check all figures; I'm interested in the last figure only - any call improvement? n = fig.used.pages end figures.pop() -- I thought no figure should stay "hanging" due to this line end if not n then return false end return n end \stopluacode \starttext AAA \startluacode context(nOfPages("Kn.pdf") or "???") -- [1] -- When you comment the line above, later Ctx detects correctly 2 pages of 'Kn.pdf'. \stopluacode \setupexternalfigures[directory=T] % Find the 'Kn.pdf' in 'T/Kn.pdf' \startluacode context(nOfPages("Kn.pdf") or "???") -- Now, Ctx should find 'T/Kn.pdf' anyway and thus detect 2 pages of 'Kn.pdf'. -- This doesn't occure when [1] is on \stopluacode \stoptext ---- Any idea? TIA. Best regards, Lukas -- Ing. Lukáš Procházka [mailto:LPr@pontex.cz] Pontex s. r. o. [mailto:pontex@pontex.cz] [http://www.pontex.cz] Bezová 1658 147 14 Praha 4 Tel: +420 244 062 238 Fax: +420 244 461 038
participants (2)
-
Hans Hagen
-
Procházka Lukáš Ing. - Pontex s. r. o.