--- /dev/null
+--
+-- luatexja/ltj-pretreat.lua
+--
+
+luatexja.load_module('base'); local ltjb = luatexja.base
+luatexja.load_module('charrange'); local ltjc = luatexja.charrange
+luatexja.load_module('stack'); local ltjs = luatexja.stack
+luatexja.load_module('jfont'); local ltjf = luatexja.jfont
+luatexja.load_module('direction'); local ltjd = luatexja.direction
+
+local Dnode = node.direct or node
+
+local nullfunc = function(n) return n end
+local to_node = (Dnode ~= node) and Dnode.tonode or nullfunc
+local to_direct = (Dnode ~= node) and Dnode.todirect or nullfunc
+
+local setfield = (Dnode ~= node) and Dnode.setfield or function(n, i, c) n[i] = c end
+local getid = (Dnode ~= node) and Dnode.getid or function(n) return n.id end
+local getfont = (Dnode ~= node) and Dnode.getfont or function(n) return n.font end
+local getchar = (Dnode ~= node) and Dnode.getchar or function(n) return n.char end
+local getfield = (Dnode ~= node) and Dnode.getfield or function(n, i) return n[i] end
+local getsubtype = (Dnode ~= node) and Dnode.getsubtype or function(n) return n.subtype end
+
+local pairs = pairs
+local floor = math.floor
+local has_attr = Dnode.has_attribute
+local set_attr = Dnode.set_attribute
+local node_traverse = Dnode.traverse
+local node_remove = Dnode.remove
+local node_next = (Dnode ~= node) and Dnode.getnext or node.next
+local node_free = Dnode.free
+local node_end_of_math = Dnode.end_of_math
+local tex_getcount = tex.getcount
+
+local id_glyph = node.id('glyph')
+local id_math = node.id('math')
+local id_whatsit = node.id('whatsit')
+local sid_user = node.subtype('user_defined')
+
+local attr_dir = luatexbase.attributes['ltj@dir']
+local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
+local attr_curtfnt = luatexbase.attributes['ltj@curtfnt']
+local attr_icflag = luatexbase.attributes['ltj@icflag']
+
+local is_ucs_in_japanese_char = ltjc.is_ucs_in_japanese_char_direct
+local ltjf_get_vert_glyph = ltjf.get_vert_glyph
+local ltjf_replace_altfont = ltjf.replace_altfont
+local attr_orig_char = luatexbase.attributes['ltj@origchar']
+local STCK = luatexja.userid_table.STCK
+local DIR = luatexja.userid_table.DIR
+local PROCESSED_BEGIN_FLAG = luatexja.icflag_table.PROCESSED_BEGIN_FLAG
+
+local dir_tate = luatexja.dir_table.dir_tate
+local lang_ja = token.create('ltj@@japanese')[2]
+------------------------------------------------------------------------
+-- MAIN PROCESS STEP 1: replace fonts
+------------------------------------------------------------------------
+local wt, wtd = {}, {}
+do
+ local ltjd_get_dir_count = ltjd.get_dir_count
+ local start_time_measure, stop_time_measure
+ = ltjb.start_time_measure, ltjb.stop_time_measure
+ local head
+ local is_dir_tate
+ local suppress_hyphenate_ja_aux = {}
+ suppress_hyphenate_ja_aux[id_glyph] = function(p)
+ if (has_attr(p, attr_icflag) or 0)<=0 and is_ucs_in_japanese_char(p) then
+ local pc = getchar(p)
+ local pf = ltjf_replace_altfont(has_attr(p, attr_curjfnt) or getfont(p), pc)
+ setfield(p, 'font', pf); set_attr(p, attr_curjfnt, pf)
+ setfield(p, 'lang', lang_ja)
+ set_attr(p, attr_orig_char, pc)
+ end
+ return p
+ end
+ suppress_hyphenate_ja_aux[id_math] = function(p)
+ return node_end_of_math(node_next(p)) end
+ suppress_hyphenate_ja_aux[50] = function(p) return p end
+ suppress_hyphenate_ja_aux[id_whatsit] = function(p)
+ if getsubtype(p)==sid_user then
+ local uid = getfield(p, 'user_id')
+ if uid==STCK then
+ wt[#wt+1] = p; node_remove(head, p)
+ elseif uid==DIR then
+ if has_attr(p, attr_icflag)<PROCESSED_BEGIN_FLAG then
+ ltjs.list_dir = has_attr(p, attr_dir)
+ else
+ wtd[#wtd+1] = p; node_remove(head, p)
+ end
+ end
+ end
+ return p
+ end
+
+ local function suppress_hyphenate_ja (h,t)
+ start_time_measure('ltj_hyphenate')
+ head = to_direct(h)
+ local p = head
+ for i = 1,#wt do wt[i]=nil end
+ for i = 1,#wtd do wtd[i]=nil end
+ ltjs.list_dir=ltjd_get_dir_count()
+ while p and p~=t do
+ local pfunc = suppress_hyphenate_ja_aux[getid(p)]
+ p = node_next(pfunc and pfunc(p) or p)
+ end
+ stop_time_measure('ltj_hyphenate'); start_time_measure('tex_hyphenate')
+ lang.hyphenate(h, t)
+ stop_time_measure('tex_hyphenate')
+ return h
+ end
+
+ luatexbase.add_to_callback('hyphenate',
+ function (head,tail)
+ return suppress_hyphenate_ja(head)
+ end,'ltj.hyphenate')
+end
+
+-- mode: true iff this function is called from hpack_filter
+local ltjs_report_stack_level = ltjs.report_stack_level
+local function set_box_stack_level(head, mode)
+ local box_set, cl = 0, tex.currentgrouplevel + 1
+ for _,p in pairs(wt) do
+ if mode and getfield(p, 'value')==cl then box_set = 1 end; node_free(p)
+ end
+ ltjs_report_stack_level(tex_getcount('ltj@@stack') + box_set)
+ for _,p in pairs(wtd) do
+ node_free(p)
+ end
+ is_dir_tate = ltjs.list_dir == dir_tate
+ if is_dir_tate then
+ for p in Dnode.traverse_id(id_glyph,to_direct(head)) do
+ if (has_attr(p, attr_icflag) or 0)<=0 and has_attr(p, attr_curjfnt)==getfont(p) then
+ local pfn = has_attr(p, attr_curtfnt) or getfont(p)
+ local pc = getchar(p)
+ local pf = ltjf_replace_altfont(pfn, pc)
+ set_attr(p, attr_dir, pc)
+ pc = ltjf_get_vert_glyph(pf, pc) or pc
+ setfield(p, 'char', pc); set_attr(p, attr_orig_char, pc)
+ setfield(p, 'font', pf); set_attr(p, attr_curjfnt, pf)
+ end
+ end
+ end
+ return head
+end
+
+-- CALLBACKS
+luatexbase.add_to_callback('hpack_filter',
+ function (head)
+ return set_box_stack_level(head, true)
+ end,'ltj.hpack_filter_pre',1)
+luatexbase.add_to_callback('pre_linebreak_filter',
+ function (head)
+ return set_box_stack_level(head, false)
+ end,'ltj.pre_linebreak_filter_pre',1)
+
+luatexja.pretreat = {
+ set_box_stack_level = set_box_stack_level,
+}
local id_whatsit = node.id('whatsit')
local attr_icflag = luatexbase.attributes['ltj@icflag']
local attr_jchar_class = luatexbase.attributes['ltj@charclass']
-local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
+local lang_ja = token.create('ltj@@japanese')[2]
local ltjf_font_metric_table = ltjf.font_metric_table
local spec_zero_glue = ltjj.spec_zero_glue
-- and ((xi == id_penalty) or (xi == id_kern) or (xi == id_kern)) do
-- x = node_prev(x); xi = getid(x)
-- end
- if xi == id_glyph and has_attr(x, attr_curjfnt) == getfont(x) then
+ if xi == id_glyph and getfield(x, 'lang')==lang_ja then
-- 和文文字
xc = x
elseif xi == id_hlist and get_attr_icflag(x) == PACKED then
local id_pbox = 257 -- already processed nodes (by \unhbox)
local id_pbox_w = 258 -- cluster which consists of a whatsit
local sid_user = node.subtype('user_defined')
+local lang_ja = token.create('ltj@@japanese')[2]
local sid_start_link = node.subtype('pdf_start_link')
local sid_start_thread = node.subtype('pdf_start_thread')
local attr_ablshift
local set_np_xspc_jachar
-local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
local attr_dir = luatexbase.attributes['ltj@dir']
local attr_icflag = luatexbase.attributes['ltj@icflag']
-------------------- Helper functions
-local function copy_attr(new, old)
- -- 仕様が決まるまで off にしておく
-end
-
-- This function is called only for acquiring `special' characters.
local function fast_find_char_class(c,m)
return m.chars[c] or 0
first_char = p; find_first_char = false
end
last_char = p; found_visible_node = true
- elseif pid==id_rule and get_attr_icflag(p)==PACKED then
+ elseif pid==id_rule and get_attr_icflag(p)==PACKED then
-- do nothing
elseif not (pid==id_ins or pid==id_mark
or pid==id_adjust or pid==id_whatsit
local first_char = first_char
if first_char then
if getid(first_char)==id_glyph then
- if getfont(first_char) == (has_attr(first_char, attr_curjfnt) or -1) then
+ if getfield(first_char, 'lang') == lang_ja then
set_np_xspc_jachar(Nx, first_char)
else
set_np_xspc_alchar(Nx, getchar(first_char),first_char, 1)
local function calc_np_aux_glyph_common(lp)
Np.nuc = lp
Np.id = npi
- if (getfont(lp) == (has_attr(lp, attr_curjfnt) or -1)) then
+ if getfield(lp, 'lang') == lang_ja then
Np.id = id_jglyph
set_np_xspc_jachar(Np, lp)
local npi, npf
lp=lx; break
else
local lid = getid(lx)
- if lid==id_glyph and not(getfont(lx) == (has_attr(lx, attr_curjfnt) or -1)) then
+ if lid==id_glyph and getfield(lx, 'lang') ~= lang_ja then
-- 欧文文字
last_glyph = lx; set_attr(lx, attr_icflag, PROCESSED); Np.last = lx
y_adjust = has_attr(lx,attr_ablshift) or 0
node_depth = max(getfield(lx, 'depth') + min(y_adjust, 0), node_depth)
- adj_depth = (y_adjust>0) and max(getfield(lp, 'depth') + y_adjust, adj_depth) or adj_depth
+ adj_depth = (y_adjust>0) and max(getfield(lx, 'depth') + y_adjust, adj_depth) or adj_depth
setfield(lx, 'yoffset', getfield(lx, 'yoffset') - y_adjust)
elseif lid==id_kern then
local ls = getsubtype(lx)
local s = Nx.last_char
if s then
if getid(s)==id_glyph then
- if getfont(s) == (has_attr(s, attr_curjfnt) or -1) then
+ if getfield(s, 'lang') == lang_ja then
set_np_xspc_jachar(Nx, s)
else
set_np_xspc_alchar(Nx, getchar(s), s, 2)
-- MISC
------------------------------------------------------------------------
do
+ local get_dir_count = ltjd.get_dir_count
local is_ucs_in_japanese_char = ltjc.is_ucs_in_japanese_char_direct
local tex_set_attr = tex.setattribute
local font = font
setfield(g, 'subtype', 1)
set_attr(g, attr_icflag, ITALIC)
if is_ucs_in_japanese_char(p) then
- f = has_attr(p, attr_curjfnt)
+ f = has_attr(p, (get_dir_count()==dir_tate) and attr_curtfnt or attr_curjfnt)
local j = font_metric_table[f]
setfield(g, 'kern', j.char_type[find_char_class(getchar(p), j)].italic)
else
luatexja.load_module('jfont'); local ltjf = luatexja.jfont
luatexja.load_module('rmlgbm'); local ltjr = luatexja.rmlgbm
luatexja.load_module('charrange'); local ltjc = luatexja.charrange
+luatexja.load_module('direction'); local ltjd = luatexja.direction
+luatexja.load_module('stack'); local ltjs = luatexja.stack
local id_glyph = node.id('glyph')
local id_whatsit = node.id('whatsit')
local identifiers = fonts.hashes.identifiers
local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
+local attr_curtfnt = luatexbase.attributes['ltj@curtfnt']
local attr_yablshift = luatexbase.attributes['ltj@yablshift']
local attr_ykblshift = luatexbase.attributes['ltj@ykblshift']
+local attr_tablshift = luatexbase.attributes['ltj@tablshift']
+local attr_tkblshift = luatexbase.attributes['ltj@tkblshift']
+local lang_ja = token.create('ltj@@japanese')[2]
local ltjf_font_metric_table = ltjf.font_metric_table
local ltjf_find_char_class = ltjf.find_char_class
local ltjr_cidfont_data = ltjr.cidfont_data
local ltjc_is_ucs_in_japanese_char = ltjc.is_ucs_in_japanese_char
+local ltjd_get_dir_count = ltjd.get_dir_count
luatexja.userid_table.OTF = luatexbase.newuserwhatsitid('char_by_cid', 'luatexja')
luatexja.userid_table.VSR = luatexbase.newuserwhatsitid('replace_vs', 'luatexja')
-- This whatsit node will be extracted to a glyph_node
local function append_jglyph(char)
local p = node_new(id_whatsit,sid_user)
- local v = tex.attribute[attr_curjfnt]
setfield(p, 'user_id', OTF)
setfield(p, 'type', 100)
setfield(p, 'value', char)
- set_attr(p, attr_yablshift, tex.attribute[attr_ykblshift])
node_write(p)
end
-local function cid(key)
- if key==0 then return append_jglyph(char) end
- local curjfnt = identifiers[tex.attribute[attr_curjfnt]]
- if not curjfnt.cidinfo or
- curjfnt.cidinfo.ordering ~= "Japan1" and
- curjfnt.cidinfo.ordering ~= "GB1" and
- curjfnt.cidinfo.ordering ~= "CNS1" and
- curjfnt.cidinfo.ordering ~= "Korea1" then
--- ltjb.package_warning('luatexja-otf',
--- 'Current Japanese font (or other CJK font) "'
--- ..curjfnt.psname..'" is not a CID-Keyed font (Adobe-Japan1 etc.)')
- return append_jglyph(get_ucs_from_rmlgbm(key))
- end
- local char = curjfnt.resources.unicodes[curjfnt.cidinfo.ordering..'.'..tostring(key)]
- if not char then
- ltjb.package_warning('luatexja-otf',
- 'Current Japanese font (or other CJK font) "'
- ..curjfnt.psname..'" does not have the specified CID character ('
- ..tostring(key)..')',
- 'Use a font including the specified CID character.')
- char = 0
+local cid
+do
+ local dir_tate = luatexja.dir_table.dir_tate
+ local tex_get_attr = tex.getattribute
+ cid = function (key)
+ if key==0 then return append_jglyph(char) end
+ local curjfnt = identifiers[tex_get_attr((ltjd_get_dir_count()==dir_tate)
+ and attr_curtfnt or attr_curjfnt)]
+ if not curjfnt.cidinfo or
+ curjfnt.cidinfo.ordering ~= "Japan1" and
+ curjfnt.cidinfo.ordering ~= "GB1" and
+ curjfnt.cidinfo.ordering ~= "CNS1" and
+ curjfnt.cidinfo.ordering ~= "Korea1" then
+ -- ltjb.package_warning('luatexja-otf',
+ -- 'Current Japanese font (or other CJK font) "'
+ -- ..curjfnt.psname..'" is not a CID-Keyed font (Adobe-Japan1 etc.)')
+ return append_jglyph(get_ucs_from_rmlgbm(key))
+ end
+ local char = curjfnt.resources.unicodes[curjfnt.cidinfo.ordering..'.'..tostring(key)]
+ if not char then
+ ltjb.package_warning('luatexja-otf',
+ 'Current Japanese font (or other CJK font) "'
+ ..curjfnt.psname..'" does not have the specified CID character ('
+ ..tostring(key)..')',
+ 'Use a font including the specified CID character.')
+ char = 0
+ end
+ return append_jglyph(char)
end
- return append_jglyph(char)
end
local function extract(head)
head = to_direct(head)
local p = head
local v
+ local is_dir_tate = ltjs.list_dir == dir_tate
+ local attr_ablshift = is_dir_tate and attr_tablshift or attr_yablshift
+ local attr_kblshift = is_dir_tate and attr_tkblshift or attr_ykblshift
+ local attr_curfnt = is_dir_tate and attr_curtfnt or attr_curjfnt
while p do
if getid(p)==id_whatsit then
if getsubtype(p)==sid_user then
local g = node_new(id_glyph)
setfield(g, 'subtype', 0)
setfield(g, 'char', getfield(p, 'value'))
- v = has_attr(p, attr_curjfnt); setfield(g, 'font',v)
- set_attr(g, attr_curjfnt, puid==OTF and v or -1)
- -- VSR yields ALchar
- v = has_attr(p, attr_yablshift)
- if v then
- set_attr(g, attr_yablshift, v)
+ v = has_attr(p, attr_curfnt); setfield(g, 'font',v)
+ if puid==OTF then
+ setfield(g, 'lang', lang_ja)
+ set_attr(g, attr_kblshift, has_attr(p, attr_kblshift))
else
- unset_attr(g, attr_yablshift)
+ set_attr(g, attr_ablshift, has_attr(p, attr_ablshift))
end
head = node_insert_after(head, p, g)
head = node_remove(head, p)
setfield(p, 'user_id', uid)
setfield(p, 'type', 100)
setfield(p, 'value', char)
- set_attr(p, attr_curjfnt, pf)
- set_attr(p, attr_yablshift, has_attr(bp, attr_ykblshift) or 0)
return p
end
node_free(q)
if pt then
local np = ivs_jglyph(pt, p, pf,
- (has_attr(p,attr_curjfnt) or 0)==pf and OTF or VSR)
+ (getfield(p, 'lang') or 0)==lang_ja and OTF or VSR)
head = node_insert_after(head, p, np)
head = node_remove(head,p)
node_free(p)
local PROCESSED_BEGIN_FLAG = luatexja.icflag_table.PROCESSED_BEGIN_FLAG
local dir_tate = luatexja.dir_table.dir_tate
-local lang_ja = token.create('ltj@japanese')[2]
+local lang_ja = token.create('ltj@@japanese')[2]
------------------------------------------------------------------------
-- MAIN PROCESS STEP 1: replace fonts
------------------------------------------------------------------------
local wt, wtd = {}, {}
do
local ltjd_get_dir_count = ltjd.get_dir_count
- local start_time_measure, stop_time_measure
+ local start_time_measure, stop_time_measure
= ltjb.start_time_measure, ltjb.stop_time_measure
local head
local is_dir_tate
if (has_attr(p, attr_icflag) or 0)<=0 and is_ucs_in_japanese_char(p) then
local pc = getchar(p)
local pf = ltjf_replace_altfont(has_attr(p, attr_curjfnt) or getfont(p), pc)
- setfield(p, 'font', pf); set_attr(p, attr_curjfnt, pf)
+ setfield(p, 'font', pf); --set_attr(p, attr_curjfnt, pf)
setfield(p, 'lang', lang_ja)
- --setfield(p, 'subtype', floor(getsubtype(p)*0.5)*2)
set_attr(p, attr_orig_char, pc)
end
return p
return node_end_of_math(node_next(p)) end
suppress_hyphenate_ja_aux[50] = function(p) return p end
suppress_hyphenate_ja_aux[id_whatsit] = function(p)
- if getsubtype(p)==sid_user then
+ if getsubtype(p)==sid_user then
local uid = getfield(p, 'user_id')
if uid==STCK then
wt[#wt+1] = p; node_remove(head, p)
is_dir_tate = ltjs.list_dir == dir_tate
if is_dir_tate then
for p in Dnode.traverse_id(id_glyph,to_direct(head)) do
- if (has_attr(p, attr_icflag) or 0)<=0 and has_attr(p, attr_curjfnt)==getfont(p) then
+ if (has_attr(p, attr_icflag) or 0)<=0 and getfield(p, 'lang')==lang_ja then
local pfn = has_attr(p, attr_curtfnt) or getfont(p)
local pc = getchar(p)
local pf = ltjf_replace_altfont(pfn, pc)
set_attr(p, attr_dir, pc)
pc = ltjf_get_vert_glyph(pf, pc) or pc
setfield(p, 'char', pc); set_attr(p, attr_orig_char, pc)
- setfield(p, 'font', pf); set_attr(p, attr_curjfnt, pf)
+ setfield(p, 'font', pf); --set_attr(p, attr_curjfnt, pf)
end
end
end