@@ -424,18 +424,31 @@ function getRotateFromAngle(angle) {
424
424
return ( angle === 'auto' ) ? 0 : angle ;
425
425
}
426
426
427
+ function getRotatedTextSize ( textBB , rotate ) {
428
+ var a = Math . PI / 180 * rotate ;
429
+ var absSin = Math . abs ( Math . sin ( a ) ) ;
430
+ var absCos = Math . abs ( Math . cos ( a ) ) ;
431
+
432
+ return {
433
+ x : textBB . width * absCos + textBB . height * absSin ,
434
+ y : textBB . width * absSin + textBB . height * absCos
435
+ } ;
436
+ }
437
+
427
438
function toMoveInsideBar ( x0 , x1 , y0 , y1 , textBB , opts ) {
428
439
var isHorizontal = ! ! opts . isHorizontal ;
429
440
var constrained = ! ! opts . constrained ;
430
441
var angle = opts . angle || 0 ;
431
- var anchor = opts . anchor || 0 ;
442
+ var anchor = opts . anchor || 'end' ;
443
+ var isEnd = anchor === 'end' ;
444
+ var isStart = anchor === 'start' ;
432
445
433
446
var textWidth = textBB . width ;
434
447
var textHeight = textBB . height ;
435
448
var lx = Math . abs ( x1 - x0 ) ;
436
449
var ly = Math . abs ( y1 - y0 ) ;
437
450
438
- // compute left space
451
+ // compute remaining space
439
452
var textpad = (
440
453
lx > ( 2 * TEXTPAD ) &&
441
454
ly > ( 2 * TEXTPAD )
@@ -463,38 +476,29 @@ function toMoveInsideBar(x0, x1, y0, y1, textBB, opts) {
463
476
}
464
477
465
478
var rotate = getRotateFromAngle ( angle ) ;
466
- var absSin = Math . abs ( Math . sin ( Math . PI / 180 * rotate ) ) ;
467
- var absCos = Math . abs ( Math . cos ( Math . PI / 180 * rotate ) ) ;
468
-
469
- // compute text space
470
- var tx = textWidth * absCos + textHeight * absSin ;
471
- var ty = textWidth * absSin + textHeight * absCos ;
479
+ var t = getRotatedTextSize ( textBB , rotate ) ;
472
480
473
481
var scale = 1 ;
474
482
if ( constrained ) {
475
483
scale = Math . min (
476
484
1 ,
477
- lx / tx ,
478
- ly / ty
485
+ lx / t . x ,
486
+ ly / t . y
479
487
) ;
480
- scale = Math . min ( 1 , scale ) ;
481
488
}
482
489
483
490
// compute text and target positions
484
491
var targetX = ( x0 + x1 ) / 2 ;
485
492
var targetY = ( y0 + y1 ) / 2 ;
486
-
487
- if ( anchor !== 'middle' ) { // case of 'start' or 'end'
488
- var targetWidth = scale * ( isHorizontal !== isAutoRotated ? textHeight : textWidth ) ;
489
- var targetHeight = scale * ( isHorizontal !== isAutoRotated ? textWidth : textHeight ) ;
490
- textpad += 0.5 * ( targetWidth * absSin + targetHeight * absCos ) ;
493
+ if ( isStart || isEnd ) {
494
+ textpad += 0.5 * scale * ( isHorizontal !== isAutoRotated ? t . x : t . y ) ;
491
495
492
496
if ( isHorizontal ) {
493
497
textpad *= dirSign ( x0 , x1 ) ;
494
- targetX = ( anchor === 'start' ) ? x0 + textpad : x1 - textpad ;
498
+ targetX = isStart ? x0 + textpad : x1 - textpad ;
495
499
} else {
496
500
textpad *= dirSign ( y0 , y1 ) ;
497
- targetY = ( anchor === 'start' ) ? y0 + textpad : y1 - textpad ;
501
+ targetY = isStart ? y0 + textpad : y1 - textpad ;
498
502
}
499
503
}
500
504
@@ -542,14 +546,10 @@ function toMoveOutsideBar(x0, x1, y0, y1, textBB, opts) {
542
546
}
543
547
544
548
var rotate = getRotateFromAngle ( angle ) ;
545
- var absSin = Math . abs ( Math . sin ( Math . PI / 180 * rotate ) ) ;
546
- var absCos = Math . abs ( Math . cos ( Math . PI / 180 * rotate ) ) ;
549
+ var t = getRotatedTextSize ( textBB , rotate ) ;
547
550
548
551
// compute text and target positions
549
- var targetWidth = scale * ( isHorizontal ? textHeight : textWidth ) ;
550
- var targetHeight = scale * ( isHorizontal ? textWidth : textHeight ) ;
551
- textpad += 0.5 * ( targetWidth * absSin + targetHeight * absCos ) ;
552
-
552
+ textpad += 0.5 * scale * ( isHorizontal ? t . x : t . y ) ;
553
553
var targetX = ( x0 + x1 ) / 2 ;
554
554
var targetY = ( y0 + y1 ) / 2 ;
555
555
0 commit comments