Permalink
Browse files

added more finish to pango command, added fonts list, added ImageMagi…

…ck functionalities
  • Loading branch information...
1 parent 57ee34b commit 9be527337b0d0db67445922e18f4a6fefd2e7d6c @shimondoodkin committed Aug 26, 2010
Showing with 181 additions and 14 deletions.
  1. +165 −12 index.js
  2. +16 −2 test.js
View
177 index.js
@@ -1,7 +1,35 @@
-var sys = require('sys'),
- exec = require('child_process').exec;
+var sys = require('sys'), fs=require('fs'), path=require('path'), exec = require('child_process').exec;
-this.tempdir=__dirname+'/';
+var that=this;
+that.tempdir='/tmp/';
+var lastcommand
+this.tryshm = function()
+{
+ path.exists('/dev/shm/',function(exists) // try shm
+ {
+ if(exists)
+ {
+ path.exists('/dev/shm/node-pango-view',function(exists)
+ {
+ if(exists)
+ {
+ that.tempdir='/dev/shm/node-pango-view/';
+ }
+ else
+ {
+ fs.mkdir('/dev/shm/node-pango-view', 0777, function (err)
+ {
+ if(err) throw err;
+ else
+ that.tempdir='/dev/shm/node-pango-view/';
+ //callback();
+ });
+ }
+ });
+ }
+ });
+}
+this.tryshm();
var errorimage=""; //error image full path
@@ -22,14 +50,15 @@ function uniquerandom()
function show_error(error, stdout, stderr)
+{
+ if(stdout)console.log('pango-view_stdout: ' + stdout);
+ if(stderr)console.log('pango-view_stderr: ' + stderr);
+ if (error !== null)
{
- sys.print('pango-view_stdout: ' + stdout);
- sys.print('pango-view_stderr: ' + stderr);
- if (error !== null)
- {
- console.log('pango-view_exec error: ' + error);
- }
+ console.log('node-pango-view last command: ' + lastcommand);
+ console.log('node-pango-view error: ' + error);
}
+}
// <html>.
var exec_option={timeout:1500};
@@ -41,15 +70,17 @@ function write(text,options,callback)
var args_str='',args=[];
args.push('-q'); // command line only
- args.push('--background=transparent');
- args.push('--markup');
+
+ if('background' in options) args.push('--background='+options['background']); else args.push('--background=transparent');
+ if(('markup' in options)) { if(options['markup']) args.push('--markup'); } else args.push('--markup');
if(('no-auto-dir' in options) && options['no-auto-dir']) args.push('--no-auto-dir');
if('dpi' in options) args.push('--dpi='+options['dpi']);
if('align' in options) args.push('--align='+options['align']);
if('ellipsize' in options) args.push('--ellipsize='+options['ellipsize']);
if('font' in options) args.push('--font='+options['font']);
if('color' in options) args.push('--foreground='+options['color']);
+ if('foreground' in options) args.push('--foreground='+options['foreground']);
if('gravity' in options) args.push('--gravity='+options['gravity']);
if('gravity-hint' in options) args.push('--gravity-hint='+options['gravity-hint']);
//if(('header' in options) && options['header']) args.push('--header');
@@ -82,13 +113,118 @@ function write(text,options,callback)
return m.slice(0, 1)+'\\\'';
})+"'";
}
+
//child = exec('pangoview *.js bad_file | wc -l',
- var child = exec('pango-view '+args_str,exec_option, show_error );
+ //console.log('pango-view '+args_str);
+ var cmd='export LANG=en_US.UTF-8;pango-view '+args_str,exec_option;
+ lastcommand=cmd;
+ var child = exec(cmd, show_error );
+
+ //var child = exec('export LANG=en_US.UTF-8;env ', show_error );
child.on('exit',function (code, signal) {if(code==0) callback(file); else callback(errorimage)});
} this.write=write;
+function compose(bg_args_str,images,callback)
+{
+ //bg is file
+ //images is array of [file,top,left,options];
+ var out_file= this.tempdir+uniquerandom()+'.png';
+
+ var imgs_args_str='',imgs_args=[];
+ for(var i=0;i<images.length;i++)
+ {
+ imgs_args.push(images[i]); // bg is 1st arument
+ //if(images[i].length>1) imgs_args.push(images[i][1]);
+ //imgs_args.push('-geometry');imgs_args.push('+'+images[i][1]+'+'+images[i][2]);
+ // some more options here
+ //imgs_args.push('-composite');
+ }
+
+ for(var i=0;i<imgs_args.length;i++)
+ {
+ imgs_args_str+=' '+imgs_args[i];
+ /*" '"+imgs_args[i].replace(/[^\\]'/g, function(m)
+ {
+ return m.slice(0, 1)+'\\\'';
+ })+"'";*/
+ }
+
+ //child = exec('pangoview *.js bad_file | wc -l',
+ //console.log('convert '+bg_args_str+' '+imgs_args_str+' '+out_file);
+ var cmd='convert '+bg_args_str+' '+imgs_args_str+' '+out_file,exec_option;
+ lastcommand=cmd;
+ var child = exec(cmd, show_error );
+
+
+ child.on('exit',function (code, signal) {if(code==0) callback(out_file); else callback(errorimage)});
+} this.compose=compose;
+
+
+function size(img_args_str,callback)
+{
+ // console.log('identify '+img_args_str);
+ var cmd ='identify '+img_args_str,exec_option;
+ lastcommand=cmd;
+ var child = exec(cmd, function(err, stdout, stderr)
+ {
+ var result;
+ if (!err)
+ {
+ var v = stdout.split(/ +/);
+ var x = v[2].split(/x/);
+ var result =
+ {
+ format: v[1],
+ width: parseInt(x[0]),
+ height: parseInt(x[1]),
+ depth: parseInt(v[4]),
+ };
+ callback(result)
+ }
+ else
+ {
+ console.log(' error in identify');
+ var result =
+ {
+ format: '',
+ width: 1,
+ height: 1,
+ depth: 8,
+ 'err':err,
+ };
+ callback(result)
+ }
+ });
+
+ //child.on('exit',function (code, signal) {if(code==0) callback(out_file); else callback(errorimage)});
+} this.size=size;
+function fonts(callback)
+{
+ var cmd ='fc-list :lang=he -f %{family}\\\\n|sort -u';
+ lastcommand=cmd;
+ var child = exec(cmd,exec_option, function(err, stdout, stderr)
+ {
+ var result;
+ if (!err)
+ {
+ //console.log(stdout);
+ var result = stdout.split("\n");
+ //result.length--;
+ callback(result);
+ }
+ else
+ {
+ console.log(' error in identify');
+ var result =['error calling "fc-list -f %{family}\\\\n"'];
+ callback(result);
+ }
+ });
+ //child.on('exit',function (code, signal) {if(code==0) callback(out_file); else callback(errorimage)});
+} this.fonts=fonts;
+
+//convert bgs/morguefile_97997.jpg 12825167297205756.png -geometry +31+105 -composite 12825167297226952.png -geometry +31+205 -composite out6.png
/*
# pango-view --help-all
Usage:
@@ -134,4 +270,21 @@ Application Options:
--waterfall Create a waterfall display
-w, --width=points Width in points to which to wrap lines or ellipsize
--wrap=word/char/word-char Text wrapping mode (needs a width to be set)
+*/
+
+
+
+
+/*
+to speed up things lets use tempfs
+
+to fstab adda like like this:
+
+//tmpfs /mnt/tmpfs tmpfs size=50m 0 0
+
+instead i can use /dev/shm as path...
+also i need a delete on interval function
+/dev/shm
+ -resize x640 -gravity center -crop 240x640+0+0
+ convert logo: -resize x640 -gravity center -crop 240x640+0+0 +repage space_crop.jpg
*/
View
18 test.js
@@ -1,5 +1,19 @@
-var pangoview=require('./index.js');
+//var pangoview=require('deps/node-pango-view');
+var pangoview=require('./index');
console.log('will use pango view');
// also ram disk might help
//pangoview.write('hello world',{},function(file){ console.log('file is:'+file); }); // later do fs.unlink(path, [callback])
-pangoview.write('שלום עולם',{font:'Arial 12'},function(file){ console.log('file is:'+file); });
+var subimages=[];
+
+function fn1(callback) { pangoview.write('שלום עולם',{font:'Arial 12'},function(file){ console.log('file is:'+file); fn2(); }); }
+function fn2(callback) { pangoview.write('שלום עולם',{font:'Arial 12'},function(file){ console.log('file is:'+file); fn3(); }); }
+function fn3(callback) { pangoview.write('שלום עולם',{font:'Arial 12'},function(file){ console.log('file is:'+file); fin(); }); }
+function fin(callback) { pangoview.compose('bgs/morguefile_97997.jpg',{}, subimages,{},callback) }
+
+function run() {
+fn1(callback)
+function (outfile){
+console.log('done')
+}
+
+}

0 comments on commit 9be5273

Please sign in to comment.