mirror of
https://github.com/cappuccino/cappuccino.git
synced 2026-10-09 18:30:59 +00:00
Improved support for resources in nib2cib.
Reviewed by me.
This commit is contained in:
1 parent
064a554985
commit
9ec781fb41
3 files changed
+119
-4
No files matched your search
@@ -25,6 +25,8 @@
|
||||
|
||||
@import <AppKit/_CPCibCustomResource.j>
|
||||
|
||||
importClass(javax.imageio.ImageIO);
|
||||
|
||||
|
||||
@implementation _CPCibCustomResource (NSCoding)
|
||||
|
||||
@@ -36,6 +38,48 @@
|
||||
{
|
||||
_className = CP_NSMapClassName([aCoder decodeObjectForKey:@"NSClassName"]);
|
||||
_resourceName = [aCoder decodeObjectForKey:@"NSResourceName"];
|
||||
|
||||
var size = CGSizeMakeZero();
|
||||
|
||||
if (![aCoder resourcesFile])
|
||||
{
|
||||
CPLog.warn("***WARNING: Resources found in nib, but no resources path specified with -R option.");
|
||||
|
||||
_properties = [CPDictionary dictionaryWithObject:CGSizeMakeZero() forKey:@"size"];
|
||||
}
|
||||
else
|
||||
{
|
||||
var resourceFile = [aCoder resourceFileForName:_resourceName];
|
||||
|
||||
if (!resourceFile)
|
||||
CPLog.warn("***WARNING: Resource named " + _resourceName + " not found in supplied resources path.");
|
||||
else
|
||||
{
|
||||
var imageStream = ImageIO.createImageInputStream(resourceFile),
|
||||
readers = ImageIO.getImageReaders(imageStream),
|
||||
reader = null;
|
||||
|
||||
if(readers.hasNext())
|
||||
reader = readers.next();
|
||||
|
||||
else
|
||||
{
|
||||
imageStream.close();
|
||||
//can't read image format... what do you want to do about it,
|
||||
//throw an exception, return ?
|
||||
}
|
||||
|
||||
reader.setInput(imageStream, true, true);
|
||||
|
||||
// Now we know the size (yay!)
|
||||
size = CGSizeMake(reader.getWidth(0), reader.getHeight(0));
|
||||
print(size.width + " " + size.height);
|
||||
reader.dispose();
|
||||
imageStream.close();
|
||||
}
|
||||
}
|
||||
|
||||
_properties = [CPDictionary dictionaryWithObject:size forKey:@"size"];
|
||||
}
|
||||
|
||||
return self;
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
|
||||
@import <Foundation/CPKeyedUnarchiver.j>
|
||||
|
||||
|
||||
@implementation Nib2CibKeyedUnarchiver : CPKeyedUnarchiver
|
||||
{
|
||||
File resourcesFile;
|
||||
}
|
||||
|
||||
- (id)initForReadingWithData:(CPData)data resourcesFile:(File)aResourcesFile
|
||||
{
|
||||
self = [super initForReadingWithData:data];
|
||||
|
||||
if (self)
|
||||
resourcesFile = aResourcesFile;
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
- (File)resourcesFile
|
||||
{
|
||||
return resourcesFile;
|
||||
}
|
||||
|
||||
- (File)resourceFileForName:(CPString)aName
|
||||
{
|
||||
var moreFiles = [resourcesFile.listFiles()];
|
||||
|
||||
do
|
||||
{
|
||||
var files = moreFiles.shift(),
|
||||
index = 0,
|
||||
count = files.length;
|
||||
|
||||
for (; index < count; ++index)
|
||||
{
|
||||
var file = files[index].getCanonicalFile(),
|
||||
name = String(file.getName());
|
||||
|
||||
if (name === aName)
|
||||
return file;
|
||||
|
||||
if (file.isDirectory())
|
||||
moreFiles.push(file.listFiles());
|
||||
}
|
||||
}
|
||||
while (moreFiles.length > 0)
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@end
|
||||
+23
-4
@@ -27,8 +27,10 @@
|
||||
|
||||
@import "NSFoundation.j"
|
||||
@import "NSAppKit.j"
|
||||
@import "Nib2CibKeyedUnarchiver.j"
|
||||
|
||||
importPackage(java.io);
|
||||
importClass(java.io.File);
|
||||
|
||||
CPLogRegister(CPLogPrint);
|
||||
|
||||
@@ -76,8 +78,21 @@ function cibExtension(aPath)
|
||||
return aPath.substr(0, dotIndex) + ".cib";
|
||||
}
|
||||
|
||||
function convert(inputFileName, outputFileName)
|
||||
function convert(inputFileName, outputFileName, resourcesPath)
|
||||
{
|
||||
var resourcesFile = nil;
|
||||
|
||||
if (resourcesPath)
|
||||
{
|
||||
resourcesFile = new File(resourcesPath).getCanonicalFile();
|
||||
|
||||
if (!resourcesFile.canRead())
|
||||
{
|
||||
print("Could not find Resources at " + resourcesFile);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Make sure we can read the file
|
||||
if (!(new Packages.java.io.File(inputFileName)).canRead())
|
||||
{
|
||||
@@ -115,7 +130,7 @@ function convert(inputFileName, outputFileName)
|
||||
[data setString:[data string].replace(/\<key\>\s*CF\$UID\s*\<\/key\>/g, "<key>CP$UID</key>")];
|
||||
|
||||
// Unarchive the NS data
|
||||
var unarchiver = [[CPKeyedUnarchiver alloc] initForReadingWithData:data],
|
||||
var unarchiver = [[Nib2CibKeyedUnarchiver alloc] initForReadingWithData:data resourcesFile:resourcesFile],
|
||||
objectData = [unarchiver decodeObjectForKey:@"IB.objectdata"],
|
||||
|
||||
data = [CPData data],
|
||||
@@ -201,6 +216,7 @@ function main()
|
||||
|
||||
inputFileName = nil,
|
||||
outputFileName = nil,
|
||||
resourcesPath = nil,
|
||||
frameworkPaths = [];
|
||||
|
||||
for (; index < count; ++index)
|
||||
@@ -212,6 +228,9 @@ function main()
|
||||
|
||||
case "-F": frameworkPaths.push(arguments[++index]);
|
||||
break;
|
||||
|
||||
case "-R": resourcesPath = arguments[++index];
|
||||
break;
|
||||
|
||||
default: if (inputFileName && inputFileName.length > 0)
|
||||
outputFileName = arguments[index];
|
||||
@@ -224,10 +243,10 @@ function main()
|
||||
outputFileName = cibExtension(inputFileName);
|
||||
|
||||
if (frameworkPaths.length)
|
||||
loadFrameworks(frameworkPaths, function() { convert(inputFileName, outputFileName); });
|
||||
loadFrameworks(frameworkPaths, function() { convert(inputFileName, outputFileName, resourcesPath); });
|
||||
|
||||
else
|
||||
convert(inputFileName, outputFileName);
|
||||
convert(inputFileName, outputFileName, resourcesPath);
|
||||
}
|
||||
|
||||
main.apply(main, args);
|
||||
Reference in new issue
Block a user