From 5dcad9788f710a5a9727cb3c1da34c7db396feb0 Mon Sep 17 00:00:00 2001
From: Max Smolens <max.smolens@kitware.com>
Date: Tue, 12 Apr 2016 12:09:34 -0400
Subject: [PATCH] vtkTextMapper: fix rendering of empty string

vtkTextMapper improperly renders empty strings. Instead of rendering nothing, as
expected for an empty string, a rectangle is rendered. The rectangle is visible
only when the text property's background is not completely transparent.

This commit fixes the problem at two levels: in vtkFreeTypeTools and in
vtkTextMapper.

vtkFreeTypeTools now checks for an empty string when rendering to an image.
Without this check, rendering an empty string results in the small rectangle.

vtkTextMapper::RenderOverlay() is updated to handle the case of an empty image.
This also serves as a revised solution to #15787:
http://www.vtk.org/Bug/view.php?id=15787

Fixes #16071:
http://www.vtk.org/Bug/view.php?id=16071
---
 Rendering/Core/vtkTextMapper.cxx                 | 16 ++++++++--------
 .../Testing/Cxx/TestFreeTypeTextMapperNoMath.cxx | 14 ++++++++++++++
 Rendering/FreeType/vtkFreeTypeTools.cxx          |  8 ++++++++
 3 files changed, 30 insertions(+), 8 deletions(-)

diff --git a/Rendering/Core/vtkTextMapper.cxx b/Rendering/Core/vtkTextMapper.cxx
index da885b6a0e..6d9ea59c4c 100644
--- a/Rendering/Core/vtkTextMapper.cxx
+++ b/Rendering/Core/vtkTextMapper.cxx
@@ -367,7 +367,7 @@ void vtkTextMapper::RenderOverlay(vtkViewport *viewport, vtkActor2D *actor)
   vtkDebugMacro(<<"RenderOverlay called");
 
   vtkRenderer *ren = NULL;
-  if (this->Input)
+  if (this->Input && this->Input[0])
     {
     vtkWindow *win = viewport->GetVTKWindow();
     if (!win)
@@ -394,15 +394,15 @@ void vtkTextMapper::RenderOverlay(vtkViewport *viewport, vtkActor2D *actor)
       info->Set(vtkProp::GeneralTextureUnit(),
         this->Texture->GetTextureUnit());
       }
-    }
 
-  vtkDebugMacro(<<"PolyData::RenderOverlay called");
-  this->Mapper->RenderOverlay(viewport, actor);
+    vtkDebugMacro(<<"PolyData::RenderOverlay called");
+    this->Mapper->RenderOverlay(viewport, actor);
 
-  // clean up
-  if (ren)
-    {
-    this->Texture->PostRender(ren);
+    // clean up
+    if (ren)
+      {
+      this->Texture->PostRender(ren);
+      }
     }
 
   vtkDebugMacro(<<"Superclass::RenderOverlay called");
diff --git a/Rendering/FreeType/Testing/Cxx/TestFreeTypeTextMapperNoMath.cxx b/Rendering/FreeType/Testing/Cxx/TestFreeTypeTextMapperNoMath.cxx
index ad66e49630..03f6d00c11 100644
--- a/Rendering/FreeType/Testing/Cxx/TestFreeTypeTextMapperNoMath.cxx
+++ b/Rendering/FreeType/Testing/Cxx/TestFreeTypeTextMapperNoMath.cxx
@@ -172,6 +172,19 @@ int TestFreeTypeTextMapperNoMath(int argc, char *argv[])
   mapper11->SetInput("oTeVaVoVAW");
   actor11->SetPosition(300, 200);
 
+  // Empty string, solid background: should not render
+  vtkNew<vtkTextMapper> mapper12;
+  vtkNew<vtkActor2D> actor12;
+  actor12->SetMapper(mapper12.GetPointer());
+  mapper12->GetTextProperty()->SetFontSize(16);
+  mapper12->GetTextProperty()->SetColor(1.0, 0.0, 0.0);
+  mapper12->GetTextProperty()->SetBackgroundColor(1.0, 0.5, 1.0);
+  mapper12->GetTextProperty()->SetBackgroundOpacity(1.0);
+  mapper12->GetTextProperty()->SetJustificationToRight();
+  mapper12->GetTextProperty()->SetVerticalJustificationToCentered();
+  mapper12->SetInput("");
+  actor12->SetPosition(0, 0);
+
   // Boring rendering setup....
 
   vtkNew<vtkRenderer> ren;
@@ -193,6 +206,7 @@ int TestFreeTypeTextMapperNoMath(int argc, char *argv[])
   ren->AddActor(actor9.GetPointer());
   ren->AddActor(actor10.GetPointer());
   ren->AddActor(actor11.GetPointer());
+  ren->AddActor(actor12.GetPointer());
 
   win->SetMultiSamples(0);
   win->Render();
diff --git a/Rendering/FreeType/vtkFreeTypeTools.cxx b/Rendering/FreeType/vtkFreeTypeTools.cxx
index 114f5f85c3..e071bea9d1 100644
--- a/Rendering/FreeType/vtkFreeTypeTools.cxx
+++ b/Rendering/FreeType/vtkFreeTypeTools.cxx
@@ -1239,6 +1239,14 @@ bool vtkFreeTypeTools::RenderStringInternal(vtkTextProperty *tprop,
     return false;
     }
 
+  if (str.empty())
+    {
+    data->Initialize();
+    textDims[0] = 0;
+    textDims[1] = 0;
+    return true;
+    }
+
   ImageMetaData metaData;
 
   // Setup the metadata cache
-- 
GitLab