OSDN Git Service

ltj-rmlgbm.lua: support 'vert' feature
[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 = 5
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", 23057, 6,
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", 19155, 6,
39                        function (i)
40                           if (13648<=i and i<=13742) or (i==17603) then
41                              return 327680 -- 655360/2
42                           end
43                        end},
44 }
45
46 -- reading CID maps
47 local make_cid_font
48 do
49    local line, fh -- line, file handler
50    local tt,cidm -- characters, cid->(Unicode)
51
52    local function load_cid_char(cid_dec, mke)
53       local cid, ucs, ucsa
54       line = fh:read("*l")
55       while line do
56          if string.find(line, "end...?char") then
57             line = fh:read("*l"); return
58          else -- WMA l is in the form "<%x+>%s%d+"
59             ucs, cid = string.match(line, "<(%x+)>%s+<?(%x+)>?")
60             cid = cid_dec(cid); ucs = tonumber(ucs, 16);
61             if not tt[ucs]  then
62                tt[ucs] = mke(cid); cidm[cid]=ucs
63             end
64          end
65          line = fh:read("*l")
66       end
67    end
68
69    local function load_cid_range(inc, cid_dec, mke)
70       local bucs, eucs, cid
71       line = fh:read("*l")
72       while line do
73         if string.find(line, "end...?range") then
74             line = fh:read("*l"); return
75          else -- WMA l is in the form "<%x+>%s+<%x+>"
76             bucs, eucs, cid = string.match(line, "<(%x+)>%s+<(%x+)>%s+<?(%x+)>?")
77             cid = cid_dec(cid);
78             bucs = tonumber(bucs, 16); eucs = tonumber(eucs, 16)
79             for ucs = bucs, eucs do
80                if not tt[ucs]  then
81                   tt[ucs] = mke(cid); cidm[cid]=ucs
82                end
83                cid = inc(cid)
84             end
85          end
86          line = fh:read("*l")
87       end
88    end
89
90    local function open_cmap_file(name, inc, cid_dec, mke)
91       fh = io.open(kpse.find_file(name, 'cmap files'), "r")
92       line = fh:read("*l")
93       while line do
94          if string.find(line, "%x+%s+begin...?char") then
95             load_cid_char(cid_dec, mke)
96          elseif string.find(line, "%x+%s+begin...?range") then
97             load_cid_range(inc, cid_dec, mke)
98          else
99             line = fh:read("*l")
100          end
101       end
102       fh:close();
103    end
104
105    local function increment(a) return a+1 end
106    local function entry(a)
107       return {index = a}
108    end
109    make_cid_font = function ()
110       local kx = cid_replace[cid_name]
111       if not kx then return end
112       local k = {
113          cidinfo = { ordering=cid_order, registry=cid_reg, supplement=kx[3] },
114          encodingbytes = 2, extend=1000, format = 'opentype',
115          direction = 0, characters = {}, parameters = {
116             ascender = 655360*0.88,
117             descender = 655360*0.12,
118          },
119          embedding = "no", cache = "yes", factor = 0, hfactor = 0, vfactor = 0,
120          tounicode = 1,
121       }
122       cidfont_data[cid_name] = k
123
124       -- CID => Unicode 符号空間
125       local tth, cidmo = {}, {}
126       tt, cidm = tth, cidmo
127       for i = 0,kx[2] do cidm[i] = -1 end
128       open_cmap_file(kx[1] .. "-H", increment, tonumber, entry)
129       k.characters = tth
130
131       -- Unicode にマップされなかった文字の処理
132       -- これらは TrueType フォントを使って表示するときはおかしくなる
133       local ttu, pricode = {}, 0xF0000
134       for i,v in ipairs(cidmo) do
135          if v==-1 then
136             tth[pricode], cidmo[i], pricode
137                = { index = i }, pricode, pricode+1;
138          end
139          ttu[cid_order .. '.' .. i] = cidmo[i]
140       end
141
142       -- shared
143       k.shared = {
144          otfdata = {
145             cidinfo= k.cidinfo, verbose = false,
146             shared = { featuredata = {}, },
147          },
148          dynamics = {}, features = {}, processes = {},
149          --rawdata = { descriptions = {} },
150      }
151       k.resources = { unicodes = ttu, }
152       k.descriptions = {}
153       cache_chars[cid_name]  = { [655360] = k.characters }
154
155       -- 縦書用字形
156       tt, cidm = {}, {}
157       local ttv = {}; k.shared.ltj_vert_table = ttv
158       for i = 0,kx[2] do cidm[i] = -1 end
159       open_cmap_file(kx[1] .. "-V", increment, tonumber, entry)
160       for i,v in pairs(tt) do
161          ttv[i] =  cidmo[v.index]
162       end
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 cidf_vert_processor
197 do
198    local traverse_id = node.traverse_id
199    local id_glyph = node.id('glyph')
200    cidf_vert_processor = {
201       function (head, fnum)
202          local fontdata = identifiers[fnum]
203          if fontdata.is_ltj_vert then
204             local vt = fontdata.shared.ltj_vert_table
205             for n in traverse_id(id_glyph, head) do
206                if n.font==fnum then
207                   n.char = vt[n.char] or n.char
208                end
209             end
210             return head, false
211          end
212       end
213    }
214 end
215
216 local function cid_cache_outdated(t) return t.version~=cache_ver end
217 local function read_cid_font()
218    local dat = ltjb.load_cache("ltj-cid-auto-" .. string.lower(cid_name),
219                                cid_cache_outdated )
220    if dat then
221       cidfont_data[cid_name] = dat[1]
222       cache_chars[cid_name]  = { [655360] = cidfont_data[cid_name].characters }
223    else
224       -- Now we must create the virtual metrics from CMap.
225       make_cid_font()
226    end
227    if cidfont_data[cid_name] then
228       cidfont_data[cid_name].shared.processes = cidf_vert_processor
229       for i,v in pairs(cidfont_data[cid_name].characters) do
230          if not v.width then v.width = 655360 end
231          v.height, v.depth = 576716.8, 78643.2 -- optimized for jfm-ujis.lua
232       end
233    end
234 end
235
236 -- High-level
237
238 local definers = fonts.definers
239 local function mk_rml(name, size, id)
240    local specification = definers.analyze(name,size)
241    --specification = definers.resolve(specification) (not needed)
242    specification.detail = specification.detail or ''
243
244    local fontdata = {}
245    local cachedata = {}
246    local s = cidfont_data[cid_name]
247    luatexja.rmlgbm.vert_addfunc(id)
248    for k, v in pairs(s) do
249       fontdata[k] = v
250       cachedata[k] = v
251    end
252    fontdata.characters = nil
253    cachedata.characters = nil
254    fontdata.shared = nil
255    cachedata.shared = nil
256    if s.shared then
257       cachedata.shared = {}
258       local shared = cachedata.shared
259       for k, v in pairs(s.shared) do
260          shared[k] = v
261       end
262    end
263
264    -- characters & scaling
265    if size < 0 then size = -size * 655.36 end
266    local scale = size / 655360
267
268    do
269       local def_height =  0.88 * size
270       -- character's default height (optimized for jfm-ujis.lua)
271       local def_depth =  0.12 * size  -- and depth.
272       if not cache_chars[cid_name][size] then
273          cache_chars[cid_name][size]  = {}
274          for k, v in pairs(cache_chars[cid_name][655360]) do
275             cache_chars[cid_name][size][k] = {
276                index = v.index, width = v.width * scale,
277                height = def_height, depth = def_depth, tounicode = v.tounicode,
278             }
279          end
280       end
281       fontdata.characters = cache_chars[cid_name][size]
282       cachedata.characters = cache_chars[cid_name][size]
283    end
284
285    -- other parameters
286    do
287       local parameters = {}
288       for k, v in pairs(s.parameters) do
289          parameters[k] = v * scale
290       end
291       fontdata.parameters  = parameters
292       fontdata.size        = size
293       fontdata.resources   = s.resources
294       cachedata.parameters = parameters
295       cachedata.size       = size
296       cachedata.resources  = s.resources
297    end
298
299    -- no embedding
300    local var = ''
301    local s = string.match(specification.detail, 'slant=([+-]*%d*%.?%d)')
302    if s and e~=0  then
303       s = s * 1000
304       var, fontdata.slant  = var .. 's' .. tostring(s), s
305    end
306    local e = string.match(specification.detail, 'extend=([+-]*%d*%.?%d)')
307    if e and e~=1  then
308       e = e * 1000
309       var, fontdata.extend  = var .. 'x' .. tostring(e), e
310    end
311    if string.match(specification.detail, '(%+?vert)') then
312       cachedata.is_ltj_vert = true
313    end
314    fontdata.name = specification.name .. size .. var; cachedata.name = fontdata.name
315    fontdata.fullname = specification.name .. var; cachedata.fullname = fontdata.fullname
316    fontdata.psname = specification.name; cachedata.psname = fontdata.psname
317    identifiers[id] = cachedata
318
319    return fontdata
320 end
321
322 local function font_callback(name, size, id, fallback)
323    local p = name:find(":") or name:len()+1
324    if name:sub(1, p-1) == 'psft' then
325       local s = "Adobe-Japan1-6"
326       local basename = name:sub(p+1)
327       local p = basename:find(":")
328       local q = basename:find("/[BI][BI]?")
329       if q and p and q<=p then
330          basename = basename:gsub('/[BI][BI]?', '', 1)
331          p = basename:find(":")
332       end
333       if p then
334          local xname = basename:sub(p+1)
335          p = 1
336          while p do
337             local q = xname:find(";", p+1) or xname:len()+1
338             if xname:sub(p, p+3)=='cid=' and q>p+4 then
339                s = xname:sub(p+4, q-1)
340             end
341             if xname:len()+1==q then p = nil else p = q + 1 end
342          end
343       end
344       cid_reg, cid_order = string.match(s, "^(.-)%-(.-)%-(%d-)$")
345       if not cid_reg then
346          cid_reg, cid_order = string.match(s, "^(.-)%-(.-)$")
347       end
348       cid_name = cid_reg .. '-' .. cid_order
349       if not cidfont_data[cid_name] then
350          read_cid_font()
351          if not cidfont_data[cid_name] then
352             ltjb.package_error('luatexja',
353                                "bad cid key `" .. s .. "'",
354                                "I couldn't find any non-embedded font information for the CID\n" ..
355                                   '`' .. s .. "'. For now, I'll use `Adobe-Japan1-6'.\n"..
356                                   'Please contact the LuaTeX-ja project team.')
357             cid_name = "Adobe-Japan1"
358          end
359       end
360       return mk_rml(basename, size, id)
361    else
362       local tfmdata=fallback(name, size, id)
363       if type (tfmdata) == "table" and tfmdata.shared then
364         luatexbase.call_callback("luaotfload.patch_font", tfmdata, name)
365       end
366       return tfmdata
367    end
368 end
369
370 luatexja.rmlgbm = {
371    cidfont_data = cidfont_data,
372    font_callback = font_callback,
373    vert_addfunc = function () end, -- dummy, set in ltj-direction.lua
374 }
375
376 cid_reg, cid_order, cid_name, cid_supp = 'Adobe', 'Japan1', 'Adobe-Japan1'
377 read_cid_font()