Changeset 2134

Show
Ignore:
Timestamp:
11/21/04 12:17:14 (4 years ago)
Author:
timothy
Message:

* Added the "green hack" back in now that is works with Safari 1.3.
* Removed old initWithHTML calls for Jaguar (no longer supported).
* Fixed a possible exception with colors and converting to mIRC formatting. We now always check/convert color spaces before trying to get the RGB values. This would happen for black, grey and white colors since they use a calibrated black/white color space, not RGB.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/NSAttributedStringAdditions.m

    r2124 r2134  
    8989        NSParameterAssert( fragment != nil ); 
    9090 
    91         NSMutableAttributedString *result = nil; 
    92         if( NSAppKitVersionNumber >= 700. ) { 
    93                 // the rgba CSS makes WebKit give colorless text where no color was specified (instead of black) 
    94                 NSString *render = [NSString stringWithFormat:@"<span style=\"color: rgba( 0, 0, 0, 0.0 )\">%@</span>", fragment]; 
    95                 result = [[NSMutableAttributedString alloc] initWithHTML:[render dataUsingEncoding:NSUTF8StringEncoding] options:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:1], @"UseWebKit", @"utf-8", @"TextEncodingName", url, @"BaseURL", nil] documentAttributes:NULL]; 
    96         } else { 
    97                 NSString *render = [NSString stringWithFormat:@"<html><head><meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" /></head><body>%@</body></html>", fragment]; 
    98                 result = [[NSAttributedString alloc] initWithHTML:[render dataUsingEncoding:NSUTF8StringEncoding] baseURL:url documentAttributes:NULL]; 
     91        NSMutableDictionary *options = [NSMutableDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:1], @"UseWebKit", @"utf-8", @"TextEncodingName", nil]; 
     92        if( url ) [options setObject:url forKey:@"BaseURL"]; 
     93 
     94        // we suround the fragment in the #01fe02 green color so we can later key it out and strip it 
     95        // this will result in colorless areas of our string, letting the color be defined by the interface 
     96 
     97        NSString *render = [NSString stringWithFormat:@"<span style=\"color: #01fe02\">%@</span>", fragment]; 
     98        NSMutableAttributedString *result = [[NSMutableAttributedString alloc] initWithHTML:[render dataUsingEncoding:NSUTF8StringEncoding] options:options documentAttributes:NULL]; 
     99 
     100        NSRange limitRange, effectiveRange; 
     101        limitRange = NSMakeRange( 0, [result length] ); 
     102        while( limitRange.length > 0 ) { 
     103                NSColor *color = [result attribute:NSForegroundColorAttributeName atIndex:limitRange.location longestEffectiveRange:&effectiveRange inRange:limitRange]; 
     104                if( [[color HTMLAttributeValue] isEqualToString:@"#01fe02"] ) // strip the color if it matched 
     105                        [result removeAttribute:NSForegroundColorAttributeName range:effectiveRange]; 
     106                limitRange = NSMakeRange( NSMaxRange( effectiveRange ), NSMaxRange( limitRange ) - NSMaxRange( effectiveRange ) ); 
    99107        } 
    100108 
     
    109117        NSMutableString *ret = [NSMutableString string]; 
    110118 
    111         if( [[options objectForKey:@"FullDocument"] boolValue] ) { 
     119        if( [[options objectForKey:@"FullDocument"] boolValue] ) 
    112120                [ret appendString:@"<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"></head><body>"]; 
    113         } 
    114121 
    115122        limitRange = NSMakeRange( 0, [self length] ); 
     
    129136                NSMutableString *styleString = [NSMutableString string]; 
    130137 
    131                 if( foregoundColor && ! [[options objectForKey:@"IgnoreFontColors"] boolValue] ) { 
     138                if( foregoundColor && ! [[options objectForKey:@"IgnoreFontColors"] boolValue] ) 
    132139                        [styleString appendFormat:@"color: %@", [foregoundColor CSSAttributeValue]]; 
    133                 } 
    134140 
    135141                if( backgroundColor && ! [[options objectForKey:@"IgnoreFontColors"] boolValue] ) { 
     
    470476                        foregroundColor = [NSColor colorWithCalibratedRed:0. green:0. blue:0. alpha:1.]; 
    471477 
     478                if( ! [[foregroundColor colorSpaceName] isEqualToString:NSCalibratedRGBColorSpace] && ! [[foregroundColor colorSpaceName] isEqualToString:NSDeviceRGBColorSpace] ) 
     479                        foregroundColor = [foregroundColor colorUsingColorSpaceName:NSCalibratedRGBColorSpace]; // we need to convert to RGB space 
     480 
    472481                if( foregroundColor && ! [[options objectForKey:@"IgnoreFontColors"] boolValue] ) { 
    473482                        char buffer[6]; 
     
    479488                        sprintf( buffer, "\003%02d", ircColor ); 
    480489                        [ret appendBytes:buffer length:strlen( buffer )]; 
     490 
     491                        if( ! [[backgroundColor colorSpaceName] isEqualToString:NSCalibratedRGBColorSpace] && ! [[backgroundColor colorSpaceName] isEqualToString:NSDeviceRGBColorSpace] ) 
     492                                backgroundColor = [backgroundColor colorUsingColorSpaceName:NSCalibratedRGBColorSpace]; // we need to convert to RGB space 
    481493 
    482494                        if( backgroundColor ) {