OSDN Git Service

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