1 //
2 //  TextAnimator.swift
3 //  lottie-swift
4 //
5 //  Created by Brandon Withrow on 1/9/19.
6 //
7 
8 import Foundation
9 
10 final class TextAnimator: Codable {
11 
12   let name: String
13 
14   /// Anchor
15   let anchor: KeyframeGroup<Vector3D>?
16 
17   /// Position
18   let position: KeyframeGroup<Vector3D>?
19 
20   /// Scale
21   let scale: KeyframeGroup<Vector3D>?
22 
23   /// Skew
24   let skew: KeyframeGroup<Vector1D>?
25 
26   /// Skew Axis
27   let skewAxis: KeyframeGroup<Vector1D>?
28 
29   /// Rotation
30   let rotation: KeyframeGroup<Vector1D>?
31 
32   /// Opacity
33   let opacity: KeyframeGroup<Vector1D>?
34 
35   /// Stroke Color
36   let strokeColor: KeyframeGroup<Color>?
37 
38   /// Fill Color
39   let fillColor: KeyframeGroup<Color>?
40 
41   /// Stroke Width
42   let strokeWidth: KeyframeGroup<Vector1D>?
43 
44   /// Tracking
45   let tracking: KeyframeGroup<Vector1D>?
46 
47   private enum CodingKeys: String, CodingKey {
48 //    case textSelector = "s" TODO
49     case textAnimator = "a"
50     case name = "nm"
51   }
52 
53   private enum TextSelectorKeys: String, CodingKey {
54     case start = "s"
55     case end = "e"
56     case offset = "o"
57   }
58 
59   private enum TextAnimatorKeys: String, CodingKey {
60     case fillColor = "fc"
61     case strokeColor = "sc"
62     case strokeWidth = "sw"
63     case tracking = "t"
64     case anchor = "a"
65     case position = "p"
66     case scale = "s"
67     case skew = "sk"
68     case skewAxis = "sa"
69     case rotation = "r"
70     case opacity = "o"
71   }
72 
73   required init(from decoder: Decoder) throws {
74     let container = try decoder.container(keyedBy: TextAnimator.CodingKeys.self)
75     self.name = try container.decodeIfPresent(String.self, forKey: .name) ?? ""
76     let animatorContainer = try container.nestedContainer(keyedBy: TextAnimatorKeys.self, forKey: .textAnimator)
77     self.fillColor = try animatorContainer.decodeIfPresent(KeyframeGroup<Color>.self, forKey: .fillColor)
78     self.strokeColor = try animatorContainer.decodeIfPresent(KeyframeGroup<Color>.self, forKey: .strokeColor)
79     self.strokeWidth = try animatorContainer.decodeIfPresent(KeyframeGroup<Vector1D>.self, forKey: .strokeWidth)
80     self.tracking = try animatorContainer.decodeIfPresent(KeyframeGroup<Vector1D>.self, forKey: .tracking)
81     self.anchor = try animatorContainer.decodeIfPresent(KeyframeGroup<Vector3D>.self, forKey: .anchor)
82     self.position = try animatorContainer.decodeIfPresent(KeyframeGroup<Vector3D>.self, forKey: .position)
83     self.scale = try animatorContainer.decodeIfPresent(KeyframeGroup<Vector3D>.self, forKey: .scale)
84     self.skew = try animatorContainer.decodeIfPresent(KeyframeGroup<Vector1D>.self, forKey: .skew)
85     self.skewAxis = try animatorContainer.decodeIfPresent(KeyframeGroup<Vector1D>.self, forKey: .skewAxis)
86     self.rotation = try animatorContainer.decodeIfPresent(KeyframeGroup<Vector1D>.self, forKey: .rotation)
87     self.opacity = try animatorContainer.decodeIfPresent(KeyframeGroup<Vector1D>.self, forKey: .opacity)
88 
89   }
90 
encodenull91   func encode(to encoder: Encoder) throws {
92     var container = encoder.container(keyedBy: CodingKeys.self)
93     var animatorContainer = container.nestedContainer(keyedBy: TextAnimatorKeys.self, forKey: .textAnimator)
94     try animatorContainer.encodeIfPresent(fillColor, forKey: .fillColor)
95     try animatorContainer.encodeIfPresent(strokeColor, forKey: .strokeColor)
96     try animatorContainer.encodeIfPresent(strokeWidth, forKey: .strokeWidth)
97     try animatorContainer.encodeIfPresent(tracking, forKey: .tracking)
98   }
99 }
100