Skip to content

Commit

Permalink
Add DPI awareness to text rendering.
Browse files Browse the repository at this point in the history
  • Loading branch information
David C. Lonie committed May 14, 2015
1 parent c8d6551 commit 07e728b
Show file tree
Hide file tree
Showing 55 changed files with 1,022 additions and 307 deletions.
2 changes: 1 addition & 1 deletion Common/Core/vtkWindow.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ vtkWindow::vtkWindow()
strcpy( this->WindowName, windowname );
this->Erase = 1;
this->DoubleBuffer = 0;
this->DPI = 120;
this->DPI = 72;
this->TileViewport[0] = 0;
this->TileViewport[1] = 0;
this->TileViewport[2] = 1.0;
Expand Down
2 changes: 1 addition & 1 deletion Common/Core/vtkWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ class VTKCOMMONCORE_EXPORT vtkWindow : public vtkObject
// Return a best estimate to the dots per inch of the display
// device being rendered (or printed).
vtkGetMacro(DPI,int);
vtkSetClampMacro(DPI,int,1,3000);
vtkSetClampMacro(DPI,int,1,VTK_INT_MAX);

// Description:
// Attempt to detect and set the DPI of the display device by querying the
Expand Down
18 changes: 12 additions & 6 deletions IO/Export/vtkGL2PSExporter.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -734,14 +734,19 @@ void vtkGL2PSExporter::DrawTextActor3D(vtkTextActor3D *textAct,
vtkTextProperty *tprop = textAct->GetTextProperty();
vtkNew<vtkPath> textPath;
vtkTextRenderer *tren = vtkTextRenderer::GetInstance();
if (tren)

if (!tren)
{
tren->StringToPath(tprop, vtkStdString(string), textPath.GetPointer());
vtkWarningMacro(<<"Cannot generate path data from 3D text string '"
<< string << "': Text renderer unavailable.");
return;
}
else

if (!tren->StringToPath(tprop, vtkStdString(string), textPath.GetPointer(),
vtkTextActor3D::GetRenderedDPI()))
{
vtkWarningMacro(<<"Cannot generate path data from 3D text string: "
<< string);
vtkWarningMacro(<<"Failed to generate path data from 3D text string '"
<< string << "': StringToPath failed.");
return;
}

Expand Down Expand Up @@ -783,7 +788,8 @@ void vtkGL2PSExporter::DrawTextActor3D(vtkTextActor3D *textAct,
textPos[2] + (forward[2] * 0.0001)};

vtkTextRenderer::Metrics metrics;
if (tren->GetMetrics(tprop, string, metrics))
if (tren->GetMetrics(tprop, string, metrics,
vtkTextActor3D::GetRenderedDPI()))
{
vtkNew<vtkPath> bgPath;
bgPath->InsertNextPoint(static_cast<double>(metrics.TopLeft.GetX()),
Expand Down
11 changes: 10 additions & 1 deletion Interaction/Widgets/vtkCaptionRepresentation.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,19 @@ void vtkCaptionRepresentation::AdjustCaptionBoundary()
return;
}

vtkWindow *win = this->Renderer->GetVTKWindow();
if (!win)
{
vtkErrorMacro(<<"No render window available: cannot determine DPI.");
return;
}

int text_bbox[4];
if (!tren->GetBoundingBox(this->CaptionActor2D->GetCaptionTextProperty(),
this->CaptionActor2D->GetCaption(), text_bbox))
this->CaptionActor2D->GetCaption(), text_bbox,
win->GetDPI()))
{
vtkErrorMacro(<<"Error calculating caption bounding box.");
return;
}

Expand Down
10 changes: 9 additions & 1 deletion Interaction/Widgets/vtkTextRepresentation.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "vtkRenderer.h"
#include "vtkStdString.h"
#include "vtkCommand.h"
#include "vtkWindow.h"

class vtkTextRepresentationObserver : public vtkCommand
{
Expand Down Expand Up @@ -270,9 +271,16 @@ void vtkTextRepresentation::CheckTextBoundary()

this->TextActor->ComputeScaledFont(this->GetRenderer());

vtkWindow *win = this->Renderer->GetVTKWindow();
if (!win)
{
vtkErrorMacro(<<"No render window available: cannot determine DPI.");
return;
}

int text_bbox[4];
if (!tren->GetBoundingBox(this->TextActor->GetScaledTextProperty(),
this->GetText(), text_bbox))
this->GetText(), text_bbox, win->GetDPI()))
{
return;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
a84ace11ef6ec63994f9be321a24b5f9
50101c180fac543f8e5cff299c31cd59
Original file line number Diff line number Diff line change
@@ -1 +1 @@
cef3b676f5b613333f0299bef1304b2d
3b0a123f21ae9514f9b02beab27ab73c

This file was deleted.

This file was deleted.

11 changes: 10 additions & 1 deletion Rendering/Annotation/vtkAxisActor.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "vtkTextProperty.h"
#include "vtkVectorText.h"
#include "vtkViewport.h"
#include "vtkWindow.h"

vtkStandardNewMacro(vtkAxisActor);
vtkCxxSetObjectMacro(vtkAxisActor, Camera, vtkCamera);
Expand Down Expand Up @@ -986,6 +987,13 @@ vtkAxisActor::SetLabelPositions2D(vtkViewport *viewport, bool force)
return;
}

vtkWindow *win = viewport->GetVTKWindow();
if (!win)
{
vtkErrorMacro(<<"No render window available: cannot determine DPI.");
return;
}

for (int i = 0; i < this->NumberOfLabelsBuilt; i++)
{
ptIdx = 4*i + 1;
Expand All @@ -1002,7 +1010,8 @@ vtkAxisActor::SetLabelPositions2D(vtkViewport *viewport, bool force)

int bbox[4];
if (!tren->GetBoundingBox(this->LabelActors2D[i]->GetTextProperty(),
this->LabelActors2D[i]->GetInput(), bbox))
this->LabelActors2D[i]->GetInput(), bbox,
win->GetDPI()))
{
vtkErrorMacro(<< "Unable to calculate bounding box for label "
<< this->LabelActors2D[i]->GetInput());
Expand Down
6 changes: 4 additions & 2 deletions Rendering/ContextOpenGL/vtkOpenGLContextDevice2D.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -871,7 +871,8 @@ void vtkOpenGLContextDevice2D::DrawString(float *point,
if (image->GetNumberOfPoints() == 0 && image->GetNumberOfCells() == 0)
{
int textDims[2];
if (!this->TextRenderer->RenderString(this->TextProp, string, image,
if (!this->TextRenderer->RenderString(this->TextProp, string,
this->RenderWindow->GetDPI(), image,
textDims))
{
return;
Expand Down Expand Up @@ -923,7 +924,8 @@ void vtkOpenGLContextDevice2D::DrawString(float *point,
void vtkOpenGLContextDevice2D::ComputeStringBounds(const vtkUnicodeString &string,
float bounds[4])
{
vtkVector2i box = this->TextRenderer->GetBounds(this->TextProp, string);
vtkVector2i box = this->TextRenderer->GetBounds(this->TextProp, string,
this->RenderWindow->GetDPI());
// Check for invalid bounding box
if (box[0] == VTK_INT_MIN || box[0] == VTK_INT_MAX ||
box[1] == VTK_INT_MIN || box[1] == VTK_INT_MAX)
Expand Down
6 changes: 4 additions & 2 deletions Rendering/ContextOpenGL2/vtkOpenGLContextDevice2D.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -1256,7 +1256,8 @@ void vtkOpenGLContextDevice2D::DrawString(float *point,
if (image->GetNumberOfPoints() == 0 && image->GetNumberOfCells() == 0)
{
int textDims[2];
if (!this->TextRenderer->RenderString(this->TextProp, string, image,
if (!this->TextRenderer->RenderString(this->TextProp, string,
this->RenderWindow->GetDPI(), image,
textDims))
{
return;
Expand Down Expand Up @@ -1317,7 +1318,8 @@ void vtkOpenGLContextDevice2D::DrawString(float *point,
void vtkOpenGLContextDevice2D::ComputeStringBounds(const vtkUnicodeString &string,
float bounds[4])
{
vtkVector2i box = this->TextRenderer->GetBounds(this->TextProp, string);
vtkVector2i box = this->TextRenderer->GetBounds(this->TextProp, string,
this->RenderWindow->GetDPI());
// Check for invalid bounding box
if (box[0] == VTK_INT_MIN || box[0] == VTK_INT_MAX ||
box[1] == VTK_INT_MIN || box[1] == VTK_INT_MAX)
Expand Down
8 changes: 7 additions & 1 deletion Rendering/Core/vtkLabeledContourMapper.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,11 @@ bool vtkLabeledContourMapper::PrepareRender(vtkRenderer *ren, vtkActor *act)
vtkCellArray *lines = input->GetLines();
vtkDataArray *scalars = input->GetPointData()->GetScalars();
vtkTextRenderer *tren = vtkTextRenderer::GetInstance();
if (!tren)
{
vtkErrorMacro(<< "Text renderer unavailable.");
return false;
}

// Maps scalar values to text properties:
typedef std::map<double, vtkTextProperty*> LabelPropertyMapType;
Expand Down Expand Up @@ -638,7 +643,8 @@ bool vtkLabeledContourMapper::PrepareRender(vtkRenderer *ren, vtkActor *act)
it->TProp = tpropIt->second;

// Assign bounding box/dims.
if (!tren->GetBoundingBox(it->TProp, it->Text, it->BoundingBox.GetData()))
if (!tren->GetBoundingBox(it->TProp, it->Text, it->BoundingBox.GetData(),
vtkTextActor3D::GetRenderedDPI()))
{
vtkErrorMacro(<<"Error calculating bounding box for string '"
<< it->Text << "'.");
Expand Down
8 changes: 4 additions & 4 deletions Rendering/Core/vtkStringToImage.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ class VTKRENDERINGCORE_EXPORT vtkStringToImage : public vtkObject
// is valid (it may not if GetBoundingBox() failed or if the string
// was empty).
virtual vtkVector2i GetBounds(vtkTextProperty *property,
const vtkUnicodeString& string) = 0;
const vtkUnicodeString& string, int dpi) = 0;
virtual vtkVector2i GetBounds(vtkTextProperty *property,
const vtkStdString& string) = 0;
const vtkStdString& string, int dpi) = 0;

// Description:
// Given a text property and a string, this function initializes the
Expand All @@ -60,11 +60,11 @@ class VTKRENDERINGCORE_EXPORT vtkStringToImage : public vtkObject
// This is useful when ScaleToPowerOfTwo is true, and the image dimensions may
// not match the dimensions of the rendered text.
virtual int RenderString(vtkTextProperty *property,
const vtkUnicodeString& string,
const vtkUnicodeString& string, int dpi,
vtkImageData *data,
int textDims[2] = NULL) = 0;
virtual int RenderString(vtkTextProperty *property,
const vtkStdString& string,
const vtkStdString& string, int dpi,
vtkImageData *data,
int text_dims[2] = NULL) = 0;

Expand Down
52 changes: 44 additions & 8 deletions Rendering/Core/vtkTextActor.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "vtkMath.h"
#include "vtkTextRenderer.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"

#include <algorithm>

Expand Down Expand Up @@ -101,6 +102,7 @@ vtkTextActor::vtkTextActor()
this->InputRendered = false;

this->FormerOrientation = 0.0;
this->RenderedDPI = 0;

this->TextRenderer = vtkTextRenderer::GetInstance();
if (!this->TextRenderer)
Expand Down Expand Up @@ -175,7 +177,8 @@ int vtkTextActor::SetConstrainedFontSize(vtkViewport* viewport,
int targetWidth,
int targetHeight)
{
return this->SetConstrainedFontSize(this, viewport, targetWidth, targetHeight);
return this->SetConstrainedFontSize(this, viewport, targetWidth,
targetHeight);
}


Expand Down Expand Up @@ -330,26 +333,43 @@ void vtkTextActor::SetNonLinearFontScale(double exp, int tgt)
}

// ----------------------------------------------------------------------------
bool vtkTextActor::RenderImage(vtkTextProperty *tprop, vtkViewport *)
bool vtkTextActor::RenderImage(vtkTextProperty *tprop, vtkViewport *vp)
{
vtkStdString text;
if (this->Input && this->Input[0])
{
text = this->Input;
}
return this->TextRenderer->RenderString(tprop, text, this->ImageData);

vtkWindow *win = vp->GetVTKWindow();
if (!win)
{
vtkErrorMacro(<<"No render window available: cannot determine DPI.");
return false;
}

return this->TextRenderer->RenderString(tprop, text, this->ImageData,
NULL, win->GetDPI());
}

// ----------------------------------------------------------------------------
bool vtkTextActor::GetImageBoundingBox(vtkTextProperty *tprop, vtkViewport *,
int bbox[4])
bool vtkTextActor::GetImageBoundingBox(vtkTextProperty *tprop, vtkViewport *vp,
int bbox[4])
{
vtkStdString text;
if (this->Input && this->Input[0])
{
text = this->Input;
}
return this->TextRenderer->GetBoundingBox(tprop, text, bbox);

vtkWindow *win = vp->GetVTKWindow();
if (!win)
{
vtkErrorMacro(<<"No render window available: cannot determine DPI.");
return false;
}

return this->TextRenderer->GetBoundingBox(tprop, text, bbox, win->GetDPI());
}

// ----------------------------------------------------------------------------
Expand Down Expand Up @@ -718,9 +738,16 @@ void vtkTextActor::ComputeScaledFont(vtkViewport *viewport)
}
int max_height = static_cast<int>(this->MaximumLineHeight * size[1]);

vtkWindow *win = viewport->GetVTKWindow();
if (!win)
{
vtkErrorMacro(<<"No render window available: cannot determine DPI.");
return;
}

int fsize = this->TextRenderer->GetConstrainedFontSize(
this->Input, this->ScaledTextProperty, size[0],
(size[1] < max_height ? size[1] : max_height));
(size[1] < max_height ? size[1] : max_height), win->GetDPI());

if (fsize == -1)
{
Expand Down Expand Up @@ -860,9 +887,17 @@ int vtkTextActor::UpdateRectangle(vtkViewport* viewport)
this->ComputeScaledFont(viewport);
}

vtkWindow *win = viewport->GetVTKWindow();
if (!win)
{
vtkErrorMacro(<<"No render window available: cannot determine DPI.");
return 0;
}

//check if we need to render the string
if(this->ScaledTextProperty->GetMTime() > this->BuildTime ||
!this->InputRendered || this->GetMTime() > this->BuildTime)
!this->InputRendered || this->GetMTime() > this->BuildTime ||
this->RenderedDPI != win->GetDPI())
{
if(!this->RenderImage(this->ScaledTextProperty, viewport))
{
Expand All @@ -879,6 +914,7 @@ int vtkTextActor::UpdateRectangle(vtkViewport* viewport)
this->Texture->SetInputData(this->ImageData);
this->Texture->Modified();
this->InputRendered = true;
this->RenderedDPI = win->GetDPI();
this->BuildTime.Modified();
}
return 1;
Expand Down
1 change: 1 addition & 0 deletions Rendering/Core/vtkTextActor.h
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ class VTKRENDERINGCORE_EXPORT vtkTextActor : public vtkTexturedActor2D
char *Input;
bool InputRendered;
double FormerOrientation;
int RenderedDPI;

vtkTextProperty *ScaledTextProperty;

Expand Down
Loading

0 comments on commit 07e728b

Please sign in to comment.