OSDN Git Service

wip
[luatex-ja/luatexja.git] / src / ltj-lotf_aux.lua
1 --
2 -- ltj-lotf_aux.lua
3 --
4
5 -- functions which access to fonts.* will be gathered in this file.
6 local aux = {}
7 luatexja.lotf_aux = aux
8
9 local getfont
10 do
11   local font_getfont = font.getfont
12   getfont = function (id) return (type(id)=="table") and id or font_getfont(id) end
13 end
14   -- accept font number or table
15 local provides_feature = luaotfload.aux.provides_feature
16 function aux.exist_feature(id, name)
17   local t = getfont(id)
18   if t and t.properties then
19     return provides_feature(id, t.properties.script, t.properties.language, name)
20   else return false
21   end
22 end 
23
24 function aux.enable_feature(id, name)
25   local t = getfont(id)
26   if t and t.shared and t.shared.features then
27     t.shared.features[name] = true
28   end
29 end
30 function aux.specified_feature(id, name)
31   local t = getfont(id)
32   return (t and t.shared and t.shared.features and t.shared.features[name])
33 end
34
35 local function get_ascender(id) -- scaled points
36   local t = getfont(id)
37   return (t and t.parameters and t.parameters.ascender) or 0
38 end
39 local function get_descender(id) -- scaled points
40   local t = getfont(id)
41   return (t and t.parameters and t.parameters.descender) or 0
42 end
43 aux.get_ascender, aux.get_descender = get_ascender, get_descender
44
45 function aux.get_vheight(id, c) -- scaled points
46   local t = getfont(id)
47   if t and t.descriptions and t.descriptions[c] and t.descriptions[c].vheight then
48     return t.descriptions[c].vheight / t.units * t.size
49   elseif t and t.shared and t.shared.rawdata and t.shared.rawdata.metadata then
50     return t.shared.rawdata.metadata.defaultvheight / t.units * t.size
51   else
52     return get_ascender(id) + get_descender(id)
53   end
54 end
55
56 local function loop_over_feat(id, feature_name, func)
57 -- feature_name: like { vert=true, vrt2 = true, ...}
58 -- func: return non-nil iff abort this fn
59   local t = getfont(id)
60   if t and t.resources and t.resources.sequences then
61     for _,i in pairs(t.resources.sequences) do
62       if i.order[1] and feature_name[i.order[1]] then
63         local f = i.features and i.features[i.order[1]]
64         if i.type == 'gsub_single' and i.steps 
65           and f and f[t.properties.script] and f[t.properties.script][t.properties.language] then
66           for _,j in pairs(i.steps) do
67             if type(j)=='table' then 
68               if type(j.coverage)=='table' then
69                 for i,k in pairs(j.coverage) do
70                   local s = func(i,k); if s then return s end
71                 end
72               end
73             end
74           end
75         end
76       end
77     end
78   end
79 end
80 aux.loop_over_feat = loop_over_feat
81
82 local vert_vrt2 = { vert=true, vrt2=true }
83 function aux.replace_vert_variant(id, c)
84   return loop_over_feat(id, vert_vrt2, 
85            function (i,k) if i==c then return k end end)
86          or c
87 end
88
89
90
91 local search
92 search = function (t, key, prefix)
93   if type(t)=="table" then
94     for i,v in pairs(t) do 
95       if i==key then print(prefix..'.'..i, v) 
96       else  search(v,key,prefix..'.'..tostring(i)) end
97     end
98   end
99 end
100 aux.t_search = search
101
102 -- EOF