KMGeneralAnnotationViewController.swift 126 KB

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