diff --git a/lib/canvas-svg/loader.js b/lib/canvas-svg/loader.js index 1bee07c..5698548 100644 --- a/lib/canvas-svg/loader.js +++ b/lib/canvas-svg/loader.js @@ -19,8 +19,9 @@ var checkAndReadFile = function (filename, callback) { path.exists(filename, function (exists) { if (exists) return fs.readFile(filename, callback); throw new Error("Cannot find file '" + filename + - "' but it is hardly required.\nTry git submodules update --init "+ - "to get all dependencies."); + "' but it is hardly required.\n"+ + "It should be already in the repo. If not get it from:"+ + " http://code.google.com/p/canvg/"); }); }; @@ -28,10 +29,10 @@ var checkAndReadFile = function (filename, callback) { // main -result = function () { +var result = function () { var args = new Vargs(arguments); if (result.debug) console.log("* loading canvg rgbcolor …"); - checkAndReadFile(depdir+'/lib/canvg/rgbcolor.js', function (err, ccode) { + checkAndReadFile(depdir+'/canvg/rgbcolor.js', function (err, rcode) { if (err) return args.callback(err); @@ -39,7 +40,8 @@ result = function () { if(rcode) code.push(rcode); - checkAndReadFile(depdir+'/lib/canvg/canvg.js', function (err, ccode) { + if (result.debug) console.log("* loading canvg …"); + checkAndReadFile(depdir+'/canvg/canvg.js', function (err, ccode) { if (err) return args.callback(err); if (ccode) diff --git a/lib/canvas-svg/require.js b/lib/canvas-svg/require.js index 74bfdfe..6dc0c56 100644 --- a/lib/canvas-svg/require.js +++ b/lib/canvas-svg/require.js @@ -15,7 +15,7 @@ module.exports = function (dir) { try { return require(path) } catch (e) { - if (e.message !== "Cannot find module '" + path + "'") + if (e.message.slice(0,18) !== "Cannot find module") throw e; return check_next(); } diff --git a/lib/node-canvas-svg.js b/lib/node-canvas-svg.js index c1e08ae..401e67b 100644 --- a/lib/node-canvas-svg.js +++ b/lib/node-canvas-svg.js @@ -15,9 +15,18 @@ var jsdom = _require('jsdom', 'jsdom/lib/jsdom'), var svg = exports.svg = {}; -exports.svg.render = function (svg, /*options, callback*/) { - var args = new Vargs(argmuents); - if (!svg) +var check_canvg = function () { + if (svg.canvg) return true; + throw new Error("You have to load Canvg first."+ + "\nIt should be already in the repo. If not get it from:"+ + " http://code.google.com/p/canvg/"); +}; + + +exports.svg.render = function (svgdata/*, options, callback*/) { + check_canvg(); + var args = new Vargs(arguments); + if (!svgdata) return args.callback(new Error("no svg given"), new Canvas(42, 42)); var doc = jsdom.jsdom(), @@ -30,7 +39,7 @@ exports.svg.render = function (svg, /*options, callback*/) { };}; win.DOMParser = true; - var obj = jsdom.jsdom("" + svg + ""); // FIXME meh :( + var obj = jsdom.jsdom("" + svgdata + ""); // FIXME meh :( obj = obj.body.firstChild; var canvas = new Canvas( parseInt(obj.getAttribute('width')), @@ -56,8 +65,12 @@ exports.svg.render = function (svg, /*options, callback*/) { options[key] = defaults[key]; } - svg.canvg(win, doc, DOMParser)(canvas, svg, options); + try { + svg.canvg(win, doc, DOMParser)(canvas, svgdata, options); + } catch(e) { + args.callback(e, canvas); + } }; - +exports.load = require('./canvas-svg/loader')(depdir, vargs, svg);