[webkit-changes] [WebKit/WebKit] da000f: [Cocoa] Add ContextualizedNSString, to allow line ...

Myles C. Maxfield noreply at github.com
Tue May 23 20:39:12 PDT 2023


  Branch: refs/heads/main
  Home:   https://github.com/WebKit/WebKit
  Commit: da000fdb0dc70c5758b808a5bbad99178068bb87
      https://github.com/WebKit/WebKit/commit/da000fdb0dc70c5758b808a5bbad99178068bb87
  Author: Myles C. Maxfield <mmaxfield at apple.com>
  Date:   2023-05-23 (Tue, 23 May 2023)

  Changed paths:
    M Source/WTF/WTF.xcodeproj/project.pbxproj
    A Source/WTF/wtf/text/cocoa/ContextualizedNSString.h
    A Source/WTF/wtf/text/cocoa/ContextualizedNSString.mm
    M Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj
    A Tools/TestWebKitAPI/Tests/WTF/cocoa/ContextualizedNSString.mm

  Log Message:
  -----------
  [Cocoa] Add ContextualizedNSString, to allow line breaking to use Core Foundation but also work with prior context
https://bugs.webkit.org/show_bug.cgi?id=257244
rdar://109747545

Reviewed by Yusuke Suzuki.

In WebKit, we (may) create a new line breaker for each element on the line. This means that,
in order to perform line breaking correctly, the line breaker needs to know about the last few
characters that came at the end of the previous element - we call this a "prior context." The
problem here is that this prior context isn't contiguous in memory with the string being
inspected; it's part of a different string altogether.

Core Foundation has facilities to perform line breaking. If we want to perform line breaking
using these facilities, it has to be possible to make CFString work with this "prior context."
CFString either A) copies and owns its character contents, or B) only accepts a single pointer
to string contents, which it doesn't own. That's not quite what we want: we want it to take
2 pointers to string contents, and act as if the contents of those 2 strings are concatenated.

The standard way of doing this is to subclass NSString. This sounds scary, but it's actually a
totally normal thing to do. There's even a whole section in the docs[1] about how to do it,
for exactly this use case. Because NSStrings are toll-free bridged with CFStrings, the CFString
methods end up calling our overridden Objective C NSString methods. This is how we can make
Core Foundation's line breaking work with prior context.

This patch adds a new NSString subclass, called ContextualizedNSString, and tests it in TestWTF.
It doesn't actually start using it in WTF yet. I'll do that in another patch.

[1] https://developer.apple.com/documentation/foundation/nsstring?language=objc

* Source/WTF/WTF.xcodeproj/project.pbxproj:
* Source/WTF/wtf/text/cocoa/ContextualizedNSString.h: Added.
* Source/WTF/wtf/text/cocoa/ContextualizedNSString.mm: Added.
(-[ContextualizedNSString initWithContext:contents:]):
(-[ContextualizedNSString length]):
(-[ContextualizedNSString characterAtIndex:]):
(-[ContextualizedNSString getCharacters:range:]):
* Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
* Tools/TestWebKitAPI/Tests/WTF/cocoa/ContextualizedNSString.mm: Added.
(TEST):

Canonical link: https://commits.webkit.org/264454@main




More information about the webkit-changes mailing list