]> granicus.if.org Git - handbrake/commitdiff
MacGui: fix the queue toolbar icon badge quality on hi-dpi screens, and try to better...
authorDamiano Galassi <damiog@gmail.com>
Mon, 17 Oct 2016 17:10:56 +0000 (19:10 +0200)
committerDamiano Galassi <damiog@gmail.com>
Mon, 17 Oct 2016 17:10:56 +0000 (19:10 +0200)
macosx/HBToolbarBadgedItem.m

index e1eec20663df25c994669de4b5b0430afc22ab8b..f87e523f835af10b33efcd26b1003ad8723b44c2 100644 (file)
     return _cache;
 }
 
-- (NSImage *)HB_renderImage:(NSImage *)image withBadge:(NSString *)badge
+- (NSImage *)HB_renderImage:(NSImage *)image withBadge:(NSString *)badgeString
 {
     NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
-    paragraphStyle.minimumLineHeight = 0.0f;
 
     NSImage *newImage = [[NSImage alloc] initWithSize:image.size];
     for (NSImageRep *rep in image.representations)
     {
-        NSSize size = rep.size;
+        NSSize size = NSMakeSize(rep.pixelsWide, rep.pixelsHigh);
         NSBitmapImageRep *newRep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL
                                                                            pixelsWide:(NSInteger)floor(size.width)
                                                                            pixelsHigh:(NSInteger)floor(size.height)
         CGContextDrawImage(context, imageRect, ref);
 
         // Work out the area
-        CGFloat iconsize = size.width * 0.5f;
-        CGFloat radius = iconsize * 0.5f;
-        NSPoint indent = NSMakePoint(10, 2);
+        CGFloat scaleFactor = rep.pixelsWide / rep.size.width;
+        CGFloat pointSize = 10 * scaleFactor;
 
-        NSFont *font = [NSFont boldSystemFontOfSize:10];
+        NSFont *font = [NSFont boldSystemFontOfSize:pointSize];
         NSDictionary *attr = @{NSParagraphStyleAttributeName : paragraphStyle,
-                     NSFontAttributeName : font,
-                     NSForegroundColorAttributeName : _badgeTextColor };
+                               NSFontAttributeName : font,
+                               NSForegroundColorAttributeName : _badgeTextColor };
 
-        NSRect textSize = [badge boundingRectWithSize:NSZeroSize options:NSStringDrawingOneShot attributes:attr];
-        NSRect badgeRect = NSMakeRect(size.width - textSize.size.width - indent.x, size.height - textSize.size.height - indent.y,
-                                      textSize.size.width + indent.x, textSize.size.height + indent.y);
+        NSRect textBounds = [badgeString boundingRectWithSize:NSZeroSize
+                                                      options:0
+                                                   attributes:attr];
+
+        NSPoint indent = NSMakePoint(10 * scaleFactor, 2 * scaleFactor);
+        CGFloat radius = (textBounds.size.height + indent.y) * 0.5f;
+
+        NSRect badgeRect = NSMakeRect(size.width - textBounds.size.width - indent.x, size.height - textBounds.size.height - indent.y,
+                                      textBounds.size.width + indent.x, textBounds.size.height + indent.y);
+        badgeRect = NSIntegralRect(badgeRect);
 
         // Draw the ellipse
         CGFloat minx = CGRectGetMinX(badgeRect);
         CGContextDrawPath(context, kCGPathFill);
 
         // Draw the text
-        NSRect textBounds = [badge boundingRectWithSize:NSZeroSize
-                                                options:NSStringDrawingUsesDeviceMetrics
-                                             attributes:attr];
-
-        badgeRect.origin.x = CGRectGetMidX(badgeRect) - (textSize.size.width * 0.5f);
-        badgeRect.origin.x -= (textBounds.size.width - textSize.size.width) * 0.5f;
+        badgeRect.origin.x = CGRectGetMidX(badgeRect) - (textBounds.size.width * 0.5f);
+        badgeRect.origin.x -= (textBounds.size.width - textBounds.size.width) * 0.5f;
         badgeRect.origin.y = CGRectGetMidY(badgeRect);
-        badgeRect.origin.y -= textBounds.origin.y;
-        badgeRect.origin.y -= ((textBounds.size.height - textSize.origin.y) * 0.5f);
+        badgeRect.origin.y -= textBounds.origin.y / 2;
+        badgeRect.origin.y -= ((textBounds.size.height - textBounds.origin.y) * 0.5f);
+
+        badgeRect.origin.y = floor(badgeRect.origin.y);
+        badgeRect.origin.x = ceil(badgeRect.origin.x);
+        badgeRect.size.height = textBounds.size.height;
+        badgeRect.size.width = textBounds.size.width;
 
-        badgeRect.size.height = textSize.size.height;
-        badgeRect.size.width = textSize.size.width;
-        [badge drawInRect:badgeRect withAttributes:attr];
+        [badgeString drawInRect:badgeRect withAttributes:attr];
 
         CGContextRestoreGState(context);