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 = {
115 ascender = 655360*0.88,
116 descender = 655360*0.12,
118 embedding = "no", cache = "yes", factor = 0, hfactor = 0, vfactor = 0,
121 cidfont_data[cid_name] = k
123 -- CID => Unicode 符号空間
124 local tth, cidmo = {}, {}
125 tt, cidm = tth, cidmo
126 for i = 0,kx[2] do cidm[i] = -1 end
127 open_cmap_file(kx[1] .. "-H", increment, tonumber, entry)
130 -- Unicode にマップされなかった文字の処理
131 -- これらは TrueType フォントを使って表示するときはおかしくなる
132 local ttu, pricode = {}, 0xF0000
133 for i,v in ipairs(cidmo) do
135 tth[pricode], cidmo[i], pricode
136 = { index = i }, pricode, pricode+1;
138 ttu[cid_order .. '.' .. i] = cidmo[i]
143 for i = 0,kx[2] do cidm[i] = -1 end
144 open_cmap_file(kx[1] .. "-V", increment, tonumber, entry)
146 for i,v in pairs(tt) do ttv[i] = cidmo[v.index] end
151 cidinfo= k.cidinfo, verbose = false,
152 shared = { featuredata = {}, },
153 luatex = { features = {},
157 dynamics = {}, features = {}, processes = {},
160 k.resources = { unicodes = ttu, }
162 cache_chars[cid_name] = { [655360] = k.characters }
165 local cidp = {nil, nil}; tt, ttu, cidm = {}, {}, {}
166 open_cmap_file(cid_name .. "-UCS2",
168 a[2] = a[2] +1 ; return a
171 cidp[1] = string.upper(string.sub(a,1,string.len(a)-4))
172 cidp[2] = tonumber(string.sub(a,-4),16)
175 function(a) return a[1] ..string.format('%04X',a[2]) end)
176 -- tt は cid -> tounicode になっているので cidm -> tounicode に変換
178 for i,v in ipairs(cidmo) do
179 k.characters[v].width = kxf(i)
181 k.characters[v].tounicode = tt[i]
186 k.characters[46].width = math.floor(655360/14);
187 ltjb.save_cache( "ltj-cid-auto-" .. string.lower(cid_name),
196 local function cid_cache_outdated(t) return t.version~=cache_ver end
197 local function read_cid_font()
198 local dat = ltjb.load_cache("ltj-cid-auto-" .. string.lower(cid_name),
201 cidfont_data[cid_name] = dat[1]
202 cache_chars[cid_name] = { [655360] = cidfont_data[cid_name].characters }
204 -- Now we must create the virtual metrics from CMap.
207 if cidfont_data[cid_name] then
208 for i,v in pairs(cidfont_data[cid_name].characters) do
209 if not v.width then v.width = 655360 end
210 v.height, v.depth = 576716.8, 78643.2 -- optimized for jfm-ujis.lua
217 local definers = fonts.definers
218 local function mk_rml(name, size, id)
219 local specification = definers.analyze(name,size)
220 --specification = definers.resolve(specification) (not needed)
221 specification.detail = specification.detail or ''
225 local s = cidfont_data[cid_name]
226 luatexja.rmlgbm.vert_addfunc(id, s.shared.ltj_vert_table)
227 for k, v in pairs(s) do
231 fontdata.characters = nil
232 cachedata.characters = nil
233 fontdata.shared = nil
234 cachedata.shared = nil
236 cachedata.shared = {}
237 local shared = cachedata.shared
238 for k, v in pairs(s.shared) do
243 -- characters & scaling
244 if size < 0 then size = -size * 655.36 end
245 local scale = size / 655360
248 local def_height = 0.88 * size
249 -- character's default height (optimized for jfm-ujis.lua)
250 local def_depth = 0.12 * size -- and depth.
251 if not cache_chars[cid_name][size] then
252 cache_chars[cid_name][size] = {}
253 for k, v in pairs(cache_chars[cid_name][655360]) do
254 cache_chars[cid_name][size][k] = {
255 index = v.index, width = v.width * scale,
256 height = def_height, depth = def_depth, tounicode = v.tounicode,
260 fontdata.characters = cache_chars[cid_name][size]
261 cachedata.characters = cache_chars[cid_name][size]
266 local parameters = {}
267 for k, v in pairs(s.parameters) do
268 parameters[k] = v * scale
270 fontdata.parameters = parameters
272 fontdata.resources = s.resources
273 cachedata.parameters = parameters
274 cachedata.size = size
275 cachedata.resources = s.resources
280 local s = string.match(specification.detail, 'slant=([+-]*%d*%.?%d)')
283 var, fontdata.slant = var .. 's' .. tostring(s), s
285 local e = string.match(specification.detail, 'extend=([+-]*%d*%.?%d)')
288 var, fontdata.extend = var .. 'x' .. tostring(e), e
290 fontdata.name = specification.name .. size .. var; cachedata.name = fontdata.name
291 fontdata.fullname = specification.name .. var; cachedata.fullname = fontdata.fullname
292 fontdata.psname = specification.name; cachedata.psname = fontdata.psname
293 fonts.hashes.identifiers[id] = cachedata
298 local function font_callback(name, size, id, fallback)
299 local p = name:find(":") or name:len()+1
300 if name:sub(1, p-1) == 'psft' then
301 local s = "Adobe-Japan1-6"
302 local basename = name:sub(p+1)
303 local p = basename:find(":")
304 local q = basename:find("/[BI][BI]?")
305 if q and p and q<=p then
306 basename = basename:gsub('/[BI][BI]?', '', 1)
307 p = basename:find(":")
310 local xname = basename:sub(p+1)
313 local q = xname:find(";", p+1) or xname:len()+1
314 if xname:sub(p, p+3)=='cid=' and q>p+4 then
315 s = xname:sub(p+4, q-1)
317 if xname:len()+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 fallback(name, size, id)
343 cidfont_data = cidfont_data,
344 font_callback = font_callback,
345 vert_addfunc = function () end, -- dummy, set in ltj-direction.lua
348 cid_reg, cid_order, cid_name, cid_supp = 'Adobe', 'Japan1', 'Adobe-Japan1'