4 luatexbase.provides_module({
5 name = 'luatexja.rmlgbm',
7 description = 'Definitions of non-embedded Japanese (or other CJK) fonts',
9 module('luatexja.rmlgbm', package.seeall)
10 local err, warn, info, log = luatexbase.errwarinf(_NAME)
12 luatexja.load_module('base'); local ltjb = luatexja.base
15 local cidfont_data = cidfont_data
16 local cache_chars = {}
19 local cid_reg, cid_order, cid_supp, cid_name
21 ["Adobe-Japan1"] = {"UniJIS2004-UTF32", 23057, 6,
23 if (231<=i and i<=632) or (8718<=i and i<=8719)
24 or (12063<=i and i<=12087) then
25 return 327680 -- 655360/2
26 elseif 9758<=i and i<=9778 then
27 return 218453 -- 655360/3
28 elseif 9738<=i and i<=9757 then
29 return 163840 -- 655360/4
32 -- 基本的には JIS X 0213:2004 に沿ったマッピング
33 ["Adobe-Korea1"] = {"UniKS-UTF32", 18351, 2,
35 if 8094<=i and i<=8100 then
36 return 327680 -- 655360/2
39 ["Adobe-GB1"] = {"UniGB-UTF32", 30283, 5,
41 if (814<=i and i<=939) or (i==7716)
42 or (22355<=i and i<=22357) then
43 return 327680 -- 655360/2
46 ["Adobe-CNS1"] = {"UniCNS-UTF32", 19155, 6,
48 if (13648<=i and i<=13742) or (i==17603) then
49 return 327680 -- 655360/2
56 local line, fh -- line, file handler
57 local tt,cidm -- characters, cid->(Unicode)
59 local function load_cid_char(cid_dec, mke)
63 if string.find(line, "end...?char") then
64 line = fh:read("*l"); return
65 else -- WMA l is in the form "<%x+>%s%d+"
66 ucs, cid = string.match(line, "<(%x+)>%s+<?(%x+)>?")
67 cid = cid_dec(cid); ucs = tonumber(ucs, 16);
69 tt[ucs] = mke(cid); cidm[cid]=ucs
76 local function load_cid_range(inc, cid_dec, mke)
80 if string.find(line, "end...?range") then
81 line = fh:read("*l"); return
82 else -- WMA l is in the form "<%x+>%s+<%x+>"
83 bucs, eucs, cid = string.match(line, "<(%x+)>%s+<(%x+)>%s+<?(%x+)>?")
85 bucs = tonumber(bucs, 16); eucs = tonumber(eucs, 16)
86 for ucs = bucs, eucs do
88 tt[ucs] = mke(cid); cidm[cid]=ucs
97 local function open_cmap_file(name, inc, cid_dec, mke)
98 fh = io.open(kpse.find_file(name, 'cmap files'), "r")
101 if string.find(line, "%x+%s+begin...?char") then
102 load_cid_char(cid_dec, mke)
103 elseif string.find(line, "%x+%s+begin...?range") then
104 load_cid_range(inc, cid_dec, mke)
112 local function increment(a) return a+1 end
113 local function entry(a)
116 function make_cid_font()
117 local kx = cid_replace[cid_name]
118 if not kx then return end
120 cidinfo = { ordering=cid_order, registry=cid_reg, supplement=kx[3] },
121 encodingbytes = 2, extend=1000, format = 'opentype',
122 direction = 0, characters = {}, parameters = {}, embedding = "no", cache = "yes",
123 ascender = 0, descender = 0, factor = 0, hfactor = 0, vfactor = 0,
126 cidfont_data[cid_name] = k
128 -- CID => Unicode 符号空間
129 -- TODO: vertical fonts?
131 for i = 0,kx[2] do cidm[i] = -1 end
132 open_cmap_file(kx[1] .. "-H", increment, tonumber, entry)
135 -- Unicode にマップされなかった文字の処理
136 -- これらは TrueType フォントを使って表示するときはおかしくなる
137 local ttu, pricode = {}, 0xF0000
138 for i,v in ipairs(cidm) do
140 tt[pricode], cidm[i], pricode
141 = { index = i }, pricode, pricode+1;
143 ttu[cid_order .. '.' .. i] = cidm[i]
148 cidinfo= k.cidinfo, verbose = false,
149 shared = { featuredata = {}, },
150 luatex = { features = {},
154 dynamics = {}, features = {}, processes = {},
156 k.resources = { unicodes = ttu, }
158 cache_chars[cid_name] = { [655360] = k.characters }
161 local cidp = {nil, nil}; local cidmo = cidm
162 tt, ttu, cidm = {}, {}, {}
163 open_cmap_file(cid_name .. "-UCS2",
165 a[2] = a[2] +1 ; return a
168 cidp[1] = string.upper(string.sub(a,1,string.len(a)-4))
169 cidp[2] = tonumber(string.sub(a,-4),16)
172 function(a) return a[1] ..string.format('%04X',a[2]) end)
173 -- tt は cid -> tounicode になっているので cidm -> tounicode に変換
175 for i,v in ipairs(cidmo) do
176 k.characters[v].width = kxf(i)
178 k.characters[v].tounicode = tt[i]
183 k.characters[46].width = math.floor(655360/14);
184 ltjb.save_cache( "ltj-cid-auto-" .. string.lower(cid_name),
191 local make_cid_font = make_cid_font
194 local function cid_cache_outdated(t) return t.version~=cache_ver end
195 local function read_cid_font()
196 local dat = ltjb.load_cache("ltj-cid-auto-" .. string.lower(cid_name),
199 cidfont_data[cid_name] = dat[1]
200 cache_chars[cid_name] = { [655360] = cidfont_data[cid_name].characters }
202 -- Now we must create the virtual metrics from CMap.
205 if cidfont_data[cid_name] then
206 for i,v in pairs(cidfont_data[cid_name].characters) do
207 if not v.width then v.width = 655360 end
208 v.height, v.depth = 576716.8, 78643.2 -- optimized for jfm-ujis.lua
215 local definers = fonts.definers
216 local function mk_rml(name, size, id)
217 local specification = definers.analyze(name,size)
218 specification = definers.resolve(specification)
219 specification.detail = specification.detail or ''
223 local s = cidfont_data[cid_name]
224 for k, v in pairs(s) do
228 fontdata.characters = nil
229 cachedata.characters = nil
230 fontdata.shared = nil
231 cachedata.shared = nil
233 cachedata.shared = {}
234 local shared = cachedata.shared
235 for k, v in pairs(s.shared) do
240 -- characters & scaling
241 if size < 0 then size = -size * 655.36 end
242 local scale = size / 655360
245 local def_height = 0.88 * size
246 -- character's default height (optimized for jfm-ujis.lua)
247 local def_depth = 0.12 * size -- and depth.
248 if not cache_chars[cid_name][size] then
249 cache_chars[cid_name][size] = {}
250 for k, v in pairs(cache_chars[cid_name][655360]) do
251 cache_chars[cid_name][size][k] = {
252 index = v.index, width = v.width * scale,
253 height = def_height, depth = def_depth, tounicode = v.tounicode,
257 fontdata.characters = cache_chars[cid_name][size]
258 cachedata.characters = cache_chars[cid_name][size]
263 local parameters = {}
264 for k, v in pairs(s.parameters) do
265 parameters[k] = v * scale
267 fontdata.parameters = parameters
268 fontdata.ascender = fontdata.ascender * scale
269 fontdata.descender = fontdata.descender * scale
270 fontdata.factor = fontdata.factor * scale
271 fontdata.hfactor = fontdata.hfactor * scale
272 fontdata.vfactor = fontdata.vfactor * scale
274 fontdata.resources = s.resources
275 cachedata.parameters = parameters
276 cachedata.ascender = fontdata.ascender
277 cachedata.descender = fontdata.descender
278 cachedata.factor = fontdata.factor
279 cachedata.hfactor = fontdata.hfactor
280 cachedata.vfactor = fontdata.vfactor
281 cachedata.size = size
282 cachedata.resources = s.resources
287 local s = string.match(specification.detail, 'slant=([+-]*%d*%.?%d)')
290 var, fontdata.slant = var .. 's' .. tostring(s), s
292 local e = string.match(specification.detail, 'extend=([+-]*%d*%.?%d)')
295 var, fontdata.extend = var .. 'x' .. tostring(e), e
297 fontdata.name = specification.name .. size .. var; cachedata.name = fontdata.name
298 fontdata.fullname = specification.name .. var; cachedata.fullname = fontdata.fullname
299 fontdata.psname = specification.name; cachedata.psname = fontdata.psname
300 fonts.hashes.identifiers[id] = cachedata
305 function font_callback(name, size, id, fallback)
306 local p = utf.find(name, ":") or utf.len(name)+1
307 if utf.sub(name, 1, p-1) == 'psft' then
308 local s = "Adobe-Japan1-6"
309 local basename = utf.sub(name,p+1)
310 local p = utf.find(basename, ":")
311 local q = utf.find(basename, "/[BI][BI]?")
312 if q and p and q<=p then
313 basename = utf.gsub(basename, '/[BI][BI]?', '', 1)
314 p = utf.find(basename, ":")
317 local xname = utf.sub(basename, p+1)
320 local q = utf.find(xname, ";", p+1) or utf.len(xname)+1
321 if utf.sub(xname, p, p+3)=='cid=' and q>p+4 then
322 s = utf.sub(xname, p+4, q-1)
324 if utf.len(xname)+1==q then p = nil else p = q + 1 end
327 cid_reg, cid_order = string.match(s, "^(.-)%-(.-)%-(%d-)$")
329 cid_reg, cid_order = string.match(s, "^(.-)%-(.-)$")
331 cid_name = cid_reg .. '-' .. cid_order
332 if not cidfont_data[cid_name] then
334 if not cidfont_data[cid_name] then
335 ltjb.package_error('luatexja',
336 "bad cid key `" .. s .. "'",
337 "I couldn't find any non-embedded font information for the CID\n" ..
338 '`' .. s .. "'. For now, I'll use `Adobe-Japan1-6'.\n"..
339 'Please contact the LuaTeX-ja project team.')
340 cid_name = "Adobe-Japan1"
343 return mk_rml(basename, size, id)
345 return fallback(name, size, id)
349 cid_reg, cid_order, cid_name, cid_supp = 'Adobe', 'Japan1', 'Adobe-Japan1'