2018-05-24 11:42:40 +00:00
|
|
|
import { assign } from 'min-dash';
|
|
|
|
|
|
|
|
import TextUtil from 'diagram-js/lib/util/Text';
|
|
|
|
|
2018-05-25 11:56:12 +00:00
|
|
|
var DEFAULT_FONT_SIZE = 12;
|
|
|
|
var LINE_HEIGHT_RATIO = 1.2;
|
|
|
|
|
2018-06-13 07:01:16 +00:00
|
|
|
var MIN_TEXT_ANNOTATION_HEIGHT = 30;
|
|
|
|
|
2018-05-24 11:42:40 +00:00
|
|
|
|
|
|
|
export default function TextRenderer(config) {
|
|
|
|
|
|
|
|
var defaultStyle = assign({
|
2018-05-25 11:56:12 +00:00
|
|
|
fontFamily: 'sans-serif',
|
|
|
|
fontSize: DEFAULT_FONT_SIZE,
|
|
|
|
fontWeight: 'normal',
|
|
|
|
lineHeight: LINE_HEIGHT_RATIO
|
2018-05-24 11:42:40 +00:00
|
|
|
}, config && config.defaultStyle || {});
|
|
|
|
|
2018-05-25 11:56:12 +00:00
|
|
|
var fontSize = parseInt(defaultStyle.fontSize, 10) - 1;
|
|
|
|
|
2018-05-24 11:42:40 +00:00
|
|
|
var externalStyle = assign({}, defaultStyle, {
|
2018-05-25 11:56:12 +00:00
|
|
|
fontSize: fontSize
|
2018-05-24 11:42:40 +00:00
|
|
|
}, config && config.externalStyle || {});
|
|
|
|
|
|
|
|
var textUtil = new TextUtil({
|
|
|
|
style: defaultStyle
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the new bounds of an externally rendered,
|
|
|
|
* layouted label.
|
|
|
|
*
|
|
|
|
* @param {Bounds} bounds
|
|
|
|
* @param {String} text
|
|
|
|
*
|
|
|
|
* @return {Bounds}
|
|
|
|
*/
|
2018-06-13 07:01:16 +00:00
|
|
|
this.getExternalLabelBounds = function(bounds, text) {
|
2018-05-24 11:42:40 +00:00
|
|
|
|
|
|
|
var layoutedDimensions = textUtil.getDimensions(text, {
|
|
|
|
box: {
|
|
|
|
width: 90,
|
|
|
|
height: 30,
|
|
|
|
x: bounds.width / 2 + bounds.x,
|
|
|
|
y: bounds.height / 2 + bounds.y
|
|
|
|
},
|
|
|
|
style: externalStyle
|
|
|
|
});
|
|
|
|
|
|
|
|
// resize label shape to fit label text
|
|
|
|
return {
|
|
|
|
x: Math.round(bounds.x + bounds.width / 2 - layoutedDimensions.width / 2),
|
|
|
|
y: Math.round(bounds.y),
|
|
|
|
width: Math.ceil(layoutedDimensions.width),
|
|
|
|
height: Math.ceil(layoutedDimensions.height)
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2018-06-13 07:01:16 +00:00
|
|
|
/**
|
|
|
|
* Get the new bounds of text annotation.
|
|
|
|
*
|
|
|
|
* @param {Bounds} bounds
|
|
|
|
* @param {String} text
|
|
|
|
*
|
|
|
|
* @return {Bounds}
|
|
|
|
*/
|
|
|
|
this.getTextAnnotationBounds = function(bounds, text) {
|
|
|
|
|
|
|
|
var layoutedDimensions = textUtil.getDimensions(text, {
|
|
|
|
box: bounds,
|
|
|
|
style: defaultStyle,
|
|
|
|
align: 'left-top',
|
|
|
|
padding: 5
|
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
x: bounds.x,
|
|
|
|
y: bounds.y,
|
|
|
|
width: bounds.width,
|
|
|
|
height: Math.max(MIN_TEXT_ANNOTATION_HEIGHT, Math.round(layoutedDimensions.height))
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2018-05-24 11:42:40 +00:00
|
|
|
/**
|
|
|
|
* Create a layouted text element.
|
|
|
|
*
|
|
|
|
* @param {String} text
|
|
|
|
* @param {Object} [options]
|
|
|
|
*
|
|
|
|
* @return {SVGElement} rendered text
|
|
|
|
*/
|
|
|
|
this.createText = function(text, options) {
|
|
|
|
return textUtil.createText(text, options || {});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get default text style.
|
|
|
|
*/
|
|
|
|
this.getDefaultStyle = function() {
|
|
|
|
return defaultStyle;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the external text style.
|
|
|
|
*/
|
|
|
|
this.getExternalStyle = function() {
|
|
|
|
return externalStyle;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
TextRenderer.$inject = [
|
|
|
|
'config.textRenderer'
|
|
|
|
];
|