Cocoa: make text subpixel rendering check to work for non-Apple displays
authorTeemu Katajisto <teemu.katajisto@digia.com>
Mon, 29 Oct 2012 10:54:17 +0000 (12:54 +0200)
committerThe Qt Project <gerrit-noreply@qt-project.org>
Fri, 2 Nov 2012 23:24:37 +0000 (00:24 +0100)
Task-number: QTBUG-27386

Change-Id: I4e12663f80060dfcea6970a705861af388d816ac
Reviewed-by: Morten Johan Sørvig <morten.sorvig@digia.com>
src/platformsupport/fontdatabases/mac/qcoretextfontdatabase.mm

index 66ca2d3..380f5ca 100644 (file)
@@ -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