1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkMathTextUtilities.cxx
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.
=========================================================================*/
#include "vtkMathTextUtilities.h"
#include "vtkFreeTypeUtilities.h"
#include "vtkMath.h"
#include "vtkObjectFactory.h"
#include "vtkTextProperty.h"
#include "vtkTextActor.h"
#include "vtkViewport.h"
#include "vtkWindow.h"
#ifdef VTK_DEBUG_LEAKS
#include "vtkDebugLeaks.h"
#endif
#include <algorithm>
//----------------------------------------------------------------------------
// The singleton, and the singleton cleanup
vtkMathTextUtilities* vtkMathTextUtilities::Instance = NULL;
vtkMathTextUtilitiesCleanup vtkMathTextUtilities::Cleanup;
//----------------------------------------------------------------------------
// Create the singleton cleanup
// Register our singleton cleanup callback against the FTLibrary so that
// it might be called before the FTLibrary singleton is destroyed.
vtkMathTextUtilitiesCleanup::vtkMathTextUtilitiesCleanup()
{
}
//----------------------------------------------------------------------------
// Delete the singleton cleanup
vtkMathTextUtilitiesCleanup::~vtkMathTextUtilitiesCleanup()
{
vtkMathTextUtilities::SetInstance(NULL);
}
//----------------------------------------------------------------------------
vtkMathTextUtilities* vtkMathTextUtilities::GetInstance()
{
if (!vtkMathTextUtilities::Instance)
{
vtkMathTextUtilities::Instance = static_cast<vtkMathTextUtilities *>(
vtkObjectFactory::CreateInstance("vtkMathTextUtilities"));
// Clean up any leaked references from vtkDebugLeaks if needed
#ifdef VTK_DEBUG_LEAKS
if (!vtkMathTextUtilities::Instance)
{
vtkDebugLeaks::DestructClass("vtkMathTextUtilities");
}
#endif
}
return vtkMathTextUtilities::Instance;
}
//----------------------------------------------------------------------------
void vtkMathTextUtilities::SetInstance(vtkMathTextUtilities* instance)
{
if (vtkMathTextUtilities::Instance == instance)
{
return;
}
if (vtkMathTextUtilities::Instance)
{
vtkMathTextUtilities::Instance->Delete();
}
vtkMathTextUtilities::Instance = instance;
// User will call ->Delete() after setting instance
if (instance)
{
instance->Register(NULL);
}
}
//----------------------------------------------------------------------------
int vtkMathTextUtilities::GetConstrainedFontSize(const char *str,
vtkTextProperty *tprop,
int targetWidth,
int targetHeight,
int dpi)
{
if (str == NULL || str[0] == '\0' || targetWidth == 0 || targetHeight == 0 ||
tprop == NULL)
{
return 0;
}
// Use the current font size as a first guess
int bbox[4];
double fontSize = tprop->GetFontSize();
if (!this->GetBoundingBox(tprop, str, dpi, bbox))
{
return -1;
}
int width = bbox[1] - bbox[0];
int height = bbox[3] - bbox[2];
// Bad assumption but better than nothing -- assume the bbox grows linearly
// with the font size:
if (width != 0 && height != 0)
{
fontSize *= std::min(
static_cast<double>(targetWidth) / static_cast<double>(width),
static_cast<double>(targetHeight) / static_cast<double>(height));
tprop->SetFontSize(static_cast<int>(fontSize));
if (!this->GetBoundingBox(tprop, str, dpi, bbox))
{
return -1;
}
width = bbox[1] - bbox[0];
height = bbox[3] - bbox[2];
}
// Now just step up/down until the bbox matches the target.
while ((width < targetWidth || height < targetHeight) && fontSize < 200)
{
fontSize += 1.;
tprop->SetFontSize(fontSize);
if (!this->GetBoundingBox(tprop, str, dpi, bbox))
{
return -1;
}
width = bbox[1] - bbox[0];
height = bbox[3] - bbox[2];
}
while ((width > targetWidth || height > targetHeight) && fontSize > 0)
{
fontSize -= 1.;
tprop->SetFontSize(fontSize);
if (!this->GetBoundingBox(tprop, str, dpi, bbox))
{
return -1;
}
width = bbox[1] - bbox[0];
height = bbox[3] - bbox[2];
}
return fontSize;
}
//----------------------------------------------------------------------------
vtkMathTextUtilities* vtkMathTextUtilities::New()
{
vtkMathTextUtilities* ret = vtkMathTextUtilities::GetInstance();
if (ret)
{
ret->Register(NULL);
}
return ret;
}
//----------------------------------------------------------------------------
vtkMathTextUtilities::vtkMathTextUtilities()
{
}
//----------------------------------------------------------------------------
vtkMathTextUtilities::~vtkMathTextUtilities()
{
}
//----------------------------------------------------------------------------
void vtkMathTextUtilities::PrintSelf(ostream &os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "Instance: " << this->Instance << endl;
}
|