OSDN Git Service

bd3a602e64801e77928917914f140f03d07254b3
[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   return (t and (t.resources and t.resources.cidinfo or t.cidinfo)) or nulltable
44 end
45 aux.get_cidinfo = get_cidinfo
46 end
47
48 local function get_asc_des(id)
49   local t, v = getfont(id), font_metric_table[id]
50   local a, d
51   if t and t.shared and t.shared.rawdata then
52     local u = t.units
53     local t2 = t.shared.rawdata.metadata
54     if t2 then
55       a, d = t2.ascender and t2.ascender/u, t2.descender and -t2.descender/u
56     end
57   elseif t.hb then -- HARF
58     local hbfont, u = t.hb.shared.font, t.hb.shared.upem
59     local h = hbfont:get_h_extents()
60     if h and u then 
61        a, d = h.ascender and h.ascender/u, h.descender and -h.descender/u
62     end
63   end
64   v.ascender, v.descender =  (a or 0.88)*t.size, (d or 0.12)*t.size
65 end
66 local function get_ascender(id) -- scaled points
67   if not font_metric_table[id].ascender then get_asc_des(id) end
68   return font_metric_table[id].ascender
69 end
70
71 local function get_descender(id) -- scaled points
72   if not font_metric_table[id].descender then get_asc_des(id) end
73   return font_metric_table[id].descender
74 end
75 aux.get_ascender, aux.get_descender = get_ascender, get_descender
76
77 do
78 local dummy_vht, dummy_vorg = {}, {}
79 setmetatable(dummy_vht, {__index = function () return 1 end } )
80 setmetatable(dummy_vorg, {__index = function () return 0.88 end } )
81 local function get_vmet_table(tfmdata, dest)
82    if (not tfmdata) or (not tfmdata.shared) or (not tfmdata.shared.rawdata) then
83      dest = dest or {}
84      dest.vorigin, dest.vheight = dummy_vorg, dummy_vht
85      dest.ind_to_uni = {}
86      return dest
87    end
88    local rawdata = tfmdata.shared.rawdata
89    local ascender = rawdata.metadata.ascender or 0
90    local default_vheight 
91      = rawdata.metadata.defaultvheight
92        or (rawdata.metadata.descender and (ascender+rawdata.metadata.descender) or units)
93    local units = tfmdata.units
94    local t_vorigin, t_vheight, t_ind_to_uni = {}, {}, {}
95    for i,v in pairs(rawdata.descriptions) do
96      t_ind_to_uni[v.index] = i
97      if v.tsb then
98        local j = v.boundingbox[4] + v.tsb
99        if j~=ascender then t_vorigin[i]= j / units end
100      end
101      if v.vheight then
102        if v.vheight~=default_vheight then t_vheight[i] = v.vheight / units end
103      end
104    end
105    local vhd, vod = default_vheight / units, ascender/units
106    t_vheight.default = vhd
107    t_vorigin.default = vod
108    setmetatable(t_vheight, {__index = function () return vhd end } )
109    setmetatable(t_vorigin, {__index = function () return vod end } )
110    dest = dest or {}
111    dest.ind_to_uni = t_ind_to_uni
112    dest.vorigin = t_vorigin -- designed size = 1.0
113    dest.vheight = t_vheight -- designed size = 1.0
114    return dest
115 end
116 aux.get_vmet_table = get_vmet_table
117 end
118 local function loop_over_duplicates(id, func)
119 -- func: return non-nil iff abort this fn
120   local t = (type(id)=="table") and id or getfont(id)
121   if t and t.resources and t.resources.duplicates then -- HARF: not executed
122     for i,v in pairs(t.resources.duplicates) do
123       func(i,v)
124     end
125   end
126 end
127 aux.loop_over_duplicates = loop_over_duplicates
128
129 local function loop_over_feat(id, feature_name, func, universal)
130 -- feature_name: like { vert=true, vrt2 = true, ...}
131 -- func: return non-nil iff abort this fn
132 -- universal: true iff look up all (script, lang) pair
133   local t = (type(id)=="table") and id or getfont(id)
134   if t and t.resources and t.resources.sequences then -- HARF: not executed
135     for _,i in pairs(t.resources.sequences) do
136       if i.order[1] and feature_name[i.order[1]] then
137         local f = i.features and i.features[i.order[1]]
138         if i.type == 'gsub_single' and i.steps 
139           and f and (universal or (f[t.properties.script] and f[t.properties.script][t.properties.language])) then
140           for _,j in pairs(i.steps) do
141             if type(j)=='table' then 
142               if type(j.coverage)=='table' then
143                 for i,k in pairs(j.coverage) do
144                   local s = func(i,k); if s then return s end
145                 end
146               end
147             end
148           end
149         end
150       end
151     end
152   end
153 end
154 aux.loop_over_feat = loop_over_feat
155
156 local vert_vrt2 = { vert=true, vrt2=true }
157 function aux.replace_vert_variant(id, c)
158   return loop_over_feat(id, vert_vrt2, 
159            function (i,k) if i==c then return k end end)
160          or c
161 end
162
163
164 --for name, func in pairs(aux) do
165 --  if type(func)=="function" then 
166 --    aux[name] = function(...)
167 --      print('LOTF_AUX', name, ...);
168 --      local a = func(...); print('RESULT', a); return a
169 --    end
170 --  end
171 --end
172
173 local search
174 search = function (t, key, prefix)
175   if type(t)=="table" then
176     prefix = prefix or ''
177     for i,v in pairs(t) do 
178       if i==key then print(prefix..'.'..i, v) 
179       else  search(v,key,prefix..'.'..tostring(i)) end
180     end
181   end
182 end
183 aux.t_search = search
184
185
186
187 -- EOF