VTK
vtkSVGContextDevice2D.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: vtkSVGContextDevice2D.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 =========================================================================*/
39 #ifndef vtkSVGContextDevice2D_h
40 #define vtkSVGContextDevice2D_h
41 
42 #include "vtkIOExportModule.h" // For export macro
43 #include "vtkContextDevice2D.h"
44 #include "vtkNew.h" // For vtkNew!
45 
46 #include <array> // For std::array!
47 
48 class vtkColor3ub;
49 class vtkColor4ub;
50 class vtkPath;
51 class vtkRenderer;
52 class vtkTransform;
53 class vtkVector3f;
54 class vtkXMLDataElement;
55 
56 class VTKIOEXPORT_EXPORT vtkSVGContextDevice2D: public vtkContextDevice2D
57 {
58 public:
59  static vtkSVGContextDevice2D* New();
61  void PrintSelf(ostream &os, vtkIndent indent) override;
62 
65  void SetSVGContext(vtkXMLDataElement *context,
66  vtkXMLDataElement *defs);
67 
78  vtkSetMacro(EmbedFonts, bool)
79  vtkGetMacro(EmbedFonts, bool)
80  vtkBooleanMacro(EmbedFonts, bool)
98  vtkSetMacro(TextAsPath, bool)
99  vtkGetMacro(TextAsPath, bool)
100  vtkBooleanMacro(TextAsPath, bool)
118  vtkSetMacro(SubdivisionThreshold, float)
119  vtkGetMacro(SubdivisionThreshold, float)
126  void GenerateDefinitions();
127 
128  void Begin(vtkViewport*) override;
129  void End() override;
130 
131  void DrawPoly(float *points, int n, unsigned char *colors = nullptr,
132  int nc_comps = 0) override;
133  void DrawLines(float *f, int n, unsigned char *colors = nullptr,
134  int nc_comps = 0) override;
135  void DrawPoints(float *points, int n, unsigned char* colors = nullptr,
136  int nc_comps = 0) override;
137  void DrawPointSprites(vtkImageData *sprite, float *points, int n,
138  unsigned char *colors = nullptr,
139  int nc_comps = 0) override;
140  void DrawMarkers(int shape, bool highlight, float *points, int n,
141  unsigned char *colors = nullptr, int nc_comps = 0) override;
142  void DrawQuad(float *, int) override;
143  void DrawQuadStrip(float *, int) override;
144  void DrawPolygon(float *, int) override;
145  void DrawColoredPolygon(float *points, int numPoints,
146  unsigned char *colors = nullptr,
147  int nc_comps = 0) override;
148  void DrawEllipseWedge(float x, float y, float outRx, float outRy,
149  float inRx, float inRy, float startAngle,
150  float stopAngle) override;
151  void DrawEllipticArc(float x, float y, float rX, float rY,
152  float startAngle, float stopAngle) override;
153  void DrawString(float *point, const vtkStdString &string) override;
154  void ComputeStringBounds(const vtkStdString &string,
155  float bounds[4]) override;
156  void DrawString(float *point, const vtkUnicodeString &string) override;
157  void ComputeStringBounds(const vtkUnicodeString &string,
158  float bounds[4]) override;
159  void ComputeJustifiedStringBounds(const char* string,
160  float bounds[4]) override;
161  void DrawMathTextString(float *point, const vtkStdString &str) override;
162  void DrawImage(float p[2], float scale, vtkImageData *image) override;
163  void DrawImage(const vtkRectf& pos, vtkImageData *image) override;
164  void SetColor4(unsigned char color[4]) override;
165  void SetTexture(vtkImageData* image, int properties) override;
166  void SetPointSize(float size) override;
167  void SetLineWidth(float width) override;
168 
169  void SetLineType(int type) override;
170  void SetMatrix(vtkMatrix3x3 *m) override;
171  void GetMatrix(vtkMatrix3x3 *m) override;
172  void MultiplyMatrix(vtkMatrix3x3 *m) override;
173  void PushMatrix() override;
174  void PopMatrix() override;
175  void SetClipping(int *x) override;
176  void EnableClipping(bool enable) override;
177 
178 protected:
180  ~vtkSVGContextDevice2D() override;
181 
182  void SetViewport(vtkViewport*);
183 
184  void PushGraphicsState();
185  void PopGraphicsState();
186 
187  // Apply clipping and transform information current active node.
188  void SetupClippingAndTransform();
189 
190  // pen -> stroke state
191  void ApplyPenStateToNode(vtkXMLDataElement *node);
192  void ApplyPenColorToNode(vtkXMLDataElement *node);
193  void ApplyPenOpacityToNode(vtkXMLDataElement *node);
194  void ApplyPenWidthToNode(vtkXMLDataElement *node);
195  void ApplyPenStippleToNode(vtkXMLDataElement *node);
196 
197  // pen -> fill state
198  void ApplyPenAsFillColorToNode(vtkXMLDataElement *node);
199  void ApplyPenAsFillOpacityToNode(vtkXMLDataElement *node);
200 
201  // brush -> fill state
202  void ApplyBrushStateToNode(vtkXMLDataElement *node);
203  void ApplyBrushColorToNode(vtkXMLDataElement *node);
204  void ApplyBrushOpacityToNode(vtkXMLDataElement *node);
205  void ApplyBrushTextureToNode(vtkXMLDataElement *node);
206 
207  // tprop --> text state
208  void ApplyTextPropertyStateToNode(vtkXMLDataElement *node, float x, float y);
209  void ApplyTextPropertyStateToNodeForPath(vtkXMLDataElement *node,
210  float x, float y);
211 
212  void ApplyTransform();
213 
214  // Add marker symbols to defs, return symbol id.
215  std::string AddCrossSymbol(bool highlight);
216  std::string AddPlusSymbol(bool highlight);
217  std::string AddSquareSymbol(bool highlight);
218  std::string AddCircleSymbol(bool highlight);
219  std::string AddDiamondSymbol(bool highlight);
220 
221  void DrawPath(vtkPath *path, std::ostream &out);
222 
223  void DrawLineGradient(const vtkVector2f &p1, const vtkColor4ub &c1,
224  const vtkVector2f &p2, const vtkColor4ub &c2,
225  bool useAlpha);
226  void DrawTriangleGradient(const vtkVector2f &p1, const vtkColor4ub &c1,
227  const vtkVector2f &p2, const vtkColor4ub &c2,
228  const vtkVector2f &p3, const vtkColor4ub &c3,
229  bool useAlpha);
230 
231  // Used by the Draw*Gradient methods to prevent subdividing triangles / lines
232  // that are already really small.
233  bool AreaLessThanTolerance(const vtkVector2f &p1,
234  const vtkVector2f &p2,
235  const vtkVector2f &p3);
236  bool LengthLessThanTolerance(const vtkVector2f &p1,
237  const vtkVector2f &p2);
238 
239  bool ColorsAreClose(const vtkColor4ub &c1, const vtkColor4ub &c2,
240  bool useAlpha);
241  bool ColorsAreClose(const vtkColor4ub &c1, const vtkColor4ub &c2,
242  const vtkColor4ub &c3, bool useAlpha);
243 
244  void WriteFonts();
245  void WriteImages();
246  void WritePatterns();
247  void WriteClipRects();
248 
249  void AdjustMatrixForSVG(const double in[9], double out[9]);
250  void GetSVGMatrix(double svg[9]);
251  static bool Transform2DEqual(const double mat3[9], const double mat4[16]);
252  static void Matrix3ToMatrix4(const double mat3[9], double mat4[16]);
253  static void Matrix4ToMatrix3(const double mat4[16], double mat3[9]);
254 
255  float GetScaledPenWidth();
256  void GetScaledPenWidth(float &x, float &y);
257  void TransformSize(float &x, float &y);
258 
259  vtkImageData* PreparePointSprite(vtkImageData *in);
260 
261  struct Details;
262  Details *Impl;
263 
268 
269  // This is a 3D transform, the 2D version doesn't support push/pop.
271  std::array<double, 9> ActiveNodeTransform;
272 
273  std::array<int, 4> ClipRect; // x, y, w, h
274  std::array<int, 4> ActiveNodeClipRect; // x, y, w, h
275 
276  float CanvasHeight; // Used in y coordinate conversions.
282 
283 private:
285  void operator=(const vtkSVGContextDevice2D&) = delete;
286 };
287 
288 #endif // vtkSVGContextDevice2D_h
Wrapper around std::string to keep symbols short.
Definition: vtkStdString.h:34
virtual void SetMatrix(vtkMatrix3x3 *m)=0
Set the model view matrix for the display.
virtual void DrawPointSprites(vtkImageData *sprite, float *points, int n, unsigned char *colors=nullptr, int nc_comps=0)=0
Draw a series of point sprites, images centred at the points supplied.
virtual void SetClipping(int *x)=0
Supply a float array of length 4 with x1, y1, width, height specifying clipping region for the device...
Represents an XML element and those nested inside.
virtual void ComputeJustifiedStringBounds(const char *string, float bounds[4])=0
Compute the bounds of the supplied string while taking into account the justification of the currentl...
virtual void DrawPoly(float *points, int n, unsigned char *colors=nullptr, int nc_comps=0)=0
Draw a poly line using the points - fastest code path due to memory layout of the coordinates...
concrete dataset representing a path defined by Bezier curves.
Definition: vtkPath.h:32
abstract specification for Viewports
Definition: vtkViewport.h:44
virtual void DrawPolygon(float *p, int n)
Draw a polygon using the specified number of points.
std::array< int, 4 > ActiveNodeClipRect
vtkContextDevice2D implementation for use with vtkSVGExporter.
vtkXMLDataElement * DefinitionNode
std::array< double, 9 > ActiveNodeTransform
abstract specification for renderers
Definition: vtkRenderer.h:57
static vtkContextDevice2D * New()
describes linear transformations via a 4x4 matrix
Definition: vtkTransform.h:54
virtual void Begin(vtkViewport *)
Begin drawing, pass in the viewport to set up the view.
virtual void SetColor4(unsigned char color[4])=0
Set the color for the device using unsigned char of length 4, RGBA.
virtual void EnableClipping(bool enable)=0
Enable or disable the clipping of the scene.
virtual void SetLineWidth(float width)=0
Set the line width.
virtual void DrawPoints(float *points, int n, unsigned char *colors=nullptr, int nc_comps=0)=0
Draw a series of points - fastest code path due to memory layout of the coordinates.
Some derived classes for the different colors commonly used.
Definition: vtkColor.h:194
a simple class to control print indentation
Definition: vtkIndent.h:33
virtual void DrawMarkers(int shape, bool highlight, float *points, int n, unsigned char *colors=nullptr, int nc_comps=0)
Draw a series of markers centered at the points supplied.
topologically and geometrically regular array of data
Definition: vtkImageData.h:39
virtual void DrawQuadStrip(float *, int)
Draw a quad using the specified number of points.
Abstract class for drawing 2D primitives.
virtual void PopMatrix()=0
Pop the current matrix off of the stack.
vtkNew< vtkTransform > Matrix
virtual void DrawLines(float *f, int n, unsigned char *colors=nullptr, int nc_comps=0)=0
Draw lines using the points - memory layout is as follows: l1p1,l1p2,l2p1,l2p2... ...
virtual void SetTexture(vtkImageData *image, int properties)=0
Set the texture for the device, it is used to fill the polygons.
virtual void DrawMathTextString(float *point, const vtkStdString &string)=0
Draw text using MathText markup for mathematical equations.
virtual void SetPointSize(float size)=0
Set the point size for glyphs/sprites.
virtual void DrawQuad(float *, int)
Draw a quad using the specified number of points.
virtual void DrawEllipticArc(float x, float y, float rX, float rY, float startAngle, float stopAngle)=0
Draw an elliptic arc with center at x,y with radii rX and rY between angles startAngle and stopAngle ...
virtual void MultiplyMatrix(vtkMatrix3x3 *m)=0
Multiply the current model view matrix by the supplied one.
virtual void DrawString(float *point, const vtkStdString &string)=0
Draw some text to the screen.
virtual void End()
End drawing, clean up the view.
virtual void DrawEllipseWedge(float x, float y, float outRx, float outRy, float inRx, float inRy, float startAngle, float stopAngle)=0
Draw an elliptic wedge with center at x, y, outer radii outRx, outRy, inner radii inRx...
std::array< int, 4 > ClipRect
virtual void DrawColoredPolygon(float *points, int numPoints, unsigned char *colors=nullptr, int nc_comps=0)
Draw a polygon using the specified number of points.
virtual void GetMatrix(vtkMatrix3x3 *m)=0
Set the model view matrix for the display.
virtual void ComputeStringBounds(const vtkStdString &string, float bounds[4])=0
Compute the bounds of the supplied string.
virtual void PushMatrix()=0
Push the current matrix onto the stack.
vtkXMLDataElement * ContextNode
vtkXMLDataElement * ActiveNode
virtual void DrawImage(float p[2], float scale, vtkImageData *image)=0
Draw the supplied image at the given x, y (p[0], p[1]) (bottom corner), scaled by scale (1...
represent and manipulate 3x3 transformation matrices
Definition: vtkMatrix3x3.h:33
virtual void SetLineType(int type)=0
Set the line type type (using anonymous enum in vtkPen).
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
String class that stores Unicode text.