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
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkFreeTypeLabelRenderStrategy.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 "vtkFreeTypeLabelRenderStrategy.h"
#include "vtkActor2D.h"
#include "vtkObjectFactory.h"
#include "vtkRenderer.h"
#include "vtkSmartPointer.h"
#include "vtkTextMapper.h"
#include "vtkTextProperty.h"
#include "vtkTextRenderer.h"
#include "vtkTimerLog.h"
#include "vtkWindow.h"
vtkStandardNewMacro(vtkFreeTypeLabelRenderStrategy);
//----------------------------------------------------------------------------
vtkFreeTypeLabelRenderStrategy::vtkFreeTypeLabelRenderStrategy()
{
this->TextRenderer = vtkTextRenderer::GetInstance();
this->Mapper = vtkTextMapper::New();
this->Actor = vtkActor2D::New();
this->Actor->SetMapper(this->Mapper);
}
//----------------------------------------------------------------------------
vtkFreeTypeLabelRenderStrategy::~vtkFreeTypeLabelRenderStrategy()
{
this->Mapper->Delete();
this->Actor->Delete();
}
void vtkFreeTypeLabelRenderStrategy::ReleaseGraphicsResources(vtkWindow *window)
{
this->Actor->ReleaseGraphicsResources(window);
}
//double compute_bounds_time1 = 0;
//int compute_bounds_iter1 = 0;
//----------------------------------------------------------------------------
void vtkFreeTypeLabelRenderStrategy::ComputeLabelBounds(
vtkTextProperty* tprop, vtkUnicodeString label, double bds[4])
{
//vtkTimerLog* timer = vtkTimerLog::New();
//timer->StartTimer();
// Check for empty string.
vtkStdString str;
label.utf8_str(str);
if (str.length() == 0)
{
bds[0] = 0;
bds[1] = 0;
bds[2] = 0;
bds[3] = 0;
return;
}
if (!tprop)
{
tprop = this->DefaultTextProperty;
}
vtkSmartPointer<vtkTextProperty> copy = tprop;
if (tprop->GetOrientation() != 0.0)
{
copy = vtkSmartPointer<vtkTextProperty>::New();
copy->ShallowCopy(tprop);
copy->SetOrientation(0.0);
}
int dpi = 72;
if (this->Renderer && this->Renderer->GetVTKWindow())
{
dpi = this->Renderer->GetVTKWindow()->GetDPI();
}
else
{
vtkWarningMacro(<<"No Renderer set. Assuming DPI of " << dpi << ".");
}
int bbox[4];
this->TextRenderer->GetBoundingBox(copy, label.utf8_str(), bbox, dpi);
// Take line offset into account
bds[0] = bbox[0];
bds[1] = bbox[1];
bds[2] = bbox[2] - tprop->GetLineOffset();
bds[3] = bbox[3] - tprop->GetLineOffset();
// Take justification into account
double sz[2] = {bds[1] - bds[0], bds[3] - bds[2]};
switch (tprop->GetJustification())
{
case VTK_TEXT_LEFT:
break;
case VTK_TEXT_CENTERED:
bds[0] -= sz[0]/2;
bds[1] -= sz[0]/2;
break;
case VTK_TEXT_RIGHT:
bds[0] -= sz[0];
bds[1] -= sz[0];
break;
}
switch (tprop->GetVerticalJustification())
{
case VTK_TEXT_BOTTOM:
break;
case VTK_TEXT_CENTERED:
bds[2] -= sz[1]/2;
bds[3] -= sz[1]/2;
break;
case VTK_TEXT_TOP:
bds[2] -= sz[1];
bds[3] -= sz[1];
break;
}
//timer->StopTimer();
//compute_bounds_time1 += timer->GetElapsedTime();
//compute_bounds_iter1++;
//if (compute_bounds_iter1 % 10000 == 0)
// {
// cerr << "ComputeLabelBounds time: " << (compute_bounds_time1 / compute_bounds_iter1) << endl;
// }
}
//double render_label_time1 = 0;
//int render_label_iter1 = 0;
//----------------------------------------------------------------------------
void vtkFreeTypeLabelRenderStrategy::RenderLabel(
int x[2], vtkTextProperty* tprop, vtkUnicodeString label)
{
//vtkTimerLog* timer = vtkTimerLog::New();
//timer->StartTimer();
if (!this->Renderer)
{
vtkErrorMacro("Renderer must be set before rendering labels.");
return;
}
if (!tprop)
{
tprop = this->DefaultTextProperty;
}
this->Mapper->SetTextProperty(tprop);
this->Mapper->SetInput(label.utf8_str());
this->Actor->GetPositionCoordinate()->SetCoordinateSystemToDisplay();
this->Actor->GetPositionCoordinate()->SetValue(x[0], x[1], 0.0);
this->Mapper->RenderOverlay(this->Renderer, this->Actor);
//timer->StopTimer();
//render_label_time1 += timer->GetElapsedTime();
//render_label_iter1++;
//if (render_label_iter1 % 100 == 0)
// {
// cerr << "RenderLabel time: " << (render_label_time1 / render_label_iter1) << endl;
// }
}
//----------------------------------------------------------------------------
void vtkFreeTypeLabelRenderStrategy::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
}
|