font-log.lua /size: 3118 b    last modification: 2020-07-01 14:35
1if not modules then modules = { } end modules ['font-log'] = {
2    version   = 1.001,
3    comment   = "companion to font-ini.mkiv",
4    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
5    copyright = "PRAGMA ADE / ConTeXt Development Team",
6    license   = "see context related readme files"
7}
8
9local next, format, lower, concat = next, string.format, string.lower, table.concat
10
11local trace_defining  = false  trackers.register("fonts.defining", function(v) trace_defining = v end)
12local report_defining = logs.reporter("fonts","defining")
13
14local basename = file.basename
15
16local fonts       = fonts
17local loggers     = { }
18fonts.loggers     = loggers
19local usedfonts   = utilities.storage.allocate()
20----- loadedfonts = utilities.storage.allocate()
21
22--[[ldx--
23<p>The following functions are used for reporting about the fonts
24used. The message itself is not that useful in regular runs but since
25we now have several readers it may be handy to know what reader is
26used for which font.</p>
27--ldx]]--
28
29function loggers.onetimemessage(font,char,message,reporter)
30    local tfmdata = fonts.hashes.identifiers[font]
31    local shared = tfmdata.shared
32    local messages = shared.messages
33    if not messages then
34        messages = { }
35        shared.messages = messages
36    end
37    local category = messages[message]
38    if not category then
39        category = { }
40        messages[message] = category
41    end
42    if not category[char] then
43        if not reporter then
44            reporter = report_defining
45        end
46        reporter("char %U in font %a with id %s: %s",char,tfmdata.properties.fullname,font,message)
47        category[char] = true
48    end
49end
50
51function loggers.register(tfmdata,source,specification) -- save file name in spec here ! ! ! ! ! !
52    if tfmdata and specification and specification.specification then
53        local name = lower(specification.name)
54        if trace_defining and not usedfonts[name] then
55            report_defining("registering %a as %a, used %a",file.basename(specification.name),source,file.basename(specification.filename))
56        end
57        specification.source = source
58     -- loadedfonts[lower(specification.specification)] = specification
59        usedfonts[lower(specification.filename or specification.name)] = source
60    end
61end
62
63function loggers.format(name) -- should be avoided
64    return usedfonts[name] or "unknown"
65end
66
67-- maybe move this to font-ctx.lua
68
69statistics.register("loaded fonts", function()
70    if next(usedfonts) then
71        local t, n = { }, 0
72        local treatmentdata = fonts.treatments.data
73        for name, used in table.sortedhash(usedfonts) do
74            n = n + 1
75            local base = basename(name)
76            if complete then
77                t[n] = format("%s -> %s",used,base)
78            else
79                t[n] = base
80            end
81            local treatment = treatmentdata[base]
82            if treatment and treatment.comment then
83                 t[n] = format("%s (%s)",t[n],treatment.comment)
84            end
85        end
86        return n > 0 and format("%s files: %s",n,concat(t,", ")) or "none"
87    end
88end)
89