mirror of https://github.com/oxen-io/session-ios
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
390 lines
9.6 KiB
Swift
390 lines
9.6 KiB
Swift
6 years ago
|
//
|
||
|
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
|
||
|
//
|
||
|
|
||
|
import UIKit
|
||
|
|
||
|
@objc public enum ImageEditorError: Int, Error {
|
||
|
case assertionError
|
||
|
case invalidInput
|
||
|
}
|
||
|
|
||
6 years ago
|
@objc
|
||
|
public enum ImageEditorItemType: Int {
|
||
|
case test
|
||
6 years ago
|
case stroke
|
||
6 years ago
|
}
|
||
|
|
||
6 years ago
|
// MARK: -
|
||
|
|
||
6 years ago
|
// Instances of ImageEditorItem should be treated
|
||
|
// as immutable, once configured.
|
||
6 years ago
|
@objc
|
||
|
public class ImageEditorItem: NSObject {
|
||
|
@objc
|
||
|
public let itemId: String
|
||
|
|
||
|
@objc
|
||
6 years ago
|
public let itemType: ImageEditorItemType
|
||
|
|
||
|
@objc
|
||
6 years ago
|
public init(itemType: ImageEditorItemType) {
|
||
6 years ago
|
self.itemId = UUID().uuidString
|
||
6 years ago
|
self.itemType = itemType
|
||
6 years ago
|
|
||
|
super.init()
|
||
|
}
|
||
6 years ago
|
|
||
|
@objc
|
||
6 years ago
|
public init(itemId: String,
|
||
|
itemType: ImageEditorItemType) {
|
||
6 years ago
|
self.itemId = itemId
|
||
6 years ago
|
self.itemType = itemType
|
||
6 years ago
|
|
||
|
super.init()
|
||
|
}
|
||
6 years ago
|
}
|
||
|
|
||
6 years ago
|
// MARK: -
|
||
|
|
||
6 years ago
|
@objc
|
||
|
public class ImageEditorStrokeItem: ImageEditorItem {
|
||
|
// Until we need to serialize these items,
|
||
|
// just use UIColor.
|
||
|
@objc
|
||
|
public let color: UIColor
|
||
|
|
||
|
// Represented in a "ULO unit" coordinate system
|
||
|
// for source image.
|
||
|
//
|
||
|
// "ULO" coordinate system is "upper-left-origin".
|
||
|
//
|
||
|
// "Unit" coordinate system means values are expressed
|
||
|
// in terms of some other values, in this case the
|
||
|
// width and height of the source image.
|
||
|
//
|
||
|
// * 0.0 = left edge
|
||
|
// * 1.0 = right edge
|
||
|
// * 0.0 = top edge
|
||
|
// * 1.0 = bottom edge
|
||
|
public typealias StrokeSample = CGPoint
|
||
|
|
||
|
@objc
|
||
|
public let unitSamples: [StrokeSample]
|
||
|
|
||
|
// Expressed as a "Unit" value as a fraction of
|
||
6 years ago
|
// min(width, height) of the destination viewport.
|
||
6 years ago
|
@objc
|
||
|
public let unitStrokeWidth: CGFloat
|
||
|
|
||
|
@objc
|
||
|
public init(color: UIColor,
|
||
|
unitSamples: [StrokeSample],
|
||
|
unitStrokeWidth: CGFloat) {
|
||
|
self.color = color
|
||
|
self.unitSamples = unitSamples
|
||
|
self.unitStrokeWidth = unitStrokeWidth
|
||
|
|
||
|
super.init(itemType: .stroke)
|
||
|
}
|
||
6 years ago
|
|
||
6 years ago
|
@objc
|
||
|
public init(itemId: String,
|
||
|
color: UIColor,
|
||
|
unitSamples: [StrokeSample],
|
||
|
unitStrokeWidth: CGFloat) {
|
||
|
self.color = color
|
||
|
self.unitSamples = unitSamples
|
||
|
self.unitStrokeWidth = unitStrokeWidth
|
||
|
|
||
|
super.init(itemId: itemId, itemType: .stroke)
|
||
|
}
|
||
|
|
||
6 years ago
|
@objc
|
||
|
public class func defaultUnitStrokeWidth() -> CGFloat {
|
||
6 years ago
|
return 0.02
|
||
6 years ago
|
}
|
||
|
|
||
|
@objc
|
||
|
public class func strokeWidth(forUnitStrokeWidth unitStrokeWidth: CGFloat,
|
||
|
dstSize: CGSize) -> CGFloat {
|
||
|
return CGFloatClamp01(unitStrokeWidth) * min(dstSize.width, dstSize.height)
|
||
|
}
|
||
6 years ago
|
}
|
||
|
|
||
|
// MARK: -
|
||
|
|
||
6 years ago
|
// ImageEditorContents represents a snapshot of canvas
|
||
|
// state.
|
||
|
//
|
||
6 years ago
|
// Instances of ImageEditorContents should be treated
|
||
|
// as immutable, once configured.
|
||
6 years ago
|
public class ImageEditorContents: NSObject {
|
||
|
|
||
6 years ago
|
// This represents the current state of each item.
|
||
6 years ago
|
var itemMap = [String: ImageEditorItem]()
|
||
6 years ago
|
|
||
|
// This represents the back-to-front ordering of the items.
|
||
6 years ago
|
var itemIds = [String]()
|
||
|
|
||
|
@objc
|
||
|
public override init() {
|
||
|
}
|
||
|
|
||
|
@objc
|
||
|
public init(itemMap: [String: ImageEditorItem],
|
||
|
itemIds: [String]) {
|
||
|
|
||
|
self.itemMap = itemMap
|
||
|
self.itemIds = itemIds
|
||
|
}
|
||
|
|
||
6 years ago
|
// Since the contents are immutable, we only modify copies
|
||
|
// made with this method.
|
||
6 years ago
|
@objc
|
||
|
public func clone() -> ImageEditorContents {
|
||
|
return ImageEditorContents(itemMap: itemMap, itemIds: itemIds)
|
||
|
}
|
||
|
|
||
|
@objc
|
||
|
public func append(item: ImageEditorItem) {
|
||
6 years ago
|
Logger.verbose("\(item.itemId)")
|
||
|
|
||
6 years ago
|
if itemMap[item.itemId] != nil {
|
||
|
owsFail("Unexpected duplicate item in item map: \(item.itemId)")
|
||
|
}
|
||
|
itemMap[item.itemId] = item
|
||
|
|
||
|
if itemIds.contains(item.itemId) {
|
||
|
owsFail("Unexpected duplicate item in item list: \(item.itemId)")
|
||
|
} else {
|
||
|
itemIds.append(item.itemId)
|
||
|
}
|
||
6 years ago
|
|
||
|
if itemIds.count != itemMap.count {
|
||
|
owsFailDebug("Invalid contents.")
|
||
|
}
|
||
|
}
|
||
|
|
||
|
@objc
|
||
|
public func replace(item: ImageEditorItem) {
|
||
6 years ago
|
Logger.verbose("\(item.itemId)")
|
||
|
|
||
6 years ago
|
if itemMap[item.itemId] == nil {
|
||
|
owsFail("Missing item in item map: \(item.itemId)")
|
||
|
}
|
||
|
itemMap[item.itemId] = item
|
||
|
|
||
|
if !itemIds.contains(item.itemId) {
|
||
|
owsFail("Missing item in item list: \(item.itemId)")
|
||
|
}
|
||
|
|
||
|
if itemIds.count != itemMap.count {
|
||
|
owsFailDebug("Invalid contents.")
|
||
|
}
|
||
6 years ago
|
}
|
||
|
|
||
|
@objc
|
||
|
public func remove(item: ImageEditorItem) {
|
||
6 years ago
|
Logger.verbose("\(item.itemId)")
|
||
|
|
||
6 years ago
|
remove(itemId: item.itemId)
|
||
|
}
|
||
|
|
||
|
@objc
|
||
|
public func remove(itemId: String) {
|
||
|
if itemMap[itemId] == nil {
|
||
|
owsFail("Missing item in item map: \(itemId)")
|
||
|
} else {
|
||
|
itemMap.removeValue(forKey: itemId)
|
||
|
}
|
||
|
|
||
|
if !itemIds.contains(itemId) {
|
||
|
owsFail("Missing item in item list: \(itemId)")
|
||
|
} else {
|
||
|
itemIds = itemIds.filter { $0 != itemId }
|
||
|
}
|
||
6 years ago
|
|
||
|
if itemIds.count != itemMap.count {
|
||
|
owsFailDebug("Invalid contents.")
|
||
|
}
|
||
|
}
|
||
|
|
||
|
@objc
|
||
|
public func itemCount() -> Int {
|
||
|
if itemIds.count != itemMap.count {
|
||
|
owsFailDebug("Invalid contents.")
|
||
|
}
|
||
|
return itemIds.count
|
||
|
}
|
||
6 years ago
|
|
||
|
@objc
|
||
|
public func items() -> [ImageEditorItem] {
|
||
|
var items = [ImageEditorItem]()
|
||
|
for itemId in itemIds {
|
||
|
guard let item = self.itemMap[itemId] else {
|
||
|
owsFailDebug("Missing item")
|
||
|
continue
|
||
|
}
|
||
|
items.append(item)
|
||
|
}
|
||
|
return items
|
||
|
}
|
||
6 years ago
|
}
|
||
|
|
||
|
// MARK: -
|
||
|
|
||
|
// Used to represent undo/redo operations.
|
||
6 years ago
|
//
|
||
|
// Because the image editor's "contents" and "items"
|
||
|
// are immutable, these operations simply take a
|
||
|
// snapshot of the current contents which can be used
|
||
|
// (multiple times) to preserve/restore editor state.
|
||
6 years ago
|
private class ImageEditorOperation: NSObject {
|
||
|
|
||
|
let contents: ImageEditorContents
|
||
|
|
||
|
required init(contents: ImageEditorContents) {
|
||
|
self.contents = contents
|
||
6 years ago
|
}
|
||
|
}
|
||
|
|
||
6 years ago
|
// MARK: -
|
||
|
|
||
6 years ago
|
@objc
|
||
|
public protocol ImageEditorModelDelegate: class {
|
||
|
func imageEditorModelDidChange()
|
||
|
}
|
||
|
|
||
|
// MARK: -
|
||
|
|
||
6 years ago
|
@objc
|
||
|
public class ImageEditorModel: NSObject {
|
||
6 years ago
|
@objc
|
||
|
public weak var delegate: ImageEditorModelDelegate?
|
||
|
|
||
6 years ago
|
@objc
|
||
|
public let srcImagePath: String
|
||
|
|
||
|
@objc
|
||
6 years ago
|
public let srcImageSizePixels: CGSize
|
||
6 years ago
|
|
||
|
private var contents = ImageEditorContents()
|
||
6 years ago
|
|
||
6 years ago
|
private var undoStack = [ImageEditorOperation]()
|
||
|
private var redoStack = [ImageEditorOperation]()
|
||
|
|
||
6 years ago
|
// We don't want to allow editing of images if:
|
||
|
//
|
||
|
// * They are invalid.
|
||
|
// * We can't determine their size / aspect-ratio.
|
||
6 years ago
|
@objc
|
||
|
public required init(srcImagePath: String) throws {
|
||
|
self.srcImagePath = srcImagePath
|
||
|
|
||
|
let srcFileName = (srcImagePath as NSString).lastPathComponent
|
||
|
let srcFileExtension = (srcFileName as NSString).pathExtension
|
||
|
guard let mimeType = MIMETypeUtil.mimeType(forFileExtension: srcFileExtension) else {
|
||
|
Logger.error("Couldn't determine MIME type for file.")
|
||
|
throw ImageEditorError.invalidInput
|
||
|
}
|
||
6 years ago
|
guard MIMETypeUtil.isImage(mimeType),
|
||
|
!MIMETypeUtil.isAnimated(mimeType) else {
|
||
6 years ago
|
Logger.error("Invalid MIME type: \(mimeType).")
|
||
|
throw ImageEditorError.invalidInput
|
||
|
}
|
||
|
|
||
6 years ago
|
let srcImageSizePixels = NSData.imageSize(forFilePath: srcImagePath, mimeType: mimeType)
|
||
|
guard srcImageSizePixels.width > 0, srcImageSizePixels.height > 0 else {
|
||
6 years ago
|
Logger.error("Couldn't determine image size.")
|
||
|
throw ImageEditorError.invalidInput
|
||
|
}
|
||
6 years ago
|
self.srcImageSizePixels = srcImageSizePixels
|
||
6 years ago
|
|
||
|
super.init()
|
||
|
}
|
||
6 years ago
|
|
||
|
@objc
|
||
|
public func itemCount() -> Int {
|
||
|
return contents.itemCount()
|
||
|
}
|
||
|
|
||
6 years ago
|
@objc
|
||
|
public func items() -> [ImageEditorItem] {
|
||
|
return contents.items()
|
||
|
}
|
||
|
|
||
6 years ago
|
@objc
|
||
|
public func canUndo() -> Bool {
|
||
|
return !undoStack.isEmpty
|
||
|
}
|
||
|
|
||
|
@objc
|
||
|
public func canRedo() -> Bool {
|
||
|
return !redoStack.isEmpty
|
||
|
}
|
||
|
|
||
|
@objc
|
||
|
public func undo() {
|
||
|
guard let undoOperation = undoStack.popLast() else {
|
||
|
owsFailDebug("Cannot undo.")
|
||
|
return
|
||
|
}
|
||
|
|
||
|
let redoOperation = ImageEditorOperation(contents: contents)
|
||
|
redoStack.append(redoOperation)
|
||
|
|
||
|
self.contents = undoOperation.contents
|
||
6 years ago
|
|
||
|
delegate?.imageEditorModelDidChange()
|
||
6 years ago
|
}
|
||
|
|
||
|
@objc
|
||
|
public func redo() {
|
||
|
guard let redoOperation = redoStack.popLast() else {
|
||
|
owsFailDebug("Cannot redo.")
|
||
|
return
|
||
|
}
|
||
|
|
||
|
let undoOperation = ImageEditorOperation(contents: contents)
|
||
|
undoStack.append(undoOperation)
|
||
|
|
||
|
self.contents = redoOperation.contents
|
||
6 years ago
|
|
||
|
delegate?.imageEditorModelDidChange()
|
||
6 years ago
|
}
|
||
|
|
||
|
@objc
|
||
|
public func append(item: ImageEditorItem) {
|
||
|
performAction { (newContents) in
|
||
|
newContents.append(item: item)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
@objc
|
||
|
public func replace(item: ImageEditorItem) {
|
||
|
performAction { (newContents) in
|
||
|
newContents.replace(item: item)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
@objc
|
||
|
public func remove(item: ImageEditorItem) {
|
||
|
performAction { (newContents) in
|
||
|
newContents.remove(item: item)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private func performAction(action: (ImageEditorContents) -> Void) {
|
||
|
let undoOperation = ImageEditorOperation(contents: contents)
|
||
|
undoStack.append(undoOperation)
|
||
|
redoStack.removeAll()
|
||
|
|
||
|
let newContents = contents.clone()
|
||
|
action(newContents)
|
||
|
contents = newContents
|
||
6 years ago
|
|
||
|
delegate?.imageEditorModelDidChange()
|
||
6 years ago
|
}
|
||
6 years ago
|
}
|