Import Ruty
This commit is contained in:
+13
@@ -0,0 +1,13 @@
|
||||
//inspired by bobnice: http://stackoverflow.com/questions/1595611/how-to-properly-create-a-custom-object-in-javascript
|
||||
var Format = require('../format');
|
||||
Function.prototype.subclass= function(base) {
|
||||
var c= Function.prototype.subclass.nonconstructor;
|
||||
c.prototype= base.prototype;
|
||||
this.prototype= new c();
|
||||
};
|
||||
Function.prototype.subclass.nonconstructor= function() {};
|
||||
|
||||
module.exports = Element = function(format){
|
||||
if(format === undefined) format = new Format();
|
||||
this.format = format;
|
||||
};
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
//thanks DTrejo for help using async
|
||||
var Utils = require("../rtf-utils"),
|
||||
Element = require('./element'),
|
||||
async = require('async');
|
||||
|
||||
module.exports = GroupElement = function(name, format) {
|
||||
Element.apply(this, [format]);
|
||||
this.elements = [];
|
||||
this.name = name;
|
||||
};
|
||||
|
||||
GroupElement.subclass(Element);
|
||||
|
||||
GroupElement.prototype.addElement = function(element){
|
||||
this.elements.push(element);
|
||||
};
|
||||
|
||||
GroupElement.prototype.getRTFCode = function(colorTable, fontTable, callback){
|
||||
var tasks = [];
|
||||
var rtf = "";
|
||||
this.elements.forEach(function(el, i) {
|
||||
if (el instanceof Element){
|
||||
tasks.push(function(cb) { el.getRTFCode(colorTable, fontTable, cb); });
|
||||
} else {
|
||||
tasks.push(function(cb) { cb(null, Utils.getRTFSafeText(el)); });
|
||||
}
|
||||
});
|
||||
|
||||
return async.parallel(tasks, function(err, results) {
|
||||
results.forEach(function(result) {
|
||||
rtf += result;
|
||||
});
|
||||
//formats the whole group
|
||||
rtf = format.formatText(rtf, colorTable, fontTable, false);
|
||||
return callback(null, rtf);
|
||||
});
|
||||
};
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
var Element = require('./element'),
|
||||
fs = require('fs'),
|
||||
imageinfo = require('imageinfo');
|
||||
|
||||
module.exports = ImageElement = function(path){
|
||||
Element.apply(this, arguments);
|
||||
this.path=path;
|
||||
this.dpi=300;
|
||||
};
|
||||
|
||||
ImageElement.subclass(Element);
|
||||
|
||||
ImageElement.prototype.getRTFCode = function(colorTable, fontTable, callback){
|
||||
var self = this;
|
||||
|
||||
//read image file
|
||||
return fs.readFile(this.path, function(err, buffer){
|
||||
|
||||
if(err) throw err;
|
||||
|
||||
//get image information and calculate twips based on DPI
|
||||
var info = imageinfo(buffer);
|
||||
var twipRatio = ((72/self.dpi) * 20);
|
||||
var twipWidth = Math.round(info.width * twipRatio),
|
||||
twipHeight = Math.round(info.height * twipRatio);
|
||||
|
||||
//output image information
|
||||
var output = "{\\pict\\pngblip\\picw" + info.width + "\\pich" + info.height +
|
||||
"\\picwgoal" + twipWidth + "\\pichgoal" + twipHeight + " ";
|
||||
|
||||
//output buffer to base16 (hex) and ensure prefixed 0 if single digit
|
||||
var bufSize = buffer.length;
|
||||
for(var i = 0; i < bufSize; i++){
|
||||
var hex = buffer[i].toString(16);
|
||||
output += (hex.length === 2) ? hex : "0" + hex;
|
||||
}
|
||||
|
||||
output += "}";
|
||||
return callback(null, output);
|
||||
});
|
||||
};
|
||||
+88
@@ -0,0 +1,88 @@
|
||||
var Utils = require("../rtf-utils"),
|
||||
Element = require('./element'),
|
||||
async = require('async');
|
||||
|
||||
//TODO: probably replace this with something from underscore
|
||||
function columnCount(target){
|
||||
var rows = target.length, max = 0, i;
|
||||
for(i = 0; i < rows; i++){
|
||||
if(target[i].length > max) max = target[i].length;
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
module.exports = TableElement = function(){
|
||||
Element.apply(this, arguments);
|
||||
this._data = [];
|
||||
this._rows = 0;
|
||||
this._cols = 0;
|
||||
};
|
||||
|
||||
TableElement.subclass(Element);
|
||||
|
||||
TableElement.prototype.addRow = function(row){
|
||||
this._data.push(row);
|
||||
};
|
||||
|
||||
TableElement.prototype.setData = function(data){
|
||||
this._data = data;
|
||||
};
|
||||
|
||||
function rowTask(row, numCols, colorTable, fontTable){
|
||||
return function(rowcb){
|
||||
var rowTasks = [];
|
||||
//generate tasks for each column
|
||||
row.forEach(function(el, i) {
|
||||
if (el instanceof Element){
|
||||
rowTasks.push(function(cb) { el.getRTFCode(colorTable, fontTable, cb); });
|
||||
} else {
|
||||
rowTasks.push(function(cb) { cb(null, Utils.getRTFSafeText(el)); });
|
||||
}
|
||||
});
|
||||
|
||||
//process the row
|
||||
return async.parallel(rowTasks, function(err, results) {
|
||||
var out = "";
|
||||
results.forEach(function(result) {
|
||||
out += (result + "\\cell ");
|
||||
});
|
||||
return rowcb(null, out);
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
TableElement.prototype.getRTFCode = function(colorTable, fontTable, callback){
|
||||
var rtf = "",
|
||||
i, j,
|
||||
tasks = [];
|
||||
|
||||
this._rows = this._data.length;
|
||||
this._cols = columnCount(this._data);
|
||||
|
||||
//eaech row requires all this data about columns
|
||||
var pre = "\\trowd\\trautofit1\\intbl";
|
||||
var post = "{\\trowd\\trautofit1\\intbl";
|
||||
//now do the first \cellx things
|
||||
for(j = 0; j<this._cols; j++){
|
||||
pre += "\\clbrdrt\\brdrs\\brdrw10\\clbrdrl\\brdrs\\brdrw10\\clbrdrb\\brdrs\\brdrw10\\clbrdrr\\brdrs\\brdrw10";
|
||||
pre += "\\cellx" + (j+1).toString();
|
||||
post += "\\cellx" + (j+1).toString();
|
||||
}
|
||||
post += "\\row }";
|
||||
|
||||
//generate tasks for each row
|
||||
for(i = 0; i < this._rows; i++){
|
||||
if(this._data[i]){
|
||||
tasks.push(rowTask(this._data[i], this._cols, colorTable, fontTable));
|
||||
}
|
||||
}
|
||||
|
||||
return async.parallel(tasks, function(err, results) {
|
||||
var rows = "";
|
||||
results.forEach(function(result) {
|
||||
rows += (pre + "{" + result + " }" + post);
|
||||
});
|
||||
rtf = "\\par" + rows + "\\pard";
|
||||
return callback(null, rtf);
|
||||
});
|
||||
};
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
var Element = require('./element');
|
||||
|
||||
module.exports = TextElement = function(text, format){
|
||||
Element.apply(this, [format]);
|
||||
this.text=text;
|
||||
};
|
||||
|
||||
TextElement.subclass(Element);
|
||||
|
||||
TextElement.prototype.getRTFCode = function(colorTable, fontTable, callback){
|
||||
return callback(null, this.format.formatText(this.text, colorTable, fontTable));
|
||||
};
|
||||
Reference in New Issue
Block a user