Bug 160895 - [Cocoa] Migrate off of deprecated CoreGraphics API CGContextSelectFont() and CGContextShowTextAtPoint()
Summary: [Cocoa] Migrate off of deprecated CoreGraphics API CGContextSelectFont() and ...
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: New Bugs (show other bugs)
Version: WebKit Nightly Build
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Myles C. Maxfield
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-08-16 00:56 PDT by Myles C. Maxfield
Modified: 2016-08-19 15:14 PDT (History)
5 users (show)

See Also:


Attachments
Patch (8.48 KB, patch)
2016-08-16 00:58 PDT, Myles C. Maxfield
no flags Details | Formatted Diff | Diff
WIP (12.71 KB, patch)
2016-08-16 19:33 PDT, Myles C. Maxfield
no flags Details | Formatted Diff | Diff
Patch (12.78 KB, patch)
2016-08-17 01:57 PDT, Myles C. Maxfield
no flags Details | Formatted Diff | Diff
Patch (12.80 KB, patch)
2016-08-17 03:16 PDT, Myles C. Maxfield
no flags Details | Formatted Diff | Diff
Patch (14.87 KB, patch)
2016-08-17 09:23 PDT, Myles C. Maxfield
no flags Details | Formatted Diff | Diff
Patch (16.73 KB, patch)
2016-08-17 13:30 PDT, Myles C. Maxfield
dino: review+
Details | Formatted Diff | Diff
Patch for committing (16.69 KB, patch)
2016-08-17 22:19 PDT, Myles C. Maxfield
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Myles C. Maxfield 2016-08-16 00:56:36 PDT
[Cocoa] Migrate off of deprecated CoreGraphics API CGContextSelectFont() and CGContextShowTextAtPoint()
Comment 1 Myles C. Maxfield 2016-08-16 00:58:23 PDT
Created attachment 286164 [details]
Patch
Comment 2 Sam Weinig 2016-08-16 06:53:34 PDT
Comment on attachment 286164 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=286164&action=review

> Source/WebCore/platform/graphics/ca/PlatformCALayer.cpp:137
>  // This function is needed to work around a bug in Windows CG <rdar://problem/22703470>
>  void PlatformCALayer::drawTextAtPoint(CGContextRef context, CGFloat x, CGFloat y, CGSize scale, CGFloat fontSize, const char* text, size_t length) const
>  {
> -#pragma clang diagnostic push
> -#pragma clang diagnostic ignored "-Wdeprecated-declarations"
> -    CGContextSetTextMatrix(context, CGAffineTransformMakeScale(scale.width, scale.height));
> -    CGContextSelectFont(context, "Helvetica", fontSize, kCGEncodingMacRoman);
> -    CGContextShowTextAtPoint(context, x, y, text, length);
> -#pragma clang diagnostic pop
> +    CGAffineTransform matrix = CGAffineTransformMakeScale(scale.width, scale.height);
> +    RetainPtr<CTFontRef> font = adoptCF(CTFontCreateWithName(CFSTR("Helvetica"), fontSize, &matrix));
> +    CFTypeRef keys[] = { kCTFontAttributeName };
> +    CFTypeRef values[] = { font.get() };
> +    RetainPtr<CFDictionaryRef> attributes = adoptCF(CFDictionaryCreate(kCFAllocatorDefault, keys, values, WTF_ARRAY_LENGTH(keys), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks));
> +    RetainPtr<CFStringRef> string = adoptCF(CFStringCreateWithBytesNoCopy(kCFAllocatorDefault, reinterpret_cast<const UInt8*>(text), length, kCFStringEncodingUTF8, false, kCFAllocatorNull));
> +    RetainPtr<CFAttributedStringRef> attributedString = adoptCF(CFAttributedStringCreate(kCFAllocatorDefault, string.get(), attributes.get()));
> +    RetainPtr<CTLineRef> line = adoptCF(CTLineCreateWithAttributedString(attributedString.get()));
> +    CGPoint textPosition = CGContextGetTextPosition(context);
> +    CGContextSetTextPosition(context, x, y);
> +    CTLineDraw(line.get(), context);

I don't think CoreText exists on Windows, so this probably won't compile.
Comment 3 Myles C. Maxfield 2016-08-16 17:58:52 PDT
Comment on attachment 286164 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=286164&action=review

>> Source/WebCore/platform/graphics/ca/PlatformCALayer.cpp:137
>> +    CTLineDraw(line.get(), context);
> 
> I don't think CoreText exists on Windows, so this probably won't compile.

Hrm. I see CoreText.dll inside the Apple Application Support folder (next to CoreGraphics.dll, etc.), and CoreText.lib inside WebKitSupportLibrary. It looks like the headers inside WebKitSupportLibrary are incomplete, but running dumpbin.exe on CoreText.lib makes it seem that the functions do exist.
Comment 4 Myles C. Maxfield 2016-08-16 19:33:46 PDT
Created attachment 286256 [details]
WIP
Comment 5 Myles C. Maxfield 2016-08-17 01:57:25 PDT
Created attachment 286291 [details]
Patch
Comment 6 Myles C. Maxfield 2016-08-17 03:16:45 PDT
Created attachment 286294 [details]
Patch
Comment 7 Myles C. Maxfield 2016-08-17 09:23:10 PDT
Created attachment 286302 [details]
Patch
Comment 8 Myles C. Maxfield 2016-08-17 13:30:12 PDT
Created attachment 286321 [details]
Patch
Comment 9 Dean Jackson 2016-08-17 14:58:32 PDT
Comment on attachment 286321 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=286321&action=review

> Source/WebCore/page/cocoa/ResourceUsageOverlayCocoa.mm:260
>  #if PLATFORM(IOS)
> -    CGContextSelectFont(context, "Courier", 10, kCGEncodingMacRoman);
> +    CFStringRef fontName = CFSTR("Courier");
> +    CGFloat fontSize = 10;

Why Courier on iOS? And why Menlo elsewhere? Can't we use SF on iOS and where available on macOS?

> Source/WebCore/platform/ios/LegacyTileCache.mm:598
> +        RetainPtr<CTFontRef> font = adoptCF(CTFontCreateWithName(CFSTR("Helvetica"), 25, &matrix));

We can probably use SF or Menlo here too.

> Source/WebCore/platform/spi/win/CoreTextSPIWin.h:2
> +/*
> + * Copyright (C) 2016 Apple Inc. All rights reserved.

Why spi/win/CoreTextSPIWin.h?

Can we just merge this with an existing CoreTextSPI.h?
Comment 10 Myles C. Maxfield 2016-08-17 15:05:15 PDT
Comment on attachment 286321 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=286321&action=review

>> Source/WebCore/page/cocoa/ResourceUsageOverlayCocoa.mm:260
>> +    CGFloat fontSize = 10;
> 
> Why Courier on iOS? And why Menlo elsewhere? Can't we use SF on iOS and where available on macOS?

That choice was in the existing code; this patch simply migrates the existing code to non-deprecated calls.

>> Source/WebCore/platform/ios/LegacyTileCache.mm:598
>> +        RetainPtr<CTFontRef> font = adoptCF(CTFontCreateWithName(CFSTR("Helvetica"), 25, &matrix));
> 
> We can probably use SF or Menlo here too.

Ditto.

>> Source/WebCore/platform/spi/win/CoreTextSPIWin.h:2
>> + * Copyright (C) 2016 Apple Inc. All rights reserved.
> 
> Why spi/win/CoreTextSPIWin.h?
> 
> Can we just merge this with an existing CoreTextSPI.h?

CoreTextSPI.h is inside cocoa/

I could move it out, but there would be a ton of new things needing to be forward-declared in order to just get CoreTextSPI.h to compile on Windows. I think that keeping this simple is a good idea, given the very different requirements of Windows and macOS/iOS.
Comment 11 Myles C. Maxfield 2016-08-17 15:06:20 PDT
Comment on attachment 286321 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=286321&action=review

>>> Source/WebCore/page/cocoa/ResourceUsageOverlayCocoa.mm:260
>>> +    CGFloat fontSize = 10;
>> 
>> Why Courier on iOS? And why Menlo elsewhere? Can't we use SF on iOS and where available on macOS?
> 
> That choice was in the existing code; this patch simply migrates the existing code to non-deprecated calls.

What I mean to say is: switching out the fonts is a good idea, but not for this patch. This patch has no behavior changes.
Comment 12 Myles C. Maxfield 2016-08-17 22:10:15 PDT
Comment on attachment 286321 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=286321&action=review

> Source/WebCore/page/cocoa/ResourceUsageOverlayCocoa.mm:266
> +    CFTypeRef keys[] = { kCTFontAttributeName };

kCTForegroundColorFromContextAttributeName

> Source/WebCore/platform/graphics/ca/PlatformCALayer.cpp:133
> +    CFTypeRef keys[] = { kCTFontAttributeName };

kCTForegroundColorFromContextAttributeName

> Source/WebCore/platform/ios/LegacyTileCache.mm:599
> +        CFTypeRef keys[] = { kCTFontAttributeName };

kCTForegroundColorFromContextAttributeName
Comment 13 Myles C. Maxfield 2016-08-17 22:19:10 PDT
Created attachment 286363 [details]
Patch for committing
Comment 14 WebKit Commit Bot 2016-08-17 23:39:51 PDT
Comment on attachment 286363 [details]
Patch for committing

Clearing flags on attachment: 286363

Committed r204592: <http://trac.webkit.org/changeset/204592>
Comment 15 Csaba Osztrogonác 2016-08-18 01:02:46 PDT
(In reply to comment #14)
> Comment on attachment 286363 [details]
> Patch for committing
> 
> Clearing flags on attachment: 286363
> 
> Committed r204592: <http://trac.webkit.org/changeset/204592>

It broke the WinCairo build, see build.webkit.org for details.
Comment 16 Darin Adler 2016-08-19 13:10:08 PDT
Comment on attachment 286363 [details]
Patch for committing

View in context: https://bugs.webkit.org/attachment.cgi?id=286363&action=review

Some comments post-landing for future patches like this one.

> Source/WebCore/page/cocoa/ResourceUsageOverlayCocoa.mm:257
> +    CGAffineTransform matrix = CGAffineTransformMakeScale(1, -1);

Might be nicer to use auto for a line like this.

> Source/WebCore/page/cocoa/ResourceUsageOverlayCocoa.mm:265
> +    RetainPtr<CTFontRef> font = adoptCF(CTFontCreateWithName(fontName, fontSize, &matrix));

In future code, I suggest auto instead of writing out the type for the result of adoptCF calls.

> Source/WebCore/page/cocoa/ResourceUsageOverlayCocoa.mm:268
> +    RetainPtr<CFDictionaryRef> attributes = adoptCF(CFDictionaryCreate(kCFAllocatorDefault, keys, values, WTF_ARRAY_LENGTH(keys), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks));

And this.

> Source/WebCore/page/cocoa/ResourceUsageOverlayCocoa.mm:272
> +    RetainPtr<CFStringRef> string = adoptCF(CFStringCreateWithBytesNoCopy(kCFAllocatorDefault, reinterpret_cast<const UInt8*>(cstr.data()), cstr.length(), kCFStringEncodingASCII, false, kCFAllocatorNull));
> +    RetainPtr<CFAttributedStringRef> attributedString = adoptCF(CFAttributedStringCreate(kCFAllocatorDefault, string.get(), attributes.get()));
> +    RetainPtr<CTLineRef> line = adoptCF(CTLineCreateWithAttributedString(attributedString.get()));

And these.

> Source/WebCore/platform/graphics/ca/PlatformCALayer.cpp:36
> +#include <CoreFoundation/CoreFoundation.h>
> +#include <CoreText/CoreText.h>
> +
>  #include "GraphicsContextCG.h"
>  #include "LayerPool.h"
>  #include "PlatformCALayerClient.h"
>  #include "TextStream.h"
>  #include <wtf/StringExtras.h>

WebKit coding style has these sorted in a single paragraph, not in two paragraphs like this.

> Source/WebCore/platform/graphics/ca/PlatformCALayer.cpp:132
> +    CGAffineTransform matrix = CGAffineTransformMakeScale(scale.width, scale.height);
> +    RetainPtr<CTFontRef> font = adoptCF(CTFontCreateWithName(CFSTR("Helvetica"), fontSize, &matrix));

auto

> Source/WebCore/platform/graphics/ca/PlatformCALayer.cpp:138
> +    RetainPtr<CFDictionaryRef> attributes = adoptCF(CFDictionaryCreate(kCFAllocatorDefault, keys, values, WTF_ARRAY_LENGTH(keys), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks));
> +    RetainPtr<CFStringRef> string = adoptCF(CFStringCreateWithBytesNoCopy(kCFAllocatorDefault, reinterpret_cast<const UInt8*>(text), length, kCFStringEncodingUTF8, false, kCFAllocatorNull));
> +    RetainPtr<CFAttributedStringRef> attributedString = adoptCF(CFAttributedStringCreate(kCFAllocatorDefault, string.get(), attributes.get()));
> +    RetainPtr<CTLineRef> line = adoptCF(CTLineCreateWithAttributedString(attributedString.get()));

auto

> Source/WebCore/platform/ios/LegacyTileCache.mm:35
> +#include <CoreText/CoreText.h>
> +
>  #include "FontAntialiasingStateSaver.h"
>  #include "LegacyTileGrid.h"
>  #include "LegacyTileGridTile.h"

Sorted in a single paragraph.

> Source/WebCore/platform/ios/LegacyTileCache.mm:598
> +        CGAffineTransform matrix = CGAffineTransformMakeScale(1, -1);
> +        RetainPtr<CTFontRef> font = adoptCF(CTFontCreateWithName(CFSTR("Helvetica"), 25, &matrix));

auto

> Source/WebCore/platform/ios/LegacyTileCache.mm:604
> +        RetainPtr<CFDictionaryRef> attributes = adoptCF(CFDictionaryCreate(kCFAllocatorDefault, keys, values, WTF_ARRAY_LENGTH(keys), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks));
> +        RetainPtr<CFStringRef> string = adoptCF(CFStringCreateWithBytesNoCopy(kCFAllocatorDefault, reinterpret_cast<const UInt8*>(text), strlen(text), kCFStringEncodingUTF8, false, kCFAllocatorNull));
> +        RetainPtr<CFAttributedStringRef> attributedString = adoptCF(CFAttributedStringCreate(kCFAllocatorDefault, string.get(), attributes.get()));
> +        RetainPtr<CTLineRef> line = adoptCF(CTLineCreateWithAttributedString(attributedString.get()));

auto

> Source/WebCore/platform/spi/win/CoreTextSPIWin.h:35
> +typedef const struct __CTFont* CTFontRef;
> +typedef const struct __CTLine* CTLineRef;

Are these really needed even if we include the headers above?
Comment 17 Myles C. Maxfield 2016-08-19 15:10:12 PDT
Comment on attachment 286363 [details]
Patch for committing

View in context: https://bugs.webkit.org/attachment.cgi?id=286363&action=review

>> Source/WebCore/platform/spi/win/CoreTextSPIWin.h:35
>> +typedef const struct __CTLine* CTLineRef;
> 
> Are these really needed even if we include the headers above?

Yes, because the Windows CoreText headers are incomplete. However, all (many) of the symbols do exist in the .dll, and I verified by hand that they actually do work.
Comment 18 Myles C. Maxfield 2016-08-19 15:14:41 PDT
Committed r204658: <http://trac.webkit.org/changeset/204658>