UNPKG

2.51 kBJavaScriptView Raw
1import { assign } from 'min-dash';
2
3import TextUtil from 'diagram-js/lib/util/Text';
4
5var DEFAULT_FONT_SIZE = 12;
6var LINE_HEIGHT_RATIO = 1.2;
7
8var MIN_TEXT_ANNOTATION_HEIGHT = 30;
9
10
11export default function TextRenderer(config) {
12
13 var defaultStyle = assign({
14 fontFamily: 'Arial, sans-serif',
15 fontSize: DEFAULT_FONT_SIZE,
16 fontWeight: 'normal',
17 lineHeight: LINE_HEIGHT_RATIO
18 }, config && config.defaultStyle || {});
19
20 var fontSize = parseInt(defaultStyle.fontSize, 10) - 1;
21
22 var externalStyle = assign({}, defaultStyle, {
23 fontSize: fontSize
24 }, config && config.externalStyle || {});
25
26 var textUtil = new TextUtil({
27 style: defaultStyle
28 });
29
30 /**
31 * Get the new bounds of an externally rendered,
32 * layouted label.
33 *
34 * @param {Bounds} bounds
35 * @param {String} text
36 *
37 * @return {Bounds}
38 */
39 this.getExternalLabelBounds = function(bounds, text) {
40
41 var layoutedDimensions = textUtil.getDimensions(text, {
42 box: {
43 width: 90,
44 height: 30,
45 x: bounds.width / 2 + bounds.x,
46 y: bounds.height / 2 + bounds.y
47 },
48 style: externalStyle
49 });
50
51 // resize label shape to fit label text
52 return {
53 x: Math.round(bounds.x + bounds.width / 2 - layoutedDimensions.width / 2),
54 y: Math.round(bounds.y),
55 width: Math.ceil(layoutedDimensions.width),
56 height: Math.ceil(layoutedDimensions.height)
57 };
58
59 };
60
61 /**
62 * Get the new bounds of text annotation.
63 *
64 * @param {Bounds} bounds
65 * @param {String} text
66 *
67 * @return {Bounds}
68 */
69 this.getTextAnnotationBounds = function(bounds, text) {
70
71 var layoutedDimensions = textUtil.getDimensions(text, {
72 box: bounds,
73 style: defaultStyle,
74 align: 'left-top',
75 padding: 5
76 });
77
78 return {
79 x: bounds.x,
80 y: bounds.y,
81 width: bounds.width,
82 height: Math.max(MIN_TEXT_ANNOTATION_HEIGHT, Math.round(layoutedDimensions.height))
83 };
84 };
85
86 /**
87 * Create a layouted text element.
88 *
89 * @param {String} text
90 * @param {Object} [options]
91 *
92 * @return {SVGElement} rendered text
93 */
94 this.createText = function(text, options) {
95 return textUtil.createText(text, options || {});
96 };
97
98 /**
99 * Get default text style.
100 */
101 this.getDefaultStyle = function() {
102 return defaultStyle;
103 };
104
105 /**
106 * Get the external text style.
107 */
108 this.getExternalStyle = function() {
109 return externalStyle;
110 };
111
112}
113
114TextRenderer.$inject = [
115 'config.textRenderer'
116];
\No newline at end of file