%#! lualatex
\documentclass{article}

\usepackage{luaotfload}
\usepackage{luacode}
\begin{luacode}
-- only for debug
function table_search(tbl, name, prefix)
  for i,x in pairs(tbl) do
    if string.match(i, name) then
      print('found: entry' .. prefix .. '.' .. i)
    elseif type(x) == 'string' and string.match(x, name)  then
      print('found: entry' .. prefix .. '.' .. i .. ' = ' .. x)
    elseif type(x) == 'table' then
      table_search(x, name, prefix .. '.' .. i)
    end
  end
end

function mkrmlgbm(nonfilename,opfilename)
	local fontdata = containers.read(fonts.otf.cache, string.lower(nonfilename))
	
	-- from fonts.otf.otf_to_tfm()
	local tfmdata
	fontdata.shared = fontdata.shared or {
	   featuredata = { },
	   anchorhash  = { },
	   initialized = false,
	}
	tfmdata = fonts.otf.copy_to_tfm(fontdata)
	tfmdata.unique = tfmdata.unique or { }
	tfmdata.shared = tfmdata.shared or { } -- combine
	local shared = tfmdata.shared
	shared.otfdata = fontdata
	shared.features = features -- default
	shared.dynamics = { }
	shared.processes = { }
	tfmdata.luatex = fontdata.luatex
	tfmdata.indices = fontdata.luatex.indices
	tfmdata.unicodes = fontdata.luatex.unicodes
	tfmdata.marks = fontdata.luatex.marks
	tfmdata.originals = fontdata.luatex.originals
	tfmdata.changed = { }
	tfmdata.has_italic = fontdata.metadata.has_italic
	if not tfmdata.language then tfmdata.language = 'dflt' end
	if not tfmdata.script   then tfmdata.script   = 'dflt' end
	_, shared.features = fonts.otf.set_features(tfmdata,fonts.define.check(features,fonts.otf.features.default))
	
	-- from fonts.otf.read_from_open_type()
	tfmdata = fonts.tfm.scale(tfmdata, 655360)
	
	-- delete extra data
	fontdata.subfonts = nil
	fontdata.metadata = {}
	fontdata.pfminfo = {}
	fontdata.luatex.filename = 'dummy.otf'
	fontdata.luatex.foundfilename = nil
	fontdata.size = nil
	fontdata.time = nil
	fontdata.glyphs = nil
	fontdata.luatex.indices = nil
	fontdata.luatex.unicodes = nil
	fontdata.luatex.originals = nil
	fontdata.luatex.tounicode = nil
	tfmdata.indices = nil
	tfmdata.descriptions = {}
	
	tfmdata.psname = ''
	tfmdata.filename = ''
	tfmdata.fontname = ''
	tfmdata.fullname = ''
	tfmdata.name = ''
	tfmdata.embedding = 'no'
	tfmdata.cache = 'yes'
	
	for k, v in pairs(tfmdata.characters) do
	   v.height = nil
	   v.depth = nil
       if v.width == 655360 then v.width = nil     -- 全角
       elseif v.width == 327680 then v.width = 327680 -- 半角
       elseif v.width == 163840 then v.width = 163840 -- 1/4 角
       elseif v.width == 218234.88 then v.width = 218234.88 -- 1/3 角
       else v.width = nil
       end
	end
	
    if fontdata then -- remove gpos data
      local tmpt = fontdata.shared.featuredata
      tmpt.gpos_single  = {}
      tmpt.gpos_pair  = {}
      tmpt.gpos_reversecontextchain  = {}
      tmpt.gpos_contextchain  = {}
      tmpt.gpos_cursive  = {}
      tmpt.gpos_mark2base  = {}
      tmpt.gpos_mark2ligature = {}
      tmpt.gpos_mark2mark = {}
      shared.otfdata.luatex.features.gpos = nil
      shared.otfdata.luatex.features.pwid = nil
      shared.otfdata.luatex.features.palt = nil
      shared.otfdata.luatex.features.vkrn = nil
      for i,x in pairs(tfmdata.shared.otfdata.luatex.sequences) do
        if string.match(x.type,'gpos') then x.subtables = {} end
      end
    end

	-- for luaotfload
    if fontdata.pfminfo then
	  fontdata.pfminfo.os2_capheight = 0
	end
    --table_search(tfmdata, 'gpos', '')
	table.tofile(opfilename, tfmdata, 'return', false, true, false)
end
mkrmlgbm('AdobeMingStd-Light', 'ltj-cid-adobe-cns1.lua')
mkrmlgbm('AdobeSongStd-Light', 'ltj-cid-adobe-gb1.lua')
mkrmlgbm('KozMinPr6N-Regular', 'ltj-cid-adobe-japan1.lua')
mkrmlgbm('AdobeMyungjoStd-Medium', 'ltj-cid-adobe-korea1.lua')
\end{luacode}

\begin{document}
\end{document}
