2 -- luatexja/ltj-rmlgbm.lua
5 luatexja.load_module('base'); local ltjb = luatexja.base
7 local cidfont_data = {}
10 localdir = file.join(kpse.expand_var("$TEXMFVAR"), aux_dir),
11 systemdir = file.join(kpse.expand_var("$TEXMFSYSVAR"), aux_dir),
14 local cid_reg, cid_order, cid_supp, cid_name
15 local taux_dir = 'luatex-cache/luatexja'
17 ["Adobe-Japan1"] = {"UniJIS-UTF32", 23057,
19 if (231<=i and i<=632) or (8718<=i and i<=8719)
20 or (12063<=i and i<=12087) then
21 return 327680 -- 655360/2
22 elseif 9758<=i and i<=9778 then
23 return 218453 -- 655360/3
24 elseif 9738<=i and i<=9757 then
25 return 163840 -- 655360/4
29 -- 基本的には JIS X 0208:1990 に沿ったマッピングだが
30 -- JIS X 0213:2004 のみにある字も使えるようにする
31 ["Adobe-Korea1"] = {"UniKS-UTF32", 18351,
33 if 8094<=i and i<=8100 then
34 return 327680 -- 655360/2
37 ["Adobe-GB1"] = {"UniGB-UTF32", 30283,
39 if (814<=i and i<=939) or (i==7716)
40 or (22355<=i and i<=22357) then
41 return 327680 -- 655360/2
44 ["Adobe-CNS1"] = {"UniCNS-UTF32", 19155,
46 if (13648<=i and i<=13742) or (i==17603) then
47 return 327680 -- 655360/2
54 local line, fh -- line, file handler
55 local tt,cidm -- characters, cid->(Unicode)
57 local function load_cid_char(cid_dec, mke)
61 if string.find(line, "end...?char") then
62 line = fh:read("*l"); return
63 else -- WMA l is in the form "<%x+>%s%d+"
64 ucs, cid = string.match(line, "<(%x+)>%s+<?(%x+)>?")
65 cid = cid_dec(cid); ucs = tonumber(ucs, 16);
67 tt[ucs] = mke(cid); cidm[cid]=ucs
74 local function load_cid_range(inc, cid_dec, mke)
78 if string.find(line, "end...?range") then
79 line = fh:read("*l"); return
80 else -- WMA l is in the form "<%x+>%s+<%x+>"
81 bucs, eucs, cid = string.match(line, "<(%x+)>%s+<(%x+)>%s+<?(%x+)>?")
83 bucs = tonumber(bucs, 16); eucs = tonumber(eucs, 16)
84 for ucs = bucs, eucs do
86 tt[ucs] = mke(cid); cidm[cid]=ucs
95 local function open_cmap_file(name, inc, cid_dec, mke)
96 fh = io.open(kpse.find_file(name, 'cmap files'), "r")
99 if string.find(line, "%x+%s+begin...?char") then
100 load_cid_char(cid_dec, mke)
101 elseif string.find(line, "%x+%s+begin...?range") then
102 load_cid_range(inc, cid_dec, mke)
110 local function increment(a) return a+1 end
111 local function entry(a) return {index = a} end
112 function make_cid_font()
114 cidinfo = { ordering=cid_order, registry=cid_reg, supplement=cid_supp },
115 encodingbytes = 2, extend=1000, format = 'opentype',
116 direction = 0, characters = {}, parameters = {}, embedding = "no", cache = "yes",
117 ascender = 0, descender = 0, factor = 0, hfactor = 0, vfactor = 0,
120 local kx = cid_replace[cid_name]
121 cidfont_data[cid_name] = k
123 -- CID => Unicode 負号空間
124 -- TODO: vertical fonts?
126 for i = 0,kx[2] do cidm[i] = -1 end
127 open_cmap_file(kx[1] .. "-H", increment, tonumber, entry)
129 open_cmap_file(kx[4] .. "-H", increment, tonumber, entry)
133 -- Unicode にマップされなかった文字の処理
134 -- これらは TrueType フォントを使って表示するときはおかしくなる
135 local ttu, pricode = {}, 0xF0000
136 for i,v in ipairs(cidm) do
138 tt[pricode], cidm[i], pricode= { index = i }, pricode, pricode+1;
140 ttu[cid_order .. '.' .. i] = cidm[i]
143 cache_chars[cid_name] = { [655360] = k.characters }
146 local cidp = {nil, nil}; local cidmo = cidm
147 tt, ttu, cidm = {}, {}, {}
148 open_cmap_file(cid_name .. "-UCS2",
150 a[2] = a[2] +1 ; return a
153 cidp[1] = string.upper(string.sub(a,1,string.len(a)-4))
154 cidp[2] = tonumber(string.sub(a,-4),16)
157 function(a) return a[1] ..string.format('%04X',a[2]) end)
158 -- tt は cid -> tounicode になっているので cidm -> tounicode に変換
159 for i,v in ipairs(cidmo) do
160 k.characters[v].width = kx[3](i)
162 k.characters[v].tounicode = tt[i]
169 cidinfo= k.cidinfo, verbose = false,
170 shared = { featuredata = {}, },
171 luatex = { features = {}, defaultwidth=1000, sequences = { }, },
173 dynamics = {}, features = {}, processes = {},
178 local savepath = path.localdir .. '/luatexja/'
179 if not lfs.isdir(savepath) then
182 savepath = file.join(savepath, "ltj-cid-auto-"
183 .. string.lower(cid_name) .. ".lua")
184 if file.iswritable(savepath) then
185 k.characters[46].width = math.floor(655360/14);
186 -- Standard fonts are ``seriffed''.
187 table.tofile(savepath, k,'return', false, true, false )
189 ltjb.package_warning('luatexja',
190 'failed to save informations of non-embedded 2-byte fonts', '')
194 local make_cid_font = make_cid_font
197 local function read_cid_font()
198 -- local v = "ltj-cid-" .. string.lower(cid_name) .. ".lua"
199 local v = "ltj-cid-auto-" .. string.lower(cid_name) .. ".lua"
200 local localpath = file.join(path.localdir, v)
201 local systempath = file.join(path.systemdir, v)
202 local kpsefound = kpse.find_file(v)
203 if kpsefound and file.isreadable(kpsefound) then
204 cidfont_data[cid_name] = require(kpsefound)
205 cache_chars[cid_name] = { [655360] = cidfont_data[cid_name].characters }
206 elseif file.isreadable(localpath) then
207 cidfont_data[cid_name] = require(localpath)
208 cache_chars[cid_name] = { [655360] = cidfont_data[cid_name].characters }
209 elseif file.isreadable(systempath) then
210 cidfont_data[cid_name] = require(systempath)
211 cache_chars[cid_name] = { [655360] = cidfont_data[cid_name].characters }
213 -- Now we must create the virtual metrics from CMap.
214 ltjb.package_info('luatexja',
215 'I try to generate informations of non-embedded 2-byte fonts...', '')
218 if cidfont_data[cid_name] then
219 for i,v in pairs(cidfont_data[cid_name].characters) do
220 if not v.width then v.width = 655360 end
221 v.height, v.depth = 576716.8, 78643.2 -- optimized for jfm-ujis.lua
227 local function mk_rml(name, size, id)
228 local specification = fonts.define.analyze(name,size)
229 specification = fonts.define.specify[':'](specification)
230 local features = specification.features.normal
234 local s = cidfont_data[cid_name]
235 for k, v in pairs(s) do
239 fontdata.characters = nil
240 cachedata.characters = nil
241 fontdata.unicodes = nil
242 fontdata.shared = nil
243 cachedata.shared = nil
245 cachedata.shared = {}
246 local shared = cachedata.shared
247 for k, v in pairs(s.shared) do
251 shared.set_dynamics = fonts.otf.set_dynamics
252 shared.processes, shared.features = fonts.otf.set_features(cachedata,fonts.define.check(features,fonts.otf.features.default))
255 -- characters & scaling
256 if size < 0 then size = -size * 655.36 end
257 local scale = size / 655360
258 local def_height = 0.88 * size -- character's default height (optimized for jfm-ujis.lua)
259 local def_depth = 0.12 * size -- and depth.
260 if not cache_chars[cid_name][size] then
261 cache_chars[cid_name][size] = {}
262 for k, v in pairs(cache_chars[cid_name][655360]) do
263 cache_chars[cid_name][size][k] = {
264 index = v.index, width = v.width * scale,
265 height = def_height, depth = def_depth, tounicode = v.tounicode,
269 fontdata.characters = cache_chars[cid_name][size]
270 cachedata.characters = cache_chars[cid_name][size]
272 local parameters = {}
273 for k, v in pairs(s.parameters) do
274 parameters[k] = v * scale
276 fontdata.parameters = parameters; cachedata.parameters = parameters
277 fontdata.ascender = fontdata.ascender * scale; cachedata.ascender = fontdata.ascender
278 fontdata.descender = fontdata.descender * scale; cachedata.descender = fontdata.descender
279 fontdata.factor = fontdata.factor * scale; cachedata.factor = fontdata.factor
280 fontdata.hfactor = fontdata.hfactor * scale; cachedata.hfactor = fontdata.hfactor
281 fontdata.vfactor = fontdata.vfactor * scale; cachedata.vfactor = fontdata.vfactor
282 fontdata.size = size; cachedata.size = size
286 if features.slant then
287 fontdata.slant = features.slant*1000; cachedata.slant = fontdata.slant
288 var = var .. 's' .. tostring(features.slant)
290 if features.extend then
291 fontdata.extend = features.extend*1000; cachedata.extend = fontdata.extend
292 var = var .. 'x' .. tostring(features.extend)
294 fontdata.name = specification.name .. size .. var; cachedata.name = fontdata.name
295 fontdata.fullname = specification.name .. var; cachedata.fullname = fontdata.fullname
296 fontdata.psname = specification.name; cachedata.psname = fontdata.psname
297 fonts.ids[id] = cachedata
302 local dr_orig = fonts.define.read
303 function fonts.define.read(name, size, id)
304 local p = utf.find(name, ":") or utf.len(name)+1
305 if utf.sub(name, 1, p-1) == 'psft' then
306 local s = "Adobe-Japan1-6"
307 local basename = utf.sub(name,p+1)
308 local p = utf.find(basename, ":")
310 local xname = utf.sub(basename, p+1)
313 local q = utf.find(xname, ";", p+1) or utf.len(xname)+1
314 if utf.sub(xname, p, p+3)=='cid=' and q>p+4 then
315 s = utf.sub(xname, p+4, q-1)
317 if utf.len(xname)+1==q then p = nil else p = q + 1 end
320 cid_reg, cid_order = string.match(s, "^(.-)%-(.-)%-(%d-)$")
322 cid_reg, cid_order = string.match(s, "^(.-)%-(.-)$")
324 cid_name = cid_reg .. '-' .. cid_order
325 if not cidfont_data[cid_name] then
327 if not cidfont_data[cid_name] then
328 ltjb.package_error('luatexja',
329 "bad cid key `" .. s .. "'",
330 "I couldn't find any non-embedded font information for the CID\n" ..
331 '`' .. s .. "'. For now, I'll use `Adobe-Japan1-6'.\n"..
332 'Please contact the LuaTeX-ja project team.')
333 cid_name = "Adobe-Japan1"
336 return mk_rml(basename, size, id)
338 return dr_orig(name, size, id)
342 cid_reg, cid_order, cid_name = 'Adobe', 'Japan1', 'Adobe-Japan1'
346 cidfont_data = cidfont_data,