@@ -420,13 +420,6 @@ protected void drawGeographic(DrawContext dc, SurfaceTileDrawContext sdc)
420
420
protected void drawText (DrawContext dc )
421
421
{
422
422
TextRenderer tr = this .getTextRenderer (dc );
423
-
424
- Point2D point = this .getOffset ().computeOffset (this .textBounds .getWidth (), this .textBounds .getHeight (), null ,
425
- null );
426
-
427
- int x = (int ) point .getX ();
428
- int y = (int ) point .getY ();
429
-
430
423
try
431
424
{
432
425
tr .begin3DRendering ();
@@ -435,9 +428,9 @@ protected void drawText(DrawContext dc)
435
428
CharSequence text = this .getText ();
436
429
437
430
tr .setColor (bgColor );
438
- tr .draw (text , x + 1 , y - 1 );
431
+ tr .draw (text , 1 , - 1 );
439
432
tr .setColor (this .getColor ());
440
- tr .draw (text , x , y );
433
+ tr .draw (text , 0 , 0 );
441
434
}
442
435
finally
443
436
{
@@ -487,9 +480,24 @@ protected void applyDrawTransform(DrawContext dc, SurfaceTileDrawContext sdc)
487
480
488
481
// Apply the scaling factor to draw the text at the correct geographic size
489
482
gl .glScaled (this .scale , this .scale , 1d );
490
-
491
- // Apply rotation angle
483
+
484
+ double widthInPixels = this .textBounds .getWidth ();
485
+ double heightInPixels = this .textBounds .getHeight ();
486
+
487
+ Point2D textDimensions = getRotatedTextDimensions ();
488
+ double rotatedPixelHeight = textDimensions .getY ();
489
+ double rotatedPixelWidth = textDimensions .getX ();
490
+
491
+ Point2D textOffset = getOffset ().computeOffset (rotatedPixelWidth , rotatedPixelHeight , null , null );
492
+
493
+ // Move to offset position.
494
+ gl .glTranslated (rotatedPixelWidth / 2.0 + textOffset .getX (), rotatedPixelHeight / 2.0 + textOffset .getY (), 0 );
495
+
496
+ // Apply rotation angle on from center.
492
497
gl .glRotated (-this .heading .degrees , 0 , 0 , 1 );
498
+
499
+ // Move to center.
500
+ gl .glTranslated (-widthInPixels / 2.0 , -heightInPixels / 2.0 , 0 );
493
501
}
494
502
495
503
/**
@@ -552,6 +560,54 @@ protected Color computeBackgroundColor(Color color)
552
560
else
553
561
return new Color (1 , 1 , 1 , 0.7f );
554
562
}
563
+
564
+ private Point2D getRotatedTextDimensions ()
565
+ {
566
+ double widthInPixels = this .textBounds .getWidth ();
567
+ double heightInPixels = this .textBounds .getHeight ();
568
+
569
+ Angle rotation = Angle .normalizedLongitude (this .heading );
570
+ double ct = Math .cos (rotation .radians );
571
+ double st = Math .sin (rotation .radians );
572
+
573
+ double hct = heightInPixels * ct ;
574
+ double wct = widthInPixels * ct ;
575
+ double hst = heightInPixels * st ;
576
+ double wst = widthInPixels * st ;
577
+
578
+ if (rotation .degrees > 0 )
579
+ {
580
+ if (rotation .degrees < 90 )
581
+ {
582
+ // 0 < theta < 90
583
+ heightInPixels = hct + wst ;
584
+ widthInPixels = wct + hst ;
585
+ }
586
+ else
587
+ {
588
+ // 90 <= theta <= 180
589
+ heightInPixels = wst - hct ;
590
+ widthInPixels = hst - wct ;
591
+ }
592
+ }
593
+ else
594
+ {
595
+ if (rotation .degrees > -90 )
596
+ {
597
+ // -90 < theta <= 0
598
+ heightInPixels = hct - wst ;
599
+ widthInPixels = wct - hst ;
600
+ }
601
+ else
602
+ {
603
+ // -180 <= theta <= -90
604
+ heightInPixels = -(hct + wst );
605
+ widthInPixels = -(wct + hst );
606
+ }
607
+ }
608
+
609
+ return new Point2D .Double (widthInPixels , heightInPixels );
610
+ }
555
611
556
612
/**
557
613
* Compute the sector covered by this surface text.
@@ -565,30 +621,32 @@ protected Sector[] computeSector(DrawContext dc)
565
621
// Compute text extent depending on distance from eye
566
622
Globe globe = dc .getGlobe ();
567
623
568
- double widthInPixels = this .textBounds .getWidth ();
569
- double heightInPixels = this .textBounds .getHeight ();
570
-
571
- double heightInMeters = this .textSizeInMeters ;
624
+ Point2D textDimensions = getRotatedTextDimensions ();
625
+ double heightInPixels = textDimensions .getY ();
626
+ double widthInPixels = textDimensions .getX ();
627
+
628
+ double heightFactor = heightInPixels / this .textBounds .getHeight ();
629
+ double heightInMeters = heightFactor * this .textSizeInMeters ;
572
630
double widthInMeters = heightInMeters * (widthInPixels / heightInPixels );
573
-
631
+
574
632
double radius = globe .getRadius ();
575
633
double heightInRadians = heightInMeters / radius ;
576
634
double widthInRadians = widthInMeters / radius ;
577
635
578
- // Compute the offset from the reference position. Convert pixels to meters based on the geographic size
579
- // of the text.
580
- Point2D point = this . getOffset ().computeOffset (widthInPixels , heightInPixels , null , null );
636
+ // Compute the offset from the reference position.
637
+ // Convert pixels to meters based on the geographic size of the text.
638
+ Point2D textOffset = getOffset ().computeOffset (widthInPixels , heightInPixels , null , null );
581
639
582
640
double metersPerPixel = heightInMeters / heightInPixels ;
583
641
584
- double dxRadians = (point .getX () * metersPerPixel ) / radius ;
585
- double dyRadians = (point .getY () * metersPerPixel ) / radius ;
586
-
642
+ double dxRadians = (textOffset .getX () * metersPerPixel ) / radius ;
643
+ double dyRadians = (textOffset .getY () * metersPerPixel ) / radius ;
644
+
587
645
double minLat = this .location .latitude .addRadians (dyRadians ).degrees ;
588
646
double maxLat = this .location .latitude .addRadians (dyRadians + heightInRadians ).degrees ;
589
647
double minLon = this .location .longitude .addRadians (dxRadians ).degrees ;
590
648
double maxLon = this .location .longitude .addRadians (dxRadians + widthInRadians ).degrees ;
591
-
649
+
592
650
this .drawLocation = LatLon .fromDegrees (minLat , minLon );
593
651
594
652
if (maxLon > 180 ) {
0 commit comments