OSDN Git Service

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