WebKitNSStringExtras.mm 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /*
  2. * Copyright (C) 2005, 2007 Apple Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
  14. * its contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
  18. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
  21. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  24. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #import "WebKitNSStringExtras.h"
  29. #import <WebCore/Font.h>
  30. #import <WebCore/FontCache.h>
  31. #import <WebCore/GraphicsContext.h>
  32. #import <WebCore/TextRun.h>
  33. #import <WebCore/WebCoreNSStringExtras.h>
  34. #import <WebKit/WebNSFileManagerExtras.h>
  35. #import <WebKit/WebNSObjectExtras.h>
  36. #import <unicode/uchar.h>
  37. #import <sys/param.h>
  38. NSString *WebKitLocalCacheDefaultsKey = @"WebKitLocalCache";
  39. static inline CGFloat webkit_CGCeiling(CGFloat value)
  40. {
  41. if (sizeof(value) == sizeof(float))
  42. return ceilf(value);
  43. return ceil(value);
  44. }
  45. using namespace WebCore;
  46. @implementation NSString (WebKitExtras)
  47. static BOOL canUseFastRenderer(const UniChar *buffer, unsigned length)
  48. {
  49. unsigned i;
  50. for (i = 0; i < length; i++) {
  51. UCharDirection direction = u_charDirection(buffer[i]);
  52. if (direction == U_RIGHT_TO_LEFT || direction > U_OTHER_NEUTRAL)
  53. return NO;
  54. }
  55. return YES;
  56. }
  57. - (void)_web_drawAtPoint:(NSPoint)point font:(NSFont *)font textColor:(NSColor *)textColor
  58. {
  59. [self _web_drawAtPoint:point font:font textColor:textColor allowingFontSmoothing:YES];
  60. }
  61. - (void)_web_drawAtPoint:(NSPoint)point font:(NSFont *)font textColor:(NSColor *)textColor allowingFontSmoothing:(BOOL)fontSmoothingIsAllowed
  62. {
  63. unsigned length = [self length];
  64. Vector<UniChar, 2048> buffer(length);
  65. [self getCharacters:buffer.data()];
  66. if (canUseFastRenderer(buffer.data(), length)) {
  67. // The following is a half-assed attempt to match AppKit's rounding rules for drawAtPoint.
  68. // It's probably incorrect for high DPI.
  69. // If you change this, be sure to test all the text drawn this way in Safari, including
  70. // the status bar, bookmarks bar, tab bar, and activity window.
  71. point.y = webkit_CGCeiling(point.y);
  72. NSGraphicsContext *nsContext = [NSGraphicsContext currentContext];
  73. CGContextRef cgContext = static_cast<CGContextRef>([nsContext graphicsPort]);
  74. GraphicsContext graphicsContext(cgContext);
  75. // Safari doesn't flip the NSGraphicsContext before calling WebKit, yet WebCore requires a flipped graphics context.
  76. BOOL flipped = [nsContext isFlipped];
  77. if (!flipped)
  78. CGContextScaleCTM(cgContext, 1, -1);
  79. FontCachePurgePreventer fontCachePurgePreventer;
  80. Font webCoreFont(FontPlatformData(font, [font pointSize]), ![nsContext isDrawingToScreen], fontSmoothingIsAllowed ? AutoSmoothing : Antialiased);
  81. TextRun run(buffer.data(), length);
  82. run.disableRoundingHacks();
  83. CGFloat red;
  84. CGFloat green;
  85. CGFloat blue;
  86. CGFloat alpha;
  87. [[textColor colorUsingColorSpaceName:NSDeviceRGBColorSpace] getRed:&red green:&green blue:&blue alpha:&alpha];
  88. graphicsContext.setFillColor(makeRGBA(red * 255, green * 255, blue * 255, alpha * 255), ColorSpaceDeviceRGB);
  89. webCoreFont.drawText(&graphicsContext, run, FloatPoint(point.x, (flipped ? point.y : (-1 * point.y))));
  90. if (!flipped)
  91. CGContextScaleCTM(cgContext, 1, -1);
  92. } else {
  93. // The given point is on the baseline.
  94. if ([[NSView focusView] isFlipped])
  95. point.y -= [font ascender];
  96. else
  97. point.y += [font descender];
  98. [self drawAtPoint:point withAttributes:[NSDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName, textColor, NSForegroundColorAttributeName, nil]];
  99. }
  100. }
  101. - (void)_web_drawDoubledAtPoint:(NSPoint)textPoint
  102. withTopColor:(NSColor *)topColor
  103. bottomColor:(NSColor *)bottomColor
  104. font:(NSFont *)font
  105. {
  106. // turn off font smoothing so translucent text draws correctly (Radar 3118455)
  107. [self _web_drawAtPoint:textPoint font:font textColor:bottomColor allowingFontSmoothing:NO];
  108. textPoint.y += 1;
  109. [self _web_drawAtPoint:textPoint font:font textColor:topColor allowingFontSmoothing:NO];
  110. }
  111. - (float)_web_widthWithFont:(NSFont *)font
  112. {
  113. unsigned length = [self length];
  114. Vector<UniChar, 2048> buffer(length);
  115. [self getCharacters:buffer.data()];
  116. if (canUseFastRenderer(buffer.data(), length)) {
  117. FontCachePurgePreventer fontCachePurgePreventer;
  118. Font webCoreFont(FontPlatformData(font, [font pointSize]), ![[NSGraphicsContext currentContext] isDrawingToScreen]);
  119. TextRun run(buffer.data(), length);
  120. run.disableRoundingHacks();
  121. return webCoreFont.width(run);
  122. }
  123. return [self sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName, nil]].width;
  124. }
  125. - (NSString *)_web_stringByAbbreviatingWithTildeInPath
  126. {
  127. NSString *resolvedHomeDirectory = [NSHomeDirectory() stringByResolvingSymlinksInPath];
  128. NSString *path;
  129. if ([self hasPrefix:resolvedHomeDirectory]) {
  130. NSString *relativePath = [self substringFromIndex:[resolvedHomeDirectory length]];
  131. path = [NSHomeDirectory() stringByAppendingPathComponent:relativePath];
  132. } else {
  133. path = self;
  134. }
  135. return [path stringByAbbreviatingWithTildeInPath];
  136. }
  137. - (NSString *)_web_stringByStrippingReturnCharacters
  138. {
  139. NSMutableString *newString = [[self mutableCopy] autorelease];
  140. [newString replaceOccurrencesOfString:@"\r" withString:@"" options:NSLiteralSearch range:NSMakeRange(0, [newString length])];
  141. [newString replaceOccurrencesOfString:@"\n" withString:@"" options:NSLiteralSearch range:NSMakeRange(0, [newString length])];
  142. return newString;
  143. }
  144. + (NSStringEncoding)_web_encodingForResource:(Handle)resource
  145. {
  146. return CFStringConvertEncodingToNSStringEncoding(stringEncodingForResource(resource));
  147. }
  148. - (BOOL)_webkit_isCaseInsensitiveEqualToString:(NSString *)string
  149. {
  150. return stringIsCaseInsensitiveEqualToString(self, string);
  151. }
  152. -(BOOL)_webkit_hasCaseInsensitivePrefix:(NSString *)prefix
  153. {
  154. return hasCaseInsensitivePrefix(self, prefix);
  155. }
  156. -(BOOL)_webkit_hasCaseInsensitiveSuffix:(NSString *)suffix
  157. {
  158. return hasCaseInsensitiveSuffix(self, suffix);
  159. }
  160. -(BOOL)_webkit_hasCaseInsensitiveSubstring:(NSString *)substring
  161. {
  162. return hasCaseInsensitiveSubstring(self, substring);
  163. }
  164. -(NSString *)_webkit_filenameByFixingIllegalCharacters
  165. {
  166. return filenameByFixingIllegalCharacters(self);
  167. }
  168. -(NSString *)_webkit_stringByTrimmingWhitespace
  169. {
  170. NSMutableString *trimmed = [[self mutableCopy] autorelease];
  171. CFStringTrimWhitespace((CFMutableStringRef)trimmed);
  172. return trimmed;
  173. }
  174. - (NSString *)_webkit_stringByCollapsingNonPrintingCharacters
  175. {
  176. NSMutableString *result = [NSMutableString string];
  177. static NSCharacterSet *charactersToTurnIntoSpaces = nil;
  178. static NSCharacterSet *charactersToNotTurnIntoSpaces = nil;
  179. if (charactersToTurnIntoSpaces == nil) {
  180. NSMutableCharacterSet *set = [[NSMutableCharacterSet alloc] init];
  181. [set addCharactersInRange:NSMakeRange(0x00, 0x21)];
  182. [set addCharactersInRange:NSMakeRange(0x7F, 0x01)];
  183. charactersToTurnIntoSpaces = [set copy];
  184. [set release];
  185. charactersToNotTurnIntoSpaces = [[charactersToTurnIntoSpaces invertedSet] retain];
  186. }
  187. unsigned length = [self length];
  188. unsigned position = 0;
  189. while (position != length) {
  190. NSRange nonSpace = [self rangeOfCharacterFromSet:charactersToNotTurnIntoSpaces
  191. options:0 range:NSMakeRange(position, length - position)];
  192. if (nonSpace.location == NSNotFound) {
  193. break;
  194. }
  195. NSRange space = [self rangeOfCharacterFromSet:charactersToTurnIntoSpaces
  196. options:0 range:NSMakeRange(nonSpace.location, length - nonSpace.location)];
  197. if (space.location == NSNotFound) {
  198. space.location = length;
  199. }
  200. if (space.location > nonSpace.location) {
  201. if (position != 0) {
  202. [result appendString:@" "];
  203. }
  204. [result appendString:[self substringWithRange:
  205. NSMakeRange(nonSpace.location, space.location - nonSpace.location)]];
  206. }
  207. position = space.location;
  208. }
  209. return result;
  210. }
  211. - (NSString *)_webkit_stringByCollapsingWhitespaceCharacters
  212. {
  213. NSMutableString *result = [[NSMutableString alloc] initWithCapacity:[self length]];
  214. NSCharacterSet *spaces = [NSCharacterSet whitespaceAndNewlineCharacterSet];
  215. static NSCharacterSet *notSpaces = nil;
  216. if (notSpaces == nil)
  217. notSpaces = [[spaces invertedSet] retain];
  218. unsigned length = [self length];
  219. unsigned position = 0;
  220. while (position != length) {
  221. NSRange nonSpace = [self rangeOfCharacterFromSet:notSpaces options:0 range:NSMakeRange(position, length - position)];
  222. if (nonSpace.location == NSNotFound)
  223. break;
  224. NSRange space = [self rangeOfCharacterFromSet:spaces options:0 range:NSMakeRange(nonSpace.location, length - nonSpace.location)];
  225. if (space.location == NSNotFound)
  226. space.location = length;
  227. if (space.location > nonSpace.location) {
  228. if (position != 0)
  229. [result appendString:@" "];
  230. [result appendString:[self substringWithRange:NSMakeRange(nonSpace.location, space.location - nonSpace.location)]];
  231. }
  232. position = space.location;
  233. }
  234. return [result autorelease];
  235. }
  236. -(NSString *)_webkit_fixedCarbonPOSIXPath
  237. {
  238. NSFileManager *fileManager = [NSFileManager defaultManager];
  239. if ([fileManager fileExistsAtPath:self]) {
  240. // Files exists, no need to fix.
  241. return self;
  242. }
  243. NSMutableArray *pathComponents = [[[self pathComponents] mutableCopy] autorelease];
  244. NSString *volumeName = [pathComponents objectAtIndex:1];
  245. if ([volumeName isEqualToString:@"Volumes"]) {
  246. // Path starts with "/Volumes", so the volume name is the next path component.
  247. volumeName = [pathComponents objectAtIndex:2];
  248. // Remove "Volumes" from the path because it may incorrectly be part of the path (3163647).
  249. // We'll add it back if we have to.
  250. [pathComponents removeObjectAtIndex:1];
  251. }
  252. if (!volumeName) {
  253. // Should only happen if self == "/", so this shouldn't happen because that always exists.
  254. return self;
  255. }
  256. if ([[fileManager _webkit_startupVolumeName] isEqualToString:volumeName]) {
  257. // Startup volume name is included in path, remove it.
  258. [pathComponents removeObjectAtIndex:1];
  259. } else if ([[fileManager contentsOfDirectoryAtPath:@"/Volumes" error:NULL] containsObject:volumeName]) {
  260. // Path starts with other volume name, prepend "/Volumes".
  261. [pathComponents insertObject:@"Volumes" atIndex:1];
  262. } else
  263. // It's valid.
  264. return self;
  265. NSString *path = [NSString pathWithComponents:pathComponents];
  266. if (![fileManager fileExistsAtPath:path])
  267. // File at canonicalized path doesn't exist, return original.
  268. return self;
  269. return path;
  270. }
  271. + (NSString *)_webkit_localCacheDirectoryWithBundleIdentifier:(NSString*)bundleIdentifier
  272. {
  273. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  274. NSString *cacheDir = [defaults objectForKey:WebKitLocalCacheDefaultsKey];
  275. if (!cacheDir || ![cacheDir isKindOfClass:[NSString class]]) {
  276. char cacheDirectory[MAXPATHLEN];
  277. size_t cacheDirectoryLen = confstr(_CS_DARWIN_USER_CACHE_DIR, cacheDirectory, MAXPATHLEN);
  278. if (cacheDirectoryLen)
  279. cacheDir = [[NSFileManager defaultManager] stringWithFileSystemRepresentation:cacheDirectory length:cacheDirectoryLen - 1];
  280. }
  281. return [cacheDir stringByAppendingPathComponent:bundleIdentifier];
  282. }
  283. @end