summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/context/base/mtx-context-precache.tex
blob: 9cbb46cf2c5e5cfbb3302cc12f760b52d622545c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
%D \module
%D   [       file=mtx-context-precache,
%D        version=2014.12.24,
%D          title=\CONTEXT\ Extra Trickry,
%D       subtitle=Precaching Fonts,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

% begin help
%
% usage: context --extra=precache [no options yet]
%
% example: context --extra=precache
%
% end help

\startluacode

local lower      = string.lower
local filesuffix = file.suffix
local findfile   = resolvers.find_file

local report     = logs.reporter("fonts","precache")

function fonts.names.precache()
    local handlers = fonts.handlers
    if not handlers then
        report("no handlers available")
        return
    end
    local otfloader = handlers.otf and handlers.otf.load
    local afmloader = handlers.afm and handlers.afm.load
    if not (otfloader or afmloader) then
        report("no otf or afm handler available")
        return
    end
    fonts.names.load()
    local data = fonts.names.data
    if not data then
        report("no font data available")
        return
    end
    local specifications = data.specifications
    if not specifications then
        report("no font specifications available")
        return
    end
    local n = 0
    for i=1,#specifications do
        local specification = specifications[i]
        local filename      = specification.filename
        local cleanfilename = specification.cleanfilename
        local foundfile     = findfile(filename)
        if foundfile and foundfile ~= "" then
            local suffix = lower(filesuffix(foundfile))
            if suffix == "otf" or suffix == "ttf" then
                if otfloader then
                    report("caching otf file: %s",foundfile)
                    otfloader(foundfile) -- todo: ttc/sub
                    n = n + 1
                end
            elseif suffix == "afm" then
                if afmloader then
                    report("caching afm file: %s",foundfile)
                    afmloader(foundfile)
                    n = n + 1
                end
            end
        end
    end
    report("%s files out of %s cached",n,#specifications)
end

\stopluacode

\starttext

\setuppapersize
  [A4,landscape]

\setuplayout
  [width=middle,
   height=middle,
   footer=0pt,
   header=1cm,
   headerdistance=0cm,
   backspace=5mm,
   topspace=5mm]

\setupbodyfont
  [dejavu,6pt,tt]

\startmode[*first]
    \startluacode
        fonts.names.precache()
    \stopluacode
\stopmode

\startluacode
    fonts.names.load()

    local specifications = fonts.names.data.specifications

    local sorted = { }
    local hashed = { }

    for i=1,#specifications do
        local filename = specifications[i].cleanfilename
        sorted[i] = filename
        hashed[filename] = i
    end

    table.sort(sorted)

    local context  = context
    local basename = file.basename

    local NC   = context.NC
    local NR   = context.NR
    local HL   = context.HL
    local bold = context.bold

    context.starttabulate { "||||||||||" }
    HL()
    NC() bold("format")
    NC() bold("cleanfilename")
    NC() bold("filename")
 -- NC() bold("familyname")
 -- NC() bold("fontname")
    NC() bold("fullname")
    NC() bold("rawname")
    NC() bold("style")
    NC() bold("variant")
    NC() bold("weight")
    NC() bold("width")
    NC() NR()
    HL()
    for i=1,#sorted do
        local specification = specifications[hashed[sorted[i]]]
        NC() context(specification.format)
        NC() context(specification.cleanfilename)
        NC() context(basename(specification.filename))
     -- NC() context(specification.familyname)
     -- NC() context(specification.fontname)
        NC() context(specification.fullname)
        NC() context(specification.rawname)
        NC() context(specification.style)
        NC() context(specification.variant)
        NC() context(specification.weight)
        NC() context(specification.width)
        NC() NR()
    end
    context.stoptabulate()
\stopluacode

\stoptext