aboutsummaryrefslogtreecommitdiffstats
path: root/NSAttributedString+Moxie.m
blob: f39a4a05b768a8503bd80d63e7759e257247bda3 (plain)
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
//
//  NSAttributedString+Moxie.m
//  Moxie
//
//  Created by Brian Cully on Tue Aug 24 2004.
//  Copyright (c) 2004 Brian Cully. All rights reserved.
//

#import "NSAttributedString+Moxie.h"

@implementation NSAttributedString (Moxie)
+ (id)attributedStringWithString: (NSString *)aString
{
    NSAttributedString *retval;
    
    retval = [[self alloc] initWithString: aString];
    return [retval autorelease];
}

+ (id)attributedStringWithString: (NSString *)aString
                      attributes: (NSDictionary *)attributes
{
    NSAttributedString *retval;
    
    retval = [[self alloc] initWithString: aString
                               attributes: attributes];
    return [retval autorelease];
}

+ (NSAttributedString *)attributedStringWithForm: (NSArray *)aForm
                               defaultAttributes: (NSDictionary *)defaultAttributes
{
    NSArray *rangeForms, *attrRange;
    NSEnumerator *rangeEnum;
    NSMutableAttributedString *result;
    NSString *string;

    if ([aForm count] < 2)
        return nil;
    
    string = [aForm objectAtIndex: 0];
    rangeForms = [aForm objectAtIndex: 1];

    result = [NSMutableAttributedString attributedStringWithString: string attributes: defaultAttributes];
    rangeEnum = [rangeForms objectEnumerator];
    while ((attrRange = [rangeEnum nextObject]) != nil) {
        NSArray *tmpForm;
        NSDictionary *attrDict;
        NSMutableDictionary *myAttributes;
        NSRange range;
        
        attrDict = [NSDictionary dictionaryWithAlist: attrRange];
        myAttributes = [NSMutableDictionary dictionary];

        // Should handle :BOLD as well.
        tmpForm = [attrDict objectForKey: @":RANGE"];
        if (tmpForm)
            range = NSMakeRange([[tmpForm objectAtIndex: 0] intValue], [[tmpForm objectAtIndex: 1] intValue]);
        else
            range = NSMakeRange(0, [string length]);

        tmpForm = [attrDict objectForKey: @":FONT"];
        if (tmpForm) {
            NSFont *font;
            
            font = [NSFont fontWithName: [tmpForm objectAtIndex: 0]
                                   size: [[tmpForm objectAtIndex: 1] doubleValue]];
            if (font)
                [myAttributes setObject: font forKey: NSFontAttributeName];
            else
                NSLog(@"WARNING: Couldn't find font named: %@.", [tmpForm objectAtIndex: 0]);
        }
        
        tmpForm = [attrDict objectForKey: @":LINK"];
        if (tmpForm) {
            [myAttributes setObject: [NSURL URLWithString: [tmpForm objectAtIndex: 0]]
                             forKey: NSLinkAttributeName];
            [myAttributes setObject: [NSCursor pointingHandCursor]
                             forKey: NSCursorAttributeName];
        }

        tmpForm = [attrDict objectForKey: @":ITALIC"];
        if (tmpForm) {
            [myAttributes setObject: [NSNumber numberWithFloat: [[tmpForm objectAtIndex: 0] doubleValue]]
                             forKey: NSObliquenessAttributeName];
        }
        
        tmpForm = [attrDict objectForKey: @":SUPER"];
        if (tmpForm) {
            [myAttributes setObject: [NSNumber numberWithInt: [[tmpForm objectAtIndex: 0] intValue]]
                             forKey: NSSuperscriptAttributeName];
        }
        
        tmpForm = [attrDict objectForKey: @":UNDERLINE"];
        if (tmpForm) {
            [myAttributes setObject: [NSNumber numberWithInt: [[tmpForm objectAtIndex: 0] intValue]]
                             forKey: NSUnderlineStyleAttributeName];
        }

        tmpForm = [attrDict objectForKey: @":STRIKETHROUGH"];
        if (tmpForm) {
            [myAttributes setObject: [NSNumber numberWithInt: [[tmpForm objectAtIndex: 0] intValue]]
                             forKey: NSStrikethroughStyleAttributeName];
        }
        
        tmpForm = [attrDict objectForKey: @":COLOR"];
        if (tmpForm) {
            int r, g, b;
            
            r = [[tmpForm objectAtIndex: 0] intValue];
            g = [[tmpForm objectAtIndex: 1] intValue];
            b = [[tmpForm objectAtIndex: 2] intValue];
            [myAttributes setObject: [NSColor colorWithCalibratedRed: (r * 1.0) / 255
                                                               green: (g * 1.0) / 255
                                                                blue: (b * 1.0) / 255
                                                               alpha: 1.0]
                             forKey: NSForegroundColorAttributeName];
        }

        tmpForm = [attrDict objectForKey: @":BACKGROUND-COLOR"];
        if (tmpForm) {
            NSColor *bgColor;
            int r, g, b;
            float a;
            
            r = [[tmpForm objectAtIndex: 0] intValue];
            g = [[tmpForm objectAtIndex: 1] intValue];
            b = [[tmpForm objectAtIndex: 2] intValue];
            a = 1.0;
            bgColor = [defaultAttributes objectForKey: NSBackgroundColorAttributeName];
            if (bgColor) {
                a = [bgColor alphaComponent];
                NSLog(@"DEBUG:\t alpha: %f", a);
            }
            [myAttributes setObject: [NSColor colorWithCalibratedRed: (r * 1.0) / 255
                                                               green: (g * 1.0) / 255
                                                                blue: (b * 1.0) / 255
                                                               alpha: a]
                             forKey: NSBackgroundColorAttributeName];
        }
        

        // These two attributes are broken, because they rely on information we might
        // not have. To whit, the FG and BG colors of the open world.
        tmpForm = [attrDict objectForKey: @":BOLD"];
        if (tmpForm) {
            NSColor *preColor;
            CGFloat r, g, b, a;
            
            preColor = [myAttributes objectForKey: NSForegroundColorAttributeName];
            if (preColor == nil)
                preColor = [defaultAttributes objectForKey: NSForegroundColorAttributeName];
            if (preColor) {
                [preColor getRed: &r green: &g blue: &b alpha: &a];
                r += 0.3; g += 0.3; b += 0.3;
                r = MIN(r, 1.0);
                g = MIN(g, 1.0);
                b = MIN(b, 1.0);
                [myAttributes setObject: [NSColor colorWithCalibratedRed: r green: g blue: b alpha: a]
                                 forKey: NSForegroundColorAttributeName];
            }
        }
        
        tmpForm = [attrDict objectForKey: @":INVERSE"];
        if (tmpForm) {
            NSColor *fgColor, *bgColor;
            
            fgColor = [myAttributes objectForKey: NSForegroundColorAttributeName];
            if (fgColor == nil)
                fgColor = [defaultAttributes objectForKey: NSForegroundColorAttributeName];
            bgColor = [myAttributes objectForKey: NSBackgroundColorAttributeName];
            if (bgColor == nil)
                bgColor = [defaultAttributes objectForKey: NSBackgroundColorAttributeName];
            
            [fgColor retain];
            [bgColor retain];
            if (fgColor && bgColor) {
                [myAttributes setObject: bgColor
                                 forKey: NSForegroundColorAttributeName];
                [myAttributes setObject: fgColor
                                 forKey: NSBackgroundColorAttributeName];
            }
            [fgColor release];
            [bgColor release];
        }
        
        [result addAttributes: myAttributes range: range];
    }
    
    return result;
}
@end