-
Notifications
You must be signed in to change notification settings - Fork 4
/
twine_builtin_effects.go
178 lines (160 loc) · 5.35 KB
/
twine_builtin_effects.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package etxt
import "strconv"
import "image/color"
//import "golang.org/x/image/font/sfnt"
import "github.com/tinne26/etxt/fract"
import "github.com/tinne26/etxt/mask"
// implements EffectPushColor
func twineEffectPushColor(renderer *Renderer, target Target, args TwineEffectArgs) {
// usage asserts
args.AssertPayloadLen(4)
args.AssertMode(SinglePass)
// bypass if measuring
if args.Measuring() { return }
// handle each trigger situation
switch args.GetTrigger() {
case TwineTriggerPush:
renderer.twineStoragePush(renderer.GetColor())
r, g, b, a := args.Payload[0], args.Payload[1], args.Payload[2], args.Payload[3]
renderer.SetColor(color.RGBA{r, g, b, a})
case TwineTriggerPop:
renderer.SetColor(renderer.twineStoragePop().(color.Color))
case TwineTriggerLineBreak, TwineTriggerLineStart:
// unused, not necessary
default:
panic("unexpected")
}
}
// implements EffectPushFont
func twineEffectPushFont(renderer *Renderer, target Target, args TwineEffectArgs) {
// usage asserts
args.AssertPayloadLen(1)
args.AssertMode(SinglePass)
// handle each trigger situation
switch args.GetTrigger() {
case TwineTriggerPush, TwineTriggerLineStart:
renderer.twineStoragePush(renderer.state.fontIndex)
renderer.Twine().SetFontIndex(FontIndex(args.Payload[0]))
case TwineTriggerPop, TwineTriggerLineBreak:
index := renderer.twineStoragePop().(FontIndex)
renderer.Twine().SetFontIndex(index)
default:
panic("unexpected")
}
}
// implements EffectShiftSize
func twineEffectShiftSize(renderer *Renderer, target Target, args TwineEffectArgs) {
// usage asserts
args.AssertPayloadLen(1)
args.AssertMode(SinglePass)
// handle each trigger situation
switch args.GetTrigger() {
case TwineTriggerPush, TwineTriggerLineStart:
renderer.twineStoragePush(renderer.state.logicalSize)
sizeShift := fract.FromInt(int(int8(args.Payload[0])))
renderer.Fract().SetSize(renderer.state.logicalSize + sizeShift)
case TwineTriggerPop, TwineTriggerLineBreak:
size := renderer.twineStoragePop().(fract.Unit)
renderer.Fract().SetSize(size)
default:
panic("unexpected")
}
}
// implements EffectSetSize
func twineEffectSetSize(renderer *Renderer, target Target, args TwineEffectArgs) {
// usage asserts
args.AssertPayloadLen(1)
args.AssertMode(SinglePass)
// handle each trigger situation
switch args.GetTrigger() {
case TwineTriggerPush, TwineTriggerLineStart:
renderer.twineStoragePush(renderer.state.logicalSize)
newSize := fract.FromInt(int(args.Payload[0]))
renderer.Fract().SetSize(newSize)
case TwineTriggerPop, TwineTriggerLineBreak:
size := renderer.twineStoragePop().(fract.Unit)
renderer.Fract().SetSize(size)
default:
panic("unexpected")
}
}
// implements EffectOblique
func twineEffectOblique(renderer *Renderer, target Target, args TwineEffectArgs) {
// usage asserts
args.AssertMode(SinglePass)
fauxRast, isFauxRast := renderer.Glyph().GetRasterizer().(*mask.FauxRasterizer)
if !isFauxRast {
panic("EffectOblique requires using the mask.FauxRasterizer")
}
var angleReference byte
switch len(args.Payload) {
case 0: angleReference = 196
case 1: angleReference = args.Payload[0]
default:
payloadLenStr := strconv.Itoa(len(args.Payload))
panic(
"EffectOblique expects one byte (below 128 for left slant, above " +
"128 for right slant) in the payload or no bytes at all (defaults " +
"to 196), but got " + payloadLenStr + " bytes instead.",
)
}
// bypass if measuring
if args.Measuring() { return }
// handle each trigger situation
switch args.GetTrigger() {
case TwineTriggerPush:
renderer.twineStoragePush(fauxRast.GetSkewFactor())
if angleReference == 128 { // (no slant)
fauxRast.SetSkewFactor(0)
} else if angleReference > 128 { // (right slant)
fauxRast.SetSkewFactor(0.3*(float32(angleReference - 128)/128.0))
} else { // angleReference < 128 // (left slant)
fauxRast.SetSkewFactor(-0.3*float32(128 - angleReference)/128.0)
}
case TwineTriggerPop:
fauxRast.SetSkewFactor(renderer.twineStoragePop().(float32))
case TwineTriggerLineBreak, TwineTriggerLineStart:
// unused, not necessary
default:
panic("unexpected")
}
}
// implements EffectFauxBold
func twineEffectFauxBold(renderer *Renderer, target Target, args TwineEffectArgs) {
// usage asserts
args.AssertMode(SinglePass)
fauxRast, isFauxRast := renderer.Glyph().GetRasterizer().(*mask.FauxRasterizer)
if !isFauxRast {
panic("EffectFauxBold requires using the mask.FauxRasterizer")
}
var thicknessReference byte
switch len(args.Payload) {
case 0: thicknessReference = 128
case 1: thicknessReference = args.Payload[0]
default:
payloadLenStr := strconv.Itoa(len(args.Payload))
panic(
"EffectFauxBold expects one byte in the payload or no bytes " +
"at all (defaults to 128), but got " + payloadLenStr + " bytes " +
"instead.",
)
}
// bypass if measuring
if args.Measuring() { return }
// handle each trigger situation
switch args.GetTrigger() {
case TwineTriggerPush:
renderer.twineStoragePush(fauxRast.GetExtraWidth())
const thicknessBasePercent = 0.14
factor := (float32(thicknessReference) + 1.0)/256.0
factor *= thicknessBasePercent
extraWidth := renderer.state.scaledSize.ToFloat32()*factor
fauxRast.SetExtraWidth(extraWidth)
case TwineTriggerPop:
fauxRast.SetExtraWidth(renderer.twineStoragePop().(float32))
case TwineTriggerLineBreak, TwineTriggerLineStart:
// unused, not necessary
default:
panic("unexpected")
}
}