geoSVGLoader.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. var _parseSVG = require("zrender/lib/tool/parseSVG");
  20. var parseSVG = _parseSVG.parseSVG;
  21. var makeViewBoxTransform = _parseSVG.makeViewBoxTransform;
  22. var Group = require("zrender/lib/container/Group");
  23. var Rect = require("zrender/lib/graphic/shape/Rect");
  24. var _util = require("zrender/lib/core/util");
  25. var assert = _util.assert;
  26. var createHashMap = _util.createHashMap;
  27. var BoundingRect = require("zrender/lib/core/BoundingRect");
  28. var _model = require("../../util/model");
  29. var makeInner = _model.makeInner;
  30. /*
  31. * Licensed to the Apache Software Foundation (ASF) under one
  32. * or more contributor license agreements. See the NOTICE file
  33. * distributed with this work for additional information
  34. * regarding copyright ownership. The ASF licenses this file
  35. * to you under the Apache License, Version 2.0 (the
  36. * "License"); you may not use this file except in compliance
  37. * with the License. You may obtain a copy of the License at
  38. *
  39. * http://www.apache.org/licenses/LICENSE-2.0
  40. *
  41. * Unless required by applicable law or agreed to in writing,
  42. * software distributed under the License is distributed on an
  43. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  44. * KIND, either express or implied. See the License for the
  45. * specific language governing permissions and limitations
  46. * under the License.
  47. */
  48. var inner = makeInner();
  49. var _default = {
  50. /**
  51. * @param {string} mapName
  52. * @param {Object} mapRecord {specialAreas, geoJSON}
  53. * @return {Object} {root, boundingRect}
  54. */
  55. load: function (mapName, mapRecord) {
  56. var originRoot = inner(mapRecord).originRoot;
  57. if (originRoot) {
  58. return {
  59. root: originRoot,
  60. boundingRect: inner(mapRecord).boundingRect
  61. };
  62. }
  63. var graphic = buildGraphic(mapRecord);
  64. inner(mapRecord).originRoot = graphic.root;
  65. inner(mapRecord).boundingRect = graphic.boundingRect;
  66. return graphic;
  67. },
  68. makeGraphic: function (mapName, mapRecord, hostKey) {
  69. // For performance consideration (in large SVG), graphic only maked
  70. // when necessary and reuse them according to hostKey.
  71. var field = inner(mapRecord);
  72. var rootMap = field.rootMap || (field.rootMap = createHashMap());
  73. var root = rootMap.get(hostKey);
  74. if (root) {
  75. return root;
  76. }
  77. var originRoot = field.originRoot;
  78. var boundingRect = field.boundingRect; // For performance, if originRoot is not used by a view,
  79. // assign it to a view, but not reproduce graphic elements.
  80. if (!field.originRootHostKey) {
  81. field.originRootHostKey = hostKey;
  82. root = originRoot;
  83. } else {
  84. root = buildGraphic(mapRecord, boundingRect).root;
  85. }
  86. return rootMap.set(hostKey, root);
  87. },
  88. removeGraphic: function (mapName, mapRecord, hostKey) {
  89. var field = inner(mapRecord);
  90. var rootMap = field.rootMap;
  91. rootMap && rootMap.removeKey(hostKey);
  92. if (hostKey === field.originRootHostKey) {
  93. field.originRootHostKey = null;
  94. }
  95. }
  96. };
  97. function buildGraphic(mapRecord, boundingRect) {
  98. var svgXML = mapRecord.svgXML;
  99. var result;
  100. var root;
  101. try {
  102. result = svgXML && parseSVG(svgXML, {
  103. ignoreViewBox: true,
  104. ignoreRootClip: true
  105. }) || {};
  106. root = result.root;
  107. assert(root != null);
  108. } catch (e) {
  109. throw new Error('Invalid svg format\n' + e.message);
  110. }
  111. var svgWidth = result.width;
  112. var svgHeight = result.height;
  113. var viewBoxRect = result.viewBoxRect;
  114. if (!boundingRect) {
  115. boundingRect = svgWidth == null || svgHeight == null ? // If svg width / height not specified, calculate
  116. // bounding rect as the width / height
  117. root.getBoundingRect() : new BoundingRect(0, 0, 0, 0);
  118. if (svgWidth != null) {
  119. boundingRect.width = svgWidth;
  120. }
  121. if (svgHeight != null) {
  122. boundingRect.height = svgHeight;
  123. }
  124. }
  125. if (viewBoxRect) {
  126. var viewBoxTransform = makeViewBoxTransform(viewBoxRect, boundingRect.width, boundingRect.height);
  127. var elRoot = root;
  128. root = new Group();
  129. root.add(elRoot);
  130. elRoot.scale = viewBoxTransform.scale;
  131. elRoot.position = viewBoxTransform.position;
  132. }
  133. root.setClipPath(new Rect({
  134. shape: boundingRect.plain()
  135. }));
  136. return {
  137. root: root,
  138. boundingRect: boundingRect
  139. };
  140. }
  141. module.exports = _default;