Commit 616d0559 authored by Will Schroeder's avatar Will Schroeder
Browse files

ENH: Centralizing how text sizes are computed to that widgets and other actors act consistently

parent f109da13
......@@ -23,7 +23,7 @@
#include "vtkViewport.h"
#include "vtkWindow.h"
vtkCxxRevisionMacro(vtkAxisActor2D, "1.43");
vtkCxxRevisionMacro(vtkAxisActor2D, "1.44");
vtkStandardNewMacro(vtkAxisActor2D);
vtkCxxSetObjectMacro(vtkAxisActor2D,LabelTextProperty,vtkTextProperty);
......@@ -521,12 +521,12 @@ void vtkAxisActor2D::BuildAxis(vtkViewport *viewport)
this->LabelTextProperty->GetMTime() > this->BuildTime ||
labeltime > this->BuildTime)
{
this->SetMultipleFontSize(viewport,
this->LabelMappers,
this->AdjustedNumberOfLabels,
size,
this->FontFactor * this->LabelFactor,
this->LastMaxLabelSize);
vtkTextMapper::SetMultipleRelativeFontSize(viewport,
this->LabelMappers,
this->AdjustedNumberOfLabels,
size,
this->LastMaxLabelSize,
0.015*this->FontFactor*this->LabelFactor);
}
// Position the mappers
......@@ -569,11 +569,7 @@ void vtkAxisActor2D::BuildAxis(vtkViewport *viewport)
if (viewportSizeHasChanged ||
this->TitleTextProperty->GetMTime() > this->BuildTime)
{
this->SetFontSize(viewport,
this->TitleMapper,
size,
this->FontFactor,
stringSize);
vtkTextMapper::SetRelativeFontSize(this->TitleMapper, viewport, size, stringSize, 0.015*this->FontFactor);
}
else
{
......@@ -604,63 +600,6 @@ void vtkAxisActor2D::BuildAxis(vtkViewport *viewport)
this->BuildTime.Modified();
}
//----------------------------------------------------------------------------
#define VTK_AA2D_FACTOR 0.015
int vtkAxisActor2D::SetFontSize(vtkViewport *viewport,
vtkTextMapper *textMapper,
int *targetSize,
double factor,
int *stringSize)
{
int fontSize, targetWidth, targetHeight;
// Find the best size for the font
// WARNING: check that the above values are in sync with the above
// similar function.
targetWidth = targetSize [0] > targetSize[1] ? targetSize[0] : targetSize[1];
targetHeight = (int)(VTK_AA2D_FACTOR * factor * targetSize[0] +
VTK_AA2D_FACTOR * factor * targetSize[1]);
fontSize = textMapper->SetConstrainedFontSize(viewport,
targetWidth, targetHeight);
textMapper->GetSize(viewport, stringSize);
return fontSize;
}
//----------------------------------------------------------------------------
int vtkAxisActor2D::SetMultipleFontSize(vtkViewport *viewport,
vtkTextMapper **textMappers,
int nbOfMappers,
int *targetSize,
double factor,
int *stringSize)
{
int fontSize, targetWidth, targetHeight;
// Find the best size for the font
// WARNING: check that the below values are in sync with the above
// similar function.
targetWidth = targetSize [0] > targetSize[1] ? targetSize[0] : targetSize[1];
targetHeight = (int)(VTK_AA2D_FACTOR * factor * targetSize[0] +
VTK_AA2D_FACTOR * factor * targetSize[1]);
fontSize =
vtkTextMapper::SetMultipleConstrainedFontSize(viewport,
targetWidth, targetHeight,
textMappers,
nbOfMappers,
stringSize);
return fontSize;
}
#undef VTK_AA2D_FACTOR
//----------------------------------------------------------------------------
void vtkAxisActor2D::UpdateAdjustedRange()
......
......@@ -257,11 +257,6 @@ public:
// instance of vtkTextMapper provided. The factor is used when you're trying
// to create text of different size-factor (it is usually = 1 but you can
// adjust the font size by making factor larger or smaller).
static int SetFontSize(vtkViewport *viewport,
vtkTextMapper *textMapper,
int *targetSize,
double factor,
int *stringSize);
static int SetMultipleFontSize(vtkViewport *viewport,
vtkTextMapper **textMappers,
int nbOfMappers,
......
......@@ -25,7 +25,7 @@
#include "vtkObjectFactory.h"
#include "vtkMath.h"
vtkCxxRevisionMacro(vtkLeaderActor2D, "1.8");
vtkCxxRevisionMacro(vtkLeaderActor2D, "1.9");
vtkStandardNewMacro(vtkLeaderActor2D);
vtkCxxSetObjectMacro(vtkLeaderActor2D,LabelTextProperty,vtkTextProperty);
......@@ -410,11 +410,11 @@ void vtkLeaderActor2D::BuildLeader(vtkViewport *viewport)
//----------------------------------------------------------------------------
#define VTK_LA2D_FACTOR 0.015
int vtkLeaderActor2D::SetFontSize(vtkViewport *viewport, vtkTextMapper *textMapper,
int *targetSize, double factor, int *stringSize)
int *targetSize, double factor, int *stringSize)
{
int fontSize, targetWidth, targetHeight;
targetWidth = targetSize [0] > targetSize[1] ? targetSize[0] : targetSize[1];
targetWidth = targetSize[0] > targetSize[1] ? targetSize[0] : targetSize[1];
targetHeight = (int)(VTK_LA2D_FACTOR * factor * targetSize[0] +
VTK_LA2D_FACTOR * factor * targetSize[1]);
......@@ -423,6 +423,8 @@ int vtkLeaderActor2D::SetFontSize(vtkViewport *viewport, vtkTextMapper *textMapp
return fontSize;
}
#undef VTK_LA2D_FACTOR
//----------------------------------------------------------------------------
int vtkLeaderActor2D::ClipLeader(double center[3], int box[2], double p1[3],
......
......@@ -14,7 +14,6 @@
=========================================================================*/
// .NAME vtkLeaderActor2D - create a leader with optional label and arrows
// .SECTION Description
// vtkLeaderActor2D creates a leader with an optional label and arrows. (A
// leader is typically used to indicate distance between points.)
// vtkLeaderActor2D is a type of vtkActor2D; that is, it is drawn on the
......@@ -27,7 +26,8 @@
//
// Use the vtkLeaderActor2D uses its superclass vtkActor2D instance variables
// Position and Position2 vtkCoordinates to place an instance of
// vtkLeaderActor2D. Using these vtkCoordinates you can specify the position
// vtkLeaderActor2D (i.e., these two data members represent the start and end
// points of the leader). Using these vtkCoordinates you can specify the position
// of the leader in a variety of coordinate systems.
//
// To control the appearance of the actor, use the superclasses
......@@ -91,6 +91,8 @@ public:
vtkGetMacro(LabelFactor, double);
//BTX
// Enums defined to support methods for control of arrow placement and
// and appearance of arrow heads.
enum {VTK_ARROW_NONE=0,VTK_ARROW_POINT1,VTK_ARROW_POINT2,VTK_ARROW_BOTH};
enum {VTK_ARROW_FILLED=0,VTK_ARROW_OPEN,VTK_ARROW_HOLLOW};
//ETX
......@@ -126,7 +128,7 @@ public:
// Description:
// Limit the minimum and maximum size of the arrows. These values are
// expressed in pixels and clamp the minimum/maximum possible size for the
// width/length of the arrrow head. (When clamped, the ratio between length
// width/length of the arrow head. (When clamped, the ratio between length
// and width is preserved.)
vtkSetClampMacro(MinimumArrowSize,double,1.0,VTK_LARGE_FLOAT);
vtkGetMacro(MinimumArrowSize,double);
......@@ -134,7 +136,7 @@ public:
vtkGetMacro(MaximumArrowSize,double);
// Description:
// Enable autolabelling. In this mode, the label is automatically updated
// Enable auto-labelling. In this mode, the label is automatically updated
// based on distance (in world coordinates) between the two end points; or
// if a curved leader is being generated, the angle in degrees between the
// two points.
......@@ -143,7 +145,7 @@ public:
vtkBooleanMacro(AutoLabel,int);
// Description:
// Specify the format to use for autolabelling.
// Specify the format to use for auto-labelling.
vtkSetStringMacro(LabelFormat);
vtkGetStringMacro(LabelFormat);
......
......@@ -26,7 +26,7 @@
#include "vtkViewport.h"
#include "vtkWindow.h"
vtkCxxRevisionMacro(vtkParallelCoordinatesActor, "1.33");
vtkCxxRevisionMacro(vtkParallelCoordinatesActor, "1.34");
vtkStandardNewMacro(vtkParallelCoordinatesActor);
vtkCxxSetObjectMacro(vtkParallelCoordinatesActor,Input,vtkDataObject);
......@@ -265,12 +265,7 @@ int vtkParallelCoordinatesActor::RenderOpaqueGeometry(vtkViewport *viewport)
}
// We could do some caching here, but hey, that's just the title
vtkAxisActor2D::SetFontSize(viewport,
this->TitleMapper,
size,
1.0,
stringSize);
vtkTextMapper::SetRelativeFontSize(this->TitleMapper, viewport, size, stringSize, 0.015);
this->TitleActor->GetPositionCoordinate()->
SetValue((this->Xs[0]+this->Xs[this->N-1])/2.0,this->YMax+stringSize[1]/2.0);
......
......@@ -18,7 +18,7 @@
#include "vtkTextProperty.h"
#include "vtkToolkits.h"
vtkCxxRevisionMacro(vtkTextMapper, "1.54");
vtkCxxRevisionMacro(vtkTextMapper, "1.55");
//----------------------------------------------------------------------------
// Needed when we don't use the vtkStandardNewMacro.
......@@ -29,7 +29,6 @@ vtkCxxSetObjectMacro(vtkTextMapper,TextProperty,vtkTextProperty);
//----------------------------------------------------------------------------
// Creates a new text mapper
vtkTextMapper::vtkTextMapper()
{
this->Input = (char*)NULL;
......@@ -45,7 +44,6 @@ vtkTextMapper::vtkTextMapper()
//----------------------------------------------------------------------------
// Shallow copy of an actor.
void vtkTextMapper::ShallowCopy(vtkTextMapper *tm)
{
this->SetInput(tm->GetInput());
......@@ -119,108 +117,71 @@ int vtkTextMapper::GetHeight(vtkViewport* viewport)
}
//----------------------------------------------------------------------------
#define VTK_TM_DEBUG 0
int vtkTextMapper::SetConstrainedFontSize(vtkViewport *viewport,
int targetWidth,
int targetHeight)
{
// If target "empty"
return this->SetConstrainedFontSize(this, viewport, targetWidth, targetHeight);
}
//----------------------------------------------------------------------------
int vtkTextMapper::SetConstrainedFontSize(vtkTextMapper *tmapper, vtkViewport *viewport,
int targetWidth, int targetHeight)
{
// If target "empty" just return
if (targetWidth == 0 && targetHeight == 0)
{
return 0;
}
// Get text property
vtkTextProperty *tprop = this->GetTextProperty();
vtkTextProperty *tprop = tmapper->GetTextProperty();
if (!tprop)
{
vtkErrorMacro(<<"Need text property to apply constaint");
vtkGenericWarningMacro(<<"Need text property to apply constraint");
return 0;
}
int fontSize = tprop->GetFontSize();
#if VTK_TM_DEBUG
int oldfontSize = fontSize;
#endif
// Use the last size as a first guess
int tempi[2];
this->GetSize(viewport, tempi);
tmapper->GetSize(viewport, tempi);
#if 1
// Now get an estimate of the target font size using bissection
#if VTK_TM_DEBUG
printf("vtkTextMapper::SetConstrainedFontSize: init size: (%d, %d) => (%d, %d)\n", tempi[0], tempi[1], targetWidth, targetHeight);
#endif
// 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] && tempi[1])
{
float fx = (float)targetWidth / (float)tempi[0];
float fy = (float)targetHeight / (float)tempi[1] ;
fontSize = (int)ceil((float)fontSize * ((fx <= fy) ? fx : fy));
tprop->SetFontSize(fontSize);
this->GetSize(viewport, tempi);
tmapper->GetSize(viewport, tempi);
}
#if VTK_TM_DEBUG
printf("vtkTextMapper::SetConstrainedFontSize: estimate size: %2d (was: %2d)\n",
fontSize, oldfontSize);
printf("vtkTextMapper::SetConstrainedFontSize: estimate size: (%d, %d) => (%d, %d)\n", tempi[0], tempi[1], targetWidth, targetHeight);
#endif
#endif
// While the size is too small increase it
while (tempi[1] <= targetHeight &&
tempi[0] <= targetWidth &&
fontSize < 100)
{
fontSize++;
#if VTK_TM_DEBUG
printf("vtkTextMapper::SetConstrainedFontSize: search+ size: %2d\n",
fontSize);
#endif
tprop->SetFontSize(fontSize);
this->GetSize(viewport, tempi);
tmapper->GetSize(viewport, tempi);
}
#if VTK_TM_DEBUG
printf("vtkTextMapper::SetConstrainedFontSize: search+ size: (%d, %d) => (%d, %d)\n", tempi[0], tempi[1], targetWidth, targetHeight);
#endif
// While the size is too large decrease it
while ((tempi[1] > targetHeight || tempi[0] > targetWidth)
&& fontSize > 0)
{
fontSize--;
#if VTK_TM_DEBUG
printf("vtkTextMapper::SetConstrainedFontSize: search- size: %2d\n",
fontSize);
#endif
tprop->SetFontSize(fontSize);
this->GetSize(viewport, tempi);
tmapper->GetSize(viewport, tempi);
}
#if VTK_TM_DEBUG
printf("vtkTextMapper::SetConstrainedFontSize: search- size: (%d, %d) => (%d, %d)\n", tempi[0], tempi[1], targetWidth, targetHeight);
printf("vtkTextMapper::SetConstrainedFontSize: new size: %2d (was: %2d)\n",
fontSize, oldfontSize);
#endif
return fontSize;
}
......@@ -244,7 +205,6 @@ int vtkTextMapper::SetMultipleConstrainedFontSize(vtkViewport *viewport,
// First try to find the constrained font size of the first mapper: it
// will be used minimize the search for the remaining mappers, given the
// fact that all mappers are likely to have the same constrained font size.
int i, first;
for (first = 0; first < nbOfMappers && !mappers[first]; first++) {}
......@@ -256,14 +216,8 @@ int vtkTextMapper::SetMultipleConstrainedFontSize(vtkViewport *viewport,
fontSize = mappers[first]->SetConstrainedFontSize(
viewport, targetWidth, targetHeight);
#if VTK_TM_DEBUG
printf("vtkTextMapper::SetMultipleConstrainedFontSize: first size: %2d\n",
fontSize);
#endif
// Find the constrained font size for the remaining mappers and
// pick the smallest
for (i = first + 1; i < nbOfMappers; i++)
{
if (mappers[i])
......@@ -278,13 +232,7 @@ int vtkTextMapper::SetMultipleConstrainedFontSize(vtkViewport *viewport,
}
}
#if VTK_TM_DEBUG
printf("vtkTextMapper::SetMultipleConstrainedFontSize: smallest size: %2d\n",
fontSize);
#endif
// Assign the smallest size to all text mappers and find the largest area
int tempi[2];
for (i = first; i < nbOfMappers; i++)
{
......@@ -309,6 +257,50 @@ int vtkTextMapper::SetMultipleConstrainedFontSize(vtkViewport *viewport,
// 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;
}
//----------------------------------------------------------------------------
int vtkTextMapper::SetRelativeFontSize(vtkTextMapper *tmapper,
vtkViewport *viewport, int *targetSize,
int *stringSize, float sizeFactor)
{
sizeFactor = (sizeFactor <= 0.0 ? 0.015 : sizeFactor);
int fontSize, targetWidth, targetHeight;
// Find the best size for the font
targetWidth = targetSize[0] > targetSize[1] ? targetSize[0] : targetSize[1];
targetHeight = (int)(sizeFactor * targetSize[0] + sizeFactor * targetSize[1]);
fontSize = tmapper->SetConstrainedFontSize(tmapper, viewport, targetWidth, targetHeight);
tmapper->GetSize(viewport, stringSize);
return fontSize;
}
//----------------------------------------------------------------------------
int vtkTextMapper::SetMultipleRelativeFontSize(vtkViewport *viewport,
vtkTextMapper **textMappers,
int nbOfMappers, int *targetSize,
int *stringSize, float sizeFactor)
{
int fontSize, targetWidth, targetHeight;
// Find the best size for the font
// WARNING: check that the below values are in sync with the above
// similar function.
targetWidth = targetSize [0] > targetSize[1] ? targetSize[0] : targetSize[1];
targetHeight = (int)(sizeFactor * targetSize[0] + sizeFactor * targetSize[1]);
fontSize =
vtkTextMapper::SetMultipleConstrainedFontSize(viewport,
targetWidth, targetHeight,
textMappers,
nbOfMappers,
stringSize);
return fontSize;
}
......@@ -317,7 +309,6 @@ int vtkTextMapper::SetMultipleConstrainedFontSize(vtkViewport *viewport,
//----------------------------------------------------------------------------
// Parse the input and create multiple text mappers if multiple lines
// (delimited by \n) are specified.
void vtkTextMapper::SetInput(const char *input)
{
if ( this->Input && input && (!strcmp(this->Input,input)))
......@@ -385,7 +376,6 @@ void vtkTextMapper::SetInput(const char *input)
//----------------------------------------------------------------------------
// Determine the number of lines in the Input string (delimited by "\n").
int vtkTextMapper::GetNumberOfLines(const char *input)
{
if ( input == NULL || input[0] == '\0')
......@@ -411,7 +401,6 @@ int vtkTextMapper::GetNumberOfLines(const char *input)
//----------------------------------------------------------------------------
// Get the next \n delimited line. Returns a string that
// must be freed by the calling function.
char *vtkTextMapper::NextLine(const char *input, int lineNum)
{
const char *ptr, *ptrEnd;
......@@ -440,7 +429,6 @@ char *vtkTextMapper::NextLine(const char *input, int lineNum)
//----------------------------------------------------------------------------
// Get the size of a multi-line text string
void vtkTextMapper::GetMultiLineSize(vtkViewport* viewport, int size[2])
{
int i;
......
......@@ -14,7 +14,7 @@
=========================================================================*/
// .NAME vtkTextMapper - 2D text annotation
// .SECTION Description
// vtkTextMapper provides 2D text annotation support for vtk. It is a
// vtkTextMapper provides 2D text annotation support for VTK. It is a
// vtkMapper2D that can be associated with a vtkActor2D and placed into a
// vtkRenderer.
//
......@@ -45,7 +45,7 @@ public:
// Description:
// Return the size[2]/width/height of the rectangle required to draw this
// mapper (in pixels).
virtual void GetSize(vtkViewport*, int size[2]) {size[0]=size[0];};
virtual void GetSize(vtkViewport*, int size[2]) {size[0]=size[0];}
virtual int GetWidth(vtkViewport*v);
virtual int GetHeight(vtkViewport*v);
......@@ -60,38 +60,51 @@ public:
virtual void SetTextProperty(vtkTextProperty *p);
vtkGetObjectMacro(TextProperty,vtkTextProperty);
vtkGetMacro(NumberOfLines,int);
// Description:
// Shallow copy of an actor.
void ShallowCopy(vtkTextMapper *tm);
// Description:
// Determine the number of lines in the Input string (delimited by "\n").
// Determine the number of lines in the input string (delimited by "\n").
int GetNumberOfLines(const char *input);
// Description:
// Get the number of lines in the input string (the method GetNumberOfLines(char*)
// must have been previously called for the return value to be valid).
vtkGetMacro(NumberOfLines,int);
// Description:
// Set and return the font size required to make this mapper fit in a given
// target rectangle (width * height, in pixels).
virtual int SetConstrainedFontSize(vtkViewport*,
int targetWidth, int targetHeight);
// target rectangle (width x height, in pixels). A static version of the method
// is also available for convenience to other classes (e.g., widgets).
virtual int SetConstrainedFontSize(vtkViewport*, int targetWidth, int targetHeight);
static int SetConstrainedFontSize(vtkTextMapper*, vtkViewport*, int targetWidth, int targetHeight);
// Description:
// Set and return the font size required to make each element of an array of
// mappers fit in a given rectangle (width * height, in pixels).
// This font size is the smallest size that was required to fit the largest
// Set and return the font size required to make each element of an array
// of mappers fit in a given rectangle (width x height, in pixels). This
// font size is the smallest size that was required to fit the largest
// mapper in this constraint.
// The resulting maximum area of the mappers is also returned.
static int SetMultipleConstrainedFontSize(vtkViewport*,
int targetWidth, int targetHeight,
vtkTextMapper** mappers,
int nbOfMappers,
int* maxResultingSize);
// Description:
// Use these methods when setting font size relative to the renderer's size. These
// methods are static so that external classes (e.g., widgets) can easily use them.
static int SetRelativeFontSize(vtkTextMapper*, vtkViewport*, int *winSize,
int *stringSize, float sizeFactor=0.0);
static int SetMultipleRelativeFontSize(vtkViewport *viewport,
vtkTextMapper **textMappers,
int nbOfMappers, int *winSize,
int *stringSize, float sizeFactor);
// Description:
// Get the available system font size matching a font size.
virtual int GetSystemFontSize(int size)
{ return size; };
{ return size; }
protected:
vtkTextMapper();
......@@ -107,7 +120,6 @@ protected:
vtkTextMapper **TextLines;
// These functions are used to parse, process, and render multiple lines
char *NextLine(const char *input, int lineNum);
void GetMultiLineSize(vtkViewport* viewport, int size[2]);
void RenderOverlayMultipleLines(vtkViewport *viewport, vtkActor2D *actor);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment