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 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211
|
/**
NSAttributedString.m
Implementation of string class with attributes
Copyright (C) 1997,1999 Free Software Foundation, Inc.
Written by: ANOQ of the sun <anoq@vip.cybercity.dk>
Date: November 1997
Rewrite by: Richard Frith-Macdonald <richard@brainstorm.co.uk>
Date: April 1999
This file is part of GNUstep-base
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
If you are interested in a warranty or support for this source code,
contact Scott Christley <scottc@net-community.com> for more information.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110 USA.
<title>NSAttributedString class reference</title>
$Date$ $Revision$
*/
/* Warning - [-initWithString:attributes:] is the designated initialiser,
* but it doesn't provide any way to perform the function of the
* [-initWithAttributedString:] initialiser.
* In order to work round this, the string argument of the
* designated initialiser has been overloaded such that it
* is expected to accept an NSAttributedString here instead of
* a string. If you create an NSAttributedString subclass, you
* must make sure that your implementation of the initialiser
* copes with either an NSString or an NSAttributedString.
* If it receives an NSAttributedString, it should ignore the
* attributes argument and use the values from the string.
*/
#import "common.h"
#import "GNUstepBase/Unicode.h"
#import "Foundation/NSAttributedString.h"
#import "Foundation/NSData.h"
#import "Foundation/NSException.h"
#import "Foundation/NSAutoreleasePool.h"
#import "Foundation/NSPortCoder.h"
#import "Foundation/NSRange.h"
@class GSAttributedString;
@interface GSAttributedString : NSObject // Help the compiler
@end
@class GSMutableAttributedString;
@interface GSMutableAttributedString : NSObject // Help the compiler
@end
@class GSMutableDictionary;
@interface GSMutableDictionary : NSObject // Help the compiler
@end
static Class dictionaryClass = 0;
static SEL eqSel;
static SEL setSel;
static SEL getSel;
static SEL allocDictSel;
static SEL initDictSel;
static SEL addDictSel;
static SEL setDictSel;
static SEL relDictSel;
static SEL remDictSel;
static IMP allocDictImp;
static IMP initDictImp;
static IMP addDictImp;
static IMP setDictImp;
static IMP relDictImp;
static IMP remDictImp;
@interface GSMutableAttributedStringTracker : NSMutableString
{
NSMutableAttributedString *_owner;
}
+ (NSMutableString*) stringWithOwner: (NSMutableAttributedString*)as;
@end
/**
* A string in which name-value pairs represented by an [NSDictionary] may
* be associated to ranges of characters. Used for text rendering by the
* GUI/AppKit framework, in which fonts, sizes, etc. are stored under standard
* attributes in the dictionaries.
*
*/
@implementation NSAttributedString
static Class NSAttributedStringClass;
static Class GSAttributedStringClass;
static Class NSMutableAttributedStringClass;
static Class GSMutableAttributedStringClass;
+ (void) initialize
{
if (self == [NSAttributedString class])
{
NSAttributedStringClass = self;
GSAttributedStringClass = [GSAttributedString class];
NSMutableAttributedStringClass
= [NSMutableAttributedString class];
GSMutableAttributedStringClass
= [GSMutableAttributedString class];
dictionaryClass = [GSMutableDictionary class];
eqSel = @selector(isEqual:);
setSel = @selector(setAttributes:range:);
getSel = @selector(attributesAtIndex:effectiveRange:);
allocDictSel = @selector(allocWithZone:);
initDictSel = @selector(initWithDictionary:);
addDictSel = @selector(addEntriesFromDictionary:);
setDictSel = @selector(setObject:forKey:);
relDictSel = @selector(release);
remDictSel = @selector(removeObjectForKey:);
allocDictImp = [dictionaryClass methodForSelector: allocDictSel];
initDictImp = [dictionaryClass instanceMethodForSelector: initDictSel];
addDictImp = [dictionaryClass instanceMethodForSelector: addDictSel];
setDictImp = [dictionaryClass instanceMethodForSelector: setDictSel];
remDictImp = [dictionaryClass instanceMethodForSelector: remDictSel];
relDictImp = [dictionaryClass instanceMethodForSelector: relDictSel];
}
}
+ (id) allocWithZone: (NSZone*)z
{
if (self == NSAttributedStringClass)
return NSAllocateObject(GSAttributedStringClass, 0, z);
else
return NSAllocateObject(self, 0, z);
}
- (Class) classForCoder
{
return NSAttributedStringClass;
}
static void
appendUIntData(NSMutableData *d, NSUInteger i)
{
unsigned int aux = i;
unsigned int len = 1;
while (aux >= 128)
{
aux /= 128;
len++;
}
{
unsigned char *p, buf[len];
p = buf;
while (i >= 128)
{
*p++ = (i & 0x7f) + 128;
i /= 128;
}
*p = i;
[d appendBytes: buf length: len];
}
}
- (void) encodeWithCoder: (NSCoder*)aCoder
{
if ([aCoder allowsKeyedCoding])
{
NSUInteger length = [self length];
[aCoder encodeObject: [self string] forKey: @"NSString"];
if (length > 0)
{
NSRange range;
NSDictionary *attrs;
attrs = [self attributesAtIndex: 0 effectiveRange: &range];
if (range.length == length)
{
[aCoder encodeObject: attrs forKey: @"NSAttributes"];
}
else
{
NSUInteger i = 0;
NSUInteger pos = 0;
NSMutableArray *attrs = [NSMutableArray arrayWithCapacity: 1];
NSMutableData *info = [NSMutableData dataWithCapacity: 2];
while (pos < length)
{
[attrs addObject: [self attributesAtIndex: pos
effectiveRange: &range]];
appendUIntData(info, range.length);
appendUIntData(info, i++);
pos = NSMaxRange(range);
}
[aCoder encodeObject: [[attrs copy] autorelease]
forKey: @"NSAttributes"];
[aCoder encodeObject: [[info copy] autorelease]
forKey: @"NSAttributeInfo"];
}
}
}
else
{
NSRange r = NSMakeRange(0, 0);
unsigned index = NSMaxRange(r);
unsigned length = [self length];
NSString *string = [self string];
NSDictionary *attrs;
[aCoder encodeObject: string];
while (index < length)
{
attrs = [self attributesAtIndex: index effectiveRange: &r];
index = NSMaxRange(r);
[aCoder encodeValueOfObjCType: @encode(unsigned) at: &index];
[aCoder encodeObject: attrs];
}
}
}
- (id) initWithCoder: (NSCoder*)aDecoder
{
if ([aDecoder allowsKeyedCoding])
{
NSString *string = [aDecoder decodeObjectForKey: @"NSString"];
if (![aDecoder containsValueForKey: @"NSAttributeInfo"])
{
NSDictionary *attributes;
attributes = [aDecoder decodeObjectForKey: @"NSAttributes"];
self = [self initWithString: string attributes: attributes];
}
else
{
NSArray *attributes = [aDecoder decodeObjectForKey: @"NSAttributes"];
NSData *info = [aDecoder decodeObjectForKey: @"NSAttributeInfo"];
unsigned int pos = 0;
const unsigned char *p = [info bytes];
const unsigned char *end = p + [info length];
NSMutableAttributedString *m = [[NSMutableAttributedString alloc]
initWithString: string
attributes: nil];
while (p < end)
{
unsigned int idx;
unsigned int len;
unsigned int shift;
NSRange r;
len = shift = 0;
while (*p & 0x80)
{
len += (*p++ - 128) << shift;
shift += 7;
}
len += *p++ << shift;
idx = shift = 0;
while (*p & 0x80)
{
idx += (*p++ - 128) << shift;
shift += 7;
}
idx += *p++ << shift;
r = NSMakeRange(pos, len);
[m setAttributes: [attributes objectAtIndex: idx] range: r];
pos = NSMaxRange(r);
}
DESTROY(self);
self = [m copy];
RELEASE(m);
}
}
else
{
NSString *string = [aDecoder decodeObject];
unsigned length = [string length];
if (length == 0)
{
self = [self initWithString: string attributes: nil];
}
else
{
unsigned index;
NSDictionary *attrs;
[aDecoder decodeValueOfObjCType: @encode(unsigned) at: &index];
attrs = [aDecoder decodeObject];
if (index == length)
{
self = [self initWithString: string attributes: attrs];
}
else
{
NSRange r = NSMakeRange(0, index);
unsigned last = index;
NSMutableAttributedString *m;
m = [NSMutableAttributedString alloc];
m = [m initWithString: string attributes: nil];
[m setAttributes: attrs range: r];
while (index < length)
{
[aDecoder decodeValueOfObjCType: @encode(unsigned)
at: &index];
attrs = [aDecoder decodeObject];
r = NSMakeRange(last, index - last);
[m setAttributes: attrs range: r];
last = index;
}
DESTROY(self);
self = [m copy];
RELEASE(m);
}
}
}
return self;
}
- (id) replacementObjectForPortCoder: (NSPortCoder*)aCoder
{
if ([aCoder isByref] == NO)
return self;
return [super replacementObjectForPortCoder: aCoder];
}
//NSCopying protocol
- (id) copyWithZone: (NSZone*)zone
{
if ([self isKindOfClass: [NSMutableAttributedString class]]
|| NSShouldRetainWithZone(self, zone) == NO)
return [[GSAttributedStringClass allocWithZone: zone]
initWithAttributedString: self];
else
return RETAIN(self);
}
//NSMutableCopying protocol
- (id) mutableCopyWithZone: (NSZone*)zone
{
return [[GSMutableAttributedStringClass allocWithZone: zone]
initWithAttributedString: self];
}
//Creating an NSAttributedString
- (id) init
{
return [self initWithString: @"" attributes: nil];
}
/**
* Initialize to aString with no attributes.
*/
- (id) initWithString: (NSString*)aString
{
return [self initWithString: aString attributes: nil];
}
/**
* Initialize to copy of attributedString.
*/
- (id) initWithAttributedString: (NSAttributedString*)attributedString
{
return [self initWithString: (NSString*)attributedString attributes: nil];
}
/**
* Initialize to aString with given attributes applying over full range of
* string.
*/
- (id) initWithString: (NSString*)aString attributes: (NSDictionary*)attributes
{
//This is the designated initializer
[self subclassResponsibility: _cmd];/* Primitive method! */
return nil;
}
- (NSString*) description
{
NSRange r = NSMakeRange(0, 0);
unsigned index = NSMaxRange(r);
unsigned length = [self length];
NSString *string = [self string];
NSDictionary *attrs;
NSMutableString *desc;
desc = [NSMutableString stringWithCapacity: length];
while (index < length &&
(attrs = [self attributesAtIndex: index effectiveRange: &r]) != nil)
{
index = NSMaxRange(r);
[desc appendFormat: @"%@%@", [string substringWithRange: r], attrs];
}
return desc;
}
//Retrieving character information
/**
* Return length of the underlying string.
*/
- (NSUInteger) length
{
return [[self string] length];
}
/**
* Return the underlying string, stripped of attributes.
*/
- (NSString*) string
{
[self subclassResponsibility: _cmd];/* Primitive method! */
return nil;
}
//Retrieving attribute information
/**
* Returns attributes and values at index, and, if effectiveRange
* is non-nil, this gets filled with a range over which these attributes
* and values still hold. This may not be the maximum range, depending
* on the implementation.
*/
- (NSDictionary*) attributesAtIndex: (NSUInteger)index
effectiveRange: (NSRange*)aRange
{
[self subclassResponsibility: _cmd];/* Primitive method! */
return nil;
}
/**
* Returns attributes and values at index, and, if longestEffectiveRange
* is non-nil, this gets filled with the range over which the attribute-value
* set is the same as at index, clipped to rangeLimit.
*/
- (NSDictionary*) attributesAtIndex: (NSUInteger)index
longestEffectiveRange: (NSRange*)aRange
inRange: (NSRange)rangeLimit
{
NSDictionary *attrDictionary, *tmpDictionary;
NSRange tmpRange;
IMP getImp;
if (NSMaxRange(rangeLimit) > [self length])
{
[NSException raise: NSRangeException
format: @"RangeError in method -attributesAtIndex:longestEffectiveRange:inRange: in class NSAttributedString"];
}
getImp = [self methodForSelector: getSel];
attrDictionary = (*getImp)(self, getSel, index, aRange);
if (aRange == 0)
return attrDictionary;
while (aRange->location > rangeLimit.location)
{
//Check extend range backwards
tmpDictionary = (*getImp)(self, getSel, aRange->location-1, &tmpRange);
if ([tmpDictionary isEqualToDictionary: attrDictionary])
{
aRange->length = NSMaxRange(*aRange) - tmpRange.location;
aRange->location = tmpRange.location;
}
else
{
break;
}
}
while (NSMaxRange(*aRange) < NSMaxRange(rangeLimit))
{
//Check extend range forwards
tmpDictionary = (*getImp)(self, getSel, NSMaxRange(*aRange), &tmpRange);
if ([tmpDictionary isEqualToDictionary: attrDictionary])
{
aRange->length = NSMaxRange(tmpRange) - aRange->location;
}
else
{
break;
}
}
*aRange = NSIntersectionRange(*aRange,rangeLimit);//Clip to rangeLimit
return attrDictionary;
}
/**
* Returns value for given attribute at index, and, if effectiveRange is
* non-nil, this gets filled with a range over which this value holds. This
* may not be the maximum range, depending on the implementation.
*/
- (id) attribute: (NSString*)attributeName
atIndex: (NSUInteger)index
effectiveRange: (NSRange*)aRange
{
NSDictionary *tmpDictionary;
id attrValue;
tmpDictionary = [self attributesAtIndex: index effectiveRange: aRange];
if (attributeName == nil)
{
if (aRange != 0)
{
*aRange = NSMakeRange(0,[self length]);
/*
* If attributeName is nil, then the attribute will not exist in the
* entire text - therefore aRange of the entire text must be correct
*/
}
return nil;
}
attrValue = [tmpDictionary objectForKey: attributeName];
return attrValue;
}
/**
* Returns value for given attribute at index, and, if longestEffectiveRange
* is non-nil, this gets filled with the range over which the attribute
* applies, clipped to rangeLimit.
*/
- (id) attribute: (NSString*)attributeName
atIndex: (NSUInteger)index
longestEffectiveRange: (NSRange*)aRange
inRange: (NSRange)rangeLimit
{
NSDictionary *tmpDictionary;
id attrValue;
id tmpAttrValue;
NSRange tmpRange;
BOOL (*eImp)(id,SEL,id);
IMP getImp;
if (NSMaxRange(rangeLimit) > [self length])
{
[NSException raise: NSRangeException
format: @"RangeError in method %@ in class %@",
NSStringFromSelector(_cmd), NSStringFromClass([self class])];
}
if (attributeName == nil)
return nil;
attrValue = [self attribute: attributeName
atIndex: index
effectiveRange: aRange];
if (aRange == 0)
return attrValue;
/*
* If attrValue == nil then eImp will be zero
*/
eImp = (BOOL(*)(id,SEL,id))[attrValue methodForSelector: eqSel];
getImp = [self methodForSelector: getSel];
while (aRange->location > rangeLimit.location)
{
//Check extend range backwards
tmpDictionary = (*getImp)(self, getSel, aRange->location-1, &tmpRange);
tmpAttrValue = [tmpDictionary objectForKey: attributeName];
if (tmpAttrValue == attrValue
|| (eImp != 0 && (*eImp)(attrValue, eqSel, tmpAttrValue)))
{
aRange->length = NSMaxRange(*aRange) - tmpRange.location;
aRange->location = tmpRange.location;
}
else
{
break;
}
}
while (NSMaxRange(*aRange) < NSMaxRange(rangeLimit))
{
//Check extend range forwards
tmpDictionary = (*getImp)(self, getSel, NSMaxRange(*aRange), &tmpRange);
tmpAttrValue = [tmpDictionary objectForKey: attributeName];
if (tmpAttrValue == attrValue
|| (eImp != 0 && (*eImp)(attrValue, eqSel, tmpAttrValue)))
{
aRange->length = NSMaxRange(tmpRange) - aRange->location;
}
else
{
break;
}
}
*aRange = NSIntersectionRange(*aRange,rangeLimit);//Clip to rangeLimit
return attrValue;
}
//Comparing attributed strings
/**
* Returns whether all characters and attributes are equal between this
* string and otherString.
*/
- (BOOL) isEqualToAttributedString: (NSAttributedString*)otherString
{
NSRange ownEffectiveRange,otherEffectiveRange;
unsigned int length;
NSDictionary *ownDictionary,*otherDictionary;
BOOL result;
if (!otherString)
return NO;
if (![[otherString string] isEqual: [self string]])
return NO;
length = [otherString length];
if (length == 0)
return YES;
ownDictionary = [self attributesAtIndex: 0
effectiveRange: &ownEffectiveRange];
otherDictionary = [otherString attributesAtIndex: 0
effectiveRange: &otherEffectiveRange];
result = YES;
while (YES)
{
if (NSIntersectionRange(ownEffectiveRange, otherEffectiveRange).length > 0
&& ![ownDictionary isEqualToDictionary: otherDictionary])
{
result = NO;
break;
}
if (NSMaxRange(ownEffectiveRange) < NSMaxRange(otherEffectiveRange))
{
ownDictionary = [self attributesAtIndex: NSMaxRange(ownEffectiveRange)
effectiveRange: &ownEffectiveRange];
}
else
{
if (NSMaxRange(otherEffectiveRange) >= length)
{
break;//End of strings
}
otherDictionary = [otherString
attributesAtIndex: NSMaxRange(otherEffectiveRange)
effectiveRange: &otherEffectiveRange];
}
}
return result;
}
- (BOOL) isEqual: (id)anObject
{
if (anObject == self)
return YES;
if ([anObject isKindOfClass: NSAttributedStringClass])
return [self isEqualToAttributedString: anObject];
return NO;
}
//Extracting a substring
/**
* Returns substring with attribute information.
*/
- (NSAttributedString*) attributedSubstringFromRange: (NSRange)aRange
{
NSAttributedString *newAttrString;
NSString *newSubstring;
NSDictionary *attrs;
NSRange range;
unsigned len = [self length];
GS_RANGE_CHECK(aRange, len);
newSubstring = [[self string] substringWithRange: aRange];
attrs = [self attributesAtIndex: aRange.location effectiveRange: &range];
range = NSIntersectionRange(range, aRange);
if (NSEqualRanges(range, aRange) == YES)
{
newAttrString = [GSAttributedStringClass alloc];
newAttrString = [newAttrString initWithString: newSubstring
attributes: attrs];
}
else
{
NSMutableAttributedString *m;
NSRange rangeToSet = range;
m = [GSMutableAttributedStringClass alloc];
m = [m initWithString: newSubstring attributes: nil];
rangeToSet.location = 0;
[m setAttributes: attrs range: rangeToSet];
while (NSMaxRange(range) < NSMaxRange(aRange))
{
attrs = [self attributesAtIndex: NSMaxRange(range)
effectiveRange: &range];
rangeToSet = NSIntersectionRange(range, aRange);
rangeToSet.location -= aRange.location;
[m setAttributes: attrs range: rangeToSet];
}
newAttrString = [m copy];
RELEASE(m);
}
IF_NO_GC(AUTORELEASE(newAttrString));
return newAttrString;
}
@end //NSAttributedString
/**
* Mutable version of [NSAttributedString].
*/
@implementation NSMutableAttributedString
+ (id) allocWithZone: (NSZone*)z
{
if (self == NSMutableAttributedStringClass)
return NSAllocateObject(GSMutableAttributedStringClass, 0, z);
else
return NSAllocateObject(self, 0, z);
}
- (Class) classForCoder
{
return NSMutableAttributedStringClass;
}
- (id) initWithCoder: (NSCoder*)aDecoder
{
if ([aDecoder allowsKeyedCoding])
{
NSString *string = [aDecoder decodeObjectForKey: @"NSString"];
if (![aDecoder containsValueForKey: @"NSAttributeInfo"])
{
NSDictionary *attributes;
attributes = [aDecoder decodeObjectForKey: @"NSAttributes"];
self = [self initWithString: string attributes: attributes];
}
else
{
NSArray *attributes = [aDecoder decodeObjectForKey: @"NSAttributes"];
NSData *info = [aDecoder decodeObjectForKey: @"NSAttributeInfo"];
unsigned int pos = 0;
const unsigned char *p = [info bytes];
const unsigned char *end = p + [info length];
self = [self initWithString: string attributes: nil];
while (p < end)
{
unsigned int idx;
unsigned int len;
unsigned int shift;
NSRange r;
len = shift = 0;
while (*p & 0x80)
{
len += (*p++ - 128) << shift;
shift += 7;
}
len += *p++ << shift;
idx = shift = 0;
while (*p & 0x80)
{
idx += (*p++ - 128) << shift;
shift += 7;
}
idx += *p++ << shift;
r = NSMakeRange(pos, len);
[self setAttributes: [attributes objectAtIndex: idx] range: r];
pos = NSMaxRange(r);
}
}
}
else
{
NSString *string = [aDecoder decodeObject];
unsigned length = [string length];
if (length == 0)
{
self = [self initWithString: string attributes: nil];
}
else
{
unsigned index;
NSDictionary *attrs;
[aDecoder decodeValueOfObjCType: @encode(unsigned) at: &index];
attrs = [aDecoder decodeObject];
if (index == length)
{
self = [self initWithString: string attributes: attrs];
}
else
{
NSRange r = NSMakeRange(0, index);
unsigned last = index;
self = [self initWithString: string attributes: nil];
[self setAttributes: attrs range: r];
while (index < length)
{
[aDecoder decodeValueOfObjCType: @encode(unsigned)
at: &index];
attrs = [aDecoder decodeObject];
r = NSMakeRange(last, index - last);
[self setAttributes: attrs range: r];
last = index;
}
}
}
}
return self;
}
//Retrieving character information
/**
* Returns mutable version of the underlying string.
*/
- (NSMutableString*) mutableString
{
return [GSMutableAttributedStringTracker stringWithOwner: self];
}
//Changing characters
/**
* Removes characters and attributes applying to them.
*/
- (void) deleteCharactersInRange: (NSRange)aRange
{
[self replaceCharactersInRange: aRange withString: nil];
}
//Changing attributes
/**
* Sets attributes to apply over range, replacing any previous attributes.
*/
- (void) setAttributes: (NSDictionary*)attributes range: (NSRange)aRange
{
[self subclassResponsibility: _cmd];// Primitive method!
}
/**
* Adds attribute applying to given range.
*/
- (void) addAttribute: (NSString*)name value: (id)value range: (NSRange)aRange
{
NSRange effectiveRange;
NSDictionary *attrDict;
NSMutableDictionary *newDict;
unsigned int tmpLength;
IMP getImp;
tmpLength = [self length];
GS_RANGE_CHECK(aRange, tmpLength);
getImp = [self methodForSelector: getSel];
attrDict = (*getImp)(self, getSel, aRange.location, &effectiveRange);
if (effectiveRange.location < NSMaxRange(aRange))
{
IMP setImp;
setImp = [self methodForSelector: setSel];
[self beginEditing];
while (effectiveRange.location < NSMaxRange(aRange))
{
effectiveRange = NSIntersectionRange(aRange, effectiveRange);
newDict = (*allocDictImp)(dictionaryClass, allocDictSel,
NSDefaultMallocZone());
newDict = (*initDictImp)(newDict, initDictSel, attrDict);
(*setDictImp)(newDict, setDictSel, value, name);
(*setImp)(self, setSel, newDict, effectiveRange);
IF_NO_GC((*relDictImp)(newDict, relDictSel));
if (NSMaxRange(effectiveRange) >= NSMaxRange(aRange))
{
effectiveRange.location = NSMaxRange(aRange);// stop the loop...
}
else if (NSMaxRange(effectiveRange) < tmpLength)
{
attrDict = (*getImp)(self, getSel, NSMaxRange(effectiveRange),
&effectiveRange);
}
}
[self endEditing];
}
}
/**
* Add attributes to apply over given range.
*/
- (void) addAttributes: (NSDictionary*)attributes range: (NSRange)aRange
{
NSRange effectiveRange;
NSDictionary *attrDict;
NSMutableDictionary *newDict;
unsigned int tmpLength;
IMP getImp;
if (!attributes)
{
[NSException raise: NSInvalidArgumentException
format: @"attributes is nil in method -addAttributes:range: "
@"in class NSMutableAtrributedString"];
}
tmpLength = [self length];
if (NSMaxRange(aRange) > tmpLength)
{
[NSException raise: NSRangeException
format: @"RangeError in method -addAttribute:value:range: "
@"in class NSMutableAttributedString"];
}
getImp = [self methodForSelector: getSel];
attrDict = (*getImp)(self, getSel, aRange.location, &effectiveRange);
if (effectiveRange.location < NSMaxRange(aRange))
{
IMP setImp;
setImp = [self methodForSelector: setSel];
[self beginEditing];
while (effectiveRange.location < NSMaxRange(aRange))
{
effectiveRange = NSIntersectionRange(aRange,effectiveRange);
newDict = (*allocDictImp)(dictionaryClass, allocDictSel,
NSDefaultMallocZone());
newDict = (*initDictImp)(newDict, initDictSel, attrDict);
(*addDictImp)(newDict, addDictSel, attributes);
(*setImp)(self, setSel, newDict, effectiveRange);
IF_NO_GC((*relDictImp)(newDict, relDictSel));
if (NSMaxRange(effectiveRange) >= NSMaxRange(aRange))
{
effectiveRange.location = NSMaxRange(aRange);// stop the loop...
}
else if (NSMaxRange(effectiveRange) < tmpLength)
{
attrDict = (*getImp)(self, getSel, NSMaxRange(effectiveRange),
&effectiveRange);
}
}
[self endEditing];
}
}
/**
* Removes given attribute from aRange.
*/
- (void) removeAttribute: (NSString*)name range: (NSRange)aRange
{
NSRange effectiveRange;
NSDictionary *attrDict;
NSMutableDictionary *newDict;
unsigned int tmpLength;
IMP getImp;
tmpLength = [self length];
GS_RANGE_CHECK(aRange, tmpLength);
getImp = [self methodForSelector: getSel];
attrDict = (*getImp)(self, getSel, aRange.location, &effectiveRange);
if (effectiveRange.location < NSMaxRange(aRange))
{
IMP setImp;
setImp = [self methodForSelector: setSel];
[self beginEditing];
while (effectiveRange.location < NSMaxRange(aRange))
{
effectiveRange = NSIntersectionRange(aRange,effectiveRange);
newDict = (*allocDictImp)(dictionaryClass, allocDictSel,
NSDefaultMallocZone());
newDict = (*initDictImp)(newDict, initDictSel, attrDict);
(*remDictImp)(newDict, remDictSel, name);
(*setImp)(self, setSel, newDict, effectiveRange);
IF_NO_GC((*relDictImp)(newDict, relDictSel));
if (NSMaxRange(effectiveRange) >= NSMaxRange(aRange))
{
effectiveRange.location = NSMaxRange(aRange);// stop the loop...
}
else if (NSMaxRange(effectiveRange) < tmpLength)
{
attrDict = (*getImp)(self, getSel, NSMaxRange(effectiveRange),
&effectiveRange);
}
}
[self endEditing];
}
}
//Changing characters and attributes
/**
* Appends attributed string to end of this one, preserving attributes.
*/
- (void) appendAttributedString: (NSAttributedString*)attributedString
{
[self replaceCharactersInRange: NSMakeRange([self length],0)
withAttributedString: attributedString];
}
/**
* Inserts attributed string within this one, preserving attributes.
*/
- (void) insertAttributedString: (NSAttributedString*)attributedString
atIndex: (NSUInteger)index
{
[self replaceCharactersInRange: NSMakeRange(index,0)
withAttributedString: attributedString];
}
/**
* Replaces substring and attributes.
*/
- (void) replaceCharactersInRange: (NSRange)aRange
withAttributedString: (NSAttributedString*)attributedString
{
NSDictionary *attrDict;
NSString *tmpStr;
unsigned max;
if (attributedString == nil)
{
[self replaceCharactersInRange: aRange withString: nil];
return;
}
[self beginEditing];
tmpStr = [attributedString string];
[self replaceCharactersInRange: aRange withString: tmpStr];
max = [tmpStr length];
if (max > 0)
{
unsigned loc = 0;
NSRange effectiveRange = NSMakeRange(0, loc);
NSRange clipRange = NSMakeRange(0, max);
IMP getImp;
IMP setImp;
getImp = [attributedString methodForSelector: getSel];
setImp = [self methodForSelector: setSel];
while (loc < max)
{
NSRange ownRange;
attrDict = (*getImp)(attributedString, getSel, loc, &effectiveRange);
ownRange = NSIntersectionRange(clipRange, effectiveRange);
ownRange.location += aRange.location;
(*setImp)(self, setSel, attrDict, ownRange);
loc = NSMaxRange(effectiveRange);
}
}
[self endEditing];
}
/** <override-subclass />
* Replaces substring; replacement is granted attributes equal to those of
* the first character of the portion replaced.
*/
- (void) replaceCharactersInRange: (NSRange)aRange
withString: (NSString*)aString
{
[self subclassResponsibility: _cmd];// Primitive method!
}
/**
* Replaces entire contents (so this object can be reused).
*/
- (void) setAttributedString: (NSAttributedString*)attributedString
{
[self replaceCharactersInRange: NSMakeRange(0,[self length])
withAttributedString: attributedString];
}
/** <override-dummy />
* Call before executing a collection of changes, for optimization.
*/
- (void) beginEditing
{
}
/** <override-dummy />
* Call after executing a collection of changes, for optimization.
*/
- (void) endEditing
{
}
@end //NSMutableAttributedString
/*
* The GSMutableAttributedStringTracker class is a concrete subclass of
* NSMutableString which keeps it's owner informed of any changes made
* to it.
*/
@implementation GSMutableAttributedStringTracker
+ (NSMutableString*) stringWithOwner: (NSMutableAttributedString*)as
{
GSMutableAttributedStringTracker *str;
NSZone *z = NSDefaultMallocZone();
str = (GSMutableAttributedStringTracker*) NSAllocateObject(self, 0, z);
str->_owner = RETAIN(as);
return AUTORELEASE(str);
}
- (void) dealloc
{
RELEASE(_owner);
[super dealloc];
}
- (NSUInteger) length
{
return [[_owner string] length];
}
- (unichar) characterAtIndex: (NSUInteger)index
{
return [[_owner string] characterAtIndex: index];
}
- (void)getCharacters: (unichar*)buffer
{
[[_owner string] getCharacters: buffer];
}
- (void)getCharacters: (unichar*)buffer range: (NSRange)aRange
{
[[_owner string] getCharacters: buffer range: aRange];
}
- (const char*) cString
{
return [[_owner string] cString];
}
- (NSUInteger) cStringLength
{
return [[_owner string] cStringLength];
}
- (NSStringEncoding) fastestEncoding
{
return [[_owner string] fastestEncoding];
}
- (NSStringEncoding) smallestEncoding
{
return [[_owner string] smallestEncoding];
}
- (int) _baseLength
{
return [[_owner string] _baseLength];
}
- (void) encodeWithCoder: (NSCoder*)aCoder
{
[[_owner string] encodeWithCoder: aCoder];
}
- (Class) classForCoder
{
return [[_owner string] classForCoder];
}
- (void) replaceCharactersInRange: (NSRange)aRange
withString: (NSString*)aString
{
[_owner replaceCharactersInRange: aRange withString: aString];
}
@end
|