Verovio
Source code documentation
toolkit.h
1 // Name: toolkit.h
3 // Author: Laurent Pugin
4 // Created: 17/10/2013
5 // Copyright (c) Authors and others. All rights reserved.
7 
8 #ifndef __VRV_TOOLKIT_H__
9 #define __VRV_TOOLKIT_H__
10 
11 #include <string>
12 
13 //----------------------------------------------------------------------------
14 
15 #include "doc.h"
16 #include "docselection.h"
17 #include "toolkitdef.h"
18 #include "view.h"
19 
20 //----------------------------------------------------------------------------
21 
22 namespace vrv {
23 
24 class EditorToolkit;
25 class RuntimeClock;
26 
31 //----------------------------------------------------------------------------
32 // Toolkit
33 //----------------------------------------------------------------------------
34 
35 class Toolkit {
36 public:
43 
49  Toolkit(bool initFont = true);
50  virtual ~Toolkit();
51 
53 
60 
68  std::string GetID() { return m_doc.GetID(); }
69 
77  std::string GetResourcePath() const;
78 
90  bool SetResourcePath(const std::string &path);
91 
97  std::string GetLog();
98 
104  std::string GetVersion() const;
105 
114  void ResetXmlIdSeed(int seed);
115 
117 
124 
135  bool LoadFile(const std::string &filename);
136 
145  bool LoadData(const std::string &data);
146 
153  bool LoadZipDataBase64(const std::string &data);
154 
162  bool LoadZipDataBuffer(const unsigned char *data, int length);
163 
174  std::string ValidatePAEFile(const std::string &filename);
175 
187  std::string ValidatePAE(const std::string &data);
188 
197 
199 
206 
212  std::string GetOptions() const;
213 
219  std::string GetDefaultOptions() const;
220 
228  std::string GetAvailableOptions() const;
229 
238  bool SetOptions(const std::string &jsonOptions);
239 
243  void ResetOptions();
244 
248  void PrintOptionUsage(const std::string &category, std::ostream &output) const;
249 
253  std::string GetOptionUsageString() const;
254 
263  bool SetScale(int scale);
264 
272  int GetScale() { return m_options->m_scale.GetValue(); }
273 
282  bool SetInputFrom(std::string const &inputFrom);
283 
292  bool SetOutputTo(std::string const &outputTo);
293 
304  bool Select(const std::string &selection);
305 
307 
314 
321  bool Edit(const std::string &editorAction);
322 
328  std::string EditInfo();
329 
331 
338 
349  std::string RenderData(const std::string &data, const std::string &jsonOptions);
350 
358  std::string RenderToSVG(int pageNo = 1, bool xmlDeclaration = false);
359 
369  bool RenderToSVGFile(const std::string &filename, int pageNo = 1);
370 
376  std::string RenderToMIDI();
377 
386  bool RenderToMIDIFile(const std::string &filename);
387 
395  std::string RenderToPAE();
396 
407  bool RenderToPAEFile(const std::string &filename);
408 
415  std::string RenderToTimemap(const std::string &jsonOptions = "");
416 
422  std::string RenderToExpansionMap();
423 
433  bool RenderToTimemapFile(const std::string &filename, const std::string &jsonOptions = "");
434 
442  bool RenderToExpansionMapFile(const std::string &filename);
443 
445 
452 
458  std::string GetHumdrum();
459 
465  std::string ConvertMEIToHumdrum(const std::string &meiData);
466 
472  std::string ConvertHumdrumToHumdrum(const std::string &humdrumData);
473 
479  std::string ConvertHumdrumToMIDI(const std::string &humdrumData);
480 
489  bool GetHumdrumFile(const std::string &filename);
490 
492 
499 
509  std::string GetMEI(const std::string &jsonOptions = "");
510 
520  bool SaveFile(const std::string &filename, const std::string &jsonOptions = "");
521 
523 
530 
539  std::string GetDescriptiveFeatures(const std::string &jsonOptions);
540 
547  std::string GetElementsAtTime(int millisec);
548 
557  int GetPageWithElement(const std::string &xmlId);
558 
567  std::string GetElementAttr(const std::string &xmlId);
568 
575  std::string GetNotatedIdForElement(const std::string &xmlId);
576 
583  std::string GetExpansionIdsForElement(const std::string &xmlId);
584 
593  int GetTimeForElement(const std::string &xmlId);
594 
603  std::string GetMIDIValuesForElement(const std::string &xmlId);
604 
614  std::string GetTimesForElement(const std::string &xmlId);
615 
617 
622 
632  void RedoLayout(const std::string &jsonOptions = "");
633 
641 
643 
644  //------------------------------------------------//
645  // Public methods not listed in the documentation //
646  //------------------------------------------------//
647 
654 
662  bool RenderToDeviceContext(int pageNo, DeviceContext *deviceContext);
663 
669  Options *GetOptionsObj() { return m_options; }
670 
676  void SetCString(const std::string &data);
677 
685  const char *GetCString();
686 
692  void GetHumdrum(std::ostream &output);
693 
699  void SetHumdrumBuffer(const char *contents);
700 
708  const char *GetHumdrumBuffer();
709 
716 
724  int GetInputFrom() { return m_inputFrom; }
725 
729  int GetOutputTo() { return m_outputTo; }
730 
735  void SetLocale();
736  void ResetLocale();
738 
745  void InitClock();
746  void ResetClock();
747  double GetRuntimeInSeconds() const;
748  void LogRuntime() const;
750 
752 
753 protected:
757  FileFormat IdentifyInputFrom(const std::string &data);
758 
762  void PrintOptionUsageOutput(const vrv::Option *option, std::ostream &output) const;
763 
768 
774 
779 
783  bool LoadData(const std::string &data, bool resetLogBuffer);
784 
785 private:
786  bool SetFont(const std::string &fontName);
787  bool IsUTF16(const std::string &filename);
788  bool LoadUTF16File(const std::string &filename);
789  bool IsZip(const std::string &filename);
790  bool LoadZipFile(const std::string &filename);
791  bool LoadZipData(const std::vector<unsigned char> &bytes);
792  void GetClassIds(const std::vector<std::string> &classStrings, std::vector<ClassId> &classIds);
793 
800  std::string GetOptions(bool defaultValues) const;
801 
805  void SetViewAndEditor();
806 
807 public:
808  //
809 private:
810  Doc m_doc;
811  DocSelection m_docSelection;
812  View m_view;
813  FileFormat m_inputFrom;
814  FileFormat m_outputTo;
815 
816  Options *m_options;
817 
818  std::optional<std::locale> m_previousLocale;
819 
823  char *m_cString;
824 
829  std::stringstream m_cerrCaptured;
830 
835  std::streambuf *m_cerrOriginalBuf;
836 
837  EditorToolkit *m_editorToolkit;
838 
839 #ifndef NO_RUNTIME
841  RuntimeClock *m_runtimeClock;
842 #endif
843 
844  //----------------//
845  // Static members //
846  //----------------//
847 
848  static char *m_humdrumBuffer;
849 };
850 
851 } // namespace vrv
852 #endif
This class is an abstract device context.
Definition: devicecontext.h:57
This class is a hold the data and corresponds to the model of a MVC design pattern.
Definition: doc.h:41
This class stores a document selection.
Definition: docselection.h:24
Definition: editortoolkit.h:28
This class is a base class of each styling parameter.
Definition: options.h:100
This class contains the document styling parameters.
Definition: options.h:576
This class represents a clock to measure runtime.
Definition: runtimeclock.h:26
Definition: toolkit.h:35
std::string ValidatePAEFile(const std::string &filename)
Validate the Plaine & Easie code from a file.
bool SaveFile(const std::string &filename, const std::string &jsonOptions="")
Get the MEI and save it to the file.
std::string GetResourcePath() const
Get the resource path for the Toolkit instance.
FileFormat IdentifyInputFrom(const std::string &data)
Identify the input file type for auto loading of input data.
std::string RenderToSVG(int pageNo=1, bool xmlDeclaration=false)
Render a page to SVG.
bool SetOptions(const std::string &jsonOptions)
Set option values.
std::string GetLog()
Get the log content for the latest operation.
std::string GetHumdrum()
Get the humdrum buffer.
std::string GetMIDIValuesForElement(const std::string &xmlId)
Return MIDI values of the element with the ID (@xml:id)
int GetPageWithElement(const std::string &xmlId)
Return the page on which the element is the ID (@xml:id) is rendered.
std::string RenderToMIDI()
Render the document to MIDI.
bool Edit(const std::string &editorAction)
Edit the MEI data - experimental code not to rely on.
std::string RenderToExpansionMap()
Render a document's expansionMap, if existing.
bool RenderToMIDIFile(const std::string &filename)
Render a document to MIDI and save it to the file.
std::string GetAvailableOptions() const
Return all available options grouped by category.
int GetScale()
Get the scale option.
Definition: toolkit.h:272
std::string GetDescriptiveFeatures(const std::string &jsonOptions)
Return descriptive features as a JSON string.
std::string GetMEI(const std::string &jsonOptions="")
Get the MEI as a string.
std::string GetDefaultOptions() const
Return a dictionary of all the options with their default value.
std::string EditInfo()
Return the editor status - experimental code not to rely on.
bool LoadFile(const std::string &filename)
Load a file from the file system.
void LogRedirectStop()
End capturing std::cerr from an external codebase for redirection to vrv::logBuffer.
void LogRedirectStart()
Start capturing std::cerr from an external codebase for redirection to vrv::logBuffer.
bool RenderToSVGFile(const std::string &filename, int pageNo=1)
Render a page to SVG and save it to the file.
bool RenderToPAEFile(const std::string &filename)
Render a document to Plaine & Easie code and save it to the file.
std::string GetOptions() const
Return a dictionary of all the options with their current value.
std::string ConvertHumdrumToMIDI(const std::string &humdrumData)
Convert Humdrum data to MIDI.
std::string GetElementsAtTime(int millisec)
Return array of IDs of elements being currently played.
bool SetScale(int scale)
Set the scale option.
std::string RenderToPAE()
Render a document to Plaine & Easie code.
bool RenderToExpansionMapFile(const std::string &filename)
Render a document's expansionMap and save it to a file.
std::string GetOptionUsageString() const
Get all usage for all option categories as string.
std::string ValidatePAE(const std::string &data)
Validate the Plaine & Easie code passed in the string data.
bool SetOutputTo(std::string const &outputTo)
Set the output to option.
std::string GetID()
Return the ID of the Toolkit instance.
Definition: toolkit.h:68
std::string ConvertHumdrumToHumdrum(const std::string &humdrumData)
Filter Humdrum data.
void ResetLogBuffer()
Resets the vrv::logBuffer.
std::string GetVersion() const
Return the version number.
void ResetOptions()
Reset all options to default values.
bool LoadData(const std::string &data)
Load a string data with the type previously specified in the options.
bool RenderToTimemapFile(const std::string &filename, const std::string &jsonOptions="")
Render a document to timemap and save it to the file.
std::string RenderToTimemap(const std::string &jsonOptions="")
Render a document to a timemap.
void ResetXmlIdSeed(int seed)
Reset the seed used to generate MEI @xml:id attribute values.
std::string GetNotatedIdForElement(const std::string &xmlId)
Return the ID string of the notated (the original) element.
int GetTimeForElement(const std::string &xmlId)
Return the time at which the element is the ID (@xml:id) is played.
void PrintOptionUsageOutput(const vrv::Option *option, std::ostream &output) const
Print formatted option usage for specific option to output stream.
bool SetInputFrom(std::string const &inputFrom)
Set the input from option.
bool LoadZipDataBuffer(const unsigned char *data, int length)
Load a MusicXML compressed file passed as a buffer of bytes.
std::string GetExpansionIdsForElement(const std::string &xmlId)
Return a vector of ID strings of all elements (the notated and the expanded) for a given element.
void RedoLayout(const std::string &jsonOptions="")
Redo the layout of the loaded data.
bool Select(const std::string &selection)
Set the value for a selection.
std::string GetElementAttr(const std::string &xmlId)
Return element attributes as a JSON string.
Toolkit(bool initFont=true)
Constructor.
std::string RenderData(const std::string &data, const std::string &jsonOptions)
Render the first page of the data to SVG.
std::string GetTimesForElement(const std::string &xmlId)
Return a JSON object string with the following key values for a given note.
bool GetHumdrumFile(const std::string &filename)
Write the humdrum buffer to the file.
void PrintOptionUsage(const std::string &category, std::ostream &output) const
Print formatted option usage for specific category (with max/min/default values) to output stream.
std::string ConvertMEIToHumdrum(const std::string &meiData)
Convert MEI data into Humdrum data.
bool LoadZipDataBase64(const std::string &data)
Load a MusicXML compressed file passed as base64 encoded string.
bool SetResourcePath(const std::string &path)
Set the resource path for the Toolkit instance and any extra fonts.
bool LoadData(const std::string &data, bool resetLogBuffer)
Load a string data with or without resetting the log buffer.
int GetPageCount()
Return the number of pages in the loaded document.
void RedoPagePitchPosLayout()
Redo the layout of the pitch postitions of the current drawing page.
This class is a drawing context and corresponds to the view of a MVC design pattern.
Definition: view.h:106
const char * GetHumdrumBuffer()
Return the content of the humdrum internal buffer.
void InitClock()
Measuring runtime.
const char * GetCString()
Return the content of the cstring internal buffer.
void SetHumdrumBuffer(const char *contents)
Copy the data to the humdrum internal buffer.
Options * GetOptionsObj()
Return the Options object of the Toolkit instance.
Definition: toolkit.h:669
void SetCString(const std::string &data)
Copy the data to the cstring internal buffer.
void SetLocale()
Setting the global locale.
void GetHumdrum(std::ostream &output)
Write the Humdrum buffer to the outputstream.
bool RenderToDeviceContext(int pageNo, DeviceContext *deviceContext)
Render the page to the deviceContext.
int GetInputFrom()
Get the input format.
Definition: toolkit.h:724
void ClearHumdrumBuffer()
Clear the Humdrum buffer of any contents.