Cocoa: make text subpixel rendering check to work for non-Apple displays
Task-number: QTBUG-27386 Change-Id: I4e12663f80060dfcea6970a705861af388d816ac Reviewed-by: Morten Johan Sørvig <morten.sorvig@digia.com>
This commit is contained in:
parent
e5ac4fb9b2
commit
efc9b77a6e
@ -137,22 +137,44 @@ QCoreTextFontDatabase::QCoreTextFontDatabase()
|
|||||||
if (appleValue.isValid()) {
|
if (appleValue.isValid()) {
|
||||||
font_smoothing = appleValue.toInt();
|
font_smoothing = appleValue.toInt();
|
||||||
} else {
|
} else {
|
||||||
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
// 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)];
|
||||||
|
|
||||||
// find the primary display (which is always the first NSScreen
|
[fontImage lockFocus];
|
||||||
// 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
|
[[NSColor whiteColor] setFill];
|
||||||
// layout of the primary display
|
NSRectFill(rect);
|
||||||
NSDictionary *d = (NSDictionary *) IODisplayCreateInfoDictionary(iodisplay, kIODisplayOnlyPreferredName);
|
|
||||||
uint displaySubpixelLayout = [[d objectForKey:@kDisplaySubPixelLayout] unsignedIntValue];
|
|
||||||
[d release];
|
|
||||||
font_smoothing = (displaySubpixelLayout == kDisplaySubPixelLayoutUndefined ? 0 : 1);
|
|
||||||
|
|
||||||
[pool release];
|
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
|
QCoreTextFontEngine::defaultGlyphFormat = (font_smoothing > 0
|
||||||
? QFontEngineGlyphCache::Raster_RGBMask
|
? QFontEngineGlyphCache::Raster_RGBMask
|
||||||
|
Loading…
x
Reference in New Issue
Block a user