Skip to content

FontFile

vtk-examples/Cxx/Visualization/FontFile


Description

The example uses an external font file. There are many sources of TrueType fonts. Here is one source.

Question

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

Code

FontFile.cxx

#include <vtkActor2D.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkTextMapper.h>
#include <vtkTextProperty.h>

#include <sstream>
#include <string>
#include <vtksys/SystemTools.hxx>

int main(int argc, char* argv[])
{
  if (argc < 2)
  {
    std::cerr << "Usage: " << argv[1] << " font.ttf e.g. CopyPaste.ttf"
              << std::endl;
    return EXIT_FAILURE;
  }

  std::string fontFile(argv[1]);
  std::stringstream str;
  str << "Font: "
      << vtksys::SystemTools::GetFilenameWithoutExtension(std::string(argv[1]))
      << "\n"
      << "Sample multiline\ntext rendered\nusing FreeTypeTools.";

  int width = 640;
  int height = 480;

  vtkNew<vtkNamedColors> colors;

  vtkNew<vtkTextMapper> mapper;

  vtkNew<vtkActor2D> actor;
  actor->SetPosition(width / 2, height / 2);
  actor->SetMapper(mapper);
  mapper->GetTextProperty()->BoldOff();
  ;
  mapper->GetTextProperty()->SetFontSize(50);
  mapper->GetTextProperty()->SetColor(
      colors->GetColor3d("MistyRose").GetData());
  mapper->GetTextProperty()->SetJustificationToCentered();
  mapper->GetTextProperty()->SetVerticalJustificationToCentered();
  mapper->GetTextProperty()->SetFontFamily(VTK_FONT_FILE);
  mapper->GetTextProperty()->SetFontFile(fontFile.c_str());
  mapper->SetInput(str.str().c_str());

  vtkNew<vtkRenderer> renderer;
  renderer->SetBackground(colors->GetColor3d("DimGray").GetData());

  vtkNew<vtkRenderWindow> renderWindow;

  renderWindow->SetSize(width, height);
  renderWindow->AddRenderer(renderer);
  renderWindow->SetWindowName("FontFile");

  vtkNew<vtkRenderWindowInteractor> interactor;
  interactor->SetRenderWindow(renderWindow);

  renderer->AddActor(actor);

  renderWindow->Render();
  interactor->Initialize();
  interactor->Start();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

project(FontFile)

find_package(VTK COMPONENTS 
  vtkCommonColor
  vtkCommonCore
  vtkInteractionStyle
  vtkRenderingContextOpenGL2
  vtkRenderingCore
  vtkRenderingFreeType
  vtkRenderingGL2PSOpenGL2
  vtkRenderingOpenGL2
  QUIET
)

if (NOT VTK_FOUND)
  message("Skipping FontFile: ${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(FontFile MACOSX_BUNDLE FontFile.cxx )
  target_link_libraries(FontFile PRIVATE ${VTK_LIBRARIES})
else ()
  # include all components
  add_executable(FontFile MACOSX_BUNDLE FontFile.cxx )
  target_link_libraries(FontFile PRIVATE ${VTK_LIBRARIES})
  # vtk_module_autoinit is needed
  vtk_module_autoinit(
    TARGETS FontFile
    MODULES ${VTK_LIBRARIES}
    )
endif ()

Download and Build FontFile

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

cd FontFile/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:

./FontFile

WINDOWS USERS

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