OSDN Git Service

Added luatexja.node_remove and luatexja.Dnode_remove ("safe node.remove")
[luatex-ja/luatexja.git] / src / ltj-rmlgbm.lua
index c62123c..b81c269 100644 (file)
@@ -1,27 +1,13 @@
 --
--- luatexja/rmlgbm.lua
+-- luatexja/ltj-rmlgbm.lua
 --
-luatexbase.provides_module({
-  name = 'luatexja.rmlgbm',
-  date = '2013/03/17',
-  version = '0.4',
-  description = 'Definitions of non-embedded Japanese (or other CJK) fonts',
-})
-module('luatexja.rmlgbm', package.seeall)
-local err, warn, info, log = luatexbase.errwarinf(_NAME)
-
 luatexja.load_module('base');      local ltjb = luatexja.base
 
-cidfont_data = {}
-local cidfont_data = cidfont_data
+local cidfont_data = {}
 local cache_chars = {}
-local path           = {
-    localdir  = file.join(kpse.expand_var("$TEXMFVAR"), aux_dir),
-    systemdir = file.join(kpse.expand_var("$TEXMFSYSVAR"), aux_dir),
-}
+local cache_ver = '2'
 
 local cid_reg, cid_order, cid_supp, cid_name
-local taux_dir = 'luatex-cache/luatexja'
 local cid_replace = {
    ["Adobe-Japan1"] = {"UniJIS2004-UTF32", 23057, 6,
                       function (i)
@@ -57,6 +43,7 @@ local cid_replace = {
 }
 
 -- reading CID maps
+local make_cid_font
 do
    local line, fh -- line, file handler
    local tt,cidm -- characters, cid->(Unicode)
@@ -118,8 +105,9 @@ do
    local function entry(a)     
       return {index = a} 
    end
-   function make_cid_font()
+   make_cid_font = function ()
       local kx = cid_replace[cid_name]
+      if not kx then return end
       local k = {
          cidinfo = { ordering=cid_order, registry=cid_reg, supplement=kx[3] },
          encodingbytes = 2, extend=1000, format = 'opentype',
@@ -184,46 +172,27 @@ do
       end
 
       -- Save
-      local savepath  = path.localdir .. '/luatexja/'
-      if not lfs.isdir(savepath) then
-         dir.mkdirs(savepath)
-      end
-      savepath = file.join(savepath, "ltj-cid-auto-" 
-                           .. string.lower(cid_name)  .. ".lua")
-      if file.iswritable(savepath) then
-         k.characters[46].width = math.floor(655360/14);
-        -- Standard fonts are ``seriffed''. 
-         table.tofile(savepath, k,'return', false, true, false )
-      else 
-         ltjb.package_warning('luatexja', 
-                              'failed to save informations of non-embedded 2-byte fonts', '')
-      end
+      k.characters[46].width = math.floor(655360/14);
+      ltjb.save_cache( "ltj-cid-auto-" .. string.lower(cid_name),
+                      {
+                         version = cache_ver,
+                         k,
+                      })
    end
 end
-local make_cid_font = make_cid_font
 
 -- 
+local function cid_cache_outdated(t) return t.version~=cache_ver end
 local function read_cid_font()
-   -- local v = "ltj-cid-" .. string.lower(cid_name) .. ".lua"
-   local v = "ltj-cid-auto-" .. string.lower(cid_name) .. ".lua"
-   local localpath  = file.join(path.localdir, v)
-   local systempath = file.join(path.systemdir, v)
-   local kpsefound  = kpse.find_file(v)
-   if kpsefound and file.isreadable(kpsefound) then
-      cidfont_data[cid_name] = require(kpsefound)
-      cache_chars[cid_name]  = { [655360] = cidfont_data[cid_name].characters }
-   elseif file.isreadable(localpath)  then
-      cidfont_data[cid_name] = require(localpath)
-      cache_chars[cid_name]  = { [655360] = cidfont_data[cid_name].characters }
-   elseif file.isreadable(systempath) then
-      cidfont_data[cid_name] = require(systempath)
+   local dat = ltjb.load_cache("ltj-cid-auto-" .. string.lower(cid_name),
+                              cid_cache_outdated )
+   if dat then 
+      cidfont_data[cid_name] = dat[1]
       cache_chars[cid_name]  = { [655360] = cidfont_data[cid_name].characters }
+   else
+      -- Now we must create the virtual metrics from CMap.
+      make_cid_font()
    end
-   -- Now we must create the virtual metrics from CMap.
-   ltjb.package_info('luatexja', 
-                       'I try to generate informations of non-embedded 2-byte fonts...', '')
-   make_cid_font()
-
    if cidfont_data[cid_name] then
       for i,v in pairs(cidfont_data[cid_name].characters) do
          if not v.width then v.width = 655360 end
@@ -306,12 +275,12 @@ local function mk_rml(name, size, id)
 
    -- no embedding
    local var = ''
-   local s = string.match(specification.detail, 'slant=([+-]*[0-9]*%.[0-9]*)')
-   if s and e~=1  then 
+   local s = string.match(specification.detail, 'slant=([+-]*%d*%.?%d)')
+   if s and e~=0  then 
       s = s * 1000
       var, fontdata.slant  = var .. 's' .. tostring(s), s
    end
-   local e = string.match(specification.detail, 'extend=([+-]*[0-9]*%.[0-9]*)')
+   local e = string.match(specification.detail, 'extend=([+-]*%d*%.?%d)')
    if e and e~=1  then 
       e = e * 1000
       var, fontdata.extend  = var .. 'x' .. tostring(e), e
@@ -319,12 +288,12 @@ local function mk_rml(name, size, id)
    fontdata.name = specification.name .. size .. var; cachedata.name = fontdata.name
    fontdata.fullname = specification.name .. var; cachedata.fullname = fontdata.fullname
    fontdata.psname = specification.name; cachedata.psname = fontdata.psname
-   fonts.ids[id] = cachedata
+   fonts.hashes.identifiers[id] = cachedata
 
    return fontdata
 end
 
-function font_callback(name, size, id, fallback)
+local function font_callback(name, size, id, fallback)
    local p = utf.find(name, ":") or utf.len(name)+1
    if utf.sub(name, 1, p-1) == 'psft' then
       local s = "Adobe-Japan1-6"
@@ -370,3 +339,9 @@ end
 
 cid_reg, cid_order, cid_name, cid_supp = 'Adobe', 'Japan1', 'Adobe-Japan1'
 read_cid_font()
+
+
+luatexja.rmlgbm = {
+   cidfont_data = cidfont_data,
+   font_callback = font_callback,
+}