2 -- luatexja/ltj-rmlgbm.lua
4 luatexja.load_module('base'); local ltjb = luatexja.base
6 local cidfont_data = {}
10 local cid_reg, cid_order, cid_supp, cid_name
12 ["Adobe-Japan1"] = {"UniJIS2004-UTF32", 23057, 6,
14 if (231<=i and i<=632) or (8718<=i and i<=8719)
15 or (12063<=i and i<=12087) then
16 return 327680 -- 655360/2
17 elseif 9758<=i and i<=9778 then
18 return 218453 -- 655360/3
19 elseif 9738<=i and i<=9757 then
20 return 163840 -- 655360/4
23 -- 基本的には JIS X 0213:2004 に沿ったマッピング
24 ["Adobe-Korea1"] = {"UniKS-UTF32", 18351, 2,
26 if 8094<=i and i<=8100 then
27 return 327680 -- 655360/2
30 ["Adobe-GB1"] = {"UniGB-UTF32", 30283, 5,
32 if (814<=i and i<=939) or (i==7716)
33 or (22355<=i and i<=22357) then
34 return 327680 -- 655360/2
37 ["Adobe-CNS1"] = {"UniCNS-UTF32", 19155, 6,
39 if (13648<=i and i<=13742) or (i==17603) then
40 return 327680 -- 655360/2
48 local line, fh -- line, file handler
49 local tt,cidm -- characters, cid->(Unicode)
51 local function load_cid_char(cid_dec, mke)
55 if string.find(line, "end...?char") then
56 line = fh:read("*l"); return
57 else -- WMA l is in the form "<%x+>%s%d+"
58 ucs, cid = string.match(line, "<(%x+)>%s+<?(%x+)>?")
59 cid = cid_dec(cid); ucs = tonumber(ucs, 16);
61 tt[ucs] = mke(cid); cidm[cid]=ucs
68 local function load_cid_range(inc, cid_dec, mke)
72 if string.find(line, "end...?range") then
73 line = fh:read("*l"); return
74 else -- WMA l is in the form "<%x+>%s+<%x+>"
75 bucs, eucs, cid = string.match(line, "<(%x+)>%s+<(%x+)>%s+<?(%x+)>?")
77 bucs = tonumber(bucs, 16); eucs = tonumber(eucs, 16)
78 for ucs = bucs, eucs do
80 tt[ucs] = mke(cid); cidm[cid]=ucs
89 local function open_cmap_file(name, inc, cid_dec, mke)
90 fh = io.open(kpse.find_file(name, 'cmap files'), "r")
93 if string.find(line, "%x+%s+begin...?char") then
94 load_cid_char(cid_dec, mke)
95 elseif string.find(line, "%x+%s+begin...?range") then
96 load_cid_range(inc, cid_dec, mke)
104 local function increment(a) return a+1 end
105 local function entry(a)
108 make_cid_font = function ()
109 local kx = cid_replace[cid_name]
110 if not kx then return end
112 cidinfo = { ordering=cid_order, registry=cid_reg, supplement=kx[3] },
113 encodingbytes = 2, extend=1000, format = 'opentype',
114 direction = 0, characters = {}, parameters = {}, embedding = "no", cache = "yes",
115 ascender = 0, descender = 0, factor = 0, hfactor = 0, vfactor = 0,
118 cidfont_data[cid_name] = k
120 -- CID => Unicode 符号空間
121 -- TODO: vertical fonts?
123 for i = 0,kx[2] do cidm[i] = -1 end
124 open_cmap_file(kx[1] .. "-H", increment, tonumber, entry)
127 -- Unicode にマップされなかった文字の処理
128 -- これらは TrueType フォントを使って表示するときはおかしくなる
129 local ttu, pricode = {}, 0xF0000
130 for i,v in ipairs(cidm) do
132 tt[pricode], cidm[i], pricode
133 = { index = i }, pricode, pricode+1;
135 ttu[cid_order .. '.' .. i] = cidm[i]
140 cidinfo= k.cidinfo, verbose = false,
141 shared = { featuredata = {}, },
142 luatex = { features = {},
146 dynamics = {}, features = {}, processes = {},
148 k.resources = { unicodes = ttu, }
150 cache_chars[cid_name] = { [655360] = k.characters }
153 local cidp = {nil, nil}; local cidmo = cidm
154 tt, ttu, cidm = {}, {}, {}
155 open_cmap_file(cid_name .. "-UCS2",
157 a[2] = a[2] +1 ; return a
160 cidp[1] = string.upper(string.sub(a,1,string.len(a)-4))
161 cidp[2] = tonumber(string.sub(a,-4),16)
164 function(a) return a[1] ..string.format('%04X',a[2]) end)
165 -- tt は cid -> tounicode になっているので cidm -> tounicode に変換
167 for i,v in ipairs(cidmo) do
168 k.characters[v].width = kxf(i)
170 k.characters[v].tounicode = tt[i]
175 k.characters[46].width = math.floor(655360/14);
176 ltjb.save_cache( "ltj-cid-auto-" .. string.lower(cid_name),
185 local function cid_cache_outdated(t) return t.version~=cache_ver end
186 local function read_cid_font()
187 local dat = ltjb.load_cache("ltj-cid-auto-" .. string.lower(cid_name),
190 cidfont_data[cid_name] = dat[1]
191 cache_chars[cid_name] = { [655360] = cidfont_data[cid_name].characters }
193 -- Now we must create the virtual metrics from CMap.
196 if cidfont_data[cid_name] then
197 for i,v in pairs(cidfont_data[cid_name].characters) do
198 if not v.width then v.width = 655360 end
199 v.height, v.depth = 576716.8, 78643.2 -- optimized for jfm-ujis.lua
206 local definers = fonts.definers
207 local function mk_rml(name, size, id)
208 local specification = definers.analyze(name,size)
209 specification = definers.resolve(specification)
210 specification.detail = specification.detail or ''
214 local s = cidfont_data[cid_name]
215 for k, v in pairs(s) do
219 fontdata.characters = nil
220 cachedata.characters = nil
221 fontdata.shared = nil
222 cachedata.shared = nil
224 cachedata.shared = {}
225 local shared = cachedata.shared
226 for k, v in pairs(s.shared) do
231 -- characters & scaling
232 if size < 0 then size = -size * 655.36 end
233 local scale = size / 655360
236 local def_height = 0.88 * size
237 -- character's default height (optimized for jfm-ujis.lua)
238 local def_depth = 0.12 * size -- and depth.
239 if not cache_chars[cid_name][size] then
240 cache_chars[cid_name][size] = {}
241 for k, v in pairs(cache_chars[cid_name][655360]) do
242 cache_chars[cid_name][size][k] = {
243 index = v.index, width = v.width * scale,
244 height = def_height, depth = def_depth, tounicode = v.tounicode,
248 fontdata.characters = cache_chars[cid_name][size]
249 cachedata.characters = cache_chars[cid_name][size]
254 local parameters = {}
255 for k, v in pairs(s.parameters) do
256 parameters[k] = v * scale
258 fontdata.parameters = parameters
259 fontdata.ascender = fontdata.ascender * scale
260 fontdata.descender = fontdata.descender * scale
261 fontdata.factor = fontdata.factor * scale
262 fontdata.hfactor = fontdata.hfactor * scale
263 fontdata.vfactor = fontdata.vfactor * scale
265 fontdata.resources = s.resources
266 cachedata.parameters = parameters
267 cachedata.ascender = fontdata.ascender
268 cachedata.descender = fontdata.descender
269 cachedata.factor = fontdata.factor
270 cachedata.hfactor = fontdata.hfactor
271 cachedata.vfactor = fontdata.vfactor
272 cachedata.size = size
273 cachedata.resources = s.resources
278 local s = string.match(specification.detail, 'slant=([+-]*%d*%.?%d)')
281 var, fontdata.slant = var .. 's' .. tostring(s), s
283 local e = string.match(specification.detail, 'extend=([+-]*%d*%.?%d)')
286 var, fontdata.extend = var .. 'x' .. tostring(e), e
288 fontdata.name = specification.name .. size .. var; cachedata.name = fontdata.name
289 fontdata.fullname = specification.name .. var; cachedata.fullname = fontdata.fullname
290 fontdata.psname = specification.name; cachedata.psname = fontdata.psname
291 fonts.hashes.identifiers[id] = cachedata
296 local function font_callback(name, size, id, fallback)
297 local p = utf.find(name, ":") or utf.len(name)+1
298 if utf.sub(name, 1, p-1) == 'psft' then
299 local s = "Adobe-Japan1-6"
300 local basename = utf.sub(name,p+1)
301 local p = utf.find(basename, ":")
302 local q = utf.find(basename, "/[BI][BI]?")
303 if q and p and q<=p then
304 basename = utf.gsub(basename, '/[BI][BI]?', '', 1)
305 p = utf.find(basename, ":")
308 local xname = utf.sub(basename, p+1)
311 local q = utf.find(xname, ";", p+1) or utf.len(xname)+1
312 if utf.sub(xname, p, p+3)=='cid=' and q>p+4 then
313 s = utf.sub(xname, p+4, q-1)
315 if utf.len(xname)+1==q then p = nil else p = q + 1 end
318 cid_reg, cid_order = string.match(s, "^(.-)%-(.-)%-(%d-)$")
320 cid_reg, cid_order = string.match(s, "^(.-)%-(.-)$")
322 cid_name = cid_reg .. '-' .. cid_order
323 if not cidfont_data[cid_name] then
325 if not cidfont_data[cid_name] then
326 ltjb.package_error('luatexja',
327 "bad cid key `" .. s .. "'",
328 "I couldn't find any non-embedded font information for the CID\n" ..
329 '`' .. s .. "'. For now, I'll use `Adobe-Japan1-6'.\n"..
330 'Please contact the LuaTeX-ja project team.')
331 cid_name = "Adobe-Japan1"
334 return mk_rml(basename, size, id)
336 return fallback(name, size, id)
340 cid_reg, cid_order, cid_name, cid_supp = 'Adobe', 'Japan1', 'Adobe-Japan1'
345 cidfont_data = cidfont_data,
346 font_callback = font_callback,