VTK
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 
46 #ifndef vtkTextRenderer_h
47 #define vtkTextRenderer_h
48 
49 #include "vtkRenderingCoreModule.h" // For export macro
50 #include "vtkObject.h"
51 #include "vtkTuple.h" // For metrics struct
52 #include "vtkVector.h" // For metrics struct
53 
54 class vtkImageData;
55 class vtkPath;
56 class vtkStdString;
57 class vtkUnicodeString;
58 class vtkTextProperty;
59 
60 namespace vtksys {
61 class RegularExpression;
62 }
63 
64 class VTKRENDERINGCORE_EXPORT vtkTextRendererCleanup
65 {
66 public:
69 
70 private:
71  vtkTextRendererCleanup(const vtkTextRendererCleanup& other) = delete;
72  vtkTextRendererCleanup& operator=(const vtkTextRendererCleanup& rhs) = delete;
73 };
74 
75 class VTKRENDERINGCORE_EXPORT vtkTextRenderer: public vtkObject
76 {
77 public:
78  struct Metrics
79  {
84  : BoundingBox(0),
85  TopLeft(0), TopRight(0), BottomLeft(0), BottomRight(0),
86  Ascent(0), Descent(0)
87  {
88  }
89 
96 
98 
107 
117  };
118 
119  vtkTypeMacro(vtkTextRenderer, vtkObject)
120  void PrintSelf(ostream &os, vtkIndent indent) override;
121 
131  static vtkTextRenderer *New();
132 
137  static vtkTextRenderer* GetInstance();
138 
144  enum Backend
145  {
146  Default = -1,
147  Detect = 0,
150 
151  UserBackend = 16
152  };
153 
155 
158  vtkSetMacro(DefaultBackend, int)
159  vtkGetMacro(DefaultBackend, int)
161 
163 
166  virtual int DetectBackend(const vtkStdString &str);
167  virtual int DetectBackend(const vtkUnicodeString &str);
169 
173  virtual bool FreeTypeIsSupported() { return false; }
174  virtual bool MathTextIsSupported() { return false; }
175 
177 
184  bool GetBoundingBox(vtkTextProperty *tprop, const vtkStdString &str,
185  int bbox[4], int dpi, int backend = Default)
186  {
187  return this->GetBoundingBoxInternal(tprop, str, bbox, dpi, backend);
188  }
190  int bbox[4], int dpi, int backend = Default)
191  {
192  return this->GetBoundingBoxInternal(tprop, str, bbox, dpi, backend);
193  }
195 
196 
198 
203  bool GetMetrics(vtkTextProperty *tprop, const vtkStdString &str,
204  Metrics &metrics, int dpi, int backend = Default)
205  {
206  return this->GetMetricsInternal(tprop, str, metrics, dpi, backend);
207  }
208  bool GetMetrics(vtkTextProperty *tprop, const vtkUnicodeString &str,
209  Metrics &metrics, int dpi, int backend = Default)
210  {
211  return this->GetMetricsInternal(tprop, str, metrics, dpi, backend);
212  }
214 
216 
230  bool RenderString(vtkTextProperty *tprop, const vtkStdString &str,
231  vtkImageData *data, int textDims[2], int dpi,
232  int backend = Default)
233  {
234  return this->RenderStringInternal(tprop, str, data, textDims, dpi, backend);
235  }
237  vtkImageData *data, int textDims[2], int dpi,
238  int backend = Default)
239  {
240  return this->RenderStringInternal(tprop, str, data, textDims, dpi, backend);
241  }
243 
245 
252  int targetWidth, int targetHeight, int dpi,
253  int backend = Default)
254  {
255  return this->GetConstrainedFontSizeInternal(str, tprop, targetWidth,
256  targetHeight, dpi, backend);
257  }
259  int targetWidth, int targetHeight, int dpi,
260  int backend = Default)
261  {
262  return this->GetConstrainedFontSizeInternal(str, tprop, targetWidth,
263  targetHeight, dpi, backend);
264  }
266 
268 
275  bool StringToPath(vtkTextProperty *tprop, const vtkStdString &str,
276  vtkPath *path, int dpi, int backend = Default)
277  {
278  return this->StringToPathInternal(tprop, str, path, dpi, backend);
279  }
281  vtkPath *path, int dpi, int backend = Default)
282  {
283  return this->StringToPathInternal(tprop, str, path, dpi, backend);
284  }
286 
294  {
295  this->SetScaleToPowerOfTwoInternal(scale);
296  }
297 
299 
300 protected:
301  vtkTextRenderer();
302  ~vtkTextRenderer() override;
303 
305 
308  virtual bool GetBoundingBoxInternal(vtkTextProperty *tprop,
309  const vtkStdString &str,
310  int bbox[4], int dpi, int backend) = 0;
311  virtual bool GetBoundingBoxInternal(vtkTextProperty *tprop,
312  const vtkUnicodeString &str,
313  int bbox[4], int dpi, int backend) = 0;
314  virtual bool GetMetricsInternal(vtkTextProperty *tprop,
315  const vtkStdString &str,
316  Metrics &metrics, int dpi, int backend) = 0;
317  virtual bool GetMetricsInternal(vtkTextProperty *tprop,
318  const vtkUnicodeString &str,
319  Metrics &metrics, int dpi, int backend) = 0;
320  virtual bool RenderStringInternal(vtkTextProperty *tprop,
321  const vtkStdString &str,
322  vtkImageData *data, int textDims[2],
323  int dpi, int backend) = 0;
324  virtual bool RenderStringInternal(vtkTextProperty *tprop,
325  const vtkUnicodeString &str,
326  vtkImageData *data, int textDims[2],
327  int dpi, int backend) = 0;
328  virtual int GetConstrainedFontSizeInternal(const vtkStdString &str,
329  vtkTextProperty *tprop,
330  int targetWidth, int targetHeight,
331  int dpi, int backend) = 0;
332  virtual int GetConstrainedFontSizeInternal(const vtkUnicodeString &str,
333  vtkTextProperty *tprop,
334  int targetWidth, int targetHeight,
335  int dpi, int backend) = 0;
336  virtual bool StringToPathInternal(vtkTextProperty *tprop,
337  const vtkStdString &str, vtkPath *path,
338  int dpi, int backend) = 0;
339  virtual bool StringToPathInternal(vtkTextProperty *tprop,
340  const vtkUnicodeString &str, vtkPath *path,
341  int dpi, int backend) = 0;
342  virtual void SetScaleToPowerOfTwoInternal(bool scale) = 0;
344 
349  static void SetInstance(vtkTextRenderer *instance);
350 
352 
358 
359  vtksys::RegularExpression *MathTextRegExp;
360  vtksys::RegularExpression *MathTextRegExp2;
361 
363 
366  virtual void CleanUpFreeTypeEscapes(vtkStdString &str);
367  virtual void CleanUpFreeTypeEscapes(vtkUnicodeString &str);
369 
374 
375 private:
376  vtkTextRenderer(const vtkTextRenderer &) = delete;
377  void operator=(const vtkTextRenderer &) = delete;
378 };
379 
380 #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:40
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:59
concrete dataset representing a path defined by Bezier curves.
Definition: vtkPath.h:35
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:39
virtual bool MathTextIsSupported()
topologically and geometrically regular array of data
Definition: vtkImageData.h:45
Some derived classes for the different vectors commonly used.
Definition: vtkVector.h:408
represent text properties.
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...
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.