OSDN Git Service

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