KMGeneralAnnotationViewController.swift 126 KB

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