You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123 lines
3.1 KiB

2 years ago
2 years ago
2 years ago
  1. //
  2. // TextDocument.swift
  3. // lottie-swift
  4. //
  5. // Created by Brandon Withrow on 1/9/19.
  6. //
  7. import Foundation
  8. // MARK: - TextJustification
  9. enum TextJustification: Int, Codable {
  10. case left
  11. case right
  12. case center
  13. }
  14. // MARK: - TextDocument
  15. final class TextDocument: Codable, DictionaryInitializable, AnyInitializable {
  16. // MARK: Lifecycle
  17. init(dictionary: [String: Any]) throws {
  18. text = try dictionary.value(for: CodingKeys.text)
  19. fontSize = try dictionary.value(for: CodingKeys.fontSize)
  20. fontFamily = try dictionary.value(for: CodingKeys.fontFamily)
  21. let justificationValue: Int = try dictionary.value(for: CodingKeys.justification)
  22. guard let justification = TextJustification(rawValue: justificationValue) else {
  23. throw InitializableError.invalidInput
  24. }
  25. self.justification = justification
  26. tracking = try dictionary.value(for: CodingKeys.tracking)
  27. lineHeight = try dictionary.value(for: CodingKeys.lineHeight)
  28. baseline = try dictionary.value(for: CodingKeys.baseline)
  29. if let fillColorRawValue = dictionary[CodingKeys.fillColorData.rawValue] {
  30. fillColorData = try? Color(value: fillColorRawValue)
  31. } else {
  32. fillColorData = nil
  33. }
  34. if let strokeColorRawValue = dictionary[CodingKeys.strokeColorData.rawValue] {
  35. strokeColorData = try? Color(value: strokeColorRawValue)
  36. } else {
  37. strokeColorData = nil
  38. }
  39. strokeWidth = try? dictionary.value(for: CodingKeys.strokeWidth)
  40. strokeOverFill = try? dictionary.value(for: CodingKeys.strokeOverFill)
  41. if let textFramePositionRawValue = dictionary[CodingKeys.textFramePosition.rawValue] {
  42. textFramePosition = try? Vector3D(value: textFramePositionRawValue)
  43. } else {
  44. textFramePosition = nil
  45. }
  46. if let textFrameSizeRawValue = dictionary[CodingKeys.textFrameSize.rawValue] {
  47. textFrameSize = try? Vector3D(value: textFrameSizeRawValue)
  48. } else {
  49. textFrameSize = nil
  50. }
  51. }
  52. convenience init(value: Any) throws {
  53. guard let dictionary = value as? [String: Any] else {
  54. throw InitializableError.invalidInput
  55. }
  56. try self.init(dictionary: dictionary)
  57. }
  58. // MARK: Internal
  59. /// The Text
  60. let text: String
  61. /// The Font size
  62. let fontSize: Double
  63. /// The Font Family
  64. let fontFamily: String
  65. /// Justification
  66. let justification: TextJustification
  67. /// Tracking
  68. let tracking: Int
  69. /// Line Height
  70. let lineHeight: Double
  71. /// Baseline
  72. let baseline: Double?
  73. /// Fill Color data
  74. let fillColorData: Color?
  75. /// Scroke Color data
  76. let strokeColorData: Color?
  77. /// Stroke Width
  78. let strokeWidth: Double?
  79. /// Stroke Over Fill
  80. let strokeOverFill: Bool?
  81. let textFramePosition: Vector3D?
  82. let textFrameSize: Vector3D?
  83. // MARK: Private
  84. private enum CodingKeys: String, CodingKey {
  85. case text = "t"
  86. case fontSize = "s"
  87. case fontFamily = "f"
  88. case justification = "j"
  89. case tracking = "tr"
  90. case lineHeight = "lh"
  91. case baseline = "ls"
  92. case fillColorData = "fc"
  93. case strokeColorData = "sc"
  94. case strokeWidth = "sw"
  95. case strokeOverFill = "of"
  96. case textFramePosition = "ps"
  97. case textFrameSize = "sz"
  98. }
  99. }