|
@@ -113,6 +113,18 @@ class KMEditPDfHanddler: NSObject {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ func fontStyleAction(fontName: String?) {
|
|
|
+ guard let font = CPDFFont.mappingFont(withFontString: fontName) else {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ let editingAreas = self.editingAreas
|
|
|
+ for area in editingAreas {
|
|
|
+ if let data = area as? CPDFEditTextArea {
|
|
|
+ self.listView?.setEditSelectionCFont(font, with: data)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
func fontAddAction() {
|
|
|
let editingAreas = self.editingAreas
|
|
|
if editingAreas.isEmpty {
|
|
@@ -174,8 +186,13 @@ class KMEditPDfHanddler: NSObject {
|
|
|
self.listView?.setCurrentSelectionIsItalic(true, with: area as! CPDFEditTextArea)
|
|
|
}
|
|
|
|
|
|
- func textAlignmentAction() {
|
|
|
- KMPrint("textAlignmentAction")
|
|
|
+ func textAlignmentAction(align: NSTextAlignment) {
|
|
|
+ let editingAreas = self.editingAreas
|
|
|
+ for area in editingAreas {
|
|
|
+ if let data = area as? CPDFEditTextArea {
|
|
|
+ self.listView?.setCurrentSelectionAlignment(align, with: data)
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
func leftRotateAction() {
|
|
@@ -333,8 +350,191 @@ class KMEditPDfHanddler: NSObject {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- func alignmentAction() {
|
|
|
- KMPrint("alignmentAction")
|
|
|
+ func alignmentAction(align: CPDFActiveAreasAlignType) {
|
|
|
+ KMPrint("updateFormAearsAlignMangent")
|
|
|
+ let stype = align
|
|
|
+
|
|
|
+ let editingAreas = self.editingAreas
|
|
|
+ if editingAreas.count >= 2 {
|
|
|
+
|
|
|
+ var zeroRect = NSRect.null
|
|
|
+ var highestRect = NSZeroRect
|
|
|
+ var widthestRect = NSZeroRect
|
|
|
+
|
|
|
+ let fristArea : CPDFEditArea = editingAreas.first as! CPDFEditArea
|
|
|
+ var leftestRect = fristArea.bounds
|
|
|
+ var rightestRect = fristArea.bounds
|
|
|
+ var topestRect = fristArea.bounds
|
|
|
+ var bottomestRect = fristArea.bounds
|
|
|
+
|
|
|
+ var leftestArea : CPDFEditArea = fristArea
|
|
|
+ var rightestArea : CPDFEditArea = fristArea
|
|
|
+ var topestArea : CPDFEditArea = fristArea
|
|
|
+ var bottomestArea : CPDFEditArea = fristArea
|
|
|
+
|
|
|
+ var totalWidth = 0.0
|
|
|
+ var totalHeight = 0.0
|
|
|
+
|
|
|
+ for i in 0 ... editingAreas.count-1 {
|
|
|
+ let area : CPDFEditArea = editingAreas[i] as! CPDFEditArea
|
|
|
+ zeroRect = zeroRect.union(area.bounds)
|
|
|
+ totalWidth = totalWidth + area.bounds.width
|
|
|
+ totalHeight = totalHeight + area.bounds.height
|
|
|
+
|
|
|
+ if area.bounds.height > highestRect.height {
|
|
|
+ highestRect = area.bounds
|
|
|
+ }
|
|
|
+
|
|
|
+ if area.bounds.width > widthestRect.width {
|
|
|
+ widthestRect = area.bounds
|
|
|
+ }
|
|
|
+
|
|
|
+ if leftestRect.minX > area.bounds.minX {
|
|
|
+ leftestRect = area.bounds
|
|
|
+ leftestArea = area
|
|
|
+ }
|
|
|
+
|
|
|
+ if area.bounds.maxX > rightestRect.maxX {
|
|
|
+ rightestRect = area.bounds
|
|
|
+ rightestArea = area
|
|
|
+ }
|
|
|
+
|
|
|
+ if area.bounds.maxY > topestRect.maxY {
|
|
|
+ topestRect = area.bounds
|
|
|
+ topestArea = area
|
|
|
+ }
|
|
|
+
|
|
|
+ if bottomestRect.minY > area.bounds.minY {
|
|
|
+ bottomestRect = area.bounds
|
|
|
+ bottomestArea = area
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ var resultAreasArray: [Any] = []
|
|
|
+ var newBoundsArray: [String] = []
|
|
|
+ if stype == .Left {
|
|
|
+ for i in 0 ... editingAreas.count-1 {
|
|
|
+ let areas = editingAreas[i] as! CPDFEditArea
|
|
|
+ var bounds = areas.bounds
|
|
|
+ bounds.origin.x = zeroRect.origin.x
|
|
|
+ newBoundsArray.append(NSStringFromRect(bounds))
|
|
|
+ }
|
|
|
+ resultAreasArray = editingAreas
|
|
|
+ } else if stype == .Right {
|
|
|
+ for i in 0 ... editingAreas.count-1 {
|
|
|
+ let areas = editingAreas[i] as! CPDFEditArea
|
|
|
+ var bounds = areas.bounds
|
|
|
+ bounds.origin.x = zeroRect.maxX - bounds.size.width
|
|
|
+ newBoundsArray.append(NSStringFromRect(bounds))
|
|
|
+ }
|
|
|
+ resultAreasArray = editingAreas
|
|
|
+ } else if stype == .Top {
|
|
|
+ for i in 0 ... editingAreas.count-1 {
|
|
|
+ let areas = editingAreas[i] as! CPDFEditArea
|
|
|
+ var bounds = areas.bounds
|
|
|
+ bounds.origin.y = zeroRect.maxY - bounds.size.height
|
|
|
+ newBoundsArray.append(NSStringFromRect(bounds))
|
|
|
+ }
|
|
|
+ resultAreasArray = editingAreas
|
|
|
+ } else if stype == .Bottom {
|
|
|
+ for i in 0 ... editingAreas.count-1 {
|
|
|
+ let areas = editingAreas[i] as! CPDFEditArea
|
|
|
+ var bounds = areas.bounds
|
|
|
+ bounds.origin.y = zeroRect.minY
|
|
|
+ newBoundsArray.append(NSStringFromRect(bounds))
|
|
|
+ }
|
|
|
+ resultAreasArray = editingAreas
|
|
|
+ } else if stype == .Horizontally {
|
|
|
+ for i in 0 ... editingAreas.count-1 {
|
|
|
+ let areas = editingAreas[i] as! CPDFEditArea
|
|
|
+ var bounds = areas.bounds
|
|
|
+ bounds.origin.y = highestRect.midY - bounds.height/2
|
|
|
+ newBoundsArray.append(NSStringFromRect(bounds))
|
|
|
+ }
|
|
|
+ resultAreasArray = editingAreas
|
|
|
+ } else if stype == .Vertical {
|
|
|
+ for i in 0 ... editingAreas.count-1 {
|
|
|
+ let areas = editingAreas[i] as! CPDFEditArea
|
|
|
+ var bounds = areas.bounds
|
|
|
+ bounds.origin.x = widthestRect.midX - bounds.width/2
|
|
|
+ newBoundsArray.append(NSStringFromRect(bounds))
|
|
|
+ }
|
|
|
+ resultAreasArray = editingAreas
|
|
|
+ } else if stype == .DisHorizontally {
|
|
|
+ let middleGap = zeroRect.width - leftestRect.width - rightestRect.width
|
|
|
+ let otherAreasTotalWidth = totalWidth - leftestRect.width - rightestRect.width
|
|
|
+
|
|
|
+ let gap = (middleGap - otherAreasTotalWidth)/CGFloat(editingAreas.count - 1)
|
|
|
+ var areasCopyArray : [CPDFEditArea] = editingAreas as! [CPDFEditArea]
|
|
|
+ areasCopyArray.sorted(by: { obj1, obj2 in
|
|
|
+ let area1 = obj1
|
|
|
+ let area2 = obj2
|
|
|
+ if area1.bounds.origin.x < area2.bounds.origin.x {
|
|
|
+ return true
|
|
|
+ } else {
|
|
|
+ return false
|
|
|
+ }
|
|
|
+ })
|
|
|
+
|
|
|
+ if let index = areasCopyArray.firstIndex(of: leftestArea) {
|
|
|
+ areasCopyArray.remove(at: index)
|
|
|
+ }
|
|
|
+
|
|
|
+ if let index = areasCopyArray.firstIndex(of: rightestArea) {
|
|
|
+ areasCopyArray.remove(at: index)
|
|
|
+ }
|
|
|
+
|
|
|
+ var leftStartX = leftestRect.maxX + gap
|
|
|
+ for i in 0 ... areasCopyArray.count-1 {
|
|
|
+ let areas = areasCopyArray[i]
|
|
|
+ var bounds = areas.bounds
|
|
|
+ bounds.origin.x = leftStartX
|
|
|
+ newBoundsArray.append(NSStringFromRect(bounds))
|
|
|
+ leftStartX = leftStartX + bounds.width + gap
|
|
|
+ }
|
|
|
+ resultAreasArray = areasCopyArray
|
|
|
+ } else if stype == .DisVertical {
|
|
|
+ let middleGap = zeroRect.height - topestRect.height - bottomestRect.height
|
|
|
+ let otherAreasTotalHeight = totalHeight - topestRect.height - bottomestRect.height
|
|
|
+
|
|
|
+ let gap = (middleGap - otherAreasTotalHeight)/CGFloat(editingAreas.count - 1)
|
|
|
+ var areasCopyArray : [CPDFEditArea] = editingAreas as! [CPDFEditArea]
|
|
|
+ areasCopyArray.sorted(by: { obj1, obj2 in
|
|
|
+ let area1 = obj1
|
|
|
+ let area2 = obj2
|
|
|
+ if area1.bounds.origin.x < area2.bounds.origin.x {
|
|
|
+ return true
|
|
|
+ } else {
|
|
|
+ return false
|
|
|
+ }
|
|
|
+ })
|
|
|
+ if let index = areasCopyArray.firstIndex(of: topestArea) {
|
|
|
+ areasCopyArray.remove(at: index)
|
|
|
+ }
|
|
|
+
|
|
|
+ if let index = areasCopyArray.firstIndex(of: bottomestArea) {
|
|
|
+ areasCopyArray.remove(at: index)
|
|
|
+ }
|
|
|
+
|
|
|
+ var bottomStartY = bottomestRect.maxY + gap
|
|
|
+ for i in 0 ... areasCopyArray.count-1 {
|
|
|
+ let areas = areasCopyArray[i]
|
|
|
+ var bounds = areas.bounds
|
|
|
+ bounds.origin.y = bottomStartY
|
|
|
+ newBoundsArray.append(NSStringFromRect(bounds))
|
|
|
+ bottomStartY = bottomStartY + bounds.height + gap
|
|
|
+ }
|
|
|
+ resultAreasArray = areasCopyArray
|
|
|
+ }
|
|
|
+
|
|
|
+ var oldBounds : [String] = []
|
|
|
+ for i in 0 ... resultAreasArray.count-1 {
|
|
|
+ let area : CPDFEditArea = resultAreasArray[i] as! CPDFEditArea
|
|
|
+ oldBounds.append(NSStringFromRect(area.bounds))
|
|
|
+ self.listView?.setBoundsEditArea(area, withBounds: NSRectFromString(newBoundsArray[i]))
|
|
|
+ }
|
|
|
+ self.listView?.setNeedsDisplayForVisiblePages()
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
func showPopWindow(positionRect: NSRect) {
|
|
@@ -380,6 +580,8 @@ class KMEditPDfHanddler: NSObject {
|
|
|
win.itemClick = { [weak self] itemKey, obj in
|
|
|
if itemKey == .color {
|
|
|
self?.fontColorAction(color: obj as? NSColor)
|
|
|
+ } else if itemKey == .fontStyle {
|
|
|
+ self?.fontStyleAction(fontName: obj as? String)
|
|
|
} else if itemKey == .fontAdd {
|
|
|
self?.fontAddAction()
|
|
|
} else if itemKey == .fontReduce {
|
|
@@ -389,7 +591,7 @@ class KMEditPDfHanddler: NSObject {
|
|
|
} else if itemKey == .fontItalic {
|
|
|
self?.fontItalicAction()
|
|
|
} else if itemKey == .textAlignment {
|
|
|
- self?.textAlignmentAction()
|
|
|
+ self?.textAlignmentAction(align: obj as? NSTextAlignment ?? .left)
|
|
|
}
|
|
|
// 图片
|
|
|
else if itemKey == .leftRotate {
|
|
@@ -409,21 +611,21 @@ class KMEditPDfHanddler: NSObject {
|
|
|
}
|
|
|
// 对齐
|
|
|
else if itemKey == .alignmentLeft {
|
|
|
- self?.alignmentAction()
|
|
|
+ self?.alignmentAction(align: .Left)
|
|
|
} else if itemKey == .alignmentCenterX {
|
|
|
- self?.alignmentAction()
|
|
|
+ self?.alignmentAction(align: .Horizontally)
|
|
|
} else if itemKey == .alignmentRight {
|
|
|
- self?.alignmentAction()
|
|
|
+ self?.alignmentAction(align: .Right)
|
|
|
} else if itemKey == .alignmentjustifiedX {
|
|
|
- self?.alignmentAction()
|
|
|
+ self?.alignmentAction(align: .DisHorizontally)
|
|
|
} else if itemKey == .alignmentTop {
|
|
|
- self?.alignmentAction()
|
|
|
+ self?.alignmentAction(align: .Top)
|
|
|
} else if itemKey == .alignmentCenterY {
|
|
|
- self?.alignmentAction()
|
|
|
+ self?.alignmentAction(align: .Vertical)
|
|
|
} else if itemKey == .alignmentBottom {
|
|
|
- self?.alignmentAction()
|
|
|
+ self?.alignmentAction(align: .Bottom)
|
|
|
} else if itemKey == .alignmentjustifiedY {
|
|
|
- self?.alignmentAction()
|
|
|
+ self?.alignmentAction(align: .DisVertical)
|
|
|
}
|
|
|
}
|
|
|
|