VTK  9.0.20200929
vtkTextRenderer.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: vtkTextRenderer.h
5 
6  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7  All rights reserved.
8  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10  This software is distributed WITHOUT ANY WARRANTY; without even
11  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12  PURPOSE. See the above copyright notice for more information.
13 
14 =========================================================================*/
15 
43 #ifndef vtkTextRenderer_h
44 #define vtkTextRenderer_h
45 
46 #include "vtkObject.h"
47 #include "vtkRenderingCoreModule.h" // For export macro
48 #include "vtkTuple.h" // For metrics struct
49 #include "vtkVector.h" // For metrics struct
50 
51 class vtkImageData;
52 class vtkPath;
53 class vtkStdString;
54 class vtkUnicodeString;
55 class vtkTextProperty;
56 
57 namespace vtksys
58 {
59 class RegularExpression;
60 }
61 
62 class VTKRENDERINGCORE_EXPORT vtkTextRendererCleanup
63 {
64 public:
67 
68 private:
69  vtkTextRendererCleanup(const vtkTextRendererCleanup& other) = delete;
70  vtkTextRendererCleanup& operator=(const vtkTextRendererCleanup& rhs) = delete;
71 };
72 
73 class VTKRENDERINGCORE_EXPORT vtkTextRenderer : public vtkObject
74 {
75 public:
76  struct Metrics
77  {
82  : BoundingBox(0)
83  , TopLeft(0)
84  , TopRight(0)
85  , BottomLeft(0)
86  , BottomRight(0)
87  , Ascent(0)
88  , Descent(0)
89  {
90  }
91 
98 
100 
109 
119  };
120 
121  vtkTypeMacro(vtkTextRenderer, vtkObject);
122  void PrintSelf(ostream& os, vtkIndent indent) override;
123 
133  static vtkTextRenderer* New();
134 
139  static vtkTextRenderer* GetInstance();
140 
146  enum Backend
147  {
148  Default = -1,
149  Detect = 0,
152 
153  UserBackend = 16
154  };
155 
157 
160  vtkSetMacro(DefaultBackend, int);
161  vtkGetMacro(DefaultBackend, int);
163 
165 
168  virtual int DetectBackend(const vtkStdString& str);
169  virtual int DetectBackend(const vtkUnicodeString& str);
171 
175  virtual bool FreeTypeIsSupported() { return false; }
176  virtual bool MathTextIsSupported() { return false; }
177 
179 
187  vtkTextProperty* tprop, const vtkStdString& str, int bbox[4], int dpi, int backend = Default)
188  {
189  return this->GetBoundingBoxInternal(tprop, str, bbox, dpi, backend);
190  }
191  bool GetBoundingBox(vtkTextProperty* tprop, const vtkUnicodeString& str, int bbox[4], int dpi,
192  int backend = Default)
193  {
194  return this->GetBoundingBoxInternal(tprop, str, bbox, dpi, backend);
195  }
197 
199 
204  bool GetMetrics(vtkTextProperty* tprop, const vtkStdString& str, Metrics& metrics, int dpi,
205  int backend = Default)
206  {
207  return this->GetMetricsInternal(tprop, str, metrics, dpi, backend);
208  }
209  bool GetMetrics(vtkTextProperty* tprop, const vtkUnicodeString& str, Metrics& metrics, int dpi,
210  int backend = Default)
211  {
212  return this->GetMetricsInternal(tprop, str, metrics, dpi, backend);
213  }
215 
217 
232  int textDims[2], int dpi, int backend = Default)
233  {
234  return this->RenderStringInternal(tprop, str, data, textDims, dpi, backend);
235  }
237  int textDims[2], int dpi, int backend = Default)
238  {
239  return this->RenderStringInternal(tprop, str, data, textDims, dpi, backend);
240  }
242 
244 
250  int GetConstrainedFontSize(const vtkStdString& str, vtkTextProperty* tprop, int targetWidth,
251  int targetHeight, int dpi, int backend = Default)
252  {
253  return this->GetConstrainedFontSizeInternal(
254  str, tprop, targetWidth, targetHeight, dpi, backend);
255  }
256  int GetConstrainedFontSize(const vtkUnicodeString& str, vtkTextProperty* tprop, int targetWidth,
257  int targetHeight, int dpi, int backend = Default)
258  {
259  return this->GetConstrainedFontSizeInternal(
260  str, tprop, targetWidth, targetHeight, dpi, backend);
261  }
263 
265 
273  vtkTextProperty* tprop, const vtkStdString& str, vtkPath* path, int dpi, int backend = Default)
274  {
275  return this->StringToPathInternal(tprop, str, path, dpi, backend);
276  }
277  bool StringToPath(vtkTextProperty* tprop, const vtkUnicodeString& str, vtkPath* path, int dpi,
278  int backend = Default)
279  {
280  return this->StringToPathInternal(tprop, str, path, dpi, backend);
281  }
283 
290  void SetScaleToPowerOfTwo(bool scale) { this->SetScaleToPowerOfTwoInternal(scale); }
291 
293 
294 protected:
295  vtkTextRenderer();
296  ~vtkTextRenderer() override;
297 
299 
302  virtual bool GetBoundingBoxInternal(
303  vtkTextProperty* tprop, const vtkStdString& str, int bbox[4], int dpi, int backend) = 0;
304  virtual bool GetBoundingBoxInternal(
305  vtkTextProperty* tprop, const vtkUnicodeString& str, int bbox[4], int dpi, int backend) = 0;
306  virtual bool GetMetricsInternal(
307  vtkTextProperty* tprop, const vtkStdString& str, Metrics& metrics, int dpi, int backend) = 0;
308  virtual bool GetMetricsInternal(vtkTextProperty* tprop, const vtkUnicodeString& str,
309  Metrics& metrics, int dpi, int backend) = 0;
310  virtual bool RenderStringInternal(vtkTextProperty* tprop, const vtkStdString& str,
311  vtkImageData* data, int textDims[2], int dpi, int backend) = 0;
312  virtual bool RenderStringInternal(vtkTextProperty* tprop, const vtkUnicodeString& str,
313  vtkImageData* data, int textDims[2], int dpi, int backend) = 0;
314  virtual int GetConstrainedFontSizeInternal(const vtkStdString& str, vtkTextProperty* tprop,
315  int targetWidth, int targetHeight, int dpi, int backend) = 0;
316  virtual int GetConstrainedFontSizeInternal(const vtkUnicodeString& str, vtkTextProperty* tprop,
317  int targetWidth, int targetHeight, int dpi, int backend) = 0;
318  virtual bool StringToPathInternal(
319  vtkTextProperty* tprop, const vtkStdString& str, vtkPath* path, int dpi, int backend) = 0;
320  virtual bool StringToPathInternal(
321  vtkTextProperty* tprop, const vtkUnicodeString& str, vtkPath* path, int dpi, int backend) = 0;
322  virtual void SetScaleToPowerOfTwoInternal(bool scale) = 0;
324 
329  static void SetInstance(vtkTextRenderer* instance);
330 
332 
338 
339  vtksys::RegularExpression* MathTextRegExp;
340  vtksys::RegularExpression* MathTextRegExp2;
341 
343 
346  virtual void CleanUpFreeTypeEscapes(vtkStdString& str);
347  virtual void CleanUpFreeTypeEscapes(vtkUnicodeString& str);
349 
354 
355 private:
356  vtkTextRenderer(const vtkTextRenderer&) = delete;
357  void operator=(const vtkTextRenderer&) = delete;
358 };
359 
360 #endif // vtkTextRenderer_h
vtkTextRendererCleanup
Definition: vtkTextRenderer.h:62
vtkTextRenderer::Backend
Backend
Available backends.
Definition: vtkTextRenderer.h:146
vtkTextRenderer::SetScaleToPowerOfTwo
void SetScaleToPowerOfTwo(bool scale)
Set to true if the graphics implementation requires texture image dimensions to be a power of two.
Definition: vtkTextRenderer.h:290
vtkX3D::scale
Definition: vtkX3D.h:235
vtkTextRenderer::Metrics::Metrics
Metrics()
Construct a Metrics object with all members initialized to 0.
Definition: vtkTextRenderer.h:81
vtkTextRenderer::GetConstrainedFontSize
int GetConstrainedFontSize(const vtkStdString &str, vtkTextProperty *tprop, int targetWidth, int targetHeight, int dpi, int backend=Default)
This function returns the font size (in points) and sets the size in tprop that is required to fit th...
Definition: vtkTextRenderer.h:250
vtkTextRenderer::Cleanup
static vtkTextRendererCleanup Cleanup
Definition: vtkTextRenderer.h:336
vtkX3D::data
Definition: vtkX3D.h:321
vtkObject::New
static vtkObject * New()
Create an object with Debug turned off, modified time initialized to zero, and reference counting on.
vtkTextRenderer::MathTextIsSupported
virtual bool MathTextIsSupported()
Definition: vtkTextRenderer.h:176
vtkPath
concrete dataset representing a path defined by Bezier curves.
Definition: vtkPath.h:32
vtkObject
abstract base class for most VTK objects
Definition: vtkObject.h:62
vtkTextRenderer::Instance
static vtkTextRenderer * Instance
The singleton instance and the singleton cleanup instance.
Definition: vtkTextRenderer.h:335
vtkVector.h
vtkTextRenderer::Metrics::TopLeft
vtkVector2i TopLeft
The corners of the rendered text (or background, if applicable), in pixels.
Definition: vtkTextRenderer.h:104
vtkTextRenderer::MathTextRegExp
vtksys::RegularExpression * MathTextRegExp
Definition: vtkTextRenderer.h:339
vtkTextRenderer::GetBoundingBox
bool GetBoundingBox(vtkTextProperty *tprop, const vtkStdString &str, int bbox[4], int dpi, int backend=Default)
Given a text property and a string, get the bounding box {xmin, xmax, ymin, ymax} of the rendered str...
Definition: vtkTextRenderer.h:186
vtkTextRenderer::GetMetrics
bool GetMetrics(vtkTextProperty *tprop, const vtkUnicodeString &str, Metrics &metrics, int dpi, int backend=Default)
Definition: vtkTextRenderer.h:209
vtkTextRenderer::FreeTypeIsSupported
virtual bool FreeTypeIsSupported()
Test for availability of various backends.
Definition: vtkTextRenderer.h:175
vtkTextRenderer::StringToPath
bool StringToPath(vtkTextProperty *tprop, const vtkUnicodeString &str, vtkPath *path, int dpi, int backend=Default)
Definition: vtkTextRenderer.h:277
vtkTextRenderer::Metrics::BottomRight
vtkVector2i BottomRight
Definition: vtkTextRenderer.h:107
vtkTextRenderer::MathText
Definition: vtkTextRenderer.h:151
vtksys
Definition: vtkVASPAnimationReader.h:31
vtkTextRenderer::MathTextRegExp2
vtksys::RegularExpression * MathTextRegExp2
Definition: vtkTextRenderer.h:340
vtkTextRenderer::DefaultBackend
int DefaultBackend
The backend to use when none is specified.
Definition: vtkTextRenderer.h:353
vtkTextRenderer::Metrics::Ascent
vtkVector2i Ascent
Vectors representing the rotated ascent and descent of the text.
Definition: vtkTextRenderer.h:116
vtkTextRenderer::GetBoundingBox
bool GetBoundingBox(vtkTextProperty *tprop, const vtkUnicodeString &str, int bbox[4], int dpi, int backend=Default)
Definition: vtkTextRenderer.h:191
vtkImageData
topologically and geometrically regular array of data
Definition: vtkImageData.h:47
vtkIndent
a simple class to control print indentation
Definition: vtkIndent.h:33
vtkUnicodeString
String class that stores Unicode text.
Definition: vtkUnicodeString.h:69
vtkTextRenderer::Metrics::Descent
vtkVector2i Descent
Vectors representing the rotated ascent and descent of the text.
Definition: vtkTextRenderer.h:117
vtkTextRenderer::RenderString
bool RenderString(vtkTextProperty *tprop, const vtkUnicodeString &str, vtkImageData *data, int textDims[2], int dpi, int backend=Default)
Definition: vtkTextRenderer.h:236
vtkObject::PrintSelf
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
vtkTextRenderer::Metrics::BoundingBox
vtkTuple< int, 4 > BoundingBox
The axis-aligned bounding box of the rendered text and background, in pixels.
Definition: vtkTextRenderer.h:97
vtkTextRenderer::GetMetrics
bool GetMetrics(vtkTextProperty *tprop, const vtkStdString &str, Metrics &metrics, int dpi, int backend=Default)
Given a text property and a string, get some metrics for the rendered string.
Definition: vtkTextRenderer.h:204
vtkTextRenderer::StringToPath
bool StringToPath(vtkTextProperty *tprop, const vtkStdString &str, vtkPath *path, int dpi, int backend=Default)
Given a text property and a string, this function populates the vtkPath path with the outline of the ...
Definition: vtkTextRenderer.h:272
vtkObject.h
vtkTextProperty
represent text properties.
Definition: vtkTextProperty.h:33
vtkTextRenderer::Metrics::BottomLeft
vtkVector2i BottomLeft
Definition: vtkTextRenderer.h:106
vtkTuple.h
vtkTextRenderer::Metrics::TopRight
vtkVector2i TopRight
Definition: vtkTextRenderer.h:105
vtkTextRenderer
Interface for generating images and path data from string data, using multiple backends.
Definition: vtkTextRenderer.h:73
vtkVector2i
Some derived classes for the different vectors commonly used.
Definition: vtkVector.h:419
vtkStdString
Wrapper around std::string to keep symbols short.
Definition: vtkStdString.h:34
vtkTextRenderer::GetConstrainedFontSize
int GetConstrainedFontSize(const vtkUnicodeString &str, vtkTextProperty *tprop, int targetWidth, int targetHeight, int dpi, int backend=Default)
Definition: vtkTextRenderer.h:256
vtkTuple< int, 4 >
vtkTextRenderer::Metrics
Definition: vtkTextRenderer.h:76
vtkTextRenderer::FreeType
Definition: vtkTextRenderer.h:150
vtkTextRenderer::RenderString
bool RenderString(vtkTextProperty *tprop, const vtkStdString &str, vtkImageData *data, int textDims[2], int dpi, int backend=Default)
Given a text property and a string, this function initializes the vtkImageData *data and renders it i...
Definition: vtkTextRenderer.h:231