Commit d978780f authored by David C. Lonie's avatar David C. Lonie

Remove the MathTextActor classes.

* Converted tests to use regular TextActors.
* Fixed the MathText path generator to rotate paths if needed.
* Update the GL2PS text export code to check for mathtext
  strings, since mathtext/freetype are both handled by the same
  actor.
* Made vtkTextRenderer::DetectBackend public so we can peek at
  the backend from the export code.

Change-Id: I52ba3c1e916c70dd80b67d59b611db1becb0846c
parent 5c052ecd
......@@ -30,8 +30,6 @@
#include "vtkImageShiftScale.h"
#include "vtkIntArray.h"
#include "vtkMapper2D.h"
#include "vtkMathTextActor.h"
#include "vtkMathTextActor3D.h"
#include "vtkMathTextUtilities.h"
#include "vtkMatrix4x4.h"
#include "vtkNew.h"
......@@ -662,15 +660,7 @@ void vtkGL2PSExporter::HandleSpecialProp(vtkProp *prop, vtkRenderer *ren)
{
if (vtkTextActor *textAct = vtkTextActor::SafeDownCast(act2d))
{
if (vtkMathTextActor *mathTextAct =
vtkMathTextActor::SafeDownCast(act2d))
{
this->DrawMathTextActor(mathTextAct, ren);
}
else
{
this->DrawTextActor(textAct, ren);
}
this->DrawTextActor(textAct, ren);
}
else if (vtkMapper2D *map2d = act2d->GetMapper())
{
......@@ -691,15 +681,7 @@ void vtkGL2PSExporter::HandleSpecialProp(vtkProp *prop, vtkRenderer *ren)
else if (vtkTextActor3D *textAct3D =
vtkTextActor3D::SafeDownCast(prop))
{
if (vtkMathTextActor3D *mathTextAct3D =
vtkMathTextActor3D::SafeDownCast(prop))
{
this->DrawMathTextActor3D(mathTextAct3D, ren);
}
else
{
this->DrawTextActor3D(textAct3D, ren);
}
this->DrawTextActor3D(textAct3D, ren);
}
else // Some other prop
{
......@@ -767,73 +749,6 @@ void vtkGL2PSExporter::DrawTextMapper(vtkTextMapper *textMap,
this->DrawViewportTextOverlay(string, tprop, coord, ren);
}
void vtkGL2PSExporter::DrawMathTextActor(vtkMathTextActor *textAct,
vtkRenderer *ren)
{
const char *string = textAct->GetInput();
vtkCoordinate *coord = textAct->GetActualPositionCoordinate();
vtkTextProperty *tprop = textAct->GetScaledTextProperty();
vtkNew<vtkPath> path;
vtkMathTextUtilities::GetInstance()->StringToPath(string,
path.GetPointer(),
tprop);
double *dcolor = tprop->GetColor();
unsigned char color[4] = {static_cast<unsigned char>(dcolor[0]*255),
static_cast<unsigned char>(dcolor[1]*255),
static_cast<unsigned char>(dcolor[2]*255),
static_cast<unsigned char>(tprop->GetOpacity()*255)
};
// Set the raster position at the center of the front plane. This is an
// overlay annotation, it shouldn't need to be clipped.
vtkNew<vtkCoordinate> rasterCoord;
rasterCoord->SetCoordinateSystemToView();
// Set a very small but finite depth -- otherwise the raster position will be
// clipped.
rasterCoord->SetValue(0.0, 0.0, 1e-5);
double *rasterPos = rasterCoord->GetComputedWorldValue(ren);
int *textPos = coord->GetComputedDisplayValue(ren);
double textPosd[2] = {static_cast<double>(textPos[0]),
static_cast<double>(textPos[1])};
vtkGL2PSUtilities::DrawPath(path.GetPointer(), rasterPos, textPosd, color,
NULL, tprop->GetOrientation());
}
void vtkGL2PSExporter::DrawMathTextActor3D(vtkMathTextActor3D *textAct,
vtkRenderer *)
{
// Get path
vtkNew<vtkPath> path;
vtkNew<vtkTextProperty> tprop;
const char *string = textAct->GetInput();
tprop->ShallowCopy(textAct->GetTextProperty());
tprop->SetOrientation(0); // Ignored in mathtextactor3d
tprop->SetJustificationToLeft(); // Ignored in mathtextactor3d
tprop->SetVerticalJustificationToBottom(); // Ignored in mathtextactor3d
vtkMathTextUtilities::GetInstance()->StringToPath(string,
path.GetPointer(),
tprop.GetPointer());
// Get actor info
vtkMatrix4x4 *actorMatrix = textAct->GetMatrix();
double *actorBounds = textAct->GetBounds();
double rasterPos[3] = {(actorBounds[1] + actorBounds[0]) * 0.5,
(actorBounds[3] + actorBounds[2]) * 0.5,
(actorBounds[5] + actorBounds[4]) * 0.5};
double *dcolor = tprop->GetColor();
unsigned char actorColor[4] = {
static_cast<unsigned char>(dcolor[0]*255),
static_cast<unsigned char>(dcolor[1]*255),
static_cast<unsigned char>(dcolor[2]*255),
static_cast<unsigned char>(tprop->GetOpacity()*255)};
vtkGL2PSUtilities::Draw3DPath(path.GetPointer(), actorMatrix, rasterPos,
actorColor);
}
void vtkGL2PSExporter::DrawViewportTextOverlay(const char *string,
vtkTextProperty *tprop,
vtkCoordinate *coord,
......
......@@ -85,8 +85,6 @@ class vtkCollection;
class vtkCoordinate;
class vtkImageData;
class vtkIntArray;
class vtkMathTextActor;
class vtkMathTextActor3D;
class vtkMatrix4x4;
class vtkPath;
class vtkProp;
......@@ -312,10 +310,6 @@ protected:
void DrawTextActor3D(vtkTextActor3D *textAct, vtkRenderer *ren);
void DrawTextMapper(vtkTextMapper *textMap, vtkActor2D *textAct,
vtkRenderer *ren);
void DrawMathTextActor(vtkMathTextActor *textAct,
vtkRenderer *ren);
void DrawMathTextActor3D(vtkMathTextActor3D *textAct,
vtkRenderer *ren);
void DrawViewportTextOverlay(const char *string, vtkTextProperty *tprop,
vtkCoordinate *coord, vtkRenderer *ren);
......
......@@ -95,7 +95,7 @@ int vtkActor2D::RenderOverlay(vtkViewport* viewport)
{
if (renderWindow->GetCapturingGL2PSSpecialProps())
{
if (this->IsA("vtkTextActor") || this->IsA("vtkMathTextActor") ||
if (this->IsA("vtkTextActor") ||
(this->Mapper && this->Mapper->IsA("vtkTextMapper")))
{
renderer->CaptureGL2PSSpecialProp(this);
......
......@@ -100,6 +100,11 @@ public:
vtkSetMacro(DefaultBackend, int)
vtkGetMacro(DefaultBackend, int)
// Description:
// Determine the appropriate back end needed to render the given string.
virtual int DetectBackend(const vtkStdString &str);
virtual int DetectBackend(const vtkUnicodeString &str);
// Description:
// Test for availability of various backends
bool FreeTypeIsSupported() { return HasFreeType; }
......@@ -247,10 +252,6 @@ protected:
static vtkTextRenderer *Instance;
static vtkTextRendererCleanup Cleanup;
// Description:
// Determine the appropriate back end needed to render the given string.
virtual int DetectBackend(const vtkStdString &str);
virtual int DetectBackend(const vtkUnicodeString &str);
vtksys::RegularExpression *MathTextRegExp;
vtksys::RegularExpression *MathTextRegExp2;
......
......@@ -4,8 +4,6 @@ set(Module_SRCS
vtkFreeTypeTools.cxx
vtkMathTextFreeTypeTextRenderer.cxx
vtkMathTextUtilities.cxx
vtkMathTextActor.cxx
vtkMathTextActor3D.cxx
vtkTextActor3D.cxx
vtkTextActor.cxx
vtkTextRendererStringToImage.cxx
......
/*=========================================================================
Program: Visualization Toolkit
Module: vtkMathTextActor.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 "vtkMathTextActor.h"
#include "vtkCellArray.h"
#include "vtkFloatArray.h"
#include "vtkImageData.h"
#include "vtkMath.h"
#include "vtkMathTextUtilities.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
#include "vtkPoints.h"
#include "vtkPolyData.h"
#include "vtkPolyDataMapper2D.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkTextProperty.h"
#include "vtkTextRenderer.h"
#include "vtkTexture.h"
#include "vtkViewport.h"
// ----------------------------------------------------------------------------
vtkStandardNewMacro(vtkMathTextActor);
// ----------------------------------------------------------------------------
bool vtkMathTextActor::IsSupported()
{
return vtkMathTextUtilities::GetInstance() != NULL;
}
// ----------------------------------------------------------------------------
vtkMathTextActor::vtkMathTextActor()
{
this->FallbackText = NULL;
}
// ----------------------------------------------------------------------------
vtkMathTextActor::~vtkMathTextActor()
{
this->SetFallbackText(NULL);
}
// ----------------------------------------------------------------------------
void vtkMathTextActor::ShallowCopy(vtkProp *prop)
{
if (vtkMathTextActor *actor = vtkMathTextActor::SafeDownCast(prop))
{
this->SetFallbackText(actor->GetFallbackText());
}
this->Superclass::ShallowCopy(prop);
}
// ----------------------------------------------------------------------------
bool vtkMathTextActor::RenderImage(vtkTextProperty *tprop,
vtkViewport *viewport)
{
vtkMathTextUtilities* util = vtkMathTextUtilities::GetInstance();
if (!util)
{ // Fall back to freetype rendering
if (!this->TextRenderer->RenderString(
tprop, this->FallbackText ? this->FallbackText : this->Input,
this->ImageData, NULL, 120, vtkTextRenderer::MathText))
{
vtkErrorMacro(<<"Failed rendering fallback text to buffer");
return false;
}
}
else
{
unsigned int dpi = 120;
vtkRenderer *ren = vtkRenderer::SafeDownCast(viewport);
if (ren)
{
if (ren->GetRenderWindow())
{
dpi = static_cast<unsigned int>(ren->GetRenderWindow()->GetDPI());
}
}
if (!util->RenderString(this->Input, this->ImageData, tprop, dpi))
{
vtkErrorMacro(<<"Failed rendering text to buffer");
return false;
}
}
return true;
}
// ----------------------------------------------------------------------------
bool vtkMathTextActor::GetImageBoundingBox(vtkTextProperty *tprop,
vtkViewport *viewport, int bbox[4])
{
vtkMathTextUtilities* util = vtkMathTextUtilities::GetInstance();
if (!util)
{ // Fall back to freetype rendering
if (!this->TextRenderer->GetBoundingBox(
tprop, this->FallbackText ? this->FallbackText : this->Input,
bbox, vtkTextRenderer::MathText))
{
vtkErrorMacro(<<"Failed rendering fallback text to buffer");
return false;
}
}
else
{
unsigned int dpi = 120;
vtkRenderer *ren = vtkRenderer::SafeDownCast(viewport);
if (ren)
{
if (ren->GetRenderWindow())
{
dpi = static_cast<unsigned int>(ren->GetRenderWindow()->GetDPI());
}
}
if (!util->GetBoundingBox(tprop, this->Input, dpi, bbox))
{
vtkErrorMacro(<<"Failed rendering text to buffer");
return false;
}
}
return true;
}
// ----------------------------------------------------------------------------
void vtkMathTextActor::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
if (this->FallbackText)
{
os << indent << "FallbackText: " << this->FallbackText << endl;
}
else
{
os << indent << "FallbackText: (none)\n";
}
}
/*=========================================================================
Program: Visualization Toolkit
Module: vtkMathTextActor.h
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.
=========================================================================*/
// .NAME vtkMathTextActor - An actor that displays math text
//
// .SECTION Description
// vtkMathTextActor can be used to place mathtext annotation into a window.
// Set the text property/attributes through the vtkTextProperty associated to
// this actor.
//
// .SECTION See Also
// vtkTextActor vtkMathTextUtilities vtkContext2D
#ifndef __vtkMathTextActor_h
#define __vtkMathTextActor_h
#include "vtkRenderingFreeTypeModule.h" // For export macro
#include "vtkTextActor.h"
class vtkTextProperty;
class vtkImageData;
class VTKRENDERINGFREETYPE_EXPORT vtkMathTextActor : public vtkTextActor
{
public:
vtkTypeMacro(vtkMathTextActor,vtkTextActor);
void PrintSelf(ostream& os, vtkIndent indent);
static vtkMathTextActor *New();
// Description:
// Returns true if MathText is available on this build of VTK. If false,
// this actor will not function.
bool IsSupported();
// Description:
// If there is no MathText implementation available (e.g. IsSupported()
// return false), the fallback text will be rendered using the FreeType
// text rendering backend.
vtkGetStringMacro(FallbackText)
vtkSetStringMacro(FallbackText)
// Description:
// Shallow copy of this actor.
void ShallowCopy(vtkProp *prop);
protected:
vtkMathTextActor();
~vtkMathTextActor();
bool RenderImage(vtkTextProperty *tprop, vtkViewport *viewport);
bool GetImageBoundingBox(
vtkTextProperty *tprop, vtkViewport *viewport, int bbox[4]);
// Description:
// Used when a MathText implementation is unavailable.
char *FallbackText;
private:
vtkMathTextActor(const vtkMathTextActor&); // Not implemented.
void operator=(const vtkMathTextActor&); // Not implemented.
};
#endif
/*=========================================================================
Program: Visualization Toolkit
Module: vtkMathTextActor3D.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 "vtkMathTextActor3D.h"
#include "vtkObjectFactory.h"
#include "vtkCamera.h"
#include "vtkImageActor.h"
#include "vtkImageData.h"
#include "vtkFreeTypeUtilities.h"
#include "vtkTransform.h"
#include "vtkTextProperty.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkWindow.h"
#include "vtkMathTextUtilities.h"
#include "vtkMath.h"
vtkStandardNewMacro(vtkMathTextActor3D)
// ----------------------------------------------------------------------------
vtkMathTextActor3D::vtkMathTextActor3D()
{
this->FallbackText = NULL;
}
// --------------------------------------------------------------------------
vtkMathTextActor3D::~vtkMathTextActor3D()
{
this->SetFallbackText(NULL);
}
// --------------------------------------------------------------------------
bool vtkMathTextActor3D::IsSupported()
{
return vtkMathTextUtilities::GetInstance() != NULL;
}
// --------------------------------------------------------------------------
int vtkMathTextActor3D::GetBoundingBox(int bbox[])
{
if (!this->TextProperty)
{
vtkErrorMacro(<<"Need valid vtkTextProperty.");
return 0;
}
if (!bbox)
{
vtkErrorMacro(<<"Need 4-element int array for bounding box.");
}
vtkMathTextUtilities *mtu = vtkMathTextUtilities::GetInstance();
if (!mtu)
{ // Use freetype fallback
vtkFreeTypeUtilities *fu = vtkFreeTypeUtilities::GetInstance();
if (!fu)
{
vtkErrorMacro(<<"Failed getting the FreeType utilities instance");
return 0;
}
fu->GetBoundingBox(this->TextProperty, this->FallbackText ?
this->FallbackText : this->Input, bbox);
if (!fu->IsBoundingBoxValid(bbox))
{
vtkErrorMacro(<<"Cannot determine bounding box of fallback text.");
return 0;
}
}
else
{
// Assume a 120 DPI output device
if (mtu->GetBoundingBox(this->TextProperty, this->Input, 120, bbox) == 0)
{
vtkErrorMacro(<<"Cannot determine bounding box of input.");
return 0;
}
}
return 1;
}
// --------------------------------------------------------------------------
void vtkMathTextActor3D::ShallowCopy(vtkProp *prop)
{
vtkMathTextActor3D *a = vtkMathTextActor3D::SafeDownCast(prop);
if (a != NULL)
{
this->SetFallbackText(a->GetFallbackText());
}
this->Superclass::ShallowCopy(prop);
}
// --------------------------------------------------------------------------
int vtkMathTextActor3D::UpdateImageActor()
{
// Need text prop
if (!this->TextProperty)
{
vtkErrorMacro(<<"Need a text property to render text actor");
return 0;
}
// No input, the assign the image actor a zilch input
if (!this->Input || !*this->Input)
{
if (this->ImageActor)
{
this->ImageActor->SetInputData(0);
}
return 1;
}
// Do we need to (re-)render the text ?
// Yes if:
// - instance has been modified since last build
// - text prop has been modified since last build
// - ImageData ivar has not been allocated yet
if (this->GetMTime() > this->BuildTime ||
this->TextProperty->GetMTime() > this->BuildTime ||
!this->ImageData)
{
this->BuildTime.Modified();
// Create the image data
if (!this->ImageData)
{
this->ImageData = vtkImageData::New();
this->ImageData->SetSpacing(1.0, 1.0, 1.0);
}
vtkMathTextUtilities *mtu = vtkMathTextUtilities::GetInstance();
if (!mtu)
{ // Render fallback text
vtkFreeTypeUtilities *fu = vtkFreeTypeUtilities::GetInstance();
if (!fu)
{
vtkErrorMacro(<<"Failed getting the FreeType utilities instance");
return 0;
}
if (!fu->RenderString(this->TextProperty, this->FallbackText ?
this->FallbackText : this->Input, this->ImageData))
{
vtkErrorMacro(<<"Failed rendering fallback text to buffer");
return 0;
}
}
else
{
if (!mtu->RenderString(this->Input, this->ImageData, this->TextProperty,
120))
{
vtkErrorMacro(<<"Failed rendering text to buffer");
return 0;
}
}
// Associate the image data (should be up to date now) to the image actor
if (this->ImageActor)
{
this->ImageActor->SetInputData(this->ImageData);
this->ImageActor->SetDisplayExtent(this->ImageData->GetExtent());
}
} // if (this->GetMTime() ...
// Position the actor
if (this->ImageActor)
{
vtkMatrix4x4 *matrix = this->ImageActor->GetUserMatrix();
if (!matrix)
{
matrix = vtkMatrix4x4::New();
this->ImageActor->SetUserMatrix(matrix);
matrix->Delete();
}
this->GetMatrix(matrix);
}
return 1;
}
// --------------------------------------------------------------------------
void vtkMathTextActor3D::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
if (this->FallbackText)
{
os << indent << "FallbackText: " << this->FallbackText << "\n";
}
else
{
os << indent << "FallbackText: (none)\n";
}
}
/*=========================================================================
Program: Visualization Toolkit
Module: vtkMathTextActor3D.h
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.
=========================================================================*/
// .NAME vtkMathTextActor3D - An actor that displays mathtext in 3D.
// .SECTION Description
// The input mathtext is rendered into a buffer, which in turn is used as a
// texture applied onto a quad (a vtkImageActor is used under the hood).
// .SECTION Caveats
// This class is experimental at the moment.
// - The TextProperty orientation is not used; instead orient this actor.
// - The TextProperty alignment is not used; instead, position this actor.
// - No checking is done regarding hardware texture size limits.
//
// .SECTION See Also
// vtkProp3D vtkMathTextActor vtkTextActor vtkTextActor3D
#ifndef __vtkMathTextActor3D_h
#define __vtkMathTextActor3D_h
#include "vtkRenderingFreeTypeModule.h" // For export macro
#include "vtkTextActor3D.h"
class VTKRENDERINGFREETYPE_EXPORT vtkMathTextActor3D : public vtkTextActor3D
{
public:
static vtkMathTextActor3D *New();
vtkTypeMacro(vtkMathTextActor3D,vtkTextActor3D)
void PrintSelf(ostream& os, vtkIndent indent);
// Description:
// Returns true if MathText is available on this build of VTK. If false,
// this actor will not function.
bool IsSupported();
// Description:
// If there is no MathText implementation available (e.g. IsSupported()
// return false), the fallback text will be rendered using the FreeType
// text rendering backend.
vtkSetStringMacro(FallbackText)
vtkGetStringMacro(FallbackText)
// Description:
// Get the bounding box for the given vtkTextProperty
// and text string str. Results are returned in the four element bbox int
// array. This call can be used for sizing other elements.
int GetBoundingBox(int bbox[4]);
// Shallow copy of this text actor. Overloads the virtual
// vtkProp method.
void ShallowCopy(vtkProp *prop);
protected:
vtkMathTextActor3D();
~vtkMathTextActor3D();
char *FallbackText;
int UpdateImageActor();
private:
vtkMathTextActor3D(const vtkMathTextActor3D&); // Not implemented.
void operator=(const vtkMathTextActor3D&); // Not implemented.
};
#endif
......@@ -234,7 +234,7 @@ int vtkTextActor::SetConstrainedFontSize(
}
// While the size is too large decrease it.. but never below 2 pt.
// (The MathTextActor subclass uses matplotlib which behaves poorly
// (The MathText rendering uses matplotlib which behaves poorly
// for very small fonts.)
while ((tempi[1] > targetHeight || tempi[0] > targetWidth)
&& fontSize > 3)
......
......@@ -44,7 +44,17 @@ float vtkGL2PSUtilities::LineWidthFactor = 5.f / 7.f;
void vtkGL2PSUtilities::DrawString(const char *str,
vtkTextProperty *tprop, double pos[])
{
if (!vtkGL2PSUtilities::TextAsPath)
vtkTextRenderer *tren(vtkTextRenderer::GetInstance());
if (tren == NULL)