Verovio
Source code documentation
svgdevicecontext.h
1 // Name: svgdevicecontext.h
3 // Author: Laurent Pugin
4 // Created: 2011
5 // Copyright (c) Authors and others. All rights reserved.
7 
8 #ifndef __VRV_SVG_DC_H__
9 #define __VRV_SVG_DC_H__
10 
11 #include <fstream>
12 #include <list>
13 #include <set>
14 #include <string>
15 #include <vector>
16 
17 //----------------------------------------------------------------------------
18 
19 #include "devicecontext.h"
20 #include "fontstore.h"
21 #include "object.h"
22 #include "options.h"
23 
24 //----------------------------------------------------------------------------
25 
26 class Glyph;
27 class Resources;
28 
29 namespace vrv {
30 
31 //----------------------------------------------------------------------------
32 // SvgDeviceContext
33 //----------------------------------------------------------------------------
34 
41 public:
46  SvgDeviceContext(const std::string &docId);
47  virtual ~SvgDeviceContext();
49 
54  void SetBackground(int color, int style = PEN_SOLID) override;
55  void SetBackgroundImage(void *image, double opacity = 1.0) override;
56  void SetBackgroundMode(int mode) override;
57  void SetTextForeground(int color) override;
58  void SetTextBackground(int color) override;
59  void SetLogicalOrigin(int x, int y) override;
61 
66  Point GetLogicalOrigin() override;
68 
73  std::string GetStringSVG(bool xml_declaration = false);
74 
79  void DrawQuadBezierPath(Point bezier[3]) override;
80  void DrawCubicBezierPath(Point bezier[4]) override;
81  void DrawCubicBezierPathFilled(Point bezier1[4], Point bezier2[4]) override;
82  void DrawBentParallelogramFilled(Point side[4], int height) override;
83  void DrawCircle(int x, int y, int radius) override;
84  void DrawEllipse(int x, int y, int width, int height) override;
85  void DrawEllipticArc(int x, int y, int width, int height, double start, double end) override;
86  void DrawLine(int x1, int y1, int x2, int y2) override;
87  void DrawPolyline(int n, Point points[], bool close) override;
88  void DrawPolygon(int n, Point points[]) override;
89  void DrawRectangle(int x, int y, int width, int height) override;
90  void DrawRotatedText(const std::string &text, int x, int y, double angle) override;
91  void DrawRoundedRectangle(int x, int y, int width, int height, int radius) override;
92  void DrawText(const std::string &text, const std::u32string &wtext = U"", int x = VRV_UNSET, int y = VRV_UNSET,
93  int width = VRV_UNSET, int height = VRV_UNSET) override;
94  void DrawMusicText(const std::u32string &text, int x, int y, bool setSmuflGlyph = false) override;
95  void DrawSpline(int n, Point points[]) override;
96  void DrawGraphicUri(int x, int y, int width, int height, const std::string &uri) override;
97  void DrawSvgShape(int x, int y, int width, int height, double scale, pugi::xml_node svg) override;
98  void DrawBackgroundImage(int x = 0, int y = 0) override;
100 
105  void StartText(int x, int y, data_HORIZONTALALIGNMENT alignment = HORIZONTALALIGNMENT_left) override;
106  void EndText() override;
107 
112  void MoveTextTo(int x, int y, data_HORIZONTALALIGNMENT alignment) override;
113  void MoveTextVerticallyTo(int y) override;
115 
119  bool ApplyOffset() override { return true; }
120 
125  void StartGraphic(Object *object, const std::string &gClass, const std::string &gId, GraphicID graphicID = PRIMARY,
126  bool prepend = false) override;
127  void EndGraphic(Object *object, View *view) override;
128  void AddGraphicClass(const std::string &className) override;
130 
135  void StartCustomGraphic(const std::string &name, std::string gClass = "", std::string gId = "") override;
136  void EndCustomGraphic() override;
138 
142  void SetCustomGraphicColor(const std::string &color) override;
143 
147  void SetCustomGraphicAttributes(const std::string &data, const std::string &value) override;
148 
153  void ResumeGraphic(Object *object, std::string gId) override;
154  void EndResumedGraphic(Object *object, View *view) override;
156 
161  void StartTextGraphic(Object *object, const std::string &gClass, const std::string &gId) override;
162  void EndTextGraphic(Object *object, View *view) override;
164 
169  void RotateGraphic(Point const &orig, double angle) override;
171 
176  void StartPage() override;
177  void EndPage() override;
179 
184  void AddDescription(const std::string &text) override;
186 
190  void AppendIdAndClass(const std::string &gId, const std::string &baseClass, const std::string &addedClasses,
191  GraphicID graphicID = PRIMARY);
192 
197 
201  bool UseGlobalStyling() override { return !m_mmOutput; }
202 
206  bool UseGlobalTextStyling() override { return this->UseGlobalStyling() && !m_textAsPaths && !m_textEmbedFont; }
207 
211  void SetMMOutput(bool mmOutput) { m_mmOutput = mmOutput; }
212 
213  void SetFacsimile(bool facsimile) { m_facsimile = facsimile; }
214  bool GetFacsimile() { return m_facsimile; }
215 
219  void SetShowHidden(bool showHidden) { m_showHidden = showHidden; }
220 
224  void SetSvgBoundingBoxes(bool svgBoundingBoxes) { m_svgBoundingBoxes = svgBoundingBoxes; }
225 
229  void SetSvgContentBoundingBoxes(bool svgContentBoundingBoxes)
230  {
231  m_svgContentBoundingBoxes = svgContentBoundingBoxes;
232  }
233 
237  void SetSvgViewBox(bool svgViewBox) { m_svgViewBox = svgViewBox; }
238 
242  void SetHtml5(bool html5) { m_html5 = html5; }
243 
247  void SetIndent(int indent) { m_indent = indent; }
248 
252  void SetFormatRaw(bool rawFormat) { m_formatRaw = rawFormat; }
253 
257  void SetRemoveXlink(bool removeXlink) { m_removeXlink = removeXlink; }
258 
262  void SetCss(const std::string &css)
263  {
264  m_css = css;
265  this->PrefixCssRules(m_css);
266  }
267 
272  void SetAdditionalAttributes(const std::vector<std::string> &additionalAttributes)
273  {
274  for (std::string s : additionalAttributes) {
275  std::string className = s.substr(0, s.find("@")); // parse <element@attribute>, e.g., "note@pname"
276  std::string attributeName = s.substr(s.find("@") + 1);
277  ClassId classId = ObjectFactory::GetInstance().GetClassId(className);
278  m_svgAdditionalAttributes.insert({ classId, attributeName });
279  }
280  }
281 
285  void SetSmuflTextFont(option_SMUFLTEXTFONT smuflTextFont) { m_smuflTextFont = smuflTextFont; }
286 
290  void SetTextEmbedFont(bool textEmbedFont) { m_textEmbedFont = textEmbedFont; }
291 
295  void SetTextAsPaths(bool textAsPaths) { m_textAsPaths = textAsPaths; }
296 
297 private:
302  bool CopyFileToStream(const std::string &filename, std::ostream &dest);
303 
307  void DrawSvgBoundingBox(Object *object, View *view);
308 
313  void StartBoundingBoxGraphic(Object *object, const std::string &gClass, const std::string &gId);
314 
318  void DrawSvgBoundingBoxRectangle(int x, int y, int width, int height);
319 
323  void VrvTextFont() { m_vrvTextFont = true; }
324 
328  void VrvTextFontFallback() { m_vrvTextFontFallback = true; }
329 
333  void IncludeMusicTextFont(const std::string &fontname);
334 
338  void IncludeTextFontFaces(const std::string &family, const std::set<Resources::FaceStyle> &faceStyles);
339 
343  void AppendFontFace(const std::string &family, const FontStore::FontFile &file, std::string src = "");
344 
349  void Commit(bool xml_declaration);
350 
351  void WriteLine(std::string);
352 
353  std::string GetColor(int color) const;
354 
355  pugi::xml_node AddChild(std::string name);
356 
361  void AppendStrokeLineCap(pugi::xml_node node, const Pen &pen);
362  void AppendStrokeLineJoin(pugi::xml_node node, const Pen &pen);
363  void AppendStrokeDashArray(pugi::xml_node node, const Pen &pen);
365 
369  void PrefixCssRules(std::string &rules);
370 
375  void DrawTextAsTspan(const std::u32string &wtext, int x, int y, int width, int height);
376  void DrawTextAsPaths(const std::u32string &wtext, int x, int y);
377  void FinishTextLine();
379 
380  //----------------//
381  // Static methods //
382  //----------------//
383 
387  static bool IsInFontFamilyList(const std::string &fontFamilyList, const std::string &family);
388 
389 public:
390  //
391 private:
397  bool m_vrvTextFont;
398 
402  bool m_vrvTextFontFallback;
403 
404  // we use a std::stringstream because we want to prepend the <defs> which will know only when we reach the end of
405  // the page
406  // some viewer seem to support to have the <defs> at the end, but some do not (pdf2svg, for example)
407  // for this reason, the full svg is finally written a string from the destructor or when Flush() is called
408  std::ostringstream m_outdata;
409 
410  bool m_committed; // did we flushed the file?
411  int m_originX, m_originY;
413  int m_textY;
415  std::map<std::string, std::set<Resources::FaceStyle>> m_textFontFaces;
416  // Text cursor and current line when text is rendered as paths
417  double m_textCursorX;
418  int m_textCursorY;
419  double m_textLineWidth;
420  data_HORIZONTALALIGNMENT m_textAlignment;
421  pugi::xml_node m_textLineNode;
422 
423  // Here we hold references to all different glyphs used so far,
424  // including any glyph for the same code but from different fonts.
425  // They will be added at the end of the file as <defs>.
426  // With multiple font support we need to keep track of:
427  // a) the glyph (to check if is has been already added)
428  // b) the id assigned to glyphs on the (that is has been consumed by the already rendered elements)
429  // To keep things as similar as possible to previous versions we generate ids with as uuuu-ss (where uuuu is the
430  // Smulf code for the glyph and ss the per-session suffix) for most of the cases (single font usage). When the same
431  // glyph has been used from several fonts we use uuuu-n-ss where n indicates the collision count.
432  class GlyphRef {
433  public:
434  GlyphRef(const Glyph *glyph, int count, const std::string &postfix);
435  const Glyph *GetGlyph() const { return m_glyph; };
436  const std::string &GetRefId() const { return m_refId; };
437 
438  private:
439  const Glyph *m_glyph;
440  std::string m_refId;
441  };
442  const std::string InsertGlyphRef(const Glyph *glyph);
443  std::vector<std::pair<const Glyph *, GlyphRef>> m_smuflGlyphs;
444  std::map<std::string, int> m_glyphCodeFontCounter;
445 
446  // pugixml data
447  pugi::xml_document m_svgDoc;
448  pugi::xml_node m_svgNode;
449  pugi::xml_node m_pageNode;
450  pugi::xml_node m_currentNode;
451  std::list<pugi::xml_node> m_svgNodeStack;
452 
453  // output as mm (for pdf generation with a 72 dpi)
454  bool m_mmOutput;
455  // show hidden flag
456  bool m_showHidden;
457  // facsimiler flag
458  bool m_facsimile;
459  // add bouding boxes in svg output
460  bool m_svgBoundingBoxes;
461  // add content bounding boxes in svg output
462  bool m_svgContentBoundingBoxes;
463  // bounding box groups of text graphics, added next to the <text> element and skipped by AddChild
464  std::set<pugi::xml_node> m_textBoundingBoxNodes;
465  // use viewbox on svg root element
466  bool m_svgViewBox;
467  // output HTML5 data-* attributes
468  bool m_html5;
469  // additional CSS
470  std::string m_css;
471  // copy additional attributes of given elements to the SVG, in the form "note@pname; layer@n"
472  std::multimap<ClassId, std::string> m_svgAdditionalAttributes;
473  // format output as raw, stripping extraneous whitespace and non-content newlines
474  bool m_formatRaw;
475  // remove xlink from href attributes
476  bool m_removeXlink;
477  // indentation value (-1 for tabs)
478  int m_indent;
479  // postfix to be added to font glyphs
480  std::string m_glyphPostfixId;
481  // embedding of the smufl text font
482  option_SMUFLTEXTFONT m_smuflTextFont;
483  // embedding of the text fonts
484  bool m_textEmbedFont;
485  // render text as glyph paths
486  bool m_textAsPaths;
487  // the document id
488  std::string m_docId;
489 };
490 
491 } // namespace vrv
492 
493 #endif // __VRV_SVG_DC_H__
This class is an abstract device context.
Definition: devicecontext.h:57
static ObjectFactory & GetInstance()
A static method returning a static object in order to guarantee initialisation.
ClassId GetClassId(const std::string &name)
Get the ClassId from the MEI element string name by making a lookup in the register.
This class represents a basic object.
Definition: object.h:66
Simple class for representing points.
Definition: devicecontextbase.h:203
This class implements a drawing context for generating SVG files.
Definition: svgdevicecontext.h:40
void SetSvgContentBoundingBoxes(bool svgContentBoundingBoxes)
Setting m_svgContentBoundingBoxes flag (false by default)
Definition: svgdevicecontext.h:229
void SetSvgViewBox(bool svgViewBox)
Setting m_svgViewBox flag (false by default)
Definition: svgdevicecontext.h:237
void SetCss(const std::string &css)
Setter for an additional CSS.
Definition: svgdevicecontext.h:262
void SetSmuflTextFont(option_SMUFLTEXTFONT smuflTextFont)
Setter for a the smufl text font option.
Definition: svgdevicecontext.h:285
void SetIndent(int indent)
Setter for indent of the SVG (default is 3, -1 for tabs)
Definition: svgdevicecontext.h:247
void SetTextEmbedFont(bool textEmbedFont)
Setting the flag for embedding the text fonts used (false by default)
Definition: svgdevicecontext.h:290
void AppendAdditionalAttributes(Object *object)
Append additional attributes, as given in m_svgAdditionalAttributes.
std::string GetStringSVG(bool xml_declaration=false)
}
void SetMMOutput(bool mmOutput)
Setting mm output flag (false by default)
Definition: svgdevicecontext.h:211
void SetTextAsPaths(bool textAsPaths)
Setting the flag for rendering text as glyph paths instead of SVG text (false by default)
Definition: svgdevicecontext.h:295
bool UseGlobalTextStyling() override
Text styles cannot be left to CSS when text is rendered as paths, and are needed for embedding the fa...
Definition: svgdevicecontext.h:206
bool UseGlobalStyling() override
In SVG use global styling but not with mm output (for pdf generation)
Definition: svgdevicecontext.h:201
void SetAdditionalAttributes(const std::vector< std::string > &additionalAttributes)
Copies additional attributes of defined elements to the SVG, each string in the form "elementName@att...
Definition: svgdevicecontext.h:272
void AppendIdAndClass(const std::string &gId, const std::string &baseClass, const std::string &addedClasses, GraphicID graphicID=PRIMARY)
Add id, data-id and class attributes.
void SetFormatRaw(bool rawFormat)
Set the SVG to have 'raw' formatting, with no extraneous whitespace or newlines.
Definition: svgdevicecontext.h:252
void SetRemoveXlink(bool removeXlink)
Removes the xlink: prefex on href attributes, necessary for some newer browsers.
Definition: svgdevicecontext.h:257
bool ApplyOffset() override
Indicate if offset should be applied.
Definition: svgdevicecontext.h:119
void SetShowHidden(bool showHidden)
Setting show hidden flag (false by default)
Definition: svgdevicecontext.h:219
void SetCustomGraphicAttributes(const std::string &data, const std::string &value) override
Method for adding custom graphic data-* attributes.
void SetHtml5(bool html5)
Setting m_html5 flag (false by default)
Definition: svgdevicecontext.h:242
void SetSvgBoundingBoxes(bool svgBoundingBoxes)
Setting m_svgBoudingBoxes flag (false by default)
Definition: svgdevicecontext.h:224
void SetCustomGraphicColor(const std::string &color) override
Method for changing the color of a custom graphic.
This class is a drawing context and corresponds to the view of a MVC design pattern.
Definition: view.h:109