OSDN Git Service

00f5be2dd46a40ec081f763eee50d81eefe49813
[luatex-ja/luatexja.git] / src / ltj-rmlgbm.lua
1 --
2 -- luatexja/rmlgbm.lua
3 --
4 luatexbase.provides_module({
5   name = 'luatexja.rmlgbm',
6   date = '2012/04/21',
7   version = '0.3',
8   description = 'Definitions of non-embedded Japanese (or other CJK) fonts',
9 })
10 module('luatexja.rmlgbm', package.seeall)
11 local err, warn, info, log = luatexbase.errwarinf(_NAME)
12
13 luatexja.load_module('base');      local ltjb = luatexja.base
14
15 local cidfont_data = {}
16 local cache_chars = {}
17 local path           = {
18     localdir  = file.join(kpse.expand_var("$TEXMFVAR"), aux_dir),
19     systemdir = file.join(kpse.expand_var("$TEXMFSYSVAR"), aux_dir),
20 }
21
22 local cid_reg, cid_order, cid_supp, cid_name
23 local taux_dir = 'luatex-cache/luatexja'
24 local cid_replace = {
25    ["Adobe-Japan1"] = "UniJIS2004-UTF32", -- JIS X 0213:2004
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 } 
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 }
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 .. '/luatexja/'
98    if not lfs.isdir(savepath) then
99       dir.mkdirs(savepath)
100    end
101    savepath = file.join(savepath, "ltj-cid-auto-" 
102                         .. string.lower(cid_name)  .. ".lua")
103    if file.iswritable(savepath) then
104       cidfont_data[cid_name].characters[46].width = 655360/14;
105       table.tofile(savepath, cidfont_data[cid_name],'return', false, true, false )
106    else 
107       ltjb.package_warning('luatexja', 
108                            'failed to save informations of non-embedded 2-byte fonts', '')
109    end
110 end
111
112 -- 
113 local function read_cid_font()
114    -- local v = "ltj-cid-" .. string.lower(cid_name) .. ".lua"
115    local v = "ltj-cid-auto-" .. string.lower(cid_name) .. ".lua"
116    local localpath  = file.join(path.localdir, v)
117    local systempath = file.join(path.systemdir, v)
118    local kpsefound  = kpse.find_file(v)
119    if kpsefound and file.isreadable(kpsefound) then
120       cidfont_data[cid_name] = require(kpsefound)
121       cache_chars[cid_name]  = { [655360] = cidfont_data[cid_name].characters }
122    elseif file.isreadable(localpath)  then
123       cidfont_data[cid_name] = require(localpath)
124       cache_chars[cid_name]  = { [655360] = cidfont_data[cid_name].characters }
125    elseif file.isreadable(systempath) then
126       cidfont_data[cid_name] = require(systempath)
127       cache_chars[cid_name]  = { [655360] = cidfont_data[cid_name].characters }
128    end
129    -- Now we must create the virtual metrics from CMap.
130    ltjb.package_info('luatexja', 
131                         'I try to generate informations of non-embedded 2-byte fonts...', '')
132    make_cid_font()
133
134    if cidfont_data[cid_name] then
135       for i,v in pairs(cidfont_data[cid_name].characters) do
136          if not v.width then v.width = 655360 end
137          v.height, v.depth = 576716.8, 78643.2 -- optimized for jfm-ujis.lua
138       end
139    end
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    for k, v in pairs(s) do
152       fontdata[k] = v
153       cachedata[k] = v
154    end
155    fontdata.characters = nil
156    cachedata.characters = nil
157    fontdata.unicodes = nil
158    fontdata.shared = nil
159    cachedata.shared = nil
160    if s.shared then
161       cachedata.shared = {}
162       local shared = cachedata.shared
163       for k, v in pairs(s.shared) do
164          shared[k] = v
165       end
166       
167       shared.set_dynamics = fonts.otf.set_dynamics 
168       shared.processes, shared.features = fonts.otf.set_features(cachedata,fonts.define.check(features,fonts.otf.features.default))
169    end
170
171    -- characters & scaling
172    if size < 0 then size = -size * 655.36 end
173    local scale = size / 655360
174    local def_height =  0.88 * size -- character's default height (optimized for jfm-ujis.lua)
175    local def_depth =  0.12 * size  -- and depth.
176    if not cache_chars[cid_name][size] then
177       cache_chars[cid_name][size]  = {}
178       for k, v in pairs(cache_chars[cid_name][655360]) do
179          cache_chars[cid_name][size][k] = { 
180             index = v.index, width = v.width * scale, 
181             height = def_height, depth = def_depth, tounicode = v.tounicode,
182          }
183       end
184    end
185    fontdata.characters = cache_chars[cid_name][size]
186    cachedata.characters = cache_chars[cid_name][size]
187
188    local parameters = {}
189    for k, v in pairs(s.parameters) do
190       parameters[k] = v * scale
191    end
192    fontdata.parameters = parameters;                cachedata.parameters = parameters
193    fontdata.ascender = fontdata.ascender * scale;   cachedata.ascender = fontdata.ascender
194    fontdata.descender = fontdata.descender * scale; cachedata.descender = fontdata.descender
195    fontdata.factor = fontdata.factor * scale;       cachedata.factor = fontdata.factor
196    fontdata.hfactor = fontdata.hfactor * scale;     cachedata.hfactor = fontdata.hfactor
197    fontdata.vfactor = fontdata.vfactor * scale;     cachedata.vfactor = fontdata.vfactor
198    fontdata.size = size;                            cachedata.size = size
199
200    -- no embedding
201    local var = ''
202    if features.slant then 
203       fontdata.slant = features.slant*1000;         cachedata.slant = fontdata.slant
204       var = var .. 's' .. tostring(features.slant)
205    end
206    if features.extend then 
207       fontdata.extend = features.extend*1000;       cachedata.extend = fontdata.extend
208        var = var .. 'x' .. tostring(features.extend)
209   end
210    fontdata.name = specification.name .. size .. var; cachedata.name = fontdata.name
211    fontdata.fullname = specification.name .. var; cachedata.fullname = fontdata.fullname
212    fontdata.psname = specification.name; cachedata.psname = fontdata.psname
213    fonts.ids[id] = cachedata
214
215    return fontdata
216 end
217
218 local dr_orig = fonts.define.read
219 function fonts.define.read(name, size, id)
220    local p = utf.find(name, ":") or utf.len(name)+1
221    if utf.sub(name, 1, p-1) == 'psft' then
222       local s = "Adobe-Japan1-6"
223       local basename = utf.sub(name,p+1)
224       local p = utf.find(basename, ":")
225       if p then 
226          local xname = utf.sub(basename, p+1)
227          p = 1
228          while p do
229             local q = utf.find(xname, ";", p+1) or utf.len(xname)+1
230             if utf.sub(xname, p, p+3)=='cid=' and q>p+4 then
231                s = utf.sub(xname, p+4, q-1)
232             end
233             if utf.len(xname)+1==q then p = nil else p = q + 1 end
234          end
235       end
236       cid_reg, cid_order = string.match(s, "^(.-)%-(.-)%-(%d-)$")
237       if not cid_reg then 
238          cid_reg, cid_order = string.match(s, "^(.-)%-(.-)$")
239       end
240       cid_name = cid_reg .. '-' .. cid_order
241       if not cidfont_data[cid_name] then 
242          read_cid_font()
243          if not cidfont_data[cid_name] then 
244             ltjb.package_error('luatexja',
245                                "bad cid key `" .. s .. "'",
246                                "I couldn't find any non-embedded font information for the CID\n" ..
247                                   '`' .. s .. "'. For now, I'll use `Adobe-Japan1-6'.\n"..
248                                   'Please contact the LuaTeX-ja project team.')
249             cid_name = "Adobe-Japan1"
250          end
251       end
252       return mk_rml(basename, size, id)
253    else 
254       return dr_orig(name, size, id)
255    end
256 end
257
258 cid_reg, cid_order, cid_name = 'Adobe', 'Japan1', 'Adobe-Japan1'
259 read_cid_font()