-
Notifications
You must be signed in to change notification settings - Fork 2
/
NSAttributedString+HTML-IM.m
212 lines (202 loc) · 5.99 KB
/
NSAttributedString+HTML-IM.m
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
//
// NSAttributedString+HTML-IM.m
// Jabber
//
// Created by David Chisnall on 27/08/2007.
// Copyright 2007 __MyCompanyName__. All rights reserved.
//
#import "NSAttributedString+HTML-IM.h"
#import <EtoileFoundation/EtoileFoundation.h>
#import <EtoileXML/ETXMLWriter.h>
// Work around some GSBreakage
#define NSFontFamiliyClassMask NSFontFamilyClassMask
#import <AppKit/NSFontDescriptor.h>
#undef NSFontFamiliyClassMask
static NSMapTable * STYLE_HANDLERS = NULL;
typedef NSString*(*styleHandler)(id);
NSString * foregroundColor(NSColor * aColour)
{
CGFloat r,g,b,a;
[[aColour colorUsingColorSpaceName:NSCalibratedRGBColorSpace]
getRed:&r
green:&g
blue:&b
alpha:&a];
return [NSString stringWithFormat:@"color: #%.2x%.2x%.2x;",
(int) (r * 255),
(int) (g * 255),
(int) (b * 255)];
}
NSString * cssGenericFontFamily(NSFontSymbolicTraits traits)
{
if(traits & NSFontMonoSpaceTrait)
{
return @"monospace";
}
switch(traits & NSFontFamilyClassMask)
{
#define FONT_FAMILY(value, name) case value: return name; break;
case NSFontOldStyleSerifsClass:
case NSFontTransitionalSerifsClass:
case NSFontModernSerifsClass:
case NSFontClarendonSerifsClass:
case NSFontSlabSerifsClass:
case NSFontOrnamentalsClass:
FONT_FAMILY(NSFontFreeformSerifsClass, @"serif")
FONT_FAMILY(NSFontSansSerifClass, @"sans-serif")
FONT_FAMILY(NSFontScriptsClass, @"cursive")
FONT_FAMILY(NSFontSymbolicClass, @"fantasy")
#undef FONT_FAMILY
}
return nil;
}
NSString * fontAttributes(NSFont * aFont)
{
NSFontSymbolicTraits traits = [[aFont fontDescriptor] symbolicTraits];
#ifndef DNDEBUG
ETLog(@"Traits: %x", traits);
#endif
NSString * genericFamily = cssGenericFontFamily(traits);
NSMutableString * style;
if(genericFamily == nil)
{
style = [NSMutableString stringWithFormat:@"font-family: %@;",
[aFont familyName]];
}
else
{
style = [NSMutableString stringWithFormat:@"font-family: %@, %@;",
[aFont familyName],
genericFamily];
}
if(traits & NSFontItalicTrait)
{
[style appendString:@"font-style: oblique;"];
}
if(traits & NSFontBoldTrait)
{
[style appendString:@"font-weight: bold;"];
}
NSDictionary * attributes = [[aFont fontDescriptor] fontAttributes];
NSString * attribute;
if((attribute = [attributes objectForKey:NSFontSizeAttribute]))
{
[style appendFormat:@"font-size: %.1fpt;", [attribute floatValue]];
}
if((attribute = [attributes objectForKey:NSUnderlineStyleAttributeName]))
{
[style appendString:@"text-decoration: underline;"];
}
return style;
}
NSString * styleFromAttributes(NSDictionary * attributes)
{
NSMutableString * style = [NSMutableString string];
if(STYLE_HANDLERS == nil)
{
STYLE_HANDLERS = NSCreateMapTable(NSNonRetainedObjectMapKeyCallBacks,NSNonOwnedPointerMapValueCallBacks,10);
NSMapInsert(STYLE_HANDLERS,(__bridge const void*)NSForegroundColorAttributeName,(void*)foregroundColor);
NSMapInsert(STYLE_HANDLERS,(__bridge const void*)NSFontAttributeName,(void*)fontAttributes);
}
NSEnumerator * enumerator = [attributes keyEnumerator];
id key;
while(nil != (key = [enumerator nextObject]))
{
styleHandler handler = NSMapGet(STYLE_HANDLERS,(__bridge const void*)key);
if(handler != NULL)
{
[style appendString:handler([attributes objectForKey:key])];
}
}
return style;
}
void addCdataWithLineBreaksToNode(ETXMLWriter *xmlWriter, NSString *cdata)
{
NSArray * segments = [cdata componentsSeparatedByString:@"\n"];
if([segments count] > 1)
{
int i;
for(i=0 ; i < [segments count] -1 ; i++)
{
[xmlWriter characters: [segments objectAtIndex:i]];
[xmlWriter startElement: @"br"];
[xmlWriter endElement];
}
[xmlWriter characters: [segments objectAtIndex:i]];
}
else
{
[xmlWriter characters: cdata];
}
}
@implementation NSAttributedString (XHTML_IM)
- (void)writeXHTMLIMToXMLWriter: (ETXMLWriter*)xmlWriter
{
#ifndef DNDEBUG
ETLog(@"Writing XHTML...");
#endif
[xmlWriter startElement: @"html"
attributes: D(@"http://jabber.org/protocol/xhtml-im", @"xmlns")];
[xmlWriter startElement: @"body"
attributes: D(@"http://www.w3.org/1999/xhtml", @"xmlns")];
NSString * plainText = [self string];
NSRange attributeRange;
NSUInteger start = 0;
NSInteger end = [self length];
while(start < end)
{
//Get the range and attributes:
NSDictionary * attributes = [self attributesAtIndex:start effectiveRange:&attributeRange];
NSString * css = styleFromAttributes(attributes);
NSURL * linkTarget = [attributes objectForKey:NSLinkAttributeName];
NSMutableDictionary *tagAttributes = [NSMutableDictionary dictionary];
if (![css isEqualToString:@""] || linkTarget != nil)
{
if (![css isEqualToString:@""])
{
[tagAttributes setObject: styleFromAttributes(attributes) forKey: @"style"];
}
if (linkTarget != nil)
{
[tagAttributes setObject: linkTarget forKey: @"href"];
[xmlWriter startElement: @"a" attributes: tagAttributes];
}
else
{
[xmlWriter startElement: @"span" attributes: tagAttributes];
}
addCdataWithLineBreaksToNode(xmlWriter, [plainText substringWithRange:attributeRange]);
[xmlWriter endElement];
}
else
{
addCdataWithLineBreaksToNode(xmlWriter, [plainText substringWithRange:attributeRange]);
}
start = attributeRange.location + attributeRange.length;
}
[xmlWriter endElement];
[xmlWriter endElement];
}
- (NSString*) stringValueWithExpandedLinks
{
NSMutableString * string = [NSMutableString stringWithString:[self string]];
NSUInteger length = [self length];
int offset = 0;
for(int i = 0 ; i<length ; i++)
{
NSRange range;
NSString * href;
if((href = [self attribute:NSLinkAttributeName
atIndex:i
effectiveRange:&range]))
{
i += range.length;
NSString * link = [NSString stringWithFormat:@" (%@)", href];
[string insertString:link
atIndex:i + offset];
offset += [link length];
}
}
return string;
}
@end