import QuartzCore
import Foundation

private struct SkimFrame {
    let textInk: CGPath
    let textAlpha: Float
    let textMask: CGPath
    let lensTint: CGPath
    let lensStroke: CGPath
    let brows: CGPath
    let nose: CGPath
    let pupils: CGPath
    let glassMask: CGPath
    let silTint: CGPath
    let silStroke: CGPath
    let inkFill: CGPath
    let inkStroke: CGPath
    let burst: CGPath
}

private enum SkimModel {
    static let duration = 3.2
    static let count = 96
    static let x0 = 27.0
    static let lines: [(Double, [Double])] = [(79, [15, 9, 19, 11]), (90, [11, 17, 8, 14]), (101, [18, 12, 15])]
    static let rs: [(Double, Double)] = [(0.1, 0.85), (0.97, 1.97), (2.28, 3.0)]
    static let gy = [0.5, 2.0, 3.5]
    static let tip = (552.0, 22.0)
    static let silData = "M5490 12781 c-148 -8 -238 -26 -295 -61 -16 -10 -61 -26 -99 -36 -46 -11 -78 -26 -97 -45 -27 -27 -83 -49 -122 -49 -11 0 -39 -19 -63 -42 -63 -62 -114 -103 -152 -122 -17 -9 -43 -33 -56 -54 -13 -20 -33 -44 -43 -53 -11 -9 -26 -25 -34 -35 -8 -11 -28 -30 -46 -42 -21 -15 -37 -39 -48 -71 -9 -27 -25 -57 -37 -67 -45 -41 -111 -148 -138 -224 -16 -45 -40 -100 -53 -123 -14 -23 -36 -73 -50 -112 -14 -38 -34 -87 -45 -107 -11 -21 -23 -68 -26 -105 -10 -99 -35 -229 -52 -268 -17 -42 -40 -217 -49 -375 -3 -63 -13 -212 -21 -330 -12 -170 -22 -403 -26 -600 -1 -34 22 -404 32 -515 6 -66 17 -212 25 -325 9 -113 25 -248 36 -300 33 -150 38 -184 49 -336 14 -175 8 -198 -55 -201 -136 -8 -288 -27 -340 -42 -143 -43 -173 -51 -229 -57 -48 -6 -66 -14 -103 -45 -29 -26 -58 -41 -86 -46 -28 -4 -54 -18 -81 -44 -23 -20 -60 -42 -83 -48 -31 -9 -54 -26 -93 -71 -29 -33 -78 -76 -110 -96 -41 -25 -63 -46 -75 -74 -23 -51 -59 -84 -119 -110 -27 -11 -64 -34 -83 -50 -18 -17 -43 -30 -54 -30 -40 0 -240 -51 -278 -71 -22 -11 -70 -25 -107 -30 -38 -5 -79 -17 -92 -27 -49 -35 -98 -62 -113 -62 -9 0 -42 -18 -74 -39 -32 -22 -77 -44 -101 -50 -31 -8 -52 -22 -75 -50 -17 -22 -50 -47 -72 -56 -23 -9 -58 -31 -78 -48 -20 -18 -69 -59 -108 -92 -39 -33 -71 -63 -71 -66 0 -4 -25 -35 -55 -70 -30 -35 -73 -85 -95 -111 -22 -26 -47 -66 -56 -90 -8 -24 -32 -68 -52 -98 -62 -91 -121 -220 -131 -286 -5 -34 -21 -85 -36 -114 -15 -30 -38 -111 -55 -193 -16 -78 -37 -167 -45 -197 -38 -136 -42 -824 -5 -985 5 -22 24 -114 42 -205 25 -119 42 -178 62 -212 15 -26 34 -77 42 -114 7 -37 24 -80 37 -98 13 -17 36 -55 51 -84 14 -28 36 -58 47 -65 12 -6 32 -35 45 -64 17 -38 38 -63 75 -90 28 -21 77 -64 109 -96 32 -31 64 -57 71 -57 7 0 38 -15 69 -34 30 -19 82 -39 115 -46 33 -6 79 -23 101 -36 118 -68 600 -70 710 -3 38 23 70 24 116 5 48 -21 55 -32 79 -127 11 -46 35 -109 53 -139 18 -30 40 -74 49 -96 9 -23 27 -58 41 -78 14 -21 32 -61 40 -91 9 -29 32 -73 51 -97 20 -23 48 -59 64 -78 15 -20 38 -56 51 -80 13 -25 56 -79 96 -120 41 -41 80 -84 88 -96 8 -11 47 -47 87 -80 39 -32 86 -74 102 -92 19 -20 50 -38 82 -47 36 -11 62 -28 91 -58 28 -29 54 -46 87 -55 26 -6 64 -23 85 -37 21 -14 64 -30 97 -36 37 -7 75 -22 101 -41 23 -17 68 -36 101 -43 32 -7 111 -27 177 -44 65 -17 154 -35 197 -41 128 -16 132 -20 159 -170 11 -61 30 -130 41 -152 11 -22 27 -71 35 -108 23 -111 32 -125 174 -269 107 -108 216 -183 307 -209 37 -11 111 -36 163 -55 219 -81 786 -76 980 7 30 13 75 27 100 31 72 10 233 54 256 70 12 7 50 21 85 30 38 10 76 28 96 45 18 16 52 34 75 40 75 19 97 28 130 54 18 13 51 29 73 35 24 6 56 26 79 50 23 24 61 47 95 60 51 19 109 60 295 209 86 68 254 228 306 291 25 30 61 71 80 92 76 81 119 137 149 194 17 33 56 93 86 134 38 51 61 96 76 143 11 38 29 78 40 89 18 18 19 37 19 453 0 366 -2 435 -14 439 -8 3 -24 32 -36 64 -32 90 -176 234 -289 291 -46 24 -95 51 -109 62 -24 20 -134 56 -240 80 -112 26 -145 91 -173 343 -18 159 -30 232 -69 422 -4 22 -16 128 -25 235 -10 125 -26 243 -45 328 -17 74 -34 170 -40 215 -18 157 -31 251 -35 258 -16 25 -47 247 -60 424 -9 116 -25 260 -36 320 -35 190 -38 216 -55 400 -16 194 -23 241 -52 410 -11 62 -25 209 -31 335 -17 345 -30 548 -41 625 -98 736 -104 2536 -10 2918 6 23 13 134 16 247 5 195 4 211 -21 325 -14 66 -32 155 -40 197 -8 47 -24 92 -40 116 -14 21 -33 68 -43 104 -10 36 -37 93 -60 128 -23 34 -42 66 -42 71 0 4 -17 20 -37 34 -21 13 -44 37 -53 53 -21 40 -100 109 -146 128 -21 9 -51 27 -66 39 -15 13 -54 33 -85 45 -32 12 -74 32 -94 46 -19 13 -67 31 -105 39 -85 17 -418 26 -614 16z"
    static let inkData = "M5490 12781 c-148 -8 -238 -26 -295 -61 -16 -10 -61 -26 -99 -36 -46 -11 -78 -26 -97 -45 -27 -27 -83 -49 -122 -49 -11 0 -39 -19 -63 -42 -63 -62 -114 -103 -152 -122 -17 -9 -43 -33 -56 -54 -13 -20 -33 -44 -43 -53 -11 -9 -26 -25 -34 -35 -8 -11 -28 -30 -46 -42 -21 -15 -37 -39 -48 -71 -9 -27 -25 -57 -37 -67 -45 -41 -111 -148 -138 -224 -16 -45 -40 -100 -53 -123 -14 -23 -36 -73 -50 -112 -14 -38 -34 -87 -45 -107 -11 -21 -23 -68 -26 -105 -10 -99 -35 -229 -52 -268 -17 -42 -40 -217 -49 -375 -3 -63 -13 -212 -21 -330 -12 -170 -22 -403 -26 -600 -1 -34 22 -404 32 -515 6 -66 17 -212 25 -325 9 -113 25 -248 36 -300 33 -150 38 -184 49 -336 14 -175 8 -198 -55 -201 -136 -8 -288 -27 -340 -42 -143 -43 -173 -51 -229 -57 -48 -6 -66 -14 -103 -45 -29 -26 -58 -41 -86 -46 -28 -4 -54 -18 -81 -44 -23 -20 -60 -42 -83 -48 -31 -9 -54 -26 -93 -71 -29 -33 -78 -76 -110 -96 -41 -25 -63 -46 -75 -74 -23 -51 -59 -84 -119 -110 -27 -11 -64 -34 -83 -50 -18 -17 -43 -30 -54 -30 -40 0 -240 -51 -278 -71 -22 -11 -70 -25 -107 -30 -38 -5 -79 -17 -92 -27 -49 -35 -98 -62 -113 -62 -9 0 -42 -18 -74 -39 -32 -22 -77 -44 -101 -50 -31 -8 -52 -22 -75 -50 -17 -22 -50 -47 -72 -56 -23 -9 -58 -31 -78 -48 -20 -18 -69 -59 -108 -92 -39 -33 -71 -63 -71 -66 0 -4 -25 -35 -55 -70 -30 -35 -73 -85 -95 -111 -22 -26 -47 -66 -56 -90 -8 -24 -32 -68 -52 -98 -62 -91 -121 -220 -131 -286 -5 -34 -21 -85 -36 -114 -15 -30 -38 -111 -55 -193 -16 -78 -37 -167 -45 -197 -38 -136 -42 -824 -5 -985 5 -22 24 -114 42 -205 25 -119 42 -178 62 -212 15 -26 34 -77 42 -114 7 -37 24 -80 37 -98 13 -17 36 -55 51 -84 14 -28 36 -58 47 -65 12 -6 32 -35 45 -64 17 -38 38 -63 75 -90 28 -21 77 -64 109 -96 32 -31 64 -57 71 -57 7 0 38 -15 69 -34 30 -19 82 -39 115 -46 33 -6 79 -23 101 -36 118 -68 600 -70 710 -3 38 23 70 24 116 5 48 -21 55 -32 79 -127 11 -46 35 -109 53 -139 18 -30 40 -74 49 -96 9 -23 27 -58 41 -78 14 -21 32 -61 40 -91 9 -29 32 -73 51 -97 20 -23 48 -59 64 -78 15 -20 38 -56 51 -80 13 -25 56 -79 96 -120 41 -41 80 -84 88 -96 8 -11 47 -47 87 -80 39 -32 86 -74 102 -92 19 -20 50 -38 82 -47 36 -11 62 -28 91 -58 28 -29 54 -46 87 -55 26 -6 64 -23 85 -37 21 -14 64 -30 97 -36 37 -7 75 -22 101 -41 23 -17 68 -36 101 -43 32 -7 111 -27 177 -44 65 -17 154 -35 197 -41 128 -16 132 -20 159 -170 11 -61 30 -130 41 -152 11 -22 27 -71 35 -108 23 -111 32 -125 174 -269 107 -108 216 -183 307 -209 37 -11 111 -36 163 -55 219 -81 786 -76 980 7 30 13 75 27 100 31 72 10 233 54 256 70 12 7 50 21 85 30 38 10 76 28 96 45 18 16 52 34 75 40 75 19 97 28 130 54 18 13 51 29 73 35 24 6 56 26 79 50 23 24 61 47 95 60 51 19 109 60 295 209 86 68 254 228 306 291 25 30 61 71 80 92 76 81 119 137 149 194 17 33 56 93 86 134 38 51 61 96 76 143 11 38 29 78 40 89 18 18 19 37 19 453 0 366 -2 435 -14 439 -8 3 -24 32 -36 64 -32 90 -176 234 -289 291 -46 24 -95 51 -109 62 -24 20 -134 56 -240 80 -112 26 -145 91 -173 343 -18 159 -30 232 -69 422 -4 22 -16 128 -25 235 -10 125 -26 243 -45 328 -17 74 -34 170 -40 215 -18 157 -31 251 -35 258 -16 25 -47 247 -60 424 -9 116 -25 260 -36 320 -35 190 -38 216 -55 400 -16 194 -23 241 -52 410 -11 62 -25 209 -31 335 -17 345 -30 548 -41 625 -98 736 -104 2536 -10 2918 6 23 13 134 16 247 5 195 4 211 -21 325 -14 66 -32 155 -40 197 -8 47 -24 92 -40 116 -14 21 -33 68 -43 104 -10 36 -37 93 -60 128 -23 34 -42 66 -42 71 0 4 -17 20 -37 34 -21 13 -44 37 -53 53 -21 40 -100 109 -146 128 -21 9 -51 27 -66 39 -15 13 -54 33 -85 45 -32 12 -74 32 -94 46 -19 13 -67 31 -105 39 -85 17 -418 26 -614 16z m425 -441 c83 -14 128 -26 145 -40 34 -27 76 -47 127 -60 27 -7 47 -19 54 -33 7 -12 28 -41 48 -64 128 -147 114 -120 145 -278 15 -72 33 -155 42 -185 19 -69 14 -374 -9 -495 -40 -208 -42 -267 -42 -1345 0 -1192 11 -1506 60 -1840 9 -57 24 -228 35 -380 11 -151 27 -318 35 -370 30 -191 44 -319 55 -515 11 -196 26 -314 55 -425 7 -30 21 -116 30 -190 16 -135 34 -243 60 -355 8 -33 21 -145 30 -250 18 -215 19 -227 45 -310 20 -63 44 -189 55 -287 3 -31 20 -110 36 -175 16 -65 34 -170 40 -233 13 -142 30 -234 62 -342 14 -46 33 -132 42 -193 9 -60 27 -144 41 -186 13 -42 31 -139 40 -215 10 -93 27 -176 50 -249 18 -60 40 -157 49 -214 27 -185 43 -218 91 -186 35 22 61 85 74 176 38 260 65 373 100 404 18 16 22 16 75 -6 31 -13 66 -33 78 -45 12 -12 40 -33 62 -48 82 -55 128 -125 174 -266 55 -166 56 -272 3 -428 -18 -51 -32 -103 -32 -115 0 -13 -10 -41 -22 -64 -13 -23 -36 -66 -52 -95 -15 -29 -43 -69 -61 -88 -18 -19 -43 -57 -55 -83 -14 -29 -49 -72 -90 -110 -37 -34 -75 -76 -83 -93 -15 -29 -71 -80 -130 -117 -15 -10 -37 -33 -50 -52 -12 -19 -53 -53 -90 -77 -37 -24 -92 -62 -121 -85 -30 -22 -69 -49 -87 -59 -19 -9 -55 -31 -81 -49 -25 -18 -65 -35 -87 -39 -27 -4 -55 -19 -81 -42 -26 -24 -59 -41 -97 -51 -32 -8 -72 -23 -89 -34 -17 -10 -96 -35 -176 -54 -80 -19 -165 -43 -189 -54 -116 -50 -603 -58 -704 -12 -27 13 -89 36 -137 52 -126 41 -202 82 -235 126 -15 21 -46 59 -68 84 -86 99 -139 256 -103 311 16 24 23 25 145 31 120 6 361 32 392 43 7 2 61 11 120 19 127 18 190 38 246 77 26 19 69 35 111 44 66 14 119 41 119 60 -1 30 -56 36 -250 25 -505 -28 -977 -7 -1175 52 -63 19 -140 38 -170 41 -67 9 -205 48 -250 71 -18 9 -56 25 -84 35 -28 9 -73 31 -101 49 -27 17 -67 34 -88 38 -21 3 -52 16 -70 29 -18 13 -58 37 -90 54 -32 17 -77 48 -100 69 -23 21 -71 58 -106 81 -35 24 -71 57 -81 73 -10 16 -50 58 -89 94 -40 36 -81 84 -93 107 -11 22 -38 60 -59 83 -22 24 -39 49 -39 57 0 7 -18 33 -40 57 -35 38 -60 90 -60 124 0 6 -18 36 -40 69 -22 32 -40 64 -40 71 0 8 -17 46 -39 87 -28 52 -41 91 -46 138 -9 88 -34 195 -56 247 -10 24 -23 81 -29 126 -14 115 -37 250 -54 327 -24 105 -36 275 -43 605 -7 329 -9 352 -37 369 -14 9 -21 6 -39 -16 -94 -115 -115 -156 -116 -228 -1 -19 -14 -63 -30 -97 -61 -128 -88 -362 -76 -665 12 -325 13 -313 -10 -338 -36 -40 -76 -52 -219 -67 -298 -31 -499 6 -629 114 -25 21 -57 46 -70 55 -14 10 -36 35 -49 57 -12 22 -51 84 -85 137 -50 79 -65 112 -78 173 -9 42 -22 103 -30 136 -39 160 -54 252 -60 385 -8 179 9 474 35 590 11 48 26 153 35 234 19 182 27 217 58 264 13 20 34 59 45 85 12 27 33 63 48 80 14 17 37 55 50 85 15 35 48 77 93 122 38 38 79 87 91 109 15 30 34 48 73 67 28 15 81 53 117 86 68 61 190 127 360 193 47 19 105 42 130 53 29 13 89 24 165 31 404 37 454 38 745 16 307 -23 331 -26 407 -57 40 -15 123 -41 185 -57 62 -16 130 -38 150 -49 21 -11 64 -23 95 -27 32 -3 78 -18 103 -32 59 -33 231 -107 350 -150 52 -18 109 -45 127 -59 33 -25 259 -134 279 -134 7 0 25 -13 40 -28 37 -37 58 -44 80 -27 29 21 5 95 -51 157 -25 28 -45 54 -45 59 0 4 -13 15 -29 25 -15 9 -36 30 -45 46 -23 39 -91 104 -138 134 -21 13 -44 34 -50 45 -7 11 -30 29 -50 40 -21 10 -52 32 -70 47 -18 16 -98 61 -178 101 -80 41 -157 82 -172 92 -15 11 -69 30 -120 43 -51 13 -109 33 -128 44 -20 12 -65 26 -102 31 -36 5 -79 16 -96 25 -66 34 -194 66 -366 90 -204 28 -229 39 -234 99 -4 43 25 60 233 141 66 26 143 56 170 68 119 50 658 43 759 -9 16 -8 88 -30 160 -48 72 -17 151 -42 175 -54 23 -12 71 -27 105 -32 49 -8 68 -17 96 -44 21 -21 57 -41 87 -50 28 -8 70 -31 92 -49 25 -21 57 -37 83 -41 30 -5 52 -18 73 -40 30 -34 115 -94 170 -122 17 -9 47 -31 68 -50 21 -19 50 -39 65 -46 15 -6 48 -30 74 -54 26 -24 54 -43 63 -43 9 0 31 -16 51 -35 40 -40 49 -43 64 -15 15 29 2 84 -30 129 -15 21 -36 53 -45 72 -10 19 -33 51 -51 71 -18 20 -36 53 -40 73 -5 28 -23 52 -71 98 -35 34 -78 83 -95 109 -17 27 -60 70 -98 98 -37 28 -73 61 -79 74 -7 13 -54 51 -105 84 -50 33 -99 74 -109 91 -13 22 -30 34 -60 41 -69 18 -98 32 -113 56 -16 23 -47 38 -101 49 -18 4 -48 19 -69 36 -33 26 -89 49 -122 49 -34 0 -84 35 -106 74 -27 46 -32 66 -46 181 -5 44 -16 107 -24 140 -40 161 -46 204 -56 371 -45 779 -50 1106 -25 1654 27 603 29 621 69 740 20 58 43 132 52 164 8 32 26 75 40 95 13 19 29 63 36 96 9 46 19 67 44 91 17 17 42 50 54 74 28 54 85 132 131 180 20 20 42 47 48 58 6 12 29 31 51 42 22 11 47 31 55 44 9 14 37 34 63 46 26 12 61 37 78 55 24 25 45 36 90 45 32 6 81 23 109 37 75 39 274 80 390 82 33 0 114 -8 180 -19z"

    nonisolated(unsafe) static let sil: CGPath = glovePath(silData)
    nonisolated(unsafe) static let ink: CGPath = glovePath(inkData)
    nonisolated(unsafe) static let frames: [SkimFrame] = (0..<count).map { build(Double($0) * duration / Double(count)) }
    nonisolated(unsafe) static let dimText: CGPath = {
        let path = CGMutablePath()
        for (y, ws) in lines {
            for (a, b) in words(ws) {
                path.move(to: CGPoint(x: a, y: y))
                path.addLine(to: CGPoint(x: b, y: y))
            }
        }
        return path
    }()

    static func clamp(_ u: Double) -> Double { max(0, min(1, u)) }
    static func ease(_ u: Double) -> Double { let v = clamp(u); return v * v * (3 - 2 * v) }
    static func eout(_ u: Double) -> Double { 1 - pow(1 - clamp(u), 3) }

    static func keys(_ t: Double, _ ks: [(Double, Double)]) -> Double {
        if t <= ks[0].0 { return ks[0].1 }
        for (a, b) in zip(ks, ks.dropFirst()) where t <= b.0 {
            return a.1 + (b.1 - a.1) * ease((t - a.0) / (b.0 - a.0))
        }
        return ks[ks.count - 1].1
    }

    static func prog(_ t: Double, _ r: (Double, Double)) -> Double { clamp((t - r.0) / (r.1 - r.0)) }

    static func steps(_ u: Double) -> Double {
        let s = u * 4
        let k = Double(min(3, Int(s)))
        return u < 1 ? (k + ease((s - k) / 0.3)) / 4 : 1
    }

    static func words(_ ws: [Double]) -> [(Double, Double)] {
        var out: [(Double, Double)] = []
        var x = x0
        for w in ws {
            out.append((x, x + w))
            x += w + 5.5
        }
        return out
    }

    static func gaze(_ t: Double) -> (Double, Double) {
        for (i, r) in rs.enumerated() {
            if t < r.0 {
                if i == 0 { return (-8, gy[0]) }
                let p = rs[i - 1].1
                let j = (i - 1) % 3
                return (keys(t, [(p + 0.04, 8), (r.0 - 0.02, -8.5), (r.0, -8)]), keys(t, [(p + 0.04, gy[j]), (r.0 - 0.02, gy[i])]))
            }
            if t <= r.1 { return (-8 + 16 * steps(prog(t, r)), gy[i]) }
        }
        return (keys(t, [(3.04, 8), (3.16, -8)]), keys(t, [(3.04, gy[2]), (3.16, gy[0])]))
    }

    static func glovePath(_ d: String) -> CGPath {
        let path = CGMutablePath()
        let chars = Array(d.utf8)
        var index = 0
        var command: UInt8 = 0
        var cx = 0.0
        var cy = 0.0
        var sx = 0.0
        var sy = 0.0
        func skip() {
            while index < chars.count, chars[index] == 32 || chars[index] == 10 || chars[index] == 13 || chars[index] == 44 { index += 1 }
        }
        func number() -> Double {
            skip()
            var end = index
            if end < chars.count, chars[end] == 45 { end += 1 }
            while end < chars.count, (chars[end] >= 48 && chars[end] <= 57) || chars[end] == 46 { end += 1 }
            let value = Double(String(decoding: chars[index..<end], as: UTF8.self)) ?? 0
            index = end
            return value
        }
        func spot(_ x: Double, _ y: Double) -> CGPoint { CGPoint(x: x * 0.1, y: 1290 - y * 0.1) }
        while index < chars.count {
            skip()
            guard index < chars.count else { break }
            let c = chars[index]
            if (c >= 65 && c <= 90) || (c >= 97 && c <= 122) {
                command = c
                index += 1
                if command == 90 || command == 122 {
                    path.closeSubpath()
                    cx = sx
                    cy = sy
                }
                continue
            }
            switch command {
            case 77, 109:
                var x = number()
                var y = number()
                if command == 109 { x += cx; y += cy }
                cx = x
                cy = y
                sx = x
                sy = y
                path.move(to: spot(x, y))
                command = command == 109 ? 108 : 76
            case 67, 99:
                var v = [number(), number(), number(), number(), number(), number()]
                if command == 99 { for j in 0..<6 { v[j] += j % 2 == 0 ? cx : cy } }
                path.addCurve(to: spot(v[4], v[5]), control1: spot(v[0], v[1]), control2: spot(v[2], v[3]))
                cx = v[4]
                cy = v[5]
            case 76, 108:
                var x = number()
                var y = number()
                if command == 108 { x += cx; y += cy }
                path.addLine(to: spot(x, y))
                cx = x
                cy = y
            default:
                index += 1
            }
        }
        return path
    }

    static func build(_ t: Double) -> SkimFrame {
        let fade = 1 - ease((t - 3.02) / 0.14)
        let textInk = CGMutablePath()
        for (li, (y, ws)) in lines.enumerated() {
            let ww = words(ws)
            let rx = x0 + (ww[ww.count - 1].1 + 2.5 - x0) * prog(t, rs[li])
            for (a, b) in ww {
                let e = min(b, rx)
                if e > a + 0.3 {
                    textInk.move(to: CGPoint(x: a, y: y))
                    textInk.addLine(to: CGPoint(x: e, y: y))
                }
            }
        }

        let dy = keys(t, [(0, 0), (1.54, 0), (1.70, 6.5), (1.80, 5.6), (1.88, 6), (2.07, 6), (2.18, -3), (2.28, 1.2), (2.38, -0.5), (2.48, 0)])
        let rot = keys(t, [(0, 0), (1.54, 0), (1.70, 5), (1.80, 3.5), (2.07, 3.5), (2.18, -1.5), (2.30, 0.6), (2.42, 0)])
        let sx = keys(t, [(0, 1), (1.64, 1), (1.72, 1.05), (1.80, 0.98), (1.88, 1), (2.07, 1), (2.13, 0.96), (2.20, 0.95), (2.28, 1.03), (2.38, 0.99), (2.46, 1)])
        let sy = keys(t, [(0, 1), (1.64, 1), (1.72, 0.93), (1.80, 1.03), (1.88, 1), (2.07, 1), (2.13, 1.05), (2.20, 1.06), (2.28, 0.96), (2.38, 1.01), (2.46, 1)])
        let (gx, gyy) = gaze(t)
        let gm = CGAffineTransform(translationX: 0, y: 4)
            .concatenating(CGAffineTransform(scaleX: sx, y: sy))
            .concatenating(CGAffineTransform(translationX: 0, y: -4))
            .concatenating(CGAffineTransform(rotationAngle: rot * .pi / 180))
            .concatenating(CGAffineTransform(translationX: 60, y: 46 + dy))

        let lensRaw = CGMutablePath()
        let brows = CGMutablePath()
        let pupils = CGMutablePath()
        var lensPaths: [CGPath] = []
        for s in [-1.0, 1.0] {
            let cx = 19.5 * s
            brows.move(to: CGPoint(x: cx + 15.5 * s, y: -6))
            brows.addQuadCurve(to: CGPoint(x: cx + 23 * s, y: -7), control: CGPoint(x: cx + 20 * s, y: -9))
            let rect = CGPath(roundedRect: CGRect(x: cx - 15.5, y: -10, width: 31, height: 20), cornerWidth: 5.5, cornerHeight: 5.5, transform: nil)
            lensRaw.addPath(rect)
            lensPaths.append(rect)
            pupils.addEllipse(in: CGRect(x: cx + gx - 3.9, y: gyy - 3.9, width: 7.8, height: 7.8))
        }
        let nose = CGMutablePath()
        nose.move(to: CGPoint(x: -4.2, y: -3))
        nose.addQuadCurve(to: CGPoint(x: 4.2, y: -3), control: CGPoint(x: 0, y: -7))
        var m1 = gm
        func moved(_ p: CGPath) -> CGPath {
            var m = m1
            return p.copy(using: &m) ?? p
        }
        m1 = gm
        let lensTint = moved(lensRaw)
        let lensStroke = lensTint
        let browsT = moved(brows)
        let noseT = moved(nose)
        let pupilsT = moved(pupils)

        let silTint = CGMutablePath()
        let silStroke = CGMutablePath()
        let inkFill = CGMutablePath()
        let inkStroke = CGMutablePath()
        let burst = CGMutablePath()
        let glassMask = CGMutablePath()
        let textMask = CGMutablePath()
        let bounds = CGRect(x: -100, y: -100, width: 320, height: 320)
        glassMask.addRect(bounds)
        textMask.addRect(bounds)
        textMask.addPath(lensTint)

        let r = rot * .pi / 180
        let tipx = 60 + 24 * cos(r) - 12 * sin(r)
        let tipy = 46 + dy + 24 * sin(r) + 12 * cos(r)
        let a = keys(t, [(0, 0), (1.80, 0), (2.05, 1), (2.22, 1), (2.52, 0)])
        if a > 0.001 {
            let ang = -12.0 * .pi / 180
            let ddx = sin(-ang)
            let ddy = cos(-ang)
            let off = 62 * (1 - a)
            let x = tipx + ddx * off
            let y = tipy + ddy * off
            let sq = keys(t, [(0, 1), (2.02, 1), (2.08, 0.9), (2.16, 1.06), (2.26, 1)])
            let m = CGAffineTransform(translationX: -tip.0, y: -tip.1)
                .concatenating(CGAffineTransform(scaleX: 0.049 / sqrt(sq), y: 0.049 * sq))
                .concatenating(CGAffineTransform(rotationAngle: ang))
                .concatenating(CGAffineTransform(translationX: x, y: y))
            var tm = m
            if let p = sil.copy(using: &tm) {
                silTint.addPath(p)
                silStroke.addPath(p)
                glassMask.addPath(p)
                textMask.addPath(p)
            }
            tm = m
            if let p = ink.copy(using: &tm) {
                inkFill.addPath(p)
                inkStroke.addPath(p)
            }
        }

        let u = (t - 2.16) / 0.3
        if u > 0 && u < 1 {
            for ang in [-60.0, -90.0, -120.0] {
                let r0 = 22 + 6 * eout(u)
                let r1 = r0 + 6 * (1 - ease((u - 0.4) / 0.6))
                let ca = cos(ang * .pi / 180)
                let sa = sin(ang * .pi / 180)
                if r1 - r0 > 0.4 {
                    burst.move(to: CGPoint(x: 60 + ca * r0, y: 40 + sa * r0))
                    burst.addLine(to: CGPoint(x: 60 + ca * r1, y: 40 + sa * r1))
                }
            }
        }

        return SkimFrame(textInk: textInk, textAlpha: Float(fade), textMask: textMask, lensTint: lensTint, lensStroke: lensStroke, brows: browsT, nose: noseT, pupils: pupilsT, glassMask: glassMask, silTint: silTint, silStroke: silStroke, inkFill: inkFill, inkStroke: inkStroke, burst: burst)
    }
}

extension FlairLayer {
    func buildSkim() {
        let unit = side
        let k = unit / 110
        let frames = SkimModel.frames
        let duration = SkimModel.duration
        let steps = SkimModel.count
        var map = CGAffineTransform(a: k, b: 0, c: 0, d: -k, tx: -5 * k, ty: unit + 5 * k)
        func placed(_ path: CGPath) -> CGPath { path.copy(using: &map) ?? path }
        let full = CGSize(width: unit, height: unit)
        let mid = CGPoint(x: unit / 2, y: unit / 2)
        let times = (0..<steps).map { NSNumber(value: Double($0) / Double(steps)) }
        func discrete(_ keyPath: String, _ values: [Any]) -> CAKeyframeAnimation {
            let animation = keyframes(keyPath, values, times: times, timing: .linear)
            animation.calculationMode = .discrete
            return animation
        }
        func paths(_ layer: CAShapeLayer, _ pick: (SkimFrame) -> CGPath) {
            layer.path = placed(pick(frames[0]))
            guard animating else { return }
            run(group([discrete("path", frames.map { placed(pick($0)) })], duration: duration), on: layer)
        }
        func masked(_ parent: CALayer, _ pick: @escaping (SkimFrame) -> CGPath) -> CALayer {
            let layer = container()
            layer.removeFromSuperlayer()
            parent.addSublayer(layer)
            let cover = CAShapeLayer()
            cover.bounds = CGRect(origin: .zero, size: full)
            cover.position = mid
            cover.fillRule = .evenOdd
            cover.fillColor = CGColor(gray: 0, alpha: 1)
            cover.path = placed(pick(frames[0]))
            layer.mask = cover
            if animating {
                run(group([discrete("path", frames.map { placed(pick($0)) })], duration: duration), on: cover)
            }
            return layer
        }

        let clipper = container()
        let tile = CAShapeLayer()
        tile.bounds = CGRect(origin: .zero, size: full)
        tile.position = mid
        tile.path = CGPath(roundedRect: CGRect(origin: .zero, size: full), cornerWidth: 22 * k, cornerHeight: 22 * k, transform: nil)
        tile.fillColor = CGColor(gray: 0, alpha: 1)
        clipper.mask = tile

        let textLayer = masked(clipper) { $0.textMask }
        let dim = shape(placed(SkimModel.dimText), size: full, at: mid, stroke: 4.4 * k, in: textLayer)
        dim.lineCap = .round
        dim.opacity = 0.32
        let ink = shape(placed(frames[0].textInk), size: full, at: mid, stroke: 4.4 * k, in: textLayer)
        ink.lineCap = .round
        if animating {
            run(group([discrete("path", frames.map { placed($0.textInk) }), discrete("opacity", frames.map { NSNumber(value: $0.textAlpha) })], duration: duration), on: ink)
        }

        let glasses = masked(clipper) { $0.glassMask }
        let lensTint = shape(placed(frames[0].lensTint), size: full, at: mid, in: glasses)
        lensTint.opacity = 0.22
        paths(lensTint) { $0.lensTint }
        let lensStroke = shape(placed(frames[0].lensStroke), size: full, at: mid, stroke: 3.6 * k, in: glasses)
        paths(lensStroke) { $0.lensStroke }
        let brows = shape(placed(frames[0].brows), size: full, at: mid, stroke: 3.4 * k, in: glasses)
        brows.lineCap = .round
        paths(brows) { $0.brows }
        let nose = shape(placed(frames[0].nose), size: full, at: mid, stroke: 3.6 * k, in: glasses)
        nose.lineCap = .round
        paths(nose) { $0.nose }
        let pupils = shape(placed(frames[0].pupils), size: full, at: mid, in: glasses)
        paths(pupils) { $0.pupils }

        let silTint = shape(placed(frames[0].silTint), size: full, at: mid, in: clipper)
        silTint.opacity = 0.22
        paths(silTint) { $0.silTint }
        let gloveWidth = 480 * 0.1 * 0.049 * k
        let silStroke = shape(placed(frames[0].silStroke), size: full, at: mid, stroke: gloveWidth, in: clipper)
        silStroke.lineJoin = .round
        paths(silStroke) { $0.silStroke }
        let inkFill = shape(placed(frames[0].inkFill), size: full, at: mid, in: clipper)
        paths(inkFill) { $0.inkFill }
        let inkStroke = shape(placed(frames[0].inkStroke), size: full, at: mid, stroke: gloveWidth, in: clipper)
        inkStroke.lineJoin = .round
        paths(inkStroke) { $0.inkStroke }
        let burst = shape(placed(frames[0].burst), size: full, at: mid, stroke: 3 * k, in: clipper)
        burst.lineCap = .round
        paths(burst) { $0.burst }
    }
}
