[vtkusers] Font size in vtkScalarBarActor

John Platt jcplatt at dsl.pipex.com
Wed Mar 9 04:28:38 EST 2016


Hi,

AFAIK (at VTK 5.10 at least) vtkScalarBarActor always chooses the font 
size to fit the actor size.

Attached is vtkScalarBarActor2 which allows a ceiling to be set on the 
title and label font size. This is based on the 5.10 version of 
vtkScalarBarActor.

HTH

John.

On 09/03/2016 02:45, Tharun wrote:
> My I know if anybody has a solution for the question above?
>
>
>
> --
> View this message in context: http://vtk.1045678.n5.nabble.com/Font-size-in-vtkScalarBarActor-tp5726868p5737107.html
> Sent from the VTK - Users mailing list archive at Nabble.com.
> _______________________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
>
> Please keep messages on-topic and check the VTK FAQ at: http://www.vtk.org/Wiki/VTK_FAQ
>
> Search the list archives at: http://markmail.org/search/?q=vtkusers
>
> Follow this link to subscribe/unsubscribe:
> http://public.kitware.com/mailman/listinfo/vtkusers
>

-------------- next part --------------
/*=========================================================================
=========================================================================*/
#include "vtkScalarBarActor2.h"
#include "vtkObjectFactory.h"
#include "vtkTextMapper.h"
#include "vtkTextProperty.h"
#include "vtkScalarsToColors.h"

vtkStandardNewMacro(vtkScalarBarActor2);


//----------------------------------------------------------------------------
// Instantiate object with 64 maximum colors; 5 labels; %%-#6.3g label
// format, no title, and vertical orientation. The initial scalar bar
// size is (0.05 x 0.8) of the viewport size.

vtkScalarBarActor2::vtkScalarBarActor2()
{
  // Optional title when scalar bar is oriented horizontally.
  this->HorizontalTitle = NULL;

  // No ceiling on the label text font size in effect on construction.
  this->LabelFontSizeCeiling = 0;
  this->TitleFontSizeCeiling = 0;
}


//----------------------------------------------------------------------------
vtkScalarBarActor2::~vtkScalarBarActor2()
{
  if (this->HorizontalTitle)
    {
    delete [] this->HorizontalTitle;
    this->HorizontalTitle = NULL;
    }
}


//----------------------------------------------------------------------------
void vtkScalarBarActor2::AllocateAndSizeLabels( int* labelSize, int* size,
                                                vtkViewport* viewport, double* range )
{
  labelSize[0] = labelSize[1] = 0;

  this->TextMappers = new vtkTextMapper * [this->NumberOfLabels];
  this->TextActors = new vtkActor2D * [this->NumberOfLabels];

  char string[512];

  double val;
  int i;
  
  // TODO: this should be optimized, maybe by keeping a list of
  // allocated mappers, in order to avoid creation/destruction of
  // their underlying text properties (i.e. each time a mapper is
  // created, text properties are created and shallow-assigned a font size
  // which value might be "far" from the target font size).

  // is this a vtkLookupTable or a subclass of vtkLookupTable 
  // with its scale set to log
  int isLogTable = this->LookupTable->UsingLogScale();

  for (i=0; i < this->NumberOfLabels; i++)
    {
    this->TextMappers[i] = vtkTextMapper::New();

    if ( isLogTable )
      {
      double lval;
      if (this->NumberOfLabels > 1)
        {
        lval = log10(range[0]) +
          static_cast<double>(i)/(this->NumberOfLabels-1) *
          (log10(range[1])-log10(range[0]));
        }
      else
        {
        lval = log10(range[0]) + 0.5*(log10(range[1])-log10(range[0]));
        }
      val = pow(10.0,lval);
      }
    else
      {
      if (this->NumberOfLabels > 1)
        {
        val = range[0] +
          static_cast<double>(i)/(this->NumberOfLabels-1) 
          * (range[1]-range[0]);
        }
      else
        {
        val = range[0] + 0.5*(range[1]-range[0]);
        }
      }

    sprintf(string, this->LabelFormat, val);
    this->TextMappers[i]->SetInput(string);

    // Shallow copy here so that the size of the label prop is not affected
    // by the automatic adjustment of its text mapper's size (i.e. its
    // mapper's text property is identical except for the font size
    // which will be modified later). This allows text actors to
    // share the same text property, and in that case specifically allows
    // the title and label text prop to be the same.
    this->TextMappers[i]->GetTextProperty()->ShallowCopy(
      this->LabelTextProperty);

    this->TextActors[i] = vtkActor2D::New();
    this->TextActors[i]->SetMapper(this->TextMappers[i]);
    this->TextActors[i]->SetProperty(this->GetProperty());
    this->TextActors[i]->GetPositionCoordinate()->
      SetReferenceCoordinate(this->PositionCoordinate);
    }

  if (this->NumberOfLabels)
    {
    int targetWidth, targetHeight;

    if ( this->Orientation == VTK_ORIENT_VERTICAL )
      {
      targetWidth = static_cast<int>(0.6*size[0]);
      targetHeight = static_cast<int>(0.86*size[1]/this->NumberOfLabels);
      }
    else
      {
      targetWidth = static_cast<int>(size[0]*0.8/this->NumberOfLabels);
      targetHeight = static_cast<int>(0.25*size[1]);
      }

    int labelFontSize = vtkTextMapper::SetMultipleConstrainedFontSize( viewport, 
                                                                       targetWidth, 
                                                                       targetHeight,
                                                                       this->TextMappers,
                                                                       this->NumberOfLabels,
                                                                       labelSize);

      // The combination of a vertically orientated bar and single digit scalar
      // values can lead to an oversized text label font. The font size can safely
      // be REDUCED while still honoring the Position and Position2 coordinates
      // (i.e. bar + labels will still fit inside the box described by these 2
      // points).

      if ( this->LabelFontSizeCeiling )
      {
         if ( labelFontSize > LabelFontSizeCeiling )
         {
            for ( i = 0; i < NumberOfLabels; i++ )
            {
               if ( this->TextMappers[i] )
               {
                  this->TextMappers[i]->GetTextProperty()->SetFontSize( this->LabelFontSizeCeiling );
               }
            }
         }
      }
  }
}


//----------------------------------------------------------------------------
// The title input is set in RenderOpaqueGeometry(). This is used as a convenience
// for changing the input.

void vtkScalarBarActor2::SizeTitle( int* titleSize, int* size, vtkViewport* viewport )
{
  titleSize[0] = titleSize[1] = 0;

   // Set the horizontal title.
   if ( this->Orientation == VTK_ORIENT_HORIZONTAL && this->HorizontalTitle )
      this->TitleMapper->SetInput(this->HorizontalTitle);


  if (this->Title == NULL || !strlen(this->Title))
    {
    return;
    }

  int targetWidth, targetHeight;
  if ( this->Orientation == VTK_ORIENT_VERTICAL )
    {
    targetWidth = static_cast<int>(0.9*size[0]);
//    targetHeight = static_cast<int>(0.1*size[1]);
    targetHeight = static_cast<int>(0.12*size[1]);
    }
  else
    {
    targetWidth = size[0];
    targetHeight = static_cast<int>(0.25*size[1]);
    }

  // Set and return the font size required to make this mapper fit in a given 
  // target rectangle (width x height, in pixels).

  int titleFontSize = this->TitleMapper->SetConstrainedFontSize( viewport, targetWidth, targetHeight );
  if ( this->TitleFontSizeCeiling )
    {
    if ( titleFontSize > TitleFontSizeCeiling )
      {
         this->TitleMapper->GetTextProperty()->SetFontSize( this->TitleFontSizeCeiling );
      }
    }

  this->TitleMapper->GetSize(viewport, titleSize);
}


//----------------------------------------------------------------------------
void vtkScalarBarActor2::ShallowCopy(vtkProp *prop)
{
  vtkScalarBarActor2 *a = vtkScalarBarActor2::SafeDownCast(prop);
  if ( a != NULL )
    {
    this->SetHorizontalTitle(a->GetHorizontalTitle());
    this->SetTitleFontSizeCeiling(a->GetTitleFontSizeCeiling());
    this->SetLabelFontSizeCeiling(a->GetLabelFontSizeCeiling());
    }

  // Now do superclass
  this->vtkScalarBarActor::ShallowCopy(prop);
}


//----------------------------------------------------------------------------
void vtkScalarBarActor2::PrintSelf(ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os,indent);

  os << indent << "Horizontal Title: " << (this->HorizontalTitle ? this->HorizontalTitle : "(none)") << "\n";
  os << indent << "Title Font Size Ceiling: " << this->TitleFontSizeCeiling << "\n";
  os << indent << "Label Font Size Ceiling: " << this->LabelFontSizeCeiling << "\n";
}
-------------- next part --------------
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkScalarBarActor.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 vtkScalarBarActor2 - Create a scalar bar with labels
// .SECTION Description

#ifndef __vtkScalarBarActor2_h
#define __vtkScalarBarActor2_h

#include "vtkScalarBarActor.h"

class VTK_RENDERING_EXPORT vtkScalarBarActor2 : public vtkScalarBarActor
{
public:
  vtkTypeMacro(vtkScalarBarActor2,vtkScalarBarActor);
  void PrintSelf(ostream& os, vtkIndent indent);

  // Description:
  // Instantiate object with 64 maximum colors; 5 labels; %%-#6.3g label
  // format, no title, and vertical orientation. The initial scalar bar
  // size is (0.05 x 0.8) of the viewport size.

  static vtkScalarBarActor2* New();

  // Description:
  // Set/Get the horizontal title of the scalar bar actor,
  vtkSetStringMacro(HorizontalTitle);
  vtkGetStringMacro(HorizontalTitle);

  // Description:
  // Set/Get a ceiling on the title text font size. This is used to REDUCE the
  // title font size determined by SetConstrainedFontSize().
  vtkSetClampMacro(TitleFontSizeCeiling, int, 0, VTK_LARGE_INTEGER);
  vtkGetMacro(TitleFontSizeCeiling, int);

  // Description:
  // Set/Get a ceiling on the label text font size. This is used to REDUCE the
  // text label font size determined by SetMultipleConstrainedFontSize().
  vtkSetClampMacro(LabelFontSizeCeiling, int, 0, VTK_LARGE_INTEGER);
  vtkGetMacro(LabelFontSizeCeiling, int);

  // Description:
  // Shallow copy of a scalar bar actor. Overloads the virtual vtkProp method.
  void ShallowCopy(vtkProp *prop);

protected:
  vtkScalarBarActor2();
  ~vtkScalarBarActor2();

  char* HorizontalTitle;
  virtual void SizeTitle( int* titleSize, int* size, vtkViewport* viewport );

  int TitleFontSizeCeiling;
  int LabelFontSizeCeiling;
  virtual void AllocateAndSizeLabels( int* labelSize, int* size,
                                      vtkViewport* viewport, double* range );

private:
  vtkScalarBarActor2(const vtkScalarBarActor2&);  // Not implemented.
  void operator=(const vtkScalarBarActor2&);  // Not implemented.
};


#endif



More information about the vtkusers mailing list