-
Notifications
You must be signed in to change notification settings - Fork 0
/
ligatures.go
91 lines (80 loc) · 2.41 KB
/
ligatures.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// seehuhn.de/go/sfnt - a library for reading and writing font files
// Copyright (C) 2024 Jochen Voss <voss@seehuhn.de>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package sfnt
import (
"slices"
"golang.org/x/exp/maps"
"golang.org/x/text/language"
"seehuhn.de/go/sfnt/cmap"
"seehuhn.de/go/sfnt/glyph"
"seehuhn.de/go/sfnt/opentype/coverage"
"seehuhn.de/go/sfnt/opentype/gtab"
)
func standardLigatures(cmap cmap.Subtable) *gtab.Info {
// `All` lists the ligatures characters, followed by the sequence of glyphs
// that should be replaced by the ligature. Longer sequences are listed
// first, so that the longest match is found first.
all := []string{"ffiffi", "fflffl", "ffff", "fifi", "flfl"}
// ligatures grouped by the first letter of the split glyph sequence
ll := map[glyph.ID][]gtab.Ligature{}
var gg []glyph.ID
ligLoop:
for _, lig := range all {
gg = gg[:0]
for _, r := range lig {
gid := cmap.Lookup(r)
if gid == 0 {
continue ligLoop
}
gg = append(gg, gid)
}
ll[gg[1]] = append(ll[gg[1]], gtab.Ligature{
In: slices.Clone(gg[2:]),
Out: gg[0],
})
}
if len(ll) == 0 {
return nil
}
// TODO(voss): merge this with the code in go-sfnt/examples/type1-to-otf/main.go
keys := maps.Keys(ll)
slices.Sort(keys)
cov := coverage.Table{}
var repl [][]gtab.Ligature
for i, gid := range keys {
cov[gid] = i
repl = append(repl, ll[gid])
}
subst := >ab.Gsub4_1{
Cov: cov,
Repl: repl,
}
gsub := >ab.Info{
ScriptList: map[language.Tag]*gtab.Features{
language.MustParse("und-Latn-x-latn"): {Optional: []gtab.FeatureIndex{0}},
},
FeatureList: []*gtab.Feature{
{Tag: "liga", Lookups: []gtab.LookupIndex{0}},
},
LookupList: []*gtab.LookupTable{
{
Meta: >ab.LookupMetaInfo{LookupType: 4},
Subtables: []gtab.Subtable{subst},
},
},
}
return gsub
}