text.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var text_1 = require("../util/text");
  4. function default_1(shape) {
  5. var attrs = shape.attr();
  6. var x = attrs.x, y = attrs.y, text = attrs.text, fontSize = attrs.fontSize, lineHeight = attrs.lineHeight;
  7. var font = attrs.font;
  8. if (!font) {
  9. // 如果未组装 font
  10. font = text_1.assembleFont(attrs);
  11. }
  12. var width = text_1.getTextWidth(text, font);
  13. var bbox;
  14. if (!width) {
  15. // 如果width不存在,四点共其实点
  16. bbox = {
  17. x: x,
  18. y: y,
  19. width: 0,
  20. height: 0,
  21. };
  22. }
  23. else {
  24. var textAlign = attrs.textAlign, textBaseline = attrs.textBaseline;
  25. var height = text_1.getTextHeight(text, fontSize, lineHeight); // attrs.height
  26. // 默认左右对齐:left, 默认上下对齐 bottom
  27. var point = {
  28. x: x,
  29. y: y - height,
  30. };
  31. if (textAlign) {
  32. if (textAlign === 'end' || textAlign === 'right') {
  33. point.x -= width;
  34. }
  35. else if (textAlign === 'center') {
  36. point.x -= width / 2;
  37. }
  38. }
  39. if (textBaseline) {
  40. if (textBaseline === 'top') {
  41. point.y += height;
  42. }
  43. else if (textBaseline === 'middle') {
  44. point.y += height / 2;
  45. }
  46. }
  47. bbox = {
  48. x: point.x,
  49. y: point.y,
  50. width: width,
  51. height: height,
  52. };
  53. }
  54. return bbox;
  55. }
  56. exports.default = default_1;
  57. //# sourceMappingURL=text.js.map