OSDN Git Service

jpotf was broken in recent commits
[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 local font_metric_table = {}
9 aux.font_metric_table = font_metric_table
10
11 local getfont = font.getfont
12 local provides_feature = luaotfload.aux.provides_feature
13 function aux.exist_feature(id, name)
14   local t = getfont(id)
15   if t and t.properties then
16     return provides_feature(id, t.properties.script, t.properties.language, name)
17   else return false
18   end
19 end 
20
21 function aux.enable_feature(id, name)
22   local t = getfont(id)
23   if t and t.shared and t.shared.features then
24     t.shared.features[name] = true
25   end
26 end
27 function aux.specified_feature(id, name)
28   local t = getfont(id)
29   return (t and t.shared and t.shared.features and t.shared.features[name])
30 end
31
32 local function get_ascender(id) -- scaled points
33   if font_metric_table[id].ascender then return font_metric_table[id].ascender end
34   local t = getfont(id)
35   local a = t and t.parameters and t.parameters.ascender or 0
36   font_metric_table[id].ascender = a; return a
37 end
38 local function get_descender(id) -- scaled points
39   if font_metric_table[id].descender then return font_metric_table[id].descender end
40   local t = getfont(id)
41   local a = t and t.parameters and t.parameters.descender or 0
42   font_metric_table[id].descender = a; return a
43 end
44 aux.get_ascender, aux.get_descender = get_ascender, get_descender
45
46 local function get_vmet_table(tfmdata, dest)
47    if tfmata and tfmdata.shared then return dest end
48    local rawdata = tfmdata.shared.rawdata
49    local ascender = rawdata.metadata.ascender or 0
50    local default_vheight 
51      = rawdata.metadata.defaultvheight
52        or (rawdata.metadata.descender and (ascender+rawdata.metadata.descender) or units)
53    local units = tfmdata.units
54    local t_vorigin, t_vheight, t_ind_to_uni = {}, {}, {}
55    for i,v in pairs(rawdata.descriptions) do
56      t_ind_to_uni[v.index] = i
57      if v.tsb then
58        local j = v.boundingbox[4] + v.tsb
59        if j~=ascender then t_vorigin[i]= j / units end
60      end
61      if v.vheight then
62        if v.vheight~=default_vheight then t_vheight[i] = v.vheight / units end
63      end
64    end
65    setmetatable(t_vheight, {__index = function () return default_vheight / units end } )
66    setmetatable(t_vorigin, {__index = function () return ascender / units end } )
67    dest = dest or {}
68    dest.ind_to_uni = t_ind_to_uni
69    dest.vorigin = t_vorigin -- designed size = 1.0
70    dest.vheight = t_vheight -- designed size = 1.0
71    return dest
72 end
73 aux.get_vmet_table = get_vmet_table
74
75 local function loop_over_duplicates(id, func)
76 -- func: return non-nil iff abort this fn
77   local t = (type(id)=="table") and id or getfont(id)
78   if t and t.resources and t.resources.duplicates then
79     for i,v in pairs(t.resources.duplicates) do
80       func(i,v)
81     end
82   end
83 end
84 aux.loop_over_duplicates = loop_over_duplicates
85
86 local function loop_over_feat(id, feature_name, func, universal)
87 -- feature_name: like { vert=true, vrt2 = true, ...}
88 -- func: return non-nil iff abort this fn
89 -- universal: true iff look up all (script, lang) pair
90   local t = (type(id)=="table") and id or getfont(id)
91   if t and t.resources and t.resources.sequences then
92     for _,i in pairs(t.resources.sequences) do
93       if i.order[1] and feature_name[i.order[1]] then
94         local f = i.features and i.features[i.order[1]]
95         if i.type == 'gsub_single' and i.steps 
96           and f and (universal or (f[t.properties.script] and f[t.properties.script][t.properties.language])) then
97           for _,j in pairs(i.steps) do
98             if type(j)=='table' then 
99               if type(j.coverage)=='table' then
100                 for i,k in pairs(j.coverage) do
101                   local s = func(i,k); if s then return s end
102                 end
103               end
104             end
105           end
106         end
107       end
108     end
109   end
110 end
111 aux.loop_over_feat = loop_over_feat
112
113 local vert_vrt2 = { vert=true, vrt2=true }
114 function aux.replace_vert_variant(id, c)
115   return loop_over_feat(id, vert_vrt2, 
116            function (i,k) if i==c then return k end end)
117          or c
118 end
119
120
121
122 local search
123 search = function (t, key, prefix)
124   if type(t)=="table" then
125     prefix = prefix or ''
126     for i,v in pairs(t) do 
127       if i==key then print(prefix..'.'..i, v) 
128       else  search(v,key,prefix..'.'..tostring(i)) end
129     end
130   end
131 end
132 aux.t_search = search
133
134 -- EOF