KMGeneralAnnotationViewController.swift 123 KB

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