TextActor

VTKExamples/Cxx/GeometricObjects/TextActor


Other Languages

See (Python), (Java)

Code

TextActor.cxx

#include <vtkNamedColors.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkSmartPointer.h>
#include <vtkTextActor.h>
#include <vtkTextProperty.h>

int main(int, char *[])
{
  vtkSmartPointer<vtkNamedColors> colors =
    vtkSmartPointer<vtkNamedColors>::New();

  // Create a rendering window and renderer.
  vtkSmartPointer<vtkRenderer> ren =
    vtkSmartPointer<vtkRenderer>::New();
  vtkSmartPointer<vtkRenderWindow> renWin =
    vtkSmartPointer<vtkRenderWindow>::New();
  renWin->SetWindowName("Regular Polygon Source");
  renWin->AddRenderer(ren);

  // Create a render window interactor.
  vtkSmartPointer<vtkRenderWindowInteractor> iren =
    vtkSmartPointer<vtkRenderWindowInteractor>::New();
  iren->SetRenderWindow(renWin);

  // Create a text actor.
  vtkSmartPointer<vtkTextActor> txt =
    vtkSmartPointer<vtkTextActor>::New();
  txt->SetInput("Hello World!");
  vtkTextProperty *txtprop = txt->GetTextProperty();
  txtprop->SetFontFamilyToArial();
  txtprop->BoldOn();
  txtprop->SetFontSize(36);
  txtprop->ShadowOn();
  txtprop->SetShadowOffset(4, 4);
  txtprop->SetColor(colors->GetColor3d("Cornsilk").GetData());
  txt->SetDisplayPosition(20, 30);

  // Assign actor to the renderer.
  ren->AddActor(txt);
  ren->SetBackground(colors->GetColor3d("DarkGreen").GetData());

  // Enable user interface interactor.
  iren->Initialize();
  renWin->Render();
  iren->Start();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

project(TextActor)

find_package(VTK COMPONENTS 
  vtkCommonColor
  vtkCommonCore
  vtkInteractionStyle
  vtkRenderingCore
  vtkRenderingFreeType
  vtkRenderingOpenGL2 QUIET)
if (NOT VTK_FOUND)
  message("Skipping TextActor: ${VTK_NOT_FOUND_MESSAGE}")
  return ()
endif()
message (STATUS "VTK_VERSION: ${VTK_VERSION}")
if (VTK_VERSION VERSION_LESS "8.90.0")
  # old system
  include(${VTK_USE_FILE})
  add_executable(TextActor MACOSX_BUNDLE TextActor.cxx )
  target_link_libraries(TextActor PRIVATE ${VTK_LIBRARIES})
else ()
  # include all components
  add_executable(TextActor MACOSX_BUNDLE TextActor.cxx )
  target_link_libraries(TextActor PRIVATE ${VTK_LIBRARIES})
  # vtk_module_autoinit is needed
  vtk_module_autoinit(
    TARGETS TextActor
    MODULES ${VTK_LIBRARIES}
    )
endif () 

Download and Build TextActor

Click here to download TextActor and its CMakeLists.txt file. Once the tarball TextActor.tar has been downloaded and extracted,

cd TextActor/build 

If VTK is installed:

cmake ..

If VTK is not installed but compiled on your system, you will need to specify the path to your VTK build:

cmake -DVTK_DIR:PATH=/home/me/vtk_build ..

Build the project:

make

and run it:

./TextActor

WINDOWS USERS

Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.