diff options
Diffstat (limited to 'Master/texmf-dist/tex/context/base/grph-inc.lua')
-rw-r--r-- | Master/texmf-dist/tex/context/base/grph-inc.lua | 189 |
1 files changed, 139 insertions, 50 deletions
diff --git a/Master/texmf-dist/tex/context/base/grph-inc.lua b/Master/texmf-dist/tex/context/base/grph-inc.lua index 7dee63eaf5f..15aff892505 100644 --- a/Master/texmf-dist/tex/context/base/grph-inc.lua +++ b/Master/texmf-dist/tex/context/base/grph-inc.lua @@ -8,6 +8,8 @@ if not modules then modules = { } end modules ['grph-inc'] = { -- figures -> managers.figures +-- todo: empty filename or only suffix always false (not found) + -- lowercase types -- mps tex tmp svg -- partly qualified @@ -20,7 +22,7 @@ if not modules then modules = { } end modules ['grph-inc'] = { --[[ The ConTeXt figure inclusion mechanisms are among the oldest code -in ConTeXt and evolve dinto a complex whole. One reason is that we +in ConTeXt and evolved into a complex whole. One reason is that we deal with backend in an abstract way. What complicates matters is that we deal with internal graphics as well: TeX code, MetaPost code, etc. Later on figure databases were introduced, which resulted in @@ -120,6 +122,8 @@ figures.used = allocate() figures.found = allocate() figures.suffixes = allocate() figures.patterns = allocate() +figures.resources = allocate() + figures.boxnumber = figures.boxnumber or 0 figures.defaultsearch = true @@ -139,6 +143,7 @@ figures.programs = allocate() local programs = figures.programs figures.localpaths = allocate { ".", "..", "../.." } + figures.cachepaths = allocate { prefix = "", path = ".", @@ -148,7 +153,7 @@ figures.cachepaths = allocate { figures.paths = allocate(table.copy(figures.localpaths)) figures.order = allocate{ - "pdf", "mps", "jpg", "png", "jp2", "jbig", "svg", "eps", "gif", "mov", "buffer", "tex", "cld", + "pdf", "mps", "jpg", "png", "jp2", "jbig", "svg", "eps", "tif", "gif", "mov", "buffer", "tex", "cld", } figures.formats = allocate{ @@ -161,6 +166,7 @@ figures.formats = allocate{ ["svg"] = { list = { "svg", "svgz" } }, ["eps"] = { list = { "eps", "ai" } }, ["gif"] = { list = { "gif" } }, + ["tif"] = { list = { "tif", "tiff" } }, ["mov"] = { list = { "mov", "flv", "mp4" } }, -- "avi" is not supported ["buffer"] = { list = { "tmp", "buffer", "buf" } }, ["tex"] = { list = { "tex" } }, @@ -191,6 +197,12 @@ end figures.setlookups() +function figures.registerresource(t) + local n = #figures.resources + 1 + figures.resources[n] = t + return n +end + local function register(tag,target,what) local data = figures.formats[target] -- resolver etc if not data then @@ -301,7 +313,7 @@ local function new() -- we could use metatables status -> used -> request but it } end - -- use table.insert|remove +-- use table.insert|remove local lastfiguredata = nil -- will be topofstack or last so no { } (else problems with getfiguredimensions) local callstack = { } @@ -402,10 +414,12 @@ local function register(askedname,specification) newformat = defaultformat end if trace_conversion then - report_inclusion("checking conversion of '%s': old format '%s', new format '%s', conversion '%s', resolution '%s'", - askedname,format,newformat,conversion or "default",resolution or "default") + report_inclusion("checking conversion of '%s' (%s): old format '%s', new format '%s', conversion '%s', resolution '%s'", + askedname,specification.fullname,format,newformat,conversion or "default",resolution or "default") end - local converter = (newformat ~= format) and converters[format] + -- quick hack + -- local converter = (newformat ~= format) and converters[format] + local converter = (newformat ~= format or resolution) and converters[format] if converter then if converter[newformat] then converter = converter[newformat] @@ -422,8 +436,7 @@ local function register(askedname,specification) report_inclusion("no converter for '%s' -> '%s'",format,newformat) end if converter then - -- local oldname = specification.fullname - local oldname = specification.foundname + local oldname = specification.fullname local newpath = file.dirname(oldname) local oldbase = file.basename(oldname) local newbase = file.removesuffix(oldbase) @@ -435,7 +448,7 @@ local function register(askedname,specification) end if not file.is_writable(newpath) then if trace_conversion then - report_inclusion("[ath '%s'is not writable, forcing conversion path '.' ",newpath) + report_inclusion("path '%s'is not writable, forcing conversion path '.' ",newpath) end newpath = "." end @@ -459,23 +472,34 @@ local function register(askedname,specification) local newtime = lfs.attributes(newname,'modification') or 0 if newtime == 0 or oldtime > newtime then if trace_conversion then - report_inclusion("converting '%s' from '%s' to '%s'",askedname,format,newformat) + report_inclusion("converting '%s' (%s) from '%s' to '%s'",askedname,oldname,format,newformat) end converter(oldname,newname,resolution or "") else if trace_conversion then - report_inclusion("no need to convert '%s' from '%s' to '%s'",askedname,format,newformat) + report_inclusion("no need to convert '%s' (%s) from '%s' to '%s'",askedname,oldname,format,newformat) end end - if io.exists(newname) then + if io.exists(newname) and io.size(newname) > 0 then specification.foundname = oldname specification.fullname = newname specification.prefix = prefix specification.subpath = subpath specification.converted = true format = newformat + if not figures.suffixes[format] then + -- maybe the new format is lowres.png (saves entry in suffixes) + -- so let's do thsi extra check + local suffix = file.suffix(newformat) + if figures.suffixes[suffix] then + if trace_figures then + report_inclusion("using suffix '%s' as format for '%s'",suffix,format) + end + format = suffix + end + end elseif io.exists(oldname) then - specification.fullname = newname + specification.fullname = oldname -- was newname specification.converted = false end end @@ -489,7 +513,7 @@ local function register(askedname,specification) else specification.found = true if trace_figures then - if validtypes[format] then + if validtypes[format] then -- format? report_inclusion("format natively supported by backend: %s",format) else report_inclusion("format supported by output file format: %s",format) @@ -508,7 +532,9 @@ end local resolve_too = true -- urls local function locate(request) -- name, format, cache - local askedname = resolvers.cleanpath(request.name) + -- not resolvers.cleanpath(request.name) as it fails on a!b.pdf and b~c.pdf + -- todo: more restricted cleanpath + local askedname = request.name local foundname = figures.found[askedname .. "->" .. (request.conversion or "default") .. "->" .. (request.resolution or "default")] if foundname then return foundname @@ -556,10 +582,10 @@ local function locate(request) -- name, format, cache if foundname then return register(askedname, { askedname = askedname, - fullname = askedname, + fullname = foundname, -- askedname, format = format, cache = askedcache, - foundname = foundname, +--~ foundname = foundname, conversion = askedconversion, resolution = askedresolution, }) @@ -571,10 +597,11 @@ local function locate(request) -- name, format, cache end if askedpath then -- path and type given, todo: strip pieces of path - if figures.exists(askedname,askedformat,resolve_too) then + local foundname = figures.exists(askedname,askedformat,resolve_too) + if foundname then return register(askedname, { askedname = askedname, - fullname = askedname, + fullname = foundname, -- askedname, format = askedformat, cache = askedcache, conversion = askedconversion, @@ -625,10 +652,11 @@ local function locate(request) -- name, format, cache for j=1,#list do local suffix = list[j] local check = file.addsuffix(askedname,suffix) - if figures.exists(check,format,resolve_too) then + local foundname = figures.exists(check,format,resolve_too) + if foundname then return register(askedname, { askedname = askedname, - fullname = check, + fullname = foundname, -- check, format = format, cache = askedcache, conversion = askedconversion, @@ -659,15 +687,18 @@ local function locate(request) -- name, format, cache if trace_figures then report_inclusion("warning: skipping path %s",path) end - elseif figures.exists(check,format,true) then - return register(askedname, { - askedname = askedname, - fullname = check, - format = format, - cache = askedcache, - conversion = askedconversion, - resolution = askedresolution, - }) + else + local foundname = figures.exists(check,format,true) + if foundname then + return register(askedname, { + askedname = askedname, + fullname = foundname, -- check + format = format, + cache = askedcache, + conversion = askedconversion, + resolution = askedresolution, + }) + end end end end @@ -686,10 +717,11 @@ local function locate(request) -- name, format, cache for k=1,#list do local suffix = list[k] local check = path .. "/" .. file.replacesuffix(askedbase,suffix) - if figures.exists(check,format,resolve_too) then + local foundname = figures.exists(check,format,resolve_too) + if foundname then return register(askedname, { askedname = askedname, - fullname = check, + fullname = foudname, -- check, format = format, cache = askedcache, conversion = askedconversion, @@ -725,7 +757,10 @@ local function locate(request) -- name, format, cache end end end - return register(askedname) + return register(askedname, { -- these two are needed for hashing 'found' + conversion = askedconversion, + resolution = askedresolution, + }) end -- -- -- plugins -- -- -- @@ -829,7 +864,10 @@ end function checkers.generic(data) local dr, du, ds = data.request, data.used, data.status - local name, page, size, color = du.fullname or "unknown generic", du.page or dr.page, dr.size or "crop", dr.color or "natural" + local name = du.fullname or "unknown generic" + local page = du.page or dr.page + local size = dr.size or "crop" + local color = dr.color or "natural" local mask = dr.mask or "none" local conversion = dr.conversion local resolution = dr.resolution @@ -839,14 +877,14 @@ function checkers.generic(data) if not resolution or resolution == "" then resolution = "unknown" end - local hash = name .. "->" .. page .. "->" .. size .. "->" .. color .. "->" .. conversion .. "->" .. resolution - .. "->" .. mask + local hash = name .. "->" .. page .. "->" .. size .. "->" .. color .. "->" .. conversion .. "->" .. resolution .. "->" .. mask local figure = figures.loaded[hash] if figure == nil then figure = img.new { - filename = name, - page = page, - pagebox = dr.size, + filename = name, + page = page, + pagebox = dr.size, + -- visiblefilename = "", -- this prohibits the full filename ending up in the file } codeinjections.setfigurecolorspace(data,figure) codeinjections.setfiguremask(data,figure) @@ -885,6 +923,11 @@ function includers.generic(data) dr.height = du.height local hash = figures.hash(data) local figure = figures.used[hash] +--~ figures.registerresource { +--~ filename = du.fullname, +--~ width = dr.width, +--~ height = dr.height, +--~ } if figure == nil then figure = ds.private if figure then @@ -1038,12 +1081,16 @@ local function makeoptions(options) return (to == "table" and concat(options," ")) or (to == "string" and options) or "" end -local function runprogram(...) - local command = format(...) - if trace_conversion or trace_programs then - report_inclusion("running %s",command) +local function runprogram(template,binary,...) + local command = format(template,binary,...) + if os.which(binary) then + if trace_conversion or trace_programs then + report_inclusion("running: %s",command) + end + os.spawn(command) + else + report_inclusion("program '%s' is not installed, not running: %s",binary,command) end - os.spawn(command) end -- -- -- eps -- -- -- @@ -1115,12 +1162,12 @@ svgconverter.default = svgconverter.pdf -- -- -- gif -- -- -- -local gifconverter = { } +local gifconverter = { } converters.gif = gifconverter programs.convert = { - command = "convert" -- imagemagick - -- command = "gm convert" -- graphicmagick + -- command = "convert" -- imagemagick + command = "gm convert" -- graphicmagick } function gifconverter.pdf(oldname,newname) @@ -1133,6 +1180,31 @@ end gifconverter.default = gifconverter.pdf +-- -- -- tif -- -- -- + +-- http://sourceforge.net/projects/gnuwin32/files/tiff/3.8.2-1/tiff-3.8.2-1-bin.zip/download + +local tifconverter = { } +converters.tif = tifconverter + +programs.convert = { +--~ command = "convert" -- imagemagick + command = "gm convert" -- graphicmagick +--~ command = "tiff2pdf" +} + +function tifconverter.pdf(oldname,newname) + local convert = programs.convert + runprogram ( + "%s %s %s %s", + convert.command, makeoptions(convert.options), oldname, newname +--~ "%s %s -z -o %s %s", +--~ convert.command, makeoptions(convert.options), newname, oldname + ) +end + +tifconverter.default = tifconverter.pdf + -- todo: lowres -- -- -- bases -- -- -- @@ -1270,7 +1342,7 @@ function figures.applyratio(width,height,w,h) -- width and height are strings an if not height or height == "" then return figures.defaultwidth, figures.defaultheight else - height = string.todimen(height) + height = todimen(height) if w and h then return height * w/h, height else @@ -1278,7 +1350,7 @@ function figures.applyratio(width,height,w,h) -- width and height are strings an end end else - width = string.todimen(width) + width = todimen(width) if not height or height == "" then if w and h then return width, width * h/w @@ -1286,7 +1358,24 @@ function figures.applyratio(width,height,w,h) -- width and height are strings an return width, figures.defaultheight end else - return width, string.todimen(height) + return width, todimen(height) end end end + +-- example of a simple plugin: +-- +-- figures.converters.png = { +-- png = function(oldname,newname,resolution) +-- local command = string.format('gm convert -depth 1 "%s" "%s"',oldname,newname) +-- logs.report(string.format("running command %s",command)) +-- os.execute(command) +-- end, +-- } + + +-- local fig = figures.push { name = pdffile } +-- figures.identify() +-- figures.check() +-- local nofpages = fig.used.pages +-- figures.pop() |