OSDN Git Service

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