vtkTextActor.cxx 32.8 KB
Newer Older
1 2 3 4 5
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkTextActor.cxx

6
  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7 8 9
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.

10 11
     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12 13 14 15 16
     PURPOSE.  See the above copyright notice for more information.

=========================================================================*/
#include "vtkTextActor.h"
#include "vtkObjectFactory.h"
17
#include "vtkPolyDataMapper2D.h"
Sebastien Barre's avatar
Sebastien Barre committed
18 19
#include "vtkTextProperty.h"
#include "vtkViewport.h"
20
#include "vtkWindow.h"
21 22
#include "vtkTransform.h"
#include "vtkImageData.h"
23 24 25 26 27 28 29
#include "vtkPoints.h"
#include "vtkPointData.h"
#include "vtkPolyData.h"
#include "vtkCellArray.h"
#include "vtkFloatArray.h"
#include "vtkTexture.h"
#include "vtkMath.h"
30
#include "vtkTextRenderer.h"
31
#include "vtkRenderer.h"
32

33 34
#include <algorithm>

35
vtkStandardNewMacro(vtkTextActor);
36
vtkCxxSetObjectMacro(vtkTextActor,Texture,vtkTexture);
Sebastien Barre's avatar
Sebastien Barre committed
37

38 39 40
// ----------------------------------------------------------------------------
vtkTextActor::vtkTextActor()
{
Sebastien Barre's avatar
Sebastien Barre committed
41
  // To remain compatible with code using vtkActor2D, we must set
42 43 44
  // position coord to Viewport, not Normalized Viewport
  // so...compute equivalent coords for initial position
  this->PositionCoordinate->SetCoordinateSystemToViewport();
45

46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
  // This intializes the rectangle structure.
  // It will be used to display the text image as a texture map.
  this->Rectangle = vtkPolyData::New();
  this->RectanglePoints = vtkPoints::New();
  // The actual corner points of the rectangle will be computed later.
  this->Rectangle->SetPoints(this->RectanglePoints);
  vtkCellArray* polys = vtkCellArray::New();
  polys->InsertNextCell(4);
  polys->InsertCellPoint(0);
  polys->InsertCellPoint(1);
  polys->InsertCellPoint(2);
  polys->InsertCellPoint(3);
  this->Rectangle->SetPolys(polys);
  polys->Delete();
  vtkFloatArray* tc = vtkFloatArray::New();
  tc->SetNumberOfComponents(2);
  tc->SetNumberOfTuples(4);
  tc->InsertComponent(0,0, 0.0);  tc->InsertComponent(0,1, 0.0);
  tc->InsertComponent(1,0, 0.0);  tc->InsertComponent(1,1, 1.0);
  tc->InsertComponent(2,0, 1.0);  tc->InsertComponent(2,1, 1.0);
66
  tc->InsertComponent(3,0, 1.0);  tc->InsertComponent(3,1, 0.0);
67
  this->Rectangle->GetPointData()->SetTCoords(tc);
68 69
  tc->Delete();

70
  this->ImageData = vtkImageData::New();
71
  this->Texture = NULL;
72
  vtkTexture* texture = vtkTexture::New();
73
  texture->SetInputData(this->ImageData);
74 75 76 77 78
  this->SetTexture(texture);
  texture->Delete();

  vtkPolyDataMapper2D *mapper = vtkPolyDataMapper2D::New();
  this->PDMapper = 0;
79 80
  this->SetMapper(mapper);
  mapper->Delete();
81 82
  // Done already in SetMapper.
  //this->PDMapper->SetInput(this->Rectangle);
83

Sebastien Barre's avatar
Sebastien Barre committed
84
  this->TextProperty = vtkTextProperty::New();
85
  this->ScaledTextProperty = vtkTextProperty::New();
86
  this->Transform = vtkTransform::New();
Sebastien Barre's avatar
Sebastien Barre committed
87

88 89
  this->LastOrigin[0]     = 0;
  this->LastOrigin[1]     = 0;
Sebastien Barre's avatar
Sebastien Barre committed
90

91 92
  this->LastSize[0]       = 0;
  this->LastSize[1]       = 0;
Sebastien Barre's avatar
Sebastien Barre committed
93

94 95
  this->MinimumSize[0]    = 10;
  this->MinimumSize[1]    = 10;
Sebastien Barre's avatar
Sebastien Barre committed
96

97
  this->MaximumLineHeight = 1.0;
98
  this->TextScaleMode     = TEXT_SCALE_MODE_NONE;
99
  this->Orientation       = 0.0;
100
  this->UseBorderAlign    = 0;
101

Ken Martin's avatar
Ken Martin committed
102 103
  this->FontScaleExponent = 1;

104 105 106 107 108
  this->Input = 0;
  this->InputRendered = false;

  this->FormerOrientation = 0.0;

109 110
  this->TextRenderer = vtkTextRenderer::GetInstance();
  if (!this->TextRenderer)
111
    {
112
    vtkErrorMacro(<<"Failed getting the TextRenderer instance!");
113
    }
114
}
Sebastien Barre's avatar
Sebastien Barre committed
115

116 117 118
// ----------------------------------------------------------------------------
vtkTextActor::~vtkTextActor()
{
119 120
  this->ImageData->Delete();
  this->Transform->Delete();
Sebastien Barre's avatar
Sebastien Barre committed
121
  this->SetTextProperty(NULL);
122 123
  this->ScaledTextProperty->Delete();
  this->ScaledTextProperty = NULL;
Zack Galbreath's avatar
Zack Galbreath committed
124 125 126 127
  if(this->Input)
    {
    delete [] this->Input;
    }
128 129 130 131 132
  this->Rectangle->Delete();
  this->Rectangle = 0;
  this->RectanglePoints->Delete();
  this->RectanglePoints = 0;
  this->SetTexture(0);
133
}
Sebastien Barre's avatar
Sebastien Barre committed
134

135
// ----------------------------------------------------------------------------
136 137
void vtkTextActor::GetBoundingBox(
  vtkViewport* vport, double bbox[4])
138
{
139 140
  if (this->UpdateRectangle(vport) && this->RectanglePoints &&
    this->RectanglePoints->GetNumberOfPoints() >= 4)
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
    {
    double x[3];
    this->RectanglePoints->GetPoint( 0, x );
    bbox[0] = bbox[1] = x[0];
    bbox[2] = bbox[3] = x[1];
    for (int i = 1; i < this->RectanglePoints->GetNumberOfPoints(); ++i)
      {
      this->RectanglePoints->GetPoint( i, x );

      if ( bbox[0] > x[0] )
        bbox[0] = x[0];
      else if ( bbox[1] < x[0] )
        bbox[1] = x[0];

      if ( bbox[2] > x[1] )
        bbox[2] = x[1];
      else if ( bbox[3] < x[1] )
        bbox[3] = x[1];
      }
    }
  else
    {
    vtkErrorMacro("UpdateRectangle failed to do so");
    }
}

167 168 169 170 171 172 173 174
//----------------------------------------------------------------------------
void vtkTextActor::GetSize(vtkViewport* vport, double size[2])
{
  double bds[4];
  // If we have a viewport, use it. Otherwise, GetBoundingBox() calls
  // UpdateRectange(NULL) which builds a (probably-too-low-resolution) image
  // to determine its size.
  this->UpdateRectangle(vport);
175
  this->GetBoundingBox(vport, bds);
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
  size[0] = bds[1] - bds[0];
  size[1] = bds[3] - bds[2];
}

//----------------------------------------------------------------------------
int vtkTextActor::SetConstrainedFontSize(vtkViewport* viewport,
                                          int targetWidth,
                                          int targetHeight)
{
  return this->SetConstrainedFontSize(this, viewport, targetWidth, targetHeight);
}


//----------------------------------------------------------------------------
int vtkTextActor::SetConstrainedFontSize(
  vtkTextActor* tactor, vtkViewport* viewport,
  int targetWidth, int targetHeight)
{
  // If target "empty" just return
  if (targetWidth == 0 && targetHeight == 0)
    {
    return 0;
    }

  vtkTextProperty* tprop = tactor->GetTextProperty();
  if (!tprop)
    {
    vtkGenericWarningMacro(<<"Need text property to apply constraint");
    return 0;
    }
  int fontSize = tprop->GetFontSize();

  // Use the last size as a first guess
  double tempi[2];
  tactor->GetSize(viewport, tempi);

  // Now get an estimate of the target font size using bissection
  // Based on experimentation with big and small font size increments,
  // ceil() gives the best result.
  // big:   floor: 10749, ceil: 10106, cast: 10749, vtkMath::Round: 10311
  // small: floor: 12122, ceil: 11770, cast: 12122, vtkMath::Round: 11768
  // I guess the best optim would be to have a look at the shape of the
  // font size growth curve (probably not that linear)
  if (tempi[0] > 0.5 && tempi[1] > 0.5)
    {
    float fx = targetWidth / static_cast<float>(tempi[0]);
    float fy = targetHeight / static_cast<float>(tempi[1]);
    fontSize = static_cast<int>(ceil(fontSize * ((fx <= fy) ? fx : fy)));
    fontSize = fontSize > 2 ? fontSize : 2;
    tprop->SetFontSize(fontSize);
    tactor->GetSize(viewport, tempi);
    }

  // While the size is too small increase it
  while (tempi[1] <= targetHeight &&
         tempi[0] <= targetWidth &&
         fontSize < 100)
    {
    fontSize++;
    tprop->SetFontSize(fontSize);
    tactor->GetSize(viewport, tempi);
    }

  // While the size is too large decrease it..  but never below 2 pt.
240
  // (The MathText rendering uses matplotlib which behaves poorly
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
  // for very small fonts.)
  while ((tempi[1] > targetHeight || tempi[0] > targetWidth)
         && fontSize > 3)
    {
    fontSize--;
    tprop->SetFontSize(fontSize);
    tactor->GetSize(viewport, tempi);
    }

  return fontSize;
}

//----------------------------------------------------------------------------
int vtkTextActor::SetMultipleConstrainedFontSize(
  vtkViewport* viewport, int targetWidth, int targetHeight,
  vtkTextActor** actors, int nbOfActors, int* maxResultingSize)
{
  maxResultingSize[0] = maxResultingSize[1] = 0;

  if (nbOfActors == 0)
    {
    return 0;
    }

  int fontSize, aSize;

  // First try to find the constrained font size of the first mapper: it
  // will be used minimize the search for the remaining actors, given the
  // fact that all actors are likely to have the same constrained font size.
  int i, first;
  for (first = 0; first < nbOfActors && !actors[first]; first++) {}

  if (first >= nbOfActors)
    {
    return 0;
    }

  fontSize = actors[first]->SetConstrainedFontSize(
    viewport, targetWidth, targetHeight);

  // Find the constrained font size for the remaining actors and
  // pick the smallest
  for (i = first + 1; i < nbOfActors; i++)
    {
    if (actors[i])
      {
      actors[i]->GetTextProperty()->SetFontSize(fontSize);
      aSize = actors[i]->SetConstrainedFontSize(
        viewport, targetWidth, targetHeight);
      if (aSize < fontSize)
        {
        fontSize = aSize;
        }
      }
    }

  // Assign the smallest size to all text actors and find the largest area
  double tempi[2];
  for (i = first; i < nbOfActors; i++)
    {
    if (actors[i])
      {
      actors[i]->GetTextProperty()->SetFontSize(fontSize);
      actors[i]->GetSize(viewport, tempi);
      if (tempi[0] > maxResultingSize[0])
        {
        maxResultingSize[0] = tempi[0];
        }
      if (tempi[1] > maxResultingSize[1])
        {
        maxResultingSize[1] = tempi[1];
        }
      }
    }

  // The above code could be optimized further since the actor
  // labels are likely to have the same height: in that case, we could
  // have searched for the largest label, found the constrained size
  // for this one, then applied this size to all others.  But who
  // knows, maybe one day the text property will support a text
  // orientation/rotation, and in that case the height will vary.
  return fontSize;
}


Ken Martin's avatar
Ken Martin committed
326 327 328
// ----------------------------------------------------------------------------
void vtkTextActor::SetNonLinearFontScale(double exp, int tgt)
{
329 330
  if (   (this->FontScaleExponent == exp)
      && (this->TextProperty->GetFontSize() == tgt) )
Ken Martin's avatar
Ken Martin committed
331 332 333 334
    {
    return;
    }
  this->FontScaleExponent = exp;
335
  this->TextProperty->SetFontSize(tgt);
Ken Martin's avatar
Ken Martin committed
336 337 338
  this->Modified();
}

339
// ----------------------------------------------------------------------------
340
void vtkTextActor::SetMapper(vtkPolyDataMapper2D *mapper)
Sebastien Barre's avatar
Sebastien Barre committed
341
{
342 343
  // I will not reference count this because the superclass does.
  this->PDMapper = mapper; // So what is the point of have the ivar PDMapper?
344
  this->Superclass::SetMapper( mapper );
345 346 347

  if (mapper)
    {
348
    mapper->SetInputData(this->Rectangle);
349
    }
350
}
Sebastien Barre's avatar
Sebastien Barre committed
351

352 353 354
// ----------------------------------------------------------------------------
void vtkTextActor::SetMapper(vtkMapper2D *mapper)
{
355
  if (mapper && mapper->IsA("vtkPolyDataMapper2D"))
356
    {
357
    this->SetMapper( static_cast<vtkPolyDataMapper2D *>(mapper) );
358 359 360
    }
  else
    {
361
    vtkErrorMacro(<<"Must use a vtkPolyDataMapper2D with this class");
362 363 364 365 366 367
  }
}

// ----------------------------------------------------------------------------
bool vtkTextActor::RenderImage(vtkTextProperty *tprop, vtkViewport *)
{
368 369 370 371 372 373
  vtkStdString text;
  if (this->Input && this->Input[0])
    {
    text = this->Input;
    }
  return this->TextRenderer->RenderString(tprop, text, this->ImageData);
374 375 376
}

// ----------------------------------------------------------------------------
377
bool vtkTextActor::GetImageBoundingBox(vtkTextProperty *tprop, vtkViewport *,
378 379
                                  int bbox[4])
{
380 381 382 383 384 385
  vtkStdString text;
  if (this->Input && this->Input[0])
    {
    text = this->Input;
    }
  return this->TextRenderer->GetBoundingBox(tprop, text, bbox);
386
}
Sebastien Barre's avatar
Sebastien Barre committed
387 388

// ----------------------------------------------------------------------------
389
void vtkTextActor::SetInput(const char* str)
Sebastien Barre's avatar
Sebastien Barre committed
390
{
391 392 393 394 395 396
  if(!str)
    {
      vtkErrorMacro(
        <<"vtkTextActor::SetInput was passed an uninitialized string");
    return;
    }
397 398 399 400 401 402 403
  if(this->Input)
    {
    if(strcmp(this->Input, str) == 0)
      {
      return;
      }
    delete [] this->Input;
Sebastien Barre's avatar
Sebastien Barre committed
404
    }
405 406 407
  this->Input = new char[strlen(str)+1];
  strcpy(this->Input, str);
  this->InputRendered = false;
408
  this->Modified();
Sebastien Barre's avatar
Sebastien Barre committed
409 410 411 412 413
}

// ----------------------------------------------------------------------------
char* vtkTextActor::GetInput()
{
414 415 416 417 418 419 420 421 422 423 424 425 426
  return this->Input;
}

// ----------------------------------------------------------------------------
void vtkTextActor::SetTextProperty(vtkTextProperty *p)
{
  if ( this->TextProperty == p )
    {
    return;
    }
  if ( this->TextProperty )
    {
    this->TextProperty->UnRegister( this );
427
    this->TextProperty = NULL;
428 429 430 431 432
    }
  this->TextProperty = p;
  if (this->TextProperty)
    {
    this->TextProperty->Register(this);
433
    this->ScaledTextProperty->ShallowCopy(this->TextProperty);
Sebastien Barre's avatar
Sebastien Barre committed
434
    }
435
  this->Modified();
Sebastien Barre's avatar
Sebastien Barre committed
436 437
}

438 439 440 441 442 443 444 445 446
// ----------------------------------------------------------------------------
void vtkTextActor::ShallowCopy(vtkProp *prop)
{
  vtkTextActor *a = vtkTextActor::SafeDownCast(prop);
  if ( a != NULL )
    {
    this->SetPosition2(a->GetPosition2());
    this->SetMinimumSize(a->GetMinimumSize());
    this->SetMaximumLineHeight(a->GetMaximumLineHeight());
447
    this->SetTextScaleMode(a->GetTextScaleMode());
Sebastien Barre's avatar
Sebastien Barre committed
448
    this->SetTextProperty(a->GetTextProperty());
449
    this->SetInput(a->GetInput());
450 451
    }
  // Now do superclass (mapper is handled by it as well).
452 453 454
  this->Superclass::ShallowCopy(prop);
}

455 456 457 458 459 460
// ----------------------------------------------------------------------------
// Release any graphics resources that are being consumed by this actor.
// The parameter window could be used to determine which graphic
// resources to release.
void vtkTextActor::ReleaseGraphicsResources(vtkWindow *win)
{
461
  this->Superclass::ReleaseGraphicsResources(win);
462
  this->Texture->ReleaseGraphicsResources(win);
463
}
Sebastien Barre's avatar
Sebastien Barre committed
464

465 466 467
// ----------------------------------------------------------------------------
int vtkTextActor::RenderOverlay(vtkViewport *viewport)
{
468 469 470 471 472
  if (!this->Visibility)
    {
    return 0;
    }

473
  // render the texture
474
  if (this->Texture && this->Input && this->Input[0] != '\0')
475 476 477 478 479 480 481
    {
    vtkRenderer* ren = vtkRenderer::SafeDownCast(viewport);
    if (ren)
      {
      this->Texture->Render(ren);
      }
    }
482

483
  // Everything is built in RenderOpaqueGeometry, just have to render
484
  return this->Superclass::RenderOverlay(viewport);
485
}
Sebastien Barre's avatar
Sebastien Barre committed
486

487 488 489
// ----------------------------------------------------------------------------
int vtkTextActor::RenderOpaqueGeometry(vtkViewport *viewport)
{
490 491 492 493 494
  if (!this->Visibility)
    {
    return 0;
    }

495
  //Make sure we have a string to render
496
  if(!this->Input || this->Input[0] == '\0')
497 498 499 500
    {
    return 0;
    }

501 502 503 504 505 506 507 508 509 510 511 512
  int *vSize = viewport->GetSize();
  //vSize == (0,0) means that we're not ready to render yet
  if(vSize[0] == 0 && vSize[1] == 0)
    {
    return 0;
    }
  //not sure what vSize == 1 means, but it can cause divide-by-zero errors
  //in some of the coordinate conversion methods used below
  if(vSize[0] == 1 || vSize[1] == 1)
    {
    return 0;
    }
513

514
  if ( ! this->UpdateRectangle(viewport) )
515
    {
516
    return 0;
517
    }
518

519
  // Everything is built, just have to render
520
  return this->Superclass::RenderOpaqueGeometry(viewport);
521
}
Sebastien Barre's avatar
Sebastien Barre committed
522

523 524 525 526 527 528 529
//-----------------------------------------------------------------------------
// Description:
// Does this prop have some translucent polygonal geometry?
int vtkTextActor::HasTranslucentPolygonalGeometry()
{
  return 0;
}
530 531

// ----------------------------------------------------------------------------
532
void vtkTextActor::SetOrientation(float orientation)
533 534 535 536 537 538 539 540 541
{
  if (this->Orientation == orientation)
    {
    return;
    }
  this->Modified();
  this->Orientation = orientation;
}

542

543
// ----------------------------------------------------------------------------
544
int vtkTextActor::GetAlignmentPoint()
545
{
546
  int alignmentCode = 0;
547

548
  if ( ! this->TextProperty)
549
    {
550
    return 0;
551
    }
552
  switch (this->TextProperty->GetJustification())
553
    {
554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
    case VTK_TEXT_LEFT:
      alignmentCode = 0;
      break;
    case VTK_TEXT_CENTERED:
      alignmentCode = 1;
      break;
    case VTK_TEXT_RIGHT:
      alignmentCode = 2;
      break;
    default:
      vtkErrorMacro(<<"Unknown justifaction code.");
    }
   switch (this->TextProperty->GetVerticalJustification())
    {
    case VTK_TEXT_BOTTOM:
      alignmentCode += 0;
      break;
    case VTK_TEXT_CENTERED:
      alignmentCode += 3;
      break;
574
    case VTK_TEXT_TOP:
575 576 577 578 579 580
      alignmentCode += 6;
      break;
    default:
      vtkErrorMacro(<<"Unknown justifaction code.");
    }
  return alignmentCode;
581 582
}

583
// ----------------------------------------------------------------------------
584
void vtkTextActor::SetAlignmentPoint(int val)
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626
{
  vtkWarningMacro(<< "Alignment point is being depricated.  You should use "
                  << "SetJustification and SetVerticalJustification in the text property.");

  switch (val)
    {
    case 0:
      this->TextProperty->SetJustificationToLeft();
      this->TextProperty->SetVerticalJustificationToBottom();
      break;
    case 1:
      this->TextProperty->SetJustificationToCentered();
      this->TextProperty->SetVerticalJustificationToBottom();
      break;
    case 2:
      this->TextProperty->SetJustificationToRight();
      this->TextProperty->SetVerticalJustificationToBottom();
      break;
    case 3:
      this->TextProperty->SetJustificationToLeft();
      this->TextProperty->SetVerticalJustificationToCentered();
      break;
    case 4:
      this->TextProperty->SetJustificationToCentered();
      this->TextProperty->SetVerticalJustificationToCentered();
      break;
    case 5:
      this->TextProperty->SetJustificationToRight();
      this->TextProperty->SetVerticalJustificationToCentered();
      break;
    case 6:
      this->TextProperty->SetJustificationToLeft();
      this->TextProperty->SetVerticalJustificationToTop();
      break;
    case 7:
      this->TextProperty->SetJustificationToCentered();
      this->TextProperty->SetVerticalJustificationToTop();
      break;
    case 8:
      this->TextProperty->SetJustificationToRight();
      this->TextProperty->SetVerticalJustificationToTop();
      break;
627 628
    }
}
629 630 631 632 633 634 635

//-----------------------------------------------------------------------------
float vtkTextActor::GetFontScale(vtkViewport *viewport)
{
  int *viewportSize = viewport->GetSize();

  // Pretend the long dimension is the "width"
636 637
  int viewportWidth
    = (viewportSize[0] > viewportSize[1]) ? viewportSize[0] : viewportSize[1];
638 639

  // Scale based on the assumtion of a 6 inch wide image at 72 DPI.
640
  return static_cast<double>(viewportWidth)/(6*72);
641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679
}

//-----------------------------------------------------------------------------
void vtkTextActor::ComputeScaledFont(vtkViewport *viewport)
{
  if (this->ScaledTextProperty->GetMTime() < this->TextProperty->GetMTime())
    {
    this->ScaledTextProperty->ShallowCopy(this->TextProperty);
    }

  if (this->TextScaleMode == TEXT_SCALE_MODE_NONE)
    {
    if (this->TextProperty)
      {
      this->ScaledTextProperty->SetFontSize(this->TextProperty->GetFontSize());
      }
    return;
    }

  if (this->TextScaleMode == TEXT_SCALE_MODE_VIEWPORT)
    {
    if (   (viewport->GetMTime() > this->BuildTime)
        || (   viewport->GetVTKWindow()
            && (viewport->GetVTKWindow()->GetMTime() > this->BuildTime) )
        || (   this->TextProperty
            && (this->TextProperty->GetMTime() > this->BuildTime) ) )
      {
      double requestedSize
        = static_cast<double>(this->TextProperty->GetFontSize());
      double scale = static_cast<double>(vtkTextActor::GetFontScale(viewport));
      double targetSize = scale*requestedSize;
      // Apply non-linear scaling
      int fsize
        = static_cast<int>(  pow(targetSize, this->FontScaleExponent)
                           * pow(requestedSize, 1.0-this->FontScaleExponent) );
      this->ScaledTextProperty->SetFontSize(fsize);
      }
    return;
    }
680

681 682 683 684 685 686 687 688 689 690 691 692 693
  //Scaled text case.  We need to be sure that our text will fit
  //inside the specified boundaries
  if(this->TextScaleMode == TEXT_SCALE_MODE_PROP)
    {
    int size[2], *point1, *point2;
    point1 = this->PositionCoordinate->GetComputedViewportValue(viewport);
    point2 = this->Position2Coordinate->GetComputedViewportValue(viewport);
    size[0] = point2[0] - point1[0];
    size[1] = point2[1] - point1[1];

    // Check to see whether we have to rebuild everything
    int positionsHaveChanged = 0;
    int orientationHasChanged = 0;
694 695
    //First check the obvious, am I changed (e.g., text changed)
    if ( this->GetMTime() > this->BuildTime )
696
      {
697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718
      positionsHaveChanged = 1; // short circuit the work of checking positions, etc.
      }
    else
      {
      if (   viewport->GetMTime() > this->BuildTime
          || (   viewport->GetVTKWindow()
              && viewport->GetVTKWindow()->GetMTime() > this->BuildTime ) )
        {
        // if the viewport has changed we may - or may not need
        // to rebuild, it depends on if the projected coords change
        if (   (this->LastSize[0]   != size[0])
            || (this->LastSize[1]   != size[1])
            || (this->LastOrigin[0] != point1[0])
            || (this->LastOrigin[1] != point1[1]) )
          {
          positionsHaveChanged = 1;
          }
        }

      // If the orientation has changed then we'll probably need to change our
      // constrained font size as well
      if(this->FormerOrientation != this->Orientation)
719
        {
720 721 722 723
        this->Transform->Identity();
        this->Transform->RotateZ(this->Orientation);
        this->FormerOrientation = this->Orientation;
        orientationHasChanged = 1;
724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739
        }
      }

    // Check to see whether we have to rebuild everything
    if (   positionsHaveChanged || orientationHasChanged
        || (this->Mapper && this->Mapper->GetMTime() > this->BuildTime)
        || (   this->TextProperty
            && (this->TextProperty->GetMTime() > this->BuildTime) ) )
      {
      vtkDebugMacro(<<"Rebuilding text");

      this->LastOrigin[0] = point1[0];
      this->LastOrigin[1] = point1[1];

      //  Lets try to minimize the number of times we change the font size.
      //  If the width of the font box has not changed by more than a pixel
740 741 742 743 744 745 746 747 748
      // (numerical issues) do not recompute font size. Also, if the text
      // string has changed then we have to change font size.
      if ( (this->Mapper && this->Mapper->GetMTime() > this->BuildTime) ||
           (this->Mapper && this->GetMTime() > this->Mapper->GetMTime()) ||
           (this->TextProperty
            && (this->TextProperty->GetMTime() > this->BuildTime) ) ||
           (this->LastSize[0] < size[0]-1) || (this->LastSize[1] < size[1]-1) ||
           (this->LastSize[0] > size[0]+1) || (this->LastSize[1] > size[1]+1) ||
           orientationHasChanged)
749 750 751
        {
        this->LastSize[0] = size[0];
        this->LastSize[1] = size[1];
752

753 754 755 756 757 758 759 760
        // limit by minimum size
        if (this->MinimumSize[0] > size[0])
          {
          size[0] = this->MinimumSize[0];
          }
        if (this->MinimumSize[1] > size[1])
          {
          size[1] = this->MinimumSize[1];
761
          }
762 763
        int max_height = static_cast<int>(this->MaximumLineHeight * size[1]);

764 765
        int fsize = this->TextRenderer->GetConstrainedFontSize(
          this->Input, this->TextProperty, size[0],
766
          (size[1] < max_height ? size[1] : max_height));
767

768 769 770 771 772 773 774 775
        if (fsize == -1)
          {
          vtkWarningMacro(<<"Could not determine constrained font size for "
                          "string:\n\t'" << this->Input << "'\n. Resetting to "
                          "20pt.");
          fsize = 20;
          }

776 777 778 779 780 781 782 783 784 785 786 787 788 789
        // apply non-linear scaling
        fsize = static_cast<int>(
                     pow(static_cast<double>(fsize), this->FontScaleExponent)
                   * pow(static_cast<double>(this->TextProperty->GetFontSize()),
                         1.0 - this->FontScaleExponent));
        // and set the new font size
        this->ScaledTextProperty->SetFontSize(fsize);
        }
      }
    return;
    }

  vtkWarningMacro(<< "Unknown text scaling mode: " << this->TextScaleMode);
}
790

791
// ----------------------------------------------------------------------------
792
void vtkTextActor::ComputeRectangle(vtkViewport *viewport)
793 794 795
{
  int dims[3];
  this->RectanglePoints->Reset();
796
  if ( this->ImageData )
797
    {
798
    int p2dims[3];
799
    this->ImageData->GetDimensions(p2dims);
800
    int text_bbox[4];
801
    if (!this->GetImageBoundingBox(this->ScaledTextProperty, viewport, text_bbox))
802 803 804 805
      {
      vtkErrorMacro("Cannot compute bounding box.")
      return;
      }
806 807
    dims[0] = ( text_bbox[1] - text_bbox[0] + 1 );
    dims[1] = ( text_bbox[3] - text_bbox[2] + 1 );
808

809
    // compute TCoords.
810
    vtkFloatArray* tc = vtkFloatArray::SafeDownCast
811
      ( this->Rectangle->GetPointData()->GetTCoords() );
812 813 814
    float tcXMax, tcYMax;
    // Add a fudge factor to the texture coordinates to prevent the top
    // row of pixels from being truncated on some systems.
815 816
    tcXMax = std::min(1.0f, (dims[0] + 0.001f) / static_cast<float>(p2dims[0]));
    tcYMax = std::min(1.0f, (dims[1] + 0.001f) / static_cast<float>(p2dims[1]));
817
    tc->InsertComponent(0, 0, 0.0);
818 819
    tc->InsertComponent(0, 1, 0.0);

820
    tc->InsertComponent(1, 0, 0.0);
821
    tc->InsertComponent(1, 1, tcYMax);
822

823 824
    tc->InsertComponent(2, 0, tcXMax);
    tc->InsertComponent(2, 1, tcYMax);
825

826
    tc->InsertComponent(3, 0, tcXMax);
827
    tc->InsertComponent(3, 1, 0.0);
828 829 830 831 832
    }
  else
    {
    dims[0] = dims[1] = 0;
    }
833

834

835 836
  // I could do this with a transform, but it is simple enough
  // to rotate the four corners in 2D ...
837
  double radians = vtkMath::RadiansFromDegrees( this->Orientation );
838
  double c = cos( radians );
839
  double s = sin( radians );
840
  double xo = 0.0, yo = 0.0;
841
  double x, y;
842 843
  // When TextScaleMode is PROP, we justify text based on the rectangle
  // formed by Position & Position2 coordinates
844
  if( ( this->TextScaleMode == TEXT_SCALE_MODE_PROP ) || this->UseBorderAlign )
845
    {
846
    double position1[3], position2[3];
847 848 849 850 851 852
    this->PositionCoordinate->GetValue( position1 );
    this->Position2Coordinate->GetValue( position2 );
    this->SpecifiedToDisplay( position1, viewport,
                              this->PositionCoordinate->GetCoordinateSystem() );
    this->SpecifiedToDisplay( position2, viewport,
                              this->Position2Coordinate->GetCoordinateSystem() );
853 854
    double maxWidth = position2[0] - position1[0];
    double maxHeight = position2[1] - position1[1];
855
    // I could get rid of "GetAlignmentPoint" and use justification directly.
856
    switch( this->GetAlignmentPoint() )
857 858 859 860
      {
      case 0:
        break;
      case 1:
861
        xo = ( maxWidth - dims[0] ) * 0.5;
862 863
        break;
      case 2:
864
        xo = ( maxWidth - dims[0] );
865 866
        break;
      case 3:
867
        yo = ( maxHeight - dims[1] ) * 0.5;
868 869
        break;
      case 4:
870 871
        xo = ( maxWidth - dims[0] ) * 0.5;
        yo = ( maxHeight - dims[1] ) * 0.5;
872 873
        break;
      case 5:
874 875
        xo = ( maxWidth - dims[0] );
        yo = ( maxHeight - dims[1] ) * 0.5;
876 877
        break;
      case 6:
878
        yo = ( maxHeight - dims[1] );
879 880
        break;
      case 7:
881 882
        xo = ( maxWidth - dims[0] ) * 0.5;
        yo = ( maxHeight - dims[1] );
883 884
        break;
      case 8:
885 886
        xo = ( maxWidth - dims[0] );
        yo = ( maxHeight - dims[1] );
887 888
        break;
      default:
889
        vtkErrorMacro( << "Bad alignment point value." );
890 891 892 893
      }
    //handle line offset.  make sure we stay within the bounds defined by
    //position1 & position2
    double offset = this->TextProperty->GetLineOffset();
894
    if( ( yo + offset + dims[1] ) > maxHeight )
895 896 897
      {
      yo = maxHeight - dims[1];
      }
898
    else if( ( yo + offset ) < 0 )
899 900
      {
      yo = 0;
901
      }
902 903 904 905
    else
      {
      yo += offset;
      }
906
    }
907 908 909
  else
    {
    // I could get rid of "GetAlignmentPoint" and use justification directly.
910
    switch ( this->GetAlignmentPoint() )
911 912 913 914
      {
      case 0:
        break;
      case 1:
915
        xo = -dims[0] * 0.5;
916 917
        break;
      case 2:
918
        xo = -dims[0];
919 920
        break;
      case 3:
921
        yo = -dims[1] * 0.5;
922 923
        break;
      case 4:
924 925
        yo = -dims[1] * 0.5;
        xo = -dims[0] * 0.5;
926 927
        break;
      case 5:
928 929
        yo = -dims[1] * 0.5;
        xo = -dims[0];
930 931
        break;
      case 6:
932
        yo = -dims[1];
933 934
        break;
      case 7:
935 936
        yo = -dims[1];
        xo = -dims[0] * 0.5;
937 938
        break;
      case 8:
939 940
        yo = -dims[1];
        xo = -dims[0];
941 942
        break;
      default:
943
        vtkErrorMacro( << "Bad alignment point value." );
944 945 946
      }
    // handle line offset
    yo += this->TextProperty->GetLineOffset();
947
    } //end unscaled text case
948

949
  x = xo;
950
  y = yo;
951
  this->RectanglePoints->InsertNextPoint( c*x-s*y,s*x+c*y,0.0 );
952 953
  x = xo;
  y = yo + dims[1];
954
  this->RectanglePoints->InsertNextPoint( c*x-s*y,s*x+c*y,0.0 );
955 956
  x = xo + dims[0];
  y = yo + dims[1];
957
  this->RectanglePoints->InsertNextPoint( c*x-s*y,s*x+c*y,0.0 );
958 959
  x = xo + dims[0];
  y = yo;
960
  this->RectanglePoints->InsertNextPoint( c*x-s*y,s*x+c*y,0.0 );
961 962
}

963 964 965
// ----------------------------------------------------------------------------
int vtkTextActor::UpdateRectangle(vtkViewport* viewport)
{
966 967 968 969 970
  if (this->TextProperty->GetMTime() > this->ScaledTextProperty->GetMTime())
    {
    this->ComputeScaledFont(viewport);
    }

971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993
  //check if we need to render the string
  if(this->ScaledTextProperty->GetMTime() > this->BuildTime ||
    !this->InputRendered || this->GetMTime() > this->BuildTime)
    {
    if(!this->RenderImage(this->ScaledTextProperty, viewport))
      {
      vtkErrorMacro(<<"Failed rendering text to buffer");
      return 0;
      }

    // Check if we need to create a new rectangle.
    // Need to check if angle has changed.
    //justification and line offset are handled in ComputeRectangle
    this->ComputeRectangle(viewport);

    this->ImageData->Modified();
    this->Texture->SetInputData(this->ImageData);
    this->Texture->Modified();
    this->InputRendered = true;
    this->BuildTime.Modified();
    }
  return 1;
}
994

995 996
// ----------------------------------------------------------------------------
void vtkTextActor::SpecifiedToDisplay(double *pos, vtkViewport *vport,
997
                                      int specified)
998
{
999 1000 1001 1002 1003
  if ( ! vport )
    {
    return;
    }

1004
  switch(specified)
1005
  {
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056
  case VTK_WORLD:
    vport->WorldToView(pos[0], pos[1], pos[2]);
  case VTK_VIEW:
    vport->ViewToNormalizedViewport(pos[0], pos[1], pos[2]);
  case VTK_NORMALIZED_VIEWPORT:
    vport->NormalizedViewportToViewport(pos[0], pos[1]);
  case VTK_VIEWPORT:
    vport->ViewportToNormalizedDisplay(pos[0], pos[1]);
  case VTK_NORMALIZED_DISPLAY:
    vport->NormalizedDisplayToDisplay(pos[0], pos[1]);
  case VTK_DISPLAY:
    break;
  }
}

// ----------------------------------------------------------------------------
void vtkTextActor::DisplayToSpecified(double *pos, vtkViewport *vport,
                                      int specified)
{
  switch(specified)
    {
    case VTK_WORLD:
      vport->DisplayToNormalizedDisplay(pos[0], pos[1]);
      vport->NormalizedDisplayToViewport(pos[0], pos[1]);
      vport->ViewportToNormalizedViewport(pos[0], pos[1]);
      vport->NormalizedViewportToView(pos[0], pos[1], pos[2]);
      vport->ViewToWorld(pos[0], pos[1], pos[2]);
      break;
    case VTK_VIEW:
      vport->DisplayToNormalizedDisplay(pos[0], pos[1]);
      vport->NormalizedDisplayToViewport(pos[0], pos[1]);
      vport->ViewportToNormalizedViewport(pos[0], pos[1]);
      vport->NormalizedViewportToView(pos[0], pos[1], pos[2]);
      break;
    case VTK_NORMALIZED_VIEWPORT:
      vport->DisplayToNormalizedDisplay(pos[0], pos[1]);
      vport->NormalizedDisplayToViewport(pos[0], pos[1]);
      vport->ViewportToNormalizedViewport(pos[0], pos[1]);
      break;
    case VTK_VIEWPORT:
      vport->DisplayToNormalizedDisplay(pos[0], pos[1]);
      vport->NormalizedDisplayToViewport(pos[0], pos[1]);
      break;
    case VTK_NORMALIZED_DISPLAY:
      vport->DisplayToNormalizedDisplay(pos[0], pos[1]);
      break;
    case VTK_DISPLAY:
      break