123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590 |
- //
- // CPDFSampleView.swift
- // ComPDFKit_Tools
- //
- // Copyright © 2014-2024 PDF Technologies, Inc. All Rights Reserved.
- //
- // THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
- // AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
- // UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
- // This notice may not be removed from this file.
- //
- import UIKit
- import ComPDFKit
- enum CPDFSamplesSelectedIndex: Int {
- case note = 0
- case highlight
- case underline
- case strikeout
- case squiggly
- case freehand
- case shapeCircle
- case shapeSquare
- case shapeArrow
- case shapeLine
- case freeText
- case signature
- case stamp
- case image
- case link
- case sound
- }
- enum CPDFArrowStyle: Int {
- case none = 0
- case openArrow = 1
- case closedArrow = 2
- case square = 3
- case circle = 4
- case diamond = 5
- }
- class CPDFSampleView: UIView {
-
- var selecIndex:CPDFSamplesSelectedIndex = .highlight
- var startArrowStyleIndex:CPDFArrowStyle = .none
- var endArrowStyleIndex:CPDFArrowStyle = .none
-
- var color:UIColor?
- var interiorColor:UIColor?
- var opcity:CGFloat = 0
- var thickness:CGFloat = 0
- var dotted:CGFloat = 0
- var familyName:String = "Helvetica"
- var styleName:String = ""
- var textAlignment:NSTextAlignment = .center
-
- var centerRect:CGRect = CGRect.zero
- var arrowRect:CGRect = CGRect.zero
- var textRect:CGRect = CGRect.zero
- var inkRect:CGRect = CGRect.zero
-
- override init(frame: CGRect) {
- super.init(frame: frame)
- }
-
- required init?(coder: NSCoder) {
- fatalError("init(coder:) has not been implemented")
- }
-
- override func layoutSubviews() {
- super.layoutSubviews()
- setNeedsDisplay()
- }
-
- override func draw(_ rect: CGRect) {
- super.draw(rect)
-
- guard let context = UIGraphicsGetCurrentContext() else { return }
-
- centerRect = rect.insetBy(dx: (bounds.size.width/20)*9, dy: (bounds.size.height/8)*3)
- let centerPoint = CGPoint(x: rect.midX, y: rect.midY)
- arrowRect = CGRect(x: centerPoint.x-bounds.size.height/4, y: centerPoint.y-bounds.size.height/4, width: bounds.size.height/2, height: bounds.size.height/2)
-
- textRect = rect.insetBy(dx: bounds.size.width/3+3, dy: bounds.size.height/3)
- inkRect = rect.insetBy(dx: bounds.size.width/4, dy: bounds.size.height/3)
-
- context.setFillColor(CPDFColorUtils.CAnnotationSampleDrawBackgoundColor().cgColor)
- context.fill(rect)
-
- drawSamples(context: context, rect: centerRect)
- }
-
- func drawSamples(context: CGContext, rect: CGRect) {
-
- switch selecIndex {
- case .note:
- context.setStrokeColor(red: 0, green: 0, blue: 0, alpha: 1.0)
-
- if let color = self.color {
- var red: CGFloat = 0, green: CGFloat = 0, blue: CGFloat = 0, alpha: CGFloat = 0
- color.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
- let updatedColor = UIColor(red: red, green: green, blue: blue, alpha: self.opcity)
- context.setFillColor(updatedColor.cgColor)
- } else {
- context.setStrokeColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 1.0)
- context.setFillColor(UIColor.clear.cgColor)
- }
-
- // Draw outer boxes.
- let width: CGFloat = 1.0
- let size: CGFloat = rect.size.height / 5
- var outerRect1 = rect.insetBy(dx: 0, dy: 0)
- outerRect1.size.height -= size
- var outerRect2 = outerRect1
- outerRect2.origin.x += size
- outerRect2.origin.y += size*4
- outerRect2.size.width = size
- outerRect2.size.height = size
-
- context.setLineWidth(width)
- context.move(to: CGPoint(x: outerRect1.minX, y: outerRect1.minY))
- context.addLine(to: CGPoint(x: outerRect1.minX, y: outerRect1.maxY))
- context.addLine(to: CGPoint(x: outerRect2.minX, y: outerRect2.minY))
- context.addLine(to: CGPoint(x: outerRect2.midX, y: outerRect2.maxY))
- context.addLine(to: CGPoint(x: outerRect2.midX, y: outerRect2.minY))
- context.addLine(to: CGPoint(x: outerRect1.maxX, y: outerRect1.maxY))
- context.addLine(to: CGPoint(x: outerRect1.maxX, y: outerRect1.minY))
- context.closePath()
- context.drawPath(using: .fillStroke)
-
- // Draw inner lines.
- let count = 3
- let xDelta = rect.size.width / 10
- let yDelta = outerRect1.size.height / CGFloat(count + 1)
-
- var lineRect = outerRect1
- lineRect.origin.x += xDelta
- lineRect.size.width -= 2*xDelta
-
- for i in 0..<count {
- let y = lineRect.maxY - yDelta * CGFloat(i + 1)
- context.move(to: CGPoint(x: lineRect.minX, y: y))
- context.addLine(to: CGPoint(x: lineRect.maxX, y: y))
- context.strokePath()
- }
-
- case .highlight:
- let colorComponents = self.color?.cgColor.components
- let red = colorComponents?[0]
- let green = colorComponents?[1]
- let blue = colorComponents?[2]
-
- let fillColor = UIColor(red: red ?? 0, green: green ?? 0, blue: blue ?? 0, alpha: self.opcity).cgColor
- context.setFillColor(fillColor)
- context.fill(self.textRect)
- let sampleStr = "Sample"
- let font = UIFont.systemFont(ofSize: 27)
- let attributes: [NSAttributedString.Key: Any] = [.font: font, .foregroundColor: UIColor.black]
- sampleStr.draw(in: self.textRect, withAttributes: attributes)
-
- case .underline:
- let sampleStr = "Sample"
- let colorComponents = self.color?.cgColor.components
- let red = colorComponents?[0]
- let green = colorComponents?[1]
- let blue = colorComponents?[2]
-
- let fillColor = UIColor(red: red ?? 0, green: green ?? 0, blue: blue ?? 0, alpha: self.opcity).cgColor
- context.setFillColor(fillColor)
- let strikeoutRect = self.textRect.insetBy(dx: 0, dy: (self.textRect.size.height/7) * 3)
- let underLineRect = strikeoutRect.offsetBy(dx: 0, dy: (self.textRect.size.height/7) * 3)
- context.fill(underLineRect)
- let font = UIFont.systemFont(ofSize: 27)
- let attributes: [NSAttributedString.Key: Any] = [.font: font, .foregroundColor: UIColor.black]
- sampleStr.draw(in: self.textRect, withAttributes: attributes)
-
- case .strikeout:
- let sampleStr = "Sample"
- var red: CGFloat = 0
- var green: CGFloat = 0
- var blue: CGFloat = 0
- var alpha: CGFloat = 0
- self.color?.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
- let fillColor = UIColor(red: red, green: green, blue: blue, alpha: self.opcity).cgColor
- context.setFillColor(fillColor)
- let strikeoutRect = self.textRect.insetBy(dx: 0, dy: (self.textRect.size.height/7)*3)
- let underLineRect = strikeoutRect.offsetBy(dx: 0, dy: (self.textRect.size.height/7))
- context.fill(underLineRect)
- let font = UIFont.systemFont(ofSize: 27)
- let attributes: [NSAttributedString.Key: Any] = [.font: font, .foregroundColor: UIColor.black]
- sampleStr.draw(in: self.textRect, withAttributes: attributes)
-
- case .squiggly:
- let sampleStr = "Sample"
- var red: CGFloat = 0
- var green: CGFloat = 0
- var blue: CGFloat = 0
- var alpha: CGFloat = 0
- self.color?.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
- context.setStrokeColor(UIColor(red: red, green: green, blue: blue, alpha: opcity).cgColor)
- let tWidth = self.textRect.size.width / 12.0
- context.move(to: CGPoint(x: self.textRect.minX, y: self.textRect.maxY))
- context.setLineWidth(2.0)
-
- context.addCurve(to: CGPoint(x: self.textRect.minX + tWidth, y: self.textRect.maxY + 5),
- control1: CGPoint(x: self.textRect.minX + tWidth * 2.0, y: self.textRect.maxY - 5),
- control2: CGPoint(x: self.textRect.minX + tWidth * 3.0, y: self.textRect.maxY))
- context.addCurve(to: CGPoint(x: self.textRect.minX + tWidth * 3.0, y: self.textRect.maxY),
- control1: CGPoint(x: self.textRect.minX + tWidth * 4.0, y: self.textRect.maxY + 5),
- control2: CGPoint(x: self.textRect.minX + tWidth * 5.0, y: self.textRect.maxY - 5))
- context.addCurve(to: CGPoint(x: self.textRect.minX + tWidth * 6.0, y: self.textRect.maxY),
- control1: CGPoint(x: self.textRect.minX + tWidth * 7.0, y: self.textRect.maxY + 5),
- control2: CGPoint(x: self.textRect.minX + tWidth * 8.0, y: self.textRect.maxY - 5))
- context.addCurve(to: CGPoint(x: self.textRect.minX + tWidth * 9.0, y: self.textRect.maxY),
- control1: CGPoint(x: self.textRect.minX + tWidth * 10.0, y: self.textRect.maxY + 5),
- control2: CGPoint(x: self.textRect.minX + tWidth * 11.0, y: self.textRect.maxY - 5))
- context.addCurve(to: CGPoint(x: self.textRect.minX + tWidth * 12.0, y: self.textRect.maxY),
- control1: CGPoint(x: self.textRect.minX + tWidth * 12.0, y: self.textRect.maxY),
- control2: CGPoint(x: self.textRect.minX + tWidth * 12.0, y: self.textRect.maxY))
- context.strokePath()
- let font = UIFont.systemFont(ofSize: 27)
- let attributes: [NSAttributedString.Key: Any] = [.font: font, .foregroundColor: UIColor.black]
- sampleStr.draw(in: self.textRect, withAttributes: attributes)
- case .freehand:
- let colorComponents = self.color?.cgColor.components
- let red = colorComponents?[0]
- let green = colorComponents?[1]
- let blue = colorComponents?[2]
-
- let strokeColor = UIColor(red: red ?? 0, green: green ?? 0, blue: blue ?? 0, alpha: self.opcity).cgColor
- context.setStrokeColor(strokeColor)
- let tWidth = self.inkRect.size.width / 3.0
- context.move(to: CGPoint(x: self.inkRect.minX, y: self.inkRect.midY))
- context.setLineWidth(self.thickness)
- context.addCurve(to: CGPoint(x: self.inkRect.minX + tWidth * 3.0, y: self.inkRect.midY),
- control1: CGPoint(x: self.inkRect.minX + tWidth, y: self.inkRect.midY - 20),
- control2: CGPoint(x: self.inkRect.minX + tWidth * 2.0, y: self.inkRect.midY + 20))
- context.strokePath()
-
- case .shapeCircle:
- let colorComponents = self.color?.cgColor.components
- let red = colorComponents?[0]
- let green = colorComponents?[1]
- let blue = colorComponents?[2]
-
- let strokeColor = UIColor(red: red ?? 0, green: green ?? 0, blue: blue ?? 0, alpha: self.opcity).cgColor
- context.setStrokeColor(strokeColor)
- if self.interiorColor != UIColor.clear && self.interiorColor != nil {
- let interColorComponents = self.interiorColor?.cgColor.components
- let interRed = interColorComponents?[0]
- let interGreen = interColorComponents?[1]
- let interBlue = interColorComponents?[2]
-
- let fillColor = UIColor(red: interRed ?? 0, green: interGreen ?? 0, blue: interBlue ?? 0, alpha: self.opcity).cgColor
- context.setFillColor(fillColor)
- } else {
- context.setFillColor(UIColor.clear.cgColor)
- }
- let dashLengths: [CGFloat] = [6.0, self.dotted]
- context.setLineDash(phase: 0, lengths: dashLengths)
- context.setLineWidth(self.thickness)
- context.addArc(center: CGPoint(x: self.bounds.maxX/2, y: self.bounds.maxY/2), radius: 30, startAngle: 0, endAngle: 2 * .pi, clockwise: false)
- context.drawPath(using: .stroke)
- context.addArc(center: CGPoint(x: self.bounds.maxX/2, y: self.bounds.maxY/2), radius: 30, startAngle: 0, endAngle: 2 * .pi, clockwise: false)
- context.drawPath(using: .fill)
-
- case .shapeSquare:
- let colorComponents = self.color?.cgColor.components
- let red = colorComponents?[0]
- let green = colorComponents?[1]
- let blue = colorComponents?[2]
-
- let strokeColor = UIColor(red: red ?? 0, green: green ?? 0, blue: blue ?? 0, alpha: self.opcity).cgColor
- context.setStrokeColor(strokeColor)
- if self.interiorColor != UIColor.clear && self.interiorColor != nil {
- let interColorComponents = self.interiorColor?.cgColor.components
- let interRed = interColorComponents?[0]
- let interGreen = interColorComponents?[1]
- let interBlue = interColorComponents?[2]
-
- let fillColor = UIColor(red: interRed ?? 0, green: interGreen ?? 0, blue: interBlue ?? 0, alpha: self.opcity).cgColor
- context.setFillColor(fillColor)
- } else {
- context.setFillColor(UIColor.clear.cgColor)
- }
- context.setLineWidth(self.thickness)
- let dashLengths: [CGFloat] = [6.0, self.dotted]
- context.setLineDash(phase: 0, lengths: dashLengths)
- context.move(to: CGPoint(x: self.centerRect.minX, y: self.centerRect.minY))
- context.addLine(to: CGPoint(x: self.centerRect.maxX + 0.1, y: self.centerRect.minY))
- context.addLine(to: CGPoint(x: self.centerRect.maxX, y: self.centerRect.maxY))
- context.addLine(to: CGPoint(x: self.centerRect.minX, y: self.centerRect.maxY))
- context.addLine(to: CGPoint(x: self.centerRect.minX, y: self.centerRect.minY))
- context.addLine(to: CGPoint(x: self.centerRect.minX + 0.1, y: self.centerRect.minY))
- context.strokePath()
- context.fill(rect)
-
- case .shapeArrow:
- let colorComponents = self.color?.cgColor.components
- let red = colorComponents?[0]
- let green = colorComponents?[1]
- let blue = colorComponents?[2]
-
- context.saveGState()
-
- let strokeColor = (self.color?.withAlphaComponent(self.opcity) ?? .black).cgColor
- context.setStrokeColor(strokeColor)
- context.setLineWidth(self.thickness)
- context.setFillColor(strokeColor)
- var start = CGPoint(x: self.arrowRect.minX, y: self.arrowRect.maxY)
- var end = CGPoint(x: self.arrowRect.maxX, y: self.arrowRect.minY)
- end = drawEndArrow(context: context, startPoint: start, endPoint: end, style: endArrowStyleIndex)
- start = drawEndArrow(context: context, startPoint: end, endPoint: start, style: startArrowStyleIndex)
- let dashLengths: [CGFloat] = [6.0, self.dotted]
- context.setLineDash(phase: 0, lengths: dashLengths)
- context.move(to: start)
- context.addLine(to: end)
- context.setBlendMode(.normal)
- context.strokePath()
-
- context.restoreGState()
- case .shapeLine:
- let colorComponents = self.color?.cgColor.components
- let red = colorComponents?[0]
- let green = colorComponents?[1]
- let blue = colorComponents?[2]
-
- let strokeColor = UIColor(red: red ?? 0, green: green ?? 0, blue: blue ?? 0, alpha: self.opcity).cgColor
- context.setStrokeColor(strokeColor)
- context.setLineWidth(self.thickness)
- var start = CGPoint(x: self.arrowRect.minX, y: self.arrowRect.maxY)
- var end = CGPoint(x: self.arrowRect.maxX, y: self.arrowRect.minY)
- end = drawEndArrow(context: context, startPoint: start, endPoint: end, style: startArrowStyleIndex)
- start = drawEndArrow(context: context, startPoint: end, endPoint: start, style: endArrowStyleIndex)
- let dashLengths: [CGFloat] = [6.0, self.dotted]
- context.setLineDash(phase: 0, lengths: dashLengths)
- context.move(to: start)
- context.addLine(to: end)
- context.strokePath()
- case .freeText:
- let colorComponents = self.color?.cgColor.components
- let red = colorComponents?[0]
- let green = colorComponents?[1]
- let blue = colorComponents?[2]
-
-
- let cfont = CPDFFont(familyName: familyName, fontStyle: styleName)
- var font = UIFont.init(name: CPDFFont.convertAppleFont(cfont) ?? "Helvetica", size: thickness)
- if font == nil {
- font = UIFont(name: "Helvetica-Oblique", size: thickness)
- }
- let sampleStr = "Sample"
- if self.color == nil {
- self.color = UIColor.white
- }
- let attributedText = NSAttributedString(string: sampleStr, attributes: [NSAttributedString.Key.font: font!, NSAttributedString.Key.foregroundColor: UIColor(red: red ?? 0, green: green ?? 0, blue: blue ?? 0, alpha: self.opcity)])
- let textSize = attributedText.boundingRect(with: self.bounds.size, options: [.usesLineFragmentOrigin, .usesFontLeading], context: nil).size
- var x: CGFloat = 0
- var y: CGFloat = 0
- switch self.textAlignment {
- case .left:
- x = self.bounds.origin.x
- y = self.bounds.origin.y + (self.bounds.size.height - textSize.height) / 2.0
- case .center:
- x = self.bounds.origin.x + (self.bounds.size.width - textSize.width) / 2.0
- y = self.bounds.origin.y + (self.bounds.size.height - textSize.height) / 2.0
- case .right:
- x = self.bounds.origin.x + (self.bounds.size.width - textSize.width)
- y = self.bounds.origin.y + (self.bounds.size.height - textSize.height) / 2.0
- default:
- x = self.bounds.origin.x + (self.bounds.size.width - textSize.width) / 2.0
- y = self.bounds.origin.y + (self.bounds.size.height - textSize.height) / 2.0
- }
- let center = CGPoint(x: x, y: y)
- attributedText.draw(at: center)
-
- default:break
- }
-
- }
-
- func drawEndArrow(context: CGContext, startPoint: CGPoint, endPoint: CGPoint, style: CPDFArrowStyle) -> CGPoint {
- var rpoint = endPoint
- switch style {
- case .openArrow:
- var points = [CGPoint](repeating: .zero, count: 3)
- rpoint = openArrowPoints(points: &points, point: endPoint, cpoint: startPoint)
- context.beginPath()
- context.addLines(between: points)
- context.setBlendMode(.normal)
- context.drawPath(using: .stroke)
- case .closedArrow:
- var points = [CGPoint](repeating: .zero, count: 3)
- rpoint = closedArrowPoints(points: &points, point: endPoint, cpoint: startPoint)
- context.beginPath()
- context.addLines(between: points)
- context.closePath()
- context.setBlendMode(.normal)
- context.drawPath(using: .stroke)
- case .square:
- var points = [CGPoint](repeating: .zero, count: 4)
- rpoint = squarePoints(points: &points, point: endPoint, cpoint: startPoint)
- context.beginPath()
- context.addLines(between: points)
- context.closePath()
- context.setBlendMode(.normal)
- context.drawPath(using: .stroke)
- case .circle:
- var points = [CGPoint](repeating: .zero, count: 1)
- rpoint = circlePoints(points: &points, point: endPoint, cpoint: startPoint)
- let radius = sqrt((rpoint.x - points[0].x) * (rpoint.x - points[0].x) + (rpoint.y - points[0].y) * (rpoint.y - points[0].y))
- context.beginPath()
- context.addArc(center: points[0], radius: radius, startAngle: 0, endAngle: CGFloat(2 * Double.pi), clockwise: true)
- context.closePath()
- context.setBlendMode(.normal)
- context.drawPath(using: .stroke)
- case .diamond:
- var points = [CGPoint](repeating: .zero, count: 4)
- rpoint = diamondPoints(points: &points, point: endPoint, cpoint: startPoint)
- context.beginPath()
- context.addLines(between: points)
- context.closePath()
- context.setBlendMode(.normal)
- context.drawPath(using: .stroke)
- default:break
- }
-
- return rpoint
- }
-
- func openArrowPoints(points: UnsafeMutablePointer<CGPoint>, point: CGPoint, cpoint: CGPoint) -> CGPoint {
- let len: CGFloat = 4 + thickness
- var dx: CGFloat = 0, dy: CGFloat = 0
- var point0 = CGPoint.zero
- var point1 = CGPoint.zero
- var point2 = CGPoint.zero
-
- let mAB = (point.x - cpoint.x) * (point.x - cpoint.x) + (point.y - cpoint.y) * (point.y - cpoint.y)
- dx = (point.x - cpoint.x) * len / sqrt(mAB)
- dy = (point.y - cpoint.y) * len / sqrt(mAB)
- point0.x = point.x - dx
- point0.y = point.y - dy
-
- let mCB = (point.x - point0.x) * (point.x - point0.x) + (point.y - point0.y) * (point.y - point0.y)
- dx = -(point.y - point0.y) * len / sqrt(mCB)
- dy = (point.x - point0.x) * len / sqrt(mCB)
- point1.x = point0.x + dx
- point1.y = point0.y + dy
- point2.x = 2 * point0.x - point1.x
- point2.y = 2 * point0.y - point1.y
-
- // Assuming points is an array of CGPoint
- points[0] = point1
- points[1] = point
- points[2] = point2
-
- return point0
- }
- func closedArrowPoints(points: UnsafeMutablePointer<CGPoint>, point: CGPoint, cpoint: CGPoint) -> CGPoint {
- let len: CGFloat = 4
- var dx: CGFloat = 0, dy: CGFloat = 0
- var point0 = CGPoint.zero
- var point1 = CGPoint.zero
- var point2 = CGPoint.zero
-
- let mAB = (point.x - cpoint.x) * (point.x - cpoint.x) + (point.y - cpoint.y) * (point.y - cpoint.y)
- dx = (point.x - cpoint.x) * len / sqrt(mAB)
- dy = (point.y - cpoint.y) * len / sqrt(mAB)
- point0.x = point.x - dx
- point0.y = point.y - dy
-
- let mCB = (point.x - point0.x) * (point.x - point0.x) + (point.y - point0.y) * (point.y - point0.y)
- dx = -(point.y - point0.y) * len / sqrt(mCB)
- dy = (point.x - point0.x) * len / sqrt(mCB)
- point1.x = point0.x + dx
- point1.y = point0.y + dy
- point2.x = 2 * point0.x - point1.x
- point2.y = 2 * point0.y - point1.y
-
- // Assuming points is an array of CGPoint
- points[0] = point1
- points[1] = point
- points[2] = point2
-
- return point0
- }
-
- func squarePoints(points: UnsafeMutablePointer<CGPoint>, point: CGPoint, cpoint: CGPoint) -> CGPoint {
- let len = max(4, thickness)
- var dx: CGFloat = 0, dy: CGFloat = 0
- var point0 = CGPoint.zero
- var point1 = CGPoint.zero
- var point2 = CGPoint.zero
- var point3 = CGPoint.zero
- var point4 = CGPoint.zero
- var point5 = CGPoint.zero
- var point6 = CGPoint.zero
-
- let mAB = (point.x - cpoint.x) * (point.x - cpoint.x) + (point.y - cpoint.y) * (point.y - cpoint.y)
- dx = (point.x - cpoint.x) * len / sqrt(mAB)
- dy = (point.y - cpoint.y) * len / sqrt(mAB)
- point0.x = point.x - dx
- point0.y = point.y - dy
- point5.x = point.x - 2 * dx
- point5.y = point.y - 2 * dy
- point6.x = point.x + dx
- point6.y = point.y + dy
-
- let mCD = (point0.x - point5.x) * (point0.x - point5.x) + (point0.y - point5.y) * (point0.y - point5.y)
- dx = -(point0.y - point5.y) * len / sqrt(mCD)
- dy = (point0.x - point5.x) * len / sqrt(mCD)
- point1.x = point5.x + dx
- point1.y = point5.y + dy
- point2.x = 2 * point5.x - point1.x
- point2.y = 2 * point5.y - point1.y
-
- let mEB = (point6.x - point.x) * (point6.x - point.x) + (point6.y - point.y) * (point6.y - point.y)
- dx = -(point6.y - point.y) * len / sqrt(mEB)
- dy = (point6.x - point.x) * len / sqrt(mEB)
- point3.x = point.x + dx
- point3.y = point.y + dy
- point4.x = 2 * point.x - point3.x
- point4.y = 2 * point.y - point3.y
-
- points[0] = point1
- points[1] = point3
- points[2] = point4
- points[3] = point2
-
- return point5
- }
- func circlePoints(points: UnsafeMutablePointer<CGPoint>, point: CGPoint, cpoint: CGPoint) -> CGPoint {
- let len = max(4, thickness)
- var dx: CGFloat = 0, dy: CGFloat = 0
- var point0 = CGPoint.zero
- var point1 = CGPoint.zero
-
- let mAB = (point.x - cpoint.x) * (point.x - cpoint.x) + (point.y - cpoint.y) * (point.y - cpoint.y)
- dx = (point.x - cpoint.x) * len / sqrt(mAB)
- dy = (point.y - cpoint.y) * len / sqrt(mAB)
- point0.x = point.x - dx
- point0.y = point.y - dy
- point1.x = point.x - 2 * dx
- point1.y = point.y - 2 * dy
-
- points[0] = point0
-
- return point1
- }
- func diamondPoints(points: UnsafeMutablePointer<CGPoint>, point: CGPoint, cpoint: CGPoint) -> CGPoint {
- let len = max(4, thickness)
- var dx: CGFloat = 0, dy: CGFloat = 0
- var point0 = CGPoint.zero
- var point1 = CGPoint.zero
- var point2 = CGPoint.zero
- var point3 = CGPoint.zero
-
- let mAB = (point.x - cpoint.x) * (point.x - cpoint.x) + (point.y - cpoint.y) * (point.y - cpoint.y)
- dx = (point.x - cpoint.x) * len / sqrt(mAB)
- dy = (point.y - cpoint.y) * len / sqrt(mAB)
- point0.x = point.x - dx
- point0.y = point.y - dy
- point3.x = point.x - 2 * dx
- point3.y = point.y - 2 * dy
-
- let mCB = (point.x - point0.x) * (point.x - point0.x) + (point.y - point0.y) * (point.y - point0.y)
- dx = -(point.y - point0.y) * len / sqrt(mCB)
- dy = (point.x - point0.x) * len / sqrt(mCB)
- point1.x = point0.x + dx
- point1.y = point0.y + dy
- point2.x = 2 * point0.x - point1.x
- point2.y = 2 * point0.y - point1.y
-
- points[0] = point1
- points[1] = point
- points[2] = point2
- points[3] = point3
-
- return point3
- }
-
- }
|