Skip to content

Commit

Permalink
* Changed fileGroups option from array of file names to regular expre…
Browse files Browse the repository at this point in the history
…ssion
  • Loading branch information
bchevalier committed Feb 2, 2015
1 parent d5fa084 commit 17f084b
Show file tree
Hide file tree
Showing 16 changed files with 69 additions and 44 deletions.
3 changes: 3 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Release history

## v0.1.7
Changed fileGroups option from array of file names to regular expression

## v0.1.6
Updated dependencies

Expand Down
2 changes: 0 additions & 2 deletions lib/CanvasRenderer/FILTERS.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict';

var Canvas = require('canvas');

// TODO:
// - bevel filter
// - gradient glow filter
Expand Down
1 change: 1 addition & 0 deletions lib/CanvasRenderer/SwfImgRenderer/PngWriter.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ PngWriter.prototype._addChunk = function (name, data, len) {
var buf = new Buffer(len + 12, 'binary');
buf.writeUInt32BE(len, 0);
buf.write(name, 4, 4);
// console.log('chunk type', name, JSON.stringify(data));
if (typeof data !== 'string') {
data.copy(buf, 8, 0, len);
} else {
Expand Down
1 change: 0 additions & 1 deletion lib/CanvasRenderer/SwfImgRenderer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ SwfImgRenderer.prototype._renderPng = function (swfObject, whenDone) {
// Should never happen unless caller is wrong
throw new Error('Invalid data for PNG file');
}

var self = this;
this._inflate(swfObject.colorData, function (buffer) {
swfObject.data = buffer;
Expand Down
14 changes: 11 additions & 3 deletions lib/CanvasRenderer/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,30 @@
var SwfImgRenderer = require('./SwfImgRenderer/index');

/* List of known issues:
- drawImage does implicit conversion of image position to integer
(due to node canvas, issue #395 on https://github.com/LearnBoost/node-canvas)
- Global Composite Operations Bug: source-in can result in an overflow if rendered image is bigger than the mask
(due to node canvas, issue #416 on https://github.com/LearnBoost/node-canvas)
- Global Composite Operations Bug (Another): source-in can result in dark outlines of the image when rotated
(due to node canvas, not reported)
- Alpha of gradient fill style bug
(due to node canvas, issue #417 on https://github.com/LearnBoost/node-canvas)
- Drawing images of dimension (w, h) = (0, 0) or with a scale of 0 makes a canvas unsuable
(due to node canvas, not reported)
- Another Global Composite Operations Bug: source-in can result in dark outlines of the image when rotated
(due to node canvas, not reported)
- putImageData does not always use the last updated data
(seems to be due to node canvas, not reported)
- Path point positions need to be rounded to one decimal value when creating a vectorial path
(due to node canvas, not reported)
- When rendering images the size of the canvas could be too small if glows or shadows are being applied
(Fix: consider glow and shadow dimensions when setting canvas dimension, increased dimension should be notified to the extractor)
- Not tested but probably does not work: rendering rotated shapes with an alpha blend mode will not have the same result as in flash
*/

Expand Down
2 changes: 1 addition & 1 deletion lib/CanvasRenderer/prepareImages.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ CanvasRenderer.prototype._prepareImages = function () {

imageContext.drawImage(img, 0, 0, img.width, img.height);
} else {
console.warn('[CanvasRenderer.prepareImages] Image is empty', imageData.id);
console.warn('[CanvasRenderer.prepareImages] Image is empty', imageData.id, JSON.stringify(imageBuffer));
}

nImagesReady += 1;
Expand Down
7 changes: 4 additions & 3 deletions lib/CanvasRenderer/renderImages.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ function getGraphicsToRender(symbols, symbolList, images) {
}
return graphics;
}

var nBytes = 0;
CanvasRenderer.prototype._renderGraphics = function (graphics, graphicDims, canvasses) {
for (var id in graphics) {
var canvas = new Canvas();
Expand All @@ -163,6 +163,7 @@ CanvasRenderer.prototype._renderGraphics = function (graphics, graphicDims, canv

canvas.width = dimensions.sw;
canvas.height = dimensions.sh;
nBytes += canvas.width * canvas.height * 4;

if (graphic.isShape) {
var transform = [dimensions.ratio, 0, 0, dimensions.ratio, - dimensions.dx, - dimensions.dy];
Expand Down Expand Up @@ -320,8 +321,8 @@ CanvasRenderer.prototype._renderImages = function (retry) {
if (atlas) {
imageList = [{ img: atlas, name: 'atlas' }];
} else {
// TODO: add an option to let the user choose whether he wants to create several
// several atlas or reduce the asset sizes when this situation happens
// TODO: add an option to let the user choose whether he wants to create
// several atlases or reduce the asset sizes when this situation happens

// Atlas could not be extracted at current ratio
// Reducing extraction ratio and attempting rendering once more
Expand Down
2 changes: 1 addition & 1 deletion lib/CanvasRenderer/vectorialRendering.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ CanvasRenderer.prototype._drawShapes = function (shapes, canvas, context, transf
}
};

// Due to a bug in cairo, path point positions have to be rounded
CanvasRenderer.prototype._createPath = function (context, shapes, transform, pixelHinting) {
context.beginPath();
var p, seg;
Expand Down Expand Up @@ -238,7 +239,6 @@ CanvasRenderer.prototype._outlineShapes = function (context, shapes, transform,
this._createPath(context, shapes, transform, pixelHinting);

var s, color, alpha, scale;

context.save();

if (line.capStart) {
Expand Down
15 changes: 7 additions & 8 deletions lib/Gordon/parser.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//See http://wwwimages.adobe.com/www.adobe.com/content/dam/Adobe/en/devnet/swf/pdf/swf-file-format-spec.pdf
// See http://wwwimages.adobe.com/www.adobe.com/content/dam/Adobe/en/devnet/swf/pdf/swf-file-format-spec.pdf

'use strict';

Expand All @@ -8,7 +8,7 @@ var Stream = require('./stream.js');
function SwfParser() {}
module.exports = SwfParser;

// TODO: figure out if we cannot get rid of this 'nlizeMatrix' function
// TODO: figure out if we can get rid of this 'nlizeMatrix' function
function nlizeMatrix(matrix) {
return {
scaleX: matrix.scaleX * 20,
Expand Down Expand Up @@ -62,7 +62,6 @@ SwfParser.prototype = {
} else if (sign === signatures.SWF) {
this._parseSwf(stream, cb);
} else {
console.log('youhou!!');
cb('Invalid SWF file: ' + swfName);
}
},
Expand All @@ -75,9 +74,9 @@ SwfParser.prototype = {
// to extract regular swf files.
// It should possible to determine the symbols of the swl table from the dofus database

//Special table at the head of a ".swl" file; we did not find why this is useful...
//Maybe to be able to easily look for which symbols are defined in a .swf without
//having to parse/decompress it all.
// Special table at the head of a ".swl" file; we did not find why this is useful...
// Maybe to be able to easily look for which symbols are defined in a .swf without
// having to parse/decompress it all.
_readSwlTable: function (stream) {
var sign = stream.readString(2);
if (sign[0] !== 'L' || sign.charCodeAt(1) !== 0) {
Expand Down Expand Up @@ -771,8 +770,8 @@ SwfParser.prototype = {
var depth = stream.readUI16();
var hasCharacter = flags & f.HAS_CHARACTER;
var hasImage = flags2 & f.HAS_IMAGE;
var className = ((flags2 & f.HAS_CLASS_NAME) || (hasImage && hasCharacter)) ? stream.readString : undefined;
var character = { depth: depth };
var baseClass = ((flags2 & f.HAS_CLASS_NAME) || (hasImage && hasCharacter)) ? stream.readString : undefined;
var character = { depth: depth, baseClass: baseClass };

if (hasCharacter) {
var objId = stream.readUI16();
Expand Down
18 changes: 16 additions & 2 deletions lib/Helper/generateExportList.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,29 @@
// }


function generateExportList(symbols, classList) {
function generateExportList(symbols, classList, attributeFilter) {
var symbolList = [];

// Creating list of symbols to export
// Starting with the symbols corresponding to the class to export
var symbolsToVisit = [];
for (var className in classList) {
var classId = classList[className];
symbolsToVisit.push(classId);

if (attributeFilter) {
var classSymbol = symbols[classId];
for (var a = 0; a < attributeFilter.length; a +=1) {
if (classSymbol[attributeFilter[a]]) {
// The class has the corresponding attribute, it is added for export
symbolsToVisit.push(classId);
}
}
} else {
symbolsToVisit.push(classId);
}
}

// Including symbols that are descendants of the classes to export
var visistedSymbols = {};
while (symbolsToVisit.length > 0) {
var symbolId = symbolsToVisit.pop();
Expand Down
9 changes: 4 additions & 5 deletions lib/Helper/groupClasses.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
'use strict';

function groupClasses(classList, classGroups, split) {

// Grouping classes
if (!split && Object.keys(classGroups).length === 0) {
return [{ name: '', list: classList }];
}

// Creating list of classes that will be exported separately
var splitName;
var classSplits = {};
for (splitName in classGroups) {
classSplits[splitName] = {};
}

var className;
var remainingClasses = [];
var remainingClasses = []; // List of classes that will be exported together
for (className in classList) {
var matchFound = false;
for (splitName in classGroups) {
var splitRegEx = classGroups[splitName];
if (className.match(splitRegEx)) {
var groupRegEx = classGroups[splitName];
if (className.match(groupRegEx)) {
classSplits[splitName][className] = classList[className];
matchFound = true;
}
Expand Down
26 changes: 15 additions & 11 deletions lib/Helper/makeFileGroups.js → lib/Helper/groupFiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,45 @@ function getFileName(uri) {
return uri.substring(uri.lastIndexOf(separator) + 1, uri.lastIndexOf('.'));
}

function makeFileGroups(uris, fileGroups) {
function groupFiles(uris, fileGroupsRegEx) {
var fileGroupsArray = [];
var u, fileName;
if (!fileGroups) {
if (!fileGroupsRegEx) {
for (u = 0; u < uris.length; u += 1) {
fileName = getFileName(uris[u]);
fileGroupsArray.push({ input: [uris[u]], output: fileName });
}
return fileGroupsArray;
}

// Creating one group per file
var fileGroupsObject = {};
for (u = 0; u < uris.length; u += 1) {
fileName = getFileName(uris[u]);
fileGroupsObject[fileName] = [uris[u]];
}


var output;
for (output in fileGroups) {
var groupFiles = fileGroups[output];
for (output in fileGroupsRegEx) {
var fileGroup = fileGroupsObject[output];
if (fileGroup === undefined) {
fileGroup = [];
fileGroupsObject[output] = fileGroup;
}

for (var f = 0; f < groupFiles.length; f += 1) {
fileName = groupFiles[f];
var groupRegEx = fileGroupsRegEx[output]; // pattern of the current group
for (u = 0; u < uris.length; u += 1) {
fileName = getFileName(uris[u]);
if (fileName === output) {
// File name is identical to the output, therefore they are merged
continue;
}
var fileArray = fileGroupsObject[fileName];
if (fileArray) {
fileGroup.push(fileArray[0]);

// Merging file to group if it matches the pattern
if (fileName.match(groupRegEx)) {
fileGroup.push(uris[u]);

// Removing the file as a stand-alone
delete fileGroupsObject[fileName];
}
}
Expand All @@ -56,4 +60,4 @@ function makeFileGroups(uris, fileGroups) {

return fileGroupsArray;
}
module.exports = makeFileGroups;
module.exports = groupFiles;
2 changes: 1 addition & 1 deletion lib/Helper/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module.exports.getClasses = require('./getClasses.js');
module.exports.getMains = require('./getMains.js');
module.exports.groupClasses = require('./groupClasses.js');
module.exports.filterClasses = require('./filterClasses.js');
module.exports.makeFileGroups = require('./makeFileGroups.js');
module.exports.groupFiles = require('./groupFiles.js');
module.exports.flattenAnimations = require('./flattenAnimations.js');
module.exports.simplifyAnimation = require('./simplifyAnimation.js');
module.exports.delocateMatrices = require('./delocateMatrices.js');
1 change: 0 additions & 1 deletion lib/SwfObjectProcessor/createSymbols.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ function createSymbol(swfObject) {
break;

case 'shape':

var shapes = swfObject.edges;
var imagesArray = [];
for (var s = 0; s < shapes.length; s += 1) {
Expand Down
8 changes: 4 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ function Jeff() {
this._classGroupName = undefined; // Name
this._classGroupList = undefined; // Classes
this._hierarchy = undefined; // Hierarchy of the symbols

}

function JeffOptions(params) {
Expand Down Expand Up @@ -86,6 +85,7 @@ function JeffOptions(params) {
this.classGroups = params.classGroups || {};
this.fileGroups = params.fileGroups || {};
this.fileGroupRatios = params.fileGroupRatios || {};
this.attributeFilter = params.attributeFilter;
this.classRatios = params.classRatios;
this.container = params.container;
this.ignoreExpression = params.ignoreExpression;
Expand Down Expand Up @@ -147,7 +147,7 @@ Jeff.prototype._extractFileGroups = function (swfUris, endExtractionCb) {
var nbFiles = 0;
var nbErrors = 0;

var filesPerGroup = helper.makeFileGroups(swfUris, this._options.fileGroups);
var filesPerGroup = helper.groupFiles(swfUris, this._options.fileGroups);
console.log('Starting conversion of', swfUris.length, 'file(s)');

var self = this;
Expand Down Expand Up @@ -274,7 +274,7 @@ Jeff.prototype._processFileGroup = function (nextGroupCb) {
function (classGroup, nextClassListCb) {
self._classGroupName = classGroup.name;
self._classGroupList = classGroup.list;
self._symbolList = helper.generateExportList(self._symbols, self._classGroupList);
self._symbolList = helper.generateExportList(self._symbols, self._classGroupList, self._options.attributeFilter);
self._renderer.renderSymbols(self,
function (imageList, graphicProperties) {
self._extractClassGroup(imageList, graphicProperties, nextClassListCb);
Expand Down Expand Up @@ -303,7 +303,7 @@ Jeff.prototype._generateImageName = function (imgName) {
if (this._options.exportAtRoot) {
imgPath = path.basename(imgPath);
}

return path.join(this._options.outDir, imgPath + '.png');
};

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "jeff",
"description": "Extracts Json meta-data and images from a SWF Flash file.",
"version": "0.1.6",
"version": "0.1.7",
"private": false,
"author": {
"name": "Brice Chevalier",
Expand Down

0 comments on commit 17f084b

Please sign in to comment.