diff --git a/Tools/nib2cib/NSCustomResource.j b/Tools/nib2cib/NSCustomResource.j index e9771a7f8..ccad4210e 100644 --- a/Tools/nib2cib/NSCustomResource.j +++ b/Tools/nib2cib/NSCustomResource.j @@ -25,6 +25,8 @@ @import +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; diff --git a/Tools/nib2cib/Nib2CibKeyedUnarchiver.j b/Tools/nib2cib/Nib2CibKeyedUnarchiver.j new file mode 100644 index 000000000..03f5415b7 --- /dev/null +++ b/Tools/nib2cib/Nib2CibKeyedUnarchiver.j @@ -0,0 +1,52 @@ + +@import + + +@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 diff --git a/Tools/nib2cib/main.j b/Tools/nib2cib/main.j index 29a8007ff..cda30b1e3 100644 --- a/Tools/nib2cib/main.j +++ b/Tools/nib2cib/main.j @@ -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(/\\s*CF\$UID\s*\<\/key\>/g, "CP$UID")]; // 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);