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 | export 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 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
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 |
|
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 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
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 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 | this.createText = function(text, options) {
|
95 | return textUtil.createText(text, options || {});
|
96 | };
|
97 |
|
98 | |
99 |
|
100 |
|
101 | this.getDefaultStyle = function() {
|
102 | return defaultStyle;
|
103 | };
|
104 |
|
105 | |
106 |
|
107 |
|
108 | this.getExternalStyle = function() {
|
109 | return externalStyle;
|
110 | };
|
111 |
|
112 | }
|
113 |
|
114 | TextRenderer.$inject = [
|
115 | 'config.textRenderer'
|
116 | ]; |
\ | No newline at end of file |