OSDN Git Service

バージョンを1.9b33に更新
[kcd/KCD.git] / KCD / StrokeTextFieldCell.swift
1 //
2 //  StrokeTextFieldCell.swift
3 //  KCD
4 //
5 //  Created by Hori,Masaki on 2017/01/01.
6 //  Copyright © 2017年 Hori,Masaki. All rights reserved.
7 //
8
9 import Cocoa
10
11 final class StrokeTextFieldCell: NSTextFieldCell {
12     
13     private static let boarderWidth: CGFloat = 2.0
14     
15     private let layoutManager = NSLayoutManager()
16     private let textContainer = NSTextContainer()
17     
18     required init(coder: NSCoder) {
19                 
20         super.init(coder: coder)
21         
22         layoutManager.addTextContainer(textContainer)
23     }
24     
25     override func drawInterior(withFrame cellFrame: NSRect, in controlView: NSView) {
26         
27         let attributedString = attributedStringValue
28         
29         if attributedString.string.hasSuffix("/") {
30             
31             super.drawInterior(withFrame: cellFrame, in: controlView)
32             
33             return
34         }
35         
36         let attribute = attributedString.attributes(at: 0, effectiveRange: nil)
37         
38         guard let forgroundColor = attribute[.foregroundColor] as? NSColor else { return }
39         
40         if forgroundColor == .controlTextColor {
41             
42             super.drawInterior(withFrame: cellFrame, in: controlView)
43             
44             return
45         }
46         
47         guard let font = attribute[.font] as? NSFont else { return }
48         
49         let textStorage = NSTextStorage(string: attributedString.string, attributes: attribute)
50         textStorage.addLayoutManager(layoutManager)
51         let range = layoutManager.glyphRange(for: textContainer)
52         let glyph = UnsafeMutablePointer<CGGlyph>.allocate(capacity: range.length)
53         let glyphLength = layoutManager.getGlyphs(in: range,
54                                                   glyphs: glyph,
55                                                   properties: nil,
56                                                   characterIndexes: nil,
57                                                   bidiLevels: nil)
58         var point = NSPoint(x: StrokeTextFieldCell.boarderWidth, y: 0)
59         point.y -= font.descender
60         
61         if controlView.isFlipped {
62             
63             point.y -= controlView.frame.height
64         }
65         
66         let nsGlyph = UnsafeMutablePointer<NSGlyph>.allocate(capacity: range.length)
67         
68         (0..<range.length).forEach { nsGlyph[$0] = NSGlyph(glyph[$0]) }
69         
70         let path = NSBezierPath()
71         path.move(to: point)
72         path.appendGlyphs(nsGlyph, count: glyphLength, in: font)
73         path.lineWidth = StrokeTextFieldCell.boarderWidth
74         path.lineJoinStyle = .roundLineJoinStyle
75         
76         if controlView.isFlipped {
77             
78             var affineTransform = AffineTransform()
79             affineTransform.scale(x: 1, y: -1)
80             path.transform(using: affineTransform)
81         }
82         
83         NSColor.black.set()
84         path.stroke()
85         forgroundColor.set()
86         path.fill()
87         
88         textStorage.removeLayoutManager(layoutManager)
89     }
90 }