160 lines
5.4 KiB
TypeScript
Raw Normal View History

/*
* Copyright [2021] [wisemapping]
*
* Licensed under WiseMapping Public License, Version 1.0 (the "License").
* It is basically the Apache License, Version 2.0 (the "License") plus the
* "powered by wisemapping" text requirement on every single page;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the license at
*
* http://www.wisemapping.org/license
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2022-02-14 15:56:02 -08:00
import SizeType from '../SizeType';
import Exporter from './Exporter';
2021-12-29 17:10:28 -08:00
2022-02-01 19:13:44 -08:00
class SVGExporter extends Exporter {
private svgElement: Element;
2022-01-02 10:37:33 -08:00
2022-02-14 15:56:02 -08:00
private static prolog = '<?xml version="1.0" encoding="utf-8" standalone="yes"?>\n';
2021-12-29 17:10:28 -08:00
2022-02-14 15:56:02 -08:00
private static regexpTranslate = /translate\((-?[0-9]+.[0-9]+),(-?[0-9]+.[0-9]+)\)/;
2022-02-15 12:44:29 -08:00
private static padding = 30;
2022-02-14 15:56:02 -08:00
private adjustToFit: boolean;
2022-02-15 18:29:02 -08:00
private static MAX_SUPPORTED_SIZE = 2500;
2022-02-14 15:56:02 -08:00
constructor(svgElement: Element, adjustToFit = true) {
2022-02-01 19:13:44 -08:00
super('svg', 'image/svg+xml');
this.svgElement = svgElement;
2022-02-14 15:56:02 -08:00
this.adjustToFit = adjustToFit;
}
2021-12-29 17:10:28 -08:00
export(): Promise<string> {
// Replace all images for in-line images ...
2022-07-13 01:45:36 +00:00
let svgTxt: string = new XMLSerializer().serializeToString(this.svgElement);
2022-02-14 15:56:02 -08:00
svgTxt = SVGExporter.prolog + svgTxt;
2021-12-31 01:52:24 -08:00
// Are namespace declared ?. Otherwise, force the declaration ...
if (svgTxt.indexOf('xmlns:xlink=') === -1) {
svgTxt = svgTxt.replace('<svg ', '<svg xmlns:xlink="http://www.w3.org/1999/xlink" ');
2022-01-02 10:37:33 -08:00
}
// Add white background. This is mainly for PNG export ...
2022-02-14 15:56:02 -08:00
let svgDoc = SVGExporter.parseXMLString(svgTxt, 'application/xml');
const svgElement = svgDoc.getElementsByTagName('svg')[0];
svgElement.setAttribute('style', 'background-color:white');
2022-02-14 15:56:02 -08:00
svgElement.setAttribute('focusable', 'false');
// Does need to be adjust ?.
if (this.adjustToFit) {
2022-02-15 12:44:29 -08:00
svgDoc = this._normalizeToFit(svgDoc);
2022-02-14 15:56:02 -08:00
}
2022-01-02 10:37:33 -08:00
2022-07-13 01:45:36 +00:00
const result = new XMLSerializer().serializeToString(svgDoc);
2022-02-14 15:56:02 -08:00
return Promise.resolve(result);
}
2021-12-29 17:10:28 -08:00
2022-07-13 01:45:36 +00:00
private _calcualteDimensions(): { minX: number; maxX: number; minY: number; maxY: number } {
2022-02-14 15:56:02 -08:00
// Collect all group elements ...
const rectElems = Array.from(this.svgElement.querySelectorAll('g>rect'));
2022-07-13 01:45:36 +00:00
const translates: SizeType[] = rectElems.map((rect: Element) => {
const g = rect.parentElement;
const transformStr = g.getAttribute('transform');
// Looking to parse translate(220.00000,279.00000) scale(1.00000,1.00000)
const match = transformStr.match(SVGExporter.regexpTranslate);
let result: SizeType = { width: 0, height: 0 };
if (match !== null) {
result = { width: Number.parseFloat(match[1]), height: Number.parseFloat(match[2]) };
// Add rect size ...
if (result.width > 0) {
const rectWidth = Number.parseFloat(rect.getAttribute('width'));
result.width += rectWidth;
2022-02-14 15:56:02 -08:00
}
2022-07-13 01:45:36 +00:00
if (result.height > 0) {
const rectHeight = Number.parseFloat(rect.getAttribute('height'));
result.height += rectHeight;
}
}
return result;
});
2022-02-14 15:56:02 -08:00
// Find max and mins ...
const widths = translates.map((t) => t.width).sort((a, b) => a - b);
const heights = translates.map((t) => t.height).sort((a, b) => a - b);
const minX = widths[0] - SVGExporter.padding;
const minY = heights[0] - SVGExporter.padding;
const maxX = widths[widths.length - 1] + SVGExporter.padding;
const maxY = heights[heights.length - 1] + SVGExporter.padding;
2022-02-15 12:44:29 -08:00
return {
2022-07-13 01:45:36 +00:00
minX,
maxX,
minY,
maxY,
2022-02-15 12:44:29 -08:00
};
}
getImgSize(): SizeType {
2022-07-13 01:45:36 +00:00
const { minX, maxX, minY, maxY } = this._calcualteDimensions();
2022-02-15 12:44:29 -08:00
2022-02-15 18:29:02 -08:00
let width: number = maxX + Math.abs(minX);
let height: number = maxY + Math.abs(minY);
// Prevents an image too big. Failures seen during export in couple of browsers
if (Math.max(width, height) > SVGExporter.MAX_SUPPORTED_SIZE) {
const scale = Math.max(width, height) / SVGExporter.MAX_SUPPORTED_SIZE;
width /= scale;
height /= scale;
}
2022-02-15 12:44:29 -08:00
return { width, height };
}
private _normalizeToFit(document: Document): Document {
2022-07-13 01:45:36 +00:00
const { minX, maxX, minY, maxY } = this._calcualteDimensions();
2022-02-15 12:44:29 -08:00
const svgElem = document.firstChild as Element;
const width = maxX + Math.abs(minX);
const height = maxY + Math.abs(minY);
svgElem.setAttribute('viewBox', `${minX} ${minY} ${width} ${height}`);
svgElem.setAttribute('preserveAspectRatio', 'xMinYMin');
2022-02-15 18:29:02 -08:00
// Get image size ...
const imgSize = this.getImgSize();
svgElem.setAttribute('width', imgSize.width.toFixed(0));
svgElem.setAttribute('height', imgSize.height.toFixed(0));
2022-02-14 15:56:02 -08:00
return document;
}
private static parseXMLString = (xmlStr: string, mimeType: DOMParserSupportedType) => {
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(xmlStr, mimeType);
// Is there any parsing error ?.
if (xmlDoc.getElementsByTagName('parsererror').length > 0) {
const xmmStr = new XMLSerializer().serializeToString(xmlDoc);
console.log(xmmStr);
2022-02-14 15:56:02 -08:00
throw new Error(`Unexpected error parsing: ${xmlStr}.Error: ${xmmStr}`);
2021-12-29 17:10:28 -08:00
}
return xmlDoc;
};
2021-12-29 17:10:28 -08:00
}
export default SVGExporter;