OSMDOptions.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. import { DrawingParametersEnum, ColoringModes } from "../MusicalScore/Graphical/DrawingParameters";
  2. import { FontStyles } from "../Common/Enums/FontStyles";
  3. /** Possible options for the OpenSheetMusicDisplay constructor and osmd.setOptions(). None are mandatory.
  4. * Note that after using setOptions(), you have to call osmd.render() again to make changes visible.
  5. * Example: osmd.setOptions({defaultColorRest: "#AAAAAA", drawSubtitle: false}); osmd.render();
  6. */
  7. export interface IOSMDOptions {
  8. /** Whether to let Vexflow align rests to preceding or following notes (Vexflow option). Default false (0).
  9. * This can naturally reduce collisions of rest notes with other notes.
  10. * Auto mode (2) only aligns rests when there are multiple voices in a measure, and at least once at the same x-coordinate.
  11. * Auto is the recommended setting, and would be default, if it couldn't in rare cases deteriorate rest placement for existing users.
  12. * The on mode (1) always aligns rests, also changing their position when there is no simultaneous note at the same x-coordinate, which is nonstandard.
  13. */
  14. alignRests?: AlignRestOption | number;
  15. /** Whether to automatically create beams for notes that don't have beams set in XML. */
  16. autoBeam?: boolean;
  17. /** Options for autoBeaming like whether to beam over rests. See AutoBeamOptions interface. */
  18. autoBeamOptions?: AutoBeamOptions;
  19. /** Automatically resize score with canvas size. Default is true. */
  20. autoResize?: boolean;
  21. /** Render Backend, will be SVG if given undefined, "SVG" or "svg", otherwise Canvas. */
  22. backend?: string;
  23. /** Defines the mode that is used for coloring: XML (0), Boomwhacker(1), CustomColorSet (2). Default XML.
  24. * If coloringMode.CustomColorSet (2) is chosen, a coloringSetCustom parameter must be added.
  25. */
  26. coloringMode?: ColoringModes;
  27. /** Set of 8 colors for automatic coloring of 7 notes from C to B + rest note in HTML form (e.g. "#00ff00" for green). */
  28. coloringSetCustom?: string[];
  29. /** Whether to enable coloring noteheads and stems, depending on coloringMode. */
  30. coloringEnabled?: boolean;
  31. /** Whether to color the stems of notes the same as their noteheads. Default false. */
  32. colorStemsLikeNoteheads?: boolean;
  33. /** Default color for a note head (without stem). Default black (undefined).
  34. * Only considered before loading a sample, not before render.
  35. * To change the color after loading a sample and before render, use note(.sourceNote).NoteheadColor.
  36. * The format is Vexflow format, either "#rrggbb" or "#rrggbbtt" where <tt> is transparency. All hex values.
  37. * E.g., a half-transparent red would be "#FF000080", invisible/transparent would be "#00000000" or "#12345600".
  38. */
  39. defaultColorNotehead?: string;
  40. /** Default color for a note stem. Default black (undefined). */
  41. defaultColorStem?: string;
  42. /** Default color for rests. Default black (undefined). */
  43. defaultColorRest?: string;
  44. /** Default color for Labels like title or lyrics. Default black (undefined). */
  45. defaultColorLabel?: string;
  46. /** Default color for labels in the title. Overrides defaultColorLabel for title labels like composer. Default black (undefined). */
  47. defaultColorTitle?: string;
  48. /** Default font used for text and labels, e.g. title or lyrics. Default Times New Roman
  49. * Note that OSMD originally always used Times New Roman, so things like layout and spacing may still be optimized for it.
  50. * Valid options are CSS font families available in the browser used for rendering, e.g. Times New Roman, Helvetica.
  51. */
  52. defaultFontFamily?: string;
  53. /** Default font style, e.g. FontStyles.Bold (1). Default Regular (0). */
  54. defaultFontStyle?: FontStyles;
  55. /** Don't show/load cursor. Will override disableCursor in drawingParameters. */
  56. disableCursor?: boolean;
  57. /** Follow Cursor: Scroll the page when cursor.next() is called and the cursor moves into a new system outside of the current view frame. */
  58. followCursor?: boolean;
  59. /** Broad Parameters like compact or preview mode.
  60. * Also try "compacttight", which is like compact but also reduces margins.
  61. * To see what this mode does and maybe adjust the spacing parameters yourself instead of using the mode,
  62. * see DrawingParameters.ts:setForCompactTightMode().
  63. */
  64. drawingParameters?: string | DrawingParametersEnum;
  65. /** Whether to draw credits (title, subtitle, composer, lyricist) (in future: copyright etc., see <credit>). */
  66. drawCredits?: boolean;
  67. /** Whether to draw the title of the piece. If false, disables drawing Subtitle as well. */
  68. drawTitle?: boolean;
  69. /** Whether to draw the subtitle of the piece. If true, enables drawing Title as well. */
  70. drawSubtitle?: boolean;
  71. /** Whether to draw the composer name (top right of the score). */
  72. drawComposer?: boolean;
  73. /** Whether to draw the lyricist's name, if given (top left of the score). */
  74. drawLyricist?: boolean;
  75. /** Whether to draw metronome marks. Default true. (currently OSMD can only draw one at the beginning) */
  76. drawMetronomeMarks?: boolean;
  77. /** Whether to draw part (instrument) names. Setting this to false also disables drawPartAbbreviations,
  78. * unless explicitly enabled (drawPartNames: false, drawPartAbbreviations: true).
  79. */
  80. drawPartNames?: boolean;
  81. /** Whether to draw part (instrument) name abbreviations each system after the first. Only draws if drawPartNames. Default true. */
  82. drawPartAbbreviations?: boolean;
  83. /** Whether to draw measure numbers (labels). Default true.
  84. * Draws a measure number label at first measure, system start measure, and every [measureNumberInterval] measures.
  85. * See the [measureNumberInterval] option, default is 2.
  86. */
  87. drawMeasureNumbers?: boolean;
  88. /** Whether to only draw measure numbers at the start of a system ("new line"), instead of every [measureNumberInterval] measures. Default false. */
  89. drawMeasureNumbersOnlyAtSystemStart?: boolean;
  90. /** Whether to draw time signatures (e.g. 4/4). Default true. */
  91. drawTimeSignatures?: boolean;
  92. /** The interval of measure numbers to draw, i.e. it draws the measure number above the beginning label every x measures. Default 2. */
  93. measureNumberInterval?: number;
  94. /** Whether to draw fingerings (only left to the note for now). Default true (unless solo part). */
  95. drawFingerings?: boolean;
  96. /** Where to draw fingerings (left, right, above, below, or auto).
  97. * Default left. Auto, above, below experimental (potential collisions because bounding box not correct)
  98. */
  99. fingeringPosition?: string;
  100. /** For above/below fingerings, whether to draw them directly above/below notes (default), or above/below staffline. */
  101. fingeringInsideStafflines?: boolean;
  102. /** Whether to draw hidden/invisible notes (print-object="no" in XML). Default false. Not yet supported. */ // TODO
  103. drawHiddenNotes?: boolean;
  104. /** Whether to draw lyrics (and their extensions and dashes). */
  105. drawLyrics?: boolean;
  106. /** Whether to calculate extra slurs with bezier curves not covered by Vexflow slurs. Default true. */
  107. drawSlurs?: boolean;
  108. /** Only draw measure n to m, where m is the number you specify. */
  109. drawUpToMeasureNumber?: number;
  110. /** Only draw measure n to m, where n is the number you specify. */
  111. drawFromMeasureNumber?: number;
  112. /** Whether to fill measures that don't have notes given in the XML with whole rests (visible = 1, invisible = 2, for layouting). Default No (0). */
  113. fillEmptyMeasuresWithWholeRest?: FillEmptyMeasuresWithWholeRests | number;
  114. /** Whether to set the wanted stem direction by xml (default) or automatically. */
  115. setWantedStemDirectionByXml?: boolean;
  116. /** Whether tuplets are labeled with ratio (e.g. 5:2 instead of 5 for quintuplets). Default false. */
  117. tupletsRatioed?: boolean;
  118. /** Whether all tuplets should be bracketed (e.g. |--5--| instead of 5). Default false.
  119. * If false, only tuplets given as bracketed in XML (bracket="yes") will be bracketed.
  120. */
  121. tupletsBracketed?: boolean;
  122. /** Whether all triplets should be bracketed. Overrides tupletsBracketed for triplets.
  123. * If false, only triplets given as bracketed in XML (bracket="yes") will be bracketed.
  124. * (Bracketing all triplets can be cluttering)
  125. */
  126. tripletsBracketed?: boolean;
  127. /** See OpenSheetMusicDisplay.PageFormatStandards for standard options like "A4 P" or "Endless". Default Endless.
  128. * Uses OpenSheetMusicDisplay.StringToPageFormat(). Unfortunately it would be error-prone to set a PageFormat type directly.
  129. */
  130. pageFormat?: string;
  131. /** A custom page/canvas background color. Default undefined/transparent.
  132. * Example: "#FFFFFF" = white. "#12345600" = transparent.
  133. * This can be useful when you want to export an image with e.g. white background color instead of transparent,
  134. * from a CanvasBackend.
  135. * Note: Using a background color will prevent the cursor from being visible for now (will be fixed at some point).
  136. */
  137. pageBackgroundColor?: string;
  138. /** This makes OSMD render on one single horizontal (staff-)line.
  139. * This option should be set before loading a score. It only starts working after load(),
  140. * calling setOptions() after load and then render() doesn't work in this case.
  141. */
  142. renderSingleHorizontalStaffline?: boolean;
  143. /** Whether to begin a new system ("line break") when given in XML ('new-system="yes"').
  144. * Default false, because OSMD does its own layout that will do line breaks interactively
  145. * at different measures. So this option may result in a system break after a single measure in a system.
  146. */
  147. newSystemFromXML?: boolean;
  148. /** Whether to begin a new page ("page break") when given in XML ('new-page="yes"').
  149. * Default false, because OSMD does its own layout that will do page breaks interactively (when given a PageFormat)
  150. * at different measures. So this option may result in a page break after a single measure on a page.
  151. */
  152. newPageFromXML?: boolean;
  153. /** The cutoff number for rendering percussion clef stafflines as a single line. Default is 4.
  154. * This is number of instruments specified, e.g. a drumset:
  155. * <score-part id="P1">
  156. * <part-name>Drumset</part-name>
  157. * <part-abbreviation>D. Set</part-abbreviation>
  158. * <score-instrument id="P1-I36">
  159. * <instrument-name>Acoustic Bass Drum</instrument-name>
  160. * </score-instrument>
  161. * <score-instrument id="P1-I37">
  162. * <instrument-name>Bass Drum 1</instrument-name>
  163. * </score-instrument>
  164. * <score-instrument id="P1-I38">
  165. * <instrument-name>Side Stick</instrument-name>
  166. * </score-instrument>
  167. * <score-instrument id="P1-I39">
  168. * <instrument-name>Acoustic Snare</instrument-name>
  169. * </score-instrument>
  170. * ...
  171. * Would still render as 5 stafflines by default, since we have 4 (or greater) instruments in this part.
  172. * While a snare:
  173. * <score-part id="P2">
  174. * <part-name>Concert Snare Drum</part-name>
  175. * <part-abbreviation>Con. Sn.</part-abbreviation>
  176. * <score-instrument id="P2-I38">
  177. * <instrument-name>Side Stick</instrument-name>
  178. * </score-instrument>
  179. * <score-instrument id="P2-I39">
  180. * <instrument-name>Acoustic Snare</instrument-name>
  181. * </score-instrument>
  182. * ...
  183. * Would render with 1 line on the staff, since we only have 2 voices.
  184. * If this value is 0, the feature is turned off.
  185. * If this value is -1, it will render all percussion clefs as a single line.
  186. */
  187. percussionOneLineCutoff?: number;
  188. /** This property is only active if the above property is active (percussionOneLineCutoff)
  189. * This is the cutoff for forcing all voices to the single line, instead of rendering them at different
  190. * positions above/below the line.
  191. * The default is 3, so if a part has less than voices, all of them will be rendered on the line.
  192. * This is for cases like a Concert snare, which has multiple 'instruments' available (snare, side stick)
  193. * should still render only on the line since there is no ambiguity.
  194. * If this value is 0, the feature is turned off.
  195. * IF this value is -1, it will render all percussion clef voices on the single line.
  196. */
  197. percussionForceVoicesOneLineCutoff?: number;
  198. /** The softmaxFactor for Vexflow's formatter. Default is 5, default in Vexflow is 100 (voice.js).
  199. * Lowering this factor makes the spacing between individual notes smaller (especially from one half note to the next).
  200. * So, to get more compact scores, try lowering this value (or set osmd.zoom, which simply scales),
  201. * or try 100 for a more expansive layout.
  202. * Setting this is the same as setting osmd.EngravingRules.SoftmaxFactorVexFlow.
  203. */
  204. spacingFactorSoftmax?: number;
  205. /**
  206. * Number in pixels, of spacing between multi-line labels
  207. */
  208. spacingBetweenTextLines?: number;
  209. /**
  210. * Set to true if the last system line should be streched across the whole page just as the other systems. Default is false
  211. */
  212. stretchLastSystemLine?: boolean;
  213. }
  214. export enum AlignRestOption {
  215. Never = 0, // false should also work
  216. Always = 1, // true should also work
  217. Auto = 2
  218. }
  219. export enum FillEmptyMeasuresWithWholeRests {
  220. No = 0,
  221. YesVisible = 1,
  222. YesInvisible = 2 // fill with invisible whole rests
  223. }
  224. export enum BackendType {
  225. SVG = 0,
  226. Canvas = 1
  227. }
  228. /** Handles [[IOSMDOptions]], e.g. returning default options with OSMDOptionsStandard() */
  229. export class OSMDOptions {
  230. /** Returns the default options for OSMD.
  231. * These are e.g. used if no options are given in the [[OpenSheetMusicDisplay]] constructor.
  232. */
  233. public static OSMDOptionsStandard(): IOSMDOptions {
  234. return {
  235. autoResize: true,
  236. backend: "svg",
  237. drawingParameters: DrawingParametersEnum.default,
  238. };
  239. }
  240. public static BackendTypeFromString(value: string): BackendType {
  241. if (value && value.toLowerCase() === "canvas") {
  242. return BackendType.Canvas;
  243. } else {
  244. return BackendType.SVG;
  245. }
  246. }
  247. }
  248. export interface AutoBeamOptions {
  249. /** Whether to extend beams over rests. Default false. */
  250. beam_rests?: boolean;
  251. /** Whether to extend beams only over rests that are in the middle of a potential beam. Default false. */
  252. beam_middle_rests_only?: boolean;
  253. /** Whether to maintain stem direction of autoBeamed notes. Discouraged, reduces beams. Default false. */
  254. maintain_stem_directions?: boolean;
  255. /** Groups of notes (fractions) to beam within a measure.
  256. * List of fractions, each fraction being [nominator, denominator].
  257. * E.g. [[3,4],[1,4]] will beam the first 3 quarters of a measure, then the last quarter.
  258. */
  259. groups?: [number[]];
  260. }