From efc9b77a6e9a0efe0c260a4a7fe18bf3f87a8f13 Mon Sep 17 00:00:00 2001 From: Teemu Katajisto Date: Mon, 29 Oct 2012 12:54:17 +0200 Subject: Cocoa: make text subpixel rendering check to work for non-Apple displays MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Task-number: QTBUG-27386 Change-Id: I4e12663f80060dfcea6970a705861af388d816ac Reviewed-by: Morten Johan Sørvig --- .../fontdatabases/mac/qcoretextfontdatabase.mm | 54 +++++++++++++++------- 1 file changed, 38 insertions(+), 16 deletions(-) (limited to 'src/platformsupport/fontdatabases/mac') diff --git a/src/platformsupport/fontdatabases/mac/qcoretextfontdatabase.mm b/src/platformsupport/fontdatabases/mac/qcoretextfontdatabase.mm index 66ca2d37fa..380f5ca8fd 100644 --- a/src/platformsupport/fontdatabases/mac/qcoretextfontdatabase.mm +++ b/src/platformsupport/fontdatabases/mac/qcoretextfontdatabase.mm @@ -137,22 +137,44 @@ QCoreTextFontDatabase::QCoreTextFontDatabase() if (appleValue.isValid()) { font_smoothing = appleValue.toInt(); } else { - NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; - - // find the primary display (which is always the first NSScreen - // according to the documentation) - NSScreen *defaultScreen = [[NSScreen screens] objectAtIndex:0]; - CGDirectDisplayID displayId = [[[defaultScreen deviceDescription] objectForKey:@"NSScreenNumber"] unsignedIntValue]; - io_service_t iodisplay = CGDisplayIOServicePort(displayId); - - // determine if font smoothing is available based on the subpixel - // layout of the primary display - NSDictionary *d = (NSDictionary *) IODisplayCreateInfoDictionary(iodisplay, kIODisplayOnlyPreferredName); - uint displaySubpixelLayout = [[d objectForKey:@kDisplaySubPixelLayout] unsignedIntValue]; - [d release]; - font_smoothing = (displaySubpixelLayout == kDisplaySubPixelLayoutUndefined ? 0 : 1); - - [pool release]; + // non-Apple displays do not provide enough information about subpixel rendering so + // draw text with cocoa and compare pixel colors to see if subpixel rendering is enabled + int w = 10; + int h = 10; + NSRect rect = NSMakeRect(0.0, 0.0, w, h); + NSImage *fontImage = [[NSImage alloc] initWithSize:NSMakeSize(w, h)]; + + [fontImage lockFocus]; + + [[NSColor whiteColor] setFill]; + NSRectFill(rect); + + NSString *str = @"X\\"; + NSFont *font = [NSFont fontWithName:@"Helvetica" size:10.0]; + NSMutableDictionary *attrs = [NSMutableDictionary dictionary]; + [attrs setObject:font forKey:NSFontAttributeName]; + [attrs setObject:[NSColor blackColor] forKey:NSForegroundColorAttributeName]; + + [str drawInRect:rect withAttributes:attrs]; + + NSBitmapImageRep *nsBitmapImage = [[NSBitmapImageRep alloc] initWithFocusedViewRect:rect]; + + [fontImage unlockFocus]; + + float red, green, blue; + for (int x = 0; x < w; x++) { + for (int y = 0; y < h; y++) { + NSColor *pixelColor = [nsBitmapImage colorAtX:x y:y]; + red = [pixelColor redComponent]; + green = [pixelColor greenComponent]; + blue = [pixelColor blueComponent]; + if (red != green || red != blue) + font_smoothing = 1; + } + } + + [nsBitmapImage release]; + [fontImage release]; } QCoreTextFontEngine::defaultGlyphFormat = (font_smoothing > 0 ? QFontEngineGlyphCache::Raster_RGBMask -- cgit v1.2.3