OSDN Git Service

changed spec. of JFM
[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 = '4'
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       -- shared
142       k.shared = {
143          otfdata = {
144             cidinfo= k.cidinfo, verbose = false,
145             shared = { featuredata = {}, },
146             luatex = { features = {},
147                        defaultwidth=1000,
148                      },
149          },
150          dynamics = {}, features = {}, processes = {},
151          --rawdata = { descriptions = {} },
152      }
153       k.resources = { unicodes = ttu, }
154       k.descriptions = {}
155       cache_chars[cid_name]  = { [655360] = k.characters }
156
157       -- 縦書用字形
158       tt, cidm = {}, {}
159       for i = 0,kx[2] do cidm[i] = -1 end
160       open_cmap_file(kx[1] .. "-V", increment, tonumber, entry)
161       for i,v in pairs(tt) do
162          ttv[i] =  cidmo[v.index]
163       end
164
165       -- tounicode エントリ
166       local cidp = {nil, nil}; tt, ttu, cidm = {}, {}, {}
167       open_cmap_file(cid_name .. "-UCS2",
168                      function(a)
169                         a[2] = a[2] +1 ; return a
170                      end,
171                      function(a)
172                         cidp[1] = string.upper(string.sub(a,1,string.len(a)-4))
173                         cidp[2] = tonumber(string.sub(a,-4),16)
174                         return cidp
175                      end,
176                      function(a) return a[1] ..string.format('%04X',a[2])  end)
177       -- tt は cid -> tounicode になっているので cidm -> tounicode に変換
178       local kxf = kx[4]
179       for i,v in ipairs(cidmo) do
180          k.characters[v].width = kxf(i)
181          if v>=0xF0000 then
182             k.characters[v].tounicode = tt[i]
183          end
184       end
185
186       -- Save
187       k.characters[46].width = math.floor(655360/14);
188       ltjb.save_cache( "ltj-cid-auto-" .. string.lower(cid_name),
189                        {
190                           version = cache_ver,
191                           k,
192                        })
193    end
194 end
195
196 --
197 local function cid_cache_outdated(t) return t.version~=cache_ver end
198 local function read_cid_font()
199    local dat = ltjb.load_cache("ltj-cid-auto-" .. string.lower(cid_name),
200                                cid_cache_outdated )
201    if dat then
202       cidfont_data[cid_name] = dat[1]
203       cache_chars[cid_name]  = { [655360] = cidfont_data[cid_name].characters }
204    else
205       -- Now we must create the virtual metrics from CMap.
206       make_cid_font()
207    end
208    if cidfont_data[cid_name] then
209       for i,v in pairs(cidfont_data[cid_name].characters) do
210          if not v.width then v.width = 655360 end
211          v.height, v.depth = 576716.8, 78643.2 -- optimized for jfm-ujis.lua
212       end
213    end
214 end
215
216 -- High-level
217
218 local definers = fonts.definers
219 local function mk_rml(name, size, id)
220    local specification = definers.analyze(name,size)
221    --specification = definers.resolve(specification) (not needed)
222    specification.detail = specification.detail or ''
223
224    local fontdata = {}
225    local cachedata = {}
226    local s = cidfont_data[cid_name]
227    luatexja.rmlgbm.vert_addfunc(id)
228    for k, v in pairs(s) do
229       fontdata[k] = v
230       cachedata[k] = v
231    end
232    fontdata.characters = nil
233    cachedata.characters = nil
234    fontdata.shared = nil
235    cachedata.shared = nil
236    if s.shared then
237       cachedata.shared = {}
238       local shared = cachedata.shared
239       for k, v in pairs(s.shared) do
240          shared[k] = v
241       end
242    end
243
244    -- characters & scaling
245    if size < 0 then size = -size * 655.36 end
246    local scale = size / 655360
247
248    do
249       local def_height =  0.88 * size
250       -- character's default height (optimized for jfm-ujis.lua)
251       local def_depth =  0.12 * size  -- and depth.
252       if not cache_chars[cid_name][size] then
253          cache_chars[cid_name][size]  = {}
254          for k, v in pairs(cache_chars[cid_name][655360]) do
255             cache_chars[cid_name][size][k] = {
256                index = v.index, width = v.width * scale,
257                height = def_height, depth = def_depth, tounicode = v.tounicode,
258             }
259          end
260       end
261       fontdata.characters = cache_chars[cid_name][size]
262       cachedata.characters = cache_chars[cid_name][size]
263    end
264
265    -- other parameters
266    do
267       local parameters = {}
268       for k, v in pairs(s.parameters) do
269          parameters[k] = v * scale
270       end
271       fontdata.parameters  = parameters
272       fontdata.size        = size
273       fontdata.resources   = s.resources
274       cachedata.parameters = parameters
275       cachedata.size       = size
276       cachedata.resources  = s.resources
277    end
278
279    -- no embedding
280    local var = ''
281    local s = string.match(specification.detail, 'slant=([+-]*%d*%.?%d)')
282    if s and e~=0  then
283       s = s * 1000
284       var, fontdata.slant  = var .. 's' .. tostring(s), s
285    end
286    local e = string.match(specification.detail, 'extend=([+-]*%d*%.?%d)')
287    if e and e~=1  then
288       e = e * 1000
289       var, fontdata.extend  = var .. 'x' .. tostring(e), e
290    end
291    fontdata.name = specification.name .. size .. var; cachedata.name = fontdata.name
292    fontdata.fullname = specification.name .. var; cachedata.fullname = fontdata.fullname
293    fontdata.psname = specification.name; cachedata.psname = fontdata.psname
294    fonts.hashes.identifiers[id] = cachedata
295
296    return fontdata
297 end
298
299 local function font_callback(name, size, id, fallback)
300    local p = name:find(":") or name:len()+1
301    if name:sub(1, p-1) == 'psft' then
302       local s = "Adobe-Japan1-6"
303       local basename = name:sub(p+1)
304       local p = basename:find(":")
305       local q = basename:find("/[BI][BI]?")
306       if q and p and q<=p then
307          basename = basename:gsub('/[BI][BI]?', '', 1)
308          p = basename:find(":")
309       end
310       if p then
311          local xname = basename:sub(p+1)
312          p = 1
313          while p do
314             local q = xname:find(";", p+1) or xname:len()+1
315             if xname:sub(p, p+3)=='cid=' and q>p+4 then
316                s = xname:sub(p+4, q-1)
317             end
318             if xname:len()+1==q then p = nil else p = q + 1 end
319          end
320       end
321       cid_reg, cid_order = string.match(s, "^(.-)%-(.-)%-(%d-)$")
322       if not cid_reg then
323          cid_reg, cid_order = string.match(s, "^(.-)%-(.-)$")
324       end
325       cid_name = cid_reg .. '-' .. cid_order
326       if not cidfont_data[cid_name] then
327          read_cid_font()
328          if not cidfont_data[cid_name] then
329             ltjb.package_error('luatexja',
330                                "bad cid key `" .. s .. "'",
331                                "I couldn't find any non-embedded font information for the CID\n" ..
332                                   '`' .. s .. "'. For now, I'll use `Adobe-Japan1-6'.\n"..
333                                   'Please contact the LuaTeX-ja project team.')
334             cid_name = "Adobe-Japan1"
335          end
336       end
337       return mk_rml(basename, size, id)
338    else
339       local tfmdata=fallback(name, size, id)
340       if type (tfmdata) == "table" and tfmdata.shared then
341         luatexbase.call_callback("luaotfload.patch_font", tfmdata, name)
342       end
343       return tfmdata
344    end
345 end
346
347 luatexja.rmlgbm = {
348    cidfont_data = cidfont_data,
349    font_callback = font_callback,
350    vert_addfunc = function () end, -- dummy, set in ltj-direction.lua
351 }
352
353 cid_reg, cid_order, cid_name, cid_supp = 'Adobe', 'Japan1', 'Adobe-Japan1'
354 read_cid_font()