4 luatexja.load_module('base'); local ltjb = luatexja.base
6 local cidfont_data = {}
9 local identifiers = fonts.hashes.identifiers
11 local cid_reg, cid_order, cid_supp, cid_name
13 ["Adobe-Japan1"] = {"UniJIS2004-UTF32", 23059, 7,
15 if (231<=i and i<=632) or (8718<=i and i<=8719)
16 or (12063<=i and i<=12087) then
17 return 327680 -- 655360/2
18 elseif 9758<=i and i<=9778 then
19 return 218453 -- 655360/3
20 elseif 9738<=i and i<=9757 then
21 return 163840 -- 655360/4
24 -- 基本的には JIS X 0213:2004 に沿ったマッピング
25 ["Adobe-Korea1"] = {"UniKS-UTF32", 18351, 2,
27 if 8094<=i and i<=8100 then
28 return 327680 -- 655360/2
31 ["Adobe-GB1"] = {"UniGB-UTF32", 30283, 5,
33 if (814<=i and i<=939) or (i==7716)
34 or (22355<=i and i<=22357) then
35 return 327680 -- 655360/2
38 ["Adobe-CNS1"] = {"UniCNS-UTF32", 19178, 7,
40 if (13648<=i and i<=13742) or (i==17603) then
41 return 327680 -- 655360/2
44 ["Adobe-KR"] = {"UniAKR-UTF32", 22896, 9,
50 elseif i==12235 or i==12236 then
51 return 163840 -- 655360/4
59 local line, fh -- line, file handler
60 local tt,cidm -- characters, cid->(Unicode)
62 local function load_cid_char(cid_dec, mke)
66 if string.find(line, "end...?char") then
67 line = fh:read("*l"); return
68 else -- WMA l is in the form "<%x+>%s%d+"
69 ucs, cid = string.match(line, "<(%x+)>%s+<?(%x+)>?")
70 cid = cid_dec(cid); ucs = tonumber(ucs, 16);
72 tt[ucs] = mke(cid); cidm[cid]=ucs
79 local function load_cid_range(inc, cid_dec, mke)
83 if string.find(line, "end...?range") then
84 line = fh:read("*l"); return
85 else -- WMA l is in the form "<%x+>%s+<%x+>"
86 bucs, eucs, cid = string.match(line, "<(%x+)>%s+<(%x+)>%s+<?(%x+)>?")
88 bucs = tonumber(bucs, 16); eucs = tonumber(eucs, 16)
89 for ucs = bucs, eucs do
91 tt[ucs] = mke(cid); cidm[cid]=ucs
100 local function open_cmap_file(name, inc, cid_dec, mke)
101 local fn = kpse.find_file(name, 'cmap files')
103 fh = io.open(fn, "r")
106 if string.find(line, "%x+%s+begin...?char") then
107 load_cid_char(cid_dec, mke)
108 elseif string.find(line, "%x+%s+begin...?range") then
109 load_cid_range(inc, cid_dec, mke)
118 local function increment(a) return a+1 end
119 local function entry(a)
122 local feat_dummy_vert = { gsub={vert={dflt={dflt=true}}} }
123 local seq_dummy_vert={{
124 features={vert={dflt={dflt=true}}},
125 --flags={false,false,false,false},
126 --index=1, name="s_s_0", skiphash=false, steps={coverage={},index=1},
127 ["type"]="gsub_single", order='vert',
129 make_cid_font = function ()
130 local kx = cid_replace[cid_name]
131 if not kx then return end
133 cidinfo = { ordering=cid_order, registry=cid_reg, supplement=kx[3] },
134 encodingbytes = 2, extend=1000, format = 'opentype',
135 direction = 0, characters = {}, parameters = {
136 ascender = 655360*0.88,
137 descender = 655360*0.12,
139 embedding = "no", cache = "yes", factor = 0, hfactor = 0, vfactor = 0,
141 properties = { language = "dflt", script = "dflt" },
143 cidfont_data[cid_name] = k
145 -- CID => Unicode 符号空間
146 local tth, cidmo = {}, {}
147 tt, cidm = tth, cidmo
148 for i = 0,kx[2] do cidm[i] = -1 end
149 open_cmap_file(kx[1] .. "-H", increment, tonumber, entry)
152 -- Unicode にマップされなかった文字の処理
153 -- これらは TrueType フォントを使って表示するときはおかしくなる
154 local ttu, pricode = {}, 0xF0000
155 for i,v in ipairs(cidmo) do
157 tth[pricode], cidmo[i], pricode
158 = { index = i }, pricode, pricode+1;
160 ttu[cid_order .. '.' .. i] = cidmo[i]
166 cidinfo= k.cidinfo, verbose = false,
167 shared = { featuredata = {}, },
169 dynamics = {}, processes = {},
170 rawdata = {}, features={},
174 features = feat_dummy_vert,
175 sequences = seq_dummy_vert,
178 k.shared.rawdata.resources=k.resources
179 k.shared.rawdata.descriptions=k.descriptions
180 cache_chars[cid_name] = { [655360] = k.characters }
184 local ttv = {}; k.ltj_vert_table = ttv
185 for i = 0,kx[2] do cidm[i] = -1 end
186 open_cmap_file(kx[1] .. "-V", increment, tonumber, entry)
187 for i,v in pairs(tt) do
188 ttv[i] = cidmo[v.index] -- "unicode" of vertical variant
192 local cidp = {nil, nil}; tt, ttu, cidm = {}, {}, {}
193 open_cmap_file(cid_name .. "-UCS2",
195 a[2] = a[2] +1 ; return a
198 cidp[1] = string.upper(string.sub(a,1,string.len(a)-4))
199 cidp[2] = tonumber(string.sub(a,-4),16)
202 function(a) return a[1] ..string.format('%04X',a[2]) end)
203 -- tt は cid -> tounicode になっているので cidm -> tounicode に変換
205 for i,v in ipairs(cidmo) do
206 k.characters[v].width = kxf(i)
208 k.characters[v].tounicode = tt[i]
213 if k and k.characters and k.characters[46] then
214 k.characters[46].width = math.floor(655360/14);
216 ltjb.save_cache( "ltj-cid-auto-" .. string.lower(cid_name),
225 local cidf_vert_processor
227 local traverse_id, is_node = node.direct.traverse_id, node.is_node
228 local to_direct = node.direct.todirect
229 local id_glyph = node.id('glyph')
230 local getfont = node.direct.getfont
231 local getchar = node.direct.getchar
232 local setchar = node.direct.setchar
233 local font_getfont = font.getfont
234 cidf_vert_processor = {
235 function (head, fnum)
236 local fontdata = font_getfont(fnum)
237 if head and luatexja.jfont.font_metric_table[fnum].vert_activated then
238 local vt = fontdata.ltj_vert_table
239 local nh = is_node(head) and to_direct(head) or head
240 for n in traverse_id(id_glyph, head) do
241 if getfont(n)==fnum then
242 local c = getchar(n); setchar(n, vt[c] or c)
251 local function cid_cache_outdated(t) return t.version~=cache_ver end
252 local function read_cid_font()
253 local dat = ltjb.load_cache("ltj-cid-auto-" .. string.lower(cid_name),
256 cidfont_data[cid_name] = dat[1]
257 cache_chars[cid_name] = { [655360] = cidfont_data[cid_name].characters }
259 -- Now we must create the virtual metrics from CMap.
262 if cidfont_data[cid_name] then
263 cidfont_data[cid_name].shared.processes = cidf_vert_processor
264 for i,v in pairs(cidfont_data[cid_name].characters) do
265 if not v.width then v.width = 655360 end
266 v.height, v.depth = 576716.8, 78643.2 -- optimized for jfm-ujis.lua
268 return cidfont_data[cid_name]
275 local function prepare_cid_font(reg, ord)
276 cid_reg, cid_order, cid_name, cid_supp = reg, ord, reg .. '-' .. ord
277 return cidfont_data[cid_name] or read_cid_font()
281 local definers = fonts.definers
282 local function mk_rml(name, size, id)
283 local specification = definers.analyze(name,size)
284 --specification = definers.resolve(specification) (not needed)
285 specification.detail = specification.detail or ''
287 local n = specification.name
288 if n:sub(1,1)=="{" then n=n:sub(2) end
289 if n:sub(-1)=="}" then n=n:sub(1,-2) end
294 local s = cidfont_data[cid_name]
295 luatexja.rmlgbm.vert_addfunc(id)
296 for k, v in pairs(s) do
300 fontdata.characters = nil
301 cachedata.characters = nil
302 fontdata.shared = nil
303 cachedata.shared = nil
305 cachedata.shared = {}
306 local shared = cachedata.shared
307 for k, v in pairs(s.shared) do
312 -- characters & scaling
313 if size < 0 then size = -size * 655.36 end
314 local scale = size / 655360
317 local def_height = 0.88 * size
318 -- character's default height (optimized for jfm-ujis.lua)
319 local def_depth = 0.12 * size -- and depth.
320 if not cache_chars[cid_name][size] then
321 cache_chars[cid_name][size] = {}
322 for k, v in pairs(cache_chars[cid_name][655360]) do
323 cache_chars[cid_name][size][k] = {
324 index = v.index, width = v.width * scale,
325 height = def_height, depth = def_depth, tounicode = v.tounicode,
329 fontdata.characters = cache_chars[cid_name][size]
330 cachedata.characters = cache_chars[cid_name][size]
335 local parameters = {}
336 for k, v in pairs(s.parameters) do
337 parameters[k] = v * scale
339 fontdata.parameters = parameters
341 fontdata.resources = s.resources
342 cachedata.parameters = parameters
343 cachedata.size = size
344 cachedata.resources = s.resources
349 local s = string.match(specification.detail, 'slant=([+-]*%d*%.?%d)')
352 var, fontdata.slant = var .. 's' .. tostring(s), s
354 local e = string.match(specification.detail, 'extend=([+-]*%d*%.?%d)')
357 var, fontdata.extend = var .. 'x' .. tostring(e), e
359 fontdata.name = specification.name .. size .. var; cachedata.name = fontdata.name
360 fontdata.fullname = specification.name .. var; cachedata.fullname = fontdata.fullname
361 fontdata.psname = specification.name; cachedata.psname = fontdata.psname
362 identifiers[id] = cachedata
367 local function font_callback(name, size, id, fallback)
368 if name:sub(1,1)=="{" and name:sub(-1)=="}" then name = name:sub(2,-2) end
369 local p = name:find(":") or 0
370 if name:sub(1, p-1) == 'psft' then
371 local s = "Adobe-Japan1-7"
372 local basename = name:sub(p+1)
373 local p = basename:find(":")
374 local q = basename:find("/[BI][BI]?")
375 if q and p and q<=p then
376 basename = basename:gsub('/[BI][BI]?', '', 1)
377 p = basename:find(":")
380 local xname = basename:sub(p+1)
383 local q = xname:find(";", p+1) or xname:len()+1
384 if xname:sub(p, p+3)=='cid=' and q>p+4 then
385 s = xname:sub(p+4, q-1)
387 if xname:len()+1==q then p = nil else p = q + 1 end
390 cid_reg, cid_order = string.match(s, "^(.-)%-(.-)%-(%d-)$")
392 cid_reg, cid_order = string.match(s, "^(.-)%-(.-)$")
394 if not prepare_cid_font(cid_reg, cid_order) then
395 ltjb.package_error('luatexja',
396 "bad cid key `" .. s .. "'",
397 "I couldn't find any non-embedded font information for the CID\n" ..
398 '`' .. s .. "'. For now, I'll use `Adobe-Japan1-6'.\n"..
399 'Please contact the LuaTeX-ja project team.')
400 cid_name = "Adobe-Japan1"
402 return mk_rml(basename, size, id)
404 local fontdata=fallback(name, size, id)
405 if type (fontdata) == "table" and fontdata.encodingbytes == 2 then
406 luatexbase.call_callback ("luaotfload.patch_font", fontdata, name, id)
408 luatexbase.call_callback ("luaotfload.patch_font_unsafe", fontdata, name, id)
415 prepare_cid_font = prepare_cid_font,
416 cidfont_data = cidfont_data,
417 font_callback = font_callback,
418 vert_addfunc = function () end, -- dummy, set in ltj-direction.lua
421 prepare_cid_font('Adobe', 'Japan1')