VTK  9.2.6
vtkMatplotlibMathTextUtilities.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: vtkMatplotlibMathTextUtilities.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 =========================================================================*/
46 #ifndef vtkMatplotlibMathTextUtilities_h
47 #define vtkMatplotlibMathTextUtilities_h
48 
49 #include "vtkMathTextUtilities.h"
50 #include "vtkRenderingMatplotlibModule.h" // For export macro
51 
52 #include <vector> // for std::vector
53 
54 struct _object;
55 typedef struct _object PyObject;
56 class vtkSmartPyObject;
57 class vtkImageData;
58 class vtkPath;
59 class vtkPythonInterpreter;
60 class vtkTextProperty;
61 struct TextColors;
62 
63 class VTKRENDERINGMATPLOTLIB_EXPORT vtkMatplotlibMathTextUtilities : public vtkMathTextUtilities
64 {
65 public:
67  void PrintSelf(ostream& os, vtkIndent indent) override;
68 
70 
71  bool IsAvailable() override;
72 
80  bool GetBoundingBox(vtkTextProperty* tprop, const char* str, int dpi, int bbox[4]) override;
81 
82  bool GetMetrics(
83  vtkTextProperty* tprop, const char* str, int dpi, vtkTextRenderer::Metrics& metrics) override;
84 
96  bool RenderString(const char* str, vtkImageData* image, vtkTextProperty* tprop, int dpi,
97  int textDims[2] = nullptr) override;
98 
106  bool StringToPath(const char* str, vtkPath* path, vtkTextProperty* tprop, int dpi) override;
107 
109 
114  void SetScaleToPowerOfTwo(bool val) override;
115  bool GetScaleToPowerOfTwo() override;
117 
118 protected:
120  ~vtkMatplotlibMathTextUtilities() override;
121 
122  bool InitializeMaskParser();
123  bool InitializePathParser();
124  bool InitializeFontPropertiesClass();
125 
126  bool CheckForError();
127  bool CheckForError(PyObject* object);
128 
134  void FindAndReplaceInString(
135  std::string& str, const std::string& strToFind, const std::string& replacementStr);
136 
137  static constexpr const char* PipeProtectString = "VTK_PROTECT_PIPE";
138 
143  void ComputeTextColors(vtkTextProperty* tprop, TextColors& tcolors);
144 
148  bool SetMathTextFont(vtkTextProperty* tprop);
149 
154  PyObject* GetFontProperties(vtkTextProperty* tprop);
155 
161  void CleanupPythonObjects();
162 
163  vtkPythonInterpreter* Interpreter;
167 
168  static void GetJustifiedBBox(int rows, int cols, vtkTextProperty* tprop, int bbox[4]);
169 
170  // Rotate the 4 2D corner points by the specified angle (degrees) around the
171  // origin and calculate the bounding box
172  static void RotateCorners(double angleDeg, double corners[4][2], double bbox[4]);
173 
175  bool PrepareImageData(vtkImageData* data, int bbox[4]);
176 
177  std::vector<int> VerticalLinesPosition;
178  std::vector<int> HorizontalLinesPosition;
179 
180 private:
182  void operator=(const vtkMatplotlibMathTextUtilities&) = delete;
183 
188  enum Availability
189  {
190  NOT_TESTED = 0,
191  AVAILABLE,
192  UNAVAILABLE
193  };
194 
201  static Availability CheckMPLAvailability();
202 
204 
207  static Availability MPLMathTextAvailable;
209 
210  typedef std::vector<std::vector<std::string>> GridOfStrings;
211 
218  bool ParseString(const char* str, GridOfStrings& strGrid, std::size_t& maxNumberOfCells);
219 
226  bool ComputeRowsAndCols(const GridOfStrings& strGrid, const std::size_t& maxNumberOfCells,
227  vtkTextProperty* tprop, PyObject* pyFontProp, int dpi, std::uint64_t& rows,
228  std::uint64_t& cols);
229 
235  bool ComputeCellRowsAndCols(const char* cellStr, PyObject* pyFontProp, int dpi,
236  std::uint64_t& rows, std::uint64_t& cols, vtkSmartPyObject* list);
237 
243  bool RenderOneCell(vtkImageData* image, int bbox[4], const std::int64_t rowStart,
244  const std::int64_t colStart, vtkSmartPyObject& pythonData, const std::uint64_t pythonRows,
245  const std::uint64_t pythonCols, const std::uint64_t cellRows, const std::uint64_t cellCols,
246  vtkTextProperty* tprop, const TextColors& tcolors);
247 
251  bool DrawInteriorLines(vtkImageData* image, int bbox[4], vtkTextProperty* tprop);
252 };
253 
254 #endif
virtual bool IsAvailable()
Returns true if mathtext rendering is available.
concrete dataset representing a path defined by Bezier curves.
Definition: vtkPath.h:35
virtual bool GetMetrics(vtkTextProperty *tprop, const char *str, int dpi, vtkTextRenderer::Metrics &metrics)=0
Return the metrics for the rendered str, tprop, and dpi.
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
Access to MatPlotLib MathText rendering.
virtual bool StringToPath(const char *str, vtkPath *path, vtkTextProperty *tprop, int dpi)=0
Parse the MathText expression in str and fill path with a contour of the glyphs.
virtual bool GetBoundingBox(vtkTextProperty *tprop, const char *str, int dpi, int bbox[4])=0
Determine the dimensions of the image that RenderString will produce for a given str, tprop, and dpi.
a simple class to control print indentation
Definition: vtkIndent.h:39
topologically and geometrically regular array of data
Definition: vtkImageData.h:53
represent text properties.
virtual bool RenderString(const char *str, vtkImageData *data, vtkTextProperty *tprop, int dpi, int textDims[2]=nullptr)=0
Render the given string str into the vtkImageData data with a resolution of dpi.
virtual bool GetScaleToPowerOfTwo()=0
Set to true if the graphics implementation requires texture image dimensions to be a power of two...
Abstract interface to equation rendering.
static vtkMathTextUtilities * New()
This is a singleton pattern New.
struct _object PyObject
virtual void SetScaleToPowerOfTwo(bool scale)=0
Set to true if the graphics implementation requires texture image dimensions to be a power of two...