OSDN Git Service

d567e09203fa7841f2ca1d45cf5f3442738aece4
[luatex-ja/luatexja.git] / src / luatexja / rmlgbm.lua
1 --
2 -- luatexja/rmlgbm.lua
3 --
4 luatexbase.provides_module({
5   name = 'luatexja.rmlgbm',
6   date = '2012/02/17',
7   version = '0.2',
8   description = 'Definitions of non-embedded Japanese fonts',
9 })
10 module('luatexja.rmlgbm', package.seeall)
11 local err, warn, info, log = luatexbase.errwarinf(_NAME)
12
13 require('luatexja.base');      local ltjb = luatexja.base
14
15 local round = tex.round
16 local cidfont_data = {}
17 local cache_chars = {}
18 local cid_reg, cid_order, cid_supp, cid_name
19 local taux_dir = 'luatex-cache/luatexja'
20 local path           = {
21     localdir  = file.join(kpse.expand_var("$TEXMFVAR"), aux_dir),
22     systemdir = file.join(kpse.expand_var("$TEXMFSYSVAR"), aux_dir),
23 }
24 local cid_replace = {
25    ["Adobe-Japan1"] = "UniJISX0213-UTF32",
26    ["Adobe-Korea1"] = "UniKS-UTF32",
27    ["Adobe-GB1"]    = "UniGB-UTF32",
28    ["Adobe-CNS1"]   = "UniCNS-UTF32",
29 }
30
31 -- reading CID maps
32 local line, fh, tt
33
34 local function load_bf_char()
35    local cid, ucs, ucsa
36    line = fh:read("*l")
37    while line do
38       if line == "endcidchar" then 
39          line = fh:read("*l"); return
40       else -- WMA l is in the form "<%x+>%s%d+"
41          ucs, cid = string.match(line, "<(%x+)>%s+(%d+)")
42          cid = tonumber(cid, 10); ucs = tonumber(ucs, 16); 
43          if not tt[ucs]  then 
44             tt[ucs] = { index = cid, width = 655360 } 
45          end
46       end
47       line = fh:read("*l")
48    end
49 end
50
51 local function load_bf_range()
52    local bucs, eucs, cid
53    line = fh:read("*l")
54    while line do
55       if line == "endcidrange" then 
56          line = fh:read("*l"); return
57       else -- WMA l is in the form "<%x+>%s+<%x+>"
58          bucs, eucs, cid = string.match(line, "<(%x+)>%s+<(%x+)>%s+(%d+)")
59          cid = tonumber(cid, 10); bucs = tonumber(bucs, 16); eucs = tonumber(eucs, 16);
60          for ucs = bucs, eucs do
61             if not tt[ucs]  then 
62                tt[ucs] = { index = cid, width = 655360 }
63             end
64             cid = cid+1
65          end
66       end
67       line = fh:read("*l")
68    end
69 end
70
71 local function make_cid_font()
72    cidfont_data[cid_name] = {
73       cidinfo = { ordering=cid_order, registry=cid_reg, supplement=cid_supp },
74       encodingbytes = 2, extend=1000, format = 'opentype',
75       direction = 0, characters = {}, parameters = {}, embedding = "no", cache = "yes", 
76       ascender = 0, descender = 0, factor = 0, hfactor = 0, vfactor = 0, 
77    }
78    tt = {}
79
80    -- Open
81    -- TODO: vertical fonts?
82    fh = io.open(kpse.find_file(cid_replace[cid_name] .. "-H", 'cmap files'), "r")
83    line = fh:read("*l")
84    while line do
85       if string.find(line, "%x+%s+begincidchar") then
86          load_bf_char()
87       elseif string.find(line, "%x+%s+begincidrange") then
88          load_bf_range()
89       else
90          line = fh:read("*l")
91       end
92    end
93    fh:close();  cidfont_data[cid_name].characters = tt
94    cache_chars[cid_name]  = { [655360] = cidfont_data[cid_name].characters }
95
96    -- Save
97    local savepath  = path.localdir
98    if not lfs.isdir(savepath) then
99       dir.mkdirs(savepath)
100    end
101    savepath = file.join(savepath, "luatexja-cid-auto-" 
102                         .. string.lower(cid_name)  .. ".lua")
103    if file.iswritable(savepath) then
104       table.tofile(savepath, cidfont_data[cid_name],'return', false, true, false )
105    else 
106       ltjb.package_warning('luatexja', 
107                            'failed to save informations of non-embedded 2-byte fonts', '')
108    end
109 end
110
111 -- 
112 local function read_cid_font()
113    local names = {
114       "luatexja-cid-std-" .. string.lower(cid_name) .. ".lua",
115       "luatexja-cid-auto-" .. string.lower(cid_name) .. ".lua",
116    }
117    local s
118    for i,v in ipairs(names) do
119       local localpath  = file.join(path.localdir, v)
120       local systempath = file.join(path.systemdir, v)
121       local kpsefound  = kpse.find_file(v)
122       if kpsefound and file.isreadable(kpsefound) then
123          cidfont_data[cid_name] = require(kpsefound)
124          cache_chars[cid_name]  = { [655360] = cidfont_data[cid_name].characters }
125          return
126       elseif file.isreadable(localpath)  then
127          cidfont_data[cid_name] = require(localpath)
128          cache_chars[cid_name]  = { [655360] = cidfont_data[cid_name].characters }
129          return
130       elseif file.isreadable(systempath) then
131          cidfont_data[cid_name] = require(systempath)
132          cache_chars[cid_name]  = { [655360] = cidfont_data[cid_name].characters }
133          return
134       end
135    end
136    -- Now we must create the virtual metrics from CMap.
137    ltjb.package_info('luatexja', 
138                         'I try to generate informations of non-embedded 2-byte fonts...', '')
139    make_cid_font()
140 end
141
142 -- High-level
143 local function mk_rml(name, size, id)
144    local specification = fonts.define.analyze(name,size)
145    specification = fonts.define.specify[':'](specification)
146    local features = specification.features.normal
147
148    local fontdata = {}
149    local cachedata = {}
150    local s = cidfont_data[cid_name]
151    if not s then 
152       -- error message?
153       s = cidfont_data["Adobe-Japan1"]
154    end
155    for k, v in pairs(s) do
156       fontdata[k] = v
157       cachedata[k] = v
158    end
159    fontdata.characters = nil
160    cachedata.characters = nil
161    fontdata.unicodes = nil
162    fontdata.shared = nil
163    cachedata.shared = nil
164    if cidfont_data[cid_name].shared then
165       cachedata.shared = {}
166       local shared = cachedata.shared
167       for k, v in pairs(cidfont_data[cid_name].shared) do
168          shared[k] = v
169       end
170       
171       shared.set_dynamics = fonts.otf.set_dynamics 
172       shared.processes, shared.features = fonts.otf.set_features(cachedata,fonts.define.check(features,fonts.otf.features.default))
173    end
174
175    -- characters & scaling
176    if size < 0 then size = -size * 655.36 end
177    local scale = size / 655360
178    if not cache_chars[cid_name][size] then
179       cache_chars[cid_name][size]  = {}
180       for k, v in pairs(cache_chars[cid_name][655360]) do
181          cache_chars[cid_name][size][k] = {}
182          cache_chars[cid_name][size][k].index = v.index
183          cache_chars[cid_name][size][k].width = round(v.width * scale)
184          cache_chars[cid_name][size][k].tounicode = v.tounicode
185       end
186    end
187    fontdata.characters = cache_chars[cid_name][size]
188    cachedata.characters = cache_chars[cid_name][size]
189
190    local parameters = {}
191    for k, v in pairs(cidfont_data[cid_name].parameters) do
192       parameters[k] = v * scale
193    end
194    fontdata.parameters = parameters;                cachedata.parameters = parameters
195    fontdata.ascender = fontdata.ascender * scale;   cachedata.ascender = fontdata.ascender
196    fontdata.descender = fontdata.descender * scale; cachedata.descender = fontdata.descender
197    fontdata.factor = fontdata.factor * scale;       cachedata.factor = fontdata.factor
198    fontdata.hfactor = fontdata.hfactor * scale;     cachedata.hfactor = fontdata.hfactor
199    fontdata.vfactor = fontdata.vfactor * scale;     cachedata.vfactor = fontdata.vfactor
200    fontdata.size = size;                            cachedata.size = size
201
202    -- no embedding
203    local var = ''
204    if features.slant then 
205       fontdata.slant = features.slant*1000;         cachedata.slant = fontdata.slant
206       var = var .. 's' .. tostring(features.slant)
207    end
208    if features.extend then 
209       fontdata.extend = features.extend*1000;       cachedata.extend = fontdata.extend
210        var = var .. 'x' .. tostring(features.extend)
211   end
212    fontdata.name = specification.name .. size .. var; cachedata.name = fontdata.name
213    fontdata.fullname = specification.name .. var; cachedata.fullname = fontdata.fullname
214    fontdata.psname = specification.name; cachedata.psname = fontdata.psname
215    fonts.ids[id] = cachedata
216
217    return fontdata
218 end
219
220 local dr_orig = fonts.define.read
221 function fonts.define.read(name, size, id)
222    local p = utf.find(name, ":") or utf.len(name)+1
223    if utf.sub(name, 1, p-1) == 'psft' then
224       local s = "Adobe-Japan1-6"
225       local basename = utf.sub(name,p+1)
226       local p = utf.find(basename, ":")
227       if p then 
228          local xname = utf.sub(basename, p+1)
229          p = 1
230          while p do
231             local q = utf.find(xname, ";", p+1) or utf.len(xname)+1
232             if utf.sub(xname, p, p+3)=='cid=' and q>p+4 then
233                s = utf.sub(xname, p+4, q-1)
234             end
235             if utf.len(xname)+1==q then p = nil else p = q + 1 end
236          end
237       end
238       cid_reg, cid_order, cid_supp = string.match(s, "(.-)%-(.-)%-(%d-)")
239       cid_name = cid_reg .. '-' .. cid_order
240       if not cidfont_data[cid_name] then read_cid_font() end
241       return mk_rml(basename, size, id)
242    else 
243       return dr_orig(name, size, id)
244    end
245 end
246