We hardly gain anything when we cache the final (pre scaled)
table. But it can be handy for debugging, so we no
longer carry this code along. Also, we now have quite some reference
to other tables so we would end up with lots of catches.

--ldx]]--
--[[ldx--

We can prefix a font specification by name: or
file:. The first case will result in a lookup in the
synonym table.

[ name: | file: ] identifier [ separator [ specification ] ]

The following function split the font specification into components
and prepares a table that will move along as we proceed.

The main read function either uses a forced reader (as determined by
a lookup) or tries to resolve the name using the list of readers.

We need to cache when possible. We do cache raw tfm data (from , or ). After that we can cache based
on specificstion (name) and size, that is, only needs a number
for an already loaded fonts. However, it may make sense to cache fonts
before they're scaled as well (store 's with applied methods
and features). However, there may be a relation between the size and
features (esp in virtual fonts) so let's not do that now.

Watch out, here we do load a font, but we don't prepare the
specification yet.

--ldx]]--
-- very experimental:
function definers.applypostprocessors(tfmdata)
local postprocessors = tfmdata.postprocessors
if postprocessors then
local properties = tfmdata.properties
for i=1,#postprocessors do
local extrahash = postprocessors[i](tfmdata) -- after scaling etc
if type(extrahash) == "string" and extrahash ~= "" then
-- e.g. a reencoding needs this
extrahash = gsub(lower(extrahash),"[^a-z]","-")
properties.fullname = formatters["%s-%s"](properties.fullname,extrahash)
end
end
end
return tfmdata
end
-- function definers.applypostprocessors(tfmdata)
-- return tfmdata
-- end
local function checkembedding(tfmdata)
local properties = tfmdata.properties
local embedding
if directive_embedall then
embedding = "full"
elseif properties and properties.filename and constructors.dontembed[properties.filename] then
embedding = "no"
else
embedding = "subset"
end
if properties then
properties.embedding = embedding
else
tfmdata.properties = { embedding = embedding }
end
tfmdata.embedding = embedding
end
local function checkfeatures(tfmdata)
local resources = tfmdata.resources
local shared = tfmdata.shared
if resources and shared then
local features = resources.features
local usedfeatures = shared.features
if features and usedfeatures then
local usedlanguage = usedfeatures.language or "dflt"
local usedscript = usedfeatures.script or "dflt"
local function check(what)
if what then
local foundlanguages = { }
for feature, scripts in next, what do
if usedscript == "auto" or scripts["*"] then
-- ok
elseif not scripts[usedscript] then
-- report_defining("font %!font:name!, feature %a, no script %a",
-- tfmdata,feature,usedscript)
else
for script, languages in next, scripts do
if languages["*"] then
-- ok
elseif context and not languages[usedlanguage] then
report_defining("font %!font:name!, feature %a, script %a, no language %a",
tfmdata,feature,script,usedlanguage)
end
end
end
for script, languages in next, scripts do
for language in next, languages do
foundlanguages[language] = true
end
end
end
if false then
foundlanguages["*"] = nil
foundlanguages = sortedkeys(foundlanguages)
for feature, scripts in sortedhash(what) do
for script, languages in next, scripts do
if not languages["*"] then
for i=1,#foundlanguages do
local language = foundlanguages[i]
if context and not languages[language] then
report_defining("font %!font:name!, feature %a, script %a, no language %a",
tfmdata,feature,script,language)
end
end
end
end
end
end
end
end
check(features.gsub)
check(features.gpos)
end
end
end
function definers.loadfont(specification)
local hash = constructors.hashinstance(specification)
-- todo: also hash by instance / factors
local tfmdata = loadedfonts[hash] -- hashes by size !
if not tfmdata then
-- normally context will not end up here often (if so there is an issue somewhere)
local forced = specification.forced or ""
if forced ~= "" then
local reader = readers[lower(forced)] -- normally forced is already lowered
tfmdata = reader and reader(specification)
if not tfmdata then
report_defining("forced type %a of %a not found",forced,specification.name)
end
else
local sequence = readers.sequence -- can be overloaded so only a shortcut here
for s=1,#sequence do
local reader = sequence[s]
if readers[reader] then -- we skip not loaded readers
if trace_defining then
report_defining("trying (reader sequence driven) type %a for %a with file %a",reader,specification.name,specification.filename)
end
tfmdata = readers[reader](specification)
if tfmdata then
break
else
specification.filename = nil
end
end
end
end
if tfmdata then
tfmdata = definers.applypostprocessors(tfmdata)
checkembedding(tfmdata) -- todo: general postprocessor
loadedfonts[hash] = tfmdata
designsizes[specification.hash] = tfmdata.parameters.designsize
checkfeatures(tfmdata)
end
end
if not tfmdata then
report_defining("font with asked name %a is not found using lookup %a",specification.name,specification.lookup)
end
return tfmdata
end
function constructors.readanddefine(name,size) -- no id -- maybe a dummy first
local specification = definers.analyze(name,size)
local method = specification.method
if method and variants[method] then
specification = variants[method](specification)
end
specification = definers.resolve(specification)
local hash = constructors.hashinstance(specification)
local id = definers.registered(hash)
if not id then
local tfmdata = definers.loadfont(specification)
if tfmdata then
tfmdata.properties.hash = hash
id = font.define(tfmdata)
definers.register(tfmdata,id)
else
id = 0 -- signal
end
end
return fontdata[id], id
end
--[[ldx--

So far the specifiers. Now comes the real definer. Here we cache
based on id's. Here we also intercept the virtual font handler. Since
it evolved stepwise I may rewrite this bit (combine code).

In the previously defined reader (the one resulting in a
table) we cached the (scaled) instances. Here we cache them again, but
this time based on id. We could combine this in one cache but this does
not gain much. By the way, passing id's back to in the callback was
introduced later in the development.
--ldx]]--
function definers.current() -- or maybe current
return lastdefined
end
function definers.registered(hash)
local id = internalized[hash]
return id, id and fontdata[id]
end
function definers.register(tfmdata,id)
if tfmdata and id then
local hash = tfmdata.properties.hash
if not hash then
report_defining("registering font, id %a, name %a, invalid hash",id,tfmdata.properties.filename or "?")
elseif not internalized[hash] then
internalized[hash] = id
if trace_defining then
report_defining("registering font, id %s, hash %a",id,hash)
end
fontdata[id] = tfmdata
end
end
end
function definers.read(specification,size,id) -- id can be optional, name can already be table
statistics.starttiming(fonts)
if type(specification) == "string" then
specification = definers.analyze(specification,size)
end
local method = specification.method
if method and variants[method] then
specification = variants[method](specification)
end
specification = definers.resolve(specification)
local hash = constructors.hashinstance(specification)
local tfmdata = definers.registered(hash) -- id
if tfmdata then
if trace_defining then
report_defining("already hashed: %s",hash)
end
else
tfmdata = definers.loadfont(specification) -- can be overloaded
if tfmdata then
if trace_defining then
report_defining("loaded and hashed: %s",hash)
end
tfmdata.properties.hash = hash
if id then
definers.register(tfmdata,id)
end
else
if trace_defining then
report_defining("not loaded and hashed: %s",hash)
end
end
end
lastdefined = tfmdata or id -- todo ! ! ! ! !
if not tfmdata then -- or id?
report_defining( "unknown font %a, loading aborted",specification.name)
elseif trace_defining and type(tfmdata) == "table" then
local properties = tfmdata.properties or { }
local parameters = tfmdata.parameters or { }
report_defining("using %a font with id %a, name %a, size %a, bytes %a, encoding %a, fullname %a, filename %a",
properties.format or "unknown", id or "-", properties.name, parameters.size, properties.encodingbytes,
properties.encodingname, properties.fullname, basename(properties.filename))
end
statistics.stoptiming(fonts)
return tfmdata
end
function font.getfont(id)
return fontdata[id] -- otherwise issues
end
--[[ldx--