CTileWatermarkPreView.swift 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. //
  2. // CTileWatermarkPreView.swift
  3. // PDFViewer-Swift
  4. //
  5. // Copyright © 2014-2024 PDF Technologies, Inc. All Rights Reserved.
  6. //
  7. // THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
  8. // AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
  9. // UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
  10. // This notice may not be removed from this file.
  11. //
  12. import UIKit
  13. import ComPDFKit
  14. class CTileWatermarkPreView: UIView {
  15. var waterString: String?
  16. var waterImageView: UIImageView?
  17. var horizontalSpacing: CGFloat = 0
  18. var verticalSpacing: CGFloat = 0
  19. var stransform: CGAffineTransform = .identity
  20. var fontColor: UIColor = .black
  21. var fontSize: CGFloat = 0
  22. var familyName: String?
  23. var fontStyleName: String?
  24. var centerPoint: CGPoint = .zero
  25. override init(frame: CGRect) {
  26. waterString = NSLocalizedString("Watermark", comment: "")
  27. horizontalSpacing = 30
  28. verticalSpacing = 30
  29. fontSize = 20
  30. fontColor = UIColor.black
  31. stransform = CGAffineTransform.identity
  32. familyName = "Helvetica"
  33. fontStyleName = "Regular"
  34. super.init(frame: frame)
  35. backgroundColor = .clear
  36. }
  37. required init?(coder aDecoder: NSCoder) {
  38. fatalError("init(coder:) has not been implemented")
  39. }
  40. override func draw(_ rect: CGRect) {
  41. super.draw(rect)
  42. let w = rect.size.width
  43. let h = rect.size.height
  44. guard let context = UIGraphicsGetCurrentContext() else { return }
  45. let width = sqrt(rect.size.height * rect.size.height + rect.size.width * rect.size.width)
  46. let newRect = CGRect(x: -(width - rect.size.width)/2, y: -(width - rect.size.height)/2, width: width, height: width)
  47. let new_w = newRect.size.width
  48. let new_h = newRect.size.height
  49. if waterString != nil && (waterImageView == nil) {
  50. context.translateBy(x: centerPoint.x, y: centerPoint.y)
  51. context.concatenate(stransform)
  52. context.translateBy(x: -(centerPoint.x), y: -(centerPoint.y))
  53. let cfont = CPDFFont(familyName: familyName ?? "Helvetica", fontStyle: fontStyleName ?? "")
  54. let font = UIFont(name: CPDFFont.convertAppleFont(cfont) ?? "Helvetica", size: fontSize)
  55. guard let contentRealSizes = waterString?.boundingRect(with: CGSize(width: new_w, height: new_h), options: .usesLineFragmentOrigin, attributes: [NSAttributedString.Key.font: font ?? UIFont.systemFont(ofSize: fontSize) ], context: nil).size else {
  56. return
  57. }
  58. let verticalWidth = contentRealSizes.width + horizontalSpacing
  59. let horizontalHeight = contentRealSizes.height + verticalSpacing
  60. var line = Int(new_h - verticalSpacing) / Int(horizontalHeight)
  61. var row = Int(new_w - horizontalSpacing) / Int(verticalWidth)
  62. if CGFloat(fmod(h, horizontalHeight)) != 0 {
  63. line += 1
  64. }
  65. if CGFloat(fmod(w, verticalWidth)) != 0 {
  66. row += 1
  67. }
  68. let attributes: [NSAttributedString.Key: Any] = [
  69. NSAttributedString.Key.font: font!,
  70. NSAttributedString.Key.foregroundColor: fontColor
  71. ]
  72. let point = CGPoint(x: centerPoint.x - contentRealSizes.width/2, y: centerPoint.y - contentRealSizes.height/2)
  73. for i in 0..<line {
  74. for j in 0..<row {
  75. waterString?.draw(in: CGRect(x: point.x + CGFloat(j) * verticalWidth, y: point.y + CGFloat(i) * horizontalHeight, width: contentRealSizes.width, height: contentRealSizes.height), withAttributes: attributes)
  76. }
  77. }
  78. for i in 1..<line {
  79. for j in 0..<row {
  80. waterString?.draw(in: CGRect(x: point.x + CGFloat(j) * verticalWidth, y: point.y - CGFloat(i) * horizontalHeight, width: contentRealSizes.width, height: contentRealSizes.height), withAttributes: attributes)
  81. }
  82. }
  83. for i in 0..<line {
  84. for j in 1..<row {
  85. waterString?.draw(in: CGRect(x: point.x - CGFloat(j) * verticalWidth, y: point.y + CGFloat(i) * horizontalHeight, width: contentRealSizes.width, height: contentRealSizes.height), withAttributes: [NSAttributedString.Key.font: font ?? UIFont(), NSAttributedString.Key.foregroundColor: fontColor])
  86. }
  87. }
  88. for i in 1..<line {
  89. for j in 1..<row {
  90. waterString?.draw(in: CGRect(x: point.x - CGFloat(j) * verticalWidth, y: point.y - CGFloat(i) * horizontalHeight, width: contentRealSizes.width, height: contentRealSizes.height), withAttributes: [NSAttributedString.Key.font: font ?? UIFont(), NSAttributedString.Key.foregroundColor: fontColor])
  91. }
  92. }
  93. } else if (waterImageView != nil) && waterImageView?.image?.size.width ?? 0 > 0 && waterImageView?.image?.size.height ?? 0 > 0 {
  94. context.translateBy(x: centerPoint.x, y: centerPoint.y)
  95. context.concatenate(stransform)
  96. context.scaleBy(x: 1, y: -1)
  97. context.translateBy(x: -(centerPoint.x), y: -(centerPoint.y))
  98. context.setAlpha(waterImageView?.alpha ?? 0)
  99. let verticalWidth = (waterImageView?.bounds.size.width ?? 0) + horizontalSpacing
  100. let horizontalHeight = (waterImageView?.bounds.size.height ?? 0) + verticalSpacing
  101. var line = Int(new_h - verticalSpacing) / Int(horizontalHeight)
  102. var row = Int(new_w - horizontalSpacing) / Int(verticalWidth)
  103. if CGFloat(fmod(h, horizontalHeight)) != 0 {
  104. line += 1
  105. }
  106. if CGFloat(fmod(w, verticalWidth)) != 0 {
  107. row += 1
  108. }
  109. let point = CGPoint(x: centerPoint.x - (waterImageView?.bounds.size.width ?? 0)/2, y: centerPoint.y - (waterImageView?.bounds.size.height ?? 0)/2)
  110. for i in 0..<line {
  111. for j in 0..<row {
  112. let area = CGRect(x: point.x + CGFloat(j) * verticalWidth, y: point.y + CGFloat(i) * horizontalHeight, width: waterImageView?.bounds.size.width ?? 0, height: waterImageView?.bounds.size.height ?? 0)
  113. context.draw((waterImageView?.image!.cgImage)!, in: area)
  114. }
  115. }
  116. for i in 1..<line {
  117. for j in 0..<row {
  118. let area = CGRect(x: point.x + CGFloat(j) * verticalWidth, y: point.y - CGFloat(i) * horizontalHeight, width: waterImageView?.bounds.size.width ?? 0, height: waterImageView?.bounds.size.height ?? 0)
  119. context.draw((waterImageView?.image!.cgImage)!, in: area)
  120. }
  121. }
  122. for i in 0..<line {
  123. for j in 1..<row {
  124. let area = CGRect(x: point.x - CGFloat(j) * verticalWidth, y: point.y + CGFloat(i) * horizontalHeight, width: waterImageView?.bounds.size.width ?? 0, height: waterImageView?.bounds.size.height ?? 0)
  125. context.draw((waterImageView?.image!.cgImage)!, in: area)
  126. }
  127. }
  128. for i in 1..<line {
  129. for j in 1..<row {
  130. let area = CGRect(x: point.x - CGFloat(j) * verticalWidth, y: point.y - CGFloat(i) * horizontalHeight, width: waterImageView?.bounds.size.width ?? 0, height: waterImageView?.bounds.size.height ?? 0)
  131. context.draw((waterImageView?.image!.cgImage)!, in: area)
  132. }
  133. }
  134. }
  135. }
  136. // Helper function to convert degrees to radians
  137. func radians(_ degrees: Double) -> Double {
  138. return degrees * .pi / 180
  139. }
  140. }