Commit 88a91ee3 authored by tapted@chromium.org's avatar tapted@chromium.org

Create app shims with 32-bit icons only, as per OSX 10.5 spec.

This removes third_party/IconFamily from Chrome, which creates
questionable results when adding icon sizes < 128px. Since OSX 10.5, OSX
hasn't used indexed icons or alpha masks when a 32-bit icon is available
(see notes in OSServices.framework/IconStorage.h).

BUG=241304
TEST=Create an app shim and inspect Resources/app.icns using OSX Preview
-- ensure the icons are correct.
TBR=darin@chromium.org

Review URL: https://chromiumcodereview.appspot.com/15650005

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@203050 0039d316-1c4b-4281-b951-d872f2087c98
parent dfbf8278
...@@ -43,7 +43,6 @@ include_rules = [ ...@@ -43,7 +43,6 @@ include_rules = [
# Allow inclusion of third-party code: # Allow inclusion of third-party code:
"+third_party/hunspell", "+third_party/hunspell",
"+third_party/icon_family", # IconFamily for Mac.
"+third_party/libxml", "+third_party/libxml",
"+third_party/mozilla", # Mozilla interface headers. "+third_party/mozilla", # Mozilla interface headers.
"+third_party/npapi", # NPAPI interface headers. "+third_party/npapi", # NPAPI interface headers.
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#import "chrome/browser/web_applications/web_app_mac.h" #import "chrome/browser/web_applications/web_app_mac.h"
#import <Carbon/Carbon.h>
#import <Cocoa/Cocoa.h> #import <Cocoa/Cocoa.h>
#include "base/file_util.h" #include "base/file_util.h"
...@@ -22,67 +23,98 @@ ...@@ -22,67 +23,98 @@
#include "content/public/browser/browser_thread.h" #include "content/public/browser/browser_thread.h"
#include "grit/chromium_strings.h" #include "grit/chromium_strings.h"
#include "skia/ext/skia_utils_mac.h" #include "skia/ext/skia_utils_mac.h"
#include "third_party/icon_family/IconFamily.h" #include "third_party/skia/include/core/SkBitmap.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/base/l10n/l10n_util_mac.h" #include "ui/base/l10n/l10n_util_mac.h"
#include "ui/gfx/image/image_family.h" #include "ui/gfx/image/image_family.h"
namespace { namespace {
// Get the 100% image representation for |image|. class ScopedCarbonHandle {
// This returns the representation with the same width and height as |image| public:
// itself. If there is no such representation, returns nil. ScopedCarbonHandle(size_t initial_size) : handle_(NewHandle(initial_size)) {
NSBitmapImageRep* NSImageGet100PRepresentation(NSImage* image) { DCHECK(handle_);
NSSize image_size = [image size]; DCHECK_EQ(noErr, MemError());
for (NSBitmapImageRep* image_rep in [image representations]) { }
NSSize image_rep_size = [image_rep size]; ~ScopedCarbonHandle() { DisposeHandle(handle_); }
if (image_rep_size.width == image_size.width &&
image_rep_size.height == image_size.height) { Handle Get() { return handle_; }
return image_rep; char* Data() { return *handle_; }
size_t HandleSize() const { return GetHandleSize(handle_); }
IconFamilyHandle GetAsIconFamilyHandle() {
return reinterpret_cast<IconFamilyHandle>(handle_);
}
bool WriteDataToFile(const base::FilePath& path) {
NSData* data = [NSData dataWithBytes:Data()
length:HandleSize()];
return [data writeToFile:base::mac::FilePathToNSString(path)
atomically:NO];
}
private:
Handle handle_;
};
void ConvertSkiaToARGB(const SkBitmap& bitmap, ScopedCarbonHandle* handle) {
CHECK_EQ(4u * bitmap.width() * bitmap.height(), handle->HandleSize());
char* argb = handle->Data();
SkAutoLockPixels lock(bitmap);
for (int y = 0; y < bitmap.height(); ++y) {
for (int x = 0; x < bitmap.width(); ++x) {
SkColor pixel = bitmap.getColor(x, y);
argb[0] = SkColorGetA(pixel);
argb[1] = SkColorGetR(pixel);
argb[2] = SkColorGetG(pixel);
argb[3] = SkColorGetB(pixel);
argb += 4;
} }
} }
return nil;
} }
// Adds |image_rep| to |icon_family|. Returns true on success, false on failure. // Adds |image| to |icon_family|. Returns true on success, false on failure.
bool AddBitmapImageRepToIconFamily(IconFamily* icon_family, bool AddGfxImageToIconFamily(IconFamilyHandle icon_family,
NSBitmapImageRep* image_rep) { const gfx::Image& image) {
NSSize size = [image_rep size]; // When called via ShowCreateChromeAppShortcutsDialog the ImageFamily will
if (size.width != size.height) // have all the representations desired here for mac, from the kDesiredSizes
// array in web_app_ui.cc.
SkBitmap bitmap = image.AsBitmap();
if (bitmap.config() != SkBitmap::kARGB_8888_Config ||
bitmap.width() != bitmap.height()) {
return false; return false;
}
switch (static_cast<int>(size.width)) { OSType icon_type;
switch (bitmap.width()) {
case 512: case 512:
return [icon_family setIconFamilyElement:kIconServices512PixelDataARGB icon_type = kIconServices512PixelDataARGB;
fromBitmapImageRep:image_rep]; break;
case 256: case 256:
return [icon_family setIconFamilyElement:kIconServices256PixelDataARGB icon_type = kIconServices256PixelDataARGB;
fromBitmapImageRep:image_rep]; break;
case 128: case 128:
return [icon_family setIconFamilyElement:kThumbnail32BitData icon_type = kIconServices128PixelDataARGB;
fromBitmapImageRep:image_rep] && break;
[icon_family setIconFamilyElement:kThumbnail8BitMask case 48:
fromBitmapImageRep:image_rep]; icon_type = kIconServices48PixelDataARGB;
break;
case 32: case 32:
return [icon_family setIconFamilyElement:kLarge32BitData icon_type = kIconServices32PixelDataARGB;
fromBitmapImageRep:image_rep] && break;
[icon_family setIconFamilyElement:kLarge8BitData
fromBitmapImageRep:image_rep] &&
[icon_family setIconFamilyElement:kLarge8BitMask
fromBitmapImageRep:image_rep] &&
[icon_family setIconFamilyElement:kLarge1BitMask
fromBitmapImageRep:image_rep];
case 16: case 16:
return [icon_family setIconFamilyElement:kSmall32BitData icon_type = kIconServices16PixelDataARGB;
fromBitmapImageRep:image_rep] && break;
[icon_family setIconFamilyElement:kSmall8BitData
fromBitmapImageRep:image_rep] &&
[icon_family setIconFamilyElement:kSmall8BitMask
fromBitmapImageRep:image_rep] &&
[icon_family setIconFamilyElement:kSmall1BitMask
fromBitmapImageRep:image_rep];
default: default:
return false; return false;
} }
ScopedCarbonHandle raw_data(bitmap.getSize());
ConvertSkiaToARGB(bitmap, &raw_data);
OSErr result = SetIconFamilyData(icon_family, icon_type, raw_data.Get());
DCHECK_EQ(noErr, result);
return result == noErr;
} }
base::FilePath GetWritableApplicationsDirectory() { base::FilePath GetWritableApplicationsDirectory() {
...@@ -98,7 +130,6 @@ base::FilePath GetWritableApplicationsDirectory() { ...@@ -98,7 +130,6 @@ base::FilePath GetWritableApplicationsDirectory() {
} // namespace } // namespace
namespace web_app { namespace web_app {
const char kChromeAppDirName[] = "Chrome Apps.localized"; const char kChromeAppDirName[] = "Chrome Apps.localized";
...@@ -237,19 +268,16 @@ bool WebAppShortcutCreator::UpdateIcon(const base::FilePath& app_path) const { ...@@ -237,19 +268,16 @@ bool WebAppShortcutCreator::UpdateIcon(const base::FilePath& app_path) const {
if (info_.favicon.empty()) if (info_.favicon.empty())
return true; return true;
scoped_nsobject<IconFamily> icon_family([[IconFamily alloc] init]); ScopedCarbonHandle icon_family(0);
bool image_added = false; bool image_added = false;
for (gfx::ImageFamily::const_iterator it = info_.favicon.begin(); for (gfx::ImageFamily::const_iterator it = info_.favicon.begin();
it != info_.favicon.end(); ++it) { it != info_.favicon.end(); ++it) {
if (it->IsEmpty()) if (it->IsEmpty())
continue; continue;
NSBitmapImageRep* image_rep = NSImageGet100PRepresentation(it->ToNSImage());
if (!image_rep)
continue;
// Missing an icon size is not fatal so don't fail if adding the bitmap // Missing an icon size is not fatal so don't fail if adding the bitmap
// doesn't work. // doesn't work.
if (!AddBitmapImageRepToIconFamily(icon_family, image_rep)) if (!AddGfxImageToIconFamily(icon_family.GetAsIconFamilyHandle(), *it))
continue; continue;
image_added = true; image_added = true;
...@@ -262,8 +290,8 @@ bool WebAppShortcutCreator::UpdateIcon(const base::FilePath& app_path) const { ...@@ -262,8 +290,8 @@ bool WebAppShortcutCreator::UpdateIcon(const base::FilePath& app_path) const {
app_path.Append("Contents").Append("Resources"); app_path.Append("Contents").Append("Resources");
if (!file_util::CreateDirectory(resources_path)) if (!file_util::CreateDirectory(resources_path))
return false; return false;
base::FilePath icon_path = resources_path.Append("app.icns");
return [icon_family writeToFile:base::mac::FilePathToNSString(icon_path)]; return icon_family.WriteDataToFile(resources_path.Append("app.icns"));
} }
NSString* WebAppShortcutCreator::GetBundleIdentifier(NSDictionary* plist) const NSString* WebAppShortcutCreator::GetBundleIdentifier(NSDictionary* plist) const
......
...@@ -2937,9 +2937,6 @@ ...@@ -2937,9 +2937,6 @@
'$(SDKROOT)/System/Library/Frameworks/SecurityInterface.framework', '$(SDKROOT)/System/Library/Frameworks/SecurityInterface.framework',
], ],
}, },
'dependencies': [
'../third_party/icon_family/icon_family.gyp:icon_family',
],
'sources': [ 'sources': [
# Build the necessary GTM sources # Build the necessary GTM sources
'../third_party/GTM/Foundation/GTMServiceManagement.h', '../third_party/GTM/Foundation/GTMServiceManagement.h',
......
// IconFamily.h
// IconFamily class interface
// by Troy Stephens, Thomas Schnitzer, David Remahl, Nathan Day, Ben Haller, Sven Janssen, Peter Hosey, Conor Dearden, Elliot Glaysher, and Dave MacLachlan
// version 0.9.4
//
// Project Home Page:
// http://iconfamily.sourceforge.net/
//
// Problems, shortcomings, and uncertainties that I'm aware of are flagged with "NOTE:". Please address bug reports, bug fixes, suggestions, etc. to the project Forums and bug tracker at https://sourceforge.net/projects/iconfamily/
/*
Copyright (c) 2001-2010 Troy N. Stephens
Portions Copyright (c) 2007 Google Inc.
Use and distribution of this source code is governed by the MIT License, whose terms are as follows.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#import <Cocoa/Cocoa.h>
#import <Carbon/Carbon.h>
// This class is a Cocoa/Objective-C wrapper for the Mac OS X Carbon API's
// "icon family" data type. Its main purpose is to enable Cocoa applications
// to easily create custom file icons from NSImage instances, and thus take
// advantage of Mac OS X's new larger RGBA "thumbnail" icon format to provide
// richly detailed thumbnail previews of the files' contents.
//
// Using IconFamily, this becomes as simple as:
//
// id iconFamily = [IconFamily iconFamilyWithThumbnailsOfImage:anImage];
// [iconFamily setAsCustomIconForFile:anExistingFile];
//
// You can also write an icon family to an .icns file using the -writeToFile:
// method.
@interface IconFamily : NSObject
{
IconFamilyHandle hIconFamily;
}
// Convenience methods. These use the corresponding -init... methods to return
// an autoreleased IconFamily instance.
+ (IconFamily*) iconFamily;
+ (IconFamily*) iconFamilyWithContentsOfFile:(NSString*)path;
+ (IconFamily*) iconFamilyWithIconOfFile:(NSString*)path;
+ (IconFamily*) iconFamilyWithIconFamilyHandle:(IconFamilyHandle)hNewIconFamily;
+ (IconFamily*) iconFamilyWithSystemIcon:(int)fourByteCode;
+ (IconFamily*) iconFamilyWithThumbnailsOfImage:(NSImage*)image;
+ (IconFamily*) iconFamilyWithThumbnailsOfImage:(NSImage*)image usingImageInterpolation:(NSImageInterpolation)imageInterpolation;
// Initializes as a new, empty IconFamily. This is IconFamily's designated
// initializer method.
- (id) init;
// Initializes an IconFamily by loading the contents of an .icns file.
- (id) initWithContentsOfFile:(NSString*)path;
// Initializes an IconFamily from an existing Carbon IconFamilyHandle.
- (id) initWithIconFamilyHandle:(IconFamilyHandle)hNewIconFamily;
// Initializes an IconFamily by loading the Finder icon that's assigned to a
// file.
- (id) initWithIconOfFile:(NSString*)path;
// Initializes an IconFamily by referencing a standard system icon.
- (id) initWithSystemIcon:(int)fourByteCode;
// Initializes an IconFamily by creating its elements from a resampled
// NSImage. The second form of this method allows you to specify the degree
// of antialiasing to be used in resampling the image, by passing in one of
// the NSImageInterpolation... constants that are defined in
// NSGraphicsContext.h. The first form of this initializer simply calls the
// second form with imageInterpolation set to NSImageInterpolationHigh, which
// produces highly smoothed thumbnails.
- (id) initWithThumbnailsOfImage:(NSImage*)image;
- (id) initWithThumbnailsOfImage:(NSImage*)image usingImageInterpolation:(NSImageInterpolation)imageInterpolation;
// Writes the icon family to an .icns file.
- (BOOL) writeToFile:(NSString*)path;
// Sets the image data for one of the icon family's elements from an
// NSBitmapImageRep. The "elementType" parameter must be one of the icon
// family element types listed below, and the format of the "bitmapImageRep"
// must match the corresponding requirements specified below. Regardless of
// the elementType, the bitmapImageRep must also be non-planar and have 8 bits
// per sample.
//
// elementType dimensions format
// ------------------- ---------- ---------------------------------------
// kIconServices512PixelDataARGB 512 x 512 32-bit RGBA, 32-bit RGB, or 24-bit RGB
// kIconServices256PixelDataARGB 256 x 256 32-bit RGBA, 32-bit RGB, or 24-bit RGB
// kThumbnail32BitData 128 x 128 32-bit RGBA, 32-bit RGB, or 24-bit RGB
// kThumbnail8BitMask 128 x 128 32-bit RGBA or 8-bit intensity
// kLarge32BitData 32 x 32 32-bit RGBA, 32-bit RGB, or 24-bit RGB
// kLarge8BitMask 32 x 32 32-bit RGBA or 8-bit intensity
// kLarge1BitMask 32 x 32 32-bit RGBA, 8-bit intensity, or 1-bit
// kSmall32BitData 16 x 16 32-bit RGBA, 32-bit RGB, or 24-bit RGB
// kSmall8BitMask 16 x 16 32-bit RGBA or 8-bit intensity
// kSmall1BitMask 16 x 16 32-bit RGBA, 8-bit intensity, or 1-bit
//
// When an RGBA image is supplied to set a "Mask" element, the mask data is
// taken from the image's alpha channel.
//
// NOTE: Setting an IconFamily's kLarge1BitMask seems to damage the IconFamily
// for some as yet unknown reason. (If you then assign the icon family
// as a file's custom icon using -setAsCustomIconForFile:, the custom
// icon doesn't appear for the file in the Finder.) However, both
// custom icon display and mouse-click hit-testing in the Finder seem to
// work fine when we only set the other four elements (thus keeping the
// existing kLarge1BitMask from the valid icon family from which we
// initialized the IconFamily via -initWithContentsOfFile:, since
// IconFamily's -init method is currently broken...), so it seems safe
// to just leave the kLarge1BitMask alone.
- (BOOL) setIconFamilyElement:(OSType)elementType
fromBitmapImageRep:(NSBitmapImageRep*)bitmapImageRep;
// Gets the image data for one of the icon family's elements as a new, 32-bit
// RGBA NSBitmapImageRep. The specified elementType should be one of
// kIconServices512PixelDataARGB, kIconServices256PixelDataARGB,
// kThumbnail32BitData, kLarge32BitData, or kSmall32BitData.
//
// The returned NSBitmapImageRep will have the corresponding 8-bit mask data
// in its alpha channel, or a fully opaque alpha channel if the icon family
// has no 8-bit mask data for the specified alpha channel.
//
// Returns nil if the requested element cannot be retrieved (e.g. if the
// icon family has no such 32BitData element).
- (NSBitmapImageRep*) bitmapImageRepWithAlphaForIconFamilyElement:(OSType)elementType;
// Creates and returns an NSImage that contains the icon family's various
// elements as its NSImageReps.
- (NSImage*) imageWithAllReps;
#if !defined(DISABLE_CUSTOM_ICON)
// NOTE: Planned method -- not yet implemented.
//
// Gets the image data for one of the icon family's elements as a new
// NSBitmapImageRep. The specified elementType should be one of
// kThumbnail32BitData, kThumbnail32BitMask, kLarge32BitData, kLarge8BitMask,
// kLarge1BitMask, kSmall32BitData, kSmall8BitMask, or kSmall1BitMask.
// - (NSBitmapImageRep*) bitmapImageRepForIconFamilyElement:(OSType)elementType;
// Writes the icon family to the resource fork of the specified file as its
// kCustomIconResource, and sets the necessary Finder bits so the icon will
// be displayed for the file in Finder views.
- (BOOL) setAsCustomIconForFile:(NSString*)path;
- (BOOL) setAsCustomIconForFile:(NSString*)path withCompatibility:(BOOL)compat;
// Same as the -setAsCustomIconForFile:... methods, but for folders (directories).
- (BOOL) setAsCustomIconForDirectory:(NSString*)path;
- (BOOL) setAsCustomIconForDirectory:(NSString*)path withCompatibility:(BOOL)compat;
// Removes the custom icon (if any) from the specified file's resource fork,
// and clears the necessary Finder bits for the file. (Note that this is a
// class method, so you don't need an instance of IconFamily to invoke it.)
+ (BOOL) removeCustomIconFromFile:(NSString*)path;
//Same as the -removeCustomIconFromFile: method, but for folders (directories).
+ (BOOL) removeCustomIconFromDirectory:(NSString*)path;
#endif // !defined(DISABLE_CUSTOM_ICON)
@end
// Methods for interfacing with the Carbon Scrap Manager (analogous to and
// interoperable with the Cocoa Pasteboard).
@interface IconFamily (ScrapAdditions)
+ (BOOL) canInitWithScrap;
+ (IconFamily*) iconFamilyWithScrap;
- (id) initWithScrap;
- (BOOL) putOnScrap;
@end
This diff is collapsed.
Copyright (c) 2001-2010 Troy N. Stephens
Portions Copyright (c) 2007 Google Inc.
Use and distribution of this source code is governed by the MIT License, whose terms are as follows.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
/*
Copyright (c) 2001-2010 Troy N. Stephens
Use and distribution of this source code is governed by the MIT License, whose terms are as follows.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#import <Foundation/Foundation.h>
#import <Carbon/Carbon.h>
@interface NSString (CarbonFSRefCreation)
// Fills in the given FSRef struct so it specifies the file whose path is in this string.
// If the file doesn't exist, and "createFile" is YES, this method will attempt to create
// an empty file with the specified path. (The caller should insure that the directory
// the file is to be placed in already exists.)
- (BOOL) getFSRef:(FSRef*)fsRef createFileIfNecessary:(BOOL)createFile;
@end
/*
Copyright (c) 2001-2010 Troy N. Stephens
Use and distribution of this source code is governed by the MIT License, whose terms are as follows.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#import "NSString+CarbonFSRefCreation.h"
@implementation NSString (CarbonFSRefCreation)
- (BOOL) getFSRef:(FSRef*)fsRef createFileIfNecessary:(BOOL)createFile
{
NSFileManager* fileManager = [NSFileManager defaultManager];
CFURLRef urlRef;
Boolean gotFSRef;
// Check whether the file exists already. If not, create an empty file if requested.
if (![fileManager fileExistsAtPath:self]) {
if (createFile) {
if (![(NSData*)[NSData data] writeToFile:self atomically:YES]) {
return NO;
}
} else {
return NO;
}
}
// Create a CFURL with the specified POSIX path.
urlRef = CFURLCreateWithFileSystemPath( kCFAllocatorDefault,
(CFStringRef) self,
kCFURLPOSIXPathStyle,
FALSE /* isDirectory */ );
if (urlRef == NULL) {
// printf( "** Couldn't make a CFURLRef for the file.\n" );
return NO;
}
// Try to create an FSRef from the URL. (If the specified file doesn't exist, this
// function will return false, but if we've reached this code we've already insured
// that the file exists.)
gotFSRef = CFURLGetFSRef( urlRef, fsRef );
CFRelease( urlRef );
if (!gotFSRef) {
// printf( "** Couldn't get an FSRef for the file.\n" );
return NO;
}
return YES;
}
@end
Name: icon_family
Short Name: IconFamily
URL: http://iconfamily.sourceforge.net/
Version: 0
Date: 07/21/2010
Revision: 46
License: MIT
Security Critical: Yes
Description:
This is an Objective-C wrapper around Mac OS X Icon Services' "IconFamily" data type. This is used to create .icns files for Web Apps installed from the browser.
Local Modifications:
chromium_icon_family.patch: Fix minor erors and warnings. Put code that the custom icon code behind a DISABLE_CUSTOM_ICON flag.
chromium_icon_family_2.patch: Add support for alpha first and non-premultiplied image formats. Patch submitted to project page: https://sourceforge.net/tracker/?func=detail&aid=3491306&group_id=164783&atid=833111
diff --git a/third_party/icon_family/IconFamily.h b/third_party/icon_family/IconFamily.h
index 6a6049f..63f6bb7 100644
--- a/third_party/icon_family/IconFamily.h
+++ b/third_party/icon_family/IconFamily.h
@@ -57,24 +57,24 @@
// Initializes as a new, empty IconFamily. This is IconFamily's designated
// initializer method.
-- init;
+- (id) init;
// Initializes an IconFamily by loading the contents of an .icns file.
-- initWithContentsOfFile:(NSString*)path;
+- (id) initWithContentsOfFile:(NSString*)path;
// Initializes an IconFamily from an existing Carbon IconFamilyHandle.
-- initWithIconFamilyHandle:(IconFamilyHandle)hNewIconFamily;
+- (id) initWithIconFamilyHandle:(IconFamilyHandle)hNewIconFamily;
// Initializes an IconFamily by loading the Finder icon that's assigned to a
// file.
-- initWithIconOfFile:(NSString*)path;
+- (id) initWithIconOfFile:(NSString*)path;
// Initializes an IconFamily by referencing a standard system icon.
-- initWithSystemIcon:(int)fourByteCode;
+- (id) initWithSystemIcon:(int)fourByteCode;
// Initializes an IconFamily by creating its elements from a resampled
// NSImage. The second form of this method allows you to specify the degree
@@ -84,8 +84,8 @@
// second form with imageInterpolation set to NSImageInterpolationHigh, which
// produces highly smoothed thumbnails.
-- initWithThumbnailsOfImage:(NSImage*)image;
-- initWithThumbnailsOfImage:(NSImage*)image usingImageInterpolation:(NSImageInterpolation)imageInterpolation;
+- (id) initWithThumbnailsOfImage:(NSImage*)image;
+- (id) initWithThumbnailsOfImage:(NSImage*)image usingImageInterpolation:(NSImageInterpolation)imageInterpolation;
// Writes the icon family to an .icns file.
@@ -147,6 +147,8 @@
- (NSImage*) imageWithAllReps;
+#if !defined(DISABLE_CUSTOM_ICON)
+
// NOTE: Planned method -- not yet implemented.
//
// Gets the image data for one of the icon family's elements as a new
@@ -178,6 +180,8 @@
+ (BOOL) removeCustomIconFromDirectory:(NSString*)path;
+#endif // !defined(DISABLE_CUSTOM_ICON)
+
@end
// Methods for interfacing with the Carbon Scrap Manager (analogous to and
@@ -185,6 +189,6 @@
@interface IconFamily (ScrapAdditions)
+ (BOOL) canInitWithScrap;
+ (IconFamily*) iconFamilyWithScrap;
-- initWithScrap;
+- (id) initWithScrap;
- (BOOL) putOnScrap;
@end
diff --git a/third_party/icon_family/IconFamily.m b/third_party/icon_family/IconFamily.m
index b9571d0..439c2de 100644
--- a/third_party/icon_family/IconFamily.m
+++ b/third_party/icon_family/IconFamily.m
@@ -91,7 +91,9 @@ enum {
+ (Handle) get1BitMaskFromBitmapImageRep:(NSBitmapImageRep*)bitmapImageRep requiredPixelSize:(int)requiredPixelSize;
+#if !defined(DISABLE_CUSTOM_ICON)
- (BOOL) addResourceType:(OSType)type asResID:(int)resID;
+#endif
@end
@@ -135,7 +137,7 @@ enum {
// This is IconFamily's designated initializer. It creates a new IconFamily that initially has no elements.
//
// The proper way to do this is to simply allocate a zero-sized handle (not to be confused with an empty handle) and assign it to hIconFamily. This technique works on Mac OS X 10.2 as well as on 10.0.x and 10.1.x. Our previous technique of allocating an IconFamily struct with a resourceSize of 0 no longer works as of Mac OS X 10.2.
-- init
+- (id) init
{
self = [super init];
if (self) {
@@ -148,7 +150,7 @@ enum {
return self;
}
-- initWithData:(NSData *)data
+- (id) initWithData:(NSData *)data
{
self = [self init];
if (self) {
@@ -166,7 +168,7 @@ enum {
return self;
}
-- initWithContentsOfFile:(NSString*)path
+- (id) initWithContentsOfFile:(NSString*)path
{
FSRef ref;
OSStatus result;
@@ -190,7 +192,7 @@ enum {
return self;
}
-- initWithIconFamilyHandle:(IconFamilyHandle)hNewIconFamily
+- (id) initWithIconFamilyHandle:(IconFamilyHandle)hNewIconFamily
{
self = [self init];
if (self) {
@@ -203,7 +205,7 @@ enum {
return self;
}
-- initWithIconOfFile:(NSString*)path
+- (id) initWithIconOfFile:(NSString*)path
{
IconRef iconRef;
OSStatus result;
@@ -257,7 +259,7 @@ enum {
return self;
}
-- initWithSystemIcon:(int)fourByteCode
+- (id) initWithSystemIcon:(int)fourByteCode
{
IconRef iconRef;
OSErr result;
@@ -295,13 +297,13 @@ enum {
return self;
}
-- initWithThumbnailsOfImage:(NSImage*)image
+- (id) initWithThumbnailsOfImage:(NSImage*)image
{
// The default is to use a high degree of antialiasing, producing a smooth image.
return [self initWithThumbnailsOfImage:image usingImageInterpolation:NSImageInterpolationHigh];
}
-- initWithThumbnailsOfImage:(NSImage*)image usingImageInterpolation:(NSImageInterpolation)imageInterpolation
+- (id) initWithThumbnailsOfImage:(NSImage*)image usingImageInterpolation:(NSImageInterpolation)imageInterpolation
{
NSImage* iconImage512x512;
NSImage* iconImage256x256;
@@ -724,6 +726,8 @@ enum {
return YES;
}
+#if !defined(DISABLE_CUSTOM_ICON)
+
- (BOOL) setAsCustomIconForFile:(NSString*)path
{
return( [self setAsCustomIconForFile:path withCompatibility:NO] );
@@ -1139,6 +1143,8 @@ enum {
return YES;
}
+#endif // !defined(DISABLE_CUSTOM_ICON)
+
- (NSData *) data
{
return [NSData dataWithBytes:*hIconFamily length:GetHandleSize((Handle)hIconFamily)];
@@ -1589,6 +1595,8 @@ enum {
return hRawData;
}
+#if !defined(DISABLE_CUSTOM_ICON)
+
- (BOOL) addResourceType:(OSType)type asResID:(int)resID
{
Handle hIconRes = NewHandle(0);
@@ -1604,6 +1612,8 @@ enum {
return YES;
}
+#endif // !defined(DISABLE_CUSTOM_ICON)
+
@end
// Methods for interfacing with the Cocoa Pasteboard.
@@ -1621,7 +1631,7 @@ enum {
return [[[IconFamily alloc] initWithScrap] autorelease];
}
-- initWithScrap
+- (id) initWithScrap
{
NSPasteboard *pboard = [NSPasteboard generalPasteboard];
@@ -1702,7 +1712,7 @@ enum {
- (NSImageRep *) iconfamily_bestRepresentation
{
#if MAC_OS_X_VERSION_MIN_REQUIRED < 1060
- if ([!self respondsToSelector:@selector(bestRepresentationForRect:context:hints:)])
+ if (![self respondsToSelector:@selector(bestRepresentationForRect:context:hints:)])
{
return [self bestRepresentationForDevice:nil];
}
diff --git a/third_party/icon_family/NSString+CarbonFSRefCreation.m b/third_party/icon_family/NSString+CarbonFSRefCreation.m
index 723de8b..fb86c52 100644
--- a/third_party/icon_family/NSString+CarbonFSRefCreation.m
+++ b/third_party/icon_family/NSString+CarbonFSRefCreation.m
@@ -23,7 +23,7 @@
// Check whether the file exists already. If not, create an empty file if requested.
if (![fileManager fileExistsAtPath:self]) {
if (createFile) {
- if (![[NSData data] writeToFile:self atomically:YES]) {
+ if (![(NSData*)[NSData data] writeToFile:self atomically:YES]) {
return NO;
}
} else {
diff --git a/third_party/icon_family/IconFamily.m b/third_party/icon_family/IconFamily.m
index 439c2de..911ea31 100644
--- a/third_party/icon_family/IconFamily.m
+++ b/third_party/icon_family/IconFamily.m
@@ -1236,6 +1236,43 @@ enum {
return [newImage autorelease];
}
+void GetRGBAFrom32BitSource(unsigned char src1, unsigned char src2, unsigned char src3, unsigned char src4,
+ unsigned char* redOut, unsigned char* greenOut, unsigned char* blueOut, unsigned char* alphaOut,
+ bool isAlphaFirst, bool isAlphaPremultiplied) {
+ unsigned char r, g, b, a;
+ if (isAlphaFirst) {
+ a = src1;
+ r = src2;
+ g = src3;
+ b = src4;
+ } else {
+ r = src1;
+ g = src2;
+ b = src3;
+ a = src4;
+ }
+
+ if (isAlphaPremultiplied) {
+ // The RGB values are premultiplied by the alpha (so that
+ // Quartz can save time when compositing the bitmap to a
+ // destination), and we undo this premultiplication (with some
+ // lossiness unfortunately) when retrieving the bitmap data.
+ float oneOverAlpha = 255.0f / (float)a;
+ r = r * oneOverAlpha;
+ g = g * oneOverAlpha;
+ b = b * oneOverAlpha;
+ }
+
+ if (redOut)
+ *redOut = r;
+ if (greenOut)
+ *greenOut = g;
+ if (blueOut)
+ *blueOut = b;
+ if (alphaOut)
+ *alphaOut = a;
+}
+
+ (Handle) get32BitDataFromBitmapImageRep:(NSBitmapImageRep*)bitmapImageRep requiredPixelSize:(int)requiredPixelSize
{
Handle hRawData;
@@ -1244,9 +1281,7 @@ enum {
unsigned char* pSrc;
unsigned char* pDest;
int x, y;
- unsigned char alphaByte;
- float oneOverAlpha;
-
+
// Get information about the bitmapImageRep.
long pixelsWide = [bitmapImageRep pixelsWide];
long pixelsHigh = [bitmapImageRep pixelsHigh];
@@ -1256,6 +1291,8 @@ enum {
BOOL isPlanar = [bitmapImageRep isPlanar];
long bytesPerRow = [bitmapImageRep bytesPerRow];
unsigned char* bitmapData = [bitmapImageRep bitmapData];
+ BOOL isAlphaFirst = [bitmapImageRep bitmapFormat] & NSAlphaFirstBitmapFormat;
+ BOOL isAlphaPremultiplied = !([bitmapImageRep bitmapFormat] & NSAlphaNonpremultipliedBitmapFormat);
// Make sure bitmap has the required dimensions.
if (pixelsWide != requiredPixelSize || pixelsHigh != requiredPixelSize)
@@ -1289,23 +1326,14 @@ enum {
for (y = 0; y < pixelsHigh; y++) {
pSrc = bitmapData + y * bytesPerRow;
for (x = 0; x < pixelsWide; x++) {
- // Each pixel is 3 bytes of RGB data, followed by 1 byte of
- // alpha. The RGB values are premultiplied by the alpha (so
- // that Quartz can save time when compositing the bitmap to a
- // destination), and we undo this premultiplication (with some
- // lossiness unfortunately) when retrieving the bitmap data.
- *pDest++ = alphaByte = *(pSrc+3);
- if (alphaByte) {
- oneOverAlpha = 255.0f / (float)alphaByte;
- *pDest++ = *(pSrc+0) * oneOverAlpha;
- *pDest++ = *(pSrc+1) * oneOverAlpha;
- *pDest++ = *(pSrc+2) * oneOverAlpha;
- } else {
- *pDest++ = 0;
- *pDest++ = 0;
- *pDest++ = 0;
- }
- pSrc+=4;
+ unsigned char r, g, b, a;
+ GetRGBAFrom32BitSource(pSrc[0], pSrc[1], pSrc[2], pSrc[3],
+ &r, &g, &b, &a, isAlphaFirst, isAlphaPremultiplied);
+ *pDest++ = a;
+ *pDest++ = r;
+ *pDest++ = g;
+ *pDest++ = b;
+ pSrc += 4;
}
}
} else if (bitsPerPixel == 24) {
@@ -1347,6 +1375,8 @@ enum {
BOOL isPlanar = [bitmapImageRep isPlanar];
long bytesPerRow = [bitmapImageRep bytesPerRow];
unsigned char* bitmapData = [bitmapImageRep bitmapData];
+ BOOL isAlphaFirst = [bitmapImageRep bitmapFormat] & NSAlphaFirstBitmapFormat;
+ BOOL isAlphaPremultiplied = !([bitmapImageRep bitmapFormat] & NSAlphaNonpremultipliedBitmapFormat);
// Make sure bitmap has the required dimensions.
if (pixelsWide != requiredPixelSize || pixelsHigh != requiredPixelSize)
@@ -1383,9 +1413,12 @@ enum {
for (y = 0; y < pixelsHigh; y++) {
pSrc = bitmapData + y * bytesPerRow;
for (x = 0; x < pixelsWide; x++) {
- cgCol.red = ((float)*(pSrc)) / 255;
- cgCol.green = ((float)*(pSrc+1)) / 255;
- cgCol.blue = ((float)*(pSrc+2)) / 255;
+ unsigned char r, g, b;
+ GetRGBAFrom32BitSource(pSrc[0], pSrc[1], pSrc[2], pSrc[3],
+ &r, &g, &b, NULL, isAlphaFirst, isAlphaPremultiplied);
+ cgCol.red = (float)r / 255;
+ cgCol.green = (float)g / 255;
+ cgCol.blue = (float)b / 255;
*pDest++ = CGPaletteGetIndexForColor(cgPal, cgCol);
@@ -1436,6 +1469,8 @@ enum {
BOOL isPlanar = [bitmapImageRep isPlanar];
long bytesPerRow = [bitmapImageRep bytesPerRow];
unsigned char* bitmapData = [bitmapImageRep bitmapData];
+ BOOL isAlphaFirst = [bitmapImageRep bitmapFormat] & NSAlphaFirstBitmapFormat;
+ BOOL isAlphaPremultiplied = !([bitmapImageRep bitmapFormat] & NSAlphaNonpremultipliedBitmapFormat);
// Make sure bitmap has the required dimensions.
if (pixelsWide != requiredPixelSize || pixelsHigh != requiredPixelSize)
@@ -1469,8 +1504,11 @@ enum {
for (y = 0; y < pixelsHigh; y++) {
pSrc = bitmapData + y * bytesPerRow;
for (x = 0; x < pixelsWide; x++) {
- pSrc += 3;
- *pDest++ = *pSrc++;
+ unsigned char a;
+ GetRGBAFrom32BitSource(pSrc[0], pSrc[1], pSrc[2], pSrc[3],
+ NULL, NULL, NULL, &a, isAlphaFirst, isAlphaPremultiplied);
+ *pDest++ = a;
+ pSrc += 4;
}
}
}
@@ -1514,6 +1552,8 @@ enum {
BOOL isPlanar = [bitmapImageRep isPlanar];
long bytesPerRow = [bitmapImageRep bytesPerRow];
unsigned char* bitmapData = [bitmapImageRep bitmapData];
+ BOOL isAlphaFirst = [bitmapImageRep bitmapFormat] & NSAlphaFirstBitmapFormat;
+ BOOL isAlphaPremultiplied = !([bitmapImageRep bitmapFormat] & NSAlphaNonpremultipliedBitmapFormat);
// Make sure bitmap has the required dimensions.
if (pixelsWide != requiredPixelSize || pixelsHigh != requiredPixelSize)
@@ -1544,14 +1584,14 @@ enum {
pSrc = bitmapData + y * bytesPerRow;
for (x = 0; x < pixelsWide; x += 8) {
maskByte = 0;
- maskByte |= (*(unsigned*)pSrc & 0xff) ? 0x80 : 0; pSrc += 4;
- maskByte |= (*(unsigned*)pSrc & 0xff) ? 0x40 : 0; pSrc += 4;
- maskByte |= (*(unsigned*)pSrc & 0xff) ? 0x20 : 0; pSrc += 4;
- maskByte |= (*(unsigned*)pSrc & 0xff) ? 0x10 : 0; pSrc += 4;
- maskByte |= (*(unsigned*)pSrc & 0xff) ? 0x08 : 0; pSrc += 4;
- maskByte |= (*(unsigned*)pSrc & 0xff) ? 0x04 : 0; pSrc += 4;
- maskByte |= (*(unsigned*)pSrc & 0xff) ? 0x02 : 0; pSrc += 4;
- maskByte |= (*(unsigned*)pSrc & 0xff) ? 0x01 : 0; pSrc += 4;
+ for (int i = 7; i >= 0; i--) {
+ unsigned char a;
+ GetRGBAFrom32BitSource(pSrc[0], pSrc[1], pSrc[2], pSrc[3],
+ NULL, NULL, NULL, &a, isAlphaFirst, isAlphaPremultiplied);
+ if (a)
+ maskByte |= 1 << i;
+ pSrc += 4;
+ }
*pDest++ = maskByte;
}
}
diff --git a/third_party/icon_family/README.chromium b/third_party/icon_family/README.chromium
index 915d197..bbe5096 100644
--- a/third_party/icon_family/README.chromium
+++ b/third_party/icon_family/README.chromium
@@ -12,3 +12,4 @@ This is an Objective-C wrapper around Mac OS X Icon Services' "IconFamily" data
Local Modifications:
chromium_icon_family.patch: Fix minor erors and warnings. Put code that the custom icon code behind a DISABLE_CUSTOM_ICON flag.
+chromium_icon_family_2.patch: Add support for alpha first and non-premultiplied image formats.
# Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
{
'targets': [
],
'conditions': [
['OS=="mac"', {
'targets' : [
{
'target_name' : 'icon_family',
'type': 'static_library',
'sources': [
'IconFamily.h',
'IconFamily.m',
'NSString+CarbonFSRefCreation.h',
'NSString+CarbonFSRefCreation.m',
],
'defines': [
'DISABLE_CUSTOM_ICON'
],
},
],
}],
],
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment