VTK  9.3.1
vtkMatplotlibMathTextUtilities.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2 // SPDX-License-Identifier: BSD-3-Clause
34 #ifndef vtkMatplotlibMathTextUtilities_h
35 #define vtkMatplotlibMathTextUtilities_h
36 
37 #include "vtkMathTextUtilities.h"
38 #include "vtkRenderingMatplotlibModule.h" // For export macro
39 
40 #include <cstdint> // for std::uint64_t
41 #include <vector> // for std::vector
42 
43 struct _object;
44 typedef struct _object PyObject;
45 
46 VTK_ABI_NAMESPACE_BEGIN
47 class vtkImageData;
48 class vtkPath;
49 class vtkPythonInterpreter;
50 class vtkSmartPyObject;
51 class vtkTextProperty;
52 struct TextColors;
53 
54 class VTKRENDERINGMATPLOTLIB_EXPORT vtkMatplotlibMathTextUtilities : public vtkMathTextUtilities
55 {
56 public:
58  void PrintSelf(ostream& os, vtkIndent indent) override;
59 
61 
62  bool IsAvailable() override;
63 
71  bool GetBoundingBox(vtkTextProperty* tprop, const char* str, int dpi, int bbox[4]) override;
72 
73  bool GetMetrics(
74  vtkTextProperty* tprop, const char* str, int dpi, vtkTextRenderer::Metrics& metrics) override;
75 
87  bool RenderString(const char* str, vtkImageData* image, vtkTextProperty* tprop, int dpi,
88  int textDims[2] = nullptr) override;
89 
97  bool StringToPath(const char* str, vtkPath* path, vtkTextProperty* tprop, int dpi) override;
98 
100 
105  void SetScaleToPowerOfTwo(bool val) override;
106  bool GetScaleToPowerOfTwo() override;
108 
109 protected:
111  ~vtkMatplotlibMathTextUtilities() override;
112 
113  bool InitializeMaskParser();
114  bool InitializePathParser();
115  bool InitializeFontPropertiesClass();
116 
117  bool CheckForError();
118  bool CheckForError(PyObject* object);
119 
125  void FindAndReplaceInString(
126  std::string& str, const std::string& strToFind, const std::string& replacementStr);
127 
128  static constexpr const char* PipeProtectString = "VTK_PROTECT_PIPE";
129 
134  void ComputeTextColors(vtkTextProperty* tprop, TextColors& tcolors);
135 
139  bool SetMathTextFont(vtkTextProperty* tprop);
140 
145  PyObject* GetFontProperties(vtkTextProperty* tprop);
146 
152  void CleanupPythonObjects();
153 
154  vtkPythonInterpreter* Interpreter;
158 
159  static void GetJustifiedBBox(int rows, int cols, vtkTextProperty* tprop, int bbox[4]);
160 
161  // Rotate the 4 2D corner points by the specified angle (degrees) around the
162  // origin and calculate the bounding box
163  static void RotateCorners(double angleDeg, double corners[4][2], double bbox[4]);
164 
166  bool PrepareImageData(vtkImageData* data, int bbox[4]);
167 
168  std::vector<int> VerticalLinesPosition;
169  std::vector<int> HorizontalLinesPosition;
170 
171 private:
173  void operator=(const vtkMatplotlibMathTextUtilities&) = delete;
174 
179  enum Availability
180  {
181  NOT_TESTED = 0,
182  AVAILABLE,
183  UNAVAILABLE
184  };
185 
192  static Availability CheckMPLAvailability();
193 
195 
198  static Availability MPLMathTextAvailable;
200 
201  typedef std::vector<std::vector<std::string>> GridOfStrings;
202 
209  bool ParseString(const char* str, GridOfStrings& strGrid, std::size_t& maxNumberOfCells);
210 
217  bool ComputeRowsAndCols(const GridOfStrings& strGrid, const std::size_t& maxNumberOfCells,
218  vtkTextProperty* tprop, PyObject* pyFontProp, int dpi, std::uint64_t& rows,
219  std::uint64_t& cols);
220 
226  bool ComputeCellRowsAndCols(const char* cellStr, PyObject* pyFontProp, int dpi,
227  std::uint64_t& rows, std::uint64_t& cols, vtkSmartPyObject* list);
228 
234  bool RenderOneCell(vtkImageData* image, int bbox[4], std::int64_t rowStart, std::int64_t colStart,
235  vtkSmartPyObject& pythonData, std::uint64_t pythonRows, std::uint64_t pythonCols,
236  std::uint64_t cellRows, std::uint64_t cellCols, vtkTextProperty* tprop,
237  const TextColors& tcolors);
238 
242  bool DrawInteriorLines(vtkImageData* image, int bbox[4], vtkTextProperty* tprop);
243 };
244 
245 VTK_ABI_NAMESPACE_END
246 #endif
virtual bool IsAvailable()
Returns true if mathtext rendering is available.
concrete dataset representing a path defined by Bezier curves.
Definition: vtkPath.h:24
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:28
topologically and geometrically regular array of data
Definition: vtkImageData.h:42
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...