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