summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/fonts/misc/cjk-gs-integrate/zrlistttc.lua
blob: 59329aacb262d46ed5b76f9c71afd4b26556e8ce (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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
-- zrlistttc.lua
-- Copyright (c) 2019 Takayuki YATO
-- Modified by Hironobu YAMASHITA
-- This software is distributed under the MIT License.
prog_name = 'zrlistttc'
version = '0.4'
mod_date = '2019/08/31'
----------------------------------------
verbose = false
ttc_index = nil
content = { 6 }
langid = nil
ttc_file = nil
----------------------------------------
do
  local reader_meta = {
    __tostring = function(self)
      return "reader("..self.name..")"
    end;
    __index = {
      cdata = function(self, ofs, len)
        return make_cdata(self:read(ofs, len))
      end;
      read = function(self, ofs, len)
        self.file:seek("set", ofs)
        local data = self.file:read(len)
        sure(data:len() == len, 1)
        return data
      end;
      close = function(self)
        self.file:close()
      end;
    }
  }
  function make_reader(fname)
    local file = io.open(fname, "rb")
    sure(file, "cannot open for input", fname)
    return setmetatable({
      name = fname, file = file
    }, reader_meta)
  end
end
----------------------------------------
do
  local cdata_meta = {
    __tostring = function(self)
      return "cdata(pos="..self._pos..")"
    end;
    __index = {
      pos = function(self, p)
        if not p then return self._pos end
        self._pos = p
        return self
      end;
      _unum = function(self, b)
        local v, data = 0, self.data
        sure(#data >= self._pos + b, 11)
        for i = 1, b do
          self._pos = self._pos + 1
          v = v * 256 + data:byte(self._pos)
        end
        return v
      end;
      _setunum = function(self, b, v)
        local t, data = {}, self.data
        t[1] = data:sub(1, self._pos)
        self._pos = self._pos + b
        sure(#data >= self._pos, 12)
        t[b + 2] = data:sub(self._pos + 1)
        for i = 1, b do
          t[b + 2 - i] = string.char(v % 256)
          v = math.floor(v / 256)
        end
        self.data = table.concat(t, '')
        return self
      end;
      str = function(self, b)
        local data = self.data
        self._pos = self._pos + b
        sure(#data >= self._pos, 13)
        return data:sub(self._pos - b + 1, self._pos)
      end;
      setstr = function(self, s)
        local t, data = {}, self.data
        t[1], t[2] = data:sub(1, self._pos), s
        self._pos = self._pos + #s
        sure(#data >= self._pos, 14)
        t[3] = data:sub(self._pos + 1)
        self.data = table.concat(t, '')
        return self
      end;
      ushort = function(self)
        return self:_unum(2)
      end;
      ulong = function(self)
        return self:_unum(4)
      end;
      setulong = function(self, v)
        return self:_setunum(4, v)
      end;
      ulongs = function(self, num)
        local t = {}
        for i = 1, num do
          t[i] = self:_unum(4)
        end
        return t
      end;
    }
  }
  function make_cdata(data)
    return setmetatable({
      data = data, _pos = 0
    }, cdata_meta)
  end
end
----------------------------------------
do
  local floor, ceil = math.floor, math.ceil
  local function div(x, y)
    return floor(x / y), x % y
  end
  local function utf16betoutf8(src)
    local s, d = { tostring(src):byte(1, -1) }, {}
    for i = 1, #s - 1, 2 do
      local c = s[i] * 256 + s[i+1]
      if c < 0x80 then d[#d+1] = c
      elseif c < 0x800 then
        local x, y = div(c, 0x40)
        d[#d+1] = x + 0xC0; d[#d+1] = y + 0x80
      elseif c < 0x10000 then
        local x, y, z = div(c, 0x1000); y, z = div(y, 0x40)
        d[#d+1] = x + 0xE0; d[#d+1] = y + 0x80; d[#d+1] = z + 0x80
      else sure(nil)
      end
    end
    return string.char(unpack(d))
  end
  local file_type = {
    [0x74746366] = 'ttc'; [0x10000] = 'ttf'; [0x4F54544F] = 'otf';
    [0x74727565] = 'ttf'
  }
  function otf_offset(reader)
    local cd = reader:cdata(0, 12)
    local tag = cd:ulong()
    local ftype = file_type[tag]; info("type", ftype)
    if ftype == 'ttc' then
      local ver = cd:ulong(); info("version", ver)
      local num = cd:ulong(); info("#fonts", num)
      cd = reader:cdata(12, 4 * num)
      local res = cd:ulongs(num); info("offset", stt(res))
      return res
    elseif ftype == 'otf' or ftype == 'ttf' then
      return { 0 }
    else sure(nil, "unknown file tag", tag)
    end
  end
  local function otf_name_table(reader, fofs, ntbl)
    local cd_d = reader:cdata(fofs + 12, 16 * ntbl)
    for i = 1, ntbl do
      local t = stt({-- tag, csum, ofs, len
        cd_d:str(4), cd_d:ulong(), cd_d:ulong(), cd_d:ulong()
      })
      if t[1] == 'name' then
        info("name table index", i)
        return reader:cdata(t[3], ceil(t[4] / 4) * 4)
      end
    end
    sure(nil, "name table is missing")
  end
  local function otf_name_records(cdata)
    local nfmt, nnum, nofs = cdata:ushort(), cdata:ushort(), cdata:ushort()
    sure(nfmt == 0, "unsupported name table format", nfmt)
    local nr = stt({})
    for i = 1, nnum do
      nr[i] = stt({ -- pid, eid, langid, nameid, len, ofs
        cdata:ushort(), cdata:ushort(), cdata:ushort(),
        cdata:ushort(), cdata:ushort(), cdata:ushort() + nofs
      })
    end
    return nr
  end
  function otf_name(cdata, nr, nameid)
    local function seek(pid, eid, lid)
      for i = 1, #nr do
        local t = nr[i]
        local ok = (t[4] == nameid and t[1] == pid and t[2] == eid and
            t[3] == lid)
        if ok then return t end
      end
    end
    local rec
    if langid then
      rec = seek(unpack(langid))
    else
      rec = seek(3, 1, 0x409) or seek(3, 10, 0x409) or
        seek(1, 0, 0) or seek(0, 3, 0) or
        seek(0, 4, 0) or seek(0, 6, 0)
    end
    info("name record", rec or 'none')
    if not rec then return '' end
    local s = cdata:pos(rec[6]):str(rec[5])
    return (rec[1] == 3) and utf16betoutf8(s) or s
  end
  function otf_list(reader, fid, fofs)
    local cd_fh = reader:cdata(fofs, 12)
    local tag = cd_fh:ulong(); info("tag", tag)
    local ntbl = cd_fh:ushort(); info("#tables", ntbl)
    local cd_n = otf_name_table(reader, fofs, ntbl)
    local ext = { id = fid; type = file_type[tag] or '' }
    local nr, val = otf_name_records(cd_n), stt({})
    info("font", otf_name(cd_n, nr, 6))
    for i = 1, #content do
      local key = content[i]
      val[i] = (type(key) == 'string') and ext[key] or
          otf_name(cd_n, nr, key)
    end
    io.stdout:write(concat(val, ",").."\n")
  end
end
----------------------------------------
do
  unpack = unpack or table.unpack
  local stt_meta = {
    __tostring = function(self)
      return "{"..concat(self, ",").."}"
    end
  }
  function stt(tbl)
    return setmetatable(tbl, stt_meta)
  end
  function concat(tbl, ...)
    local t = {}
    for i = 1, #tbl do t[i] = tostring(tbl[i]) end
    return table.concat(t, ...)
  end
  function info(...)
    if not verbose then return end
    local t = { prog_name, ... }
    io.stderr:write(concat(t, ": ").."\n")
  end
  function abort(...)
    verbose = true; info(...)
    os.exit(-1)
  end
  function sure(val, a1, ...)
    if val then return val end
    if type(a1) == "number" then
      a1 = "error("..a1..")"
    end
    abort(a1, ...)
  end
end
----------------------------------------
do
  local function show_usage()
    io.stdout:write(([[
This is %s v%s <%s> by 'ZR'
Usage: %s[.lua] [-v] [-c <spec>] <ttc_file>
  -v    be verbose
  -i    show only one font with a specified index
  -c    content specification; comma-separated list of items,
        where an item is either 'id', 'type', or an name-ID
]]):format(prog_name, version, mod_date, prog_name))
    os.exit(0)
  end
  local function langid_spec(str)
    local p, e, l = str:match('^(%d+),(%d+),(%d+)$')
    sure(p, "invalid langid spec", str)
    return { tonumber(p), tonumber(e), tonumber(l) }
  end
  local function content_spec(str)
    local t, repo = {}, {
      copyright = 0; family = 1; subfamily = 2; fullname = 4;
      version = 5; psname = 6; url = 11; license = 13;
      tfamily = 16; tsubfamily = 17;
      id = -1; type = -1;
    }
    for k in str:gmatch('[^,]+') do
      local v = (k:match('^%d+$')) and tonumber(k) or repo[k]
      sure(v, "unknown content key", k)
      t[#t+1] = (v < 0) and k or v
    end
    return t
  end
  local function ttc_index_spec(str)
    local p = str:match('^(%d+)$')
    sure(p, "invalid ttc_index spec", str)
    return tonumber(p)
  end
  function read_option()
    if #arg == 0 then show_usage() end
    local idx = 1
    while idx <= #arg do
      local opt = arg[idx]
      if opt:sub(1, 1) ~= '-' then break end
      if opt == '-h' or opt == '--help' then
        show_usage()
      elseif opt == '-v' then
        verbose = true
      elseif opt == '-i' then
        idx = idx + 1; sure(arg[idx], "ttc_index spec is missing")
        ttc_index = ttc_index_spec(arg[idx])
      elseif opt == '-c' then
        idx = idx + 1; sure(arg[idx], "content spec is missing")
        content = stt(content_spec(arg[idx]))
      elseif opt == '-l' then
        idx = idx + 1; sure(arg[idx], "langid spec is missing")
        langid = stt(langid_spec(arg[idx]))
      else abort("invalid option", opt)
      end
      idx = idx + 1
    end
    sure(#arg == idx, "wrong number of arguments")
    ttc_file = arg[idx]
  end
  function main()
    read_option()
    local reader = make_reader(ttc_file)
    local tofs = otf_offset(reader)
    if ttc_index then
      if ttc_index < 0 or ttc_index > #tofs - 1 then
        abort("non-existing ttc_index", ttc_index)
      end
        otf_list(reader, ttc_index, tofs[ttc_index + 1])
    else
      for i = 1, #tofs do
        otf_list(reader, i - 1, tofs[i])
      end
    end
    reader:close()
  end
end
----------------------------------------
main()
-- EOF