OSDN Git Service

ltjs*.cls: added option "xreal" (patch NTSS for "better" optical scaling)
[luatex-ja/luatexja.git] / src / ltj-base.lua
index 0275521..e66a196 100644 (file)
@@ -8,12 +8,13 @@ local node, table, tex, token = node, table, tex, token
 local cat_lp = luatexbase.catcodetables['latex-package']
 
 -------------------- 
-luatexja.base = {}
+local ltjb = {}
+luatexja.base = ltjb
 
 local public_name = 'luatexja'
 local public_version = 'alpha'
-luatexja.base.public_name = public_name
-luatexja.base.public_version = public_version
+ltjb.public_name = public_name
+ltjb.public_version = public_version
 
 
 -------------------- Fully-expandable error messaging
@@ -150,7 +151,7 @@ local function mprint(...)
    end
    return tex.print(unpack(lines))
 end
-luatexja.base.mprint = mprint
+ltjb.mprint = mprint
 
 -------------------- Handling of TeX values
 do
@@ -226,9 +227,9 @@ do
       s.shrink or 0, s.shrink_order or 0))
   end
 
-  luatexja.base.to_dimen = to_dimen
-  luatexja.base.dump_skip = dump_skip
-  luatexja.base.to_skip = to_skip
+  ltjb.to_dimen = to_dimen
+  ltjb.dump_skip = dump_skip
+  ltjb.to_skip = to_skip
 end
 
 -------------------- Virtual table for LaTeX counters
@@ -245,217 +246,11 @@ do
   function mt_counter.__newindex(tbl, key, val)
     tex.count['c@'..key] = val
   end
-  luatexja.base.counter = counter
+  ltjb.counter = counter
 
 --! ixbase.length は tex.skip と全く同じなので不要.
 end
 
--------------------- Number handling in TeX source
-do
-
-  local tok_escape = token.create("ltj@@q@escape")
-  local tok_num = token.create("ltj@@q@escapenum")
-  local c_id_assign_int = token.command_id("assign_int")
-  local c_id_char_given = token.command_id("char_given")
-
-  local function error_scan()
-    package_error("luatexja",
-      "Missing number of a permitted form, treated as zero",
-      "A number should have been here; I inserted '0'.")
-  end
-
-  local function get_expd_next()
-    local next = token.get_next()
-    while token.is_expandable(next) do
-      token.expand(next)
-      next = token.get_next()
-    end
-    return next
-  end
-
-  local function grab_decimal(next, res)
-    table.insert(res, next)
-    while true do
-      next = get_expd_next()
-      if not (next[1] == 12 and 0x30 <= next[2] and next[2] <= 0x39) then
-        break
-      end
-      table.insert(res, next)
-    end
-    if next[1] == 10 then next = nil end
-    return true, next
-  end
-
-  local function grab_hexa(next, res)
-    local ok = false
-    table.insert(res, next)
-    while true do
-      next = get_expd_next()
-      if not ((next[1] == 12 and (0x30 <= next[2] and next[2] <= 0x39)) or
-              ((next[1] == 12 or next[1] == 11) and
-               (0x41 <= next[2] and next[2] <= 0x46))) then
-        break
-      end
-      ok = true
-      table.insert(res, next)
-    end
-    if next[1] == 10 then next = nil end
-    return ok, next
-  end
-
-  local function grab_octal(next, res)
-    local ok = false
-    table.insert(res, next)
-    while true do
-      next = get_expd_next()
-      if not (next[1] == 12 and (0x30 <= next[2] and next[2] <= 0x37)) then
-        break
-      end
-      ok = true
-      table.insert(res, next)
-    end
-    if next[1] == 10 then next = nil end
-    return ok, next
-  end
-
-  local function grab_charnum(next, res)
-    table.insert(res, next)
-    next = token.get_next()
-    table.insert(res, next)
-    next = get_expd_next()
-    if next[1] == 10 then next = nil end
-    return true, next
-  end
-
-  local function scan_with(delay, scanner)
-    local function proc()
-      if delay ~= 0 then
-        if delay > 0 then delay = delay - 1 end
-        return token.get_next()
-      else
-        local cont, back = scanner()
-        if not cont then
-          ltb.remove_from_callback("token_filter", "ltj@grab@num")
-        end
-        return back
-      end
-    end
-    ltb.add_to_callback("token_filter", proc, "ltj@grab@num", 1)
-  end
-
-  local function scan_brace()
-    scan_with(1, function()
-      local next = token.get_next()
-      if next[1] == 1 then
-        return false, { tok_escape, next }
-      elseif next[1] == 10 then
-        return true, { next }
-      else
-        return false, { next }
-      end
-    end)
-  end
-
-  local function scan_number()
-    scan_with(1, function()
-      local next = get_expd_next()
-      local res, ok = { tok_num }, false
-      while true do
-        if next[1] == 12 and (next[2] == 0x2B or next[2] == 0x2D) then
-          table.insert(res, next)
-        elseif next[1] ~= 10 then
-          break
-        end
-        next = get_expd_next()
-      end
-      if next[1] == 12 and 0x30 <= next[2] and next[2] <= 0x39 then
-        ok, next = grab_decimal(next, res)
-      elseif next[1] == 12 and next[2] == 0x22 then
-        ok, next = grab_hexa(next, res)
-      elseif next[1] == 12 and next[2] == 0x27 then
-        ok, next = grab_octal(next, res)
-      elseif next[1] == 12 and next[2] == 0x60 then
-        ok, next = grab_charnum(next, res)
-      elseif next[1] == c_id_assign_int or next[1] == c_id_char_given then
-        table.insert(res, next)
-        ok, next = true, nil
-      end
-      if ok then
-         table.insert(res, tok_num)
-      else
-         error_scan()
-         res = { tok_escape }
-      end
-       if next then table.insert(res, next) end
-       return false, res
-    end)
-  end
-
-  luatexja.base.scan_brace = scan_brace
-  luatexja.base.scan_number = scan_number
-end
-
--------------------- TeX register allocation
--- not used in current LuaTeX-ja
-
-do
-  local cmod_base_count = token.create('ltj@@count@zero')[2]
-  local cmod_base_attr = token.create('ltj@@attr@zero')[2]
-  local cmod_base_dimen = token.create('ltj@@dimen@zero')[2]
-  local cmod_base_skip = token.create('ltj@@skip@zero')[2]
-
-  local function const_number(name)
-    if name:sub(1, 1) == '\\' then name = name:sub(2) end
-    return token.create(name)[2]
-  end
-
-  local function count_number(name)
-    if name:sub(1, 1) == '\\' then name = name:sub(2) end
-    return token.create(name)[2] - cmod_base_count
-  end
-
-  local function attribute_number(name)
-    if name:sub(1, 1) == '\\' then name = name:sub(2) end
-    return token.create(name)[2] - cmod_base_attr
-  end
-
-  local function dimen_number(name)
-    if name:sub(1, 1) == '\\' then name = name:sub(2) end
-    return token.create(name)[2] - cmod_base_dimen
-  end
-
-  local function skip_number(name)
-    if name:sub(1, 1) == '\\' then name = name:sub(2) end
-    return token.create(name)[2] - cmod_base_skip
-  end
-
-  luatexja.base.const_number = const_number
-  luatexja.base.count_number = count_number
-  luatexja.base.attribute_number = attribute_number
-  luatexja.base.dimen_number = dimen_number
-  luatexja.base.skip_number = skip_number
-end
-
--------------------- mock of debug logger
-if not debug or debug == _G.debug then
-  local function no_op() end
-  debug = no_op
-  package_debug = no_op
-  show_term = no_op
-  show_log = no_op
-  function debug_logger()
-    return no_op
-  end
-end
-
--------------------- getting next token
-local cstemp = nil
-local function get_cs(s)
-   cstemp = token.csname_name(token.get_next())
-   tex.sprint(cat_lp,'\\' .. s)
-end
-luatexja.base.get_cs = get_cs
-
 -------------------- common error message
 do
    local function in_unicode(c, admit_math)
@@ -471,7 +266,7 @@ do
       end
       return c
    end
-   luatexja.base.in_unicode = in_unicode
+   ltjb.in_unicode = in_unicode
 end
 
 -------------------- cache management
@@ -569,36 +364,80 @@ do
       end
    end
 
-   luatexja.base.load_cache = load_cache
-   luatexja.base.save_cache_luc = save_cache_luc
-   luatexja.base.save_cache = save_cache
+   ltjb.load_cache = load_cache
+   ltjb.save_cache_luc = save_cache_luc
+   ltjb.save_cache = save_cache
+end
+
+----
+do
+   local tex_set_attr, tex_get_attr = tex.setattribute, tex.getattribute
+   function ltjb.ensure_tex_attr(a, v)
+      if tex_get_attr(a)~=v then
+        tex_set_attr(a, v)
+      end
+   end
+end
+----
+
+ltjb._error_set_break = _error_set_break
+ltjb._error_set_message = _error_set_message
+ltjb._error_show = _error_show
+ltjb._generic_warn_info = _generic_warn_info
+
+ltjb.package_error = package_error
+ltjb.package_warning = package_warning
+ltjb.package_warning_no_line = package_warning_no_line
+ltjb.package_info = package_info
+ltjb.package_info_no_line = package_info_no_line
+
+ltjb.generic_error = generic_error
+ltjb.generic_warning = generic_warning
+ltjb.generic_warning_no_line = generic_warning_no_line
+ltjb.generic_info = generic_info
+ltjb.generic_info_no_line = generic_info_no_line
+
+ltjb.ltj_warning_no_line = ltj_warning_no_line
+ltjb.ltj_error = ltj_error
+
+---- deterministic version of luatexbase.add_to_callback
+function ltjb.add_to_callback(name,fun,description,priority)
+    local priority= priority
+    if priority==nil then
+       priority=#luatexbase.callback_descriptions(name)+1
+    end
+    if(luatexbase.callbacktypes[name] == 3 and
+    priority == 1 and
+    #luatexbase.callback_descriptions(name)==1) then
+       luatexbase.module_warning("luatexbase",
+       "resetting exclusive callback: " .. name)
+       luatexbase.reset_callback(name)
+    end
+    local saved_callback={},ff,dd
+    for k,v in ipairs(luatexbase.callback_descriptions(name)) do
+       if k >= priority then
+           ff,dd= luatexbase.remove_from_callback(name, v)
+           saved_callback[#saved_callback+1]={ff,dd}
+       end
+    end
+    luatexbase.base_add_to_callback(name,fun,description)
+    for _,v in ipairs(saved_callback) do
+       luatexbase.base_add_to_callback(name,v[1],v[2])
+    end
+    return
 end
 
-luatexja.base._error_set_break = _error_set_break
-luatexja.base._error_set_message = _error_set_message
-luatexja.base._error_show = _error_show
-luatexja.base._generic_warn_info = _generic_warn_info
-
-luatexja.base.package_error = package_error
-luatexja.base.package_warning = package_warning
-luatexja.base.package_warning_no_line = package_warning_no_line
-luatexja.base.package_info = package_info
-luatexja.base.package_info_no_line = package_info_no_line
-
-luatexja.base.generic_error = generic_error
-luatexja.base.generic_warning = generic_warning
-luatexja.base.generic_warning_no_line = generic_warning_no_line
-luatexja.base.generic_info = generic_info
-luatexja.base.generic_info_no_line = generic_info_no_line
-
-luatexja.base.ltj_warning_no_line = ltj_warning_no_line
-luatexja.base.ltj_error = ltj_error
-
-luatexja.base.debug = debug
-luatexja.base.package_debug = package_debug
-luatexja.base.debug_logger = debug_logger
-luatexja.base.show_term = show_term
-luatexja.base.show_log = show_log
+-------------------- mock of debug logger
+if not ltjb.out_debug then
+   local function no_op() end
+   ltjb.start_time_measure = no_op
+   ltjb.stop_time_measure = no_op
+   ltjb.out_debug = no_op
+   ltjb.package_debug = no_op
+   ltjb.debug_logger = function() return no_op end
+   ltjb.show_term = no_op
+   ltjb.show_log = no_op
+end
 
 -------------------- all done
 -- EOF