|
@@ -368,4 +368,201 @@ import Cocoa
|
|
|
|
|
|
return image
|
|
return image
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ class func drawImageAtpageRect(rect: NSRect, data: KMWatermarkModel) -> NSImage? {
|
|
|
|
+ var size = NSZeroSize
|
|
|
|
+ let text: String = data.text
|
|
|
|
+ if text.count > 0 {
|
|
|
|
+ var font = NSFont.systemFont(ofSize: 52)
|
|
|
|
+ if data.isTilePage {
|
|
|
|
+ font = NSFont.systemFont(ofSize: data.getTextFontSize())
|
|
|
|
+ }
|
|
|
|
+ let style = NSMutableParagraphStyle()
|
|
|
|
+ style.alignment = .center
|
|
|
|
+ style.lineBreakMode = .byCharWrapping
|
|
|
|
+ let attributes: [NSAttributedString.Key: Any] = [ .paragraphStyle: style, .font: font ]
|
|
|
|
+ size = text.boundingRect(with: CGSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude), options: [.usesLineFragmentOrigin, .usesFontLeading], attributes: attributes).size
|
|
|
|
+ } else if let image = data.image {
|
|
|
|
+ size = image.size
|
|
|
|
+ size.width *= data.scale
|
|
|
|
+ size.height *= data.scale
|
|
|
|
+ }
|
|
|
|
+ let radian = data.rotation * (CGFloat.pi/180)
|
|
|
|
+ let t = CGAffineTransform(rotationAngle: radian)
|
|
|
|
+ var newRect = rect
|
|
|
|
+ if !data.isTilePage {
|
|
|
|
+ newRect = CGRectApplyAffineTransform(CGRect(x: 0, y: 0, width: size.width + 40, height: size.height + 40), t)
|
|
|
|
+ }
|
|
|
|
+ let image = NSImage(size: newRect.size)
|
|
|
|
+ image.lockFocus()
|
|
|
|
+ NSGraphicsContext.current?.imageInterpolation = .high
|
|
|
|
+ NSGraphicsContext.saveGraphicsState()
|
|
|
|
+ NSColor.clear.set()
|
|
|
|
+ rect.fill()
|
|
|
|
+ NSGraphicsContext.restoreGraphicsState()
|
|
|
|
+ guard let context = NSGraphicsContext.current?.cgContext else { return nil }
|
|
|
|
+ let imageSize = newRect.size
|
|
|
|
+ NSGraphicsContext.current = NSGraphicsContext(cgContext: context, flipped: false)
|
|
|
|
+ NSGraphicsContext.saveGraphicsState()
|
|
|
|
+
|
|
|
|
+ if text.count > 0 {
|
|
|
|
+ var font = NSFont.systemFont(ofSize: 52)
|
|
|
|
+ if data.isTilePage {
|
|
|
|
+ font = NSFont.systemFont(ofSize: data.getTextFontSize())
|
|
|
|
+ }
|
|
|
|
+ var color = data.getTextColor()
|
|
|
|
+ var red: CGFloat = 0, green: CGFloat = 0, blue: CGFloat = 0
|
|
|
|
+// color.usingColorSpace(NSColorSpace.rg)?.getRed(&red, green: &green, blue: &blue, alpha: nil)
|
|
|
|
+ color.usingColorSpaceName(.calibratedRGB)?.getRed(&red, green: &green, blue: &blue, alpha: nil)
|
|
|
|
+ color = NSColor.init(calibratedRed: red, green: green, blue: blue, alpha: data.opacity)
|
|
|
|
+
|
|
|
|
+ size = .zero
|
|
|
|
+
|
|
|
|
+ let alpha = data.opacity
|
|
|
|
+ let tileHorizontalSpace = data.tileHorizontalSpace
|
|
|
|
+ let tileVerticalSpace = data.tileVerticalSpace
|
|
|
|
+ let horizontalSpace = data.horizontalSpace
|
|
|
|
+ let verticalSpace = data.verticalSpace
|
|
|
|
+ let style = NSMutableParagraphStyle()
|
|
|
|
+ style.alignment = .center
|
|
|
|
+ style.lineBreakMode = .byCharWrapping
|
|
|
|
+ let attributes: [NSAttributedString.Key: Any] = [ .paragraphStyle: style, .foregroundColor: NSColor(calibratedRed: red, green: green, blue: blue, alpha: alpha), .font: font ]
|
|
|
|
+ size = text.boundingRect(with: CGSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude), options: [.usesLineFragmentOrigin, .usesFontLeading], attributes: attributes).size
|
|
|
|
+ let textRect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
|
|
|
|
+ if data.isTilePage {
|
|
|
|
+ let width = sqrt(image.size.height * image.size.height + image.size.width * image.size.width)
|
|
|
|
+ let newRect = CGRect(x: -(width - image.size.width) / 2, y: -(width - image.size.height) / 2, width: width, height: width)
|
|
|
|
+ let new_w = newRect.size.width
|
|
|
|
+ let new_h = newRect.size.height
|
|
|
|
+ context.translateBy(x: image.size.width / 2, y: image.size.height / 2)
|
|
|
|
+ context.concatenate(CGAffineTransform(rotationAngle: radian))
|
|
|
|
+ context.translateBy(x: -(image.size.width / 2), y: -(image.size.height / 2))
|
|
|
|
+ let verticalWidth = size.width + tileHorizontalSpace / 3.0
|
|
|
|
+ let horizontalHeight = size.height + tileVerticalSpace / 3.0
|
|
|
|
+ let line: Int = Int((new_h - tileHorizontalSpace / 3.0) / horizontalHeight + 1)
|
|
|
|
+ let row: Int = Int((new_w - tileVerticalSpace / 3.0) / verticalWidth + 1)
|
|
|
|
+ let point = CGPoint(x: image.size.width / 2 - size.width / 2 + horizontalSpace / 3.0, y: image.size.height / 2 - size.height / 2 + verticalSpace / 3.0)
|
|
|
|
+ for i in 0..<line {
|
|
|
|
+ for j in 0..<row {
|
|
|
|
+ let drawRect = CGRect(x: point.x + CGFloat(j) * verticalWidth, y: point.y + CGFloat(i) * horizontalHeight, width: size.width, height: size.height)
|
|
|
|
+ text.draw(in: drawRect, withAttributes: attributes)
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ for i in 1..<line {
|
|
|
|
+ for j in 0..<row {
|
|
|
|
+ let drawRect = CGRect(x: point.x + CGFloat(j) * verticalWidth, y: point.y - CGFloat(i) * horizontalHeight, width: size.width, height: size.height)
|
|
|
|
+ text.draw(in: drawRect, withAttributes: attributes)
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ for i in 0..<line {
|
|
|
|
+ for j in 1..<row {
|
|
|
|
+ let drawRect = CGRect(x: point.x - CGFloat(j) * verticalWidth, y: point.y + CGFloat(i) * horizontalHeight, width: size.width, height: size.height)
|
|
|
|
+ text.draw(in: drawRect, withAttributes: attributes)
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ for i in 1..<line {
|
|
|
|
+ for j in 1..<row {
|
|
|
|
+ let drawRect = CGRect(x: point.x - CGFloat(j) * verticalWidth, y: point.y - CGFloat(i) * horizontalHeight, width: size.width, height: size.height)
|
|
|
|
+ text.draw(in: drawRect, withAttributes: attributes)
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ var textRect = textRect
|
|
|
|
+ if data.verticalMode == 0 {
|
|
|
|
+ textRect.origin.y = imageSize.height - textRect.size.height
|
|
|
|
+ } else if data.verticalMode == 1 {
|
|
|
|
+ textRect.origin.y = (imageSize.height - textRect.size.height) / 2.0
|
|
|
|
+ } else {
|
|
|
|
+ textRect.origin.y = 0
|
|
|
|
+ }
|
|
|
|
+ if data.horizontalMode == 0 {
|
|
|
|
+ textRect.origin.x = 0
|
|
|
|
+ } else if data.horizontalMode == 1{
|
|
|
|
+ textRect.origin.x = (imageSize.width - textRect.size.width) / 2.0
|
|
|
|
+ } else {
|
|
|
|
+ textRect.origin.x = imageSize.width - textRect.size.width
|
|
|
|
+ }
|
|
|
|
+ let contextCenter = CGPoint(x: textRect.midX, y: textRect.midY)
|
|
|
|
+ context.translateBy(x: contextCenter.x, y: contextCenter.y)
|
|
|
|
+ context.rotate(by: radian)
|
|
|
|
+ context.translateBy(x: -contextCenter.x, y: -contextCenter.y)
|
|
|
|
+ data.text.draw(in: CGRect(x: textRect.origin.x + (textRect.size.width - size.width) / 2.0, y: textRect.origin.y + (textRect.size.height - size.height) / 2.0, width: size.width, height: size.height), withAttributes: attributes)
|
|
|
|
+ }
|
|
|
|
+ }else if let image = data.image {
|
|
|
|
+ var size = image.size
|
|
|
|
+ size.width *= data.scale
|
|
|
|
+ size.height *= data.scale
|
|
|
|
+ let width = sqrt(image.size.height * image.size.height + image.size.width * image.size.width)
|
|
|
|
+ let newRect = CGRect(x: -(width - image.size.width)/2, y: -(width - image.size.height)/2, width: width, height: width)
|
|
|
|
+ let new_w = newRect.size.width
|
|
|
|
+ let new_h = newRect.size.height
|
|
|
|
+ let radian = data.rotation * (CGFloat.pi / 180)
|
|
|
|
+ let t = CGAffineTransform(rotationAngle: radian)
|
|
|
|
+ if data.isTilePage {
|
|
|
|
+ context.translateBy(x: image.size.width/2, y: image.size.height/2)
|
|
|
|
+ context.concatenate(t)
|
|
|
|
+ context.translateBy(x: -(image.size.width/2), y: -(image.size.height/2))
|
|
|
|
+
|
|
|
|
+ let verticalWidth = size.width + data.tileHorizontalSpace * data.scale
|
|
|
|
+ let horizontalHeight = size.height + data.tileVerticalSpace * data.scale
|
|
|
|
+ let line: Int = Int((new_h - data.tileVerticalSpace * data.scale)/horizontalHeight + 1)
|
|
|
|
+ let row: Int = Int((new_w - data.tileHorizontalSpace * data.scale) / verticalWidth + 1)
|
|
|
|
+ let point = CGPoint(x: image.size.width/2 - size.width/2 + data.horizontalSpace*data.scale, y:image.size.height/2 - size.height/2 + data.verticalSpace * data.scale)
|
|
|
|
+ for i in 0..<line/2+1 {
|
|
|
|
+ for j in 0..<row {
|
|
|
|
+ let area = CGRect(x: point.x + CGFloat(j) * verticalWidth, y: point.y + CGFloat(i) * horizontalHeight, width: size.width, height: size.height)
|
|
|
|
+ data.image.draw(in: area, from: NSZeroRect, operation: .sourceOver, fraction: data.opacity)
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ for i in 1..<line/2+1 {
|
|
|
|
+ for j in 0..<row {
|
|
|
|
+ let area = CGRect(x: point.x + CGFloat(j) * verticalWidth, y: point.y - CGFloat(i) * horizontalHeight, width: size.width, height: size.height)
|
|
|
|
+ data.image.draw(in: area, from: NSZeroRect, operation: .sourceOver, fraction: data.opacity)
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ for i in 0..<line/2+1 {
|
|
|
|
+ for j in 1..<row {
|
|
|
|
+ let area = CGRect(x: point.x - CGFloat(j) * verticalWidth, y: point.y + CGFloat(i) * horizontalHeight, width: size.width, height: size.height)
|
|
|
|
+ data.image.draw(in: area, from: NSZeroRect, operation: .sourceOver, fraction: data.opacity)
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ for i in 1..<(line/2 + 1) {
|
|
|
|
+ for j in 1..<row {
|
|
|
|
+ let area = CGRect(x: point.x - CGFloat(j)*verticalWidth, y: point.y - CGFloat(i)*horizontalHeight, width: size.width, height: size.height)
|
|
|
|
+ data.image.draw(in: area, from: NSZeroRect, operation: .sourceOver, fraction: data.opacity)
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }else {
|
|
|
|
+ var size = data.image.size
|
|
|
|
+ size.width *= data.scale
|
|
|
|
+ size.height *= data.scale
|
|
|
|
+ let radian = data.rotation * (CGFloat.pi / 180)
|
|
|
|
+ let t = CGAffineTransform(rotationAngle: radian)
|
|
|
|
+ var imageRect = CGRect(origin: .zero, size: size).applying(t)
|
|
|
|
+ if data.verticalMode == 0 {
|
|
|
|
+ imageRect.origin.y = imageSize.height - imageRect.size.height
|
|
|
|
+ } else if data.verticalMode == 1 {
|
|
|
|
+ imageRect.origin.y = (imageSize.height - imageRect.size.height) / 2.0
|
|
|
|
+ } else {
|
|
|
|
+ imageRect.origin.y = 0
|
|
|
|
+ }
|
|
|
|
+ if data.horizontalMode == 0 {
|
|
|
|
+ imageRect.origin.x = 0
|
|
|
|
+ } else if data.horizontalMode == 1 {
|
|
|
|
+ imageRect.origin.x = (imageSize.width - imageRect.size.width) / 2.0
|
|
|
|
+ } else {
|
|
|
|
+ imageRect.origin.x = imageSize.width - imageRect.size.width
|
|
|
|
+ }
|
|
|
|
+ let contextCenter = CGPoint(x: imageRect.midX, y: imageRect.midY)
|
|
|
|
+ context.translateBy(x: contextCenter.x, y: contextCenter.y)
|
|
|
|
+ context.rotate(by: radian)
|
|
|
|
+ context.translateBy(x: -contextCenter.x, y: -contextCenter.y)
|
|
|
|
+ data.image.draw(in: CGRect(x: imageRect.origin.x+(imageRect.size.width-size.width)/2.0, y: imageRect.origin.y+(imageRect.size.height-size.height)/2.0, width: size.width, height: size.height), from: NSZeroRect, operation: .sourceOver, fraction: data.opacity)
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ NSGraphicsContext.restoreGraphicsState()
|
|
|
|
+ NSGraphicsContext.current?.imageInterpolation = .default
|
|
|
|
+ image.unlockFocus()
|
|
|
|
+ return image
|
|
|
|
+ }
|
|
}
|
|
}
|