OSDN Git Service

b1be80610df11e637309616e1ac8a365c1e8d3b6
[luatex-ja/luatexja.git] / src / ltj-rmlgbm.lua
1 --
2 -- luatexja/ltj-rmlgbm.lua
3 --
4 luatexja.load_module('base');      local ltjb = luatexja.base
5
6 local cidfont_data = {}
7 local cache_chars = {}
8 local cache_ver = '3'
9
10 local cid_reg, cid_order, cid_supp, cid_name
11 local cid_replace = {
12    ["Adobe-Japan1"] = {"UniJIS2004-UTF32", 23057, 6,
13                        function (i)
14                           if (231<=i and i<=632) or (8718<=i and i<=8719)
15                              or (12063<=i and i<=12087) then
16                              return 327680 -- 655360/2
17                           elseif 9758<=i and i<=9778 then
18                              return 218453 -- 655360/3
19                           elseif 9738<=i and i<=9757 then
20                              return 163840 -- 655360/4
21                           end
22                        end},
23                        -- 基本的には JIS X 0213:2004 に沿ったマッピング
24    ["Adobe-Korea1"] = {"UniKS-UTF32",  18351, 2,
25                        function (i)
26                           if 8094<=i and i<=8100 then
27                              return 327680 -- 655360/2
28                           end
29                        end},
30    ["Adobe-GB1"]    = {"UniGB-UTF32",  30283, 5,
31                        function (i)
32                           if (814<=i and i<=939) or (i==7716)
33                              or (22355<=i and i<=22357) then
34                              return 327680 -- 655360/2
35                           end
36                        end},
37    ["Adobe-CNS1"]   = {"UniCNS-UTF32", 19155, 6,
38                        function (i)
39                           if (13648<=i and i<=13742) or (i==17603) then
40                              return 327680 -- 655360/2
41                           end
42                        end},
43 }
44
45 -- reading CID maps
46 local make_cid_font
47 do
48    local line, fh -- line, file handler
49    local tt,cidm -- characters, cid->(Unicode)
50
51    local function load_cid_char(cid_dec, mke)
52       local cid, ucs, ucsa
53       line = fh:read("*l")
54       while line do
55          if string.find(line, "end...?char") then
56             line = fh:read("*l"); return
57          else -- WMA l is in the form "<%x+>%s%d+"
58             ucs, cid = string.match(line, "<(%x+)>%s+<?(%x+)>?")
59             cid = cid_dec(cid); ucs = tonumber(ucs, 16);
60             if not tt[ucs]  then
61                tt[ucs] = mke(cid); cidm[cid]=ucs
62             end
63          end
64          line = fh:read("*l")
65       end
66    end
67
68    local function load_cid_range(inc, cid_dec, mke)
69       local bucs, eucs, cid
70       line = fh:read("*l")
71       while line do
72         if string.find(line, "end...?range") then
73             line = fh:read("*l"); return
74          else -- WMA l is in the form "<%x+>%s+<%x+>"
75             bucs, eucs, cid = string.match(line, "<(%x+)>%s+<(%x+)>%s+<?(%x+)>?")
76             cid = cid_dec(cid);
77             bucs = tonumber(bucs, 16); eucs = tonumber(eucs, 16)
78             for ucs = bucs, eucs do
79                if not tt[ucs]  then
80                   tt[ucs] = mke(cid); cidm[cid]=ucs
81                end
82                cid = inc(cid)
83             end
84          end
85          line = fh:read("*l")
86       end
87    end
88
89    local function open_cmap_file(name, inc, cid_dec, mke)
90       fh = io.open(kpse.find_file(name, 'cmap files'), "r")
91       line = fh:read("*l")
92       while line do
93          if string.find(line, "%x+%s+begin...?char") then
94             load_cid_char(cid_dec, mke)
95          elseif string.find(line, "%x+%s+begin...?range") then
96             load_cid_range(inc, cid_dec, mke)
97          else
98             line = fh:read("*l")
99          end
100       end
101       fh:close();
102    end
103
104    local function increment(a) return a+1 end
105    local function entry(a)
106       return {index = a}
107    end
108    make_cid_font = function ()
109       local kx = cid_replace[cid_name]
110       if not kx then return end
111       local k = {
112          cidinfo = { ordering=cid_order, registry=cid_reg, supplement=kx[3] },
113          encodingbytes = 2, extend=1000, format = 'opentype',
114          direction = 0, characters = {}, parameters = {
115             ascender = 655360*0.88,
116             descender = 655360*0.12,
117          },
118          embedding = "no", cache = "yes", factor = 0, hfactor = 0, vfactor = 0,
119          tounicode = 1,
120       }
121       cidfont_data[cid_name] = k
122
123       -- CID => Unicode 符号空間
124       local tth, cidmo = {}, {}
125       tt, cidm = tth, cidmo
126       for i = 0,kx[2] do cidm[i] = -1 end
127       open_cmap_file(kx[1] .. "-H", increment, tonumber, entry)
128       k.characters = tth
129
130       -- Unicode にマップされなかった文字の処理
131       -- これらは TrueType フォントを使って表示するときはおかしくなる
132       local ttu, pricode = {}, 0xF0000
133       for i,v in ipairs(cidmo) do
134          if v==-1 then
135             tth[pricode], cidmo[i], pricode
136                = { index = i }, pricode, pricode+1;
137          end
138          ttu[cid_order .. '.' .. i] = cidmo[i]
139       end
140
141       -- 縦書用字形
142       tt, cidm = {}, {}
143       for i = 0,kx[2] do cidm[i] = -1 end
144       open_cmap_file(kx[1] .. "-V", increment, tonumber, entry)
145       local ttv = {}
146       for i,v in pairs(tt) do ttv[i] =  cidmo[v.index] end
147
148       -- shared
149       k.shared = {
150          otfdata = {
151             cidinfo= k.cidinfo, verbose = false,
152             shared = { featuredata = {}, },
153             luatex = { features = {},
154                        defaultwidth=1000,
155                      },
156          },
157          dynamics = {}, features = {}, processes = {},
158          ltj_vert_table = ttv
159       }
160       k.resources = { unicodes = ttu, }
161       k.descriptions = {}
162       cache_chars[cid_name]  = { [655360] = k.characters }
163
164       -- tounicode エントリ
165       local cidp = {nil, nil}; tt, ttu, cidm = {}, {}, {}
166       open_cmap_file(cid_name .. "-UCS2",
167                      function(a)
168                         a[2] = a[2] +1 ; return a
169                      end,
170                      function(a)
171                         cidp[1] = string.upper(string.sub(a,1,string.len(a)-4))
172                         cidp[2] = tonumber(string.sub(a,-4),16)
173                         return cidp
174                      end,
175                      function(a) return a[1] ..string.format('%04X',a[2])  end)
176       -- tt は cid -> tounicode になっているので cidm -> tounicode に変換
177       local kxf = kx[4]
178       for i,v in ipairs(cidmo) do
179          k.characters[v].width = kxf(i)
180          if v>=0xF0000 then
181             k.characters[v].tounicode = tt[i]
182          end
183       end
184
185       -- Save
186       k.characters[46].width = math.floor(655360/14);
187       ltjb.save_cache( "ltj-cid-auto-" .. string.lower(cid_name),
188                        {
189                           version = cache_ver,
190                           k,
191                        })
192    end
193 end
194
195 --
196 local function cid_cache_outdated(t) return t.version~=cache_ver end
197 local function read_cid_font()
198    local dat = ltjb.load_cache("ltj-cid-auto-" .. string.lower(cid_name),
199                                cid_cache_outdated )
200    if dat then
201       cidfont_data[cid_name] = dat[1]
202       cache_chars[cid_name]  = { [655360] = cidfont_data[cid_name].characters }
203    else
204       -- Now we must create the virtual metrics from CMap.
205       make_cid_font()
206    end
207    if cidfont_data[cid_name] then
208       for i,v in pairs(cidfont_data[cid_name].characters) do
209          if not v.width then v.width = 655360 end
210          v.height, v.depth = 576716.8, 78643.2 -- optimized for jfm-ujis.lua
211       end
212    end
213 end
214
215 -- High-level
216
217 local definers = fonts.definers
218 local function mk_rml(name, size, id)
219    local specification = definers.analyze(name,size)
220    --specification = definers.resolve(specification) (not needed)
221    specification.detail = specification.detail or ''
222
223    local fontdata = {}
224    local cachedata = {}
225    local s = cidfont_data[cid_name]
226    luatexja.rmlgbm.vert_addfunc(id, s.shared.ltj_vert_table)
227    for k, v in pairs(s) do
228       fontdata[k] = v
229       cachedata[k] = v
230    end
231    fontdata.characters = nil
232    cachedata.characters = nil
233    fontdata.shared = nil
234    cachedata.shared = nil
235    if s.shared then
236       cachedata.shared = {}
237       local shared = cachedata.shared
238       for k, v in pairs(s.shared) do
239          shared[k] = v
240       end
241    end
242
243    -- characters & scaling
244    if size < 0 then size = -size * 655.36 end
245    local scale = size / 655360
246
247    do
248       local def_height =  0.88 * size
249       -- character's default height (optimized for jfm-ujis.lua)
250       local def_depth =  0.12 * size  -- and depth.
251       if not cache_chars[cid_name][size] then
252          cache_chars[cid_name][size]  = {}
253          for k, v in pairs(cache_chars[cid_name][655360]) do
254             cache_chars[cid_name][size][k] = {
255                index = v.index, width = v.width * scale,
256                height = def_height, depth = def_depth, tounicode = v.tounicode,
257             }
258          end
259       end
260       fontdata.characters = cache_chars[cid_name][size]
261       cachedata.characters = cache_chars[cid_name][size]
262    end
263
264    -- other parameters
265    do
266       local parameters = {}
267       for k, v in pairs(s.parameters) do
268          parameters[k] = v * scale
269       end
270       fontdata.parameters  = parameters
271       fontdata.size        = size
272       fontdata.resources   = s.resources
273       cachedata.parameters = parameters
274       cachedata.size       = size
275       cachedata.resources  = s.resources
276    end
277
278    -- no embedding
279    local var = ''
280    local s = string.match(specification.detail, 'slant=([+-]*%d*%.?%d)')
281    if s and e~=0  then
282       s = s * 1000
283       var, fontdata.slant  = var .. 's' .. tostring(s), s
284    end
285    local e = string.match(specification.detail, 'extend=([+-]*%d*%.?%d)')
286    if e and e~=1  then
287       e = e * 1000
288       var, fontdata.extend  = var .. 'x' .. tostring(e), e
289    end
290    fontdata.name = specification.name .. size .. var; cachedata.name = fontdata.name
291    fontdata.fullname = specification.name .. var; cachedata.fullname = fontdata.fullname
292    fontdata.psname = specification.name; cachedata.psname = fontdata.psname
293    fonts.hashes.identifiers[id] = cachedata
294
295    return fontdata
296 end
297
298 local function font_callback(name, size, id, fallback)
299    local p = name:find(":") or name:len()+1
300    if name:sub(1, p-1) == 'psft' then
301       local s = "Adobe-Japan1-6"
302       local basename = name:sub(p+1)
303       local p = basename:find(":")
304       local q = basename:find("/[BI][BI]?")
305       if q and p and q<=p then
306          basename = basename:gsub('/[BI][BI]?', '', 1)
307          p = basename:find(":")
308       end
309       if p then
310          local xname = basename:sub(p+1)
311          p = 1
312          while p do
313             local q = xname:find(";", p+1) or xname:len()+1
314             if xname:sub(p, p+3)=='cid=' and q>p+4 then
315                s = xname:sub(p+4, q-1)
316             end
317             if xname:len()+1==q then p = nil else p = q + 1 end
318          end
319       end
320       cid_reg, cid_order = string.match(s, "^(.-)%-(.-)%-(%d-)$")
321       if not cid_reg then
322          cid_reg, cid_order = string.match(s, "^(.-)%-(.-)$")
323       end
324       cid_name = cid_reg .. '-' .. cid_order
325       if not cidfont_data[cid_name] then
326          read_cid_font()
327          if not cidfont_data[cid_name] then
328             ltjb.package_error('luatexja',
329                                "bad cid key `" .. s .. "'",
330                                "I couldn't find any non-embedded font information for the CID\n" ..
331                                   '`' .. s .. "'. For now, I'll use `Adobe-Japan1-6'.\n"..
332                                   'Please contact the LuaTeX-ja project team.')
333             cid_name = "Adobe-Japan1"
334          end
335       end
336       return mk_rml(basename, size, id)
337    else
338       local tfmdata=fallback(name, size, id)
339       if type (tfmdata) == "table" and tfmdata.shared then
340         luatexbase.call_callback("luaotfload.patch_font", tfmdata, name)
341       end
342       return tfmdata
343    end
344 end
345
346 luatexja.rmlgbm = {
347    cidfont_data = cidfont_data,
348    font_callback = font_callback,
349    vert_addfunc = function () end, -- dummy, set in ltj-direction.lua
350 }
351
352 cid_reg, cid_order, cid_name, cid_supp = 'Adobe', 'Japan1', 'Adobe-Japan1'
353 read_cid_font()
354
355