OSDN Git Service

moved (almost) luaotfload-related code into ltj-lotf_aux.lua
[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
33 do
34 local nulltable = {}
35 local function get_cidinfo(id) -- table
36   local t = getfont(id)
37   local a = t and (t.cidinfo or (t.resources and t.resources and t.resources.cidinfo)) or nulltable
38   return a
39 end
40 aux.get_cidinfo = get_cidinfo
41 end
42
43 local function get_ascender(id) -- scaled points
44   if font_metric_table[id].ascender then return font_metric_table[id].ascender end
45   local t = getfont(id)
46   local a = t and t.parameters and t.parameters.ascender or 0
47   font_metric_table[id].ascender = a; return a
48 end
49
50 local function get_descender(id) -- scaled points
51   if font_metric_table[id].descender then return font_metric_table[id].descender end
52   local t = getfont(id)
53   local a = t and t.parameters and t.parameters.descender or 0
54   font_metric_table[id].descender = a; return a
55 end
56 aux.get_ascender, aux.get_descender = get_ascender, get_descender
57
58 local function get_vmet_table(tfmdata, dest)
59    if tfmata and tfmdata.shared then return dest end
60    local rawdata = tfmdata.shared.rawdata
61    local ascender = rawdata.metadata.ascender or 0
62    local default_vheight 
63      = rawdata.metadata.defaultvheight
64        or (rawdata.metadata.descender and (ascender+rawdata.metadata.descender) or units)
65    local units = tfmdata.units
66    local t_vorigin, t_vheight, t_ind_to_uni = {}, {}, {}
67    for i,v in pairs(rawdata.descriptions) do
68      t_ind_to_uni[v.index] = i
69      if v.tsb then
70        local j = v.boundingbox[4] + v.tsb
71        if j~=ascender then t_vorigin[i]= j / units end
72      end
73      if v.vheight then
74        if v.vheight~=default_vheight then t_vheight[i] = v.vheight / units end
75      end
76    end
77    setmetatable(t_vheight, {__index = function () return default_vheight / units end } )
78    setmetatable(t_vorigin, {__index = function () return ascender / units end } )
79    dest = dest or {}
80    dest.ind_to_uni = t_ind_to_uni
81    dest.vorigin = t_vorigin -- designed size = 1.0
82    dest.vheight = t_vheight -- designed size = 1.0
83    return dest
84 end
85 aux.get_vmet_table = get_vmet_table
86
87 local function loop_over_duplicates(id, func)
88 -- func: return non-nil iff abort this fn
89   local t = (type(id)=="table") and id or getfont(id)
90   if t and t.resources and t.resources.duplicates then
91     for i,v in pairs(t.resources.duplicates) do
92       func(i,v)
93     end
94   end
95 end
96 aux.loop_over_duplicates = loop_over_duplicates
97
98 local function loop_over_feat(id, feature_name, func, universal)
99 -- feature_name: like { vert=true, vrt2 = true, ...}
100 -- func: return non-nil iff abort this fn
101 -- universal: true iff look up all (script, lang) pair
102   local t = (type(id)=="table") and id or getfont(id)
103   if t and t.resources and t.resources.sequences then
104     for _,i in pairs(t.resources.sequences) do
105       if i.order[1] and feature_name[i.order[1]] then
106         local f = i.features and i.features[i.order[1]]
107         if i.type == 'gsub_single' and i.steps 
108           and f and (universal or (f[t.properties.script] and f[t.properties.script][t.properties.language])) then
109           for _,j in pairs(i.steps) do
110             if type(j)=='table' then 
111               if type(j.coverage)=='table' then
112                 for i,k in pairs(j.coverage) do
113                   local s = func(i,k); if s then return s end
114                 end
115               end
116             end
117           end
118         end
119       end
120     end
121   end
122 end
123 aux.loop_over_feat = loop_over_feat
124
125 local vert_vrt2 = { vert=true, vrt2=true }
126 function aux.replace_vert_variant(id, c)
127   return loop_over_feat(id, vert_vrt2, 
128            function (i,k) if i==c then return k end end)
129          or c
130 end
131
132
133
134 local search
135 search = function (t, key, prefix)
136   if type(t)=="table" then
137     prefix = prefix or ''
138     for i,v in pairs(t) do 
139       if i==key then print(prefix..'.'..i, v) 
140       else  search(v,key,prefix..'.'..tostring(i)) end
141     end
142   end
143 end
144 aux.t_search = search
145
146 -- EOF