4 luatexbase.provides_module({
5 name = 'luatexja.rmlgbm',
8 description = 'Definitions of non-embedded Japanese (or other CJK) fonts',
10 module('luatexja.rmlgbm', package.seeall)
11 local err, warn, info, log = luatexbase.errwarinf(_NAME)
13 luatexja.load_module('base'); local ltjb = luatexja.base
15 local cidfont_data = {}
16 local cache_chars = {}
18 localdir = file.join(kpse.expand_var("$TEXMFVAR"), aux_dir),
19 systemdir = file.join(kpse.expand_var("$TEXMFSYSVAR"), aux_dir),
22 local cid_reg, cid_order, cid_supp, cid_name
23 local taux_dir = 'luatex-cache/luatexja'
25 ["Adobe-Japan1"] = "UniJIS2004-UTF32", -- JIS X 0213:2004
26 ["Adobe-Korea1"] = "UniKS-UTF32",
27 ["Adobe-GB1"] = "UniGB-UTF32",
28 ["Adobe-CNS1"] = "UniCNS-UTF32",
34 local function load_bf_char()
38 if line == "endcidchar" then
39 line = fh:read("*l"); return
40 else -- WMA l is in the form "<%x+>%s%d+"
41 ucs, cid = string.match(line, "<(%x+)>%s+(%d+)")
42 cid = tonumber(cid, 10); ucs = tonumber(ucs, 16);
44 tt[ucs] = { index = cid }
51 local function load_bf_range()
55 if line == "endcidrange" then
56 line = fh:read("*l"); return
57 else -- WMA l is in the form "<%x+>%s+<%x+>"
58 bucs, eucs, cid = string.match(line, "<(%x+)>%s+<(%x+)>%s+(%d+)")
59 cid = tonumber(cid, 10); bucs = tonumber(bucs, 16); eucs = tonumber(eucs, 16);
60 for ucs = bucs, eucs do
62 tt[ucs] = { index = cid }
71 local function make_cid_font()
72 cidfont_data[cid_name] = {
73 cidinfo = { ordering=cid_order, registry=cid_reg, supplement=cid_supp },
74 encodingbytes = 2, extend=1000, format = 'opentype',
75 direction = 0, characters = {}, parameters = {}, embedding = "no", cache = "yes",
76 ascender = 0, descender = 0, factor = 0, hfactor = 0, vfactor = 0,
81 -- TODO: vertical fonts?
82 fh = io.open(kpse.find_file(cid_replace[cid_name] .. "-H", 'cmap files'), "r")
85 if string.find(line, "%x+%s+begincidchar") then
87 elseif string.find(line, "%x+%s+begincidrange") then
93 fh:close(); cidfont_data[cid_name].characters = tt
94 cache_chars[cid_name] = { [655360] = cidfont_data[cid_name].characters }
97 local savepath = path.localdir .. '/luatexja/'
98 if not lfs.isdir(savepath) then
101 savepath = file.join(savepath, "ltj-cid-auto-"
102 .. string.lower(cid_name) .. ".lua")
103 if file.iswritable(savepath) then
104 table.tofile(savepath, cidfont_data[cid_name],'return', false, true, false )
106 ltjb.package_warning('luatexja',
107 'failed to save informations of non-embedded 2-byte fonts', '')
112 local function read_cid_font()
113 -- local v = "ltj-cid-" .. string.lower(cid_name) .. ".lua"
114 local v = "ltj-cid-auto-" .. string.lower(cid_name) .. ".lua"
115 local localpath = file.join(path.localdir, v)
116 local systempath = file.join(path.systemdir, v)
117 local kpsefound = kpse.find_file(v)
118 if kpsefound and file.isreadable(kpsefound) then
119 cidfont_data[cid_name] = require(kpsefound)
120 cache_chars[cid_name] = { [655360] = cidfont_data[cid_name].characters }
121 elseif file.isreadable(localpath) then
122 cidfont_data[cid_name] = require(localpath)
123 cache_chars[cid_name] = { [655360] = cidfont_data[cid_name].characters }
124 elseif file.isreadable(systempath) then
125 cidfont_data[cid_name] = require(systempath)
126 cache_chars[cid_name] = { [655360] = cidfont_data[cid_name].characters }
128 -- Now we must create the virtual metrics from CMap.
129 ltjb.package_info('luatexja',
130 'I try to generate informations of non-embedded 2-byte fonts...', '')
133 if cidfont_data[cid_name] then
134 for i,v in pairs(cidfont_data[cid_name].characters) do
135 if not v.width then v.width = 655360 end
136 v.height, v.depth = 576716.8, 78643.2 -- optimized for jfm-ujis.lua
142 local function mk_rml(name, size, id)
143 local specification = fonts.define.analyze(name,size)
144 specification = fonts.define.specify[':'](specification)
145 local features = specification.features.normal
149 local s = cidfont_data[cid_name]
150 for k, v in pairs(s) do
154 fontdata.characters = nil
155 cachedata.characters = nil
156 fontdata.unicodes = nil
157 fontdata.shared = nil
158 cachedata.shared = nil
160 cachedata.shared = {}
161 local shared = cachedata.shared
162 for k, v in pairs(s.shared) do
166 shared.set_dynamics = fonts.otf.set_dynamics
167 shared.processes, shared.features = fonts.otf.set_features(cachedata,fonts.define.check(features,fonts.otf.features.default))
170 -- characters & scaling
171 if size < 0 then size = -size * 655.36 end
172 local scale = size / 655360
173 local def_height = 0.88 * size -- character's default height (optimized for jfm-ujis.lua)
174 local def_depth = 0.12 * size -- and depth.
175 if not cache_chars[cid_name][size] then
176 cache_chars[cid_name][size] = {}
177 for k, v in pairs(cache_chars[cid_name][655360]) do
178 cache_chars[cid_name][size][k] = {
179 index = v.index, width = v.width * scale,
180 height = def_height, depth = def_depth, tounicode = v.tounicode,
184 fontdata.characters = cache_chars[cid_name][size]
185 cachedata.characters = cache_chars[cid_name][size]
187 local parameters = {}
188 for k, v in pairs(s.parameters) do
189 parameters[k] = v * scale
191 fontdata.parameters = parameters; cachedata.parameters = parameters
192 fontdata.ascender = fontdata.ascender * scale; cachedata.ascender = fontdata.ascender
193 fontdata.descender = fontdata.descender * scale; cachedata.descender = fontdata.descender
194 fontdata.factor = fontdata.factor * scale; cachedata.factor = fontdata.factor
195 fontdata.hfactor = fontdata.hfactor * scale; cachedata.hfactor = fontdata.hfactor
196 fontdata.vfactor = fontdata.vfactor * scale; cachedata.vfactor = fontdata.vfactor
197 fontdata.size = size; cachedata.size = size
201 if features.slant then
202 fontdata.slant = features.slant*1000; cachedata.slant = fontdata.slant
203 var = var .. 's' .. tostring(features.slant)
205 if features.extend then
206 fontdata.extend = features.extend*1000; cachedata.extend = fontdata.extend
207 var = var .. 'x' .. tostring(features.extend)
209 fontdata.name = specification.name .. size .. var; cachedata.name = fontdata.name
210 fontdata.fullname = specification.name .. var; cachedata.fullname = fontdata.fullname
211 fontdata.psname = specification.name; cachedata.psname = fontdata.psname
212 fonts.ids[id] = cachedata
217 local dr_orig = fonts.define.read
218 function fonts.define.read(name, size, id)
219 local p = utf.find(name, ":") or utf.len(name)+1
220 if utf.sub(name, 1, p-1) == 'psft' then
221 local s = "Adobe-Japan1-6"
222 local basename = utf.sub(name,p+1)
223 local p = utf.find(basename, ":")
225 local xname = utf.sub(basename, p+1)
228 local q = utf.find(xname, ";", p+1) or utf.len(xname)+1
229 if utf.sub(xname, p, p+3)=='cid=' and q>p+4 then
230 s = utf.sub(xname, p+4, q-1)
232 if utf.len(xname)+1==q then p = nil else p = q + 1 end
235 cid_reg, cid_order = string.match(s, "^(.-)%-(.-)%-(%d-)$")
237 cid_reg, cid_order = string.match(s, "^(.-)%-(.-)$")
239 cid_name = cid_reg .. '-' .. cid_order
240 if not cidfont_data[cid_name] then
242 if not cidfont_data[cid_name] then
243 ltjb.package_error('luatexja',
244 "bad cid key `" .. s .. "'",
245 "I couldn't find any non-embedded font information for the CID\n" ..
246 '`' .. s .. "'. For now, I'll use `Adobe-Japan1-6'.\n"..
247 'Please contact the LuaTeX-ja project team.')
248 cid_name = "Adobe-Japan1"
251 return mk_rml(basename, size, id)
253 return dr_orig(name, size, id)
257 cid_reg, cid_order, cid_name = 'Adobe', 'Japan1', 'Adobe-Japan1'