WebCore:
authoraroben <aroben@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 21 Feb 2007 05:03:02 +0000 (05:03 +0000)
committeraroben <aroben@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 21 Feb 2007 05:03:02 +0000 (05:03 +0000)
        Reviewed by Darin and Anders.

        Fix <rdar://problem/4736215> Make WebCoreStringTruncator use WebCore types

        * WebCore.exp: Updated symbols.
        * WebCore.xcodeproj/project.pbxproj: Updated file names.
        * bridge/mac/WebCoreStringTruncator.h: Removed.
        * bridge/mac/WebCoreStringTruncator.mm: Removed.
        * platform/StringTruncator.cpp: Added. Moved from bridge/mac/WebCoreStringTruncator.mm.
        (WebCore::textBreakAtOrPreceding): Added.
        (WebCore::boundedTextBreakFollowing): Added.
        (WebCore::centerTruncateToBuffer):
        (WebCore::rightTruncateToBuffer):
        (WebCore::stringWidth): Now takes a const Font& instead of a Font*.
        (WebCore::truncateString):
        (WebCore::StringTruncator::centerTruncate):
        (WebCore::StringTruncator::rightTruncate):
        (WebCore::StringTruncator::width):
        * platform/StringTruncator.h: Added.
        * platform/TextBreakIterator.h: Added isTextBreak declaration.
        * platform/TextBreakIteratorICU.cpp:
        (WebCore::isTextBreak): Implemented.
        * platform/mac/FileChooserMac.mm:
        (WebCore::FileChooser::basenameForWidth): Updated for changes to WebCoreStringTruncator.
        * platform/qt/TextBreakIteratorQt.cpp:
        (WebCore::isTextBreak): Stubbed out.

WebKit:

        Reviewed by Darin and Anders.

        Update WebKit for WebCore fix for <rdar://problem/4736215> Make
        WebCoreStringTruncator use WebCore types.

        * Misc/WebStringTruncator.m:
        (defaultMenuFont): Moved from WebCoreStringTruncator.mm.
        (core): Added.
        (+[WebStringTruncator centerTruncateString:toWidth:]):
        (+[WebStringTruncator centerTruncateString:toWidth:withFont:]):
        (+[WebStringTruncator rightTruncateString:toWidth:withFont:]):
        (+[WebStringTruncator widthOfString:font:]):
        * WebKit.xcodeproj/project.pbxproj: Changed WebStringTruncator to ObjC++.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@19762 268f45cc-cd09-0410-ab3c-d52691b4dbfc

13 files changed:
WebCore/ChangeLog
WebCore/WebCore.exp
WebCore/WebCore.xcodeproj/project.pbxproj
WebCore/bridge/mac/WebCoreStringTruncator.mm [deleted file]
WebCore/platform/StringTruncator.cpp [new file with mode: 0644]
WebCore/platform/StringTruncator.h [moved from WebCore/bridge/mac/WebCoreStringTruncator.h with 74% similarity]
WebCore/platform/TextBreakIterator.h
WebCore/platform/TextBreakIteratorICU.cpp
WebCore/platform/mac/FileChooserMac.mm
WebCore/platform/qt/TextBreakIteratorQt.cpp
WebKit/ChangeLog
WebKit/Misc/WebStringTruncator.m
WebKit/WebKit.xcodeproj/project.pbxproj

index 927af38d7cc74041d5d52a664cb195c07544f549..a1a8bfe4723d2bb7d676c0ea1957d6ef6ec46619 100644 (file)
@@ -1,3 +1,32 @@
+2007-02-20  Adam Roben  <aroben@apple.com>
+
+        Reviewed by Darin and Anders.
+
+        Fix <rdar://problem/4736215> Make WebCoreStringTruncator use WebCore types
+
+        * WebCore.exp: Updated symbols.
+        * WebCore.xcodeproj/project.pbxproj: Updated file names.
+        * bridge/mac/WebCoreStringTruncator.h: Removed.
+        * bridge/mac/WebCoreStringTruncator.mm: Removed.
+        * platform/StringTruncator.cpp: Added. Moved from bridge/mac/WebCoreStringTruncator.mm.
+        (WebCore::textBreakAtOrPreceding): Added.
+        (WebCore::boundedTextBreakFollowing): Added.
+        (WebCore::centerTruncateToBuffer):
+        (WebCore::rightTruncateToBuffer):
+        (WebCore::stringWidth): Now takes a const Font& instead of a Font*.
+        (WebCore::truncateString):
+        (WebCore::StringTruncator::centerTruncate):
+        (WebCore::StringTruncator::rightTruncate):
+        (WebCore::StringTruncator::width):
+        * platform/StringTruncator.h: Added.
+        * platform/TextBreakIterator.h: Added isTextBreak declaration.
+        * platform/TextBreakIteratorICU.cpp:
+        (WebCore::isTextBreak): Implemented.
+        * platform/mac/FileChooserMac.mm:
+        (WebCore::FileChooser::basenameForWidth): Updated for changes to WebCoreStringTruncator.
+        * platform/qt/TextBreakIteratorQt.cpp:
+        (WebCore::isTextBreak): Stubbed out.
+
 2007-02-20  Justin Garcia  <justin.garcia@apple.com>
 
         Reviewed by harrison
index 61f2472a97abc4d3ebf3305932a9ca6a31c8e688..b19e218eb7d4a61e5d2d1a24a4fd0e4a7caec497 100644 (file)
 .objc_class_name_WebCoreJavaScript
 .objc_class_name_WebCoreKeyGenerator
 .objc_class_name_WebCoreScriptDebugger
-.objc_class_name_WebCoreStringTruncator
 .objc_class_name_WebCoreViewFactory
 .objc_class_name_WebDashboardRegion
 .objc_class_name_WebScriptObject
@@ -279,6 +278,9 @@ __ZN7WebCore15ContextMenuItem26releasePlatformDescriptionEv
 __ZN7WebCore15FocusController12advanceFocusEPNS_13KeyboardEventE
 __ZN7WebCore15FocusController15setFocusedFrameEN3WTF10PassRefPtrINS_5FrameEEE
 __ZN7WebCore15FocusController18focusedOrMainFrameEv
+__ZN7WebCore15StringTruncator13rightTruncateERKNS_6StringEfRKNS_4FontE
+__ZN7WebCore15StringTruncator14centerTruncateERKNS_6StringEfRKNS_4FontE
+__ZN7WebCore15StringTruncator5widthERKNS_6StringERKNS_4FontE
 __ZN7WebCore16DeprecatedString6appendENS_14DeprecatedCharE
 __ZN7WebCore16DeprecatedString6appendERKS0_
 __ZN7WebCore16DeprecatedString6appendEc
@@ -310,6 +312,10 @@ __ZN7WebCore24notifyHistoryItemChangedE
 __ZN7WebCore26NetscapePlugInStreamLoader6createEPNS_5FrameEP11objc_object
 __ZN7WebCore36InitializeLoggingChannelsIfNecessaryEv
 __ZN7WebCore3macERKNS_23AuthenticationChallengeE
+__ZN7WebCore4FontC1ERKNS_16FontPlatformDataEb
+__ZN7WebCore4FontC1Ev
+__ZN7WebCore4FontD1Ev
+__ZN7WebCore4FontaSERKS0_
 __ZN7WebCore4KURLC1EP5NSURL
 __ZN7WebCore4KURLC1ERKNS_16DeprecatedStringE
 __ZN7WebCore4KURLC1Ev
index 7f0c849ea120fe0b2602e45ef7e2a9dc3e6a6a30..139b99bf46c2439475b01264b7504a256b0fbac0 100644 (file)
@@ -33,6 +33,8 @@
                066C772D0AB603D200238CC4 /* FileChooserMac.mm in Sources */ = {isa = PBXBuildFile; fileRef = 066C772C0AB603D200238CC4 /* FileChooserMac.mm */; };
                066C77300AB603FD00238CC4 /* RenderFileUploadControl.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 066C772E0AB603FD00238CC4 /* RenderFileUploadControl.cpp */; };
                066C77310AB603FD00238CC4 /* RenderFileUploadControl.h in Headers */ = {isa = PBXBuildFile; fileRef = 066C772F0AB603FD00238CC4 /* RenderFileUploadControl.h */; };
+               06A6A73C0B8BA44800DF1703 /* StringTruncator.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 06A6A73A0B8BA44800DF1703 /* StringTruncator.cpp */; };
+               06A6A73D0B8BA44800DF1703 /* StringTruncator.h in Headers */ = {isa = PBXBuildFile; fileRef = 06A6A73B0B8BA44800DF1703 /* StringTruncator.h */; settings = {ATTRIBUTES = (Private, ); }; };
                06E81ED70AB5D5E900C87837 /* LocalCurrentGraphicsContext.h in Headers */ = {isa = PBXBuildFile; fileRef = 06E81ED60AB5D5E900C87837 /* LocalCurrentGraphicsContext.h */; };
                06E81EEC0AB5DA9700C87837 /* LocalCurrentGraphicsContext.mm in Sources */ = {isa = PBXBuildFile; fileRef = 06E81EEB0AB5DA9700C87837 /* LocalCurrentGraphicsContext.mm */; };
                1402645E0AFDC19B005919E2 /* LoggingMac.mm in Sources */ = {isa = PBXBuildFile; fileRef = 1402645D0AFDC19B005919E2 /* LoggingMac.mm */; };
                658436860AE01B7400E53753 /* FrameLoadRequest.h in Headers */ = {isa = PBXBuildFile; fileRef = 658436850AE01B7400E53753 /* FrameLoadRequest.h */; settings = {ATTRIBUTES = (Private, ); }; };
                658821660AF4CDF700F01D1F /* ResourceResponse.h in Headers */ = {isa = PBXBuildFile; fileRef = 658821650AF4CDF700F01D1F /* ResourceResponse.h */; settings = {ATTRIBUTES = (Private, ); }; };
                6588216C0AF4CE1200F01D1F /* ResourceResponseMac.mm in Sources */ = {isa = PBXBuildFile; fileRef = 658821690AF4CE1200F01D1F /* ResourceResponseMac.mm */; };
-               65901A4409FC6039005BD752 /* WebCoreStringTruncator.h in Headers */ = {isa = PBXBuildFile; fileRef = 65901A4209FC6039005BD752 /* WebCoreStringTruncator.h */; settings = {ATTRIBUTES = (Private, ); }; };
-               65901A4509FC6039005BD752 /* WebCoreStringTruncator.mm in Sources */ = {isa = PBXBuildFile; fileRef = 65901A4309FC6039005BD752 /* WebCoreStringTruncator.mm */; };
                6593923709AE4346002C531F /* KURL.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 6593923509AE4346002C531F /* KURL.cpp */; };
                6593923809AE4346002C531F /* KURL.h in Headers */ = {isa = PBXBuildFile; fileRef = 6593923609AE4346002C531F /* KURL.h */; settings = {ATTRIBUTES = (Private, ); }; };
                6593923A09AE435C002C531F /* KURLMac.mm in Sources */ = {isa = PBXBuildFile; fileRef = 6593923909AE435C002C531F /* KURLMac.mm */; };
                066C772C0AB603D200238CC4 /* FileChooserMac.mm */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.objcpp; path = FileChooserMac.mm; sourceTree = "<group>"; };
                066C772E0AB603FD00238CC4 /* RenderFileUploadControl.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; path = RenderFileUploadControl.cpp; sourceTree = "<group>"; };
                066C772F0AB603FD00238CC4 /* RenderFileUploadControl.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = RenderFileUploadControl.h; sourceTree = "<group>"; };
+               06A6A73A0B8BA44800DF1703 /* StringTruncator.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; path = StringTruncator.cpp; sourceTree = "<group>"; };
+               06A6A73B0B8BA44800DF1703 /* StringTruncator.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = StringTruncator.h; sourceTree = "<group>"; };
                06E81ED60AB5D5E900C87837 /* LocalCurrentGraphicsContext.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LocalCurrentGraphicsContext.h; sourceTree = "<group>"; };
                06E81EEB0AB5DA9700C87837 /* LocalCurrentGraphicsContext.mm */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.objcpp; path = LocalCurrentGraphicsContext.mm; sourceTree = "<group>"; };
                1402645D0AFDC19B005919E2 /* LoggingMac.mm */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.objcpp; path = LoggingMac.mm; sourceTree = "<group>"; };
                658436850AE01B7400E53753 /* FrameLoadRequest.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = FrameLoadRequest.h; sourceTree = "<group>"; };
                658821650AF4CDF700F01D1F /* ResourceResponse.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = ResourceResponse.h; sourceTree = "<group>"; };
                658821690AF4CE1200F01D1F /* ResourceResponseMac.mm */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.objcpp; path = ResourceResponseMac.mm; sourceTree = "<group>"; };
-               65901A4209FC6039005BD752 /* WebCoreStringTruncator.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = WebCoreStringTruncator.h; sourceTree = "<group>"; };
-               65901A4309FC6039005BD752 /* WebCoreStringTruncator.mm */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.objcpp; path = WebCoreStringTruncator.mm; sourceTree = "<group>"; };
                6593923509AE4346002C531F /* KURL.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; path = KURL.cpp; sourceTree = "<group>"; };
                6593923609AE4346002C531F /* KURL.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = KURL.h; sourceTree = "<group>"; };
                6593923909AE435C002C531F /* KURLMac.mm */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.objcpp; path = KURLMac.mm; sourceTree = "<group>"; };
                                F5B2A52C02E22573018635CB /* WebCoreJavaScript.mm */,
                                7E6FEED60898582300C44C3F /* WebCoreScriptDebugger.h */,
                                7E6FEED70898582300C44C3F /* WebCoreScriptDebugger.mm */,
-                               65901A4209FC6039005BD752 /* WebCoreStringTruncator.h */,
-                               65901A4309FC6039005BD752 /* WebCoreStringTruncator.mm */,
                        );
                        path = mac;
                        sourceTree = "<group>";
                                93126F6009D7A736008D9626 /* StringHash.h */,
                                93CD4FDC0995F9EA007ECC97 /* StringImpl.cpp */,
                                93CD4FDD0995F9EA007ECC97 /* StringImpl.h */,
+                               06A6A73A0B8BA44800DF1703 /* StringTruncator.cpp */,
+                               06A6A73B0B8BA44800DF1703 /* StringTruncator.h */,
                                93E62D990985F41600E1B5E3 /* SystemTime.h */,
                                9352071809BD3BA500F2038D /* TextBoundaries.h */,
                                BC18C5CE0B2A886F0018461D /* TextBreakIterator.h */,
                                14115B7309F84CD600CA4FC1 /* JSNodeFilter.h in Headers */,
                                14E8378E09F85D4F00B85AE4 /* JSEvent.h in Headers */,
                                93EB169709F880C00091F8FF /* WebCoreSystemInterface.h in Headers */,
-                               65901A4409FC6039005BD752 /* WebCoreStringTruncator.h in Headers */,
                                BC066F6F09FEB2FA00C589A7 /* WebCoreTextRenderer.h in Headers */,
                                14DC0D3809FED073007B0235 /* JSNode.h in Headers */,
                                14D0C82509FF0EF5006B36D9 /* kjs_domnode.h in Headers */,
                                DDE63ED50B7D45A800226998 /* DOMTextEvent.h in Headers */,
                                37919C240B7D188600A56998 /* PositionIterator.h in Headers */,
                                DD05FE0D0B8BA3C6009ACDFE /* WebCoreObjCExtras.h in Headers */,
+                               06A6A73D0B8BA44800DF1703 /* StringTruncator.h in Headers */,
                        );
                        runOnlyForDeploymentPostprocessing = 0;
                };
                                14115B7209F84CD600CA4FC1 /* JSNodeFilter.cpp in Sources */,
                                14E8378409F85D1C00B85AE4 /* JSEvent.cpp in Sources */,
                                93EB169509F880B00091F8FF /* WebCoreSystemInterface.mm in Sources */,
-                               65901A4509FC6039005BD752 /* WebCoreStringTruncator.mm in Sources */,
                                BCFE8E320A02A1D30009E61D /* WebCoreTextRenderer.mm in Sources */,
                                14DC0D3709FED073007B0235 /* JSNode.cpp in Sources */,
                                142011B60A003133008303F9 /* JSCSSStyleDeclaration.cpp in Sources */,
                                A7CFB3D50B7ED1180070C32D /* DragImageMac.mm in Sources */,
                                37919C230B7D188600A56998 /* PositionIterator.cpp in Sources */,
                                DD05FE0E0B8BA3C6009ACDFE /* WebCoreObjCExtras.c in Sources */,
+                               06A6A73C0B8BA44800DF1703 /* StringTruncator.cpp in Sources */,
                        );
                        runOnlyForDeploymentPostprocessing = 0;
                };
diff --git a/WebCore/bridge/mac/WebCoreStringTruncator.mm b/WebCore/bridge/mac/WebCoreStringTruncator.mm
deleted file mode 100644 (file)
index 10d137a..0000000
+++ /dev/null
@@ -1,236 +0,0 @@
-/*
- * Copyright (C) 2005 Apple Computer, Inc.  All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * 1.  Redistributions of source code must retain the above copyright
- *     notice, this list of conditions and the following disclaimer. 
- * 2.  Redistributions in binary form must reproduce the above copyright
- *     notice, this list of conditions and the following disclaimer in the
- *     documentation and/or other materials provided with the distribution. 
- * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
- *     its contributors may be used to endorse or promote products derived
- *     from this software without specific prior written permission. 
- *
- * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
- * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
- * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
- * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-#import "config.h"
-#import "WebCoreStringTruncator.h"
-
-#import "CharacterNames.h"
-#import "Font.h"
-#import "FontPlatformData.h"
-#import "TextStyle.h"
-#import <wtf/Assertions.h>
-
-using namespace WebCore;
-
-#define STRING_BUFFER_SIZE 2048
-
-static NSFont *currentFont;
-static Font* currentRenderer = 0;
-static float currentEllipsisWidth;
-
-typedef unsigned TruncationFunction(NSString *string, unsigned length, unsigned keepCount, unichar *buffer);
-
-static unsigned centerTruncateToBuffer(NSString *string, unsigned length, unsigned keepCount, unichar *buffer)
-{
-    ASSERT(keepCount < length);
-    ASSERT(keepCount < STRING_BUFFER_SIZE);
-    
-    unsigned omitStart = (keepCount + 1) / 2;
-    unsigned omitEnd = NSMaxRange([string rangeOfComposedCharacterSequenceAtIndex:omitStart + (length - keepCount) - 1]);
-    omitStart = [string rangeOfComposedCharacterSequenceAtIndex:omitStart].location;
-    
-    NSRange beforeRange = NSMakeRange(0, omitStart);
-    NSRange afterRange = NSMakeRange(omitEnd, length - omitEnd);
-    
-    unsigned truncatedLength = beforeRange.length + 1 + afterRange.length;
-    ASSERT(truncatedLength <= length);
-
-    [string getCharacters:buffer range:beforeRange];
-    buffer[beforeRange.length] = horizontalEllipsis;
-    [string getCharacters:&buffer[beforeRange.length + 1] range:afterRange];
-    
-    return truncatedLength;
-}
-
-static unsigned rightTruncateToBuffer(NSString *string, unsigned length, unsigned keepCount, unichar *buffer)
-{
-    ASSERT(keepCount < length);
-    ASSERT(keepCount < STRING_BUFFER_SIZE);
-    
-    NSRange keepRange = NSMakeRange(0, [string rangeOfComposedCharacterSequenceAtIndex:keepCount].location);
-    
-    [string getCharacters:buffer range:keepRange];
-    buffer[keepRange.length] = horizontalEllipsis;
-    
-    return keepRange.length + 1;
-}
-
-static float stringWidth(Font* renderer, const unichar *characters, unsigned length)
-{
-    TextRun run(characters, length);
-    TextStyle style;
-    style.disableRoundingHacks();
-    return renderer->floatWidth(run, style);
-}
-
-static NSString *truncateString(NSString *string, float maxWidth, NSFont *font, TruncationFunction truncateToBuffer)
-{
-    unsigned length = [string length];
-    if (length == 0) {
-        return string;
-    }
-
-    unichar stringBuffer[STRING_BUFFER_SIZE];
-    unsigned keepCount;
-    unsigned truncatedLength;
-    float width;
-    unichar ellipsis;
-    unsigned keepCountForLargestKnownToFit, keepCountForSmallestKnownToNotFit;
-    float widthForLargestKnownToFit, widthForSmallestKnownToNotFit;
-    float ratio;
-    
-    ASSERT_ARG(font, font);
-    ASSERT_ARG(maxWidth, maxWidth >= 0);
-    
-    if (![currentFont isEqual:font]) {
-        [currentFont release];
-        currentFont = [font retain];
-        FontPlatformData f(font);
-        delete currentRenderer;
-        currentRenderer = new Font(f, ![[NSGraphicsContext currentContext] isDrawingToScreen]);
-        ellipsis = horizontalEllipsis;
-        currentEllipsisWidth = stringWidth(currentRenderer, &ellipsis, 1);
-    }
-    
-    ASSERT(currentRenderer);
-
-    if (length > STRING_BUFFER_SIZE) {
-        keepCount = STRING_BUFFER_SIZE - 1; // need 1 character for the ellipsis
-        truncatedLength = centerTruncateToBuffer(string, length, keepCount, stringBuffer);
-    } else {
-        keepCount = length;
-        [string getCharacters:stringBuffer];
-        truncatedLength = length;
-    }
-
-    width = stringWidth(currentRenderer, stringBuffer, truncatedLength);
-    if (width <= maxWidth) {
-        return string;
-    }
-
-    keepCountForLargestKnownToFit = 0;
-    widthForLargestKnownToFit = currentEllipsisWidth;
-    
-    keepCountForSmallestKnownToNotFit = keepCount;
-    widthForSmallestKnownToNotFit = width;
-    
-    if (currentEllipsisWidth >= maxWidth) {
-        keepCountForLargestKnownToFit = 1;
-        keepCountForSmallestKnownToNotFit = 2;
-    }
-    
-    while (keepCountForLargestKnownToFit + 1 < keepCountForSmallestKnownToNotFit) {
-        ASSERT(widthForLargestKnownToFit <= maxWidth);
-        ASSERT(widthForSmallestKnownToNotFit > maxWidth);
-
-        ratio = (keepCountForSmallestKnownToNotFit - keepCountForLargestKnownToFit)
-            / (widthForSmallestKnownToNotFit - widthForLargestKnownToFit);
-        keepCount = static_cast<unsigned>(maxWidth * ratio);
-        
-        if (keepCount <= keepCountForLargestKnownToFit) {
-            keepCount = keepCountForLargestKnownToFit + 1;
-        } else if (keepCount >= keepCountForSmallestKnownToNotFit) {
-            keepCount = keepCountForSmallestKnownToNotFit - 1;
-        }
-        
-        ASSERT(keepCount < length);
-        ASSERT(keepCount > 0);
-        ASSERT(keepCount < keepCountForSmallestKnownToNotFit);
-        ASSERT(keepCount > keepCountForLargestKnownToFit);
-        
-        truncatedLength = truncateToBuffer(string, length, keepCount, stringBuffer);
-
-        width = stringWidth(currentRenderer, stringBuffer, truncatedLength);
-        if (width <= maxWidth) {
-            keepCountForLargestKnownToFit = keepCount;
-            widthForLargestKnownToFit = width;
-        } else {
-            keepCountForSmallestKnownToNotFit = keepCount;
-            widthForSmallestKnownToNotFit = width;
-        }
-    }
-    
-    if (keepCountForLargestKnownToFit == 0) {
-        keepCountForLargestKnownToFit = 1;
-    }
-    
-    if (keepCount != keepCountForLargestKnownToFit) {
-        keepCount = keepCountForLargestKnownToFit;
-        truncatedLength = truncateToBuffer(string, length, keepCount, stringBuffer);
-    }
-    
-    return [NSString stringWithCharacters:stringBuffer length:truncatedLength];
-}
-
-@implementation WebCoreStringTruncator
-
-static NSFont *defaultMenuFont(void)
-{
-    static NSFont *defaultMenuFont = nil;
-    if (defaultMenuFont == nil) {
-        defaultMenuFont = [[NSFont menuFontOfSize:0] retain];
-    }
-    return defaultMenuFont;
-}
-
-+ (NSString *)centerTruncateString:(NSString *)string toWidth:(float)maxWidth
-{
-    return truncateString(string, maxWidth, defaultMenuFont(), centerTruncateToBuffer);
-}
-
-+ (NSString *)centerTruncateString:(NSString *)string toWidth:(float)maxWidth withFont:(NSFont *)font
-{
-    return truncateString(string, maxWidth, font, centerTruncateToBuffer);
-}
-
-+ (NSString *)rightTruncateString:(NSString *)string toWidth:(float)maxWidth withFont:(NSFont *)font
-{
-    return truncateString(string, maxWidth, font, rightTruncateToBuffer);
-}
-
-+ (float)widthOfString:(NSString *)string font:(NSFont *)font
-{
-    unsigned length = [string length];
-    unichar *s = static_cast<unichar*>(malloc(sizeof(unichar) * length));
-    [string getCharacters:s];
-    FontPlatformData f(font);
-    Font fontRenderer(f, ![[NSGraphicsContext currentContext] isDrawingToScreen]);
-    float width = stringWidth(&fontRenderer, s, length);
-    free(s);
-    return width;
-}
-
-+ (void)clear
-{
-    delete currentRenderer;
-    currentRenderer = 0;
-    [currentFont release];
-    currentFont = nil;
-}
-
-@end
diff --git a/WebCore/platform/StringTruncator.cpp b/WebCore/platform/StringTruncator.cpp
new file mode 100644 (file)
index 0000000..75b6471
--- /dev/null
@@ -0,0 +1,200 @@
+/*
+ * Copyright (C) 2005, 2006, 2007 Apple Inc.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1.  Redistributions of source code must retain the above copyright
+ *     notice, this list of conditions and the following disclaimer. 
+ * 2.  Redistributions in binary form must reproduce the above copyright
+ *     notice, this list of conditions and the following disclaimer in the
+ *     documentation and/or other materials provided with the distribution. 
+ * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
+ *     its contributors may be used to endorse or promote products derived
+ *     from this software without specific prior written permission. 
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+#include "StringTruncator.h"
+
+#include "CharacterNames.h"
+#include "Font.h"
+#include "FontPlatformData.h"
+#include "TextBreakIterator.h"
+#include "TextStyle.h"
+#include <wtf/Assertions.h>
+#include <wtf/Vector.h>
+
+namespace WebCore {
+
+#define STRING_BUFFER_SIZE 2048
+
+typedef unsigned TruncationFunction(const String&, unsigned length, unsigned keepCount, UChar* buffer);
+
+static inline int textBreakAtOrPreceding(TextBreakIterator* it, int offset)
+{
+    if (isTextBreak(it, offset))
+        return offset;
+
+    int result = textBreakPreceding(it, offset);
+    return result == TextBreakDone ? 0 : result;
+}
+
+static inline int boundedTextBreakFollowing(TextBreakIterator* it, int offset, int length)
+{
+    int result = textBreakFollowing(it, offset);
+    return result == TextBreakDone ? length : result;
+}
+
+static unsigned centerTruncateToBuffer(const String& string, unsigned length, unsigned keepCount, UChar* buffer)
+{
+    ASSERT(keepCount < length);
+    ASSERT(keepCount < STRING_BUFFER_SIZE);
+    
+    unsigned omitStart = (keepCount + 1) / 2;
+    TextBreakIterator* it = characterBreakIterator(string.characters(), length);
+    unsigned omitEnd = boundedTextBreakFollowing(it, omitStart + (length - keepCount) - 1, length);
+    omitStart = textBreakAtOrPreceding(it, omitStart);
+    
+    unsigned truncatedLength = omitStart + 1 + (length - omitEnd);
+    ASSERT(truncatedLength <= length);
+
+    memcpy(buffer, string.characters(), sizeof(UChar) * omitStart);
+    buffer[omitStart] = horizontalEllipsis;
+    memcpy(&buffer[omitStart + 1], &string.characters()[omitEnd], sizeof(UChar) * (length - omitEnd));
+    
+    return truncatedLength;
+}
+
+static unsigned rightTruncateToBuffer(const String& string, unsigned length, unsigned keepCount, UChar* buffer)
+{
+    ASSERT(keepCount < length);
+    ASSERT(keepCount < STRING_BUFFER_SIZE);
+    
+    TextBreakIterator* it = characterBreakIterator(string.characters(), length);
+    unsigned keepLength = textBreakAtOrPreceding(it, keepCount);
+    unsigned truncatedLength = keepLength + 1;
+    
+    memcpy(buffer, string.characters(), sizeof(UChar) * keepLength);
+    buffer[keepLength] = horizontalEllipsis;
+    
+    return truncatedLength;
+}
+
+static float stringWidth(const Font& renderer, const UChar* characters, unsigned length)
+{
+    TextRun run(characters, length);
+    TextStyle style;
+    style.disableRoundingHacks();
+    return renderer.floatWidth(run, style);
+}
+
+static String truncateString(const String& string, float maxWidth, const Font& font, TruncationFunction truncateToBuffer)
+{
+    if (string.isEmpty())
+        return string;
+    
+    ASSERT(maxWidth >= 0);
+    
+    float currentEllipsisWidth = stringWidth(font, &horizontalEllipsis, 1);
+    
+    UChar stringBuffer[STRING_BUFFER_SIZE];
+    unsigned truncatedLength;
+    unsigned keepCount;
+    unsigned length = string.length();
+
+    if (length > STRING_BUFFER_SIZE) {
+        keepCount = STRING_BUFFER_SIZE - 1; // need 1 character for the ellipsis
+        truncatedLength = centerTruncateToBuffer(string, length, keepCount, stringBuffer);
+    } else {
+        keepCount = length;
+        memcpy(stringBuffer, string.characters(), sizeof(UChar) * length);
+        truncatedLength = length;
+    }
+
+    float width = stringWidth(font, stringBuffer, truncatedLength);
+    if (width <= maxWidth)
+        return string;
+
+    unsigned keepCountForLargestKnownToFit = 0;
+    unsigned widthForLargestKnownToFit = currentEllipsisWidth;
+    
+    unsigned keepCountForSmallestKnownToNotFit = keepCount;
+    unsigned widthForSmallestKnownToNotFit = width;
+    
+    if (currentEllipsisWidth >= maxWidth) {
+        keepCountForLargestKnownToFit = 1;
+        keepCountForSmallestKnownToNotFit = 2;
+    }
+    
+    while (keepCountForLargestKnownToFit + 1 < keepCountForSmallestKnownToNotFit) {
+        ASSERT(widthForLargestKnownToFit <= maxWidth);
+        ASSERT(widthForSmallestKnownToNotFit > maxWidth);
+
+        float ratio = (keepCountForSmallestKnownToNotFit - keepCountForLargestKnownToFit)
+            / (widthForSmallestKnownToNotFit - widthForLargestKnownToFit);
+        keepCount = static_cast<unsigned>(maxWidth * ratio);
+        
+        if (keepCount <= keepCountForLargestKnownToFit) {
+            keepCount = keepCountForLargestKnownToFit + 1;
+        } else if (keepCount >= keepCountForSmallestKnownToNotFit) {
+            keepCount = keepCountForSmallestKnownToNotFit - 1;
+        }
+        
+        ASSERT(keepCount < length);
+        ASSERT(keepCount > 0);
+        ASSERT(keepCount < keepCountForSmallestKnownToNotFit);
+        ASSERT(keepCount > keepCountForLargestKnownToFit);
+        
+        truncatedLength = truncateToBuffer(string, length, keepCount, stringBuffer);
+
+        width = stringWidth(font, stringBuffer, truncatedLength);
+        if (width <= maxWidth) {
+            keepCountForLargestKnownToFit = keepCount;
+            widthForLargestKnownToFit = width;
+        } else {
+            keepCountForSmallestKnownToNotFit = keepCount;
+            widthForSmallestKnownToNotFit = width;
+        }
+    }
+    
+    if (keepCountForLargestKnownToFit == 0) {
+        keepCountForLargestKnownToFit = 1;
+    }
+    
+    if (keepCount != keepCountForLargestKnownToFit) {
+        keepCount = keepCountForLargestKnownToFit;
+        truncatedLength = truncateToBuffer(string, length, keepCount, stringBuffer);
+    }
+    
+    return String(stringBuffer, truncatedLength);
+}
+
+String StringTruncator::centerTruncate(const String& string, float maxWidth, const Font& font)
+{
+    return truncateString(string, maxWidth, font, centerTruncateToBuffer);
+}
+
+String StringTruncator::rightTruncate(const String& string, float maxWidth, const Font& font)
+{
+    return truncateString(string, maxWidth, font, rightTruncateToBuffer);
+}
+
+float StringTruncator::width(const String& string, const Font& font)
+{
+    return stringWidth(font, string.characters(), string.length());
+}
+
+} // namespace WebCore
similarity index 74%
rename from WebCore/bridge/mac/WebCoreStringTruncator.h
rename to WebCore/platform/StringTruncator.h
index c944eaea18f78de6a8c5bd153d2f7a78f3b3af83..66c4ae91e4b612587123bf8ed2bd1014407ed4bd 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2005 Apple Computer, Inc.  All rights reserved.
+ * Copyright (C) 2005, 2006, 2007 Apple Inc.  All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-@interface WebCoreStringTruncator : NSObject
+#ifndef StringTruncator_h
+#define StringTruncator_h
 
-+ (NSString *)centerTruncateString:(NSString *)string toWidth:(float)maxWidth withFont:(NSFont *)font;
+namespace WebCore {
+    
+    class Font;
+    class String;
+    
+    class StringTruncator {
+    public:
+        static String centerTruncate(const String&, float maxWidth, const Font&);
+        static String rightTruncate(const String&, float maxWidth, const Font&);
+        static float width(const String&, const Font&);
+    };
+    
+} // namespace WebCore
 
-// Default font is [NSFont menuFontOfSize:0].
-+ (NSString *)centerTruncateString:(NSString *)string toWidth:(float)maxWidth;
-
-+ (NSString *)rightTruncateString:(NSString *)string toWidth:(float)maxWidth withFont:(NSFont *)font;
-
-+ (float)widthOfString:(NSString *)string font:(NSFont *)font;
-
-+ (void)clear;
-
-@end
+#endif // !defined(StringTruncator_h)
index 9666269973e11fc61cde70fb284b2ec2416ce07c..6d21d643fea9031f4108b81ccb1bec4016ab3644 100644 (file)
@@ -38,6 +38,7 @@ namespace WebCore {
     int textBreakCurrent(TextBreakIterator*);
     int textBreakPreceding(TextBreakIterator*, int);
     int textBreakFollowing(TextBreakIterator*, int);
+    bool isTextBreak(TextBreakIterator*, int);
 
     const int TextBreakDone = -1;
 
index bde63df97206146e1c934bb84add8a9e28ce32ab..d08d54d552dd13769b28477cd025f130846cbb97 100644 (file)
@@ -101,4 +101,9 @@ int textBreakCurrent(TextBreakIterator* bi)
     return ubrk_current(bi);
 }
 
+bool isTextBreak(TextBreakIterator* bi, int pos)
+{
+    return ubrk_isBoundary(bi, pos);
+}
+
 }
index 39ff0624cb6e78d4e2d67c64f5276817136c5804..73ff0f834192e131f9337fb88401e4a20e87cfd0 100644 (file)
@@ -35,8 +35,8 @@
 #import "Frame.h"
 #import "Icon.h"
 #import "LocalizedStrings.h"
+#import "StringTruncator.h"
 #import "WebCoreFrameBridge.h"
-#import "WebCoreStringTruncator.h"
 
 using namespace WebCore;
 
@@ -122,8 +122,7 @@ String FileChooser::basenameForWidth(const Font& font, int width) const
     else
         strToTruncate = [[NSFileManager defaultManager] displayNameAtPath:m_filename];
 
-    return [WebCoreStringTruncator centerTruncateString:strToTruncate
-        toWidth:width withFont:font.primaryFont()->getNSFont()];
+    return StringTruncator::centerTruncate(strToTruncate, width, font);
 }
 
 }
index a9020dfc6cbcb2a1b961cc597cea42cf5435eb47..fd694eeba3f0bf23c75b486949b5ede796f5d5af 100644 (file)
@@ -174,5 +174,10 @@ int textBreakCurrent(TextBreakIterator* bi)
     return bi->currentPos;
 }
 
+bool isTextBreak(TextBreakIterator*, int)
+{
+    return true;
+}
+
 }
 
index 8cf49f33733546093904988a26def79558408903..a76864d3f49817b4458aad2d4417dd9296e83678 100644 (file)
@@ -1,3 +1,19 @@
+2007-02-20  Adam Roben  <aroben@apple.com>
+
+        Reviewed by Darin and Anders.
+
+        Update WebKit for WebCore fix for <rdar://problem/4736215> Make
+        WebCoreStringTruncator use WebCore types.
+
+        * Misc/WebStringTruncator.m:
+        (defaultMenuFont): Moved from WebCoreStringTruncator.mm.
+        (core): Added.
+        (+[WebStringTruncator centerTruncateString:toWidth:]):
+        (+[WebStringTruncator centerTruncateString:toWidth:withFont:]):
+        (+[WebStringTruncator rightTruncateString:toWidth:withFont:]):
+        (+[WebStringTruncator widthOfString:font:]):
+        * WebKit.xcodeproj/project.pbxproj: Changed WebStringTruncator to ObjC++.
+
 2007-02-20  Timothy Hatcher  <timothy@apple.com>
 
         Reviewed by John.
index bd23a0b0bcaa1c8ef4d94b9e386abf340bccec42..7ca645fdb6ae6d819860ae71825a60a5ed8ead7f 100644 (file)
 #import "WebStringTruncator.h"
 
 #import "WebSystemInterface.h"
-#import <WebCore/WebCoreStringTruncator.h>
+#import <WebCore/Font.h>
+#import <WebCore/FontPlatformData.h>
+#import <WebCore/PlatformString.h>
+#import <WebCore/StringTruncator.h>
+
+using namespace WebCore;
+
+static NSFont *defaultMenuFont()
+{
+    static NSFont *defaultMenuFont = [[NSFont menuFontOfSize:0] retain];
+    return defaultMenuFont;
+}
+
+static Font& fontFromNSFont(NSFont *font)
+{
+    static NSFont *currentFont;
+    static Font currentRenderer;
+
+    if ([font isEqual:currentFont])
+        return currentRenderer;
+    
+    [currentFont release];
+    currentFont = [font retain];
+    FontPlatformData f(font);
+    currentRenderer = Font(f, ![[NSGraphicsContext currentContext] isDrawingToScreen]);
+    return currentRenderer;
+}
 
 @implementation WebStringTruncator
 
 
 + (NSString *)centerTruncateString:(NSString *)string toWidth:(float)maxWidth
 {
-    return [WebCoreStringTruncator centerTruncateString:string toWidth:maxWidth];
+    return StringTruncator::centerTruncate(string, maxWidth, fontFromNSFont(defaultMenuFont()));
 }
 
 + (NSString *)centerTruncateString:(NSString *)string toWidth:(float)maxWidth withFont:(NSFont *)font
 {
-    return [WebCoreStringTruncator centerTruncateString:string toWidth:maxWidth withFont:font];
+    return StringTruncator::centerTruncate(string, maxWidth, fontFromNSFont(font));
 }
 
 + (NSString *)rightTruncateString:(NSString *)string toWidth:(float)maxWidth withFont:(NSFont *)font
 {
-    return [WebCoreStringTruncator rightTruncateString:string toWidth:maxWidth withFont:font];
+    return StringTruncator::rightTruncate(string, maxWidth, fontFromNSFont(font));
 }
 
 + (float)widthOfString:(NSString *)string font:(NSFont *)font
 {
-    return [WebCoreStringTruncator widthOfString:string font:font];
+    return StringTruncator::width(string, fontFromNSFont(font));
 }
 
 @end
index b929543bc2a43d798ec29eaa4fb717fe1a0f121d..631283aa7544e2e581242fc4e4e9975a9437100d 100644 (file)
                F5883BE0025E5E9D01000102 /* WebNullPluginView.h */ = {isa = PBXFileReference; fileEncoding = 4; indentWidth = 4; lastKnownFileType = sourcecode.c.h; path = WebNullPluginView.h; sourceTree = "<group>"; tabWidth = 8; usesTabs = 0; };
                F5883BE1025E5E9D01000102 /* WebNullPluginView.mm */ = {isa = PBXFileReference; fileEncoding = 4; indentWidth = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = WebNullPluginView.mm; sourceTree = "<group>"; tabWidth = 8; usesTabs = 0; };
                F5927D4E02D26C5E01CA2DBB /* WebKitErrors.h */ = {isa = PBXFileReference; fileEncoding = 4; indentWidth = 4; lastKnownFileType = sourcecode.c.h; path = WebKitErrors.h; sourceTree = "<group>"; tabWidth = 8; usesTabs = 0; };
-               F59668C802AD2923018635CA /* WebStringTruncator.h */ = {isa = PBXFileReference; fileEncoding = 4; indentWidth = 4; lastKnownFileType = sourcecode.c.h; path = WebStringTruncator.h; sourceTree = "<group>"; tabWidth = 8; usesTabs = 0; };
-               F59668C902AD2923018635CA /* WebStringTruncator.m */ = {isa = PBXFileReference; fileEncoding = 4; indentWidth = 4; lastKnownFileType = sourcecode.c.objc; path = WebStringTruncator.m; sourceTree = "<group>"; tabWidth = 8; usesTabs = 0; };
+               F59668C802AD2923018635CA /* WebStringTruncator.h */ = {isa = PBXFileReference; explicitFileType = sourcecode.cpp.objcpp; fileEncoding = 4; indentWidth = 4; path = WebStringTruncator.h; sourceTree = "<group>"; tabWidth = 8; usesTabs = 0; };
+               F59668C902AD2923018635CA /* WebStringTruncator.m */ = {isa = PBXFileReference; explicitFileType = sourcecode.cpp.objcpp; fileEncoding = 4; indentWidth = 4; path = WebStringTruncator.m; sourceTree = "<group>"; tabWidth = 8; usesTabs = 0; };
                F59EAE3E0253C7EE018635CA /* WebCoreStatistics.h */ = {isa = PBXFileReference; fileEncoding = 4; indentWidth = 4; lastKnownFileType = sourcecode.c.h; path = WebCoreStatistics.h; sourceTree = "<group>"; tabWidth = 8; usesTabs = 0; };
                F59EAE410253C8DE018635CA /* WebCoreStatistics.mm */ = {isa = PBXFileReference; fileEncoding = 4; indentWidth = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = WebCoreStatistics.mm; sourceTree = "<group>"; tabWidth = 8; usesTabs = 0; };
                F5A55DC702BAA2E8018635CC /* WebHTMLRepresentationPrivate.h */ = {isa = PBXFileReference; fileEncoding = 4; indentWidth = 4; lastKnownFileType = sourcecode.c.h; path = WebHTMLRepresentationPrivate.h; sourceTree = "<group>"; tabWidth = 8; usesTabs = 0; };