RESOLVED FIXED 160895
[Cocoa] Migrate off of deprecated CoreGraphics API CGContextSelectFont() and CGContextShowTextAtPoint()
https://bugs.webkit.org/show_bug.cgi?id=160895
Summary [Cocoa] Migrate off of deprecated CoreGraphics API CGContextSelectFont() and ...
Myles C. Maxfield
Reported 2016-08-16 00:56:36 PDT
[Cocoa] Migrate off of deprecated CoreGraphics API CGContextSelectFont() and CGContextShowTextAtPoint()
Attachments
Patch (8.48 KB, patch)
2016-08-16 00:58 PDT, Myles C. Maxfield
no flags
WIP (12.71 KB, patch)
2016-08-16 19:33 PDT, Myles C. Maxfield
no flags
Patch (12.78 KB, patch)
2016-08-17 01:57 PDT, Myles C. Maxfield
no flags
Patch (12.80 KB, patch)
2016-08-17 03:16 PDT, Myles C. Maxfield
no flags
Patch (14.87 KB, patch)
2016-08-17 09:23 PDT, Myles C. Maxfield
no flags
Patch (16.73 KB, patch)
2016-08-17 13:30 PDT, Myles C. Maxfield
dino: review+
Patch for committing (16.69 KB, patch)
2016-08-17 22:19 PDT, Myles C. Maxfield
no flags
Myles C. Maxfield
Comment 1 2016-08-16 00:58:23 PDT
Sam Weinig
Comment 2 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.
Myles C. Maxfield
Comment 3 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.
Myles C. Maxfield
Comment 4 2016-08-16 19:33:46 PDT
Myles C. Maxfield
Comment 5 2016-08-17 01:57:25 PDT
Myles C. Maxfield
Comment 6 2016-08-17 03:16:45 PDT
Myles C. Maxfield
Comment 7 2016-08-17 09:23:10 PDT
Myles C. Maxfield
Comment 8 2016-08-17 13:30:12 PDT
Dean Jackson
Comment 9 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?
Myles C. Maxfield
Comment 10 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.
Myles C. Maxfield
Comment 11 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.
Myles C. Maxfield
Comment 12 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
Myles C. Maxfield
Comment 13 2016-08-17 22:19:10 PDT
Created attachment 286363 [details] Patch for committing
WebKit Commit Bot
Comment 14 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>
Csaba Osztrogonác
Comment 15 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.
Darin Adler
Comment 16 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?
Myles C. Maxfield
Comment 17 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.
Myles C. Maxfield
Comment 18 2016-08-19 15:14:41 PDT
Note You need to log in before you can comment on or make changes to this bug.