KMGeneralAnnotationViewController.swift 106 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264
  1. //
  2. // KMGeneralAnnotationViewController.swift
  3. // PDF Master
  4. //
  5. // Created by wanjun on 2023/12/1.
  6. //
  7. import Cocoa
  8. /**
  9. NSString *SKNFreeTextString = @"FreeText";
  10. NSString *SKNTextString = @"Text";
  11. NSString *SKNNoteString = @"Note";
  12. NSString *SKNCircleString = @"Circle";
  13. NSString *SKNSquareString = @"Square";
  14. NSString *SKNMarkUpString = @"MarkUp";
  15. NSString *SKNHighlightString = @"Highlight";
  16. NSString *SKNUnderlineString = @"Underline";
  17. NSString *SKNStrikeOutString = @"StrikeOut";
  18. NSString *SKNLineString = @"Line";
  19. NSString *SKNInkString = @"Ink";
  20. NSString *SKNPDFAnnotationTypeKey = @"type";
  21. NSString *SKNPDFAnnotationBoundsKey = @"bounds";
  22. NSString *SKNPDFAnnotationPageKey = @"page";
  23. NSString *SKNPDFAnnotationPageIndexKey = @"pageIndex";
  24. NSString *SKNPDFAnnotationContentsKey = @"contents";
  25. NSString *SKNPDFAnnotationStringKey = @"string";
  26. NSString *SKNPDFAnnotationColorKey = @"color";
  27. NSString *SKNPDFAnnotationBorderKey = @"border";
  28. NSString *SKNPDFAnnotationLineWidthKey = @"lineWidth";
  29. NSString *SKNPDFAnnotationBorderStyleKey = @"borderStyle";
  30. NSString *SKNPDFAnnotationDashPatternKey = @"dashPattern";
  31. NSString *SKNPDFAnnotationModificationDateKey = @"modificationDate";
  32. NSString *SKNPDFAnnotationUserNameKey = @"userName";
  33. NSString *SKNPDFAnnotationInteriorColorKey = @"interiorColor";
  34. NSString *SKNPDFAnnotationStartLineStyleKey = @"startLineStyle";
  35. NSString *SKNPDFAnnotationEndLineStyleKey = @"endLineStyle";
  36. NSString *SKNPDFAnnotationStartPointKey = @"startPoint";
  37. NSString *SKNPDFAnnotationEndPointKey = @"endPoint";
  38. NSString *SKNPDFAnnotationFontKey = @"font";
  39. NSString *SKNPDFAnnotationFontColorKey = @"fontColor";
  40. NSString *SKNPDFAnnotationFontNameKey = @"fontName";
  41. NSString *SKNPDFAnnotationFontSizeKey = @"fontSize";
  42. NSString *SKNPDFAnnotationAlignmentKey = @"alignment";
  43. NSString *SKNPDFAnnotationRotationKey = @"rotation";
  44. NSString *SKNPDFAnnotationQuadrilateralPointsKey = @"quadrilateralPoints";
  45. NSString *SKNPDFAnnotationIconTypeKey = @"iconType";
  46. NSString *SKNPDFAnnotationPointListsKey = @"pointLists";
  47. */
  48. enum KMActiveAnnotationType: UInt {
  49. case highlight = 0
  50. case strikeOut
  51. case underline
  52. case inkAndLine
  53. case text
  54. case squareAndCircle
  55. case freeText
  56. case selfSignFreeText
  57. }
  58. enum KMFreeTextAnnotationAlignmentType: UInt {
  59. case left = 0
  60. case center
  61. case right
  62. }
  63. let KMColorPickerViewHeight: CGFloat = 64
  64. @objcMembers class KMGeneralAnnotationViewController: NSViewController, NSTextViewDelegate {
  65. var _annotations: [CPDFAnnotation]?
  66. var subType: KMSelfSignAnnotationFreeTextSubType = .none
  67. var _activeAnnotationType: KMActiveAnnotationType = .highlight
  68. var pdfView: CPDFListView?
  69. var annotationModel: CPDFAnnotationModel?
  70. // var annotationCallback: ((CPDFAnnotation) -> Void)?
  71. @IBOutlet private weak var imageBox: NSBox!
  72. @IBOutlet private weak var imageBoxLayoutConstraint: NSLayoutConstraint!
  73. @IBOutlet private weak var textImageBoxView: NSView!
  74. @IBOutlet private weak var textImageBox: KMBox!
  75. @IBOutlet private weak var textAnnotationImageView: NSImageView!
  76. @IBOutlet private weak var textImageBoxButton: NSButton!
  77. @IBOutlet private weak var textImageUpDateButton: NSButton!
  78. @IBOutlet private weak var generalImageBoxView: NSView!
  79. @IBOutlet private weak var generalImageView: NSImageView!
  80. @IBOutlet private weak var fontView: NSView! // 字体
  81. @IBOutlet private weak var fontLabel: NSTextField!
  82. @IBOutlet private weak var fontButton: NSButton!
  83. @IBOutlet private weak var fontPopUpButton: KMPopUpButton!
  84. @IBOutlet private weak var fontSizeComboBox: KMComboBox!
  85. @IBOutlet private weak var fontStylePopUpButton: KMPopUpButton!
  86. @IBOutlet private weak var leftAlignButton: NSButton!
  87. @IBOutlet private weak var centerAlignButton: NSButton!
  88. @IBOutlet private weak var rightAlignButton: NSButton!
  89. @IBOutlet private weak var fontViewTopConstant: NSLayoutConstraint!
  90. @IBOutlet private weak var fontViewCorloPV: KMColorPickerView!
  91. @IBOutlet private weak var fontViewColorPVConstraint: NSLayoutConstraint!
  92. @IBOutlet private weak var rotateView: NSView! // 旋转
  93. @IBOutlet private weak var rotateLabel: NSTextField!
  94. @IBOutlet private weak var leftRotateBox: KMBox!
  95. @IBOutlet private weak var leftRotateButton: NSButton!
  96. @IBOutlet private weak var rightRotateBox: KMBox!
  97. @IBOutlet private weak var rightRotateButton: NSButton!
  98. @IBOutlet private weak var rotateViewTopConstant: NSLayoutConstraint!
  99. @IBOutlet private weak var typeView: NSView! // 类型
  100. @IBOutlet private weak var typeLabel: NSTextField!
  101. @IBOutlet private weak var tureTypeButton: NSButton!
  102. @IBOutlet private weak var falseTypeButton: NSButton!
  103. @IBOutlet private weak var circletypeButton: NSButton!
  104. @IBOutlet private weak var lineTypeButton: NSButton!
  105. @IBOutlet private weak var dotTypeButton: NSButton!
  106. @IBOutlet private weak var typeViewTopConstant: NSLayoutConstraint!
  107. @IBOutlet private weak var dateView: NSView! // 日期
  108. @IBOutlet private weak var dateLabel: NSTextField!
  109. @IBOutlet private weak var datePopUpButton: KMPopUpButton!
  110. @IBOutlet private weak var dateCheckButton: NSButton!
  111. @IBOutlet private weak var dateViewTopConstant: NSLayoutConstraint!
  112. @IBOutlet private weak var iconView: NSView! // 图标
  113. @IBOutlet private weak var iconLabel: NSTextField!
  114. @IBOutlet private weak var commentBox: NSBox!
  115. @IBOutlet private weak var commentButton: NSButton!
  116. @IBOutlet private weak var noteBox: NSBox!
  117. @IBOutlet private weak var noteButton: NSButton!
  118. @IBOutlet private weak var keyBox: NSBox!
  119. @IBOutlet private weak var keyButton: NSButton!
  120. @IBOutlet private weak var helpBox: NSBox!
  121. @IBOutlet private weak var helpButton: NSButton!
  122. @IBOutlet private weak var paragraphBox: NSBox!
  123. @IBOutlet private weak var paragraphButton: NSButton!
  124. @IBOutlet private weak var insertBox: NSBox!
  125. @IBOutlet private weak var insertButton: NSButton!
  126. @IBOutlet private weak var addParagraphBox: NSBox!
  127. @IBOutlet private weak var addParagraphButton: NSButton!
  128. @IBOutlet private weak var iconViewTopConstant: NSLayoutConstraint!
  129. @IBOutlet private weak var iconButtonHeightConstant: NSLayoutConstraint!
  130. @IBOutlet private weak var borderAndLineView: NSView! // 边框
  131. @IBOutlet private weak var borderLabel: NSTextField!
  132. @IBOutlet private weak var borderButton: NSButton!
  133. @IBOutlet private weak var lineWidthComboBox: KMComboBox!
  134. @IBOutlet private weak var lineWidthSlider: NSSlider!
  135. @IBOutlet private weak var lineTypeLabel: NSTextField!
  136. @IBOutlet private weak var lineStyleButton: KMButton!
  137. @IBOutlet private weak var dottedLineStyleButton: KMButton!
  138. @IBOutlet private weak var rightArrowStyleButton: KMButton!
  139. @IBOutlet private weak var lineStyleBox: NSBox!
  140. @IBOutlet private weak var dottedLineStyleBox: NSBox!
  141. @IBOutlet private weak var rightArrowStyleBox: NSBox!
  142. @IBOutlet private weak var borderViewTopConstant: NSLayoutConstraint!
  143. @IBOutlet private weak var fillColorView: NSView!
  144. @IBOutlet private weak var fillColorLabel: NSTextField!
  145. @IBOutlet private weak var fillColorPickerView: KMColorPickerView!
  146. @IBOutlet private weak var fillColorViewTopConstraint: NSLayoutConstraint!
  147. @IBOutlet private weak var opacityView: NSView! // 透明度
  148. @IBOutlet private weak var opacityLabel: NSTextField!
  149. @IBOutlet private weak var opacitySlider: NSSlider!
  150. @IBOutlet private weak var opacityComboBox: KMComboBox!
  151. @IBOutlet private weak var opacityViewTopConstant: NSLayoutConstraint!
  152. @IBOutlet private weak var noteView: NSView! // 笔记
  153. @IBOutlet private weak var noteViewLabel: NSTextField!
  154. @IBOutlet private var noteTextView: NSTextView!
  155. @IBOutlet private weak var noteViewTopConstant: NSLayoutConstraint!
  156. @IBOutlet private weak var colorView: NSView! // 颜色
  157. @IBOutlet private weak var colorTitleLabel: NSTextField!
  158. @IBOutlet private weak var textColorBox: NSBox!
  159. @IBOutlet private weak var textColorBoxConstraint: NSLayoutConstraint!
  160. @IBOutlet private weak var textColorPickerView: KMColorPickerView!
  161. @IBOutlet private weak var borderColorBox: NSBox!
  162. @IBOutlet private weak var borderColorPickerView: KMColorPickerView!
  163. @IBOutlet private weak var borderColorBoxConstraint: NSLayoutConstraint!
  164. @IBOutlet private weak var lineWidthBox: NSBox!
  165. @IBOutlet private weak var widthLabel: NSTextField!
  166. @IBOutlet private weak var widthComboBox: KMComboBox!
  167. @IBOutlet private weak var widthSlider: NSSlider!
  168. @IBOutlet private weak var ColorViewTopConstant: NSLayoutConstraint!
  169. @IBOutlet private weak var textColorPickerViewConstant: NSLayoutConstraint!
  170. var isannotationMode: Bool = false
  171. var selectedAlignmentButtonLayer: CALayer?
  172. var iconButtonArray: [NSBox] = []
  173. var lineStyleCount: Int = 0
  174. var isTextEdit: Bool = false
  175. var fonts: [[String : [String]]]!
  176. var noteIcons: [NSImage?] = [nil, nil, nil, nil, nil, nil, nil]
  177. private func makeNoteIcons() {
  178. if noteIcons[0] != nil { return }
  179. let bounds = CGRect(origin: .zero, size: NSMakeSize(16, 16))
  180. let annotation = CPDFTextAnnotation()
  181. annotation.bounds = bounds
  182. annotation.color = NSColor.clear
  183. let page = CPDFPage()
  184. page.setBounds(bounds, for: .mediaBox)
  185. page.addAnnotation(annotation)
  186. for i in 0..<7 {
  187. annotation.setIconType(CPDFTextAnnotationIconType(rawValue: i) ?? .comment)
  188. noteIcons[i] = NSImage.bitmapImage(with: NSMakeSize(16, 16), drawingHandler: { rect in
  189. page.draw(with: .mediaBox, to: (NSGraphicsContext.current?.cgContext))
  190. })
  191. }
  192. }
  193. deinit {
  194. NotificationCenter.default.removeObserver(self)
  195. if ((annotationModel?.annotation) != nil) && annotationModel?.annotations.count == 1 {
  196. let keys = ["contents", "markupText"]
  197. for key in keys {
  198. annotationModel?.annotation.removeObserver(self, forKeyPath: key)
  199. }
  200. }
  201. textColorPickerView.target = nil
  202. textColorPickerView.action = nil
  203. borderColorPickerView.target = nil
  204. borderColorPickerView.action = nil
  205. fillColorPickerView.target = nil
  206. fillColorPickerView.action = nil
  207. NSColorPanel.shared.setTarget(nil)
  208. NSColorPanel.shared.setAction(nil)
  209. }
  210. // MARK: View Methods
  211. override func loadView() {
  212. super.loadView()
  213. DispatchQueue.main.async {
  214. if NSColorPanel.shared.isVisible {
  215. NSColorPanel.shared.close()
  216. }
  217. }
  218. isannotationMode = true
  219. fonts = (CPDFAnnotationModel.supportFonts() as! [[String : [String]]])
  220. imageBox.borderColor = KMAppearance.Interactive.s0Color()
  221. imageBox.borderWidth = 1.0
  222. imageBox.fillColor = KMAppearance.Layout.l1Color()
  223. textImageBox.fillColor = KMAppearance.Layout.l1Color()
  224. fontLabel.stringValue = NSLocalizedString("Fonts", comment: "")
  225. fontLabel.textColor = KMAppearance.Layout.h0Color()
  226. colorTitleLabel.stringValue = NSLocalizedString("Color", comment: "")
  227. colorTitleLabel.textColor = KMAppearance.Layout.h0Color()
  228. opacityLabel.stringValue = NSLocalizedString("Opacity", comment: "")
  229. opacityLabel.textColor = KMAppearance.Layout.h1Color()
  230. noteViewLabel.stringValue = NSLocalizedString("Note", comment: "")
  231. noteViewLabel.textColor = KMAppearance.Layout.h0Color()
  232. rotateLabel.stringValue = NSLocalizedString("Rotate", comment: "")
  233. rotateLabel.textColor = KMAppearance.Layout.h0Color()
  234. typeLabel.stringValue = NSLocalizedString("Type", comment: "")
  235. typeLabel.textColor = KMAppearance.Layout.h0Color()
  236. dateLabel.stringValue = NSLocalizedString("Date", comment: "")
  237. dateLabel.textColor = KMAppearance.Layout.h0Color()
  238. iconLabel.stringValue = NSLocalizedString("Icon", comment: "")
  239. iconLabel.textColor = KMAppearance.Layout.h0Color()
  240. borderLabel.stringValue = NSLocalizedString("Line and Border Style", comment: "")
  241. borderLabel.textColor = KMAppearance.Layout.h0Color()
  242. fillColorLabel.stringValue = NSLocalizedString("Fill", comment: "")
  243. fillColorLabel.textColor = KMAppearance.Layout.h0Color()
  244. lineStyleCount = 0
  245. if annotationType == .highlight {
  246. activeAnnotationType = .highlight
  247. } else if annotationType == .strikeOut {
  248. activeAnnotationType = .strikeOut
  249. } else if annotationType == .underline {
  250. activeAnnotationType = .underline
  251. } else if annotationType == .ink || annotationType == .line || annotationType == .arrow {
  252. activeAnnotationType = .inkAndLine
  253. } else if annotationType == .anchored {
  254. activeAnnotationType = .text
  255. } else if annotationType == .circle || annotationType == .square {
  256. borderColorPickerView.isCallColorPanelAction = isannotationMode
  257. textColorPickerView.isCallColorPanelAction = isannotationMode
  258. activeAnnotationType = .squareAndCircle
  259. } else if annotationType == .freeText {
  260. if subType == .date {
  261. colorTitleLabel.stringValue = NSLocalizedString("Fill Color", comment: "")
  262. activeAnnotationType = .selfSignFreeText
  263. } else {
  264. activeAnnotationType = .freeText
  265. }
  266. fontViewCorloPV.isCallColorPanelAction = isannotationMode
  267. var alignmentType: KMFreeTextAnnotationAlignmentType = .left
  268. if annotationModel?.alignment() == .left {
  269. alignmentType = .left
  270. } else if annotationModel?.alignment() == .center {
  271. alignmentType = .center
  272. } else if annotationModel?.alignment() == .right {
  273. alignmentType = .right
  274. }
  275. alignmentTypeSelected(alignmentType)
  276. }
  277. textColorPickerView.isCallColorPanelAction = isannotationMode
  278. borderColorPickerView.isCallColorPanelAction = isannotationMode
  279. if annotationType == .freeText {
  280. fontViewCorloPV.isFreeText = true
  281. } else {
  282. fontViewCorloPV.isFreeText = false
  283. }
  284. if annotationType == .ink || annotationType == .line || annotationType == .arrow || annotationType == .square || annotationType == .circle {
  285. let lineType = annotationModel?.style()
  286. if lineType == .dashed {
  287. let lineType = annotationModel?.dashPattern()
  288. // if lineType.count == 0 {
  289. // let number = NSNumber(value: Float(4))
  290. // annotation.setDashPattern([number])
  291. //
  292. // let userDefaults = UserDefaults.standard
  293. // if annotationType == .ink {
  294. // userDefaults.set(annotationModel?.dashPattern(), forKey: SKInkNoteDashPatternKey)
  295. // } else if annotationType == .line {
  296. // userDefaults.set(annotationModel?.dashPattern(), forKey: SKLineNoteDashPatternKey)
  297. // } else if annotationType == .square {
  298. // userDefaults.set(annotationModel?.dashPattern(), forKey: SKSquareNoteDashPatternKey)
  299. // } else if annotationType == .circle {
  300. // userDefaults.set(annotationModel?.dashPattern(), forKey: SKCircleNoteDashPatternKey)
  301. // } else if annotationType == .freeText {
  302. // userDefaults.set(annotationModel?.dashPattern(), forKey: CFreeTextNoteDashPatternKey)
  303. // }
  304. // }
  305. }
  306. }
  307. opacityComboBox.type = .none
  308. opacityComboBox.comboxRect = opacityComboBox.bounds
  309. lineWidthComboBox.type = .none
  310. lineWidthComboBox.comboxRect = lineWidthComboBox.bounds
  311. widthComboBox.type = .none
  312. widthComboBox.comboxRect = widthComboBox.bounds
  313. fontPopUpButton.type = .arrowDown
  314. fontSizeComboBox.type = .none
  315. fontSizeComboBox.comboxRect = fontSizeComboBox.bounds
  316. fontStylePopUpButton.type = .arrowUpDown
  317. datePopUpButton.type = .arrowUpDown
  318. noteTextView.backgroundColor = KMAppearance.Layout.l1Color()
  319. let views: [NSView] = [fontPopUpButton, fontSizeComboBox, fontStylePopUpButton, lineWidthComboBox, opacityComboBox, widthComboBox, datePopUpButton, noteTextView]
  320. for view in views {
  321. view.wantsLayer = true
  322. view.layer?.borderWidth = 1.0
  323. view.layer?.cornerRadius = 1.0
  324. }
  325. refreshLineTypeUI()
  326. rightArrowStyleButton.isHidden = true
  327. rightArrowStyleBox.isHidden = true
  328. annotationFontReload()
  329. reloadData()
  330. updateViewColor()
  331. if ((annotationModel?.annotation) != nil) && annotationModel?.annotations.count == 1 {
  332. let annotation = annotationModel?.annotation
  333. let keys = ["contents", "markupText"]
  334. for key in keys {
  335. annotation?.addObserver(self, forKeyPath: key, options: [.new, .old], context: nil)
  336. }
  337. }
  338. // NotificationCenter.default.addObserver(self, selector: #selector(alignmentTypeNotification(_:)), name: "KMAnnotationAlignmentTypeNotification1", object: nil)
  339. // NotificationCenter.default.addObserver(self, selector: #selector(loadingUIAndLocalization(_:)), name: "KMPreferenceDidChangeNotificationName", object: nil)
  340. // NotificationCenter.default.addObserver(self, selector: #selector(annotationChangeNotification(_:)), name: CPDFListViewAnnotationsAttributeHasChangeNotification, object: nil)
  341. }
  342. override func viewDidLoad() {
  343. super.viewDidLoad()
  344. // Do view setup here.
  345. }
  346. override func viewDidAppear() {
  347. super.viewDidAppear()
  348. let showConvertDetails = KMPropertiesViewPopController.showChangeColorDetails()
  349. let fileURL = (self.view.window?.windowController?.document as? NSDocument)?.fileURL
  350. if showConvertDetails && (fileURL != nil) {
  351. KMPropertiesViewPopController.defaultManager().showChangeColorDetailsView(self.textColorPickerView.firstButton)
  352. }
  353. }
  354. func reloadData() {
  355. var annotationColor = annotationModel?.color()
  356. if annotationType == .signFalse ||
  357. annotationType == .signTure ||
  358. annotationType == .signDot ||
  359. annotationType == .signCircle ||
  360. annotationType == .signLine {
  361. annotationColor = annotationModel?.color()
  362. } else if annotationType == .square ||
  363. annotationType == .circle {
  364. annotationColor = annotationModel?.color()
  365. } else if annotationType == .freeText || annotationType == .signDate || annotationType == .signText {
  366. annotationColor = annotationModel?.interiorColor()
  367. } else {
  368. annotationColor = annotationModel?.color()
  369. }
  370. guard let color = annotationColor else { return }
  371. var opacity: CGFloat = 1.0
  372. color.usingColorSpaceName(.calibratedRGB)?.getRed(nil, green: nil, blue: nil, alpha: &opacity)
  373. if annotationType == .signFalse ||
  374. annotationType == .signTure ||
  375. annotationType == .signDot ||
  376. annotationType == .signCircle ||
  377. annotationType == .signLine {
  378. opacity = (annotationModel?.opacity())!
  379. } else if annotationType == .square ||
  380. annotationType == .circle {
  381. opacity = (annotationModel?.opacity())!
  382. } else if annotationType == .freeText || annotationType == .signDate || annotationType == .signText {
  383. opacity = (annotationModel?.interiorOpacity())!
  384. } else {
  385. opacity = (annotationModel?.opacity())!
  386. }
  387. opacitySlider.floatValue = Float(opacity)
  388. opacitySlider.toolTip = "\(Int(opacity * 100))%"
  389. opacityComboBox.stringValue = "\(Int(opacity * 100))%"
  390. if annotationModel?.annotations != nil {
  391. let contextString = annotation.string() ?? ""
  392. noteTextView.string = contextString
  393. noteTextView.textColor = NSColor.labelColor
  394. noteTextView.delegate = self
  395. }
  396. if annotationType == .freeText {
  397. imageBox.contentView = textImageBoxView
  398. textAnnotationImageView.image = annotationModel?.annotationImage
  399. } else if annotationType == .signText || annotationType == .signDate {
  400. imageBox.contentView = generalImageBoxView
  401. generalImageView.image = annotationModel?.annotationImage
  402. } else {
  403. imageBox.contentView = generalImageBoxView
  404. generalImageView.image = annotationModel?.annotationImage
  405. }
  406. if annotationType == .square || annotationType == .circle || annotationType == .line || annotationType == .arrow || annotationType == .ink {
  407. if annotationType == .ink {
  408. textColorPickerView.setColor(color)
  409. } else if annotationType == .square || annotationType == .circle {
  410. var red: CGFloat = 0.0, green: CGFloat = 0.0, blue: CGFloat = 0.0, opacity: CGFloat = 0.0
  411. let modelColor = annotationModel?.interiorColor() ?? .black
  412. modelColor.usingColorSpaceName(.calibratedRGB)?.getRed(&red, green: &green, blue: &blue, alpha: &opacity)
  413. let interiorColor = NSColor(red: red, green: green, blue: blue, alpha: (annotationModel?.interiorOpacity())!)
  414. textColorPickerView.setColor(interiorColor)
  415. textColorPickerView.isFillColor = true
  416. borderColorPickerView.setColor(color)
  417. } else if annotationType == .line || annotationType == .arrow {
  418. var red: CGFloat = 0.0, green: CGFloat = 0.0, blue: CGFloat = 0.0, opacity: CGFloat = 0.0
  419. let modelColor = annotationModel?.interiorColor() ?? .black
  420. modelColor.usingColorSpaceName(.calibratedRGB)?.getRed(&red, green: &green, blue: &blue, alpha: &opacity)
  421. let interiorColor = NSColor(red: red, green: green, blue: blue, alpha: (annotationModel?.interiorOpacity())!)
  422. textColorPickerView.setColor(interiorColor)
  423. textColorPickerView.isFillColor = true
  424. borderColorPickerView.setColor(color)
  425. }
  426. lineWidthSlider.floatValue = Float(annotationModel?.lineWidth() ?? 1.0)
  427. lineWidthSlider.toolTip = "\(annotationModel?.lineWidth() ?? 1.0) pt"
  428. lineWidthComboBox.stringValue = String(format: "%0.1f pt", (annotationModel?.lineWidth())!)
  429. } else if annotationType == .freeText || annotationType == .signText || annotationType == .signDate {
  430. if annotationType == .signText || annotationType == .signDate {
  431. if subType == .date {
  432. dateView.isHidden = false
  433. datePopUpButton.removeAllItems()
  434. let freeText = annotation as! KMSelfSignAnnotationFreeText
  435. // self.datePopUpButton.addItems(withTitles: KMSelfSignAnnotationFreeText.fetchAllDateString(includeTime: <#T##Bool#>))
  436. if freeText.dateFormatIndex >= 0, freeText.dateFormatIndex < datePopUpButton.numberOfItems {
  437. datePopUpButton.selectItem(at: freeText.dateFormatIndex)
  438. } else {
  439. datePopUpButton.selectItem(at: 0)
  440. }
  441. dateCheckButton.state = freeText.includeTime ? NSControl.StateValue.on : NSControl.StateValue.off
  442. textColorPickerView.setColor(annotation.color)
  443. }
  444. } else {
  445. lineWidthSlider.minValue = 0.0
  446. lineWidthSlider.floatValue = Float((annotationModel?.lineWidth())!)
  447. lineWidthSlider.toolTip = String(format: "%0.1f pt", (annotationModel?.lineWidth())!)
  448. lineWidthComboBox.stringValue = String(format: "%0.1f pt", (annotationModel?.lineWidth())!)
  449. var alignmentType: KMFreeTextAnnotationAlignmentType = .left
  450. if annotationModel?.alignment() == .left {
  451. alignmentType = .left
  452. } else if annotationModel?.alignment() == .center {
  453. alignmentType = .center
  454. } else if annotationModel?.alignment() == .right {
  455. alignmentType = .right
  456. }
  457. alignmentTypeSelected(alignmentType)
  458. annotationFontReload()
  459. fillColorPickerView.color = annotationModel?.color()
  460. }
  461. fontViewCorloPV.color = annotationModel?.fontColor()
  462. fontSizeComboBox.stringValue = String(format: "%0.1f pt", (annotationModel?.fontSize())!)
  463. } else if annotationType == .highlight || annotationType == .strikeOut || annotationType == .underline || annotationType == .anchored {
  464. textColorPickerView.setColor(color)
  465. }
  466. noteView.isHidden = isannotationMode
  467. if annotationType == .signText || annotationType == .signDate {
  468. if subType == .date {
  469. noteView.isHidden = true
  470. } else {
  471. noteView.isHidden = false
  472. }
  473. }
  474. if annotationModel?.annotations != nil {
  475. noteView.isHidden = annotationModel?.annotation==nil
  476. } else {
  477. if annotationType == .stamp ||
  478. annotationType == .ink ||
  479. annotationType == .square ||
  480. annotationType == .circle ||
  481. annotationType == .arrow ||
  482. annotationType == .line ||
  483. annotationType == .highlight ||
  484. annotationType == .strikeOut ||
  485. annotationType == .underline ||
  486. annotationType == .freeText {
  487. noteView.isHidden = true
  488. } else {
  489. noteView.isHidden = annotationModel?.annotation==nil
  490. }
  491. }
  492. }
  493. // MARK: Set & Get
  494. var annotations: [CPDFAnnotation] {
  495. get {
  496. return annotationModel?.annotations as! [CPDFAnnotation]
  497. }
  498. }
  499. var annotation: CPDFAnnotation {
  500. get {
  501. return (annotationModel?.annotation)!
  502. }
  503. }
  504. var annotationType: CAnnotationType {
  505. get {
  506. return (annotationModel?.annotationType as? CAnnotationType)!
  507. }
  508. }
  509. func updateannotationMode() {
  510. let defaults = UserDefaults.standard
  511. if annotationType == .freeText {
  512. // if ([annotation isKindOfClass:[SKNPDFAnnotationNote class]]) {
  513. // [sud setColor:self.annotation.color forKey:SKNPDFAnnotationColorKey];
  514. // [sud setInteger:self.annotation.iconType forKey:SKAnchoredNoteIconTypeKey];
  515. // }
  516. if annotation.isKind(of: CPDFFreeTextAnnotation.self) {
  517. defaults.setColor(annotation.color, forKey: CFreeTextNoteColorKey)
  518. defaults.setColor((annotation as! CPDFFreeTextAnnotation).fontColor, forKey: CFreeTextNoteFontColorKey)
  519. defaults.set((annotation as! CPDFFreeTextAnnotation).alignment, forKey: CFreeTextNoteFontNameKey)
  520. defaults.set((annotation as! CPDFFreeTextAnnotation).fontName(), forKey: CFreeTextNoteFontNameKey)
  521. defaults.set((annotation as! CPDFFreeTextAnnotation).fontSize(), forKey: CFreeTextNoteFontSizeKey)
  522. defaults.set(annotation.lineWidth(), forKey: CFreeTextNoteLineWidthKey)
  523. defaults.set(annotation.borderStyle(), forKey: CFreeTextNoteLineStyleKey)
  524. defaults.set(annotation.dashPattern(), forKey: CFreeTextNoteDashPatternKey)
  525. }
  526. } else if annotationType == .circle {
  527. defaults.setColor(annotation.color, forKey: CCircleNoteColorKey)
  528. defaults.setColor((annotation as! CPDFCircleAnnotation).interiorColor, forKey: CCircleNoteInteriorColorKey)
  529. defaults.set(annotation.lineWidth(), forKey: CCircleNoteLineWidthKey)
  530. defaults.set((annotation as! CPDFCircleAnnotation).borderStyle(), forKey: CCircleNoteLineStyleKey)
  531. defaults.set((annotation as! CPDFCircleAnnotation).dashPattern(), forKey: CCircleNoteDashPatternKey)
  532. } else if annotationType == .square {
  533. defaults.setColor(annotation.color, forKey: CSquareNoteColorKey)
  534. defaults.setColor((annotation as! CPDFSquareAnnotation).interiorColor, forKey: CSquareNoteInteriorColorKey)
  535. defaults.set(annotation.lineWidth(), forKey: CSquareNoteLineWidthKey)
  536. defaults.set((annotation as! CPDFSquareAnnotation).borderStyle(), forKey: CSquareNoteLineStyleKey)
  537. defaults.set((annotation as! CPDFSquareAnnotation).dashPattern(), forKey: CSquareNoteDashPatternKey)
  538. } else if annotationType == .highlight {
  539. defaults.setColor(annotation.color, forKey: SKHighlightNoteColorKey)
  540. } else if annotationType == .underline {
  541. defaults.setColor(annotation.color, forKey: CUnderlineNoteColorKey)
  542. } else if annotationType == .strikeOut {
  543. defaults.setColor(annotation.color, forKey: CStrikeOutNoteColorKey)
  544. } else if annotationType == .line || annotationType == .arrow {
  545. defaults.setColor(annotation.color, forKey: CLineNoteColorKey)
  546. defaults.set(annotation.lineWidth(), forKey: CLineNoteLineWidthKey)
  547. defaults.set(annotation.borderStyle(), forKey: CLineNoteLineStyleKey)
  548. defaults.set(annotation.dashPattern(), forKey: CLineNoteDashPatternKey)
  549. defaults.setColor((annotation as! CPDFLineAnnotation).interiorColor, forKey: CLineNoteInteriorColorKey)
  550. defaults.set((annotation as! CPDFLineAnnotation).startLineStyle, forKey: CLineNoteStartLineStyleKey)
  551. defaults.set((annotation as! CPDFLineAnnotation).endLineStyle, forKey: CLineNoteEndLineStyleKey)
  552. } else if annotationType == .ink {
  553. defaults.setColor(annotation.color, forKey: CInkNoteColorKey)
  554. defaults.set(annotation.lineWidth(), forKey: CInkNoteLineWidthKey)
  555. defaults.set(annotation.borderStyle(), forKey: CInkNoteLineStyleKey)
  556. defaults.set(annotation.dashPattern(), forKey: CInkNoteDashPatternKey)
  557. } else if annotationType == .anchored {
  558. // [sud setColor:self.annotation.color forKey:SKNPDFAnnotationColorKey];
  559. // [sud setInteger:self.annotation.iconType forKey:SKAnchoredNoteIconTypeKey];
  560. }
  561. }
  562. var activeAnnotationType: KMActiveAnnotationType {
  563. set {
  564. _activeAnnotationType = newValue
  565. switch self.activeAnnotationType {
  566. case .highlight, .strikeOut, .underline:
  567. self.fontView.isHidden = true
  568. self.borderAndLineView.isHidden = true
  569. self.fillColorView.isHidden = true
  570. self.rotateView.isHidden = true
  571. self.typeView.isHidden = true
  572. self.dateView.isHidden = true
  573. self.iconView.isHidden = true
  574. self.createMarkupProperties()
  575. case .inkAndLine:
  576. self.fontView.isHidden = true
  577. self.fillColorView.isHidden = true
  578. self.rotateView.isHidden = true
  579. self.typeView.isHidden = true
  580. self.dateView.isHidden = true
  581. self.iconView.isHidden = true
  582. self.createInkAndLineProperties()
  583. case .text:
  584. self.fontView.isHidden = true
  585. self.borderAndLineView.isHidden = true
  586. self.fillColorView.isHidden = true
  587. self.rotateView.isHidden = true
  588. self.typeView.isHidden = true
  589. self.dateView.isHidden = true
  590. self.createTextProperties()
  591. case .squareAndCircle:
  592. self.fontView.isHidden = true
  593. self.rotateView.isHidden = true
  594. self.typeView.isHidden = true
  595. self.dateView.isHidden = true
  596. self.iconView.isHidden = true
  597. self.fillColorView.isHidden = true
  598. self.createSquareAndCircleProperties()
  599. case .freeText:
  600. self.colorView.isHidden = true
  601. self.rotateView.isHidden = true
  602. self.typeView.isHidden = true
  603. self.dateView.isHidden = true
  604. self.iconView.isHidden = true
  605. self.createFreeTextProperties()
  606. case .selfSignFreeText:
  607. self.rotateView.isHidden = true
  608. self.borderAndLineView.isHidden = true
  609. self.fillColorView.isHidden = true
  610. self.typeView.isHidden = true
  611. self.iconView.isHidden = true
  612. self.createSelfSignFreeTextProperties()
  613. default:
  614. break
  615. }
  616. }
  617. get {
  618. return _activeAnnotationType
  619. }
  620. }
  621. // MARK: private
  622. func updateViewColor() {
  623. if KMAppearance.isDarkMode() {
  624. let darkColor = NSColor(red: 57/255.0, green: 60/255.0, blue: 62/255.0, alpha: 1.0).cgColor
  625. let borderColor = NSColor(red: 86/255.0, green: 88/255.0, blue: 90/255.0, alpha: 1.0).cgColor
  626. setViewColor(fontPopUpButton, darkColor, borderColor)
  627. setViewColor(fontSizeComboBox, darkColor, borderColor)
  628. setViewColor(fontStylePopUpButton, darkColor, borderColor)
  629. setViewColor(lineWidthComboBox, darkColor, borderColor)
  630. setViewColor(opacityComboBox, darkColor, borderColor)
  631. setViewColor(datePopUpButton, darkColor, borderColor)
  632. setViewColor(widthComboBox, darkColor, borderColor)
  633. setViewColor(noteTextView, darkColor, borderColor)
  634. } else {
  635. let lightColor = NSColor.white.cgColor
  636. let borderColor = NSColor(red: 218/255.0, green: 219/255.0, blue: 222/255.0, alpha: 1.0).cgColor
  637. setViewColor(fontPopUpButton, lightColor, borderColor)
  638. setViewColor(fontSizeComboBox, lightColor, borderColor)
  639. setViewColor(fontStylePopUpButton, lightColor, borderColor)
  640. setViewColor(lineWidthComboBox, lightColor, borderColor)
  641. setViewColor(opacityComboBox, lightColor, borderColor)
  642. setViewColor(datePopUpButton, lightColor, borderColor)
  643. setViewColor(widthComboBox, lightColor, borderColor)
  644. setViewColor(noteTextView, lightColor, borderColor)
  645. }
  646. }
  647. func setViewColor(_ view: NSView, _ backgroundColor: CGColor, _ borderColor: CGColor) {
  648. view.layer?.backgroundColor = backgroundColor
  649. view.layer?.borderColor = borderColor
  650. }
  651. func refreshLineTypeUI() {
  652. lineStyleBox.borderColor = KMAppearance.Interactive.s0Color()
  653. dottedLineStyleBox.borderColor = KMAppearance.Interactive.s0Color()
  654. rightArrowStyleBox.borderColor = KMAppearance.Interactive.s0Color()
  655. lineStyleBox.fillColor = KMAppearance.Layout.l1Color()
  656. dottedLineStyleBox.fillColor = KMAppearance.Layout.l1Color()
  657. rightArrowStyleBox.fillColor = KMAppearance.Layout.l1Color()
  658. lineStyleBox.borderWidth = 1
  659. dottedLineStyleBox.borderWidth = 1
  660. rightArrowStyleBox.borderWidth = 1
  661. if lineStyleCount == 0 {
  662. lineStyleBox.fillColor = KMAppearance.Status.selColor()
  663. lineStyleBox.borderWidth = 0.0
  664. } else if lineStyleCount == 1 {
  665. dottedLineStyleBox.fillColor = KMAppearance.Status.selColor()
  666. dottedLineStyleBox.borderWidth = 0
  667. } else if lineStyleCount == 2 {
  668. rightArrowStyleBox.fillColor = KMAppearance.Status.selColor()
  669. rightArrowStyleBox.borderWidth = 0
  670. }
  671. }
  672. func hiddenSubviews() {
  673. fontViewTopConstant.constant = fontView.isHidden ? -(fontView.bounds.size.height) - 20 : 10
  674. ColorViewTopConstant.constant = colorView.isHidden ? -(colorView.bounds.size.height) : 10
  675. borderViewTopConstant.constant = borderAndLineView.isHidden ? -(borderAndLineView.bounds.size.height) : 20
  676. opacityViewTopConstant.constant = opacityView.isHidden ? -opacityView.bounds.size.height : 20
  677. rotateViewTopConstant.constant = rotateView.isHidden ? -(rotateView.bounds.size.height) : 20
  678. typeViewTopConstant.constant = typeView.isHidden ? -(typeView.bounds.size.height) : 20
  679. dateViewTopConstant.constant = dateView.isHidden ? -(dateView.bounds.size.height) : 20
  680. iconViewTopConstant.constant = iconView.isHidden ? -(iconView.bounds.size.height) : 20
  681. noteViewTopConstant.constant = noteView.isHidden ? -(noteView.bounds.size.height) : 20
  682. fillColorViewTopConstraint.constant = fillColorView.isHidden ? -fillColorView.bounds.size.height : 20
  683. fontViewColorPVConstraint.constant = fontViewCorloPV.isHidden ? -fontViewCorloPV.bounds.size.height : 10
  684. }
  685. func setActiveAnnotationType(_ activeAnnotationType: KMActiveAnnotationType) {
  686. self.activeAnnotationType = activeAnnotationType
  687. switch activeAnnotationType {
  688. case .highlight, .strikeOut, .underline:
  689. fontView.isHidden = true
  690. borderAndLineView.isHidden = true
  691. fillColorView.isHidden = true
  692. rotateView.isHidden = true
  693. typeView.isHidden = true
  694. dateView.isHidden = true
  695. iconView.isHidden = true
  696. createMarkupProperties()
  697. case .inkAndLine:
  698. fontView.isHidden = true
  699. fillColorView.isHidden = true
  700. rotateView.isHidden = true
  701. typeView.isHidden = true
  702. dateView.isHidden = true
  703. iconView.isHidden = true
  704. createInkAndLineProperties()
  705. case .text:
  706. fontView.isHidden = true
  707. borderAndLineView.isHidden = true
  708. fillColorView.isHidden = true
  709. rotateView.isHidden = true
  710. typeView.isHidden = true
  711. dateView.isHidden = true
  712. createTextProperties()
  713. case .squareAndCircle:
  714. fontView.isHidden = true
  715. rotateView.isHidden = true
  716. typeView.isHidden = true
  717. dateView.isHidden = true
  718. iconView.isHidden = true
  719. fillColorView.isHidden = true
  720. createSquareAndCircleProperties()
  721. case .freeText:
  722. colorView.isHidden = true
  723. rotateView.isHidden = true
  724. typeView.isHidden = true
  725. dateView.isHidden = true
  726. iconView.isHidden = true
  727. createFreeTextProperties()
  728. case .selfSignFreeText:
  729. rotateView.isHidden = true
  730. borderAndLineView.isHidden = true
  731. fillColorView.isHidden = true
  732. typeView.isHidden = true
  733. iconView.isHidden = true
  734. createSelfSignFreeTextProperties()
  735. default:
  736. break
  737. }
  738. }
  739. func createMarkupProperties() {
  740. let colorViewHeight = colorTitleLabel.frame.height + KMColorPickerViewHeight + 30
  741. colorView.frame = CGRect(x: 0, y: 0, width: view.frame.size.width, height: colorViewHeight)
  742. textColorBoxConstraint.constant = 10
  743. borderColorBoxConstraint.constant = 0
  744. textColorBox.isHidden = true
  745. lineWidthBox.isHidden = true
  746. imageBoxLayoutConstraint.constant = generalImageBoxView.frame.height
  747. hiddenSubviews()
  748. textColorPickerView.noContentString = true
  749. if activeAnnotationType == .highlight {
  750. textColorPickerView.annotationTypeString = NSLocalizedString("Highlight", comment: "Description for export")
  751. } else if activeAnnotationType == .strikeOut {
  752. textColorPickerView.annotationTypeString = NSLocalizedString("Strikethrough", comment: "Description for export")
  753. } else if activeAnnotationType == .underline {
  754. textColorPickerView.annotationTypeString = NSLocalizedString("Underline", comment: "Description for export")
  755. }
  756. textColorPickerView.annotationType = (activeAnnotationType == .highlight) ? .markupHighlightColors : .markupOtherColors
  757. }
  758. func openFreeTextStylesViewController() {
  759. // let vc = KMFreeTextStylesViewController()
  760. // vc.annotations = self.annotations
  761. // vc.view.frame = CGRect(x: 0, y: 0, width: 280, height: CGRectGetHeight(self.view.window?.frame ?? CGRect.zero) - 300)
  762. //
  763. // let createFilePopover = NSPopover()
  764. // createFilePopover.contentViewController = vc
  765. // createFilePopover.animates = true
  766. // createFilePopover.behavior = .transient
  767. // createFilePopover.show(relativeTo: CGRect(x: _textImageBox.bounds.origin.x, y: 10, width: _textImageBox.bounds.size.width, height: _textImageBox.bounds.size.height), of: _textImageBox, preferredEdge: .minY)
  768. //
  769. // // Assuming _textImageBox is an NSView instance
  770. // createFilePopover.show(relativeTo: _textImageBox.bounds, of: _textImageBox, preferredEdge: .minY)
  771. // Release is not needed in Swift due to automatic reference counting (ARC)
  772. }
  773. func changeStoredFontInfo(_ sender: Any) {
  774. // if let senderFont = sender as? NSFont {
  775. // for tAnnotation in self.annotations {
  776. // tAnnotation.removeAllAppearanceStreams()
  777. // let font = senderFont.convert(tAnnotation.font)
  778. // if let validFont = font {
  779. // tAnnotation.font = validFont
  780. // }
  781. // }
  782. // }
  783. }
  784. private func updateAnnotation() {
  785. if annotationModel?.annotation != nil {
  786. if annotation is CPDFFreeTextAnnotation {
  787. let textNote = (annotation as! CPDFFreeTextAnnotation)
  788. if let isEdit = pdfView?.isEdit(withCurrentFreeText: textNote), isEdit {
  789. pdfView?.commitEditAnnotationFreeText(textNote)
  790. }
  791. pdfView?.setNeedsDisplay(pdfView?.activeAnnotation)
  792. }
  793. for tAnnotation in annotations {
  794. if tAnnotation is CPDFStampAnnotation {
  795. (tAnnotation as? KMSelfSignAnnotation)?.updateAppearanceStream()
  796. }
  797. if tAnnotation is CPDFMarkupAnnotation {
  798. pdfView?.setNeedsDisplayFor(tAnnotation.page)
  799. } else {
  800. pdfView?.setNeedsDisplayAnnotationViewFor(tAnnotation.page)
  801. }
  802. }
  803. }
  804. if annotationType == .freeText {
  805. textAnnotationImageView.image = annotationModel?.annotationImage
  806. } else {
  807. generalImageView.image = annotationModel?.annotationImage
  808. }
  809. }
  810. // MARK: Line Note
  811. func createInkAndLineProperties() {
  812. if annotationType == .ink {
  813. let colorViewHeight = CGRectGetHeight(colorTitleLabel.frame) + KMColorPickerViewHeight + 30
  814. colorView.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: colorViewHeight)
  815. textColorBoxConstraint.constant = 10
  816. borderColorBoxConstraint.constant = 0
  817. textColorBox.isHidden = true
  818. lineWidthBox.isHidden = true
  819. imageBoxLayoutConstraint.constant = CGRectGetHeight(generalImageBoxView.frame)
  820. hiddenSubviews()
  821. textColorPickerView.noContentString = true
  822. textColorPickerView.annotationTypeString = NSLocalizedString("Line Color", comment: "")
  823. textColorPickerView.annotationType = .inkColors
  824. } else if annotationType == .line || annotationType == .arrow {
  825. let colorViewHeight = CGRectGetHeight(colorTitleLabel.frame) + KMColorPickerViewHeight * 2 + 40
  826. colorView.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: colorViewHeight)
  827. textColorBoxConstraint.constant = KMColorPickerViewHeight + 20
  828. borderColorBoxConstraint.constant = 0
  829. lineWidthBox.isHidden = true
  830. textColorPickerViewConstant.constant = 57
  831. imageBoxLayoutConstraint.constant = CGRectGetHeight(generalImageBoxView.frame)
  832. hiddenSubviews()
  833. borderColorPickerView.annotationTypeString = NSLocalizedString("Line Color", comment: "")
  834. textColorPickerView.annotationTypeString = NSLocalizedString("Fill Color", comment: "")
  835. borderColorPickerView.annotationType = .lineColors
  836. textColorPickerView.annotationType = .lineFillColors
  837. borderColorPickerView.annotationTypeString = NSLocalizedString("Line Color", comment: "")
  838. borderColorPickerView.annotationType = .lineColors
  839. }
  840. lineTypeLabel.stringValue = NSLocalizedString("Line and Border Style", comment: "")
  841. lineTypeLabel.textColor = KMAppearance.Layout.h0Color()
  842. lineWidthSlider.floatValue = Float((annotationModel?.lineWidth())!)
  843. lineWidthComboBox.stringValue = String(format: "%0.1f pt", (annotationModel?.lineWidth())!)
  844. lineStyleButton.wantsLayer = true
  845. dottedLineStyleButton.wantsLayer = true
  846. rightArrowStyleButton.wantsLayer = true
  847. lineStyleButton.image = borderStyleSolid(false)
  848. dottedLineStyleButton.image = borderStyleDashed(false)
  849. rightArrowStyleButton.image = lineStyleRightArrow(false)
  850. if annotationModel?.style() == .solid {
  851. lineStyleCount = 0
  852. lineStyleButton.image = borderStyleSolid(true)
  853. lineStyleBox.borderWidth = 0
  854. lineStyleBox.fillColor = KMAppearance.Status.selColor()
  855. } else if annotationModel?.style() == .dashed {
  856. lineStyleCount = 1
  857. dottedLineStyleButton.image = borderStyleDashed(true)
  858. dottedLineStyleBox.borderWidth = 0
  859. dottedLineStyleBox.fillColor = KMAppearance.Status.selColor()
  860. }
  861. if (annotationType == .line || annotationType == .arrow) && annotationModel?.endLineStyle() == .closedArrow {
  862. lineStyleCount = 2
  863. rightArrowStyleButton.image = lineStyleRightArrow(true)
  864. rightArrowStyleBox.borderWidth = 0
  865. rightArrowStyleBox.fillColor = KMAppearance.Status.selColor()
  866. }
  867. }
  868. func createTextProperties() {
  869. makeNoteIcons()
  870. let colorViewHeight = colorTitleLabel.frame.height + KMColorPickerViewHeight + 30
  871. colorView.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: colorViewHeight)
  872. textColorBoxConstraint.constant = 10
  873. borderColorBoxConstraint.constant = 0
  874. textColorBox.isHidden = true
  875. lineWidthBox.isHidden = true
  876. imageBoxLayoutConstraint.constant = generalImageBoxView.frame.height
  877. hiddenSubviews()
  878. textColorPickerView.annotationTypeString = NSLocalizedString("Anchored Note", comment: "Description for export")
  879. commentButton.title = NSLocalizedString("Comment", tableName: "NoteWindow", comment: "")
  880. commentButton.toolTip = NSLocalizedString("Comment", tableName: "NoteWindow", comment: "")
  881. commentButton.setTitleColor(KMAppearance.Layout.h0Color())
  882. paragraphButton.title = NSLocalizedString("Paragraph", tableName: "NoteWindow", comment: "")
  883. paragraphButton.toolTip = NSLocalizedString("Paragraph", tableName: "NoteWindow", comment: "")
  884. paragraphButton.setTitleColor(KMAppearance.Layout.h0Color())
  885. insertButton.title = NSLocalizedString("Insert", tableName: "NoteWindow", comment: "")
  886. insertButton.toolTip = NSLocalizedString("Insert", tableName: "NoteWindow", comment: "")
  887. insertButton.setTitleColor(KMAppearance.Layout.h0Color())
  888. addParagraphButton.title = NSLocalizedString("New Paragraph", tableName: "NoteWindow", comment: "")
  889. addParagraphButton.toolTip = NSLocalizedString("New Paragraph", tableName: "NoteWindow", comment: "")
  890. addParagraphButton.setTitleColor(KMAppearance.Layout.h0Color())
  891. keyButton.title = NSLocalizedString("Key", tableName: "NoteWindow", comment: "")
  892. keyButton.toolTip = NSLocalizedString("Key", tableName: "NoteWindow", comment: "")
  893. keyButton.setTitleColor(KMAppearance.Layout.h0Color())
  894. noteButton.title = NSLocalizedString("Note", tableName: "NoteWindow", comment: "")
  895. noteButton.toolTip = NSLocalizedString("Note", tableName: "NoteWindow", comment: "")
  896. noteButton.setTitleColor(KMAppearance.Layout.h0Color())
  897. helpButton.title = NSLocalizedString("Help", tableName: "NoteWindow", comment: "")
  898. helpButton.toolTip = NSLocalizedString("Help", tableName: "NoteWindow", comment: "")
  899. helpButton.setTitleColor(KMAppearance.Layout.h0Color())
  900. let languages = NSLocale.preferredLanguages
  901. var currentLocaleLanguageCode = "en"
  902. if let firstLanguage = languages.first {
  903. currentLocaleLanguageCode = firstLanguage
  904. if currentLocaleLanguageCode.hasPrefix("zh") {
  905. currentLocaleLanguageCode = "zh"
  906. }
  907. }
  908. let buttonArray: [NSButton] = [commentButton, keyButton, noteButton, helpButton, addParagraphButton, paragraphButton, insertButton]
  909. for button in buttonArray {
  910. button.wantsLayer = true
  911. button.layer?.cornerRadius = 6.0
  912. if currentLocaleLanguageCode == "zh" {
  913. iconButtonHeightConstant.constant = 56.0
  914. button.imagePosition = .imageAbove
  915. } else {
  916. iconButtonHeightConstant.constant = 50.0
  917. button.imagePosition = .imageOnly
  918. }
  919. }
  920. iconButtonArray = [commentBox, keyBox, noteBox, helpBox, addParagraphBox, paragraphBox, insertBox]
  921. // let iconType = annotation.iconType
  922. // clickAnnotationTextIconButtonAction(iconType: iconType, boxArr: iconButtonArray)
  923. textColorPickerView.annotationType = .anchoredNoteColors
  924. textColorPickerView.isCallColorPanelAction = true
  925. }
  926. func clickAnnotationTextIconButtonAction(iconType: UInt, boxArr buttonArr: [NSBox]) {
  927. for i in 0..<buttonArr.count {
  928. if iconType == 0 {
  929. commentButton.layer?.backgroundColor = KMAppearance.Status.selColor().cgColor
  930. commentButton.image = NSImage(named: KMImageNameUXIconPropertybarNoteAnnotationSel)
  931. } else {
  932. commentButton.layer?.backgroundColor = NSColor.clear.cgColor
  933. commentButton.image = NSImage(named: KMImageNameUXIconPropertybarNoteAnnotationNor)
  934. }
  935. if iconType == 1 {
  936. keyButton.layer?.backgroundColor = KMAppearance.Status.selColor().cgColor
  937. keyButton.image = NSImage(named: KMImageNameUXIconPropertybarNoteKeywordSel)
  938. } else {
  939. keyButton.layer?.backgroundColor = NSColor.clear.cgColor
  940. keyButton.image = NSImage(named: KMImageNameUXIconPropertybarNoteKeywordNor)
  941. }
  942. if iconType == 2 {
  943. noteButton.layer?.backgroundColor = KMAppearance.Status.selColor().cgColor
  944. noteButton.image = NSImage(named: KMImageNameUXIconPropertybarNoteNotesSel)
  945. } else {
  946. noteButton.layer?.backgroundColor = NSColor.clear.cgColor
  947. noteButton.image = NSImage(named: KMImageNameUXIconPropertybarNoteNotesNor)
  948. }
  949. if iconType == 3 {
  950. helpButton.layer?.backgroundColor = KMAppearance.Status.selColor().cgColor
  951. helpButton.image = NSImage(named: KMImageNameUXIconPropertybarNoteHelpSel)
  952. } else {
  953. helpButton.layer?.backgroundColor = NSColor.clear.cgColor
  954. helpButton.image = NSImage(named: KMImageNameUXIconPropertybarNoteHelpNor)
  955. }
  956. if iconType == 4 {
  957. addParagraphButton.layer?.backgroundColor = KMAppearance.Status.selColor().cgColor
  958. addParagraphButton.image = NSImage(named: KMImageNameUXIconPropertybarNoteNewparagraphSel)
  959. } else {
  960. addParagraphButton.layer?.backgroundColor = NSColor.clear.cgColor
  961. addParagraphButton.image = NSImage(named: KMImageNameUXIconPropertybarNoteNewparagraphNor)
  962. }
  963. if iconType == 5 {
  964. paragraphButton.layer?.backgroundColor = KMAppearance.Status.selColor().cgColor
  965. paragraphButton.image = NSImage(named: KMImageNameUXIconPropertybarNoteParagraphSel)
  966. } else {
  967. paragraphButton.layer?.backgroundColor = NSColor.clear.cgColor
  968. paragraphButton.image = NSImage(named: KMImageNameUXIconPropertybarNoteParagraphNor)
  969. }
  970. if iconType == 6 {
  971. insertButton.layer?.backgroundColor = KMAppearance.Status.selColor().cgColor
  972. insertButton.image = NSImage(named: KMImageNameUXIconPropertybarNoteInsertSel)
  973. } else {
  974. insertButton.layer?.backgroundColor = NSColor.clear.cgColor
  975. insertButton.image = NSImage(named: KMImageNameUXIconPropertybarNoteInsertNor)
  976. }
  977. }
  978. }
  979. func borderStyleSolid(_ isSelect: Bool) -> NSImage {
  980. let size = NSSize(width: 49.0, height: 12.0)
  981. let image = NSImage(size: size, flipped: false, drawingHandler: { rect in
  982. let path = NSBezierPath(rect: NSRect(x: 15.0, y: 5.0, width: 20.0, height: 1.0))
  983. path.lineWidth = 2.0
  984. isSelect ? KMAppearance.Layout.m_1Color().setStroke() : KMAppearance.Layout.m_1Color().setStroke()
  985. path.stroke()
  986. return true
  987. })
  988. return image
  989. }
  990. func borderStyleDashed(_ isSelect: Bool) -> NSImage {
  991. let size = NSSize(width: 49.0, height: 12.0)
  992. let image = NSImage(size: size, flipped: false, drawingHandler: { rect in
  993. let path = NSBezierPath()
  994. path.move(to: NSPoint(x: 15.0, y: 5.0))
  995. path.line(to: NSPoint(x: 19.0, y: 5.0))
  996. path.move(to: NSPoint(x: 23.0, y: 5.0))
  997. path.line(to: NSPoint(x: 27.0, y: 5.0))
  998. path.move(to: NSPoint(x: 31.0, y: 5.0))
  999. path.line(to: NSPoint(x: 35.0, y: 5.0))
  1000. path.lineWidth = 2.0
  1001. isSelect ? KMAppearance.Layout.m_1Color().setStroke() : KMAppearance.Layout.m_1Color().setStroke()
  1002. path.stroke()
  1003. return true
  1004. })
  1005. return image
  1006. }
  1007. func lineStyleRightArrow(_ isSelect: Bool) -> NSImage {
  1008. let size = NSSize(width: 49.0, height: 12.0)
  1009. let image = NSImage(size: size, flipped: false, drawingHandler: { rect in
  1010. let path = NSBezierPath()
  1011. path.move(to: NSPoint(x: 6.0, y: 6.0))
  1012. path.line(to: NSPoint(x: 43.0, y: 6.0))
  1013. path.move(to: NSPoint(x: 32.0, y: 3.0))
  1014. path.line(to: NSPoint(x: 43.0, y: 6.0))
  1015. path.line(to: NSPoint(x: 32.0, y: 9.0))
  1016. path.close()
  1017. path.lineWidth = 2.0
  1018. if isSelect {
  1019. KMAppearance.Layout.l0Color().setStroke()
  1020. } else {
  1021. KMAppearance.Interactive.s0Color().setStroke()
  1022. }
  1023. path.stroke()
  1024. return true
  1025. })
  1026. return image
  1027. }
  1028. // MARK: SKSquareNote、SKCircleNote、SKArrowNote Properties
  1029. func createSquareAndCircleProperties() {
  1030. let colorViewHeight = CGRectGetHeight(colorTitleLabel.frame) + KMColorPickerViewHeight * 2 + 40
  1031. colorView.frame = CGRect(x: 0, y: 0, width: view.frame.size.width, height: colorViewHeight)
  1032. textColorBoxConstraint.constant = KMColorPickerViewHeight + 20
  1033. borderColorBoxConstraint.constant = 0
  1034. lineWidthBox.isHidden = true
  1035. textColorPickerViewConstant.constant = 57
  1036. imageBoxLayoutConstraint.constant = CGRectGetHeight(generalImageBoxView.frame)
  1037. hiddenSubviews()
  1038. lineTypeLabel.stringValue = NSLocalizedString("Line and Border Style", comment: "")
  1039. lineTypeLabel.textColor = KMAppearance.Layout.h0Color()
  1040. lineWidthSlider.floatValue = Float((annotationModel?.lineWidth())!)
  1041. lineWidthComboBox.stringValue = String(format: "%0.1f pt", (annotationModel?.lineWidth())!)
  1042. textColorPickerView.annotationTypeString = NSLocalizedString("Fill Color", comment: "")
  1043. borderColorPickerView.annotationTypeString = NSLocalizedString("Line Color", comment: "")
  1044. textColorPickerView.annotationType = .lineFillColors
  1045. borderColorPickerView.annotationType = .lineColors
  1046. lineStyleButton.wantsLayer = true
  1047. dottedLineStyleButton.wantsLayer = true
  1048. rightArrowStyleButton.wantsLayer = true
  1049. lineStyleButton.image = borderStyleSolid(false)
  1050. dottedLineStyleButton.image = borderStyleDashed(false)
  1051. if annotationModel?.annotation != nil {
  1052. if annotationModel?.annotation.borderStyle() == .solid {
  1053. lineStyleCount = 0
  1054. lineStyleButton.image = borderStyleSolid(true)
  1055. lineStyleBox.borderWidth = 0
  1056. lineStyleBox.fillColor = KMAppearance.Status.selColor()
  1057. } else if annotationModel?.annotation.borderStyle() == .dashed {
  1058. lineStyleCount = 1
  1059. dottedLineStyleButton.image = borderStyleDashed(true)
  1060. dottedLineStyleBox.borderWidth = 1
  1061. dottedLineStyleBox.fillColor = KMAppearance.Status.selColor()
  1062. }
  1063. } else {
  1064. if annotationModel?.style() == .solid {
  1065. lineStyleCount = 0
  1066. lineStyleButton.image = borderStyleSolid(true)
  1067. lineStyleBox.borderWidth = 0
  1068. lineStyleBox.fillColor = KMAppearance.Status.selColor()
  1069. } else if annotationModel?.style() == .dashed {
  1070. lineStyleCount = 1
  1071. dottedLineStyleButton.image = borderStyleDashed(true)
  1072. dottedLineStyleBox.borderWidth = 1
  1073. dottedLineStyleBox.fillColor = KMAppearance.Status.selColor()
  1074. }
  1075. }
  1076. }
  1077. // MARK: SKFreeTextNote、PDFAnnotationFreeText Properties
  1078. func createFreeTextProperties() {
  1079. imageBox.fillColor = KMAppearance.Layout.w0Color()
  1080. textColorBox.isHidden = true
  1081. lineWidthBox.isHidden = true
  1082. imageBoxLayoutConstraint.constant = CGRectGetHeight(textImageBoxView.frame)
  1083. textImageBox.downCallback = { [weak self] downEntered, mouseBox, event in
  1084. if downEntered {
  1085. self?.openFreeTextStylesViewController()
  1086. }
  1087. }
  1088. textImageUpDateButton.wantsLayer = true
  1089. textImageBoxButton.image = NSImage(named: KMImageNameUXIconBtnTriDownNor)
  1090. textImageBoxButton.isEnabled = true
  1091. textImageUpDateButton.title = NSLocalizedString("Click to refresh", comment: "")
  1092. textImageUpDateButton.setTitleColor(KMAppearance.Layout.w0Color())
  1093. textImageUpDateButton.layer?.cornerRadius = 1.0
  1094. textImageUpDateButton.layer?.backgroundColor = KMAppearance.Interactive.m0Color().cgColor
  1095. textImageUpDateButton.isHidden = true
  1096. hiddenSubviews()
  1097. fontViewCorloPV.noContentString = true
  1098. fontViewCorloPV.annotationTypeString = NSLocalizedString("Color", comment: "")
  1099. fillColorPickerView.noContentString = true
  1100. fillColorPickerView.annotationTypeString = NSLocalizedString("Color", comment: "")
  1101. lineTypeLabel.stringValue = NSLocalizedString("Line and Border Style", comment: "")
  1102. lineTypeLabel.textColor = KMAppearance.Layout.h0Color()
  1103. leftAlignButton.toolTip = NSLocalizedString("Left Alignment", comment: "")
  1104. centerAlignButton.toolTip = NSLocalizedString("Center", comment: "")
  1105. rightAlignButton.toolTip = NSLocalizedString("Right Alignment", comment: "")
  1106. lineWidthSlider.floatValue = Float((annotationModel?.lineWidth())!)
  1107. lineWidthComboBox.stringValue = String(format: "%0.1f pt", Float((annotationModel?.lineWidth())!))
  1108. leftRotateBox.borderColor = KMAppearance.Interactive.s0Color()
  1109. leftRotateBox.borderWidth = 0.5
  1110. leftRotateButton.image = NSImage(named: KMImageNameUXIconPropertybarRotateCounterclockwiseNor)
  1111. leftRotateBox.downCallback = { [weak self] downEntered, mouseBox, event in
  1112. if downEntered {
  1113. self?.leftRotateBox.fillColor = KMAppearance.Status.selColor()
  1114. self?.leftRotateButton.image = NSImage(named: KMImageNameUXIconPropertybarRotateCounterclockwisePre)
  1115. } else {
  1116. self?.leftRotateBox.fillColor = KMAppearance.Layout.w0Color()
  1117. self?.leftRotateButton.image = NSImage(named: KMImageNameUXIconPropertybarRotateCounterclockwiseNor)
  1118. }
  1119. }
  1120. rightRotateBox.borderColor = KMAppearance.Interactive.s0Color()
  1121. rightRotateBox.borderWidth = 0.5
  1122. rightRotateButton.image = NSImage(named: KMImageNameUXIconPropertybarRotateClockwiseNor)
  1123. rightRotateBox.downCallback = { [weak self] downEntered, mouseBox, event in
  1124. if downEntered {
  1125. self?.rightRotateBox.fillColor = KMAppearance.Status.selColor()
  1126. self?.rightRotateButton.image = NSImage(named: KMImageNameUXIconPropertybarRotateClockwisePre)
  1127. } else {
  1128. self?.rightRotateBox.fillColor = KMAppearance.Layout.w0Color()
  1129. self?.rightRotateButton.image = NSImage(named: KMImageNameUXIconPropertybarRotateClockwiseNor)
  1130. }
  1131. }
  1132. lineStyleButton.wantsLayer = true
  1133. dottedLineStyleButton.wantsLayer = true
  1134. rightArrowStyleButton.wantsLayer = true
  1135. lineStyleButton.image = borderStyleSolid(false)
  1136. dottedLineStyleButton.image = borderStyleDashed(false)
  1137. if annotationModel?.style() == .solid {
  1138. lineStyleCount = 0
  1139. lineStyleButton.image = borderStyleSolid(true)
  1140. lineStyleBox.borderWidth = 0
  1141. lineStyleBox.fillColor = KMAppearance.Status.selColor()
  1142. lineStyleBox.borderWidth = 0
  1143. } else if annotationModel?.style() == .dashed {
  1144. lineStyleCount = 1
  1145. dottedLineStyleButton.image = borderStyleDashed(true)
  1146. dottedLineStyleBox.fillColor = KMAppearance.Status.selColor()
  1147. dottedLineStyleBox.borderWidth = 0
  1148. } else {
  1149. lineStyleCount = 0
  1150. lineStyleButton.image = borderStyleSolid(true)
  1151. lineStyleBox.borderWidth = 0
  1152. lineStyleBox.fillColor = KMAppearance.Status.selColor()
  1153. lineStyleBox.borderWidth = 0
  1154. }
  1155. fontViewCorloPV.annotationType = .freeTextColors
  1156. fillColorPickerView.annotationType = .freeTextFillColors
  1157. }
  1158. func alignmentTypeSelected(_ type: KMFreeTextAnnotationAlignmentType) {
  1159. selectedAlignmentButtonLayer?.removeFromSuperlayer()
  1160. selectedAlignmentButtonLayer = CALayer()
  1161. selectedAlignmentButtonLayer?.cornerRadius = 6.0
  1162. leftAlignButton.wantsLayer = true
  1163. centerAlignButton.wantsLayer = true
  1164. rightAlignButton.wantsLayer = true
  1165. selectedAlignmentButtonLayer?.bounds = leftAlignButton.layer?.bounds ?? CGRect.zero
  1166. selectedAlignmentButtonLayer?.anchorPoint = CGPoint(x: 0, y: 0)
  1167. var color = NSColor(red: 71/255.0, green: 126/255.0, blue: 222/255.0, alpha: 0.16)
  1168. if #available(macOS 10.14, *) {
  1169. let appearanceName = NSApp.effectiveAppearance.bestMatch(from: [.aqua, .darkAqua])
  1170. if appearanceName == .darkAqua {
  1171. color = NSColor(red: 34/255.0, green: 122/255.0, blue: 255/255.0, alpha: 0.3)
  1172. }
  1173. }
  1174. selectedAlignmentButtonLayer?.backgroundColor = color.cgColor
  1175. leftAlignButton.image = NSImage(named: KMImageNameUXIconPropertybarTextalignLeftNor)
  1176. centerAlignButton.image = NSImage(named: KMImageNameUXIconPropertybarTextalignCenterNor)
  1177. rightAlignButton.image = NSImage(named: KMImageNameUXIconPropertybarTextalignRightNor)
  1178. if type == .left {
  1179. leftAlignButton.image = NSImage(named: KMImageNameUXIconPropertybarTextalignLeftSel)
  1180. } else if type == .center {
  1181. centerAlignButton.image = NSImage(named: KMImageNameUXIconPropertybarTextalignCenterSel)
  1182. } else if type == .right {
  1183. rightAlignButton.image = NSImage(named: KMImageNameUXIconPropertybarTextalignRightSel)
  1184. }
  1185. DispatchQueue.main.asyncAfter(deadline: .now() + 0.01) { [weak self] in
  1186. guard let self = self else { return }
  1187. if type == .left {
  1188. self.leftAlignButton.layer?.insertSublayer(self.selectedAlignmentButtonLayer!, at: 0)
  1189. } else if type == .center {
  1190. self.centerAlignButton.layer?.insertSublayer(self.selectedAlignmentButtonLayer!, at: 0)
  1191. } else if type == .right {
  1192. self.rightAlignButton.layer?.insertSublayer(self.selectedAlignmentButtonLayer!, at: 0)
  1193. }
  1194. }
  1195. }
  1196. // MARK: SKFreeTextNote、KMSelfSignAnnotationFreeText Properties
  1197. func createSelfSignFreeTextProperties() {
  1198. let colorViewHeight = CGRectGetHeight(colorTitleLabel.frame) + KMColorPickerViewHeight + 30
  1199. colorView.frame = CGRect(x: 0, y: 0, width: view.frame.size.width, height: colorViewHeight)
  1200. textColorBoxConstraint.constant = 10
  1201. borderColorBoxConstraint.constant = 0
  1202. textColorBox.isHidden = true
  1203. lineWidthBox.isHidden = true
  1204. imageBoxLayoutConstraint.constant = CGRectGetHeight(generalImageBoxView.frame)
  1205. hiddenSubviews()
  1206. fontViewTopConstant.constant = colorView.frame.size.height + opacityView.frame.size.height - 10 + 10
  1207. ColorViewTopConstant.constant = -(fontView.frame.size.height + fontViewTopConstant.constant)
  1208. dateViewTopConstant.constant = fontView.frame.size.height + 30
  1209. fontViewCorloPV.noContentString = true
  1210. fontViewCorloPV.annotationTypeString = ""
  1211. textColorPickerView.annotationTypeString = NSLocalizedString("Background", comment: "")
  1212. dateCheckButton.title = NSLocalizedString("Time", comment: "")
  1213. if let attrTitle = dateCheckButton.attributedTitle.mutableCopy() as? NSMutableAttributedString {
  1214. let len = attrTitle.length
  1215. let range = NSRange(location: 0, length: len)
  1216. attrTitle.addAttribute(.foregroundColor, value: KMAppearance.Layout.h1Color(), range: range)
  1217. attrTitle.fixAttributes(in: range)
  1218. dateCheckButton.attributedTitle = attrTitle
  1219. }
  1220. if activeAnnotationType == .selfSignFreeText { // 添加日期 字体默认颜色不要透明色
  1221. textColorPickerView.annotationType = .freeTextFillColors
  1222. fontViewCorloPV.annotationType = .freeTextColors
  1223. } else {
  1224. textColorPickerView.annotationType = .freeTextColors
  1225. fontViewCorloPV.annotationType = .freeTextFillColors
  1226. }
  1227. }
  1228. func setFontStyle(fontName: String, currentStyle style: String?) -> UInt {
  1229. var selectIndex: UInt = 0
  1230. let menu = NSMenu()
  1231. if let fontFamily = NSFontManager.shared.availableMembers(ofFontFamily: fontName) {
  1232. for i in 0..<fontFamily.count {
  1233. let array = fontFamily[i]
  1234. if let styleName = array[1] as? String {
  1235. if style == styleName {
  1236. selectIndex = UInt(i)
  1237. }
  1238. let attributeFontDescriptor = NSFontDescriptor(fontAttributes: [NSFontDescriptor.AttributeName.family: fontName, NSFontDescriptor.AttributeName.face: styleName])
  1239. let font = NSFont(descriptor: attributeFontDescriptor, size: 12.0)
  1240. let attrited = [NSAttributedString.Key.font: font]
  1241. let string = NSAttributedString(string: styleName, attributes: attrited)
  1242. let item = NSMenuItem()
  1243. item.attributedTitle = string
  1244. menu.addItem(item)
  1245. }
  1246. }
  1247. }
  1248. if style == nil {
  1249. selectIndex = 0
  1250. }
  1251. fontStylePopUpButton.menu = menu
  1252. return selectIndex
  1253. }
  1254. // MARK: UI Action
  1255. @IBAction func colorPickerViewAction(_ sender: Any) {
  1256. var fillColor: NSColor = NSColor.clear
  1257. var red: CGFloat = 0.0, green: CGFloat = 0.0, blue: CGFloat = 0.0, opacity: CGFloat = 0.0
  1258. if let textColor = textColorPickerView.color {
  1259. textColor.usingColorSpaceName(.calibratedRGB)?.getRed(&red, green: &green, blue: &blue, alpha: &opacity)
  1260. if fabs(opacity - 0) < 0.001 {
  1261. if annotationType == .square || annotationType == .circle || annotationType == .line || annotationType == .arrow {
  1262. annotationModel?.setInteriorOpacity(0)
  1263. annotationModel?.setInteriorColor(.black)
  1264. } else if annotationType == .freeText || annotationType == .signText || annotationType == .signDate {
  1265. annotationModel?.setOpacity(0)
  1266. annotationModel?.setFontColor(.black)
  1267. } else {
  1268. annotationModel?.setOpacity(0)
  1269. annotationModel?.setFontColor(.black)
  1270. }
  1271. } else {
  1272. if annotationType == .square || annotationType == .circle || annotationType == .line || annotationType == .arrow {
  1273. annotationModel?.setInteriorOpacity(opacity)
  1274. annotationModel?.setInteriorColor(textColor)
  1275. } else if annotationType == .freeText || annotationType == .signText || annotationType == .signDate {
  1276. annotationModel?.setOpacity(opacity)
  1277. annotationModel?.setFontColor(textColor)
  1278. } else {
  1279. annotationModel?.setOpacity(opacity)
  1280. annotationModel?.setColor(textColor)
  1281. }
  1282. }
  1283. opacitySlider.floatValue = Float(opacity)
  1284. let color = NSColor(calibratedRed: red, green: green, blue: blue, alpha: opacity)
  1285. fillColor = color
  1286. if annotationModel?.annotations != nil {
  1287. if annotationModel?.annotation is CPDFFreeTextAnnotation {
  1288. let textNote = (annotationModel?.annotation as! CPDFFreeTextAnnotation)
  1289. if let isEdit = pdfView?.isEdit(withCurrentFreeText: textNote), isEdit {
  1290. pdfView?.setEditAnnotationFreeTextColor(textColor, freeText: textNote)
  1291. } else {
  1292. updateAnnotation()
  1293. }
  1294. } else {
  1295. updateAnnotation()
  1296. }
  1297. generalImageView.image = annotationModel?.annotationImage
  1298. } else {
  1299. updateAnnotation()
  1300. }
  1301. }
  1302. }
  1303. @IBAction func fillColorPickerViewAction(_ sender: Any) {
  1304. var fillColor = NSColor.clear
  1305. var red: CGFloat = 0.0, green: CGFloat = 0.0, blue: CGFloat = 0.0, opacity: CGFloat = 0.0
  1306. if let borderColor = borderColorPickerView.color {
  1307. borderColor.usingColorSpaceName(.calibratedRGB)?.getRed(&red, green: &green, blue: &blue, alpha: &opacity)
  1308. opacitySlider.floatValue = Float(opacity)
  1309. let color = NSColor(calibratedRed: red, green: green, blue: blue, alpha: opacity)
  1310. fillColor = color
  1311. }
  1312. if annotationType == .freeText {
  1313. if let fillColorPickerColor = fillColorPickerView.color {
  1314. fillColorPickerColor.usingColorSpaceName(.calibratedRGB)?.getRed(&red, green: &green, blue: &blue, alpha: &opacity)
  1315. opacitySlider.floatValue = Float(opacity)
  1316. let color = NSColor(calibratedRed: red, green: green, blue: blue, alpha: opacity)
  1317. annotationModel?.setColor(color)
  1318. annotationModel?.setOpacity(opacity)
  1319. }
  1320. } else {
  1321. annotationModel?.setColor(fillColor)
  1322. annotationModel?.setOpacity(opacity)
  1323. }
  1324. updateAnnotation()
  1325. }
  1326. @IBAction func fontViewCorloPVAction(_ sender: Any) {
  1327. // var fillColor: NSColor = NSColor.clear
  1328. // var red: CGFloat = 0.0, green: CGFloat = 0.0, blue: CGFloat = 0.0, opacity: CGFloat = 0.0
  1329. //
  1330. // if let textColor = fontViewCorloPV.color {
  1331. // textColor.usingColorSpaceName(.calibratedRGB)?.getRed(&red, green: &green, blue: &blue, alpha: &opacity)
  1332. // if fabs(opacity - 0) < 0.001 {
  1333. // if annotationType == .freeText || annotationType == .signText || annotationType == .signDate {
  1334. // annotationModel?.setOpacity(0)
  1335. // annotationModel?.setFontColor(.black)
  1336. // } else {
  1337. // annotationModel?.setOpacity(0)
  1338. // annotationModel?.setFontColor(.black)
  1339. // }
  1340. // } else {
  1341. // if annotationType == .freeText || annotationType == .signText || annotationType == .signDate {
  1342. // annotationModel?.setOpacity(opacity)
  1343. // annotationModel?.setFontColor(textColor)
  1344. // } else {
  1345. // annotationModel?.setOpacity(opacity)
  1346. // annotationModel?.setColor(textColor)
  1347. // }
  1348. // }
  1349. //
  1350. // opacitySlider.floatValue = Float(opacity)
  1351. //
  1352. // if annotationModel?.annotation != nil {
  1353. // if annotationModel?.annotation is CPDFFreeTextAnnotation {
  1354. // let textNote = (annotationModel?.annotation as! CPDFFreeTextAnnotation)
  1355. // if let isEdit = pdfView?.isEdit(withCurrentFreeText: textNote), isEdit {
  1356. // pdfView?.setEditAnnotationFreeTextColor(textColor, freeText: textNote)
  1357. // } else {
  1358. // updateAnnotation()
  1359. // }
  1360. // } else {
  1361. // updateAnnotation()
  1362. // }
  1363. // generalImageView.image = annotationModel?.annotationImage
  1364. // } else {
  1365. // updateAnnotation()
  1366. // }
  1367. // }
  1368. if annotationType == .freeText {
  1369. annotationModel?.setFontColor(fontViewCorloPV.color)
  1370. } else {
  1371. annotationModel?.setColor(fontViewCorloPV.color)
  1372. }
  1373. updateAnnotation()
  1374. }
  1375. @IBAction func opacitySliderAction(_ sender: Any) {
  1376. var opcity = CGFloat(opacitySlider.floatValue)
  1377. if opcity == 0 {
  1378. opcity = 0.0001
  1379. }
  1380. if annotationType == .signFalse ||
  1381. annotationType == .signTure ||
  1382. annotationType == .signDot ||
  1383. annotationType == .signCircle ||
  1384. annotationType == .signLine {
  1385. annotationModel?.setOpacity(opcity)
  1386. } else if annotationType == .square ||
  1387. annotationType == .circle {
  1388. if opcity != 0 {
  1389. annotationModel?.setOpacity(opcity)
  1390. annotationModel?.setInteriorOpacity(opcity)
  1391. } else {
  1392. annotationModel?.setColor(nil)
  1393. annotationModel?.setInteriorColor(nil)
  1394. }
  1395. } else if annotationType == .freeText || annotationType == .signDate || annotationType == .signText {
  1396. annotationModel?.setOpacity(opcity)
  1397. var color = annotationModel?.fontColor()
  1398. if annotationModel?.opacity() != 0 {
  1399. var red: CGFloat = 0.0
  1400. var green: CGFloat = 0.0
  1401. var blue: CGFloat = 0.0
  1402. var opacity: CGFloat = 0.0
  1403. color?.usingColorSpaceName(.calibratedRGB)?.getRed(&red, green: &green, blue: &blue, alpha: &opacity)
  1404. color = NSColor(red: red, green: green, blue: blue, alpha: (annotationModel?.opacity())!)
  1405. } else {
  1406. color = .clear
  1407. }
  1408. } else {
  1409. annotationModel?.setOpacity(opcity)
  1410. }
  1411. opacityComboBox.stringValue = "\(Int(opcity * 100))%"
  1412. updateAnnotation()
  1413. }
  1414. @IBAction func opacityComboBoxAction(_ sender: Any) {
  1415. opacitySlider.floatValue = Float(opacityComboBox.floatValue / 100)
  1416. var opcity = CGFloat(opacitySlider.floatValue)
  1417. if opcity == 0 {
  1418. opcity = 0.0001
  1419. }
  1420. if annotationType == .signFalse ||
  1421. annotationType == .signTure ||
  1422. annotationType == .signDot ||
  1423. annotationType == .signCircle ||
  1424. annotationType == .signLine {
  1425. annotationModel?.setOpacity(opcity)
  1426. } else if annotationType == .square ||
  1427. annotationType == .circle {
  1428. if opcity != 0 {
  1429. annotationModel?.setOpacity(opcity)
  1430. annotationModel?.setInteriorOpacity(opcity)
  1431. } else {
  1432. annotationModel?.setColor(nil)
  1433. annotationModel?.setInteriorColor(nil)
  1434. }
  1435. } else if annotationType == .freeText || annotationType == .signDate || annotationType == .signText {
  1436. annotationModel?.setInteriorOpacity(opcity)
  1437. } else {
  1438. annotationModel?.setOpacity(opcity)
  1439. }
  1440. opacitySlider.floatValue = Float(opcity)
  1441. opacitySlider.toolTip = "\(Int(opcity * 100))%"
  1442. updateAnnotation()
  1443. }
  1444. @IBAction func lineWidthSliderAction(_ sender: Any) {
  1445. annotationModel?.setLineWidth(CGFloat(lineWidthSlider.floatValue))
  1446. lineWidthComboBox.stringValue = String(format: "%0.1f pt", (annotationModel?.lineWidth())!)
  1447. updateAnnotation()
  1448. }
  1449. @IBAction func lineWidthComboBoxAction(_ sender: Any) {
  1450. var lineWidth = lineWidthComboBox.floatValue
  1451. if lineWidth > 18 {
  1452. lineWidth = 18
  1453. } else if lineWidth < 0.5 {
  1454. lineWidth = 0.5
  1455. }
  1456. annotationModel?.setLineWidth(CGFloat(lineWidth))
  1457. lineWidthSlider.floatValue = Float((annotationModel?.lineWidth())!)
  1458. updateAnnotation()
  1459. }
  1460. @IBAction func alignButtonAction(_ sender: NSButton) {
  1461. var alignmentType: KMFreeTextAnnotationAlignmentType = .left
  1462. if annotationModel?.annotations != nil {
  1463. for tAnnotation in self.annotations {
  1464. switch sender.tag {
  1465. case 0:
  1466. if tAnnotation is CPDFFreeTextAnnotation {
  1467. (tAnnotation as! CPDFFreeTextAnnotation).alignment = .left
  1468. }
  1469. alignmentType = .left
  1470. case 1:
  1471. if tAnnotation is CPDFFreeTextAnnotation {
  1472. (tAnnotation as! CPDFFreeTextAnnotation).alignment = .center
  1473. }
  1474. alignmentType = .center
  1475. case 2:
  1476. if tAnnotation is CPDFFreeTextAnnotation {
  1477. (tAnnotation as! CPDFFreeTextAnnotation).alignment = .right
  1478. }
  1479. alignmentType = .right
  1480. default:
  1481. break
  1482. }
  1483. }
  1484. } else {
  1485. switch sender.tag {
  1486. case 0:
  1487. annotationModel?.setAlignment(.left)
  1488. alignmentType = .left
  1489. case 1:
  1490. annotationModel?.setAlignment(.center)
  1491. alignmentType = .center
  1492. case 2:
  1493. annotationModel?.setAlignment(.right)
  1494. alignmentType = .right
  1495. default:
  1496. break
  1497. }
  1498. }
  1499. alignmentTypeSelected(alignmentType)
  1500. updateAnnotation()
  1501. NotificationCenter.default.post(name: NSNotification.Name("KMAnnotationAlignmentTypeNotification"),
  1502. object: "\(alignmentType.rawValue)")
  1503. }
  1504. @IBAction func currentFontColorButtonAction(_ sender: NSButton) {
  1505. // if ([_annotation isKindOfClass:[PDFAnnotationFreeText class]]) {
  1506. // KMFontColorViewController *vc = [[[KMFontColorViewController alloc] initWithNibName:@"KMFontColorViewController" bundle:[NSBundle mainBundle]] autorelease];
  1507. // vc.annotationFontColor = _annotation.fontColor;
  1508. // __block typeof(self) blockSelf = self;
  1509. // vc.fontColorChangeCallback = ^(NSColor *fontColor) {
  1510. // for (PDFAnnotation *tAnnotation in blockSelf.annotations) {
  1511. // tAnnotation.fontColor = fontColor;
  1512. // }
  1513. // };
  1514. // NSPopover *popover = [[NSPopover alloc] init];
  1515. // popover.delegate = self;
  1516. // popover.contentViewController = vc;
  1517. // popover.animates = YES;
  1518. // popover.behavior = NSPopoverBehaviorTransient;
  1519. // [popover showRelativeToRect:CGRectMake([sender bounds].origin.x, 0, [sender bounds].size.width, [sender bounds].size.height) ofView:sender preferredEdge:CGRectMaxYEdge];
  1520. // [popover release];
  1521. // }
  1522. }
  1523. @IBAction func borderButtonAction(_ sender: NSButton) {
  1524. let fontWindowController = KMAnnotationLineWindowController.sharedAnnotationLine
  1525. guard let window = fontWindowController.window else { return }
  1526. fontWindowController.annotations = annotations
  1527. fontWindowController.annotationLineChangeBlock = { [weak self] lineNote in
  1528. if lineNote.borderStyle() == .solid {
  1529. self!.lineTypeButtonAction(self!.lineStyleButton)
  1530. } else if lineNote.borderStyle() == .dashed {
  1531. self!.lineTypeButtonAction(self!.dottedLineStyleButton)
  1532. for tAnnotation in self!.annotations {
  1533. tAnnotation.setDashPattern(lineNote.dashPattern())
  1534. }
  1535. }
  1536. }
  1537. window.orderFront(sender)
  1538. }
  1539. func lineTypePopAction() {
  1540. annotationModel?.setStyle(CPDFBorderStyle(rawValue: lineStyleCount) ?? .solid)
  1541. updateAnnotation()
  1542. }
  1543. @IBAction func lineTypeButtonAction(_ sender: NSButton) {
  1544. let tag = sender.tag
  1545. switch tag {
  1546. case 0:
  1547. lineStyleCount = 0
  1548. refreshLineTypeUI()
  1549. lineStyleButton.image = borderStyleSolid(true)
  1550. dottedLineStyleButton.image = borderStyleDashed(false)
  1551. rightArrowStyleButton.image = lineStyleRightArrow(false)
  1552. lineTypePopAction()
  1553. case 1:
  1554. lineStyleCount = 1
  1555. refreshLineTypeUI()
  1556. lineStyleButton.image = borderStyleSolid(false)
  1557. dottedLineStyleButton.image = borderStyleDashed(true)
  1558. rightArrowStyleButton.image = lineStyleRightArrow(false)
  1559. lineTypePopAction()
  1560. case 2:
  1561. lineStyleCount = 2
  1562. refreshLineTypeUI()
  1563. lineStyleButton.image = borderStyleSolid(false)
  1564. dottedLineStyleButton.image = borderStyleDashed(false)
  1565. rightArrowStyleButton.image = lineStyleRightArrow(true)
  1566. for tAnnotation in annotations {
  1567. if tAnnotation is CPDFLineAnnotation {
  1568. (tAnnotation as! CPDFLineAnnotation).endLineStyle = .closedArrow
  1569. }
  1570. }
  1571. default:
  1572. break
  1573. }
  1574. }
  1575. @IBAction func iconButtonAction(_ sender: NSButton) {
  1576. var type: CPDFTextAnnotationIconType = .comment
  1577. switch sender.tag {
  1578. case 0:
  1579. type = .comment
  1580. case 1:
  1581. type = .key
  1582. case 2:
  1583. type = .note
  1584. case 3:
  1585. type = .help
  1586. case 4:
  1587. type = .newParagraph
  1588. case 5:
  1589. type = .paragraph
  1590. case 6, _:
  1591. type = .insert
  1592. }
  1593. if annotationModel?.annotations != nil {
  1594. for tAnnotation in self.annotations {
  1595. if tAnnotation is CPDFTextAnnotation {
  1596. (tAnnotation as! CPDFTextAnnotation).setIconType(type)
  1597. }
  1598. }
  1599. } else {
  1600. annotationModel?.setAnchoredIconType(type)
  1601. }
  1602. generalImageView.image = annotationModel?.annotationImage
  1603. updateannotationMode()
  1604. clickAnnotationTextIconButtonAction(iconType: UInt(sender.tag), boxArr: iconButtonArray)
  1605. }
  1606. @IBAction func textImageBoxButtonAction(_ sender: Any) {
  1607. openFreeTextStylesViewController()
  1608. }
  1609. // MARK: Font、FontSize Action
  1610. @IBAction func fontButtonAction(_ sender: NSButton) {
  1611. let fontWindowController = KMAnnotationFontWindowController.sharedAnnotationFont
  1612. let window = fontWindowController.window
  1613. fontWindowController.annotations = annotations
  1614. fontWindowController.annotationAlignCallback = { [weak self] selectedCount in
  1615. var alignmentType: KMFreeTextAnnotationAlignmentType = .left
  1616. if selectedCount == NSTextAlignment.left.rawValue {
  1617. alignmentType = .left
  1618. } else if selectedCount == NSTextAlignment.center.rawValue {
  1619. alignmentType = .center
  1620. } else if selectedCount == NSTextAlignment.right.rawValue {
  1621. alignmentType = .right
  1622. }
  1623. self!.alignmentTypeSelected(alignmentType)
  1624. }
  1625. fontWindowController.annotationCallback = { [weak self] annotation in
  1626. if annotation is CPDFFreeTextAnnotation {
  1627. if let family = (annotation as! CPDFFreeTextAnnotation).font?.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.family) as? String,
  1628. let style = (annotation as! CPDFFreeTextAnnotation).font?.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.face) as? String {
  1629. DispatchQueue.main.async {
  1630. let selectedStyleIndex = self!.setFontStyle(fontName: family, currentStyle: style)
  1631. self!.fontStylePopUpButton.selectItem(at: Int(selectedStyleIndex))
  1632. }
  1633. DispatchQueue.global(qos: .default).async {
  1634. let fonts = NSFontManager.shared.availableFontFamilies
  1635. let menu = NSMenu()
  1636. var selectedIndex = 0
  1637. for (index, fontName) in fonts.enumerated() {
  1638. if let font = NSFont(name: fontName, size: 12.0) {
  1639. let attributedString = NSAttributedString(string: fontName, attributes: [.font: font])
  1640. let item = NSMenuItem()
  1641. item.attributedTitle = attributedString
  1642. menu.addItem(item)
  1643. if (annotation as! CPDFFreeTextAnnotation).font?.fontName == font.fontName {
  1644. selectedIndex = index
  1645. }
  1646. }
  1647. }
  1648. DispatchQueue.main.async {
  1649. self!.fontPopUpButton.menu = menu
  1650. self!.fontPopUpButton.selectItem(at: selectedIndex)
  1651. }
  1652. }
  1653. }
  1654. }
  1655. }
  1656. window?.orderFront(sender)
  1657. }
  1658. @IBAction func fontPopUpButtonAction(_ sender: NSPopUpButton) {
  1659. guard let selectedItem = fontPopUpButton.selectedItem else { return }
  1660. let resultAtt = NSMutableAttributedString(attributedString: selectedItem.attributedTitle!)
  1661. let familyString = resultAtt.string
  1662. let selectIndex = setFontStyle(fontName: familyString, currentStyle: nil)
  1663. guard let styleString = fontStylePopUpButton.selectedItem?.title else { return }
  1664. fontStylePopUpButton.selectItem(at: Int(selectIndex))
  1665. if annotationModel?.annotations != nil {
  1666. for tAnnotation in annotations {
  1667. if tAnnotation is CPDFFreeTextAnnotation {
  1668. let newAnnotation = (tAnnotation as! CPDFFreeTextAnnotation)
  1669. if let font = newAnnotation.font {
  1670. if let fontSize = font.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.size) as? String {
  1671. let attributeFontDescriptor = NSFontDescriptor(fontAttributes: [NSFontDescriptor.AttributeName.family: familyString, NSFontDescriptor.AttributeName.face: styleString])
  1672. if let newFont = NSFont(descriptor: attributeFontDescriptor, size: fontSize.stringToCGFloat()) {
  1673. newAnnotation.font = newFont
  1674. }
  1675. }
  1676. if let tFont = newAnnotation.font {
  1677. if let family = tFont.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.family) as? String,
  1678. let style = tFont.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.face) as? String {
  1679. DispatchQueue.main.async {
  1680. let selectedStyleIndex = self.setFontStyle(fontName: family, currentStyle: style)
  1681. self.fontStylePopUpButton.selectItem(at: Int(selectedStyleIndex))
  1682. }
  1683. }
  1684. }
  1685. }
  1686. } else if tAnnotation is KMSelfSignAnnotationFreeText {
  1687. let newAnnotation = (tAnnotation as! KMSelfSignAnnotationFreeText)
  1688. if let font = newAnnotation.font {
  1689. if let fontSize = font.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.size) as? String {
  1690. let attributeFontDescriptor = NSFontDescriptor(fontAttributes: [NSFontDescriptor.AttributeName.family: familyString, NSFontDescriptor.AttributeName.face: styleString])
  1691. if let newFont = NSFont(descriptor: attributeFontDescriptor, size: fontSize.stringToCGFloat()) {
  1692. newAnnotation.font = newFont
  1693. }
  1694. }
  1695. newAnnotation.updateBounds()
  1696. if let tFont = newAnnotation.font {
  1697. if let family = tFont.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.family) as? String,
  1698. let style = tFont.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.face) as? String {
  1699. DispatchQueue.main.async {
  1700. let selectedStyleIndex = self.setFontStyle(fontName: family, currentStyle: style)
  1701. self.fontStylePopUpButton.selectItem(at: Int(selectedStyleIndex))
  1702. }
  1703. }
  1704. }
  1705. }
  1706. }
  1707. }
  1708. } else {
  1709. if annotationType == .freeText || annotationType == .signText || annotationType == .signDate {
  1710. let font = NSFont(name: (annotationModel?.fontName())!, size: (annotationModel?.fontSize())!) ?? NSFont.systemFont(ofSize: 16)
  1711. if let fontSize = font.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.size) as? String {
  1712. let attributeFontDescriptor = NSFontDescriptor(fontAttributes: [NSFontDescriptor.AttributeName.family: familyString, NSFontDescriptor.AttributeName.face: styleString])
  1713. if let newFont = NSFont(descriptor: attributeFontDescriptor, size: fontSize.stringToCGFloat()) {
  1714. annotationModel?.setFontName(newFont.fontName)
  1715. annotationModel?.setFontSize(newFont.pointSize)
  1716. }
  1717. }
  1718. let tFont = NSFont(name: (annotationModel?.fontName())!, size: (annotationModel?.fontSize())!) ?? NSFont.systemFont(ofSize: 16)
  1719. if let family = tFont.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.family) as? String,
  1720. let style = tFont.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.face) as? String {
  1721. DispatchQueue.main.async {
  1722. let selectedStyleIndex = self.setFontStyle(fontName: family, currentStyle: style)
  1723. self.fontStylePopUpButton.selectItem(at: Int(selectedStyleIndex))
  1724. }
  1725. }
  1726. }
  1727. }
  1728. updateAnnotation()
  1729. }
  1730. @IBAction func fontSizeComboBoxAction(_ sender: NSComboBox) {
  1731. if annotationModel?.annotations != nil {
  1732. for tAnnotation in annotations {
  1733. if tAnnotation is CPDFFreeTextAnnotation {
  1734. let newAnnotation = (tAnnotation as! CPDFFreeTextAnnotation)
  1735. if let font = newAnnotation.font {
  1736. let newFont = NSFont(name: font.fontName, size: CGFloat(fontSizeComboBox.floatValue))
  1737. if let newFont = newFont {
  1738. newAnnotation.font = newFont
  1739. }
  1740. }
  1741. } else if tAnnotation is KMSelfSignAnnotationFreeText {
  1742. let newAnnotation = (tAnnotation as! KMSelfSignAnnotationFreeText)
  1743. if let font = newAnnotation.font {
  1744. let newFont = NSFont(name: font.fontName, size: CGFloat(fontSizeComboBox.floatValue))
  1745. if let newFont = newFont {
  1746. newAnnotation.font = newFont
  1747. }
  1748. }
  1749. newAnnotation.updateBounds()
  1750. }
  1751. }
  1752. } else {
  1753. if annotationType == .freeText || annotationType == .signText || annotationType == .signDate {
  1754. let font = NSFont(name: (annotationModel?.fontName()!)!, size: (annotationModel?.fontSize())!) ?? NSFont.systemFont(ofSize: 16)
  1755. let newFont = NSFont(name: font.fontName, size: CGFloat(fontSizeComboBox.floatValue))
  1756. if let newFont = newFont {
  1757. annotationModel?.setFontName(newFont.fontName)
  1758. annotationModel?.setFontSize(newFont.pointSize)
  1759. }
  1760. }
  1761. }
  1762. updateAnnotation()
  1763. NotificationCenter.default.post(name: NSNotification.Name("KMAnnotationFontTypeNotification"),
  1764. object: pdfView)
  1765. adjustFreeText()
  1766. }
  1767. //字重 NSFontWeight
  1768. @IBAction func fontStylePopUpButtonAction(_ sender: NSPopUpButton) {
  1769. if let styleString = self.fontStylePopUpButton.selectedItem?.title {
  1770. if annotationModel?.annotations != nil {
  1771. for tAnnotation in self.annotations {
  1772. if tAnnotation is CPDFFreeTextAnnotation {
  1773. let newAnnotation = (tAnnotation as! CPDFFreeTextAnnotation)
  1774. if let font = newAnnotation.font {
  1775. if let fontSize = font.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.size) as? String,
  1776. let familyString = font.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.family) as? String {
  1777. let attributeFontDescriptor = NSFontDescriptor(fontAttributes: [NSFontDescriptor.AttributeName.family: familyString, NSFontDescriptor.AttributeName.face: styleString])
  1778. if let newFont = NSFont(descriptor: attributeFontDescriptor, size: fontSize.stringToCGFloat()) {
  1779. newAnnotation.font = newFont
  1780. }
  1781. }
  1782. }
  1783. } else if tAnnotation is KMSelfSignAnnotationFreeText {
  1784. let newAnnotation = (tAnnotation as! KMSelfSignAnnotationFreeText)
  1785. if let font = newAnnotation.font {
  1786. if let fontSize = font.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.size) as? String,
  1787. let familyString = font.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.family) as? String {
  1788. let attributeFontDescriptor = NSFontDescriptor(fontAttributes: [NSFontDescriptor.AttributeName.family: familyString, NSFontDescriptor.AttributeName.face: styleString])
  1789. if let newFont = NSFont(descriptor: attributeFontDescriptor, size: fontSize.stringToCGFloat()) {
  1790. newAnnotation.font = newFont
  1791. }
  1792. newAnnotation.updateBounds()
  1793. }
  1794. }
  1795. }
  1796. }
  1797. } else {
  1798. if annotationType == .freeText || annotationType == .signText || annotationType == .signDate {
  1799. let font = NSFont(name: (annotationModel?.fontName())!, size: (annotationModel?.fontSize())!) ?? NSFont.systemFont(ofSize: 16)
  1800. if let fontSize = font.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.size) as? String,
  1801. let familyString = font.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.family) as? String {
  1802. let attributeFontDescriptor = NSFontDescriptor(fontAttributes: [NSFontDescriptor.AttributeName.family: familyString, NSFontDescriptor.AttributeName.face: styleString])
  1803. if let newFont = NSFont(descriptor: attributeFontDescriptor, size: fontSize.stringToCGFloat()) {
  1804. annotationModel?.setFontName(newFont.fontName)
  1805. annotationModel?.setFontSize(newFont.pointSize)
  1806. }
  1807. }
  1808. }
  1809. }
  1810. }
  1811. updateAnnotation()
  1812. }
  1813. @IBAction func buttonClicked_SwitchIncludeTime(_ sender: NSButton) {
  1814. for case let tAnnotation as KMSelfSignAnnotationFreeText in annotations {
  1815. tAnnotation.includeTime = sender.state == .on ? true : false
  1816. let index = datePopUpButton.indexOfSelectedItem
  1817. datePopUpButton.removeAllItems()
  1818. datePopUpButton.addItems(withTitles: KMSelfSignAnnotationFreeText.fetchAllDateString(includeTime: false))
  1819. datePopUpButton.selectItem(at: index)
  1820. if self.subType == .date && isannotationMode {
  1821. UserDefaults.standard.set(sender.state == .on ? true : false, forKey: SKAnnotationSelfSignDateFormatterIncludeTimeKey)
  1822. }
  1823. }
  1824. reloadData()
  1825. }
  1826. @IBAction func dateCheckButtonAction(_ sender: NSPopUpButton) {
  1827. for case let tAnnotation as KMSelfSignAnnotationFreeText in annotations {
  1828. if subType == .date && isannotationMode {
  1829. let fetchArray = KMSelfSignAnnotationFreeText.fetchAllDateString(includeTime: false)
  1830. if sender.indexOfSelectedItem >= 0 && sender.indexOfSelectedItem <= fetchArray.count - 1 {
  1831. UserDefaults.standard.set(sender.indexOfSelectedItem, forKey: SKAnnotationSelfSignDateFormatterKey)
  1832. }
  1833. }
  1834. tAnnotation.dateFormatIndex = sender.indexOfSelectedItem
  1835. }
  1836. reloadData()
  1837. }
  1838. func annotationFontReload() {
  1839. DispatchQueue.global(qos: .default).async { [self] in
  1840. let fonts = NSFontManager.shared.availableFontFamilies
  1841. let menu = NSMenu()
  1842. var selectedIndex = 0
  1843. for (index, fontName) in fonts.enumerated() {
  1844. if let font = NSFont(name: fontName, size: 12.0) {
  1845. let attributedString = NSAttributedString(string: fontName, attributes: [NSAttributedString.Key.font: font])
  1846. let item = NSMenuItem()
  1847. item.attributedTitle = attributedString
  1848. menu.addItem(item)
  1849. if annotationType == .freeText {
  1850. if annotationModel?.fontName() == font.fontName {
  1851. selectedIndex = index
  1852. }
  1853. }
  1854. }
  1855. }
  1856. if annotationType == .freeText {
  1857. let freetextFont = NSFont(name: (annotationModel?.fontName() as? String)!, size: 16) ?? NSFont.systemFont(ofSize: 16)
  1858. if let family = freetextFont.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.family) as? String,
  1859. let style = freetextFont.fontDescriptor.object(forKey: NSFontDescriptor.AttributeName.face) as? String {
  1860. DispatchQueue.main.async { [self] in
  1861. fontPopUpButton.menu = menu
  1862. fontPopUpButton.selectItem(at: selectedIndex)
  1863. let selectedStyleIndex = setFontStyle(fontName: family, currentStyle: style)
  1864. fontStylePopUpButton.selectItem(at: Int(selectedStyleIndex))
  1865. }
  1866. }
  1867. }
  1868. }
  1869. }
  1870. func adjustFreeText() {
  1871. if annotationModel?.annotations != nil {
  1872. if annotationType == .freeText || annotationType == .signText || annotationType == .signDate {
  1873. for an in (annotationModel?.annotations)! {
  1874. if let freeTextAn = an as? CPDFFreeTextAnnotation {
  1875. let font = freeTextAn.font
  1876. var attributes: [NSAttributedString.Key: Any] = [NSAttributedString.Key.font: font as Any]
  1877. let style = NSMutableParagraphStyle()
  1878. style.alignment = freeTextAn.alignment
  1879. attributes[NSAttributedString.Key.paragraphStyle] = style
  1880. let contents = freeTextAn.contents ?? ""
  1881. let textViewSize = contents.boundingRect(with: CGSize(width: freeTextAn.bounds.size.width, height: CGFloat(MAXFLOAT)),
  1882. options: .usesLineFragmentOrigin,
  1883. attributes: attributes).size
  1884. var rect = freeTextAn.bounds
  1885. if textViewSize.height != freeTextAn.bounds.size.height {
  1886. rect.origin.y -= (textViewSize.height - rect.size.height)
  1887. rect.size.height = textViewSize.height
  1888. }
  1889. if textViewSize.width < freeTextAn.bounds.size.width {
  1890. rect.size.width = textViewSize.width
  1891. }
  1892. freeTextAn.bounds = rect
  1893. }
  1894. }
  1895. }
  1896. }
  1897. }
  1898. // MARK: NSNotification
  1899. @objc func textDidChange(_ notification: Notification) {
  1900. self.isTextEdit = true
  1901. if self.subType == KMSelfSignAnnotationFreeTextSubType.freeText && isannotationMode {
  1902. if let obj = notification.object as? NSObject {
  1903. for tAnnotation in annotations {
  1904. tAnnotation.setString(noteTextView.string)
  1905. }
  1906. if obj.isEqual(to: noteTextView) {
  1907. UserDefaults.standard.set(noteTextView.string, forKey: SKAnnotationSelfSignPlaceHolderStringKey)
  1908. }
  1909. if annotation.isKind(of: CPDFFreeTextAnnotation.self) {
  1910. textAnnotationImageView.image = annotationModel?.annotationImage
  1911. } else {
  1912. generalImageView.image = annotationModel?.annotationImage
  1913. }
  1914. }
  1915. } else {
  1916. if let obj = notification.object as? NSObject, obj.isEqual(to: noteTextView) {
  1917. for tAnnotation in self.annotations {
  1918. tAnnotation.setString(noteTextView.string)
  1919. }
  1920. }
  1921. }
  1922. }
  1923. @objc func themeChanged(_ notification: Notification) {
  1924. DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) { [self] in
  1925. updateViewColor()
  1926. }
  1927. }
  1928. }
  1929. class KMGeneralAnnotationColorButton: NSButton {
  1930. var _circleColor: NSColor = .clear
  1931. var _buttonBorderColor: NSColor = .clear
  1932. // MARK: Get & Set
  1933. var circleColor: NSColor {
  1934. set {
  1935. _circleColor = newValue
  1936. needsDisplay = true
  1937. }
  1938. get {
  1939. return _circleColor
  1940. }
  1941. }
  1942. var buttonBorderColor: NSColor {
  1943. set {
  1944. _buttonBorderColor = newValue
  1945. needsDisplay = true
  1946. }
  1947. get {
  1948. return _buttonBorderColor
  1949. }
  1950. }
  1951. // MARK: Init
  1952. required init?(coder: NSCoder) {
  1953. super.init(coder: coder)
  1954. wantsLayer = true
  1955. layer?.cornerRadius = 12.0
  1956. layer?.masksToBounds = true
  1957. layer?.borderColor = NSColor.clear.cgColor
  1958. }
  1959. override func draw(_ dirtyRect: NSRect) {
  1960. super.draw(dirtyRect)
  1961. let path3 = NSBezierPath(ovalIn: NSMakeRect(3, 3, dirtyRect.size.width - 6, dirtyRect.size.height - 6))
  1962. }
  1963. }
  1964. class KMNoteIconButtonCell: NSButtonCell {
  1965. override func drawImage(_ image: NSImage, withFrame frame: NSRect, in controlView: NSView) {
  1966. let titleSize = self.attributedTitle.size()
  1967. let imageSize = self.image!.size
  1968. var rect = NSZeroRect
  1969. let leftGap = (controlView.bounds.size.width - image.size.width) / 2.0
  1970. rect.origin.x = leftGap
  1971. if self.imagePosition == .imageAbove {
  1972. rect.origin.y = 6
  1973. } else {
  1974. rect.origin.y = (controlView.frame.size.height - image.size.height) / 2.0
  1975. }
  1976. rect.size = imageSize
  1977. super.drawImage(self.image!, withFrame: rect, in: controlView)
  1978. }
  1979. override func drawTitle(_ title: NSAttributedString, withFrame frame: NSRect, in controlView: NSView) -> NSRect {
  1980. let titleSize = self.attributedTitle.size()
  1981. let imageSize = self.image!.size
  1982. let totalHeight = titleSize.height + imageSize.height + 1
  1983. var rect = NSZeroRect
  1984. rect.origin.x = (controlView.bounds.size.width - titleSize.width) / 2.0
  1985. rect.origin.y = (controlView.bounds.size.height - totalHeight) / 2.0 + imageSize.height + 1
  1986. rect.size = titleSize
  1987. return super.drawTitle(title, withFrame: rect, in: controlView)
  1988. }
  1989. }