KMGeneralAnnotationViewController.swift 123 KB

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