Commit 350ff2a5 authored by Dan Lipsa's avatar Dan Lipsa

ENH: Add vtkTextProperty::UseTightBoundingBox to center a label to anchor.

This property allows the user to center the string to the anchor point.
By default, the bounding box for the string is computed
using the font metrics which includes ascents and descents.
As descents might not be present in the string, the label will not
be perfectly centered on the anchor point. Setting this property
on will compute the bounding box using the current string rather than
the font metric. This results in perfectly centered labels.
This does not work well for temporal data if the string changes as
it results in text that moves around.
parent 9a0904d6
......@@ -51,6 +51,7 @@ vtkTextProperty::vtkTextProperty()
this->Justification = VTK_TEXT_LEFT;
this->VerticalJustification = VTK_TEXT_BOTTOM;
this->UseTightBoundingBox = 0;
this->LineOffset = 0.0;
this->LineSpacing = 1.1; // why not 1.0 ?
......
......@@ -159,6 +159,15 @@ public:
{this->SetVerticalJustification(VTK_TEXT_TOP);};
const char *GetVerticalJustificationAsString();
// Description:
// If this property is on, text is alligned to drawn pixels not to font metrix.
// If the text does not include descents, the bounding box will not extend below
// the baseline. This option can be used to get centered labels. It does not
// work well if the string changes as the string position will move around.
vtkSetMacro(UseTightBoundingBox, int);
vtkGetMacro(UseTightBoundingBox, int);
vtkBooleanMacro(UseTightBoundingBox, int);
// Description:
// Set/Get the text's orientation (in degrees).
vtkSetMacro(Orientation,double);
......@@ -199,6 +208,7 @@ protected:
int ShadowOffset[2];
int Justification;
int VerticalJustification;
int UseTightBoundingBox;
double Orientation;
double LineOffset;
double LineSpacing;
......
......@@ -50,7 +50,7 @@ singleLineTextActorC = vtk.vtkActor2D()
singleLineTextActorC.SetMapper(singleLineTextC)
singleLineTextActorC.GetPositionCoordinate().SetCoordinateSystemToNormalizedDisplay()
singleLineTextActorC.GetPositionCoordinate().SetValue(0.05,0.75)
# The text is on a single line and top-justified.
# The text is on a single line and top-justified - above 0.5
singleLineTextT = vtk.vtkTextMapper()
singleLineTextT.SetInput("Single line (top)")
tprop = singleLineTextT.GetTextProperty()
......@@ -61,7 +61,83 @@ singleLineTextActorT = vtk.vtkActor2D()
singleLineTextActorT.SetMapper(singleLineTextT)
singleLineTextActorT.GetPositionCoordinate().SetCoordinateSystemToNormalizedDisplay()
singleLineTextActorT.GetPositionCoordinate().SetValue(0.05,0.65)
# The text is on multiple lines and left- and top-justified.
# The text is on a single line and top-justified -below 0.5
singleLineTextTB = vtk.vtkTextMapper()
singleLineTextTB.SetInput("Single line below (top)")
tprop = singleLineTextTB.GetTextProperty()
tprop.ShallowCopy(singleLineTextProp)
tprop.SetVerticalJustificationToTop()
tprop.SetColor(0,0,1)
singleLineTextActorTB = vtk.vtkActor2D()
singleLineTextActorTB.SetMapper(singleLineTextTB)
singleLineTextActorTB.GetPositionCoordinate().SetCoordinateSystemToNormalizedDisplay()
singleLineTextActorTB.GetPositionCoordinate().SetValue(0.5,0.25)
# The text is on a single line and centered -below 0.5
singleLineTextCC = vtk.vtkTextMapper()
singleLineTextCC.SetInput("HHHHH")
tprop = singleLineTextCC.GetTextProperty()
tprop.ShallowCopy(singleLineTextProp)
tprop.SetJustificationToCentered()
tprop.SetVerticalJustificationToCentered()
tprop.UseTightBoundingBoxOn()
tprop.SetColor(0,0,0)
singleLineTextActorCC = vtk.vtkActor2D()
singleLineTextActorCC.SetMapper(singleLineTextCC)
singleLineTextActorCC.GetPositionCoordinate().SetCoordinateSystemToNormalizedDisplay()
singleLineTextActorCC.GetPositionCoordinate().SetValue(0.5,0.15)
# The text is on a single line and bottom-right justified with a tight bounding-box
singleLineTextBR = vtk.vtkTextMapper()
singleLineTextBR.SetInput("Line bottom")
tprop = singleLineTextBR.GetTextProperty()
tprop.ShallowCopy(singleLineTextProp)
tprop.SetJustificationToRight()
tprop.SetVerticalJustificationToBottom()
tprop.UseTightBoundingBoxOn()
tprop.SetColor(0,0,1)
singleLineTextActorBR = vtk.vtkActor2D()
singleLineTextActorBR.SetMapper(singleLineTextBR)
singleLineTextActorBR.GetPositionCoordinate().SetCoordinateSystemToNormalizedDisplay()
singleLineTextActorBR.GetPositionCoordinate().SetValue(0.95,0.15)
# The text is on a single line and bottom-right justified with a tight bounding-box
singleLineTextBL = vtk.vtkTextMapper()
singleLineTextBL.SetInput("Tight line (bottom)")
tprop = singleLineTextBL.GetTextProperty()
tprop.ShallowCopy(singleLineTextProp)
tprop.SetJustificationToLeft()
tprop.SetVerticalJustificationToBottom()
tprop.UseTightBoundingBoxOn()
tprop.SetColor(0,0,1)
singleLineTextActorBL = vtk.vtkActor2D()
singleLineTextActorBL.SetMapper(singleLineTextBL)
singleLineTextActorBL.GetPositionCoordinate().SetCoordinateSystemToNormalizedDisplay()
singleLineTextActorBL.GetPositionCoordinate().SetValue(0.05,0.15)
# The text is on a single line and top-justified - above 0.5
singleLineTextLTT = vtk.vtkTextMapper()
singleLineTextLTT.SetInput("Single line (top)")
tprop = singleLineTextLTT.GetTextProperty()
tprop.ShallowCopy(singleLineTextProp)
tprop.SetVerticalJustificationToTop()
tprop.UseTightBoundingBoxOn()
tprop.SetColor(0,0,1)
singleLineTextActorLTT = vtk.vtkActor2D()
singleLineTextActorLTT.SetMapper(singleLineTextLTT)
singleLineTextActorLTT.GetPositionCoordinate().SetCoordinateSystemToNormalizedDisplay()
singleLineTextActorLTT.GetPositionCoordinate().SetValue(0.05,0.15)
# The text is on a single line and top-justified - above 0.5
singleLineTextRTT = vtk.vtkTextMapper()
singleLineTextRTT.SetInput("nge ne op")
tprop = singleLineTextRTT.GetTextProperty()
tprop.ShallowCopy(singleLineTextProp)
tprop.SetJustificationToRight()
tprop.SetVerticalJustificationToTop()
tprop.UseTightBoundingBoxOn()
tprop.SetColor(0,0,1)
singleLineTextActorRTT = vtk.vtkActor2D()
singleLineTextActorRTT.SetMapper(singleLineTextRTT)
singleLineTextActorRTT.GetPositionCoordinate().SetCoordinateSystemToNormalizedDisplay()
singleLineTextActorRTT.GetPositionCoordinate().SetValue(0.95,0.15)
# The text is on multiple lines and left- and top-justified - below 0.5
textMapperL = vtk.vtkTextMapper()
textMapperL.SetInput("This is\nmulti-line\ntext output\n(left-top)")
tprop = textMapperL.GetTextProperty()
......@@ -73,6 +149,18 @@ textActorL = vtk.vtkActor2D()
textActorL.SetMapper(textMapperL)
textActorL.GetPositionCoordinate().SetCoordinateSystemToNormalizedDisplay()
textActorL.GetPositionCoordinate().SetValue(0.05,0.5)
# The text is on multiple lines and left- and top-justified - above 0.5
textMapperLA = vtk.vtkTextMapper()
textMapperLA.SetInput("This is\nmulti-line\ntext output\nabove (left-top)")
tprop = textMapperLA.GetTextProperty()
tprop.ShallowCopy(multiLineTextProp)
tprop.SetJustificationToLeft()
tprop.SetVerticalJustificationToTop()
tprop.SetColor(1,0,0)
textActorLA = vtk.vtkActor2D()
textActorLA.SetMapper(textMapperLA)
textActorLA.GetPositionCoordinate().SetCoordinateSystemToNormalizedDisplay()
textActorLA.GetPositionCoordinate().SetValue(0.5,0.85)
# The text is on multiple lines and center-justified (both horizontal and
# vertical).
textMapperC = vtk.vtkTextMapper()
......@@ -110,11 +198,16 @@ Pts.InsertNextPoint(0.95,1.0,0.0)
Pts.InsertNextPoint(0.0,0.5,0.0)
Pts.InsertNextPoint(1.0,0.5,0.0)
Pts.InsertNextPoint(0.00,0.85,0.0)
Pts.InsertNextPoint(0.50,0.85,0.0)
Pts.InsertNextPoint(1.00,0.85,0.0)
Pts.InsertNextPoint(0.00,0.75,0.0)
Pts.InsertNextPoint(0.50,0.75,0.0)
Pts.InsertNextPoint(0.00,0.65,0.0)
Pts.InsertNextPoint(0.50,0.65,0.0)
Pts.InsertNextPoint(0.00,0.25,0.0)
Pts.InsertNextPoint(1.00,0.25,0.0)
Pts.InsertNextPoint(0.00,0.15,0.0)
Pts.InsertNextPoint(1.00,0.15,0.0)
# Set up the lines that use these points.
Lines = vtk.vtkCellArray()
Lines.InsertNextCell(2)
......@@ -138,6 +231,14 @@ Lines.InsertCellPoint(11)
Lines.InsertNextCell(2)
Lines.InsertCellPoint(12)
Lines.InsertCellPoint(13)
Lines.InsertNextCell(2)
Lines.InsertCellPoint(14)
Lines.InsertCellPoint(15)
Lines.InsertNextCell(2)
Lines.InsertCellPoint(16)
Lines.InsertCellPoint(17)
# Create a grid that uses these points and lines.
Grid = vtk.vtkPolyData()
Grid.SetPoints(Pts)
......@@ -165,11 +266,18 @@ iren.SetRenderWindow(renWin)
#
ren1.AddActor2D(gridActor)
ren1.AddActor2D(textActorL)
ren1.AddActor2D(singleLineTextActorT)
ren1.AddActor2D(textActorLA)
ren1.AddActor2D(textActorC)
ren1.AddActor2D(textActorR)
ren1.AddActor2D(singleLineTextActorB)
ren1.AddActor2D(singleLineTextActorC)
ren1.AddActor2D(singleLineTextActorT)
ren1.AddActor2D(singleLineTextActorLTT)
ren1.AddActor2D(singleLineTextActorRTT)
ren1.AddActor2D(singleLineTextActorTB)
ren1.AddActor2D(singleLineTextActorBR)
ren1.AddActor2D(singleLineTextActorBL)
ren1.AddActor2D(singleLineTextActorCC)
ren1.SetBackground(1,1,1)
renWin.SetSize(500,300)
ren1.GetActiveCamera().Zoom(1.5)
......
......@@ -58,7 +58,7 @@ vtkActor2D singleLineTextActorC
SetCoordinateSystemToNormalizedDisplay
[singleLineTextActorC GetPositionCoordinate] SetValue 0.05 0.75
# The text is on a single line and top-justified.
# The text is on a single line and top-justified - above 0.5
vtkTextMapper singleLineTextT
singleLineTextT SetInput "Single line (top)"
set tprop [singleLineTextT GetTextProperty]
......@@ -71,7 +71,89 @@ vtkActor2D singleLineTextActorT
SetCoordinateSystemToNormalizedDisplay
[singleLineTextActorT GetPositionCoordinate] SetValue 0.05 0.65
# The text is on multiple lines and left- and top-justified.
# The text is on a single line and top-justified -below 0.5
vtkTextMapper singleLineTextTB
singleLineTextTB SetInput "Single line below (top)"
set tprop [singleLineTextTB GetTextProperty]
$tprop ShallowCopy singleLineTextProp
$tprop SetVerticalJustificationToTop
$tprop SetColor 0 0 1
vtkActor2D singleLineTextActorTB
singleLineTextActorTB SetMapper singleLineTextTB
[singleLineTextActorTB GetPositionCoordinate] \
SetCoordinateSystemToNormalizedDisplay
[singleLineTextActorTB GetPositionCoordinate] SetValue 0.5 0.25
# The text is on a single line and centered -below 0.5
vtkTextMapper singleLineTextCC
singleLineTextCC SetInput "HHHHH"
set tprop [singleLineTextCC GetTextProperty]
$tprop ShallowCopy singleLineTextProp
$tprop SetJustificationToCentered
$tprop SetVerticalJustificationToCentered
$tprop UseTightBoundingBoxOn
$tprop SetColor 0 0 0
vtkActor2D singleLineTextActorCC
singleLineTextActorCC SetMapper singleLineTextCC
[singleLineTextActorCC GetPositionCoordinate] SetCoordinateSystemToNormalizedDisplay
[singleLineTextActorCC GetPositionCoordinate] SetValue 0.5 0.15
# The text is on a single line and bottom-right justified with a tight bounding-box
vtkTextMapper singleLineTextBR
singleLineTextBR SetInput "Line bottom"
set tprop [singleLineTextBR GetTextProperty]
$tprop ShallowCopy singleLineTextProp
$tprop SetJustificationToRight
$tprop SetVerticalJustificationToBottom
$tprop UseTightBoundingBoxOn
$tprop SetColor 0 0 1
vtkActor2D singleLineTextActorBR
singleLineTextActorBR SetMapper singleLineTextBR
[singleLineTextActorBR GetPositionCoordinate] SetCoordinateSystemToNormalizedDisplay
[singleLineTextActorBR GetPositionCoordinate] SetValue 0.95 0.15
# The text is on a single line and bottom-right justified with a tight bounding-box
vtkTextMapper singleLineTextBL
singleLineTextBL SetInput "Tight line (bottom)"
set tprop [singleLineTextBL GetTextProperty]
$tprop ShallowCopy singleLineTextProp
$tprop SetJustificationToLeft
$tprop SetVerticalJustificationToBottom
$tprop UseTightBoundingBoxOn
$tprop SetColor 0 0 1
vtkActor2D singleLineTextActorBL
singleLineTextActorBL SetMapper singleLineTextBL
[singleLineTextActorBL GetPositionCoordinate] SetCoordinateSystemToNormalizedDisplay
[singleLineTextActorBL GetPositionCoordinate] SetValue 0.05 0.15
# The text is on a single line and top-justified - above 0.5
vtkTextMapper singleLineTextLTT
singleLineTextLTT SetInput "Single line (top)"
set tprop [singleLineTextLTT GetTextProperty]
$tprop ShallowCopy singleLineTextProp
$tprop SetVerticalJustificationToTop
$tprop UseTightBoundingBoxOn
$tprop SetColor 0 0 1
vtkActor2D singleLineTextActorLTT
singleLineTextActorLTT SetMapper singleLineTextLTT
[singleLineTextActorLTT GetPositionCoordinate] SetCoordinateSystemToNormalizedDisplay
[singleLineTextActorLTT GetPositionCoordinate] SetValue 0.05 0.15
# The text is on a single line and top-justified - above 0.5
vtkTextMapper singleLineTextRTT
singleLineTextRTT SetInput "nge ne op"
set tprop [singleLineTextRTT GetTextProperty]
$tprop ShallowCopy singleLineTextProp
$tprop SetJustificationToRight
$tprop SetVerticalJustificationToTop
$tprop UseTightBoundingBoxOn
$tprop SetColor 0 0 1
vtkActor2D singleLineTextActorRTT
singleLineTextActorRTT SetMapper singleLineTextRTT
[singleLineTextActorRTT GetPositionCoordinate] SetCoordinateSystemToNormalizedDisplay
[singleLineTextActorRTT GetPositionCoordinate] SetValue 0.95 0.15
# The text is on multiple lines and left- and top-justified - below 0.5
vtkTextMapper textMapperL
textMapperL SetInput "This is\nmulti-line\ntext output\n(left-top)"
set tprop [textMapperL GetTextProperty]
......@@ -84,6 +166,19 @@ vtkActor2D textActorL
[textActorL GetPositionCoordinate] SetCoordinateSystemToNormalizedDisplay
[textActorL GetPositionCoordinate] SetValue 0.05 0.5
# The text is on multiple lines and left- and top-justified - above 0.5
vtkTextMapper textMapperLA
textMapperLA SetInput "This is\nmulti-line\ntext output\nabove (left-top)"
set tprop [textMapperLA GetTextProperty]
$tprop ShallowCopy multiLineTextProp
$tprop SetJustificationToLeft
$tprop SetVerticalJustificationToTop
$tprop SetColor 1 0 0
vtkActor2D textActorLA
textActorLA SetMapper textMapperLA
[textActorLA GetPositionCoordinate] SetCoordinateSystemToNormalizedDisplay
[textActorLA GetPositionCoordinate] SetValue 0.5 0.85
# The text is on multiple lines and center-justified (both horizontal and
# vertical).
vtkTextMapper textMapperC
......@@ -123,12 +218,17 @@ vtkPoints Pts
Pts InsertNextPoint 0.95 1.0 0.0
Pts InsertNextPoint 0.0 0.5 0.0
Pts InsertNextPoint 1.0 0.5 0.0
Pts InsertNextPoint 0.00 0.85 0.0
Pts InsertNextPoint 0.50 0.85 0.0
Pts InsertNextPoint 0.0 0.85 0.0
Pts InsertNextPoint 1.0 0.85 0.0
Pts InsertNextPoint 0.00 0.75 0.0
Pts InsertNextPoint 0.50 0.75 0.0
Pts InsertNextPoint 0.00 0.65 0.0
Pts InsertNextPoint 0.50 0.65 0.0
Pts InsertNextPoint 0.00 0.25 0.0
Pts InsertNextPoint 1.00 0.25 0.0
Pts InsertNextPoint 0.00 0.15 0.0
Pts InsertNextPoint 1.00 0.15 0.0
# Set up the lines that use these points.
vtkCellArray Lines
Lines InsertNextCell 2
......@@ -152,13 +252,20 @@ vtkCellArray Lines
Lines InsertNextCell 2
Lines InsertCellPoint 12
Lines InsertCellPoint 13
Lines InsertNextCell 2
Lines InsertCellPoint 14
Lines InsertCellPoint 15
Lines InsertNextCell 2
Lines InsertCellPoint 16
Lines InsertCellPoint 17
# Create a grid that uses these points and lines.
vtkPolyData Grid
Grid SetPoints Pts
Grid SetLines Lines
# Set up the coordinate system.
vtkCoordinate normCoords
normCoords SetCoordinateSystemToNormalizedViewport
normCoords SetCoordinateSystemToNormalizedDisplay
# Set up the mapper and actor (2D) for the grid.
vtkPolyDataMapper2D mapper
......@@ -182,11 +289,18 @@ vtkRenderWindowInteractor iren
#
ren1 AddActor2D gridActor
ren1 AddActor2D textActorL
ren1 AddActor2D singleLineTextActorT
ren1 AddActor2D textActorLA
ren1 AddActor2D textActorC
ren1 AddActor2D textActorR
ren1 AddActor2D singleLineTextActorB
ren1 AddActor2D singleLineTextActorC
ren1 AddActor2D singleLineTextActorT
ren1 AddActor2D singleLineTextActorLTT
ren1 AddActor2D singleLineTextActorRTT
ren1 AddActor2D singleLineTextActorTB
ren1 AddActor2D singleLineTextActorBR
ren1 AddActor2D singleLineTextActorBL
ren1 AddActor2D singleLineTextActorCC
ren1 SetBackground 1 1 1
renWin SetSize 500 300
......
......@@ -40,6 +40,8 @@
#include <algorithm>
#include <map>
#include <vector>
#include <sstream>
#include <limits>
// Print debug info
#define VTK_FTFC_DEBUG 0
......@@ -1340,7 +1342,7 @@ bool vtkFreeTypeTools::RenderStringInternal(vtkTextProperty *tprop,
return false;
}
// Draw a yellow dot at the anchor point:
// Draw a red dot at the anchor point:
if (this->DebugTextures)
{
unsigned char *ptr =
......@@ -1348,12 +1350,11 @@ bool vtkFreeTypeTools::RenderStringInternal(vtkTextProperty *tprop,
if (ptr)
{
ptr[0] = 255;
ptr[1] = 255;
ptr[1] = 0;
ptr[2] = 0;
ptr[3] = 255;
}
}
return true;
}
......@@ -1389,10 +1390,27 @@ bool vtkFreeTypeTools::StringToPathInternal(vtkTextProperty *tprop,
return true;
}
namespace
{
const char* DEFAULT_HEIGHT_STRING = "_/7Agfy";
}
//----------------------------------------------------------------------------
bool vtkFreeTypeTools::CalculateBoundingBox(const vtkUnicodeString& str, MetaData &metaData)
{
return CalculateBoundingBox(str, metaData, vtkUnicodeString::from_utf8(DEFAULT_HEIGHT_STRING));
}
//----------------------------------------------------------------------------
bool vtkFreeTypeTools::CalculateBoundingBox(const vtkStdString& str, MetaData &metaData)
{
return CalculateBoundingBox(str, metaData, vtkStdString(DEFAULT_HEIGHT_STRING));
}
//----------------------------------------------------------------------------
template <typename T>
bool vtkFreeTypeTools::CalculateBoundingBox(const T& str,
MetaData &metaData)
MetaData &metaData, const T& defaultHeightString)
{
// Calculate the metrics for each line. These will be used to calculate
// a bounding box, but first we need to know the maximum line length to
......@@ -1424,18 +1442,26 @@ bool vtkFreeTypeTools::CalculateBoundingBox(const T& str,
metaData.lineMetrics.back().width);
// Calculate line height from a reference set of characters, since the global
// face values are usually way too big. This is the same string used to
// determine height in vtkFreeTypeUtilities.
const char *heightString = "_/7Agfy";
metaData.ascent = 0;
metaData.descent = 0;
while (*heightString)
// face values are usually way too big.
T heightString;
if (metaData.textProperty->GetUseTightBoundingBox())
{
heightString = str;
}
else
{
heightString = defaultHeightString;
}
metaData.ascent = std::numeric_limits<int>::min();
metaData.descent = std::numeric_limits<int>::max();
typename T::const_iterator it = heightString.begin();
while (it != heightString.end())
{
FT_BitmapGlyph bitmapGlyph;
FT_UInt glyphIndex;
// Use the unrotated face to get correct metrics:
FT_Bitmap *bitmap = this->GetBitmap(
*heightString, &metaData.unrotatedScaler, glyphIndex, bitmapGlyph);
*it, &metaData.unrotatedScaler, glyphIndex, bitmapGlyph);
if (bitmap)
{
metaData.ascent = std::max(bitmapGlyph->top - 1, metaData.ascent);
......@@ -1443,7 +1469,7 @@ bool vtkFreeTypeTools::CalculateBoundingBox(const T& str,
(bitmapGlyph->top - 1))),
metaData.descent);
}
++heightString;
++it;
}
// Set line height. Descent is negative.
metaData.height = metaData.ascent - metaData.descent;
......
......@@ -356,8 +356,10 @@ private:
// Description:
// This function initializes calculates the size of the required bounding box
// and stores it in the MetaData provided.
bool CalculateBoundingBox(const vtkStdString& str, MetaData &metaData);
bool CalculateBoundingBox(const vtkUnicodeString& str, MetaData &metaData);
template <typename T>
bool CalculateBoundingBox(const T& str, MetaData &metaData);
bool CalculateBoundingBox(const T& str, MetaData &metaData, const T& defaultHeightString);
// Description:
// Internal helper method called by RenderString.
......
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