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.

110 lines
4.5 KiB

  1. //
  2. // SizeExtensions.swift
  3. // Kingfisher
  4. //
  5. // Created by onevcat on 2018/09/28.
  6. //
  7. // Copyright (c) 2019 Wei Wang <onevcat@gmail.com>
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the "Software"), to deal
  11. // in the Software without restriction, including without limitation the rights
  12. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. // THE SOFTWARE.
  26. import CoreGraphics
  27. extension CGSize: KingfisherCompatibleValue {}
  28. extension KingfisherWrapper where Base == CGSize {
  29. /// Returns a size by resizing the `base` size to a target size under a given content mode.
  30. ///
  31. /// - Parameters:
  32. /// - size: The target size to resize to.
  33. /// - contentMode: Content mode of the target size should be when resizing.
  34. /// - Returns: The resized size under the given `ContentMode`.
  35. public func resize(to size: CGSize, for contentMode: ContentMode) -> CGSize {
  36. switch contentMode {
  37. case .aspectFit:
  38. return constrained(size)
  39. case .aspectFill:
  40. return filling(size)
  41. case .none:
  42. return size
  43. }
  44. }
  45. /// Returns a size by resizing the `base` size by making it aspect fitting the given `size`.
  46. ///
  47. /// - Parameter size: The size in which the `base` should fit in.
  48. /// - Returns: The size fitted in by the input `size`, while keeps `base` aspect.
  49. public func constrained(_ size: CGSize) -> CGSize {
  50. let aspectWidth = round(aspectRatio * size.height)
  51. let aspectHeight = round(size.width / aspectRatio)
  52. return aspectWidth > size.width ?
  53. CGSize(width: size.width, height: aspectHeight) :
  54. CGSize(width: aspectWidth, height: size.height)
  55. }
  56. /// Returns a size by resizing the `base` size by making it aspect filling the given `size`.
  57. ///
  58. /// - Parameter size: The size in which the `base` should fill.
  59. /// - Returns: The size be filled by the input `size`, while keeps `base` aspect.
  60. public func filling(_ size: CGSize) -> CGSize {
  61. let aspectWidth = round(aspectRatio * size.height)
  62. let aspectHeight = round(size.width / aspectRatio)
  63. return aspectWidth < size.width ?
  64. CGSize(width: size.width, height: aspectHeight) :
  65. CGSize(width: aspectWidth, height: size.height)
  66. }
  67. /// Returns a `CGRect` for which the `base` size is constrained to an input `size` at a given `anchor` point.
  68. ///
  69. /// - Parameters:
  70. /// - size: The size in which the `base` should be constrained to.
  71. /// - anchor: An anchor point in which the size constraint should happen.
  72. /// - Returns: The result `CGRect` for the constraint operation.
  73. public func constrainedRect(for size: CGSize, anchor: CGPoint) -> CGRect {
  74. let unifiedAnchor = CGPoint(x: anchor.x.clamped(to: 0.0...1.0),
  75. y: anchor.y.clamped(to: 0.0...1.0))
  76. let x = unifiedAnchor.x * base.width - unifiedAnchor.x * size.width
  77. let y = unifiedAnchor.y * base.height - unifiedAnchor.y * size.height
  78. let r = CGRect(x: x, y: y, width: size.width, height: size.height)
  79. let ori = CGRect(origin: .zero, size: base)
  80. return ori.intersection(r)
  81. }
  82. private var aspectRatio: CGFloat {
  83. return base.height == 0.0 ? 1.0 : base.width / base.height
  84. }
  85. }
  86. extension CGRect {
  87. func scaled(_ scale: CGFloat) -> CGRect {
  88. return CGRect(x: origin.x * scale, y: origin.y * scale,
  89. width: size.width * scale, height: size.height * scale)
  90. }
  91. }
  92. extension Comparable {
  93. func clamped(to limits: ClosedRange<Self>) -> Self {
  94. return min(max(self, limits.lowerBound), limits.upperBound)
  95. }
  96. }