Some additional JSLint; update jsPDF and SVG plugin; handle canvg for image export within svgcanvas and provide new "datauri" to "exported" event.
git-svn-id: http://svg-edit.googlecode.com/svn/trunk@2875 eee81c28-f429-11dd-99c0-75d572ba1dddmaster
parent
c9544ce5bf
commit
6f7651299c
|
@ -2,6 +2,8 @@
|
|||
/*jslint vars: true */
|
||||
var initEmbed;
|
||||
|
||||
// Todo: Get rid of frame.contentWindow dependencies so can be more easily adjusted to work cross-domain
|
||||
|
||||
$(function () {'use strict';
|
||||
|
||||
var svgCanvas = null;
|
||||
|
@ -35,6 +37,7 @@ $(function () {'use strict';
|
|||
|
||||
function exportPNG() {
|
||||
var str = frame.contentWindow.svgEditor.uiStrings.notification.loadingImage;
|
||||
|
||||
var exportWindow = window.open(
|
||||
'data:text/html;charset=utf-8,' + encodeURIComponent('<title>' + str + '</title><h1>' + str + '</h1>'),
|
||||
'svg-edit-exportWindow'
|
||||
|
|
File diff suppressed because one or more lines are too long
|
@ -1,8 +1,11 @@
|
|||
/*globals RGBColor, DOMParser, jsPDF*/
|
||||
/*jslint eqeq:true, vars:true*/
|
||||
/*
|
||||
* svgToPdf.js
|
||||
*
|
||||
* Copyright 2012 Florian Hülsmann <fh@cbix.de>
|
||||
* Updated in 2013 by Datascope Analytics <info@datascopeanalytics.com>
|
||||
* Copyright 2012-2014 Florian Hülsmann <fh@cbix.de>
|
||||
* Copyright 2014 Ben Gribaudo <www.bengribaudo.com>
|
||||
*
|
||||
* This script is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
|
@ -18,55 +21,57 @@
|
|||
*
|
||||
*/
|
||||
|
||||
(function(jsPDFAPI, undef) {
|
||||
'use strict';
|
||||
|
||||
// These are all of the currently supported attributes of the svg elements
|
||||
// that we are converting to PDF. The path is empty and taken care of in a
|
||||
// different way below.
|
||||
var pdfSvgAttr = {
|
||||
// allowed attributes. all others are removed from the preview.
|
||||
g: ['stroke', 'fill', 'stroke-width'],
|
||||
line: ['x1', 'y1', 'x2', 'y2', 'stroke', 'stroke-width', 'fill'],
|
||||
line: ['x1', 'y1', 'x2', 'y2', 'stroke', 'stroke-width'],
|
||||
rect: ['x', 'y', 'width', 'height', 'stroke', 'fill', 'stroke-width'],
|
||||
ellipse: ['cx', 'cy', 'rx', 'ry', 'stroke', 'fill', 'stroke-width'],
|
||||
circle: ['cx', 'cy', 'r', 'stroke', 'fill', 'stroke-width'],
|
||||
text: ['x', 'y', 'font-size', 'font-family', 'text-anchor', 'font-weight', 'font-style', 'fill'],
|
||||
path: []
|
||||
text: ['x', 'y', 'font-size', 'font-family', 'text-anchor', 'font-weight', 'font-style', 'fill']
|
||||
};
|
||||
|
||||
var attributeIsNotEmpty = function (node, attr) {
|
||||
var attVal = attr ? node.getAttribute(attr) : node;
|
||||
return attVal !== '' && attVal !== null;
|
||||
};
|
||||
|
||||
var nodeIs = function (node, possible) {
|
||||
return possible.indexOf(node.tagName.toLowerCase()) > -1;
|
||||
};
|
||||
|
||||
var removeAttributes = function(node, attributes) {
|
||||
var toRemove = [];
|
||||
[].forEach.call(node.attributes, function(a) {
|
||||
if (attributeIsNotEmpty(a) && attributes.indexOf(a.name.toLowerCase()) == -1) {
|
||||
toRemove.push(a.name);
|
||||
}
|
||||
});
|
||||
|
||||
toRemove.forEach(function(a) {
|
||||
node.removeAttribute(a.name);
|
||||
});
|
||||
};
|
||||
|
||||
var svgElementToPdf = function(element, pdf, options) {
|
||||
// pdf is a jsPDF object
|
||||
var remove = (typeof(options.removeInvalid) == 'undefined' ? remove = false : options.removeInvalid);
|
||||
var k = (typeof(options.removeInvalid) == 'undefined' ? 1.0 : options.scale);
|
||||
|
||||
var x_offset = (typeof(options.x_offset) == 'undefined' ? 0 : options.x_offset);
|
||||
var y_offset = (typeof(options.y_offset) == 'undefined' ? 0 : options.y_offset);
|
||||
var ego_or_link = (typeof(options.ego_or_link) == 'undefined' ? false : options.ego_or_link);
|
||||
//console.log("options =", options);
|
||||
var remove = (options.removeInvalid == undef ? false : options.removeInvalid);
|
||||
var k = (options.scale == undef ? 1.0 : options.scale);
|
||||
var colorMode = null;
|
||||
$(element).children().each(function(i, node) {
|
||||
var n = $(node);
|
||||
if (n.is("g")) {
|
||||
if (n.attr('transform') != null) {
|
||||
var matrix = n.attr('transform').replace(/,/g, ' ').replace('matrix(', '').replace(')', ' cm');
|
||||
pdf.internal.write('q');
|
||||
pdf.internal.write(matrix);
|
||||
svgElementToPdf(n, pdf, options);
|
||||
pdf.internal.write('Q');
|
||||
} else {
|
||||
svgElementToPdf(n, pdf, options);
|
||||
}
|
||||
}
|
||||
|
||||
[].forEach.call(element.children, function(node) {
|
||||
//console.log("passing: ", node);
|
||||
var hasFillColor = false;
|
||||
var hasStrokeColor = false;
|
||||
if (n.is('g,line,rect,ellipse,circle,text,path')) {
|
||||
|
||||
var fillColor = n.attr('fill');
|
||||
if (fillColor != null) {
|
||||
var fillRGB = new RGBColor(fillColor);
|
||||
|
||||
var fillRGB;
|
||||
if(nodeIs(node, ['g', 'line', 'rect', 'ellipse', 'circle', 'text'])) {
|
||||
var fillColor = node.getAttribute('fill');
|
||||
if(attributeIsNotEmpty(fillColor)) {
|
||||
fillRGB = new RGBColor(fillColor);
|
||||
if(fillRGB.ok) {
|
||||
//console.log("fillRGB is okay");
|
||||
hasFillColor = true;
|
||||
colorMode = 'F';
|
||||
} else {
|
||||
|
@ -74,596 +79,149 @@ var svgElementToPdf = function(element, pdf, options) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(nodeIs(node, ['g', 'line', 'rect', 'ellipse', 'circle'])) {
|
||||
if(hasFillColor) {
|
||||
|
||||
pdf.setFillColor(fillRGB.r, fillRGB.g, fillRGB.b);
|
||||
}
|
||||
|
||||
var strokeColor = n.attr('stroke');
|
||||
if (strokeColor != null && strokeColor !== 'none') {
|
||||
|
||||
if(attributeIsNotEmpty(node, 'stroke-width')) {
|
||||
pdf.setLineWidth(k * parseInt(node.getAttribute('stroke-width'), 10));
|
||||
}
|
||||
var strokeColor = node.getAttribute('stroke');
|
||||
if(attributeIsNotEmpty(strokeColor)) {
|
||||
var strokeRGB = new RGBColor(strokeColor);
|
||||
if(strokeRGB.ok) {
|
||||
hasStrokeColor = true;
|
||||
|
||||
pdf.setDrawColor(strokeRGB.r, strokeRGB.g, strokeRGB.b);
|
||||
if(colorMode == 'F') {
|
||||
colorMode = 'FD';
|
||||
} else {
|
||||
|
||||
colorMode = "S";
|
||||
colorMode = null;
|
||||
}
|
||||
} else {
|
||||
|
||||
colorMode = null;
|
||||
}
|
||||
}
|
||||
switch (n.get(0).tagName.toLowerCase()) {
|
||||
}
|
||||
switch(node.tagName.toLowerCase()) {
|
||||
case 'svg':
|
||||
case 'a':
|
||||
case 'g':
|
||||
svgElementToPdf(node, pdf, options);
|
||||
removeAttributes(node, pdfSvgAttr.g);
|
||||
break;
|
||||
case 'line':
|
||||
pdf.line(
|
||||
k * parseInt(n.attr('x1')),
|
||||
k * parseInt(n.attr('y1')),
|
||||
k * parseInt(n.attr('x2')),
|
||||
k * parseInt(n.attr('y2')));
|
||||
|
||||
// $.each(node.attributes, function(i, a) {
|
||||
// if(a != null && pdfSvgAttr.line.indexOf(a.name.toLowerCase()) == -1) {
|
||||
// node.removeAttribute(a.name);
|
||||
// }
|
||||
// });
|
||||
k*parseInt(node.getAttribute('x1'), 10),
|
||||
k*parseInt(node.getAttribute('y1'), 10),
|
||||
k*parseInt(node.getAttribute('x2'), 10),
|
||||
k*parseInt(node.getAttribute('y2'), 10)
|
||||
);
|
||||
removeAttributes(node, pdfSvgAttr.line);
|
||||
break;
|
||||
case 'rect':
|
||||
//console.log("in rectangle");
|
||||
pdf.rect(
|
||||
k * (parseInt(n.attr('x')) + x_offset),
|
||||
k * (parseInt(n.attr('y')) + y_offset),
|
||||
k * parseInt(n.attr('width')),
|
||||
k * parseInt(n.attr('height')),
|
||||
colorMode);
|
||||
// $.each(node.attributes, function(i, a) {
|
||||
// if(a != null && pdfSvgAttr.rect.indexOf(a.name.toLowerCase()) == -1) {
|
||||
// node.removeAttribute(a.name);
|
||||
// }
|
||||
// });
|
||||
k*parseInt(node.getAttribute('x'), 10),
|
||||
k*parseInt(node.getAttribute('y'), 10),
|
||||
k*parseInt(node.getAttribute('width'), 10),
|
||||
k*parseInt(node.getAttribute('height'), 10),
|
||||
colorMode
|
||||
);
|
||||
removeAttributes(node, pdfSvgAttr.rect);
|
||||
break;
|
||||
case 'ellipse':
|
||||
pdf.ellipse(
|
||||
k * parseInt(n.attr('cx')),
|
||||
k * parseInt(n.attr('cy')),
|
||||
k * parseInt(n.attr('rx')),
|
||||
k * parseInt(n.attr('ry')),
|
||||
colorMode);
|
||||
// $.each(node.attributes, function(i, a) {
|
||||
// if(a != null && pdfSvgAttr.ellipse.indexOf(a.name.toLowerCase()) == -1) {
|
||||
// node.removeAttribute(a.name);
|
||||
// }
|
||||
// });
|
||||
k*parseInt(node.getAttribute('cx'), 10),
|
||||
k*parseInt(node.getAttribute('cy'), 10),
|
||||
k*parseInt(node.getAttribute('rx'), 10),
|
||||
k*parseInt(node.getAttribute('ry'), 10),
|
||||
colorMode
|
||||
);
|
||||
removeAttributes(node, pdfSvgAttr.ellipse);
|
||||
break;
|
||||
case 'circle':
|
||||
pdf.circle(
|
||||
k * (parseInt(n.attr('cx')) + x_offset),
|
||||
k * (parseInt(n.attr('cy')) + y_offset),
|
||||
k * parseInt(n.attr('r')),
|
||||
colorMode);
|
||||
// $.each(node.attributes, function(i, a) {
|
||||
// if(a != null && pdfSvgAttr.circle.indexOf(a.name.toLowerCase()) == -1) {
|
||||
// node.removeAttribute(a.name);
|
||||
// }
|
||||
// });
|
||||
k*parseInt(node.getAttribute('cx'), 10),
|
||||
k*parseInt(node.getAttribute('cy'), 10),
|
||||
k*parseInt(node.getAttribute('r'), 10),
|
||||
colorMode
|
||||
);
|
||||
removeAttributes(node, pdfSvgAttr.circle);
|
||||
break;
|
||||
case 'text':
|
||||
var fontFamily = 'helvetica';
|
||||
if(node.hasAttribute('font-family')) {
|
||||
|
||||
switch (n.attr('font-family').toLowerCase().split(',')[0]) {
|
||||
case 'times':
|
||||
var fontFamily = 'times';
|
||||
pdf.setFont('times');
|
||||
break;
|
||||
case 'courier':
|
||||
var fontFamily = 'courier';
|
||||
pdf.setFont('courier');
|
||||
break;
|
||||
|
||||
switch((node.getAttribute('font-family') || '').toLowerCase()) {
|
||||
case 'serif': pdf.setFont('times'); break;
|
||||
case 'monospace': pdf.setFont('courier'); break;
|
||||
default:
|
||||
node.setAttribute('font-family', 'sans-serif');
|
||||
pdf.setFont('helvetica');
|
||||
}
|
||||
}
|
||||
|
||||
// If no font family, we set the font to the times by default
|
||||
else {
|
||||
pdf.setFont('helvetica');
|
||||
if(hasFillColor) {
|
||||
pdf.setTextColor(fillRGB.r, fillRGB.g, fillRGB.b);
|
||||
}
|
||||
|
||||
// if (colorMode !== "FD" || colorMode !== 'F') {
|
||||
// console.log("I'm setting text color");
|
||||
// pdf.setTextColor(fillRGB.r, fillRGB.g, fillRGB.b);
|
||||
// }
|
||||
|
||||
var fontType = "normal";
|
||||
var fontType = "";
|
||||
if(node.hasAttribute('font-weight')) {
|
||||
if (n.attr('font-weight') == "bold") {
|
||||
if(node.getAttribute('font-weight') == "bold") {
|
||||
fontType = "bold";
|
||||
} else {
|
||||
node.removeAttribute('font-weight');
|
||||
}
|
||||
}
|
||||
|
||||
if(node.hasAttribute('font-style')) {
|
||||
if (n.attr('font-style') == "italic") {
|
||||
if(node.getAttribute('font-style') == "italic") {
|
||||
fontType += "italic";
|
||||
} else {
|
||||
node.removeAttribute('font-style');
|
||||
}
|
||||
}
|
||||
|
||||
pdf.setFontType(fontType);
|
||||
var pdfFontSize = 9;
|
||||
var pdfFontSize = 16;
|
||||
if(node.hasAttribute('font-size')) {
|
||||
pdfFontSize = parseInt(n.attr('font-size'));
|
||||
pdfFontSize = parseInt(node.getAttribute('font-size'), 10);
|
||||
}
|
||||
|
||||
var fontMetrics = pdf.internal.getFont(fontFamily, fontType)
|
||||
.metadata.Unicode;
|
||||
|
||||
var text_value = n.text();
|
||||
var name_length = pdf.getStringUnitWidth(
|
||||
text_value,
|
||||
fontMetrics) * pdfFontSize;
|
||||
|
||||
|
||||
var box = node.getBBox();
|
||||
//FIXME: use more accurate positioning!!
|
||||
var label_offset = 0;
|
||||
var x = parseInt(n.attr('x')) + x_offset - label_offset;
|
||||
var y = parseInt(n.attr('y')) + y_offset;
|
||||
|
||||
|
||||
|
||||
var x, y, xOffset = 0;
|
||||
if(node.hasAttribute('text-anchor')) {
|
||||
switch (n.attr('text-anchor')) {
|
||||
case 'end':
|
||||
break;
|
||||
case 'middle':
|
||||
label_offset = name_length / 2;
|
||||
break;
|
||||
case 'start':
|
||||
label_offset = 0;
|
||||
break;
|
||||
case 'default':
|
||||
label_offset = 0;
|
||||
break;
|
||||
switch(node.getAttribute('text-anchor')) {
|
||||
case 'end': xOffset = box.width; break;
|
||||
case 'middle': xOffset = box.width / 2; break;
|
||||
case 'start': break;
|
||||
case 'default': node.setAttribute('text-anchor', 'start'); break;
|
||||
}
|
||||
x = parseInt(n.attr('x')) + x_offset - label_offset;
|
||||
y = parseInt(n.attr('y')) + y_offset;
|
||||
x = parseInt(node.getAttribute('x'), 10) - xOffset;
|
||||
y = parseInt(node.getAttribute('y'), 10);
|
||||
}
|
||||
|
||||
|
||||
|
||||
//console.log("fontSize:", pdfFontSize, "text:", node.textContent);
|
||||
pdf.setFontSize(pdfFontSize).text(
|
||||
k * x,
|
||||
k * y,
|
||||
text_value);
|
||||
// $.each(node.attributes, function(i, a) {
|
||||
// if(a != null && pdfSvgAttr.text.indexOf(a.name.toLowerCase()) == -1) {
|
||||
// node.removeAttribute(a.name);
|
||||
// }
|
||||
// });
|
||||
break;
|
||||
case 'path':
|
||||
// //console.log("node is: ",node.attributes)
|
||||
// $.each(node.attributes, function(i, a) {
|
||||
// //console.log(pdfSvgAttr.path.indexOf(a.name.toLowerCase()));
|
||||
// });
|
||||
|
||||
var updated_path = n.attr('d');
|
||||
// Separate the svg 'd' string to a list of letter
|
||||
// and number elements. Iterate on this list.
|
||||
// console.log('path before',path);
|
||||
svg_regex = /(m|l|h|v|c|s|a|z)/gi;
|
||||
// console.log('TESTING REGULAR
|
||||
// EXPRESSION',svg_regex.test('m')); The crazy ie9 case
|
||||
// where they take our path and make some crazy scientific
|
||||
// notation
|
||||
updated_path = updated_path.replace(/(e)?-/g, function($0, $1) {
|
||||
return $1 ? $0 : ' -';
|
||||
})
|
||||
// .replace(/-/g, ' -')
|
||||
.replace(svg_regex, ' $1 ')
|
||||
.replace(/,+/g, ' ')
|
||||
.replace(/^\s+|\s+$/g, '')
|
||||
.split(/[\s]+/);
|
||||
|
||||
var svg_element = null;
|
||||
var i = 0;
|
||||
|
||||
// mx and my will define the starting points from each m/M case
|
||||
var mx = null;
|
||||
var my = null;
|
||||
// x and y will redefine the starting points
|
||||
var x = null;
|
||||
var y = null;
|
||||
|
||||
// big list contains the large list of lists to pass to
|
||||
// jspdf to render the appropriate path
|
||||
var big_list = [];
|
||||
|
||||
// for S/s shorthand bezier calculations of 2nd control pts
|
||||
var previous_element = {
|
||||
element: null,
|
||||
prev_numbers: [],
|
||||
point: []
|
||||
};
|
||||
var m_flag = 0;
|
||||
// Go through our list until we are done with the updated_path
|
||||
while (i < updated_path.length) {
|
||||
|
||||
// Numbers will hold the list of numbers for the
|
||||
// appropriate updated_path
|
||||
var numbers = [];
|
||||
|
||||
// svg_element is a letter corresponding to the type
|
||||
// of updated_path to draw
|
||||
var sci_regex = /[+\-]?(?:0|[1-9]\d*)(?:\.\d*)?(?:[eE][+\-]?\d+)?/;
|
||||
var svg_element = updated_path[i];
|
||||
if (sci_regex.test(svg_element)) {
|
||||
svg_element = String(Number(svg_element));
|
||||
}
|
||||
i++
|
||||
//if svg_element is s/S, need to find 1st control pts
|
||||
if (/s/i.test(svg_element)) {
|
||||
previous_element.point = find_s_points(svg_element,
|
||||
previous_element);
|
||||
}
|
||||
|
||||
// for some reason z followed by another letter
|
||||
// i.e. 'z m' skips that 2nd letter, so added if
|
||||
// statement to get around that.
|
||||
if (/z/i.test(svg_element) == false) {
|
||||
|
||||
// Parse through the updated_path until we find the next
|
||||
// letter or we are at the end of the updated_path
|
||||
while ((svg_regex.test(updated_path[i]) == false) && (i != updated_path.length)) {
|
||||
numbers.push(k * parseFloat(updated_path[i]));
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
switch (svg_element) {
|
||||
case 'm':
|
||||
//paths and subpaths must always start with m/M.
|
||||
//thus we call pdf.lines
|
||||
if (big_list.length != 0) {
|
||||
pdf.lines(big_list, mx, my, [1, 1], null);
|
||||
}
|
||||
big_list = [];
|
||||
|
||||
// check if this is 1st command in the path
|
||||
if (previous_element.element == null) {
|
||||
x = numbers[0];
|
||||
mx = numbers[0];
|
||||
y = numbers[1];
|
||||
my = numbers[1];
|
||||
} else {
|
||||
x += numbers[0];
|
||||
mx += numbers[0];
|
||||
y += numbers[1];
|
||||
my += numbers[1];
|
||||
}
|
||||
if (numbers.length != 2) {
|
||||
var lines_numbers = numbers.slice(2, numbers.length);
|
||||
|
||||
var new_numbers = change_numbers(lines_numbers,
|
||||
x, y, true);
|
||||
_.each(new_numbers, function(num) {
|
||||
big_list.push(num);
|
||||
});
|
||||
// pdf.lines(new_numbers,x,y,[1,1],null);
|
||||
x += sums(new_numbers, true);
|
||||
y += sums(new_numbers, false);
|
||||
}
|
||||
break;
|
||||
case 'M':
|
||||
if (big_list.length != 0) {
|
||||
pdf.lines(big_list, mx, my, [1, 1], null);
|
||||
}
|
||||
big_list = [];
|
||||
|
||||
x = numbers[0];
|
||||
mx = numbers[0] + x_offset;
|
||||
y = numbers[1];
|
||||
my = numbers[1] + y_offset;
|
||||
|
||||
if (numbers.length != 2) {
|
||||
x = numbers[0];
|
||||
y = numbers[1];
|
||||
var lines_numbers = numbers.slice(2, numbers.length);
|
||||
var new_numbers = change_numbers(lines_numbers,
|
||||
x, y, false);
|
||||
pdf.lines(new_numbers, x, y, [1, 1], null);
|
||||
x += new_numbers[new_numbers.length - 1][0];
|
||||
y += new_numbers[new_numbers.length - 1][1];
|
||||
|
||||
}
|
||||
break;
|
||||
case 'l':
|
||||
var new_numbers = change_numbers(numbers, x, y, true);
|
||||
_.each(new_numbers, function(num) {
|
||||
big_list.push(num);
|
||||
});
|
||||
// pdf.lines(new_numbers,x,y,[1,1],null);
|
||||
x += sums(new_numbers, true);
|
||||
y += sums(new_numbers, false);
|
||||
break;
|
||||
case 'L':
|
||||
var new_numbers = change_numbers(numbers, x, y, false);
|
||||
_.each(new_numbers, function(num) {
|
||||
big_list.push(num);
|
||||
});
|
||||
//pdf.lines(new_numbers,x,y,[1,1],null);
|
||||
x += new_numbers[new_numbers.length - 1][0];
|
||||
y += new_numbers[new_numbers.length - 1][1];
|
||||
break;
|
||||
case 'h':
|
||||
// x does not change. Only y changes
|
||||
var sum = _.reduce(numbers,
|
||||
|
||||
function(memo, num) {
|
||||
return memo + num;
|
||||
}, 0);
|
||||
var new_numbers = [
|
||||
[sum, 0]
|
||||
];
|
||||
_.each(new_numbers, function(num) {
|
||||
big_list.push(num);
|
||||
});
|
||||
// pdf.lines([[sum,0]],x,y,[1,1],null);
|
||||
x += sum;
|
||||
break;
|
||||
case 'H':
|
||||
big_list.push([numbers[numbers.length - 1] - x, 0]);
|
||||
// pdf.lines([[numbers[numbers.length-1]-x,0]],
|
||||
// x,y,[1,1],null);
|
||||
x = numbers[numbers.length - 1];
|
||||
break;
|
||||
case 'v':
|
||||
var sum = _.reduce(numbers,
|
||||
|
||||
function(memo, num) {
|
||||
return memo + num;
|
||||
}, 0);
|
||||
var new_numbers = [
|
||||
[0, sum]
|
||||
];
|
||||
_.each(new_numbers, function(num) {
|
||||
big_list.push(num);
|
||||
});
|
||||
// pdf.lines([[0,sum]],x,y,[1,1],null);
|
||||
y += sum;
|
||||
break;
|
||||
case 'V':
|
||||
big_list.push([0, numbers[numbers.length - 1] - y]);
|
||||
// pdf.lines([[0,numbers[numbers.length-1]-y]],
|
||||
// x,y,[1,1],null);
|
||||
y = numbers[numbers.length - 1];
|
||||
break;
|
||||
case 'c':
|
||||
var new_numbers = bezier_numbers(numbers, x, y, true);
|
||||
_.each(new_numbers, function(num) {
|
||||
big_list.push(num);
|
||||
});
|
||||
// pdf.lines(new_numbers,x,y,[1,1],null);
|
||||
x += sums(new_numbers, true);
|
||||
y += sums(new_numbers, false);
|
||||
break;
|
||||
case 'C':
|
||||
var new_numbers = bezier_numbers(numbers, x, y, false);
|
||||
_.each(new_numbers, function(num) {
|
||||
big_list.push(num);
|
||||
});
|
||||
// pdf.lines(new_numbers,x,y,[1,1],null);
|
||||
x = numbers[numbers.length - 2];
|
||||
y = numbers[numbers.length - 1];
|
||||
break;
|
||||
case 's':
|
||||
var new_numbers = s_bezier_numbers(numbers, x, y,
|
||||
true, previous_element);
|
||||
_.each(new_numbers, function(num) {
|
||||
big_list.push(num);
|
||||
});
|
||||
// pdf.lines(new_numbers,x,y,[1,1],null);
|
||||
x += sums(new_numbers, true);
|
||||
y += sums(new_numbers, false);
|
||||
break;
|
||||
case 'S':
|
||||
var new_numbers = s_bezier_numbers(numbers, x, y, false,
|
||||
previous_element);
|
||||
_.each(new_numbers, function(num) {
|
||||
big_list.push(num);
|
||||
});
|
||||
// pdf.lines(new_numbers,x,y,[1,1],null);
|
||||
x = numbers[numbers.length - 2];
|
||||
y = numbers[numbers.length - 1];
|
||||
break;
|
||||
case 'A':
|
||||
// for now a hack.treat this as a line
|
||||
|
||||
|
||||
break;
|
||||
case 'a':
|
||||
|
||||
break;
|
||||
case 'z':
|
||||
big_list.push([mx - x, my - y]);
|
||||
x = mx;
|
||||
y = my;
|
||||
// pdf.lines([[mx-x,my-y]],x,y,[1,1],null);
|
||||
break;
|
||||
case 'Z':
|
||||
big_list.push([mx - x, my - y]);
|
||||
x = mx;
|
||||
y = my;
|
||||
// pdf.lines([[mx-x,my-y]],x,y,[1,1],null);
|
||||
break;
|
||||
default:
|
||||
console.log('Sorry, the', svg_element, 'svg command is not yet available.');
|
||||
}
|
||||
previous_element.element = svg_element;
|
||||
previous_element.prev_numbers = numbers;
|
||||
}
|
||||
pdf.lines(big_list, mx, my, [1, 1], colorMode);
|
||||
var numbs = null;
|
||||
node.textContent
|
||||
);
|
||||
removeAttributes(node, pdfSvgAttr.text);
|
||||
break;
|
||||
//TODO: image
|
||||
default:
|
||||
//console.log("cannot identify: ", node);
|
||||
if (remove) {
|
||||
//console.log("can't translate to pdf:", node);
|
||||
n.remove();
|
||||
console.log("can't translate to pdf:", node);
|
||||
node.parentNode.removeChild(node);
|
||||
}
|
||||
}
|
||||
});
|
||||
return pdf;
|
||||
}
|
||||
};
|
||||
|
||||
function sums(ListOfLists, is_x) {
|
||||
if (is_x) {
|
||||
var sum = _.reduce(ListOfLists,
|
||||
jsPDFAPI.addSVG = function(element, x, y, options) {
|
||||
|
||||
function(memo, num) {
|
||||
return memo + num[num.length - 2];
|
||||
}, 0);
|
||||
} else {
|
||||
var sum = _.reduce(ListOfLists,
|
||||
options = (options === undef ? {} : options);
|
||||
options.x_offset = x;
|
||||
options.y_offset = y;
|
||||
|
||||
function(memo, num) {
|
||||
return memo + num[num.length - 1];
|
||||
}, 0);
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
function change_numbers(numbers, x, y, relative) {
|
||||
var i = 0;
|
||||
var prev_x = x;
|
||||
var prev_y = y;
|
||||
var new_numbers = [];
|
||||
while (i < numbers.length) {
|
||||
if (relative) {
|
||||
x = numbers[i];
|
||||
y = numbers[i + 1];
|
||||
} else {
|
||||
x = numbers[i] - prev_x;
|
||||
y = numbers[i + 1] - prev_y;
|
||||
}
|
||||
prev_x = numbers[i];
|
||||
prev_y = numbers[i + 1];
|
||||
new_numbers.push([x, y]);
|
||||
i += 2;
|
||||
}
|
||||
return new_numbers;
|
||||
}
|
||||
|
||||
function bezier_numbers(numbers, x, y, relative) {
|
||||
// the bezier numbers are ALL relative to the
|
||||
// previous case line's (x,y), not all relative to
|
||||
// each other.
|
||||
var i = 0;
|
||||
var prev_x = x;
|
||||
var prev_y = y;
|
||||
var new_numbers = [];
|
||||
while (i < numbers.length) {
|
||||
if (relative) {
|
||||
var numbers_to_push = numbers.slice(i, i + 6);
|
||||
} else {
|
||||
var numbers_to_push = [];
|
||||
for (var k = i; k < i + 6; k = k + 2) {
|
||||
numbers_to_push.push(
|
||||
numbers[k] - prev_x,
|
||||
numbers[k + 1] - prev_y);
|
||||
}
|
||||
}
|
||||
prev_x = numbers[i + 4];
|
||||
prev_y = numbers[i + 5];
|
||||
new_numbers.push(numbers_to_push);
|
||||
i += 6;
|
||||
}
|
||||
return new_numbers;
|
||||
}
|
||||
|
||||
function s_bezier_numbers(numbers, x, y, relative,
|
||||
previous_element) {
|
||||
var i = 0;
|
||||
var prev_x = x;
|
||||
var prev_y = y;
|
||||
var new_numbers = [];
|
||||
while (i < numbers.length) {
|
||||
var numbers_to_push = [];
|
||||
//need to check if relative for the 4 s/S numbers
|
||||
if (relative) {
|
||||
//find that 1st control point
|
||||
if (i < 4 && (previous_element.element == 'c' || previous_element.element == 's')) {
|
||||
// case 1: there was a prev c/C/s/S
|
||||
// outside this numbers segment
|
||||
numbers_to_push.push(previous_element.point[0],
|
||||
previous_element.point[1]);
|
||||
} else if (i >= 4) {
|
||||
//case 1: there was a prev s/S
|
||||
//within this numbers segment
|
||||
numbers_to_push.push(numbers[i - 2] - numbers[i - 4],
|
||||
numbers[i - 1] - numbers[i - 3]);
|
||||
} else {
|
||||
// case 2: no prev c/C/s/S, therefore
|
||||
// 1st control pt = current pt.
|
||||
numbers_to_push.push(prev_x, prev_y);
|
||||
}
|
||||
//then add the rest of the s numbers
|
||||
for (var k = i; k < i + 4; k++) {
|
||||
numbers_to_push.push(numbers[k]);
|
||||
}
|
||||
} else {
|
||||
//find that 1st control point
|
||||
if (i < 4 && (previous_element.element == 'C' || previous_element.element == 'S')) {
|
||||
// case 1: there was a prev c/C/s/S
|
||||
// outside this numbers segment
|
||||
numbers_to_push.push(previous_element.point[0] - prev_x,
|
||||
previous_element.point[1] - prev_y);
|
||||
} else if (i >= 4) {
|
||||
//case 1: there was a prev s/S
|
||||
//within this numbers segment
|
||||
numbers_to_push.push(numbers[i - 2] + numbers[i - 2] - numbers[i - 4],
|
||||
numbers[i - 1] + numbers[i - 1] - numbers[i - 3]);
|
||||
} else {
|
||||
// case 2: no prev c/C/s/S, therefore
|
||||
// 1st control pt = current pt.
|
||||
numbers_to_push.push(prev_x, prev_y);
|
||||
}
|
||||
//then add the rest of the s numbers
|
||||
for (var k = i; k < i + 4; k = k + 2) {
|
||||
numbers_to_push.push(numbers[k] - prev_x);
|
||||
numbers_to_push.push(numbers[k + 1] - prev_y);
|
||||
}
|
||||
}
|
||||
|
||||
prev_x = numbers[i + 2];
|
||||
prev_y = numbers[i + 3];
|
||||
|
||||
new_numbers.push(numbers_to_push);
|
||||
i += 4;
|
||||
}
|
||||
return new_numbers;
|
||||
}
|
||||
|
||||
function find_s_points(svg_element, previous_element) {
|
||||
if (/(s|c)/.test(previous_element.element)) {
|
||||
numbs = previous_element.prev_numbers;
|
||||
previous_element.point = [numbs[numbs.length - 2] - numbs[numbs.length - 4],
|
||||
numbs[numbs.length - 1] - numbs[numbs.length - 3]];
|
||||
} else if (/(S|C)/.test(previous_element.element)) {
|
||||
numbs = previous_element.prev_numbers;
|
||||
previous_element.point = [2 * numbs[numbs.length - 2] - numbs[numbs.length - 4],
|
||||
2 * numbs[numbs.length - 1] - numbs[numbs.length - 3]];
|
||||
}
|
||||
return previous_element.point
|
||||
if (typeof element === 'string') {
|
||||
element = new DOMParser().parseFromString(element, 'text/xml').documentElement;
|
||||
}
|
||||
svgElementToPdf(element, this, options);
|
||||
return this;
|
||||
};
|
||||
}(jsPDF.API));
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
/*globals svgEditor:true, globalStorage, widget, svgedit, canvg, svgElementToPdf, jQuery, $, DOMParser, FileReader */
|
||||
/*globals svgEditor:true, globalStorage, widget, svgedit, canvg, jQuery, $, DOMParser, FileReader */
|
||||
/*jslint vars: true, eqeq: true, todo: true, forin: true, continue: true, regexp: true */
|
||||
/*
|
||||
* svg-editor.js
|
||||
|
@ -409,11 +409,11 @@ TODOS
|
|||
}
|
||||
if (opts.exportImage) {
|
||||
customExportImage = opts.exportImage;
|
||||
svgCanvas.bind('exported', Utils.buildCanvgCallback(customExportImage));
|
||||
svgCanvas.bind('exported', customExportImage); // canvg and our RGBColor will be available to the method
|
||||
}
|
||||
if (opts.exportPDF) {
|
||||
customExportPDF = opts.exportPDF;
|
||||
svgCanvas.bind('exportedPDF', Utils.buildJSPDFCallback(customExportPDF));
|
||||
svgCanvas.bind('exportedPDF', customExportPDF); // jsPDF and our RGBColor will be available to the method
|
||||
}
|
||||
});
|
||||
};
|
||||
|
@ -1071,26 +1071,16 @@ TODOS
|
|||
|
||||
var exportHandler = function(win, data) {
|
||||
var issues = data.issues,
|
||||
type = data.type || 'PNG',
|
||||
exportWindowName = data.exportWindowName;
|
||||
|
||||
if (exportWindowName) {
|
||||
exportWindow = window.open('', exportWindowName); // A hack to get the window via JSON-able name without opening a new one
|
||||
}
|
||||
if (!$('#export_canvas').length) {
|
||||
$('<canvas>', {id: 'export_canvas'}).hide().appendTo('body');
|
||||
}
|
||||
var c = $('#export_canvas')[0];
|
||||
c.width = svgCanvas.contentW;
|
||||
c.height = svgCanvas.contentH;
|
||||
|
||||
canvg(c, data.svg, {renderCallback: function() {
|
||||
var dataURLType = (type === 'ICO' ? 'BMP' : type).toLowerCase();
|
||||
var datauri = data.quality ? c.toDataURL('image/' + dataURLType, data.quality) : c.toDataURL('image/' + dataURLType);
|
||||
exportWindow.location.href = datauri;
|
||||
exportWindow.location.href = data.datauri;
|
||||
var done = $.pref('export_notice_done');
|
||||
if (done !== 'all') {
|
||||
var note = uiStrings.notification.saveFromBrowser.replace('%s', type);
|
||||
var note = uiStrings.notification.saveFromBrowser.replace('%s', data.type);
|
||||
|
||||
// Check if there's issues
|
||||
if (issues.length) {
|
||||
|
@ -1103,7 +1093,6 @@ TODOS
|
|||
$.pref('export_notice_done', 'all');
|
||||
exportWindow.alert(note);
|
||||
}
|
||||
}});
|
||||
};
|
||||
|
||||
var operaRepaint = function() {
|
||||
|
@ -2843,7 +2832,7 @@ TODOS
|
|||
svgCanvas.bind('transition', elementTransition);
|
||||
svgCanvas.bind('changed', elementChanged);
|
||||
svgCanvas.bind('saved', saveHandler);
|
||||
svgCanvas.bind('exported', Utils.buildCanvgCallback(exportHandler));
|
||||
svgCanvas.bind('exported', exportHandler);
|
||||
svgCanvas.bind('exportedPDF', function (win, data) {
|
||||
var exportWindowName = data.exportWindowName;
|
||||
if (exportWindowName) {
|
||||
|
|
|
@ -291,6 +291,7 @@ var InsertElementCommand = svgedit.history.InsertElementCommand;
|
|||
var RemoveElementCommand = svgedit.history.RemoveElementCommand;
|
||||
var ChangeElementCommand = svgedit.history.ChangeElementCommand;
|
||||
var BatchCommand = svgedit.history.BatchCommand;
|
||||
var call;
|
||||
// Implement the svgedit.history.HistoryEventHandler interface.
|
||||
canvas.undoMgr = new svgedit.history.UndoManager({
|
||||
handleHistoryEvent: function(eventType, cmd) {
|
||||
|
@ -395,7 +396,7 @@ $(opac_ani).attr({
|
|||
|
||||
var restoreRefElems = function(elem) {
|
||||
// Look for missing reference elements, restore any found
|
||||
var o, i,
|
||||
var o, i, l,
|
||||
attrs = $(elem).attr(ref_attrs);
|
||||
for (o in attrs) {
|
||||
var val = attrs[o];
|
||||
|
@ -850,7 +851,7 @@ var copyElem = function(el) {
|
|||
};
|
||||
|
||||
// Set scope for these functions
|
||||
var getId, getNextId, call;
|
||||
var getId, getNextId;
|
||||
var textActions, pathActions;
|
||||
|
||||
(function(c) {
|
||||
|
@ -1146,8 +1147,9 @@ var removeFromSelection = this.removeFromSelection = function(elemsToRemove) {
|
|||
// find every element and remove it from our array copy
|
||||
var i,
|
||||
j = 0,
|
||||
newSelectedItems = new Array(selectedElements.length),
|
||||
newSelectedItems = [],
|
||||
len = selectedElements.length;
|
||||
newSelectedItems.length = len;
|
||||
for (i = 0; i < len; ++i) {
|
||||
var elem = selectedElements[i];
|
||||
if (elem) {
|
||||
|
@ -2852,7 +2854,8 @@ textActions = canvas.textActions = (function() {
|
|||
|
||||
matrix = xform ? svgedit.math.getMatrix(curtext) : null;
|
||||
|
||||
chardata = new Array(len);
|
||||
chardata = [];
|
||||
chardata.length = len;
|
||||
textinput.focus();
|
||||
|
||||
$(curtext).unbind('dblclick', selectWord).dblclick(selectWord);
|
||||
|
@ -2931,7 +2934,8 @@ pathActions = canvas.pathActions = function() {
|
|||
}
|
||||
this.selected_pts.sort();
|
||||
i = this.selected_pts.length;
|
||||
var grips = new Array(i);
|
||||
var grips = [];
|
||||
grips.length = i;
|
||||
// Loop through points to be selected and highlight each
|
||||
while (i--) {
|
||||
var pt = this.selected_pts[i];
|
||||
|
@ -4364,7 +4368,23 @@ this.rasterExport = function(imgType, quality, exportWindowName) {
|
|||
var mimeType = 'image/' + imgType.toLowerCase();
|
||||
var issues = getIssues();
|
||||
var str = this.svgCanvasToString();
|
||||
call('exported', {svg: str, issues: issues, type: imgType, mimeType: mimeType, quality: quality, exportWindowName: exportWindowName});
|
||||
|
||||
svgedit.utilities.buildCanvgCallback(function () {
|
||||
var type = imgType || 'PNG';
|
||||
if (!$('#export_canvas').length) {
|
||||
$('<canvas>', {id: 'export_canvas'}).hide().appendTo('body');
|
||||
}
|
||||
var c = $('#export_canvas')[0];
|
||||
c.width = svgCanvas.contentW;
|
||||
c.height = svgCanvas.contentH;
|
||||
|
||||
canvg(c, str, {renderCallback: function() {
|
||||
var dataURLType = (type === 'ICO' ? 'BMP' : type).toLowerCase();
|
||||
var datauri = quality ? c.toDataURL('image/' + dataURLType, quality) : c.toDataURL('image/' + dataURLType);
|
||||
|
||||
call('exported', {datauri: datauri, svg: str, issues: issues, type: imgType, mimeType: mimeType, quality: quality, exportWindowName: exportWindowName});
|
||||
}});
|
||||
})();
|
||||
};
|
||||
|
||||
this.exportPDF = function (exportWindowName, outputType) {
|
||||
|
@ -4389,7 +4409,7 @@ this.exportPDF = function (exportWindowName, outputType) {
|
|||
});
|
||||
var issues = getIssues();
|
||||
var str = that.svgCanvasToString();
|
||||
svgElementToPdf(str, doc, {});
|
||||
doc.addSVG(str, 0, 0);
|
||||
|
||||
// doc.output('save'); // Works to open in a new
|
||||
// window; todo: configure this and other export
|
||||
|
|
Loading…
Reference in New Issue