summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/context/base/grph-inc.lua
diff options
context:
space:
mode:
Diffstat (limited to 'Master/texmf-dist/tex/context/base/grph-inc.lua')
-rw-r--r--Master/texmf-dist/tex/context/base/grph-inc.lua132
1 files changed, 101 insertions, 31 deletions
diff --git a/Master/texmf-dist/tex/context/base/grph-inc.lua b/Master/texmf-dist/tex/context/base/grph-inc.lua
index 8a9fd6b47d2..33dd0766d2b 100644
--- a/Master/texmf-dist/tex/context/base/grph-inc.lua
+++ b/Master/texmf-dist/tex/context/base/grph-inc.lua
@@ -47,6 +47,8 @@ local contains = table.contains
local concat, insert, remove = table.concat, table.insert, table.remove
local todimen = string.todimen
+local P = lpeg.P
+
local settings_to_array = utilities.parsers.settings_to_array
local settings_to_hash = utilities.parsers.settings_to_hash
local allocate = utilities.storage.allocate
@@ -133,12 +135,12 @@ figures.defaultdepth = 0
figures.nofprocessed = 0
figures.preferquality = true -- quality over location
-figures.existers = allocate() local existers = figures.existers
-figures.checkers = allocate() local checkers = figures.checkers
-figures.includers = allocate() local includers = figures.includers
-figures.converters = allocate() local converters = figures.converters
-figures.identifiers = allocate() local identifiers = figures.identifiers
-figures.programs = allocate() local programs = figures.programs
+local existers = allocate() figures.existers = existers
+local checkers = allocate() figures.checkers = checkers
+local includers = allocate() figures.includers = includers
+local converters = allocate() figures.converters = converters
+local identifiers = allocate() figures.identifiers = identifiers
+local programs = allocate() figures.programs = programs
figures.localpaths = allocate {
".", "..", "../.."
@@ -150,13 +152,13 @@ figures.cachepaths = allocate {
subpath = ".",
}
-figures.paths = allocate(table.copy(figures.localpaths))
+figures.paths = allocate(table.copy(figures.localpaths))
figures.order = allocate{
- "pdf", "mps", "jpg", "png", "jp2", "jbig", "svg", "eps", "tif", "gif", "mov", "buffer", "tex", "cld",
+ "pdf", "mps", "jpg", "png", "jp2", "jbig", "svg", "eps", "tif", "gif", "mov", "buffer", "tex", "cld", "auto",
}
-figures.formats = allocate{
+local formats = allocate {
["pdf"] = { list = { "pdf" } },
["mps"] = { patterns = { "mps", "%d+" } },
["jpg"] = { list = { "jpg", "jpeg" } },
@@ -171,13 +173,43 @@ figures.formats = allocate{
["buffer"] = { list = { "tmp", "buffer", "buf" } },
["tex"] = { list = { "tex" } },
["cld"] = { list = { "cld" } },
+ ["auto"] = { list = { "auto" } },
+}
+
+local magics = allocate {
+ { format = "png", pattern = P("\137PNG\013\010\026\010") }, -- 89 50 4E 47 0D 0A 1A 0A,
+ { format = "jpg", pattern = P("\255\216\255") }, -- FF D8 FF
+ { format = "jp2", pattern = P("\000\000\000\012\106\080\032\032\013\010"), }, -- 00 00 00 0C 6A 50 20 20 0D 0A },
+ { format = "gif", pattern = P("GIF") },
+ { format = "pdf", pattern = (1 - P("%PDF"))^0 * P("%PDF") },
}
-function figures.setlookups()
+figures.formats = formats -- frozen
+figures.magics = magics -- frozen
+
+function figures.guess(filename)
+ local f = io.open(filename,'rb')
+ if f then
+ local str = f:read(100)
+ f:close()
+ for i=1,#magics do
+ local pattern = magics[i]
+ if pattern.pattern:match(str) then
+ local format = pattern.format
+ if trace_figures then
+ report_inclusion("file %q has format %s",filename,format)
+ end
+ return format
+ end
+ end
+ end
+end
+
+function figures.setlookups() -- tobe redone .. just set locals
local fs, fp = allocate(), allocate()
figures.suffixes, figures.patterns = fs, fp
for _, format in next, figures.order do
- local data = figures.formats[format]
+ local data = formats[format]
local list = data.list
if list then
for i=1,#list do
@@ -204,10 +236,10 @@ function figures.registerresource(t)
end
local function register(tag,target,what)
- local data = figures.formats[target] -- resolver etc
+ local data = formats[target] -- resolver etc
if not data then
data = { }
- figures.formats[target] = data
+ formats[target] = data
end
local d = data[tag] -- list or pattern
if d and not contains(d,what) then
@@ -439,7 +471,16 @@ local function register(askedname,specification)
local oldname = specification.fullname
local newpath = file.dirname(oldname)
local oldbase = file.basename(oldname)
- local newbase = file.removesuffix(oldbase)
+ --
+ -- problem: we can have weird filenames, like a.b.c (no suffix) and a.b.c.gif
+ -- so we cannot safely remove a suffix (unless we do that for known suffixes)
+ --
+ -- local newbase = file.removesuffix(oldbase) -- assumes a known suffix
+ --
+ -- so we now have (also see *):
+ --
+ local newbase = oldbase
+ --
local fc = specification.cache or figures.cachepaths.path
if fc and fc ~= "" and fc ~= "." then
newpath = fc
@@ -463,7 +504,19 @@ local function register(askedname,specification)
if resolution and resolution ~= "" then -- the order might change
newbase = newbase .. "_" .. resolution
end
- local newbase = file.addsuffix(newbase,newformat)
+ --
+ -- see *, we had:
+ --
+ -- local newbase = file.addsuffix(newbase,newformat)
+ --
+ -- but now have (result of Aditya's web image testing):
+ --
+ -- as a side effect we can now have multiple fetches with different
+ -- original formats, not that it matters much (apart from older conversions
+ -- sticking around)
+ --
+ local newbase = newbase .. "." .. newformat
+ --
local newname = file.join(newpath,newbase)
dir.makedirs(newpath)
oldname = file.collapsepath(oldname)
@@ -578,14 +631,14 @@ local function locate(request) -- name, format, cache
end
end
if format then
- local foundname, quitscanning = figures.exists(askedname,format,resolve_too) -- not askedformat
+ local foundname, quitscanning, forcedformat = figures.exists(askedname,format,resolve_too) -- not askedformat
if foundname then
return register(askedname, {
askedname = askedname,
fullname = foundname, -- askedname,
- format = format,
+ format = forcedformat or format,
cache = askedcache,
---~ foundname = foundname,
+ -- foundname = foundname, -- no
conversion = askedconversion,
resolution = askedresolution,
})
@@ -597,12 +650,12 @@ local function locate(request) -- name, format, cache
end
if askedpath then
-- path and type given, todo: strip pieces of path
- local foundname = figures.exists(askedname,askedformat,resolve_too)
+ local foundname, quitscanning, forcedformat = figures.exists(askedname,askedformat,resolve_too)
if foundname then
return register(askedname, {
askedname = askedname,
fullname = foundname, -- askedname,
- format = askedformat,
+ format = forcedformat or askedformat,
cache = askedcache,
conversion = askedconversion,
resolution = askedresolution,
@@ -616,7 +669,8 @@ local function locate(request) -- name, format, cache
local check = path .. "/" .. askedname
-- we pass 'true' as it can be an url as well, as the type
-- is given we don't waste much time
- if figures.exists(check,askedformat,resolve_too) then
+ local foundname, quitscanning, forcedformat = figures.exists(check,askedformat,resolve_too)
+ if foundname then
return register(check, {
askedname = askedname,
fullname = check,
@@ -648,16 +702,16 @@ local function locate(request) -- name, format, cache
local figureorder = figures.order
for i=1,#figureorder do
local format = figureorder[i]
- local list = figures.formats[format].list or { format }
+ local list = formats[format].list or { format }
for j=1,#list do
local suffix = list[j]
local check = file.addsuffix(askedname,suffix)
- local foundname = figures.exists(check,format,resolve_too)
+ local foundname, quitscanning, forcedformat = figures.exists(check,format,resolve_too)
if foundname then
return register(askedname, {
askedname = askedname,
fullname = foundname, -- check,
- format = format,
+ format = forcedformat or format,
cache = askedcache,
conversion = askedconversion,
resolution = askedresolution,
@@ -674,7 +728,7 @@ local function locate(request) -- name, format, cache
local figureorder = figures.order
for j=1,#figureorder do
local format = figureorder[j]
- local list = figures.formats[format].list or { format }
+ local list = formats[format].list or { format }
for k=1,#list do
local suffix = list[k]
-- local name = file.replacesuffix(askedbase,suffix)
@@ -688,12 +742,12 @@ local function locate(request) -- name, format, cache
report_inclusion("warning: skipping path %s",path)
end
else
- local foundname = figures.exists(check,format,true)
+ local foundname, quitscanning, forcedformat = figures.exists(check,format,true)
if foundname then
return register(askedname, {
askedname = askedname,
fullname = foundname, -- check
- format = format,
+ format = forcedformat or format,
cache = askedcache,
conversion = askedconversion,
resolution = askedresolution,
@@ -713,16 +767,16 @@ local function locate(request) -- name, format, cache
local path = figurepaths[i]
for j=1,#figureorder do
local format = figureorder[j]
- local list = figures.formats[format].list or { format }
+ local list = formats[format].list or { format }
for k=1,#list do
local suffix = list[k]
local check = path .. "/" .. file.replacesuffix(askedbase,suffix)
- local foundname = figures.exists(check,format,resolve_too)
+ local foundname, quitscanning, forcedformat = figures.exists(check,format,resolve_too)
if foundname then
return register(askedname, {
askedname = askedname,
fullname = foudname, -- check,
- format = format,
+ format = forcedformat or format,
cache = askedcache,
conversion = askedconversion,
resolution = askedresolution,
@@ -739,7 +793,7 @@ local function locate(request) -- name, format, cache
local figureorder = figures.order
for j=1,#figureorder do
local format = figureorder[j]
- local list = figures.formats[format].list or { format }
+ local list = formats[format].list or { format }
for k=1,#list do
local suffix = list[k]
local check = resolvers.findfile(file.replacesuffix(askedname,suffix))
@@ -1064,6 +1118,22 @@ end
includers.buffers = includers.nongeneric
+-- -- -- auto -- -- --
+
+function existers.auto(askedname)
+ local name = gsub(askedname, ".auto$", "")
+ local format = figures.guess(name)
+ if format then
+ report_inclusion("format guess for %q: %s",name,format)
+ else
+ report_inclusion("format guess for %q is not possible",name)
+ end
+ return format and name, true, format
+end
+
+checkers.auto = checkers.generic
+includers.auto = includers.generic
+
-- -- -- cld -- -- --
existers.cld = existers.tex