text.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
  4. var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
  5. var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));
  6. function _createSuper(Derived) { return function () { var Super = (0, _getPrototypeOf2["default"])(Derived), result; if (_isNativeReflectConstruct()) { var NewTarget = (0, _getPrototypeOf2["default"])(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2["default"])(this, result); }; }
  7. function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
  8. var Util = require('../../util/common');
  9. var Shape = require('../shape');
  10. var RectUtil = require('../util/rect');
  11. var textWidthCacheCounter = 0;
  12. var textWidthCache = {};
  13. var TEXT_CACHE_MAX = 5000;
  14. var Text = /*#__PURE__*/function (_Shape) {
  15. (0, _inheritsLoose2["default"])(Text, _Shape);
  16. var _super = _createSuper(Text);
  17. function Text() {
  18. return _Shape.apply(this, arguments) || this;
  19. }
  20. var _proto = Text.prototype;
  21. _proto._initProperties = function _initProperties() {
  22. _Shape.prototype._initProperties.call(this);
  23. this._attrs.canFill = true;
  24. this._attrs.canStroke = true;
  25. this._attrs.type = 'text';
  26. };
  27. _proto.getDefaultAttrs = function getDefaultAttrs() {
  28. return {
  29. lineWidth: 0,
  30. lineCount: 1,
  31. fontSize: 12,
  32. fontFamily: 'sans-serif',
  33. fontStyle: 'normal',
  34. fontWeight: 'normal',
  35. fontVariant: 'normal',
  36. textAlign: 'start',
  37. textBaseline: 'bottom',
  38. lineHeight: null,
  39. textArr: null
  40. };
  41. };
  42. _proto._getFontStyle = function _getFontStyle() {
  43. var attrs = this._attrs.attrs;
  44. var fontSize = attrs.fontSize,
  45. fontFamily = attrs.fontFamily,
  46. fontWeight = attrs.fontWeight,
  47. fontStyle = attrs.fontStyle,
  48. fontVariant = attrs.fontVariant;
  49. return fontStyle + " " + fontVariant + " " + fontWeight + " " + fontSize + "px " + fontFamily;
  50. };
  51. _proto._afterAttrsSet = function _afterAttrsSet() {
  52. var attrs = this._attrs.attrs;
  53. attrs.font = this._getFontStyle();
  54. if (attrs.text) {
  55. var text = attrs.text;
  56. var textArr = null;
  57. var lineCount = 1;
  58. if (Util.isString(text) && text.indexOf('\n') !== -1) {
  59. textArr = text.split('\n');
  60. lineCount = textArr.length;
  61. }
  62. attrs.lineCount = lineCount;
  63. attrs.textArr = textArr;
  64. }
  65. this.set('attrs', attrs);
  66. };
  67. _proto._getTextHeight = function _getTextHeight() {
  68. var attrs = this._attrs.attrs;
  69. if (attrs.height) {
  70. return attrs.height;
  71. }
  72. var lineCount = attrs.lineCount;
  73. var fontSize = attrs.fontSize * 1;
  74. if (lineCount > 1) {
  75. var spaceingY = this._getSpaceingY();
  76. return fontSize * lineCount + spaceingY * (lineCount - 1);
  77. }
  78. return fontSize;
  79. };
  80. _proto._getSpaceingY = function _getSpaceingY() {
  81. var attrs = this._attrs.attrs;
  82. var lineHeight = attrs.lineHeight;
  83. var fontSize = attrs.fontSize * 1;
  84. return lineHeight ? lineHeight - fontSize : fontSize * 0.14;
  85. };
  86. _proto.drawInner = function drawInner(context) {
  87. var self = this;
  88. var attrs = self._attrs.attrs;
  89. var text = attrs.text;
  90. var x = attrs.x;
  91. var y = attrs.y;
  92. if (Util.isNil(text) || isNaN(x) || isNaN(y)) {
  93. // text will be 0
  94. return;
  95. }
  96. var textArr = attrs.textArr;
  97. var fontSize = attrs.fontSize * 1;
  98. var spaceingY = self._getSpaceingY();
  99. if (attrs.rotate) {
  100. // do rotation
  101. context.translate(x, y);
  102. context.rotate(attrs.rotate);
  103. x = 0;
  104. y = 0;
  105. }
  106. var textBaseline = attrs.textBaseline;
  107. var height;
  108. if (textArr) {
  109. height = self._getTextHeight();
  110. }
  111. var subY; // context.beginPath();
  112. if (self.hasFill()) {
  113. var fillOpacity = attrs.fillOpacity;
  114. if (!Util.isNil(fillOpacity) && fillOpacity !== 1) {
  115. context.globalAlpha = fillOpacity;
  116. }
  117. if (textArr) {
  118. for (var i = 0, len = textArr.length; i < len; i++) {
  119. var subText = textArr[i];
  120. subY = y + i * (spaceingY + fontSize) - height + fontSize; // bottom;
  121. if (textBaseline === 'middle') {
  122. subY += height - fontSize - (height - fontSize) / 2;
  123. }
  124. if (textBaseline === 'top') {
  125. subY += height - fontSize;
  126. }
  127. context.fillText(subText, x, subY);
  128. }
  129. } else {
  130. context.fillText(text, x, y);
  131. }
  132. }
  133. if (self.hasStroke()) {
  134. if (textArr) {
  135. for (var _i = 0, _len = textArr.length; _i < _len; _i++) {
  136. var _subText = textArr[_i];
  137. subY = y + _i * (spaceingY + fontSize) - height + fontSize; // bottom;
  138. if (textBaseline === 'middle') {
  139. subY += height - fontSize - (height - fontSize) / 2;
  140. }
  141. if (textBaseline === 'top') {
  142. subY += height - fontSize;
  143. }
  144. context.strokeText(_subText, x, subY);
  145. }
  146. } else {
  147. context.strokeText(text, x, y);
  148. }
  149. }
  150. };
  151. _proto.calculateBox = function calculateBox() {
  152. var self = this;
  153. var attrs = self._attrs.attrs;
  154. var x = attrs.x,
  155. y = attrs.y,
  156. textAlign = attrs.textAlign,
  157. textBaseline = attrs.textBaseline;
  158. var width = self._getTextWidth(); // attrs.width
  159. if (!width) {
  160. return {
  161. minX: x,
  162. minY: y,
  163. maxX: x,
  164. maxY: y
  165. };
  166. }
  167. var height = self._getTextHeight(); // attrs.height
  168. if (attrs.rotate) {
  169. var rotatedBox = RectUtil.calcRotatedBox({
  170. width: width,
  171. height: height,
  172. rotate: attrs.rotate
  173. });
  174. width = rotatedBox.width;
  175. height = rotatedBox.height;
  176. }
  177. var point = {
  178. x: x,
  179. y: y - height
  180. }; // default textAlign: start, textBaseline: bottom
  181. if (textAlign) {
  182. if (textAlign === 'end' || textAlign === 'right') {
  183. point.x -= width;
  184. } else if (textAlign === 'center') {
  185. point.x -= width / 2;
  186. }
  187. }
  188. if (textBaseline) {
  189. if (textBaseline === 'top') {
  190. point.y += height;
  191. } else if (textBaseline === 'middle') {
  192. point.y += height / 2;
  193. }
  194. }
  195. return {
  196. minX: point.x,
  197. minY: point.y,
  198. maxX: point.x + width,
  199. maxY: point.y + height
  200. };
  201. };
  202. _proto._getTextWidth = function _getTextWidth() {
  203. var attrs = this._attrs.attrs;
  204. if (attrs.width) {
  205. return attrs.width;
  206. }
  207. var text = attrs.text;
  208. var context = this.get('context');
  209. if (Util.isNil(text)) return undefined;
  210. var font = attrs.font;
  211. var textArr = attrs.textArr;
  212. var key = text + '' + font;
  213. if (textWidthCache[key]) {
  214. return textWidthCache[key];
  215. }
  216. var width = 0;
  217. if (textArr) {
  218. for (var i = 0, length = textArr.length; i < length; i++) {
  219. var subText = textArr[i];
  220. width = Math.max(width, Util.measureText(subText, font, context).width);
  221. }
  222. } else {
  223. width = Util.measureText(text, font, context).width;
  224. }
  225. if (textWidthCacheCounter > TEXT_CACHE_MAX) {
  226. textWidthCacheCounter = 0;
  227. textWidthCache = {};
  228. }
  229. textWidthCacheCounter++;
  230. textWidthCache[key] = width;
  231. return width;
  232. };
  233. return Text;
  234. }(Shape);
  235. Shape.Text = Text;
  236. module.exports = Text;