@@ -457,25 +457,16 @@ function toMoveInsideBar(x0, x1, y0, y1, textBB, opts) {
457457 lx -= 2 * textpad ;
458458 ly -= 2 * textpad ;
459459
460- var autoRotate = ( angle === 'auto' ) ;
461- var isAutoRotated = false ;
462- if ( autoRotate &&
460+ var rotate = getRotateFromAngle ( angle ) ;
461+ if ( ( angle === 'auto' ) &&
463462 ! ( textWidth <= lx && textHeight <= ly ) &&
464463 ( textWidth > lx || textHeight > ly ) && (
465464 ! ( textWidth > ly || textHeight > lx ) ||
466465 ( ( textWidth < textHeight ) !== ( lx < ly ) )
467466 ) ) {
468- isAutoRotated = true ;
469- }
470-
471- if ( isAutoRotated ) {
472- // don't rotate yet only swap bar width with height
473- var tmp = ly ;
474- ly = lx ;
475- lx = tmp ;
467+ rotate += 90 ;
476468 }
477469
478- var rotate = getRotateFromAngle ( angle ) ;
479470 var t = getRotatedTextSize ( textBB , rotate ) ;
480471
481472 var scale = 1 ;
@@ -488,31 +479,42 @@ function toMoveInsideBar(x0, x1, y0, y1, textBB, opts) {
488479 }
489480
490481 // compute text and target positions
482+ var textX = ( textBB . left + textBB . right ) / 2 ;
483+ var textY = ( textBB . top + textBB . bottom ) / 2 ;
491484 var targetX = ( x0 + x1 ) / 2 ;
492485 var targetY = ( y0 + y1 ) / 2 ;
486+ var anchorX = 0 ;
487+ var anchorY = 0 ;
493488 if ( isStart || isEnd ) {
494- textpad += 0.5 * scale * ( isHorizontal !== isAutoRotated ? t . x : t . y ) ;
489+ var extrapad = ( isHorizontal ? t . x : t . y ) / 2 ;
490+ var dir = isHorizontal ? dirSign ( x0 , x1 ) : dirSign ( y0 , y1 ) ;
495491
496492 if ( isHorizontal ) {
497- textpad *= dirSign ( x0 , x1 ) ;
498- targetX = isStart ? x0 + textpad : x1 - textpad ;
493+ if ( isStart ) {
494+ targetX = x0 + dir * textpad ;
495+ anchorX = - dir * extrapad ;
496+ } else {
497+ targetX = x1 - dir * textpad ;
498+ anchorX = dir * extrapad ;
499+ }
499500 } else {
500- textpad *= dirSign ( y0 , y1 ) ;
501- targetY = isStart ? y0 + textpad : y1 - textpad ;
501+ if ( isStart ) {
502+ targetY = y0 + dir * textpad ;
503+ anchorY = - dir * extrapad ;
504+ } else {
505+ targetY = y1 - dir * textpad ;
506+ anchorY = dir * extrapad ;
507+ }
502508 }
503509 }
504510
505- var textX = ( textBB . left + textBB . right ) / 2 ;
506- var textY = ( textBB . top + textBB . bottom ) / 2 ;
507-
508- // lastly apply auto rotation
509- if ( isAutoRotated ) rotate += 90 ;
510-
511511 return {
512512 textX : textX ,
513513 textY : textY ,
514514 targetX : targetX ,
515515 targetY : targetY ,
516+ anchorX : anchorX ,
517+ anchorY : anchorY ,
516518 scale : scale ,
517519 rotate : rotate
518520 } ;
@@ -549,24 +551,30 @@ function toMoveOutsideBar(x0, x1, y0, y1, textBB, opts) {
549551 var t = getRotatedTextSize ( textBB , rotate ) ;
550552
551553 // compute text and target positions
552- textpad += 0.5 * scale * ( isHorizontal ? t . x : t . y ) ;
554+ var extrapad = ( isHorizontal ? t . x : t . y ) / 2 ;
555+ var textX = ( textBB . left + textBB . right ) / 2 ;
556+ var textY = ( textBB . top + textBB . bottom ) / 2 ;
553557 var targetX = ( x0 + x1 ) / 2 ;
554558 var targetY = ( y0 + y1 ) / 2 ;
559+ var anchorX = 0 ;
560+ var anchorY = 0 ;
555561
562+ var dir = isHorizontal ? dirSign ( x1 , x0 ) : dirSign ( y0 , y1 ) ;
556563 if ( isHorizontal ) {
557- targetX = x1 - textpad * dirSign ( x1 , x0 ) ;
564+ targetX = x1 - dir * textpad ;
565+ anchorX = dir * extrapad ;
558566 } else {
559- targetY = y1 + textpad * dirSign ( y0 , y1 ) ;
567+ targetY = y1 + dir * textpad ;
568+ anchorY = - dir * extrapad ;
560569 }
561570
562- var textX = ( textBB . left + textBB . right ) / 2 ;
563- var textY = ( textBB . top + textBB . bottom ) / 2 ;
564-
565571 return {
566572 textX : textX ,
567573 textY : textY ,
568574 targetX : targetX ,
569575 targetY : targetY ,
576+ anchorX : anchorX ,
577+ anchorY : anchorY ,
570578 scale : scale ,
571579 rotate : rotate
572580 } ;
0 commit comments