@@ -162,9 +162,8 @@ acgraph.vector.svg.Renderer.prototype.getAttribute_ = function(el, key) {
162162//----------------------------------------------------------------------------------------------------------------------
163163/**
164164 * Desc.
165- * @private
166165 */
167- acgraph . vector . svg . Renderer . prototype . createMeasurement_ = function ( ) {
166+ acgraph . vector . svg . Renderer . prototype . createMeasurement = function ( ) {
168167 this . measurement_ = this . createSVGElement_ ( 'svg' ) ;
169168 this . measurementText_ = this . createTextElement ( ) ;
170169 this . measurementTextNode_ = this . createTextNode ( '' ) ;
@@ -173,6 +172,9 @@ acgraph.vector.svg.Renderer.prototype.createMeasurement_ = function() {
173172 goog . dom . appendChild ( this . measurement_ , this . measurementText_ ) ;
174173 goog . dom . appendChild ( goog . dom . getDocument ( ) . body , this . measurement_ ) ;
175174
175+ this . measurementLayerForBBox_ = this . createLayerElement ( ) ;
176+ goog . dom . appendChild ( this . measurement_ , this . measurementLayerForBBox_ ) ;
177+
176178 //We need set 'display: block' for <svg> element to prevent scrollbar on 100% height of parent container (see DVF-620)
177179 this . setAttributes_ ( this . measurement_ , { 'display' : 'block' , 'width' : 0 , 'height' : 0 } ) ;
178180
@@ -189,7 +191,7 @@ acgraph.vector.svg.Renderer.prototype.createMeasurement_ = function() {
189191 */
190192acgraph . vector . svg . Renderer . prototype . measure = function ( text , style ) {
191193 //if (text == '') return new goog.math.Rect(0, 0, 0, 0);
192- if ( ! this . measurement_ ) this . createMeasurement_ ( ) ;
194+ if ( ! this . measurement_ ) this . createMeasurement ( ) ;
193195
194196 var spaceWidth = null ;
195197 var additionWidth = 0 ;
@@ -248,13 +250,53 @@ acgraph.vector.svg.Renderer.prototype.measure = function(text, style) {
248250} ;
249251
250252
253+ /** @inheritDoc */
254+ acgraph . vector . svg . Renderer . prototype . getBBox = function ( element , text , style ) {
255+ var boundsCache = this . textBoundsCache ;
256+ var styleHash = this . getStyleHash ( style ) ;
257+ var styleCache = boundsCache [ styleHash ] ;
258+ if ( ! styleCache ) styleCache = boundsCache [ styleHash ] = { } ;
259+ var textBoundsCache = styleCache [ text ] ;
260+
261+ if ( textBoundsCache ) {
262+ return textBoundsCache ;
263+ } else {
264+ var spaceWidth = null ;
265+ var additionWidth = 0 ;
266+
267+ if ( text . length == 0 ) {
268+ return this . getEmptyStringBounds ( style ) ;
269+ }
270+
271+ if ( goog . string . isSpace ( text ) ) {
272+ return this . getSpaceBounds ( style ) ;
273+ } else {
274+ if ( goog . string . startsWith ( text , ' ' ) )
275+ additionWidth += spaceWidth = this . getSpaceBounds ( style ) . width ;
276+ if ( goog . string . endsWith ( text , ' ' ) )
277+ additionWidth += spaceWidth || this . getSpaceBounds ( style ) . width ;
278+ }
279+
280+ var parentNode = element . parentNode ;
281+
282+ this . measurementLayerForBBox_ . appendChild ( element ) ;
283+ var bbox = element [ 'getBBox' ] ( ) ;
284+ if ( parentNode ) parentNode . appendChild ( element ) ;
285+
286+ var x = element . getAttribute ( 'x' ) || 0 ;
287+ var y = element . getAttribute ( 'y' ) || 0 ;
288+ return styleCache [ text ] = new goog . math . Rect ( bbox . x - x , bbox . y - y , bbox . width + additionWidth , bbox . height ) ;
289+ }
290+ } ;
291+
292+
251293/**
252294 * Measure any svg nodes.
253295 * @param {string|Node } element .
254296 * @return {goog.math.Rect } .
255297 */
256298acgraph . vector . svg . Renderer . prototype . measureElement = function ( element ) {
257- if ( ! this . measurement_ ) this . createMeasurement_ ( ) ;
299+ if ( ! this . measurement_ ) this . createMeasurement ( ) ;
258300
259301 if ( goog . isString ( element ) ) {
260302 this . measurementGroupNode_ . innerHTML = element ;
0 commit comments