SizeExtensions.swift 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. //
  2. // SizeExtensions.swift
  3. // Kingfisher
  4. //
  5. // Created by onevcat on 2018/09/28.
  6. //
  7. // Copyright (c) 2018 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: KingfisherStructCompatible {}
  28. extension KingfisherStruct where Base == CGSize {
  29. public func resize(to size: CGSize, for contentMode: ContentMode) -> CGSize {
  30. switch contentMode {
  31. case .aspectFit:
  32. return constrained(size)
  33. case .aspectFill:
  34. return filling(size)
  35. case .none:
  36. return base
  37. }
  38. }
  39. public func constrained(_ size: CGSize) -> CGSize {
  40. let aspectWidth = round(aspectRatio * size.height)
  41. let aspectHeight = round(size.width / aspectRatio)
  42. return aspectWidth > size.width ?
  43. CGSize(width: size.width, height: aspectHeight) :
  44. CGSize(width: aspectWidth, height: size.height)
  45. }
  46. public func filling(_ size: CGSize) -> CGSize {
  47. let aspectWidth = round(aspectRatio * size.height)
  48. let aspectHeight = round(size.width / aspectRatio)
  49. return aspectWidth < size.width ?
  50. CGSize(width: size.width, height: aspectHeight) :
  51. CGSize(width: aspectWidth, height: size.height)
  52. }
  53. public func constrainedRect(for size: CGSize, anchor: CGPoint) -> CGRect {
  54. let unifiedAnchor = CGPoint(x: anchor.x.clamped(to: 0.0...1.0),
  55. y: anchor.y.clamped(to: 0.0...1.0))
  56. let x = unifiedAnchor.x * base.width - unifiedAnchor.x * size.width
  57. let y = unifiedAnchor.y * base.height - unifiedAnchor.y * size.height
  58. let r = CGRect(x: x, y: y, width: size.width, height: size.height)
  59. let ori = CGRect(origin: .zero, size: base)
  60. return ori.intersection(r)
  61. }
  62. private var aspectRatio: CGFloat {
  63. return base.height == 0.0 ? 1.0 : base.width / base.height
  64. }
  65. }
  66. extension CGRect {
  67. func scaled(_ scale: CGFloat) -> CGRect {
  68. return CGRect(x: origin.x * scale, y: origin.y * scale,
  69. width: size.width * scale, height: size.height * scale)
  70. }
  71. }
  72. extension Comparable {
  73. func clamped(to limits: ClosedRange<Self>) -> Self {
  74. return min(max(self, limits.lowerBound), limits.upperBound)
  75. }
  76. }