Skip to content

TextActor

Repository source: TextActor


Other languages

See (Python), (Java)

Question

If you have a question about this example, please use the VTK Discourse Forum

Code

TextActor.cxx

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

int main(int, char*[])
{
  vtkNew<vtkNamedColors> colors;

  // Create a rendering window and renderer.
  vtkNew<vtkRenderer> ren;
  vtkNew<vtkRenderWindow> renWin;
  renWin->SetWindowName("TextActor");
  renWin->AddRenderer(ren);

  // Create a render window interactor.
  vtkNew<vtkRenderWindowInteractor> iren;
  iren->SetRenderWindow(renWin);

  // Create a text actor.
  vtkNew<vtkTextActor> txt;
  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.12 FATAL_ERROR)

project(TextActor)

find_package(VTK COMPONENTS 
  CommonColor
  CommonCore
  InteractionStyle
  RenderingContextOpenGL2
  RenderingCore
  RenderingFreeType
  RenderingGL2PSOpenGL2
  RenderingOpenGL2
)

if (NOT VTK_FOUND)
  message(FATAL_ERROR "TextActor: Unable to find the VTK build folder.")
endif()

# Prevent a "command line is too long" failure in Windows.
set(CMAKE_NINJA_FORCE_RESPONSE_FILE "ON" CACHE BOOL "Force Ninja to use response files.")
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}
)

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.