UNPKG

2.97 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/**
11 * @typedef { {
12 * fontFamily: string;
13 * fontSize: number;
14 * fontWeight: string;
15 * lineHeight: number;
16 * } } TextRendererStyle
17 *
18 * @typedef { {
19 * defaultStyle?: Partial<TextRendererStyle>;
20 * externalStyle?: Partial<TextRendererStyle>;
21 * } } TextRendererConfig
22 *
23 * @typedef { import('diagram-js/lib/util/Text').TextLayoutConfig } TextLayoutConfig
24 *
25 * @typedef { import('diagram-js/lib/util/Types').Rect } Rect
26 */
27
28
29/**
30 * Renders text and computes text bounding boxes.
31 *
32 * @param {TextRendererConfig} [config]
33 */
34export 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 * Get the new bounds of an externally rendered,
55 * layouted label.
56 *
57 * @param {Rect} bounds
58 * @param {string} text
59 *
60 * @return {Rect}
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 // resize label shape to fit label text
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 * Get the new bounds of text annotation.
84 *
85 * @param {Rect} bounds
86 * @param {string} text
87 *
88 * @return {Rect}
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 * Create a layouted text element.
109 *
110 * @param {string} text
111 * @param {TextLayoutConfig} [options]
112 *
113 * @return {SVGElement} rendered text
114 */
115 this.createText = function(text, options) {
116 return textUtil.createText(text, options || {});
117 };
118
119 /**
120 * Get default text style.
121 */
122 this.getDefaultStyle = function() {
123 return defaultStyle;
124 };
125
126 /**
127 * Get the external text style.
128 */
129 this.getExternalStyle = function() {
130 return externalStyle;
131 };
132
133}
134
135TextRenderer.$inject = [
136 'config.textRenderer'
137];
\No newline at end of file