2 -- luatexja/ltj-rmlgbm.lua
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", 23057, 6,
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", 19155, 6,
40 if (13648<=i and i<=13742) or (i==17603) then
41 return 327680 -- 655360/2
49 local line, fh -- line, file handler
50 local tt,cidm -- characters, cid->(Unicode)
52 local function load_cid_char(cid_dec, mke)
56 if string.find(line, "end...?char") then
57 line = fh:read("*l"); return
58 else -- WMA l is in the form "<%x+>%s%d+"
59 ucs, cid = string.match(line, "<(%x+)>%s+<?(%x+)>?")
60 cid = cid_dec(cid); ucs = tonumber(ucs, 16);
62 tt[ucs] = mke(cid); cidm[cid]=ucs
69 local function load_cid_range(inc, cid_dec, mke)
73 if string.find(line, "end...?range") then
74 line = fh:read("*l"); return
75 else -- WMA l is in the form "<%x+>%s+<%x+>"
76 bucs, eucs, cid = string.match(line, "<(%x+)>%s+<(%x+)>%s+<?(%x+)>?")
78 bucs = tonumber(bucs, 16); eucs = tonumber(eucs, 16)
79 for ucs = bucs, eucs do
81 tt[ucs] = mke(cid); cidm[cid]=ucs
90 local function open_cmap_file(name, inc, cid_dec, mke)
91 fh = io.open(kpse.find_file(name, 'cmap files'), "r")
94 if string.find(line, "%x+%s+begin...?char") then
95 load_cid_char(cid_dec, mke)
96 elseif string.find(line, "%x+%s+begin...?range") then
97 load_cid_range(inc, cid_dec, mke)
105 local function increment(a) return a+1 end
106 local function entry(a)
109 make_cid_font = function ()
110 local kx = cid_replace[cid_name]
111 if not kx then return end
113 cidinfo = { ordering=cid_order, registry=cid_reg, supplement=kx[3] },
114 encodingbytes = 2, extend=1000, format = 'opentype',
115 direction = 0, characters = {}, parameters = {
116 ascender = 655360*0.88,
117 descender = 655360*0.12,
119 embedding = "no", cache = "yes", factor = 0, hfactor = 0, vfactor = 0,
121 properties = { language = "dflt", script = "dflt" },
123 cidfont_data[cid_name] = k
125 -- CID => Unicode 符号空間
126 local tth, cidmo = {}, {}
127 tt, cidm = tth, cidmo
128 for i = 0,kx[2] do cidm[i] = -1 end
129 open_cmap_file(kx[1] .. "-H", increment, tonumber, entry)
132 -- Unicode にマップされなかった文字の処理
133 -- これらは TrueType フォントを使って表示するときはおかしくなる
134 local ttu, pricode = {}, 0xF0000
135 for i,v in ipairs(cidmo) do
137 tth[pricode], cidmo[i], pricode
138 = { index = i }, pricode, pricode+1;
140 ttu[cid_order .. '.' .. i] = cidmo[i]
146 cidinfo= k.cidinfo, verbose = false,
147 shared = { featuredata = {}, },
149 dynamics = {}, features = {}, processes = {},
150 --rawdata = { descriptions = {} },
152 k.resources = { unicodes = ttu, }
154 cache_chars[cid_name] = { [655360] = k.characters }
158 local ttv = {}; k.shared.ltj_vert_table = ttv
159 for i = 0,kx[2] do cidm[i] = -1 end
160 open_cmap_file(kx[1] .. "-V", increment, tonumber, entry)
161 for i,v in pairs(tt) do
162 ttv[i] = cidmo[v.index]
166 local cidp = {nil, nil}; tt, ttu, cidm = {}, {}, {}
167 open_cmap_file(cid_name .. "-UCS2",
169 a[2] = a[2] +1 ; return a
172 cidp[1] = string.upper(string.sub(a,1,string.len(a)-4))
173 cidp[2] = tonumber(string.sub(a,-4),16)
176 function(a) return a[1] ..string.format('%04X',a[2]) end)
177 -- tt は cid -> tounicode になっているので cidm -> tounicode に変換
179 for i,v in ipairs(cidmo) do
180 k.characters[v].width = kxf(i)
182 k.characters[v].tounicode = tt[i]
187 k.characters[46].width = math.floor(655360/14);
188 ltjb.save_cache( "ltj-cid-auto-" .. string.lower(cid_name),
197 local cidf_vert_processor
199 local traverse_id = node.traverse_id
200 local id_glyph = node.id('glyph')
201 cidf_vert_processor = {
202 function (head, fnum)
203 local fontdata = identifiers[fnum]
204 if luatexja.jfont.font_metric_table[fnum].vert_activated then
205 local vt = fontdata.shared.ltj_vert_table
206 for n in traverse_id(id_glyph, head) do
208 n.char = vt[n.char] or n.char
217 local function cid_cache_outdated(t) return t.version~=cache_ver end
218 local function read_cid_font()
219 local dat = ltjb.load_cache("ltj-cid-auto-" .. string.lower(cid_name),
222 cidfont_data[cid_name] = dat[1]
223 cache_chars[cid_name] = { [655360] = cidfont_data[cid_name].characters }
225 -- Now we must create the virtual metrics from CMap.
228 if cidfont_data[cid_name] then
229 cidfont_data[cid_name].shared.processes = cidf_vert_processor
230 for i,v in pairs(cidfont_data[cid_name].characters) do
231 if not v.width then v.width = 655360 end
232 v.height, v.depth = 576716.8, 78643.2 -- optimized for jfm-ujis.lua
234 return cidfont_data[cid_name]
241 local function prepare_cid_font(reg, ord)
242 cid_reg, cid_order, cid_name, cid_supp = reg, ord, reg .. '-' .. ord
243 return cidfont_data[cid_name] or read_cid_font()
247 local definers = fonts.definers
248 local function mk_rml(name, size, id)
249 local specification = definers.analyze(name,size)
250 --specification = definers.resolve(specification) (not needed)
251 specification.detail = specification.detail or ''
255 local s = cidfont_data[cid_name]
256 luatexja.rmlgbm.vert_addfunc(id)
257 for k, v in pairs(s) do
261 fontdata.characters = nil
262 cachedata.characters = nil
263 fontdata.shared = nil
264 cachedata.shared = nil
266 cachedata.shared = {}
267 local shared = cachedata.shared
268 for k, v in pairs(s.shared) do
273 -- characters & scaling
274 if size < 0 then size = -size * 655.36 end
275 local scale = size / 655360
278 local def_height = 0.88 * size
279 -- character's default height (optimized for jfm-ujis.lua)
280 local def_depth = 0.12 * size -- and depth.
281 if not cache_chars[cid_name][size] then
282 cache_chars[cid_name][size] = {}
283 for k, v in pairs(cache_chars[cid_name][655360]) do
284 cache_chars[cid_name][size][k] = {
285 index = v.index, width = v.width * scale,
286 height = def_height, depth = def_depth, tounicode = v.tounicode,
290 fontdata.characters = cache_chars[cid_name][size]
291 cachedata.characters = cache_chars[cid_name][size]
296 local parameters = {}
297 for k, v in pairs(s.parameters) do
298 parameters[k] = v * scale
300 fontdata.parameters = parameters
302 fontdata.resources = s.resources
303 cachedata.parameters = parameters
304 cachedata.size = size
305 cachedata.resources = s.resources
310 local s = string.match(specification.detail, 'slant=([+-]*%d*%.?%d)')
313 var, fontdata.slant = var .. 's' .. tostring(s), s
315 local e = string.match(specification.detail, 'extend=([+-]*%d*%.?%d)')
318 var, fontdata.extend = var .. 'x' .. tostring(e), e
320 fontdata.name = specification.name .. size .. var; cachedata.name = fontdata.name
321 fontdata.fullname = specification.name .. var; cachedata.fullname = fontdata.fullname
322 fontdata.psname = specification.name; cachedata.psname = fontdata.psname
323 identifiers[id] = cachedata
328 local function font_callback(name, size, id, fallback)
329 if name:sub(1,1)=="{" and name:sub(-1)=="}" then name = name:sub(2,-2) end
330 local p = name:find(":") or 0
331 if name:sub(1, p-1) == 'psft' then
332 local s = "Adobe-Japan1-6"
333 local basename = name:sub(p+1)
334 local p = basename:find(":")
335 local q = basename:find("/[BI][BI]?")
336 if q and p and q<=p then
337 basename = basename:gsub('/[BI][BI]?', '', 1)
338 p = basename:find(":")
341 local xname = basename:sub(p+1)
344 local q = xname:find(";", p+1) or xname:len()+1
345 if xname:sub(p, p+3)=='cid=' and q>p+4 then
346 s = xname:sub(p+4, q-1)
348 if xname:len()+1==q then p = nil else p = q + 1 end
351 cid_reg, cid_order = string.match(s, "^(.-)%-(.-)%-(%d-)$")
353 cid_reg, cid_order = string.match(s, "^(.-)%-(.-)$")
355 if not prepare_cid_font(cid_reg, cid_order) then
356 ltjb.package_error('luatexja',
357 "bad cid key `" .. s .. "'",
358 "I couldn't find any non-embedded font information for the CID\n" ..
359 '`' .. s .. "'. For now, I'll use `Adobe-Japan1-6'.\n"..
360 'Please contact the LuaTeX-ja project team.')
361 cid_name = "Adobe-Japan1"
363 return mk_rml(basename, size, id)
365 local tfmdata=fallback(name, size, id)
366 if type (tfmdata) == "table" and tfmdata.shared then
367 luatexbase.call_callback("luaotfload.patch_font", tfmdata, name)
374 prepare_cid_font = prepare_cid_font,
375 cidfont_data = cidfont_data,
376 font_callback = font_callback,
377 vert_addfunc = function () end, -- dummy, set in ltj-direction.lua
380 prepare_cid_font('Adobe', 'Japan1')