1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 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 167 168 169 170 171 172 173 174 175 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 240 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 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 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 627 628 629 630 631 632 633 634 635 636 637 638 639 640 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 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkTextActor.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkTextActor.h"
#include "vtkObjectFactory.h"
#include "vtkPolyDataMapper2D.h"
#include "vtkTextProperty.h"
#include "vtkViewport.h"
#include "vtkWindow.h"
#include "vtkTransform.h"
#include "vtkImageData.h"
#include "vtkPoints.h"
#include "vtkPointData.h"
#include "vtkPolyData.h"
#include "vtkCellArray.h"
#include "vtkFloatArray.h"
#include "vtkTexture.h"
#include "vtkMath.h"
#include "vtkTextRenderer.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include <algorithm>
vtkStandardNewMacro(vtkTextActor);
// ----------------------------------------------------------------------------
vtkTextActor::vtkTextActor()
{
// To remain compatible with code using vtkActor2D, we must set
// position coord to Viewport, not Normalized Viewport
// so...compute equivalent coords for initial position
this->PositionCoordinate->SetCoordinateSystemToViewport();
// 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);
tc->InsertComponent(3,0, 1.0); tc->InsertComponent(3,1, 0.0);
this->Rectangle->GetPointData()->SetTCoords(tc);
tc->Delete();
this->ImageData = vtkImageData::New();
vtkTexture* texture = vtkTexture::New();
texture->SetInputData(this->ImageData);
this->SetTexture(texture);
texture->Delete();
vtkPolyDataMapper2D *mapper = vtkPolyDataMapper2D::New();
this->SetMapper(mapper);
mapper->SetInputData(this->Rectangle);
mapper->Delete();
this->TextProperty = vtkTextProperty::New();
this->ScaledTextProperty = vtkTextProperty::New();
this->Transform = vtkTransform::New();
this->LastOrigin[0] = 0;
this->LastOrigin[1] = 0;
this->LastSize[0] = 0;
this->LastSize[1] = 0;
this->MinimumSize[0] = 10;
this->MinimumSize[1] = 10;
this->MaximumLineHeight = 1.0;
this->TextScaleMode = TEXT_SCALE_MODE_NONE;
this->Orientation = 0.0;
this->UseBorderAlign = 0;
this->FontScaleExponent = 1;
this->Input = 0;
this->InputRendered = false;
this->FormerOrientation = 0.0;
this->RenderedDPI = 0;
this->TextRenderer = vtkTextRenderer::GetInstance();
if (!this->TextRenderer)
{
vtkErrorMacro(<<"Failed getting the TextRenderer instance!");
}
}
// ----------------------------------------------------------------------------
vtkTextActor::~vtkTextActor()
{
this->ImageData->Delete();
this->Transform->Delete();
this->SetTextProperty(NULL);
this->ScaledTextProperty->Delete();
this->ScaledTextProperty = NULL;
delete [] this->Input;
this->Rectangle->Delete();
this->Rectangle = 0;
this->RectanglePoints->Delete();
this->RectanglePoints = 0;
this->SetTexture(0);
}
// ----------------------------------------------------------------------------
void vtkTextActor::GetBoundingBox(
vtkViewport* vport, double bbox[4])
{
if (this->UpdateRectangle(vport) && this->RectanglePoints &&
this->RectanglePoints->GetNumberOfPoints() >= 4)
{
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");
}
}
//----------------------------------------------------------------------------
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);
this->GetBoundingBox(vport, bds);
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.
// (The MathText rendering uses matplotlib which behaves poorly
// 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] = static_cast<int>(tempi[0]);
}
if (tempi[1] > maxResultingSize[1])
{
maxResultingSize[1] = static_cast<int>(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;
}
// ----------------------------------------------------------------------------
void vtkTextActor::SetNonLinearFontScale(double exp, int tgt)
{
if ( (this->FontScaleExponent == exp)
&& (this->TextProperty->GetFontSize() == tgt) )
{
return;
}
this->FontScaleExponent = exp;
this->TextProperty->SetFontSize(tgt);
this->Modified();
}
// ----------------------------------------------------------------------------
bool vtkTextActor::RenderImage(vtkTextProperty *tprop, vtkViewport *vp)
{
vtkStdString text;
if (this->Input && this->Input[0])
{
text = this->Input;
}
vtkWindow *win = vp->GetVTKWindow();
if (!win)
{
vtkErrorMacro(<<"No render window available: cannot determine DPI.");
return false;
}
return this->TextRenderer->RenderString(tprop, text, this->ImageData,
NULL, win->GetDPI());
}
// ----------------------------------------------------------------------------
bool vtkTextActor::GetImageBoundingBox(vtkTextProperty *tprop, vtkViewport *vp,
int bbox[4])
{
vtkStdString text;
if (this->Input && this->Input[0])
{
text = this->Input;
}
vtkWindow *win = vp->GetVTKWindow();
if (!win)
{
vtkErrorMacro(<<"No render window available: cannot determine DPI.");
return false;
}
return this->TextRenderer->GetBoundingBox(tprop, text, bbox, win->GetDPI());
}
// ----------------------------------------------------------------------------
void vtkTextActor::SetInput(const char* str)
{
if(!str)
{
str = "";
}
if(this->Input)
{
if(strcmp(this->Input, str) == 0)
{
return;
}
delete [] this->Input;
}
this->Input = new char[strlen(str)+1];
strcpy(this->Input, str);
this->InputRendered = false;
this->Modified();
}
// ----------------------------------------------------------------------------
char* vtkTextActor::GetInput()
{
return this->Input;
}
// ----------------------------------------------------------------------------
void vtkTextActor::SetTextProperty(vtkTextProperty *p)
{
if ( this->TextProperty == p )
{
return;
}
if ( this->TextProperty )
{
this->TextProperty->UnRegister( this );
this->TextProperty = NULL;
}
this->TextProperty = p;
if (this->TextProperty)
{
this->TextProperty->Register(this);
this->ScaledTextProperty->ShallowCopy(this->TextProperty);
}
this->Modified();
}
// ----------------------------------------------------------------------------
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());
this->SetTextScaleMode(a->GetTextScaleMode());
this->SetTextProperty(a->GetTextProperty());
this->SetInput(a->GetInput());
}
// Now do superclass (mapper is handled by it as well).
this->Superclass::ShallowCopy(prop);
}
// ----------------------------------------------------------------------------
// 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)
{
this->Superclass::ReleaseGraphicsResources(win);
}
// ----------------------------------------------------------------------------
int vtkTextActor::RenderOverlay(vtkViewport *viewport)
{
if (!this->Visibility || !this->Input || !this->Input[0])
{
return 0;
}
// Everything is built in RenderOpaqueGeometry, just have to render
return this->Superclass::RenderOverlay(viewport);
}
// ----------------------------------------------------------------------------
int vtkTextActor::RenderOpaqueGeometry(vtkViewport *viewport)
{
if (!this->Visibility)
{
return 0;
}
//Make sure we have a string to render
if(!this->Input || this->Input[0] == '\0')
{
return 0;
}
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;
}
if ( ! this->UpdateRectangle(viewport) )
{
return 0;
}
// Everything is built, just have to render
// but we do not render opaque geometry so return 0
return 0; // this->Superclass::RenderOpaqueGeometry(viewport);
}
//-----------------------------------------------------------------------------
// Description:
// Does this prop have some translucent polygonal geometry?
int vtkTextActor::HasTranslucentPolygonalGeometry()
{
return 0;
}
// ----------------------------------------------------------------------------
void vtkTextActor::SetOrientation(float orientation)
{
if (this->Orientation == orientation)
{
return;
}
this->Modified();
this->Orientation = orientation;
}
// ----------------------------------------------------------------------------
int vtkTextActor::GetAlignmentPoint()
{
int alignmentCode = 0;
if ( ! this->TextProperty)
{
return 0;
}
switch (this->TextProperty->GetJustification())
{
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;
case VTK_TEXT_TOP:
alignmentCode += 6;
break;
default:
vtkErrorMacro(<<"Unknown justifaction code.");
}
return alignmentCode;
}
// ----------------------------------------------------------------------------
void vtkTextActor::SetAlignmentPoint(int val)
{
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;
}
}
//-----------------------------------------------------------------------------
float vtkTextActor::GetFontScale(vtkViewport *viewport)
{
int *viewportSize = viewport->GetSize();
// Pretend the long dimension is the "width"
int viewportWidth
= (viewportSize[0] > viewportSize[1]) ? viewportSize[0] : viewportSize[1];
// Scale based on the assumtion of a 6 inch wide image at 72 DPI.
return static_cast<double>(viewportWidth)/(6*72);
}
//-----------------------------------------------------------------------------
void vtkTextActor::ComputeScaledFont(vtkViewport *viewport)
{
if (this->ScaledTextProperty->GetMTime() < this->TextProperty->GetMTime())
{
this->ScaledTextProperty->ShallowCopy(this->TextProperty);
}
// Combine this actor's orientation with the set text property's rotation
double rotAngle = this->TextProperty->GetOrientation() + this->Orientation;
this->ScaledTextProperty->SetOrientation(rotAngle);
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;
}
//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;
//First check the obvious, am I changed (e.g., text changed)
if ( this->GetMTime() > this->BuildTime )
{
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 != rotAngle)
{
this->FormerOrientation = rotAngle;
orientationHasChanged = 1;
}
}
// 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
// (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)
{
this->LastSize[0] = size[0];
this->LastSize[1] = size[1];
// 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];
}
int max_height = static_cast<int>(this->MaximumLineHeight * size[1]);
vtkWindow *win = viewport->GetVTKWindow();
if (!win)
{
vtkErrorMacro(<<"No render window available: cannot determine DPI.");
return;
}
int fsize = this->TextRenderer->GetConstrainedFontSize(
this->Input, this->ScaledTextProperty, size[0],
(size[1] < max_height ? size[1] : max_height), win->GetDPI());
if (fsize == -1)
{
vtkWarningMacro(<<"Could not determine constrained font size for "
"string:\n\t'" << this->Input << "'\n. Resetting to "
"20pt.");
fsize = 20;
}
// 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);
}
// ----------------------------------------------------------------------------
void vtkTextActor::ComputeRectangle(vtkViewport *viewport)
{
int dims[2] = {0, 0};
int anchorOffset[2] = {0, 0};
this->RectanglePoints->Reset();
if ( this->ImageData )
{
int p2dims[3];
this->ImageData->GetDimensions(p2dims);
int text_bbox[4];
if (!this->GetImageBoundingBox(this->ScaledTextProperty, viewport, text_bbox))
{
vtkErrorMacro("Cannot compute bounding box.")
return;
}
dims[0] = ( text_bbox[1] - text_bbox[0] + 1 );
dims[1] = ( text_bbox[3] - text_bbox[2] + 1 );
anchorOffset[0] = text_bbox[0];
anchorOffset[1] = text_bbox[2];
// compute TCoords.
vtkFloatArray* tc = vtkFloatArray::SafeDownCast
( this->Rectangle->GetPointData()->GetTCoords() );
float tcXMax, tcYMax;
// Add a fudge factor to the texture coordinates to prevent the top
// row of pixels from being truncated on some systems.
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]));
tc->InsertComponent(0, 0, 0.0);
tc->InsertComponent(0, 1, 0.0);
tc->InsertComponent(1, 0, 0.0);
tc->InsertComponent(1, 1, tcYMax);
tc->InsertComponent(2, 0, tcXMax);
tc->InsertComponent(2, 1, tcYMax);
tc->InsertComponent(3, 0, tcXMax);
tc->InsertComponent(3, 1, 0.0);
}
double xo = 0.0, yo = 0.0;
// When TextScaleMode is PROP, we justify text based on the rectangle
// formed by Position & Position2 coordinates
if( ( this->TextScaleMode == TEXT_SCALE_MODE_PROP ) || this->UseBorderAlign )
{
double position1[3], position2[3];
this->PositionCoordinate->GetValue( position1 );
this->Position2Coordinate->GetValue( position2 );
this->SpecifiedToDisplay( position1, viewport,
this->PositionCoordinate->GetCoordinateSystem() );
this->SpecifiedToDisplay( position2, viewport,
this->Position2Coordinate->GetCoordinateSystem() );
double maxWidth = position2[0] - position1[0];
double maxHeight = position2[1] - position1[1];
// I could get rid of "GetAlignmentPoint" and use justification directly.
switch( this->GetAlignmentPoint() )
{
case 0:
break;
case 1:
xo = ( maxWidth - dims[0] ) * 0.5;
break;
case 2:
xo = ( maxWidth - dims[0] );
break;
case 3:
yo = ( maxHeight - dims[1] ) * 0.5;
break;
case 4:
xo = ( maxWidth - dims[0] ) * 0.5;
yo = ( maxHeight - dims[1] ) * 0.5;
break;
case 5:
xo = ( maxWidth - dims[0] );
yo = ( maxHeight - dims[1] ) * 0.5;
break;
case 6:
yo = ( maxHeight - dims[1] );
break;
case 7:
xo = ( maxWidth - dims[0] ) * 0.5;
yo = ( maxHeight - dims[1] );
break;
case 8:
xo = ( maxWidth - dims[0] );
yo = ( maxHeight - dims[1] );
break;
default:
vtkErrorMacro( << "Bad alignment point value." );
}
}
else
{
xo = anchorOffset[0];
yo = anchorOffset[1];
} //end unscaled text case
this->RectanglePoints->InsertNextPoint(xo, yo, 0.0 );
this->RectanglePoints->InsertNextPoint(xo, yo + dims[1], 0.0 );
this->RectanglePoints->InsertNextPoint(xo + dims[0], yo + dims[1], 0.0 );
this->RectanglePoints->InsertNextPoint(xo + dims[0], yo, 0.0 );
}
// ----------------------------------------------------------------------------
int vtkTextActor::UpdateRectangle(vtkViewport* viewport)
{
if (this->TextProperty->GetMTime() > this->ScaledTextProperty->GetMTime())
{
this->ComputeScaledFont(viewport);
}
vtkWindow *win = viewport->GetVTKWindow();
if (!win)
{
vtkErrorMacro(<<"No render window available: cannot determine DPI.");
return 0;
}
//check if we need to render the string
if(this->ScaledTextProperty->GetMTime() > this->BuildTime ||
!this->InputRendered || this->GetMTime() > this->BuildTime ||
this->RenderedDPI != win->GetDPI())
{
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->RenderedDPI = win->GetDPI();
this->BuildTime.Modified();
}
return 1;
}
// ----------------------------------------------------------------------------
void vtkTextActor::SpecifiedToDisplay(double *pos, vtkViewport *vport,
int specified)
{
if ( ! vport )
{
return;
}
switch(specified)
{
case VTK_WORLD:
vport->WorldToView(pos[0], pos[1], pos[2]);
VTK_FALLTHROUGH;
case VTK_VIEW:
vport->ViewToNormalizedViewport(pos[0], pos[1], pos[2]);
VTK_FALLTHROUGH;
case VTK_NORMALIZED_VIEWPORT:
vport->NormalizedViewportToViewport(pos[0], pos[1]);
VTK_FALLTHROUGH;
case VTK_VIEWPORT:
vport->ViewportToNormalizedDisplay(pos[0], pos[1]);
VTK_FALLTHROUGH;
case VTK_NORMALIZED_DISPLAY:
vport->NormalizedDisplayToDisplay(pos[0], pos[1]);
VTK_FALLTHROUGH;
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;
}
}
// ----------------------------------------------------------------------------
void vtkTextActor::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
if (this->Input)
{
os << indent << "Input: " << this->Input << endl;
}
else
{
os << indent << "Input: (none)\n";
}
if (this->TextProperty)
{
os << indent << "Text Property:\n";
this->TextProperty->PrintSelf(os,indent.GetNextIndent());
}
else
{
os << indent << "Text Property: (none)\n";
}
os << indent << "Scaled Text Property:\n";
this->ScaledTextProperty->PrintSelf(os, indent.GetNextIndent());
os << indent << "MaximumLineHeight: " << this->MaximumLineHeight << endl;
os << indent << "MinimumSize: " << this->MinimumSize[0] << " " << this->MinimumSize[1] << endl;
os << indent << "TextScaleMode: " << this->TextScaleMode << endl;
os << indent << "Orientation: " << this->Orientation << endl;
os << indent << "FontScaleExponent: " << this->FontScaleExponent << endl;
os << indent << "UseBorderAlign: " << this->UseBorderAlign << "\n";
}
|