-
Notifications
You must be signed in to change notification settings - Fork 0
/
attributed_string.rb
executable file
·143 lines (124 loc) · 3.61 KB
/
attributed_string.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
module HotCocoa
class NSRangedProxyAttributeHash
ATTRIBUTE_KEYS = { :font => NSFontAttributeName,
:paragraph_style => NSParagraphStyleAttributeName,
:color => NSForegroundColorAttributeName,
:underline_style => NSUnderlineStyleAttributeName,
:superscript => NSSuperscriptAttributeName,
:background_color => NSBackgroundColorAttributeName,
:attachment => NSAttachmentAttributeName,
:ligature => NSLigatureAttributeName,
:baseline_offset => NSBaselineOffsetAttributeName,
:kerning => NSKernAttributeName,
:link => NSLinkAttributeName,
:stroke_width => NSStrokeWidthAttributeName,
:stroke_color => NSStrokeColorAttributeName,
:underline_color => NSUnderlineColorAttributeName,
:strikethrough_style => NSStrikethroughStyleAttributeName,
:strikethrough_color => NSStrikethroughColorAttributeName,
:shadow => NSShadowAttributeName,
:obliqueness => NSObliquenessAttributeName,
:expansion_factor => NSExpansionAttributeName,
:cursor => NSCursorAttributeName,
:tool_tip => NSToolTipAttributeName,
:character_shape => NSCharacterShapeAttributeName,
:glyph_info => NSGlyphInfoAttributeName,
:marked_clause_segment => NSMarkedClauseSegmentAttributeName,
:spelling_state => NSSpellingStateAttributeName }
def initialize(proxy)
@proxy = proxy
end
def [](k)
k = attribute_for_key(k)
@proxy.string.attribute(k, atIndex:@proxy.range.first, effectiveRange:nil)
end
def []=(k,v)
k = attribute_for_key(k)
@proxy.string.removeAttribute(k, range:@proxy.range.to_NSRange(@proxy.string.length - 1))
@proxy.string.addAttribute(k, value:v, range:@proxy.range.to_NSRange(@proxy.string.length - 1))
end
def <<(attributes)
attributes.each_pair do |k, v|
self[k] = v
end
self
end
alias :merge :<<
def to_hash
@proxy.string.attributesAtIndex(@proxy.range.first, effectiveRange:nil).inject({}) do |h, pair|
h[key_for_attribute(pair.first)] = pair.last
h
end
end
def inspect
to_hash.inspect
end
private
def key_for_attribute(attribute)
(ATTRIBUTE_KEYS.select { |k,v| v == attribute }.first || [attribute]).first
end
def attribute_for_key(key)
ATTRIBUTE_KEYS[key] || key
end
end
class NSRangedProxyAttributedString
attr_reader :string, :range
def initialize(string, range)
@string = string
@range = range
end
def attributes
NSRangedProxyAttributeHash.new(self)
end
end
end
class String
def with_attributes(attributes = {})
attributed_string = NSMutableAttributedString.alloc.initWithString(self)
attributed_string.attributes << attributes
attributed_string
end
end
class Range
def to_NSRange(max = nil)
location = first
if last == -1 and max
length = max - first + 1
else
length = last - first + 1
end
NSRange.new(location, length)
end
end
class NSMutableAttributedString
def with_attributes(attributes = {})
string.with_attributes(attributes)
end
def <<(s)
case s
when String
mutableString.appendString s
else
appendAttributedString s
end
end
def +(s)
attributed_string = mutableCopy
attributed_string << s
attributed_string
end
def attributes
HotCocoa::NSRangedProxyAttributedString.new(self, 0..-1).attributes
end
def [](r)
HotCocoa::NSRangedProxyAttributedString.new(self, r)
end
def []=(r, s)
case s
when String
replaceCharactersInRange(r.to_NSRange(length - 1), :withString => s)
else
replaceCharactersInRange(r.to_NSRange(length - 1), :withAttributedString => s)
end
end
end