1
1
var cache , imVersion ,
2
2
async = require ( "async" ) ,
3
3
Cache = require ( "./cache" ) ,
4
- im = require ( "gm" ) . subClass ( { imageMagick : true } ) ,
4
+ im = require ( "gm" ) . subClass ( { imageMagick : true } ) ,
5
5
semver = require ( "semver" ) ,
6
6
dimensionLimit = 3000 ,
7
7
namedColors = require ( "./themeroller-colors" ) ;
@@ -10,9 +10,9 @@ cache = new Cache( "Image Cache" );
10
10
11
11
function expandColor ( color ) {
12
12
if ( color . length === 3 && / ^ [ 0 - 9 a - f ] + $ / i. test ( color ) ) {
13
- return [ 0 , 0 , 1 , 1 , 2 , 2 ] . map ( function ( i ) {
13
+ return [ 0 , 0 , 1 , 1 , 2 , 2 ] . map ( function ( i ) {
14
14
return color [ i ] ;
15
- } ) . join ( "" ) ;
15
+ } ) . join ( "" ) ;
16
16
}
17
17
return color ;
18
18
}
@@ -28,11 +28,11 @@ function hashColor( color ) {
28
28
function pick ( obj ) {
29
29
var copy = { } ;
30
30
var keys = [ ] . concat . apply ( [ ] , [ ] . slice . call ( arguments , 1 ) ) ;
31
- keys . forEach ( function ( key ) {
31
+ keys . forEach ( function ( key ) {
32
32
if ( key in obj ) {
33
33
copy [ key ] = obj [ key ] ;
34
34
}
35
- } ) ;
35
+ } ) ;
36
36
return copy ;
37
37
}
38
38
@@ -49,56 +49,58 @@ function stream2Buffer( callback ) {
49
49
stdin . on ( "data" , function ( chunk ) {
50
50
chunks . push ( chunk ) ;
51
51
dataLen += chunk . length ;
52
- } ) ;
52
+ } ) ;
53
53
54
54
stderr . on ( "data" , function ( chunk ) {
55
55
err += chunk ;
56
- } ) ;
56
+ } ) ;
57
57
58
58
stdin . on ( "end" , function ( ) {
59
59
var i = 0 ,
60
60
buffer = Buffer . alloc ( dataLen ) ;
61
61
if ( err . length ) {
62
62
return callback ( new Error ( err ) ) ;
63
63
}
64
- chunks . forEach ( function ( chunk ) {
64
+ chunks . forEach ( function ( chunk ) {
65
65
chunk . copy ( buffer , i , 0 , chunk . length ) ;
66
66
i += chunk . length ;
67
- } ) ;
67
+ } ) ;
68
68
callback ( null , buffer ) ;
69
- } ) ;
69
+ } ) ;
70
70
71
71
stdin . on ( "error" , function ( err ) {
72
72
callback ( err ) ;
73
- } ) ;
73
+ } ) ;
74
74
} ;
75
75
}
76
76
77
77
function validateColor ( color ) {
78
78
color = color . replace ( / ^ # / , "" ) ;
79
79
if ( ( color . length === 3 || color . length === 6 ) && / ^ [ 0 - 9 a - f ] + $ / i. test ( color ) ) {
80
+
80
81
// ok
81
82
} else if ( namedColors . indexOf ( color . toLowerCase ( ) ) !== - 1 ) {
83
+
82
84
// ok
83
85
} else {
84
86
throw new Error ( "invalid color \"" + color + "\"" ) ;
85
87
}
86
88
}
87
89
88
90
function validateDimension ( params , dimensionParams ) {
89
- var invalidParams = dimensionParams . filter ( function ( param ) {
91
+ var invalidParams = dimensionParams . filter ( function ( param ) {
90
92
return parseInt ( params [ param ] , 10 ) > dimensionLimit ;
91
- } ) ;
93
+ } ) ;
92
94
93
95
if ( invalidParams . length ) {
94
96
throw new Error ( "dimension bigger than allowed limit " + JSON . stringify ( pick ( params , invalidParams ) ) ) ;
95
97
}
96
98
}
97
99
98
100
function validateInteger ( params , integerParams ) {
99
- var invalidParams = integerParams . filter ( function ( param ) {
100
- return isNaN ( parseInt ( params [ param ] , 10 ) ) || ( / [ ^ 0 - 9 ] / ) . test ( params [ param ] ) ;
101
- } ) ;
101
+ var invalidParams = integerParams . filter ( function ( param ) {
102
+ return isNaN ( parseInt ( params [ param ] , 10 ) ) || ( / [ ^ 0 - 9 ] / ) . test ( params [ param ] ) ;
103
+ } ) ;
102
104
103
105
if ( invalidParams . length ) {
104
106
throw new Error ( "got a non-integer " + JSON . stringify ( pick ( params , invalidParams ) ) ) ;
@@ -113,9 +115,9 @@ function validateOpacity( opacity ) {
113
115
}
114
116
115
117
function validatePresence ( params , requiredParams ) {
116
- var missingParams = requiredParams . filter ( function ( param ) {
118
+ var missingParams = requiredParams . filter ( function ( param ) {
117
119
return ! params [ param ] ;
118
- } ) ;
120
+ } ) ;
119
121
120
122
if ( missingParams . length ) {
121
123
throw new Error ( "missing \"" + missingParams . join ( "\", \"" ) + "\"" ) ;
@@ -151,7 +153,7 @@ generateIcon = function( params, callback ) {
151
153
// IM > 6.7.9: (see #132 http://git.io/gfSacg)
152
154
// $ convert <icons_mask_filename> -set colorspace RGB -background <color> -alpha shape -set colorspace sRGB output.png
153
155
154
- imageQueue . push ( function ( innerCallback ) {
156
+ imageQueue . push ( function ( innerCallback ) {
155
157
try {
156
158
if ( semver . gt ( imVersion , "6.7.9" ) ) {
157
159
im ( __dirname + "/../assets/icon/mask.png" )
@@ -166,7 +168,7 @@ generateIcon = function( params, callback ) {
166
168
. out ( "-alpha" , "shape" )
167
169
. stream ( "png" , stream2Buffer ( innerCallback ) ) ;
168
170
}
169
- } catch ( err ) {
171
+ } catch ( err ) {
170
172
return innerCallback ( err ) ;
171
173
}
172
174
} , callback ) ;
@@ -183,12 +185,12 @@ generateTexture = function( params, callback ) {
183
185
// http://www.imagemagick.org/Usage/compose/#dissolve
184
186
// $ convert -size <width>x<height> 'xc:<color>' <texture_filename> -compose dissolve -define compose:args=<opacity>,100 -composite output.png
185
187
186
- imageQueue . push ( function ( innerCallback ) {
188
+ imageQueue . push ( function ( innerCallback ) {
187
189
try {
188
190
im ( params . width , params . height , color )
189
191
. out ( __dirname + "/../assets/texture/" + filename , "-compose" , "dissolve" , "-define" , "compose:args=" + params . opacity + ",100" , "-composite" )
190
192
. stream ( "png" , stream2Buffer ( innerCallback ) ) ;
191
- } catch ( err ) {
193
+ } catch ( err ) {
192
194
return innerCallback ( err ) ;
193
195
}
194
196
} , callback ) ;
@@ -314,19 +316,19 @@ Image.prototype = {
314
316
cached . data = data ;
315
317
delete cached . callbacks ;
316
318
}
317
- callbacks . forEach ( function ( callback ) {
319
+ callbacks . forEach ( function ( callback ) {
318
320
callback ( err , filename , data ) ;
319
- } ) ;
321
+ } ) ;
320
322
delete cached . callbacks ;
321
323
if ( err ) {
322
324
cache . destroy ( filename ) ;
323
325
}
324
- } ) ;
326
+ } ) ;
325
327
}
326
328
} ;
327
329
328
330
// Check the ImageMagick installation using node-gm (in a hacky way).
329
- async . series ( [
331
+ async . series ( [
330
332
function ( callback ) {
331
333
var wrappedCallback = function ( err ) {
332
334
if ( err ) {
@@ -335,15 +337,15 @@ async.series([
335
337
callback ( ) ;
336
338
} ;
337
339
try {
338
- im ( ) . _spawn ( [ "convert" , "-version" ] , true , wrappedCallback ) ;
339
- } catch ( err ) {
340
+ im ( ) . _spawn ( [ "convert" , "-version" ] , true , wrappedCallback ) ;
341
+ } catch ( err ) {
340
342
return wrappedCallback ( err ) ;
341
343
}
342
344
} ,
343
345
function ( callback ) {
344
- im ( ) . _spawn ( [ "convert" , "-version" ] , false , stream2Buffer ( function ( err , buffer ) {
346
+ im ( ) . _spawn ( [ "convert" , "-version" ] , false , stream2Buffer ( function ( _err , buffer ) {
345
347
var output = buffer . toString ( "utf8" ) ;
346
- if ( ! ( / I m a g e M a g i c k / ) . test ( output ) ) {
348
+ if ( ! ( / I m a g e M a g i c k / ) . test ( output ) ) {
347
349
return callback ( new Error ( "ImageMagick not installed.\n" + output ) ) ;
348
350
}
349
351
imVersion = output . split ( "\n" ) [ 0 ] . replace ( / ^ V e r s i o n : I m a g e M a g i c k ( [ ^ ] * ) .* / , "$1" ) ;
@@ -352,13 +354,14 @@ async.series([
352
354
}
353
355
imageQueue . resume ( ) ;
354
356
callback ( ) ;
355
- } ) ) ;
357
+ } ) ) ;
356
358
}
357
359
] , function ( err ) {
358
360
if ( err ) {
361
+
359
362
// On error, abort
360
363
throw new Error ( err ) ;
361
364
}
362
- } ) ;
365
+ } ) ;
363
366
364
367
module . exports = Image ;
0 commit comments