OSDN Git Service

trying to support "mode=harf" (luaotfload 3.1) (not completed)
[luatex-ja/luatexja.git] / src / ltj-lotf_aux.lua
1 --
2 -- ltj-lotf_aux.lua
3 --
4
5 -- functions which access to caches by luaotfload gathered in this file.
6 -- lines with marked by "-- HARFLOAD" are codes for harfload
7 local aux = {}
8 luatexja.lotf_aux = aux
9
10 local font_metric_table = {}
11 aux.font_metric_table = font_metric_table
12
13 local getfont = font.getfont
14 local provides_feature = luaotfload.aux.provides_feature
15 function aux.exist_feature(id, name)
16   local t = getfont(id)
17   if t and t.properties then
18     return provides_feature(id, t.properties.script, t.properties.language, name)
19   else return false
20   end
21 end 
22
23 function aux.enable_feature(id, name)
24   local t = getfont(id)
25   if t and t.shared and t.shared.features then
26     t.shared.features[name] = true
27   elseif t and t.hb then -- HARF
28     local hb, tf = luaotfload.harfbuzz, t.hb.spec.hb_features
29     tf[#tf+1] = hb.Feature.new(name)
30   end
31 end
32 function aux.specified_feature(id, name)
33   local t = getfont(id)
34   return t and (t.shared and t.shared.features and t.shared.features[name])
35          or (t.hb and t.hb.spec and t.hb.spec.features.raw and t.hb.spec.features.raw[name]) -- HARF
36 end
37
38
39 do
40 local nulltable = {}
41 local function get_cidinfo(id) -- table
42   local t = getfont(id)
43   local a = t and (t.cidinfo or (t.resources and t.resources and t.resources.cidinfo)) or nulltable
44   return a
45 end
46 aux.get_cidinfo = get_cidinfo
47 end
48
49 local function get_asc_des(id)
50   local t, v = getfont(id), font_metric_table[id]
51   local a, d
52   if t and t.shared then
53     local u = t.units
54     local t2 = t.shared.rawdata.metadata
55     if t2 then
56       a, d = t2.ascender and t2.ascender/u, t2.descender and -t2.descender/u
57     end
58   -- HARF h_extents() returns "too large" value...?
59   -- rawdata.metadata.horizontalmetrics is same
60   end
61   v.ascender, v.descender =  (a or 0.88)*t.size, (d or 0.12)*t.size
62 end
63 local function get_ascender(id) -- scaled points
64   if not font_metric_table[id].ascender then get_asc_des(id) end
65   return font_metric_table[id].ascender
66 end
67
68 local function get_descender(id) -- scaled points
69   if not font_metric_table[id].descender then get_asc_des(id) end
70   return font_metric_table[id].descender
71 end
72 aux.get_ascender, aux.get_descender = get_ascender, get_descender
73
74 do
75 local dummy_vht, dummy_vorg = {}, {}
76 setmetatable(dummy_vht, {__index = function () return 1 end } )
77 setmetatable(dummy_vorg, {__index = function () return 0.88 end } )
78 local function get_vmet_table(tfmdata, dest)
79    if (not tfmdata) or (not tfmdata.shared) then
80      dest = dest or {}
81      dest.vorigin, dest.vheight = dummy_vorg, dummy_vht
82      return dest
83    end
84    local rawdata = tfmdata.shared.rawdata
85    local ascender = rawdata.metadata.ascender or 0
86    local default_vheight 
87      = rawdata.metadata.defaultvheight
88        or (rawdata.metadata.descender and (ascender+rawdata.metadata.descender) or units)
89    local units = tfmdata.units
90    local t_vorigin, t_vheight, t_ind_to_uni = {}, {}, {}
91    for i,v in pairs(rawdata.descriptions) do
92      t_ind_to_uni[v.index] = i
93      if v.tsb then
94        local j = v.boundingbox[4] + v.tsb
95        if j~=ascender then t_vorigin[i]= j / units end
96      end
97      if v.vheight then
98        if v.vheight~=default_vheight then t_vheight[i] = v.vheight / units end
99      end
100    end
101    setmetatable(t_vheight, {__index = function () return default_vheight / units end } )
102    setmetatable(t_vorigin, {__index = function () return ascender / units end } )
103    dest = dest or {}
104    dest.ind_to_uni = t_ind_to_uni
105    dest.vorigin = t_vorigin -- designed size = 1.0
106    dest.vheight = t_vheight -- designed size = 1.0
107    return dest
108 end
109 aux.get_vmet_table = get_vmet_table
110 end
111 local function loop_over_duplicates(id, func)
112 -- func: return non-nil iff abort this fn
113   local t = (type(id)=="table") and id or getfont(id)
114   if t and t.resources and t.resources.duplicates then -- HARF: not executed
115     for i,v in pairs(t.resources.duplicates) do
116       func(i,v)
117     end
118   end
119 end
120 aux.loop_over_duplicates = loop_over_duplicates
121
122 local function loop_over_feat(id, feature_name, func, universal)
123 -- feature_name: like { vert=true, vrt2 = true, ...}
124 -- func: return non-nil iff abort this fn
125 -- universal: true iff look up all (script, lang) pair
126   local t = (type(id)=="table") and id or getfont(id)
127   if t and t.resources and t.resources.sequences then -- HARF: not executed
128     for _,i in pairs(t.resources.sequences) do
129       if i.order[1] and feature_name[i.order[1]] then
130         local f = i.features and i.features[i.order[1]]
131         if i.type == 'gsub_single' and i.steps 
132           and f and (universal or (f[t.properties.script] and f[t.properties.script][t.properties.language])) then
133           for _,j in pairs(i.steps) do
134             if type(j)=='table' then 
135               if type(j.coverage)=='table' then
136                 for i,k in pairs(j.coverage) do
137                   local s = func(i,k); if s then return s end
138                 end
139               end
140             end
141           end
142         end
143       end
144     end
145   end
146 end
147 aux.loop_over_feat = loop_over_feat
148
149 local vert_vrt2 = { vert=true, vrt2=true }
150 function aux.replace_vert_variant(id, c)
151   return loop_over_feat(id, vert_vrt2, 
152            function (i,k) if i==c then return k end end)
153          or c
154 end
155
156
157 --for name, func in pairs(aux) do
158 --  if type(func)=="function" then 
159 --    aux[name] = function(...)
160 --      print('LOTF_AUX', name, ...);
161 --      local a = func(...); print('RESULT', a); return a
162 --    end
163 --  end
164 --end
165
166 local search
167 search = function (t, key, prefix)
168   if type(t)=="table" then
169     prefix = prefix or ''
170     for i,v in pairs(t) do 
171       if i==key then print(prefix..'.'..i, v) 
172       else  search(v,key,prefix..'.'..tostring(i)) end
173     end
174   end
175 end
176 aux.t_search = search
177
178
179
180 -- EOF