-
Notifications
You must be signed in to change notification settings - Fork 0
/
family_mapping.go
161 lines (144 loc) · 4.25 KB
/
family_mapping.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package fontconfig
import (
"fmt"
"log"
"github.com/benoitkugler/textlayout/language"
)
func exprAsString(expr *expression) string {
if s, ok := expr.u.(String); ok && expr.op == opString {
return string(s)
}
return ""
}
func (d directive) editFamily() []ruleEdit {
var out []ruleEdit
for _, edit := range d.edits {
if edit.object == FAMILY {
out = append(out, edit)
}
}
return out
}
func exprAsStringList(expr *expression) []string {
if s, ok := expr.u.(String); ok && expr.op == opString {
return []string{string(s)}
}
if c, ok := expr.u.(exprTree); ok && expr.op == opComma {
l1 := exprAsStringList(c.left)
l2 := exprAsStringList(c.right)
return append(l1, l2...)
}
return nil
}
type ExportedFamilySubstitution struct {
Comment string
TestCode string
OpCode string // how to insert the families
AdditionalFamilies []string // the families to add
}
// GenerateSubstitution exports the Standard family substitution
// rules
func GenerateSubstitution() ([]ExportedFamilySubstitution, error) {
var substitutions []ExportedFamilySubstitution
for _, ruleset := range Standard.subst {
comment := ruleset.name
if ruleset.description != "" {
comment += fmt.Sprintf(" (%s)", ruleset.description)
}
for _, directive := range ruleset.subst[MatchQuery] {
edits := directive.editFamily()
if len(edits) == 0 {
continue
}
if len(edits) != 1 {
return nil, fmt.Errorf("edit not supported: %v", edits)
}
edit := edits[0]
subs := ExportedFamilySubstitution{
Comment: comment,
AdditionalFamilies: exprAsStringList(edit.expr),
}
switch edit.op.getOp() {
case opAppend:
subs.OpCode = "opAppend"
case opAppendLast:
subs.OpCode = "opAppendLast"
case opPrepend:
subs.OpCode = "opPrepend"
case opPrependFirst:
subs.OpCode = "opPrependFirst"
case opAssign:
subs.OpCode = "opReplace"
default:
return nil, fmt.Errorf("unexpected operation %v", edit.op)
}
// extract the test
tests := directive.tests
switch len(tests) {
case 1:
test := tests[0]
if test.object != FAMILY {
return nil, fmt.Errorf("test not supported: %v", test)
}
familyTarget := exprAsString(test.expr)
if familyTarget == "" {
return nil, fmt.Errorf("test not supported: %v", test)
}
switch test.op.getOp() {
case opEqual:
subs.TestCode = fmt.Sprintf("familyEquals(%q)", familyTarget)
case opContains:
subs.TestCode = fmt.Sprintf("familyContains(%q)", familyTarget)
default:
return nil, fmt.Errorf("test not supported: %v", test)
}
case 2:
var (
lang, fam string
langOp, famOp opKind
)
if tests[0].object == FAMILY && tests[1].object == LANG {
lang, fam = exprAsString(tests[1].expr), exprAsString(tests[0].expr)
langOp, famOp = tests[1].op.getOp(), tests[0].op.getOp()
} else if tests[1].object == FAMILY && tests[0].object == LANG {
lang, fam = exprAsString(tests[0].expr), exprAsString(tests[1].expr)
langOp, famOp = tests[0].op.getOp(), tests[1].op.getOp()
} else {
log.Println("ignored test", tests)
continue
}
switch langOp {
case opEqual:
switch famOp {
case opEqual:
subs.TestCode = fmt.Sprintf("langAndFamilyEqual{lang:%q,family: %q}", language.NewLanguage(lang), fam)
case opNotEqual:
subs.TestCode = fmt.Sprintf("langEqualsAndNoFamily{lang:%q,family: %q}", language.NewLanguage(lang), fam)
default:
return nil, fmt.Errorf("family op not supported: %v", tests)
}
case opContains:
if famOp != opEqual {
return nil, fmt.Errorf("family op not supported: %v", tests)
}
subs.TestCode = fmt.Sprintf("langContainsAndFamilyEquals{lang:%q,family: %q}", language.NewLanguage(lang), fam)
default:
return nil, fmt.Errorf("test not supported: %v", tests)
}
case 3:
// we special case the generic fallback
if tests[0].object == FAMILY && tests[1].object == FAMILY && tests[2].object == FAMILY {
subs.TestCode = "noGenericFamily{}"
} else {
log.Println("ignored test", tests)
continue
}
default:
log.Println("ignored test", tests)
continue
}
substitutions = append(substitutions, subs)
}
}
return substitutions, nil
}