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 EditResponse();
329 
335  std::string EditStatus();
336 
338 
345 
356  std::string RenderData(const std::string &data, const std::string &jsonOptions);
357 
365  std::string RenderToSVG(int pageNo = 1, bool xmlDeclaration = false);
366 
376  bool RenderToSVGFile(const std::string &filename, int pageNo = 1);
377 
383  std::string RenderToMIDI();
384 
393  bool RenderToMIDIFile(const std::string &filename);
394 
402  std::string RenderToPAE();
403 
414  bool RenderToPAEFile(const std::string &filename);
415 
422  std::string RenderToTimemap(const std::string &jsonOptions = "");
423 
429  std::string RenderToExpansionMap();
430 
440  bool RenderToTimemapFile(const std::string &filename, const std::string &jsonOptions = "");
441 
449  bool RenderToExpansionMapFile(const std::string &filename);
450 
452 
459 
465  std::string GetHumdrum();
466 
472  std::string ConvertMEIToHumdrum(const std::string &meiData);
473 
479  std::string ConvertHumdrumToHumdrum(const std::string &humdrumData);
480 
486  std::string ConvertHumdrumToMIDI(const std::string &humdrumData);
487 
496  bool GetHumdrumFile(const std::string &filename);
497 
499 
506 
516  std::string GetMEI(const std::string &jsonOptions = "");
517 
527  bool SaveFile(const std::string &filename, const std::string &jsonOptions = "");
528 
530 
537 
546  std::string GetDescriptiveFeatures(const std::string &jsonOptions);
547 
554  std::string GetElementsAtTime(int millisec);
555 
564  int GetPageWithElement(const std::string &xmlId);
565 
574  std::string GetElementAttr(const std::string &xmlId);
575 
582  std::string GetNotatedIdForElement(const std::string &xmlId);
583 
590  std::string GetExpansionIdsForElement(const std::string &xmlId);
591 
600  int GetTimeForElement(const std::string &xmlId);
601 
610  std::string GetMIDIValuesForElement(const std::string &xmlId);
611 
621  std::string GetTimesForElement(const std::string &xmlId);
622 
624 
629 
639  void RedoLayout(const std::string &jsonOptions = "");
640 
648 
650 
651  //------------------------------------------------//
652  // Public methods not listed in the documentation //
653  //------------------------------------------------//
654 
661 
669  bool RenderToDeviceContext(int pageNo, DeviceContext *deviceContext);
670 
676  Options *GetOptionsObj() { return m_options; }
677 
683  void SetCString(const std::string &data);
684 
692  const char *GetCString();
693 
699  void GetHumdrum(std::ostream &output);
700 
706  void SetHumdrumBuffer(const char *contents);
707 
715  const char *GetHumdrumBuffer();
716 
723 
731  FileFormat GetInputFrom() { return m_options->GetInputFrom(); }
732 
736  FileFormat GetOutputTo() { return m_options->GetOutputTo(); }
737 
742  void SetLocale();
743  void ResetLocale();
745 
752  void InitClock();
753  void ResetClock();
754  double GetRuntimeInSeconds() const;
755  void LogRuntime() const;
757 
759 
760 protected:
764  FileFormat IdentifyInputFrom(const std::string &data);
765 
769  void PrintOptionUsageOutput(const vrv::Option *option, std::ostream &output) const;
770 
775 
781 
786 
790  bool LoadData(const std::string &data, bool resetLogBuffer);
791 
795  void ResetMidiDoc();
796 
797 private:
798  bool SetFont(const std::string &fontName);
799  bool IsUTF16(const std::string &filename);
800  bool LoadUTF16File(const std::string &filename);
801  bool IsZip(const std::string &filename);
802  bool LoadZipFile(const std::string &filename);
803  bool LoadZipData(const std::vector<unsigned char> &bytes);
804  void GetClassIds(const std::vector<std::string> &classStrings, std::vector<ClassId> &classIds);
805 
812  std::string GetOptions(bool defaultValues) const;
813 
817  void SetViewAndEditor();
818 
823  void SetMidiDoc();
824 
825 public:
826  //
827 protected:
828  Doc m_doc;
829  DocSelection m_docSelection;
830  View m_view;
831 
832 private:
833  Doc *m_midiDoc;
834 
835  Options *m_options;
836 
837  std::optional<std::locale> m_previousLocale;
838 
842  char *m_cString;
843 
848  std::stringstream m_cerrCaptured;
849 
854  std::streambuf *m_cerrOriginalBuf;
855 
856  EditorToolkit *m_editorToolkit;
857 
858 #ifndef NO_RUNTIME
860  RuntimeClock *m_runtimeClock;
861 #endif
862 
863  //----------------//
864  // Static members //
865  //----------------//
866 
867  static char *m_humdrumBuffer;
868 };
869 
870 } // namespace vrv
871 #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:30
This class is a base class of each styling parameter.
Definition: options.h:103
This class contains the document styling parameters.
Definition: options.h:580
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.
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 EditStatus()
Return the editor status - experimental code not to rely on.
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.
std::string EditResponse()
Return the editor response - experimental code not to rely on.
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.
void ResetMidiDoc()
Reset the Doc pointer used for MIDI / Timemap rendering.
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:109
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:676
void SetCString(const std::string &data)
Copy the data to the cstring internal buffer.
void SetLocale()
Setting the global locale.
FileFormat GetInputFrom()
Get the input format.
Definition: toolkit.h:731
void GetHumdrum(std::ostream &output)
Write the Humdrum buffer to the outputstream.
bool RenderToDeviceContext(int pageNo, DeviceContext *deviceContext)
Render the page to the deviceContext.
void ClearHumdrumBuffer()
Clear the Humdrum buffer of any contents.