-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(draw): unify text rendering into service
A newly introduced TextRenderer is responsible for text rendering and text related bounds computation. This removes a bunch of code duplication, too.
- Loading branch information
Showing
9 changed files
with
245 additions
and
166 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { assign } from 'min-dash'; | ||
|
||
import TextUtil from 'diagram-js/lib/util/Text'; | ||
|
||
|
||
export default function TextRenderer(config) { | ||
|
||
var defaultStyle = assign({ | ||
fontFamily: 'Arial, sans-serif', | ||
fontSize: 12 | ||
}, config && config.defaultStyle || {}); | ||
|
||
var externalStyle = assign({}, defaultStyle, { | ||
fontSize: parseInt(defaultStyle.fontSize, 10) - 1 | ||
}, 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} | ||
*/ | ||
this.getLayoutedBounds = function(bounds, text) { | ||
|
||
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) | ||
}; | ||
|
||
}; | ||
|
||
/** | ||
* 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' | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
import BpmnRenderer from './BpmnRenderer'; | ||
import TextRenderer from './TextRenderer'; | ||
|
||
import PathMap from './PathMap'; | ||
|
||
export default { | ||
__init__: [ 'bpmnRenderer' ], | ||
bpmnRenderer: [ 'type', BpmnRenderer ], | ||
textRenderer: [ 'type', TextRenderer ], | ||
pathMap: [ 'type', PathMap ] | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.