File: vtkMatplotlibMathTextUtilities.h

package info (click to toggle)
paraview 5.13.2%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 544,220 kB
  • sloc: cpp: 3,374,605; ansic: 1,332,409; python: 150,381; xml: 122,166; sql: 65,887; sh: 7,317; javascript: 5,262; yacc: 4,417; java: 3,977; perl: 2,363; lex: 1,929; f90: 1,397; makefile: 170; objc: 153; tcl: 59; pascal: 50; fortran: 29
file content (246 lines) | stat: -rw-r--r-- 8,909 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
// SPDX-License-Identifier: BSD-3-Clause
/**
 * @class   vtkMatplotlibMathTextUtilities
 * @brief   Access to MatPlotLib MathText rendering
 *
 * vtkMatplotlibMathTextUtilities provides access to the MatPlotLib MathText
 * implementation.
 *
 * This class is aware of a number of environment variables that can be used to
 * configure and debug python initialization (all are optional):
 * - VTK_MATPLOTLIB_DEBUG: Enable verbose debugging output during initialization
 * of the python environment.
 *
 * This class handles rendering multiline and multicolumn strings into image data.
 * Use '\n' to define a line, and '|' to define a column.
 *
 * This class does not support rendering multiline and multicolumn strings into
 * a vtkPath.
 *
 * Example :
 *
 * str =    "$\\sum_{i=0}^\\infty x_i$ | 2 | 3 | 4 \n"
 *        +  1 | 2 | 3";
 *
 * The vertical space between two lines can be set with vtkTextProperty::SetLineSpacing and
 * vtkTextProperty::SetLineOffset
 *
 * The horizontal space between two cells can be set with vtkTextProperty::SetCellOffset
 *
 * Line separators between grid cells can also be drawn.
 */

#ifndef vtkMatplotlibMathTextUtilities_h
#define vtkMatplotlibMathTextUtilities_h

#include "vtkMathTextUtilities.h"
#include "vtkRenderingMatplotlibModule.h" // For export macro

#include <cstdint> // for std::uint64_t
#include <vector>  // for std::vector

struct _object;
typedef struct _object PyObject;

VTK_ABI_NAMESPACE_BEGIN
class vtkImageData;
class vtkPath;
class vtkPythonInterpreter;
class vtkSmartPyObject;
class vtkTextProperty;
struct TextColors;

class VTKRENDERINGMATPLOTLIB_EXPORT vtkMatplotlibMathTextUtilities : public vtkMathTextUtilities
{
public:
  vtkTypeMacro(vtkMatplotlibMathTextUtilities, vtkMathTextUtilities);
  void PrintSelf(ostream& os, vtkIndent indent) override;

  static vtkMatplotlibMathTextUtilities* New();

  bool IsAvailable() override;

  /**
   * Given a text property and a string, get the bounding box {xmin, xmax,
   * ymin, ymax} of the rendered string in pixels. The origin of the bounding
   * box is the anchor point described by the horizontal and vertical
   * justification text property variables.
   * Returns true on success, false otherwise.
   */
  bool GetBoundingBox(vtkTextProperty* tprop, const char* str, int dpi, int bbox[4]) override;

  bool GetMetrics(
    vtkTextProperty* tprop, const char* str, int dpi, vtkTextRenderer::Metrics& metrics) override;

  /**
   * Render the given string @a str into the vtkImageData @a image with a
   * resolution of @a dpi. The image is resized automatically. textDims
   * will be overwritten by the pixel width and height of the rendered string.
   * This is useful when ScaleToPowerOfTwo is true, and the image dimensions may
   * not match the dimensions of the rendered text.
   * The origin of the image's extents is aligned with the anchor point
   * described by the text property's vertical and horizontal justification
   * options.
   * This function supports multiline and multicolumn strings.
   */
  bool RenderString(const char* str, vtkImageData* image, vtkTextProperty* tprop, int dpi,
    int textDims[2] = nullptr) override;

  /**
   * Parse the MathText expression in str and fill path with a contour of the
   * glyphs. The origin of the path coordinates is aligned with the anchor point
   * described by the text property's horizontal and vertical justification
   * options.
   * This function does not support multiline and multicolumn strings.
   */
  bool StringToPath(const char* str, vtkPath* path, vtkTextProperty* tprop, int dpi) override;

  ///@{
  /**
   * Set to true if the graphics implementation requires texture image dimensions
   * to be a power of two. Default is true, but this member will be set
   * appropriately when GL is inited.
   */
  void SetScaleToPowerOfTwo(bool val) override;
  bool GetScaleToPowerOfTwo() override;
  ///@}

protected:
  vtkMatplotlibMathTextUtilities();
  ~vtkMatplotlibMathTextUtilities() override;

  bool InitializeMaskParser();
  bool InitializePathParser();
  bool InitializeFontPropertiesClass();

  bool CheckForError();
  bool CheckForError(PyObject* object);

  /**
   * Replace each occurrence of strToFind in str by replacementStr.
   * Used to protect escaped pipe before the splitting process, and recover them
   * after.
   */
  void FindAndReplaceInString(
    std::string& str, const std::string& strToFind, const std::string& replacementStr);

  static constexpr const char* PipeProtectString = "VTK_PROTECT_PIPE";

  /**
   * Compute rgba values of the foreground, background and frame
   * of the text property.
   */
  void ComputeTextColors(vtkTextProperty* tprop, TextColors& tcolors);

  /**
   * Modify matplotlib.rcParams to customize math text font.
   */
  bool SetMathTextFont(vtkTextProperty* tprop);

  /**
   * Returns a matplotlib.font_manager.FontProperties PyObject, initialized from
   * the vtkTextProperty tprop.
   */
  PyObject* GetFontProperties(vtkTextProperty* tprop);

  /**
   * Cleanup and destroy any python objects. This is called during destructor as
   * well as when the Python interpreter is finalized. Thus this class must
   * handle the case where the internal python objects disappear between calls.
   */
  void CleanupPythonObjects();

  vtkPythonInterpreter* Interpreter;
  PyObject* MaskParser;
  PyObject* PathParser;
  PyObject* FontPropertiesClass;

  static void GetJustifiedBBox(int rows, int cols, vtkTextProperty* tprop, int bbox[4]);

  // Rotate the 4 2D corner points by the specified angle (degrees) around the
  // origin and calculate the bounding box
  static void RotateCorners(double angleDeg, double corners[4][2], double bbox[4]);

  bool ScaleToPowerOfTwo;
  bool PrepareImageData(vtkImageData* data, int bbox[4]);

  std::vector<int> VerticalLinesPosition;
  std::vector<int> HorizontalLinesPosition;

private:
  vtkMatplotlibMathTextUtilities(const vtkMatplotlibMathTextUtilities&) = delete;
  void operator=(const vtkMatplotlibMathTextUtilities&) = delete;

  /**
   * Used for runtime checking of matplotlib's mathtext availability.
   * @sa IsAvailable
   */
  enum Availability
  {
    NOT_TESTED = 0,
    AVAILABLE,
    UNAVAILABLE
  };

  /**
   * Function used to check MPL availability and update MPLMathTextAvailable.
   * This will do tests only the first time this method is called. This method
   * is called internally when matplotlib rendering is first needed and is used
   * to implement IsAvailable.
   */
  static Availability CheckMPLAvailability();

  ///@{
  /**
   * Cache the availability of matplotlib in the current python session.
   */
  static Availability MPLMathTextAvailable;
  ///@}

  typedef std::vector<std::vector<std::string>> GridOfStrings;

  /**
   * Parse the string to handle multiline and multicolumn.
   * Divide it in lines (split with '\n') and cells (split with '|')
   * and store each cell string in strGrid. Also compute the maximum number of cells of all
   * lines to ensure that all lines have the same number of cells.
   */
  bool ParseString(const char* str, GridOfStrings& strGrid, std::size_t& maxNumberOfCells);

  /**
   * Given a grid of string and its corresponding maximum number of cells,
   * text property and dpi, compute the resulting number of rows and cols
   * of the image.
   * Precondition : Matplotlib rendering is available and mask parser is initialized.
   */
  bool ComputeRowsAndCols(const GridOfStrings& strGrid, const std::size_t& maxNumberOfCells,
    vtkTextProperty* tprop, PyObject* pyFontProp, int dpi, std::uint64_t& rows,
    std::uint64_t& cols);

  /**
   * Given a cell string, text property and dpi, call python mathtext to render the cell and store
   * it in list if list is not nullptr, and store in rows and cols the size of the python data.
   * Precondition : Matplotlib rendering is available and mask parser is initialized.
   */
  bool ComputeCellRowsAndCols(const char* cellStr, PyObject* pyFontProp, int dpi,
    std::uint64_t& rows, std::uint64_t& cols, vtkSmartPyObject* list);

  /**
   * Render in the image starting from (rowStart, colStart) to (rowStart + cellRows, colStart +
   * cellCols) a cell of size (pythonRows, pythonCols) with pixels value stored in pythonData. If
   * the python cell size is inferior to the cell size, fill with background color.
   */
  bool RenderOneCell(vtkImageData* image, int bbox[4], std::int64_t rowStart, std::int64_t colStart,
    vtkSmartPyObject& pythonData, std::uint64_t pythonRows, std::uint64_t pythonCols,
    std::uint64_t cellRows, std::uint64_t cellCols, vtkTextProperty* tprop,
    const TextColors& tcolors);

  /**
   * Draw interior borders between cells.
   */
  bool DrawInteriorLines(vtkImageData* image, int bbox[4], vtkTextProperty* tprop);
};

VTK_ABI_NAMESPACE_END
#endif