| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728 |
- /**
- * DevExtreme (exporter/image_creator.js)
- * Version: 19.1.16
- * Build date: Tue Oct 18 2022
- *
- * Copyright (c) 2012 - 2022 Developer Express Inc. ALL RIGHTS RESERVED
- * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
- */
- "use strict";
- var _renderer = require("../core/renderer");
- var _renderer2 = _interopRequireDefault(_renderer);
- var _color = require("../color");
- var _color2 = _interopRequireDefault(_color);
- var _type = require("../core/utils/type");
- var _svg = require("../core/utils/svg");
- var _svg2 = _interopRequireDefault(_svg);
- var _iterator = require("../core/utils/iterator");
- var _extend = require("../core/utils/extend");
- var _dom_adapter = require("../core/dom_adapter");
- var _dom_adapter2 = _interopRequireDefault(_dom_adapter);
- var _dom = require("../core/utils/dom");
- var _dom2 = _interopRequireDefault(_dom);
- var _window = require("../core/utils/window");
- var _window2 = _interopRequireDefault(_window);
- var _inflector = require("../core/utils/inflector");
- var _deferred = require("../core/utils/deferred");
- function _interopRequireDefault(obj) {
- return obj && obj.__esModule ? obj : {
- "default": obj
- }
- }
- var window = _window2.default.getWindow();
- var _math = Math;
- var PI = _math.PI;
- var _min = _math.min;
- var _abs = _math.abs;
- var _sqrt = _math.sqrt;
- var _pow = _math.pow;
- var _atan2 = _math.atan2;
- var _cos = _math.cos;
- var _sin = _math.sin;
- var _number = Number;
- var IMAGE_QUALITY = 1;
- var TEXT_DECORATION_LINE_WIDTH_COEFF = .05;
- var DEFAULT_FONT_SIZE = "10px";
- var DEFAULT_FONT_FAMILY = "sans-serif";
- var DEFAULT_TEXT_COLOR = "#000";
- function createCanvas(width, height, margin) {
- var canvas = (0, _renderer2.default)("<canvas>")[0];
- canvas.width = width + 2 * margin;
- canvas.height = height + 2 * margin;
- canvas.hidden = true;
- return canvas
- }
- function getStringFromCanvas(canvas, mimeType) {
- var dataURL = canvas.toDataURL(mimeType, IMAGE_QUALITY);
- var imageData = window.atob(dataURL.substring(("data:" + mimeType + ";base64,").length));
- return imageData
- }
- function arcTo(x1, y1, x2, y2, radius, largeArcFlag, clockwise, context) {
- var cBx = (x1 + x2) / 2;
- var cBy = (y1 + y2) / 2;
- var aB = _atan2(y1 - y2, x1 - x2);
- var k = largeArcFlag ? 1 : -1;
- aB += 90 * (PI / 180) * (clockwise ? 1 : -1);
- var opSide = _sqrt(_pow(x2 - x1, 2) + _pow(y2 - y1, 2)) / 2;
- var adjSide = _sqrt(_abs(_pow(radius, 2) - _pow(opSide, 2)));
- var centerX = cBx + k * (adjSide * _cos(aB));
- var centerY = cBy + k * (adjSide * _sin(aB));
- var startAngle = _atan2(y1 - centerY, x1 - centerX);
- var endAngle = _atan2(y2 - centerY, x2 - centerX);
- context.arc(centerX, centerY, radius, startAngle, endAngle, !clockwise)
- }
- function getElementOptions(element, rootAppended) {
- var attr = parseAttributes(element.attributes || {});
- var options = (0, _extend.extend)({}, attr, {
- text: element.textContent.replace(/\s+/g, " "),
- textAlign: "middle" === attr["text-anchor"] ? "center" : attr["text-anchor"]
- });
- var transform = attr.transform;
- var coords;
- if (transform) {
- coords = transform.match(/translate\(-*\d+([.]\d+)*(,*\s*-*\d+([.]\d+)*)*/);
- if (coords) {
- coords = coords[0].match(/-*\d+([.]\d+)*/g);
- options.translateX = _number(coords[0]);
- options.translateY = coords[1] ? _number(coords[1]) : 0
- }
- coords = transform.match(/rotate\(-*\d+([.]\d+)*(,*\s*-*\d+([.]\d+)*,*\s*-*\d+([.]\d+)*)*/);
- if (coords) {
- coords = coords[0].match(/-*\d+([.]\d+)*/g);
- options.rotationAngle = _number(coords[0]);
- options.rotationX = coords[1] && _number(coords[1]);
- options.rotationY = coords[2] && _number(coords[2])
- }
- coords = transform.match(/scale\(-*\d+([.]\d+)*(,*\s*-*\d+([.]\d+)*)*/);
- if (coords) {
- coords = coords[0].match(/-*\d+([.]\d+)*/g);
- options.scaleX = _number(coords[0]);
- if (coords.length > 1) {
- options.scaleY = _number(coords[1])
- } else {
- options.scaleY = options.scaleX
- }
- }
- }
- parseStyles(element, options, rootAppended);
- return options
- }
- function drawRect(context, options) {
- var x = options.x;
- var y = options.y;
- var width = options.width;
- var height = options.height;
- var cornerRadius = options.rx;
- if (!cornerRadius) {
- context.rect(x, y, width, height)
- } else {
- cornerRadius = _min(cornerRadius, width / 2, height / 2);
- context.save();
- context.translate(x, y);
- context.moveTo(width / 2, 0);
- context.arcTo(width, 0, width, height, cornerRadius);
- context.arcTo(width, height, 0, height, cornerRadius);
- context.arcTo(0, height, 0, 0, cornerRadius);
- context.arcTo(0, 0, cornerRadius, 0, cornerRadius);
- context.lineTo(width / 2, 0);
- context.restore()
- }
- }
- function drawImage(context, options, shared) {
- var d = new _deferred.Deferred;
- var image = new window.Image;
- image.onload = function() {
- context.save();
- context.globalAlpha = options.globalAlpha;
- transformElement(context, options);
- clipElement(context, options, shared);
- context.drawImage(image, options.x, options.y, options.width, options.height);
- context.restore();
- d.resolve()
- };
- image.onerror = function() {
- d.resolve()
- };
- image.setAttribute("crossOrigin", "anonymous");
- image.src = options.href || options["xlink:href"];
- return d
- }
- function drawPath(context, dAttr) {
- var dArray = dAttr.replace(/,/g, " ").split(/([A-Z])/i).filter(function(item) {
- return "" !== item.trim()
- });
- var i = 0;
- var prevParams;
- var prevParamsLen;
- do {
- var params = (dArray[i + 1] || "").trim().split(" ");
- switch (dArray[i]) {
- case "M":
- context.moveTo(_number(params[0]), _number(params[1]));
- i += 2;
- break;
- case "L":
- for (var j = 0; j < params.length / 2; j++) {
- context.lineTo(_number(params[2 * j]), _number(params[2 * j + 1]))
- }
- i += 2;
- break;
- case "C":
- context.bezierCurveTo(_number(params[0]), _number(params[1]), _number(params[2]), _number(params[3]), _number(params[4]), _number(params[5]));
- i += 2;
- break;
- case "a":
- prevParams = dArray[i - 1].trim().split(" ");
- prevParamsLen = prevParams.length - 1;
- arcTo(_number(prevParams[prevParamsLen - 1]), _number(prevParams[prevParamsLen]), _number(prevParams[prevParamsLen - 1]) + _number(params[5]), _number(prevParams[prevParamsLen]) + _number(params[6]), _number(params[0]), _number(params[3]), _number(params[4]), context);
- i += 2;
- break;
- case "A":
- prevParams = dArray[i - 1].trim().split(" ");
- prevParamsLen = prevParams.length - 1;
- arcTo(_number(prevParams[prevParamsLen - 1]), _number(prevParams[prevParamsLen]), _number(params[5]), _number(params[6]), _number(params[0]), _number(params[3]), _number(params[4]), context);
- i += 2;
- break;
- case "Z":
- context.closePath();
- i += 1
- }
- } while (i < dArray.length)
- }
- function parseStyles(element, options, rootAppended) {
- var style = element.style || {};
- var field;
- for (field in style) {
- if ("" !== style[field]) {
- options[(0, _inflector.camelize)(field)] = style[field]
- }
- }
- if (rootAppended && _dom_adapter2.default.isElementNode(element)) {
- style = window.getComputedStyle(element);
- ["fill", "stroke", "stroke-width", "font-family", "font-size", "font-style", "font-weight"].forEach(function(prop) {
- if (prop in style && "" !== style[prop]) {
- options[(0, _inflector.camelize)(prop)] = style[prop]
- }
- });
- ["opacity", "fill-opacity", "stroke-opacity"].forEach(function(prop) {
- if (prop in style && "" !== style[prop] && "1" !== style[prop]) {
- options[prop] = _number(style[prop])
- }
- })
- }
- options.textDecoration = options.textDecoration || options.textDecorationLine;
- options.globalAlpha = (0, _type.isDefined)(options.opacity) ? options.opacity : options.globalAlpha
- }
- function parseUrl(urlString) {
- var matches = urlString && urlString.match(/url\(.*#(.*?)["']?\)/i);
- return matches && matches[1]
- }
- function setFontStyle(context, options) {
- var fontParams = [];
- options.fontSize = options.fontSize || DEFAULT_FONT_SIZE;
- options.fontFamily || DEFAULT_FONT_FAMILY;
- options.fill = options.fill || DEFAULT_TEXT_COLOR;
- options.fontStyle && fontParams.push(options.fontStyle);
- options.fontWeight && fontParams.push(options.fontWeight);
- fontParams.push(options.fontSize);
- fontParams.push(options.fontFamily);
- context.font = fontParams.join(" ");
- context.textAlign = options.textAlign;
- context.fillStyle = options.fill;
- context.globalAlpha = options.globalAlpha
- }
- function drawText(context, options, shared) {
- setFontStyle(context, options);
- applyFilter(context, options, shared);
- options.text && context.fillText(options.text, options.x || 0, options.y || 0);
- strokeElement(context, options, true);
- drawTextDecoration(context, options, shared)
- }
- function drawTextDecoration(context, options, shared) {
- if (!options.textDecoration || "none" === options.textDecoration) {
- return
- }
- var x = options.x;
- var textWidth = context.measureText(options.text).width;
- var textHeight = parseInt(options.fontSize, 10);
- var lineHeight = textHeight * TEXT_DECORATION_LINE_WIDTH_COEFF < 1 ? 1 : textHeight * TEXT_DECORATION_LINE_WIDTH_COEFF;
- var y = options.y;
- switch (options.textDecoration) {
- case "line-through":
- y -= textHeight / 3 + lineHeight / 2;
- break;
- case "overline":
- y -= textHeight - lineHeight;
- break;
- case "underline":
- y += lineHeight
- }
- context.rect(x, y, textWidth, lineHeight);
- fillElement(context, options, shared);
- strokeElement(context, options)
- }
- function aggregateOpacity(options) {
- options.strokeOpacity = void 0 !== options["stroke-opacity"] ? options["stroke-opacity"] : 1;
- options.fillOpacity = void 0 !== options["fill-opacity"] ? options["fill-opacity"] : 1;
- if (void 0 !== options.opacity) {
- options.strokeOpacity *= options.opacity;
- options.fillOpacity *= options.opacity
- }
- }
- function hasTspan(element) {
- var nodes = element.childNodes;
- for (var i = 0; i < nodes.length; i++) {
- if ("tspan" === nodes[i].tagName) {
- return true
- }
- }
- return false
- }
- function drawTextElement(childNodes, context, options, shared) {
- var lines = [];
- var line;
- var offset = 0;
- for (var i = 0; i < childNodes.length; i++) {
- var element = childNodes[i];
- if (void 0 === element.tagName) {
- drawElement(element, context, options, shared)
- } else {
- if ("tspan" === element.tagName || "text" === element.tagName) {
- var elementOptions = getElementOptions(element, shared.rootAppended);
- var mergedOptions = (0, _extend.extend)({}, options, elementOptions);
- if ("tspan" === element.tagName && hasTspan(element)) {
- drawTextElement(element.childNodes, context, mergedOptions, shared);
- continue
- }
- mergedOptions.textAlign = "start";
- if (!line || void 0 !== elementOptions.x) {
- line = {
- elements: [],
- options: [],
- widths: [],
- offsets: []
- };
- lines.push(line)
- }
- if (void 0 !== elementOptions.y) {
- offset = 0
- }
- if (void 0 !== elementOptions.dy) {
- offset += parseFloat(elementOptions.dy)
- }
- line.elements.push(element);
- line.options.push(mergedOptions);
- line.offsets.push(offset);
- setFontStyle(context, mergedOptions);
- line.widths.push(context.measureText(mergedOptions.text).width)
- }
- }
- }
- lines.forEach(function(line) {
- var commonWidth = line.widths.reduce(function(commonWidth, width) {
- return commonWidth + width
- }, 0);
- var xDiff = 0;
- var currentOffset = 0;
- if ("center" === options.textAlign) {
- xDiff = commonWidth / 2
- }
- if ("end" === options.textAlign) {
- xDiff = commonWidth
- }
- line.options.forEach(function(o, index) {
- var width = line.widths[index];
- o.x = o.x - xDiff + currentOffset;
- o.y += line.offsets[index];
- currentOffset += width
- });
- line.elements.forEach(function(element, index) {
- drawTextElement(element.childNodes, context, line.options[index], shared)
- })
- })
- }
- function drawElement(element, context, parentOptions, shared) {
- var tagName = element.tagName;
- var isText = "text" === tagName || "tspan" === tagName || void 0 === tagName;
- var isImage = "image" === tagName;
- var options = (0, _extend.extend)({}, parentOptions, getElementOptions(element, shared.rootAppended));
- if ("hidden" === options.visibility || options["hidden-for-export"]) {
- return
- }
- context.save();
- !isImage && transformElement(context, options);
- clipElement(context, options, shared);
- aggregateOpacity(options);
- var promise;
- context.beginPath();
- switch (element.tagName) {
- case void 0:
- drawText(context, options, shared);
- break;
- case "text":
- case "tspan":
- drawTextElement(element.childNodes, context, options, shared);
- break;
- case "image":
- promise = drawImage(context, options, shared);
- break;
- case "path":
- drawPath(context, options.d);
- break;
- case "rect":
- drawRect(context, options);
- context.closePath();
- break;
- case "circle":
- context.arc(options.cx, options.cy, options.r, 0, 2 * PI, 1)
- }
- if (!isText) {
- applyFilter(context, options, shared);
- fillElement(context, options, shared);
- strokeElement(context, options)
- }
- applyGradient(context, options, shared, element);
- context.restore();
- return promise
- }
- function applyGradient(context, options, _ref, element) {
- var gradients = _ref.gradients;
- if (0 === gradients.length) {
- return
- }
- var id = parseUrl(options.fill);
- if (id && gradients[id]) {
- var box = element.getBBox();
- var gradient = context.createLinearGradient(box.x, 0, box.x + box.width, 0);
- gradients[id].forEach(function(opt) {
- var offset = parseInt(opt.offset.replace(/%/, ""));
- gradient.addColorStop(offset / 100, opt.stopColor)
- });
- context.globalAlpha = options.opacity;
- context.fillStyle = gradient;
- context.fill()
- }
- }
- function applyFilter(context, options, shared) {
- var filterOptions;
- var id = parseUrl(options.filter);
- if (id) {
- filterOptions = shared.filters[id];
- if (!filterOptions) {
- filterOptions = {
- offsetX: 0,
- offsetY: 0,
- blur: 0,
- color: "#000"
- }
- }
- context.shadowOffsetX = filterOptions.offsetX;
- context.shadowOffsetY = filterOptions.offsetY;
- context.shadowColor = filterOptions.color;
- context.shadowBlur = filterOptions.blur
- }
- }
- function transformElement(context, options) {
- context.translate(options.translateX || 0, options.translateY || 0);
- options.translateX = void 0;
- options.translateY = void 0;
- if (options.rotationAngle) {
- context.translate(options.rotationX || 0, options.rotationY || 0);
- context.rotate(options.rotationAngle * PI / 180);
- context.translate(-(options.rotationX || 0), -(options.rotationY || 0));
- options.rotationAngle = void 0;
- options.rotationX = void 0;
- options.rotationY = void 0
- }
- if (isFinite(options.scaleX)) {
- context.scale(options.scaleX, options.scaleY);
- options.scaleX = void 0;
- options.scaleY = void 0
- }
- }
- function clipElement(context, options, shared) {
- if (options["clip-path"]) {
- drawElement(shared.clipPaths[parseUrl(options["clip-path"])], context, {}, shared);
- context.clip();
- options["clip-path"] = void 0
- }
- }
- function hex2rgba(hexColor, alpha) {
- var color = new _color2.default(hexColor);
- return "rgba(" + color.r + "," + color.g + "," + color.b + "," + alpha + ")"
- }
- function createGradient(element) {
- var options = [];
- (0, _iterator.each)(element.childNodes, function(_, _ref2) {
- var attributes = _ref2.attributes;
- options.push({
- offset: attributes.offset.value,
- stopColor: attributes["stop-color"].value
- })
- });
- return options
- }
- function createFilter(element) {
- var color;
- var filterOptions = {};
- (0, _iterator.each)(element.childNodes, function(_, node) {
- var attr = node.attributes;
- if (!attr.result) {
- return
- }
- switch (attr.result.value) {
- case "gaussianBlurResult":
- filterOptions.blur = _number(attr.stdDeviation.value);
- break;
- case "offsetResult":
- filterOptions.offsetX = _number(attr.dx.value);
- filterOptions.offsetY = _number(attr.dy.value);
- break;
- case "floodResult":
- color = attr["flood-color"] ? attr["flood-color"].value : "#000";
- var opacity = attr["flood-opacity"] ? attr["flood-opacity"].value : 1;
- filterOptions.color = hex2rgba(color, opacity)
- }
- });
- return filterOptions
- }
- function asyncEach(array, callback) {
- var d = arguments.length > 2 && void 0 !== arguments[2] ? arguments[2] : new _deferred.Deferred;
- if (0 === array.length) {
- return d.resolve()
- }
- var result = callback(array[0]);
- function next() {
- asyncEach(Array.prototype.slice.call(array, 1), callback, d)
- }
- if ((0, _type.isPromise)(result)) {
- result.then(next)
- } else {
- next()
- }
- return d
- }
- function drawCanvasElements(elements, context, parentOptions, shared) {
- return asyncEach(elements, function(element) {
- switch (element.tagName && element.tagName.toLowerCase()) {
- case "g":
- case "svg":
- var options = (0, _extend.extend)({}, parentOptions, getElementOptions(element, shared.rootAppended));
- context.save();
- transformElement(context, options);
- clipElement(context, options, shared);
- var onDone = function() {
- context.restore()
- };
- var d = drawCanvasElements(element.childNodes, context, options, shared);
- if ((0, _type.isPromise)(d)) {
- d.then(onDone)
- } else {
- onDone()
- }
- return d;
- case "defs":
- return drawCanvasElements(element.childNodes, context, {}, shared);
- case "clippath":
- shared.clipPaths[element.attributes.id.textContent] = element.childNodes[0];
- break;
- case "pattern":
- shared.patterns[element.attributes.id.textContent] = element;
- break;
- case "filter":
- shared.filters[element.id] = createFilter(element);
- break;
- case "lineargradient":
- shared.gradients[element.attributes.id.textContent] = createGradient(element);
- break;
- default:
- return drawElement(element, context, parentOptions, shared)
- }
- })
- }
- function setLineDash(context, options) {
- var matches = options["stroke-dasharray"] && options["stroke-dasharray"].match(/(\d+)/g);
- if (matches && matches.length) {
- matches = (0, _iterator.map)(matches, function(item) {
- return _number(item)
- });
- context.setLineDash(matches)
- }
- }
- function strokeElement(context, options, isText) {
- var stroke = options.stroke;
- if (stroke && "none" !== stroke && 0 !== options["stroke-width"]) {
- setLineDash(context, options);
- context.lineJoin = options["stroke-linejoin"];
- context.lineWidth = options["stroke-width"];
- context.globalAlpha = options.strokeOpacity;
- context.strokeStyle = stroke;
- isText ? context.strokeText(options.text, options.x, options.y) : context.stroke();
- context.globalAlpha = 1
- }
- }
- function getPattern(context, pattern, shared) {
- var options = getElementOptions(pattern, shared.rootAppended);
- var patternCanvas = createCanvas(options.width, options.height, 0);
- var patternContext = patternCanvas.getContext("2d");
- drawCanvasElements(pattern.childNodes, patternContext, options, shared);
- return context.createPattern(patternCanvas, "repeat")
- }
- function fillElement(context, options, shared) {
- var fill = options.fill;
- if (fill && "none" !== fill) {
- if (fill.search(/url/) === -1) {
- context.fillStyle = fill
- } else {
- var pattern = shared.patterns[parseUrl(fill)];
- if (!pattern) {
- return
- }
- context.fillStyle = getPattern(context, pattern, shared)
- }
- context.globalAlpha = options.fillOpacity;
- context.fill();
- context.globalAlpha = 1
- }
- }
- var parseAttributes = function(attributes) {
- var newAttributes = {};
- var attr;
- (0, _iterator.each)(attributes, function(index, item) {
- attr = item.textContent;
- if (isFinite(attr)) {
- attr = _number(attr)
- }
- newAttributes[item.name.toLowerCase()] = attr
- });
- return newAttributes
- };
- function drawBackground(context, width, height, backgroundColor, margin) {
- context.fillStyle = backgroundColor || "#ffffff";
- context.fillRect(-margin, -margin, width + 2 * margin, height + 2 * margin)
- }
- function createInvisibleDiv() {
- var invisibleDiv = _dom_adapter2.default.createElement("div");
- invisibleDiv.style.left = "-9999px";
- invisibleDiv.style.position = "absolute";
- return invisibleDiv
- }
- function getCanvasFromSvg(markup, width, height, backgroundColor, margin) {
- var canvas = createCanvas(width, height, margin);
- var context = canvas.getContext("2d");
- var svgElem = _svg2.default.getSvgElement(markup);
- var invisibleDiv;
- var markupIsDomElement = _dom_adapter2.default.isElementNode(markup);
- context.translate(margin, margin);
- _dom_adapter2.default.getBody().appendChild(canvas);
- if (!markupIsDomElement) {
- invisibleDiv = createInvisibleDiv();
- invisibleDiv.appendChild(svgElem);
- _dom_adapter2.default.getBody().appendChild(invisibleDiv)
- }
- if (svgElem.attributes.direction) {
- canvas.dir = svgElem.attributes.direction.textContent
- }
- drawBackground(context, width, height, backgroundColor, margin);
- return drawCanvasElements(svgElem.childNodes, context, {}, {
- clipPaths: {},
- patterns: {},
- filters: {},
- gradients: {},
- rootAppended: markupIsDomElement && _dom2.default.contains(_dom_adapter2.default.getBody(), markup)
- }).then(function() {
- invisibleDiv && _dom_adapter2.default.getBody().removeChild(invisibleDiv);
- _dom_adapter2.default.getBody().removeChild(canvas);
- return canvas
- })
- }
- exports.imageCreator = {
- getImageData: function(markup, options) {
- var mimeType = "image/" + options.format;
- var width = options.width;
- var height = options.height;
- var backgroundColor = options.backgroundColor;
- if ((0, _type.isFunction)(options.__parseAttributesFn)) {
- parseAttributes = options.__parseAttributesFn
- }
- var deferred = new _deferred.Deferred;
- getCanvasFromSvg(markup, width, height, backgroundColor, options.margin).then(function(canvas) {
- deferred.resolve(getStringFromCanvas(canvas, mimeType))
- });
- return deferred
- },
- getData: function(markup, options) {
- var that = this;
- var deferred = new _deferred.Deferred;
- exports.imageCreator.getImageData(markup, options).then(function(binaryData) {
- var mimeType = "image/" + options.format;
- var data = (0, _type.isFunction)(window.Blob) && !options.forceProxy ? that._getBlob(binaryData, mimeType) : that._getBase64(binaryData);
- deferred.resolve(data)
- });
- return deferred
- },
- _getBlob: function(binaryData, mimeType) {
- var dataArray = new Uint8Array(binaryData.length);
- for (var i = 0; i < binaryData.length; i++) {
- dataArray[i] = binaryData.charCodeAt(i)
- }
- return new window.Blob([dataArray.buffer], {
- type: mimeType
- })
- },
- _getBase64: function(binaryData) {
- return window.btoa(binaryData)
- }
- };
- exports.getData = function(data, options, callback) {
- return exports.imageCreator.getData(data, options).then(callback)
- };
- exports.testFormats = function(formats) {
- var canvas = createCanvas(100, 100, 0);
- return formats.reduce(function(r, f) {
- var mimeType = ("image/" + f).toLowerCase();
- if (canvas.toDataURL(mimeType).indexOf(mimeType) !== -1) {
- r.supported.push(f)
- } else {
- r.unsupported.push(f)
- }
- return r
- }, {
- supported: [],
- unsupported: []
- })
- };
|