font-log.lua /size: 3100 b    last modification: 2023-12-21 09:44
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-- The following functions are used for reporting about the fonts used. The message
23-- itself is not that useful in regular runs but since we now have several readers
24-- it may be handy to know what reader is used for which font.
25
26function loggers.onetimemessage(font,char,message,reporter)
27    local tfmdata = fonts.hashes.identifiers[font]
28    local shared = tfmdata.shared
29    local messages = shared.messages
30    if not messages then
31        messages = { }
32        shared.messages = messages
33    end
34    local category = messages[message]
35    if not category then
36        category = { }
37        messages[message] = category
38    end
39    if not category[char] then
40        if not reporter then
41            reporter = report_defining
42        end
43        reporter("char %U in font %a with id %s: %s",char,tfmdata.properties.fullname,font,message)
44        category[char] = true
45    end
46end
47
48function loggers.register(tfmdata,source,specification) -- save file name in spec here ! ! ! ! ! !
49    if tfmdata and specification and specification.specification then
50        local name = lower(specification.name)
51        if trace_defining and not usedfonts[name] then
52            report_defining("registering %a as %a, used %a",file.basename(specification.name),source,file.basename(specification.filename))
53        end
54        specification.source = source
55     -- loadedfonts[lower(specification.specification)] = specification
56        usedfonts[lower(specification.filename or specification.name)] = source
57    end
58end
59
60function loggers.format(name) -- should be avoided
61    return usedfonts[name] or "unknown"
62end
63
64-- maybe move this to font-ctx.lua
65
66statistics.register("loaded fonts", function()
67    if next(usedfonts) then
68        local t, n = { }, 0
69        local treatmentdata = fonts.treatments.data
70        for name, used in table.sortedhash(usedfonts) do
71            n = n + 1
72            local base = basename(name)
73            if complete then
74                t[n] = format("%s -> %s",used,base)
75            else
76                t[n] = base
77            end
78            local treatment = treatmentdata[base]
79            if treatment and treatment.comment then
80                 t[n] = format("%s (%s)",t[n],treatment.comment)
81            end
82        end
83        return n > 0 and format("%s files: %s",n,concat(t,", ")) or "none"
84    end
85end)
86