VTK  9.0.1
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
bool StringToPath(vtkTextProperty *tprop, const vtkUnicodeString &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 ...
Wrapper around std::string to keep symbols short.
Definition: vtkStdString.h:34
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...
abstract base class for most VTK objects
Definition: vtkObject.h:62
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
concrete dataset representing a path defined by Bezier curves.
Definition: vtkPath.h:32
vtkVector2i TopRight
The corners of the rendered text (or background, if applicable), in pixels.
Backend
Available backends.
vtkVector2i Descent
Vectors representing the rotated ascent and descent of the text.
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 ...
vtkVector2i TopLeft
The corners of the rendered text (or background, if applicable), in pixels.
bool GetMetrics(vtkTextProperty *tprop, const vtkUnicodeString &str, Metrics &metrics, int dpi, int backend=Default)
Given a text property and a string, get some metrics for the rendered string.
static vtkTextRendererCleanup Cleanup
The singleton instance and the singleton cleanup instance.
vtksys::RegularExpression * MathTextRegExp2
void SetScaleToPowerOfTwo(bool scale)
Set to true if the graphics implementation requires texture image dimensions to be a power of two...
int DefaultBackend
The backend to use when none is specified.
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...
vtkTuple< int, 4 > BoundingBox
The axis-aligned bounding box of the rendered text and background, in pixels.
bool RenderString(vtkTextProperty *tprop, const vtkUnicodeString &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...
a simple class to control print indentation
Definition: vtkIndent.h:33
virtual bool MathTextIsSupported()
topologically and geometrically regular array of data
Definition: vtkImageData.h:41
Some derived classes for the different vectors commonly used.
Definition: vtkVector.h:419
represent text properties.
virtual bool FreeTypeIsSupported()
Test for availability of various backends.
Metrics()
Construct a Metrics object with all members initialized to 0.
Interface for generating images and path data from string data, using multiple backends.
vtkVector2i Ascent
Vectors representing the rotated ascent and descent of the text.
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.
int GetConstrainedFontSize(const vtkUnicodeString &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...
vtkVector2i BottomLeft
The corners of the rendered text (or background, if applicable), in pixels.
bool GetBoundingBox(vtkTextProperty *tprop, const vtkUnicodeString &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...
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...
static vtkObject * New()
Create an object with Debug turned off, modified time initialized to zero, and reference counting on...
vtksys::RegularExpression * MathTextRegExp
vtkVector2i BottomRight
The corners of the rendered text (or background, if applicable), in pixels.
String class that stores Unicode text.
static vtkTextRenderer * Instance
The singleton instance and the singleton cleanup instance.