124124 else {
125125 text = text || '';
126126 font = font || DEFAULT_FONT;
127- var res = /^([0-9]*? )px$ /.exec(font);
128- var fontSize = +( res && res[1]) || DEFAULT_FONT_SIZE;
127+ var res = /(\d+ )px/.exec(font);
128+ var fontSize = res && + res[1] || DEFAULT_FONT_SIZE;
129129 var width = 0;
130130 if (font.indexOf('mono') >= 0) {
131131 width = fontSize * text.length;
578578 function isPrimitive(obj) {
579579 return obj[primitiveKey];
580580 }
581+ var MapPolyfill = (function () {
582+ function MapPolyfill() {
583+ this.data = {};
584+ }
585+ MapPolyfill.prototype["delete"] = function (key) {
586+ var existed = this.has(key);
587+ if (existed) {
588+ delete this.data[key];
589+ }
590+ return existed;
591+ };
592+ MapPolyfill.prototype.has = function (key) {
593+ return this.data.hasOwnProperty(key);
594+ };
595+ MapPolyfill.prototype.get = function (key) {
596+ return this.data[key];
597+ };
598+ MapPolyfill.prototype.set = function (key, value) {
599+ this.data[key] = value;
600+ return this;
601+ };
602+ MapPolyfill.prototype.keys = function () {
603+ return keys(this.data);
604+ };
605+ MapPolyfill.prototype.forEach = function (callback) {
606+ var data = this.data;
607+ for (var key in data) {
608+ if (data.hasOwnProperty(key)) {
609+ callback(data[key], key);
610+ }
611+ }
612+ };
613+ return MapPolyfill;
614+ }());
615+ var isNativeMapSupported = typeof Map === 'function';
616+ function maybeNativeMap() {
617+ return (isNativeMapSupported ? new Map() : new MapPolyfill());
618+ }
581619 var HashMap = (function () {
582620 function HashMap(obj) {
583- this.data = {};
584621 var isArr = isArray(obj);
585- this.data = {} ;
622+ this.data = maybeNativeMap() ;
586623 var thisMap = this;
587624 (obj instanceof HashMap)
588625 ? obj.each(visit)
591628 isArr ? thisMap.set(value, key) : thisMap.set(key, value);
592629 }
593630 }
631+ HashMap.prototype.hasKey = function (key) {
632+ return this.data.has(key);
633+ };
594634 HashMap.prototype.get = function (key) {
595- return this.data.hasOwnProperty (key) ? this.data[key] : null ;
635+ return this.data.get (key);
596636 };
597637 HashMap.prototype.set = function (key, value) {
598- return (this.data[key] = value);
638+ this.data.set(key, value);
639+ return value;
599640 };
600641 HashMap.prototype.each = function (cb, context) {
601- for (var key in this.data) {
602- if (this.data.hasOwnProperty(key)) {
603- cb.call(context, this.data[key], key);
604- }
605- }
642+ this.data.forEach(function (value, key) {
643+ cb.call(context, value, key);
644+ });
606645 };
607646 HashMap.prototype.keys = function () {
608- return keys(this.data);
647+ var keys = this.data.keys();
648+ return isNativeMapSupported
649+ ? Array.from(keys)
650+ : keys;
609651 };
610652 HashMap.prototype.removeKey = function (key) {
611- delete this.data[key] ;
653+ this.data["delete"](key) ;
612654 };
613655 return HashMap;
614656 }());
41494191 var encodeBase64 = (function () {
41504192 if (env.hasGlobalWindow && isFunction(window.btoa)) {
41514193 return function (str) {
4152- return window.btoa(unescape(str));
4194+ return window.btoa(unescape(encodeURIComponent( str) ));
41534195 };
41544196 }
41554197 if (typeof Buffer !== 'undefined') {
71967238 function registerPainter(name, Ctor) {
71977239 painterCtors[name] = Ctor;
71987240 }
7199- var version = '5.4.0 ';
7241+ var version = '5.4.1 ';
72007242
72017243 var STYLE_MAGIC_KEY = '__zr_style_' + Math.round((Math.random() * 10));
72027244 var DEFAULT_COMMON_STYLE = {
@@ -14959,14 +15001,19 @@
1495915001 if (clearColor && clearColor !== 'transparent') {
1496015002 var clearColorGradientOrPattern = void 0;
1496115003 if (isGradientObject(clearColor)) {
14962- clearColorGradientOrPattern = clearColor.__canvasGradient
15004+ var shouldCache = clearColor.global || (clearColor.__width === width
15005+ && clearColor.__height === height);
15006+ clearColorGradientOrPattern = shouldCache
15007+ && clearColor.__canvasGradient
1496315008 || getCanvasGradient(ctx, clearColor, {
1496415009 x: 0,
1496515010 y: 0,
1496615011 width: width,
1496715012 height: height
1496815013 });
1496915014 clearColor.__canvasGradient = clearColorGradientOrPattern;
15015+ clearColor.__width = width;
15016+ clearColor.__height = height;
1497015017 }
1497115018 else if (isImagePatternObject(clearColor)) {
1497215019 clearColor.scaleX = clearColor.scaleX || dpr;
1624716294 }
1624816295 };
1624916296 }
16250- var buitinShapesDef = {
16297+ var builtinShapesDef = {
1625116298 circle: [createAttrsConvert(['cx', 'cy', 'r'])],
1625216299 polyline: [convertPolyShape, validatePolyShape],
1625316300 polygon: [convertPolyShape, validatePolyShape]
1626416311 function brushSVGPath(el, scope) {
1626516312 var style = el.style;
1626616313 var shape = el.shape;
16267- var builtinShpDef = buitinShapesDef [el.type];
16314+ var builtinShpDef = builtinShapesDef [el.type];
1626816315 var attrs = {};
1626916316 var needsAnimate = scope.animation;
1627016317 var svgElType = 'path';
@@ -16280,11 +16327,12 @@
1628016327 builtinShpDef[0](shape, attrs, mul);
1628116328 }
1628216329 else {
16330+ var needBuildPath = !el.path || el.shapeChanged();
1628316331 if (!el.path) {
1628416332 el.createPathProxy();
1628516333 }
1628616334 var path = el.path;
16287- if (el.shapeChanged() ) {
16335+ if (needBuildPath ) {
1628816336 path.beginPath();
1628916337 el.buildPath(path, el.shape);
1629016338 el.pathUpdated();
1650416552 }
1650516553 function setPattern(el, attrs, target, scope) {
1650616554 var val = el.style[target];
16507- var patternAttrs = {
16508- 'patternUnits': 'userSpaceOnUse'
16509- };
16555+ var boundingRect = el.getBoundingRect();
16556+ var patternAttrs = {};
16557+ var repeat = val.repeat;
16558+ var noRepeat = repeat === 'no-repeat';
16559+ var repeatX = repeat === 'repeat-x';
16560+ var repeatY = repeat === 'repeat-y';
1651016561 var child;
1651116562 if (isImagePattern(val)) {
1651216563 var imageWidth_1 = val.imageWidth;
@@ -16531,16 +16582,28 @@
1653116582 var setSizeToVNode_1 = function (vNode, img) {
1653216583 if (vNode) {
1653316584 var svgEl = vNode.elm;
16534- var width = (vNode.attrs.width = imageWidth_1 || img.width);
16535- var height = (vNode.attrs.height = imageHeight_1 || img.height);
16585+ var width = imageWidth_1 || img.width;
16586+ var height = imageHeight_1 || img.height;
16587+ if (vNode.tag === 'pattern') {
16588+ if (repeatX) {
16589+ height = 1;
16590+ width /= boundingRect.width;
16591+ }
16592+ else if (repeatY) {
16593+ width = 1;
16594+ height /= boundingRect.height;
16595+ }
16596+ }
16597+ vNode.attrs.width = width;
16598+ vNode.attrs.height = height;
1653616599 if (svgEl) {
1653716600 svgEl.setAttribute('width', width);
1653816601 svgEl.setAttribute('height', height);
1653916602 }
1654016603 }
1654116604 };
1654216605 var createdImage = createOrUpdateImage(imageSrc, null, el, function (img) {
16543- setSizeToVNode_1(patternVNode, img);
16606+ noRepeat || setSizeToVNode_1(patternVNode, img);
1654416607 setSizeToVNode_1(child, img);
1654516608 });
1654616609 if (createdImage && createdImage.width && createdImage.height) {
1656416627 if (!child) {
1656516628 return;
1656616629 }
16567- patternAttrs.patternTransform = getSRTTransformString(val);
16630+ var patternWidth;
16631+ var patternHeight;
16632+ if (noRepeat) {
16633+ patternWidth = patternHeight = 1;
16634+ }
16635+ else if (repeatX) {
16636+ patternHeight = 1;
16637+ patternWidth = patternAttrs.width / boundingRect.width;
16638+ }
16639+ else if (repeatY) {
16640+ patternWidth = 1;
16641+ patternHeight = patternAttrs.height / boundingRect.height;
16642+ }
16643+ else {
16644+ patternAttrs.patternUnits = 'userSpaceOnUse';
16645+ }
16646+ if (patternWidth != null && !isNaN(patternWidth)) {
16647+ patternAttrs.width = patternWidth;
16648+ }
16649+ if (patternHeight != null && !isNaN(patternHeight)) {
16650+ patternAttrs.height = patternHeight;
16651+ }
16652+ var patternTransform = getSRTTransformString(val);
16653+ patternTransform && (patternAttrs.patternTransform = patternTransform);
1656816654 var patternVNode = createVNode('pattern', '', patternAttrs, [child]);
1656916655 var patternKey = vNodeToString(patternVNode);
1657016656 var patternCache = scope.patternCache;
@@ -16916,30 +17002,15 @@
1691617002 SVGPainter.prototype.renderToVNode = function (opts) {
1691717003 opts = opts || {};
1691817004 var list = this.storage.getDisplayList(true);
16919- var bgColor = this._backgroundColor;
1692017005 var width = this._width;
1692117006 var height = this._height;
1692217007 var scope = createBrushScope(this._id);
1692317008 scope.animation = opts.animation;
1692417009 scope.willUpdate = opts.willUpdate;
1692517010 scope.compress = opts.compress;
1692617011 var children = [];
16927- if (bgColor && bgColor !== 'none') {
16928- var _a = normalizeColor(bgColor), color = _a.color, opacity = _a.opacity;
16929- this._bgVNode = createVNode('rect', 'bg', {
16930- width: width,
16931- height: height,
16932- x: '0',
16933- y: '0',
16934- id: '0',
16935- fill: color,
16936- 'fill-opacity': opacity
16937- });
16938- children.push(this._bgVNode);
16939- }
16940- else {
16941- this._bgVNode = null;
16942- }
17012+ var bgVNode = this._bgVNode = createBackgroundVNode(width, height, this._backgroundColor, scope);
17013+ bgVNode && children.push(bgVNode);
1694317014 var mainVNode = !opts.compress
1694417015 ? (this._mainVNode = createVNode('g', 'main', {}, [])) : null;
1694517016 this._paintList(list, scope, mainVNode ? mainVNode.children : children);
1696817039 };
1696917040 SVGPainter.prototype.setBackgroundColor = function (backgroundColor) {
1697017041 this._backgroundColor = backgroundColor;
16971- var bgVNode = this._bgVNode;
16972- if (bgVNode && bgVNode.elm) {
16973- var _a = normalizeColor(backgroundColor), color = _a.color, opacity = _a.opacity;
16974- bgVNode.elm.setAttribute('fill', color);
16975- if (opacity < 1) {
16976- bgVNode.elm.setAttribute('fill-opacity', opacity);
16977- }
16978- }
1697917042 };
1698017043 SVGPainter.prototype.getSvgRoot = function () {
1698117044 return this._mainVNode && this._mainVNode.elm;
@@ -17040,10 +17103,20 @@
1704017103 viewportStyle.width = width + 'px';
1704117104 viewportStyle.height = height + 'px';
1704217105 }
17043- var svgDom = this._svgDom;
17044- if (svgDom) {
17045- svgDom.setAttribute('width', width);
17046- svgDom.setAttribute('height', height);
17106+ if (!isPattern(this._backgroundColor)) {
17107+ var svgDom = this._svgDom;
17108+ if (svgDom) {
17109+ svgDom.setAttribute('width', width);
17110+ svgDom.setAttribute('height', height);
17111+ }
17112+ var bgEl = this._bgVNode && this._bgVNode.elm;
17113+ if (bgEl) {
17114+ bgEl.setAttribute('width', width);
17115+ bgEl.setAttribute('height', height);
17116+ }
17117+ }
17118+ else {
17119+ this.refresh();
1704717120 }
1704817121 }
1704917122 };
@@ -17071,13 +17144,13 @@
1707117144 this._oldVNode = null;
1707217145 };
1707317146 SVGPainter.prototype.toDataURL = function (base64) {
17074- var str = encodeURIComponent( this.renderToString() );
17147+ var str = this.renderToString();
1707517148 var prefix = 'data:image/svg+xml;';
1707617149 if (base64) {
1707717150 str = encodeBase64(str);
1707817151 return str && prefix + 'base64,' + str;
1707917152 }
17080- return prefix + 'charset=UTF-8,' + str;
17153+ return prefix + 'charset=UTF-8,' + encodeURIComponent( str) ;
1708117154 };
1708217155 return SVGPainter;
1708317156 }());
1708717160 logError('In SVG mode painter not support method "' + method + '"');
1708817161 }
1708917162 };
17163+ }
17164+ function createBackgroundVNode(width, height, backgroundColor, scope) {
17165+ var bgVNode;
17166+ if (backgroundColor && backgroundColor !== 'none') {
17167+ bgVNode = createVNode('rect', 'bg', {
17168+ width: width,
17169+ height: height,
17170+ x: '0',
17171+ y: '0',
17172+ id: '0'
17173+ });
17174+ if (isGradient(backgroundColor)) {
17175+ setGradient({ fill: backgroundColor }, bgVNode.attrs, 'fill', scope);
17176+ }
17177+ else if (isPattern(backgroundColor)) {
17178+ setPattern({
17179+ style: {
17180+ fill: backgroundColor
17181+ },
17182+ dirty: noop,
17183+ getBoundingRect: function () { return ({ width: width, height: height }); }
17184+ }, bgVNode.attrs, 'fill', scope);
17185+ }
17186+ else {
17187+ var _a = normalizeColor(backgroundColor), color = _a.color, opacity = _a.opacity;
17188+ bgVNode.attrs.fill = color;
17189+ opacity < 1 && (bgVNode.attrs['fill-opacity'] = opacity);
17190+ }
17191+ }
17192+ return bgVNode;
1709017193 }
1709117194
1709217195 registerPainter('canvas', CanvasPainter);
0 commit comments