Verovio
Source code documentation
devicecontext.h
1 // Name: devicecontext.h
3 // Author: Laurent Pugin
4 // Created: 2010
5 // Copyright (c) Authors and others. All rights reserved.
7 
8 #ifndef __VRV_DC_H__
9 #define __VRV_DC_H__
10 
11 #include <stack>
12 #include <string>
13 
14 //----------------------------------------------------------------------------
15 
16 #include "devicecontextbase.h"
17 #include "resources.h"
18 #include "vrvdef.h"
19 
20 //----------------------------------------------------------------------------
21 
22 #include "pugixml.hpp"
23 
24 namespace vrv {
25 
26 class Glyph;
27 class Object;
28 class View;
29 class Zone;
30 
31 extern "C" {
32 static inline double DegToRad(double deg)
33 {
34  return (deg * M_PI) / 180.0;
35 }
36 static inline double RadToDeg(double deg)
37 {
38  return (deg * 180.0) / M_PI;
39 }
40 }
41 
42 // ---------------------------------------------------------------------------
43 // DeviceContext
44 // ---------------------------------------------------------------------------
45 
58 public:
64  {
65  m_classId = DEVICE_CONTEXT;
66  m_resources = NULL;
67  m_isDeactivatedX = false;
68  m_isDeactivatedY = false;
69  m_width = 0;
70  m_height = 0;
71  m_contentHeight = 0;
72  m_userScaleX = 1.0;
73  m_userScaleY = 1.0;
74  m_baseWidth = 0;
75  m_baseHeight = 0;
76  m_pushBack = false;
77  m_viewBoxFactor = (double)DEFINITION_FACTOR;
78  this->SetBrush(-1.0);
79  this->SetPen(1, PEN_SOLID);
80  }
81  DeviceContext(ClassId classId)
82  {
83  m_classId = classId;
84  m_resources = NULL;
85  m_isDeactivatedX = false;
86  m_isDeactivatedY = false;
87  m_width = 0;
88  m_height = 0;
89  m_contentHeight = 0;
90  m_userScaleX = 1.0;
91  m_userScaleY = 1.0;
92  m_baseWidth = 0;
93  m_baseHeight = 0;
94  m_pushBack = false;
95  m_viewBoxFactor = (double)DEFINITION_FACTOR;
96  this->SetBrush(-1.0);
97  this->SetPen(1, PEN_SOLID);
98  }
99  virtual ~DeviceContext();
100  ClassId GetClassId() const { return m_classId; }
101  bool Is(ClassId classId) const { return (m_classId == classId); }
103 
109  const Resources *GetResources(bool showWarning = false) const;
110  bool HasResources() const { return (m_resources != NULL); }
111  void SetResources(const Resources *resources) { m_resources = resources; }
112  void ResetResources() { m_resources = NULL; }
114 
118  const Glyph *GetMusicGlyph(char32_t code) const;
119 
125  void SetWidth(int width) { m_width = width; }
126  void SetHeight(int height) { m_height = height; }
127  void SetContentHeight(int height) { m_contentHeight = height; }
128  void SetUserScale(double scaleX, double scaleY)
129  {
130  m_userScaleX = scaleX;
131  m_userScaleY = scaleY;
132  }
133  void SetBaseSize(int width, int height)
134  {
135  m_baseWidth = width;
136  m_baseHeight = height;
137  }
138  void SetViewBoxFactor(double ppuFactor);
139  int GetWidth() const { return m_width; }
140  int GetHeight() const { return m_height; }
141  int GetContentHeight() const { return m_contentHeight; }
142  double GetUserScaleX() { return m_userScaleX; }
143  double GetUserScaleY() { return m_userScaleY; }
144  std::pair<int, int> GetBaseSize() const { return std::make_pair(m_baseWidth, m_baseHeight); }
145  double GetViewBoxFactor() const { return m_viewBoxFactor; }
147 
153  void SetBrush(float opacity, int color = COLOR_NONE);
154  void SetPen(int width, PenStyle style, int dashLength = 0, int gapLength = 0,
155  LineCapStyle lineCap = LINECAP_DEFAULT, LineJoinStyle lineJoin = LINEJOIN_DEFAULT, float opacity = -1.0,
156  int color = COLOR_NONE);
157  void SetFont(FontInfo *font);
158  void SetPushBack() { m_pushBack = true; }
159  void ResetBrush();
160  void ResetPen();
161  void ResetFont();
162  void ResetPushBack() { m_pushBack = false; }
163  virtual void SetBackground(int color, int style = PEN_SOLID) = 0;
164  virtual void SetBackgroundImage(void *image, double opacity = 1.0) = 0;
165  virtual void SetBackgroundMode(int mode) = 0;
166  virtual void SetTextForeground(int color) = 0;
167  virtual void SetTextBackground(int color) = 0;
168  virtual void SetLogicalOrigin(int x, int y) = 0;
170 
175  FontInfo *GetFont();
176  bool HasFont() const { return !m_fontStack.empty(); }
178 
183  virtual void GetTextExtent(const std::string &string, TextExtend *extend, bool typeSize);
184  virtual void GetTextExtent(const std::u32string &string, TextExtend *extend, bool typeSize);
185  virtual void GetSmuflTextExtent(const std::u32string &string, TextExtend *extend);
186 
191  virtual Point GetLogicalOrigin() = 0;
193 
198  virtual void DrawQuadBezierPath(Point bezier[3]) = 0;
199  virtual void DrawCubicBezierPath(Point bezier[4]) = 0;
200  virtual void DrawCubicBezierPathFilled(Point bezier1[4], Point bezier2[4]) = 0;
201  virtual void DrawBentParallelogramFilled(Point side[4], int height) = 0;
202  virtual void DrawCircle(int x, int y, int radius) = 0;
203  virtual void DrawEllipse(int x, int y, int width, int height) = 0;
204  virtual void DrawEllipticArc(int x, int y, int width, int height, double start, double end) = 0;
205  virtual void DrawLine(int x1, int y1, int x2, int y2) = 0;
206  virtual void DrawPolyline(int n, Point points[], bool close = false) = 0;
207  virtual void DrawPolygon(int n, Point points[]) = 0;
208  virtual void DrawRectangle(int x, int y, int width, int height) = 0;
209  virtual void DrawRotatedText(const std::string &text, int x, int y, double angle) = 0;
210  virtual void DrawRoundedRectangle(int x, int y, int width, int height, int radius) = 0;
211  virtual void DrawText(const std::string &text, const std::u32string &wtext = U"", int x = VRV_UNSET,
212  int y = VRV_UNSET, int width = VRV_UNSET, int height = VRV_UNSET) = 0;
213  virtual void DrawMusicText(const std::u32string &text, int x, int y, bool setSmuflGlyph = false) = 0;
214  virtual void DrawSpline(int n, Point points[]) = 0;
215  virtual void DrawGraphicUri(int x, int y, int width, int height, const std::string &uri) = 0;
216  virtual void DrawSvgShape(int x, int y, int width, int height, double scale, pugi::xml_node svg) = 0;
217  virtual void DrawBackgroundImage(int x = 0, int y = 0) = 0;
219 
224  virtual void DrawPlaceholder(int x, int y) {}
225 
232  virtual void StartText(int x, int y, data_HORIZONTALALIGNMENT alignment = HORIZONTALALIGNMENT_left) = 0;
233  virtual void EndText() = 0;
234 
240  virtual void MoveTextTo(int x, int y, data_HORIZONTALALIGNMENT alignment) = 0;
241  virtual void MoveTextVerticallyTo(int y) = 0;
243 
253  void DeactivateGraphic();
254  void DeactivateGraphicX();
255  void DeactivateGraphicY();
256  void ReactivateGraphic();
258 
262  virtual bool ApplyOffset() { return false; }
263 
269  virtual void StartGraphic(Object *object, const std::string &gClass, const std::string &gId,
270  GraphicID graphicID = PRIMARY, bool preprend = false) = 0;
271  virtual void EndGraphic(Object *object, View *view) = 0;
272  virtual void AddGraphicClass(const std::string &className) {};
274 
280  virtual void StartCustomGraphic(const std::string &name, std::string gClass = "", std::string gId = "") {}
281  virtual void EndCustomGraphic() {}
283 
287  virtual void SetCustomGraphicColor(const std::string &color) {}
288 
292  virtual void SetCustomGraphicAttributes(const std::string &data, const std::string &value) {}
293 
299  virtual void ResumeGraphic(Object *object, std::string gId) = 0;
300  virtual void EndResumedGraphic(Object *object, View *view) = 0;
302 
308  virtual void StartTextGraphic(Object *object, const std::string &gClass, const std::string &gId)
309  {
310  StartGraphic(object, gClass, gId);
311  }
312  virtual void EndTextGraphic(Object *object, View *view) { EndGraphic(object, view); }
314 
320  virtual void RotateGraphic(Point const &orig, double angle) = 0;
322 
327  virtual void StartPage() = 0;
328  virtual void EndPage() = 0;
330 
335  virtual void AddDescription(const std::string &text) {}
337 
343  virtual bool UseGlobalStyling() { return false; }
344 
349  virtual bool UseGlobalTextStyling() { return this->UseGlobalStyling(); }
350 
351  //----------------//
352  // Static methods //
353  //----------------//
354 
356  static int RGB2Int(char red, char green, char blue) { return (red << 16 | green << 8 | blue); }
357 
358 private:
359  void AddGlyphToTextExtend(const Glyph *glyph, TextExtend *extend);
360  void AddShapedRunToTextExtend(const FontStore::ShapedRun &run, TextExtend *extend);
361 
362 public:
363  //
364 protected:
365  std::stack<Pen> m_penStack;
366  std::stack<Brush> m_brushStack;
367  std::stack<FontInfo *> m_fontStack;
368 
371  bool m_isDeactivatedY;
372 
375 
376  Zone *m_facsimile = NULL;
377 
378 private:
380  ClassId m_classId;
381 
383  const Resources *m_resources;
384 
386  int m_width;
387  int m_height;
388 
390  int m_baseWidth;
391  int m_baseHeight;
392 
394  int m_contentHeight;
395 
397  double m_userScaleX;
398  double m_userScaleY;
399 
401  double m_viewBoxFactor;
402 };
403 
404 } // namespace vrv
405 
406 #endif // __AX_DC_H__
This class is an abstract device context.
Definition: devicecontext.h:57
bool m_isDeactivatedX
flag for indicating if the graphic is deactivated
Definition: devicecontext.h:370
virtual void SetCustomGraphicColor(const std::string &color)
Method for changing the color of a custom graphic.
Definition: devicecontext.h:287
virtual void SetCustomGraphicAttributes(const std::string &data, const std::string &value)
Method for adding custom graphic data-* attributes.
Definition: devicecontext.h:292
virtual bool ApplyOffset()
Indicate if offset should be applied.
Definition: devicecontext.h:262
const Glyph * GetMusicGlyph(char32_t code) const
Return the glyph in the music font of the current font (or in the current music font if none)
static int RGB2Int(char red, char green, char blue)
Color conversion method.
Definition: devicecontext.h:356
virtual void DrawPlaceholder(int x, int y)
Special method for forcing bounding boxes to be updated Used for invisible elements (e....
Definition: devicecontext.h:224
virtual bool UseGlobalTextStyling()
Method indicating if text styles (e.g., bold tempo) are left to the global styling.
Definition: devicecontext.h:349
bool m_pushBack
push back mode
Definition: devicecontext.h:374
virtual bool UseGlobalStyling()
Method indicating if default global styling is used.
Definition: devicecontext.h:343
This class is used for storing a music font glyph.
Definition: glyph.h:32
This class represents a basic object.
Definition: object.h:66
This class provides resource values.
Definition: resources.h:33
Simple class for representing text extends.
Definition: devicecontextbase.h:345
This class is a drawing context and corresponds to the view of a MVC design pattern.
Definition: view.h:109
Implements the zone element in MEI.
Definition: zone.h:30
A shaped text run.
Definition: fontstore.h:73