- Breaking change: Rename config file to `svgedit-config-iife.js` (or for the module version, `svgedit-config-es.js`);
also expect one directory higher; incorporates #207 (@iuyiuy)
- Breaking change: Separate `extIconsPath` from `extPath` (not copying over icons)
- Breaking change: Don't reference `custom.css` in HTML; can instead be referenced in JavaScript through
the config file (provided in `svgedit-config-sample-iife.js`/`svgedit-config-sample-es.js` as `svgedit-custom.css` for
better namespacing); incorporates #207 (@iuyiuy)
- Breaking change: Remove minified jgraduate/spinbtn files (minified within Rollup routine)
- Fix: Zoom when scrolled; incorporates #169 (@AndrolGenhald), adapting for conventions; also allow avoidance when shift key pressed
- Fix: Update Atom feed reference in HTML
- Fixes related to recent commits: Some path and method name fixes needed, function order, missing methods, variable scope declaration, no need for DOMContentLoaded listeners in modules, switch back to non-default export, avoid trimming nullish, deal with mock tests, fix `math.matrixMultiply`, use jquery-svg where needed for array/SVG attributes; add babel-polyfill and defer script to imagelib; other misc. fixes
- Enhancement: Move config-sample.js out of `editor` directory
- Enhancement: For `callback`-style extensions, also provide config object; add following
to that object: buildCanvgCallback, canvg, decode64, encode64, executeAfterLoads, getTypeMap, isChrome, ieIE, NS, text2xml
- Enhancement: Complete ES6 modules work (extensions, locales, tests), along with Babel;
make Node build routine for converting modular source to non-modular,
use `loadStylesheets` for modular stylehsheet defining (but parallel loading);
- Enhancement: Add `stylesheets` config for modular but parallel stylesheet loading with `@default` option for simple inclusion/exclusion of defaults (if not going with default).
- Refactoring: Clean up `svg-editor.html`: consistent indents; avoid extra lbs, avoid long lines
- Refactoring: Avoid embedded API adding inline JavaScript listener
- Refactoring: Move layers and context code to `draw.js`
- Refactoring: Move `pathActions` from `svgcanvas.js` (though preserve aliases to these methods on `canvas`) and `convertPath` from `svgutils.js` to `path.js`
- Refactoring: Move `getStrokedBBox` from `svgcanvas.js` (while keeping an alias) to `svgutils.js` (as `getStrokedBBoxDefaultVisible` to avoid conflict with existing)
- Docs: Remove "dependencies" comments in code except where summarizing role of jQuery or a non-obvious dependency
- Refactoring/Linting: Enfore `no-extra-semi` and `quote-props` rules
- Refactoring: Further avoidance of quotes on properties (as possible)
- Refactoring: Use `class` in place of functions where intended as classes
- Refactoring: Consistency and granularity in extensions imports
- Testing: Update QUnit to 2.6.1 (node_modules) and Sinon to 5.0.8 (and add sinon-test at 2.1.3) and enforce eslint-plugin-qunit linting rules; update custom extensions
- Testing: Add node-static for automating (and accessing out-of-directory contents)
- Testing: Avoid HTML attributes for styling
- Testing: Add npm `test` script
- Testing: Comment out unused jQuery SVG test
- Testing: Add test1 and svgutils_performance_test to all tests page
- Testing: Due apparently to Path having not been a formal class, the test was calling it without `new`; refactored now with sufficient mock data to take into account it is a class
- npm: Update devDeps
- npm: Add html modules and config build to test script
2018-05-22 10:03:16 +00:00
|
|
|
/* globals jsPDF */
|
2014-04-08 05:13:54 +00:00
|
|
|
/*
|
|
|
|
* svgToPdf.js
|
2018-05-13 03:03:45 +00:00
|
|
|
*
|
2014-06-01 21:29:54 +00:00
|
|
|
* Copyright 2012-2014 Florian Hülsmann <fh@cbix.de>
|
|
|
|
* Copyright 2014 Ben Gribaudo <www.bengribaudo.com>
|
2018-05-13 03:03:45 +00:00
|
|
|
*
|
2014-04-08 05:13:54 +00:00
|
|
|
* 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
|
|
|
|
* (at your option) any later version.
|
2018-05-13 03:03:45 +00:00
|
|
|
*
|
2014-04-08 05:13:54 +00:00
|
|
|
* This script is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Lesser General Public License for more details.
|
2018-05-13 03:03:45 +00:00
|
|
|
*
|
2014-04-08 05:13:54 +00:00
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* along with this file. If not, see <http://www.gnu.org/licenses/>.
|
2018-05-13 03:03:45 +00:00
|
|
|
*
|
2014-04-08 05:13:54 +00:00
|
|
|
*/
|
|
|
|
|
2018-05-18 03:25:45 +00:00
|
|
|
import RGBColor from '../canvg/rgbcolor.js';
|
2014-04-08 05:13:54 +00:00
|
|
|
|
2018-05-18 03:25:45 +00:00
|
|
|
const jsPDFAPI = jsPDF.API;
|
|
|
|
const pdfSvgAttr = {
|
2018-05-18 06:23:36 +00:00
|
|
|
// allowed attributes. all others are removed from the preview.
|
|
|
|
g: ['stroke', 'fill', 'stroke-width'],
|
|
|
|
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']
|
2014-04-08 05:13:54 +00:00
|
|
|
};
|
|
|
|
|
2018-05-18 03:25:45 +00:00
|
|
|
const attributeIsNotEmpty = function (node, attr) {
|
|
|
|
const attVal = attr ? node.getAttribute(attr) : node;
|
2018-05-18 06:23:36 +00:00
|
|
|
return attVal !== '' && attVal !== null;
|
2014-06-01 21:29:54 +00:00
|
|
|
};
|
2014-04-08 05:13:54 +00:00
|
|
|
|
2018-05-18 03:25:45 +00:00
|
|
|
const nodeIs = function (node, possible) {
|
|
|
|
return possible.includes(node.tagName.toLowerCase());
|
2014-06-01 21:29:54 +00:00
|
|
|
};
|
2014-04-08 05:13:54 +00:00
|
|
|
|
2018-05-18 03:25:45 +00:00
|
|
|
const removeAttributes = function (node, attributes) {
|
|
|
|
const toRemove = [];
|
2018-05-18 06:23:36 +00:00
|
|
|
[].forEach.call(node.attributes, function (a) {
|
2018-05-18 03:25:45 +00:00
|
|
|
if (attributeIsNotEmpty(a) && !attributes.includes(a.name.toLowerCase())) {
|
2018-05-18 06:23:36 +00:00
|
|
|
toRemove.push(a.name);
|
|
|
|
}
|
|
|
|
});
|
2014-06-01 21:29:54 +00:00
|
|
|
|
- Breaking change: Extension now formatted as export (and `this` is set to editor, including for `callback`)
- Breaking change: Locale now formatted as export
- Breaking change: Moved out remaining modular i18n (imagelib) to own folder
- Breaking change: Drop `executeAfterLoads` (and getJSPDF/getCanvg)
- Breaking change: `RGBColor` must accept `new`
- Breaking change: canvg - `stackBlurCanvasRGBA` must be set now by function (`setStackBlurCanvasRGBA`) rather than global; `canvg` now a named export
- Breaking change: Avoid passing `canvg`/`buildCanvgCallback` to extensions (have them import)
- Fix: i18nize imaglib more deeply
- Fix: Positioning of Document Properties dialog (Fixes #246)
- Fix (regression): PDF Export (Fixes #249)
- Fix (regression): Add polyfill for `ChildNode`/`ParentNode` (and use further)
- Fix (regression): Apply Babel universally to dependencies
- Fix (regression): Ordering of `uaPrefix` function in `svgEditor.js`
- Fix (regression): Embedded API
- Fix (embedded editor): Fix backspace key in Firefox so it doesn't navigate out of frame
- Fix: Alert if no exportWindow for PDF (e.g., if blocked)
- Refactoring( RGBColor) `RGBColor` as class, without rebuilding constants, optimize string replacement, move methods to prototype, use templates and object literals, use `Object.keys`
- Refactoring (canvg) Use classes more internally, use shorthand objects; array extras, return to lazy-loading
- Refactoring: Use Promises in place of `$.getScript`; always return Promises in case deciding to await resolving
- Refactoring: Avoid importing `RGBColor` into `svgutils.js` (jsPDF imports it itself)
- Refactoring: Arrow functions, destructuring, shorter property references
- Refactoring: Fix `lang` and `dir` for locales (though not in use currently anyways)
- Refactoring: Provide path config for canvg, jspdf
2018-06-02 01:14:38 +00:00
|
|
|
toRemove.forEach((a) => {
|
2018-05-18 06:23:36 +00:00
|
|
|
node.removeAttribute(a.name);
|
|
|
|
});
|
2014-06-01 21:29:54 +00:00
|
|
|
};
|
2014-04-08 05:13:54 +00:00
|
|
|
|
2018-05-18 03:25:45 +00:00
|
|
|
const svgElementToPdf = function (element, pdf, options) {
|
2018-05-18 06:23:36 +00:00
|
|
|
// pdf is a jsPDF object
|
2018-05-30 23:52:13 +00:00
|
|
|
// console.log('options =', options);
|
2018-05-18 03:25:45 +00:00
|
|
|
const remove = (options.removeInvalid === undefined ? false : options.removeInvalid);
|
|
|
|
const k = (options.scale === undefined ? 1.0 : options.scale);
|
|
|
|
let colorMode = null;
|
2018-05-18 06:23:36 +00:00
|
|
|
[].forEach.call(element.children, function (node) {
|
2018-05-30 23:52:13 +00:00
|
|
|
// console.log('passing: ', node);
|
2018-05-18 03:25:45 +00:00
|
|
|
// let hasStrokeColor = false;
|
|
|
|
let hasFillColor = false;
|
|
|
|
let fillRGB;
|
2018-05-18 06:23:36 +00:00
|
|
|
if (nodeIs(node, ['g', 'line', 'rect', 'ellipse', 'circle', 'text'])) {
|
2018-05-18 03:25:45 +00:00
|
|
|
const fillColor = node.getAttribute('fill');
|
2018-05-18 06:23:36 +00:00
|
|
|
if (attributeIsNotEmpty(fillColor)) {
|
|
|
|
fillRGB = new RGBColor(fillColor);
|
|
|
|
if (fillRGB.ok) {
|
|
|
|
hasFillColor = true;
|
|
|
|
colorMode = 'F';
|
|
|
|
} else {
|
|
|
|
colorMode = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (nodeIs(node, ['g', 'line', 'rect', 'ellipse', 'circle'])) {
|
|
|
|
if (hasFillColor) {
|
|
|
|
pdf.setFillColor(fillRGB.r, fillRGB.g, fillRGB.b);
|
|
|
|
}
|
|
|
|
if (attributeIsNotEmpty(node, 'stroke-width')) {
|
|
|
|
pdf.setLineWidth(k * parseInt(node.getAttribute('stroke-width'), 10));
|
|
|
|
}
|
2018-05-18 03:25:45 +00:00
|
|
|
const strokeColor = node.getAttribute('stroke');
|
2018-05-18 06:23:36 +00:00
|
|
|
if (attributeIsNotEmpty(strokeColor)) {
|
2018-05-18 03:25:45 +00:00
|
|
|
const strokeRGB = new RGBColor(strokeColor);
|
2018-05-18 06:23:36 +00:00
|
|
|
if (strokeRGB.ok) {
|
|
|
|
// hasStrokeColor = true;
|
|
|
|
pdf.setDrawColor(strokeRGB.r, strokeRGB.g, strokeRGB.b);
|
|
|
|
if (colorMode === 'F') {
|
|
|
|
colorMode = 'FD';
|
|
|
|
} else {
|
|
|
|
colorMode = null;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
colorMode = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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(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':
|
|
|
|
pdf.rect(
|
|
|
|
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(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(node.getAttribute('cx'), 10),
|
|
|
|
k * parseInt(node.getAttribute('cy'), 10),
|
|
|
|
k * parseInt(node.getAttribute('r'), 10),
|
|
|
|
colorMode
|
|
|
|
);
|
|
|
|
removeAttributes(node, pdfSvgAttr.circle);
|
|
|
|
break;
|
|
|
|
case 'text':
|
|
|
|
if (node.hasAttribute('font-family')) {
|
|
|
|
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 (hasFillColor) {
|
|
|
|
pdf.setTextColor(fillRGB.r, fillRGB.g, fillRGB.b);
|
|
|
|
}
|
2018-05-18 03:25:45 +00:00
|
|
|
let fontType = '';
|
2018-05-18 06:23:36 +00:00
|
|
|
if (node.hasAttribute('font-weight')) {
|
|
|
|
if (node.getAttribute('font-weight') === 'bold') {
|
|
|
|
fontType = 'bold';
|
|
|
|
} else {
|
|
|
|
node.removeAttribute('font-weight');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (node.hasAttribute('font-style')) {
|
|
|
|
if (node.getAttribute('font-style') === 'italic') {
|
|
|
|
fontType += 'italic';
|
|
|
|
} else {
|
|
|
|
node.removeAttribute('font-style');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pdf.setFontType(fontType);
|
2018-05-18 03:25:45 +00:00
|
|
|
const pdfFontSize = node.hasAttribute('font-size')
|
|
|
|
? parseInt(node.getAttribute('font-size'), 10)
|
|
|
|
: 16;
|
|
|
|
|
|
|
|
const box = node.getBBox();
|
2018-05-18 06:23:36 +00:00
|
|
|
// FIXME: use more accurate positioning!!
|
2018-05-18 03:25:45 +00:00
|
|
|
let x, y, xOffset = 0;
|
2018-05-18 06:23:36 +00:00
|
|
|
if (node.hasAttribute('text-anchor')) {
|
|
|
|
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(node.getAttribute('x'), 10) - xOffset;
|
|
|
|
y = parseInt(node.getAttribute('y'), 10);
|
|
|
|
}
|
2018-05-30 23:52:13 +00:00
|
|
|
// console.log('fontSize:', pdfFontSize, 'text:', node.textContent);
|
2018-05-18 06:23:36 +00:00
|
|
|
pdf.setFontSize(pdfFontSize).text(
|
|
|
|
k * x,
|
|
|
|
k * y,
|
|
|
|
node.textContent
|
|
|
|
);
|
|
|
|
removeAttributes(node, pdfSvgAttr.text);
|
|
|
|
break;
|
|
|
|
// TODO: image
|
|
|
|
default:
|
|
|
|
if (remove) {
|
|
|
|
console.log("can't translate to pdf:", node);
|
- Breaking change: Extension now formatted as export (and `this` is set to editor, including for `callback`)
- Breaking change: Locale now formatted as export
- Breaking change: Moved out remaining modular i18n (imagelib) to own folder
- Breaking change: Drop `executeAfterLoads` (and getJSPDF/getCanvg)
- Breaking change: `RGBColor` must accept `new`
- Breaking change: canvg - `stackBlurCanvasRGBA` must be set now by function (`setStackBlurCanvasRGBA`) rather than global; `canvg` now a named export
- Breaking change: Avoid passing `canvg`/`buildCanvgCallback` to extensions (have them import)
- Fix: i18nize imaglib more deeply
- Fix: Positioning of Document Properties dialog (Fixes #246)
- Fix (regression): PDF Export (Fixes #249)
- Fix (regression): Add polyfill for `ChildNode`/`ParentNode` (and use further)
- Fix (regression): Apply Babel universally to dependencies
- Fix (regression): Ordering of `uaPrefix` function in `svgEditor.js`
- Fix (regression): Embedded API
- Fix (embedded editor): Fix backspace key in Firefox so it doesn't navigate out of frame
- Fix: Alert if no exportWindow for PDF (e.g., if blocked)
- Refactoring( RGBColor) `RGBColor` as class, without rebuilding constants, optimize string replacement, move methods to prototype, use templates and object literals, use `Object.keys`
- Refactoring (canvg) Use classes more internally, use shorthand objects; array extras, return to lazy-loading
- Refactoring: Use Promises in place of `$.getScript`; always return Promises in case deciding to await resolving
- Refactoring: Avoid importing `RGBColor` into `svgutils.js` (jsPDF imports it itself)
- Refactoring: Arrow functions, destructuring, shorter property references
- Refactoring: Fix `lang` and `dir` for locales (though not in use currently anyways)
- Refactoring: Provide path config for canvg, jspdf
2018-06-02 01:14:38 +00:00
|
|
|
node.remove();
|
2018-05-18 06:23:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return pdf;
|
2014-06-01 21:29:54 +00:00
|
|
|
};
|
2014-04-08 05:13:54 +00:00
|
|
|
|
2018-05-13 03:03:45 +00:00
|
|
|
jsPDFAPI.addSVG = function (element, x, y, options) {
|
2018-05-18 03:25:45 +00:00
|
|
|
options = (options === undefined ? {} : options);
|
2018-05-18 06:23:36 +00:00
|
|
|
options.x_offset = x;
|
|
|
|
options.y_offset = y;
|
2014-04-08 05:13:54 +00:00
|
|
|
|
2018-05-18 06:23:36 +00:00
|
|
|
if (typeof element === 'string') {
|
|
|
|
element = new DOMParser().parseFromString(element, 'text/xml').documentElement;
|
|
|
|
}
|
|
|
|
svgElementToPdf(element, this, options);
|
|
|
|
return this;
|
2018-05-13 03:03:45 +00:00
|
|
|
};
|