vexflow-native 0.1.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (259) hide show
  1. package/README.md +286 -11
  2. package/lib/module/base/FontManager.js +139 -0
  3. package/lib/module/base/FontManager.js.map +1 -0
  4. package/lib/module/base/TextMeasureContext.js +46 -0
  5. package/lib/module/base/TextMeasureContext.js.map +1 -0
  6. package/lib/module/base/VexflowCanvas.js +48 -0
  7. package/lib/module/base/VexflowCanvas.js.map +1 -0
  8. package/lib/module/base/VexflowColorScheme.js +16 -0
  9. package/lib/module/base/VexflowColorScheme.js.map +1 -0
  10. package/lib/module/base/VexflowRecordingContext.js +425 -0
  11. package/lib/module/base/VexflowRecordingContext.js.map +1 -0
  12. package/lib/module/base/VexflowRecordingReplay.js +140 -0
  13. package/lib/module/base/VexflowRecordingReplay.js.map +1 -0
  14. package/lib/module/base/VexflowRecordingTypes.js +4 -0
  15. package/lib/module/base/VexflowRecordingTypes.js.map +1 -0
  16. package/lib/module/base/index.js +8 -0
  17. package/lib/module/base/index.js.map +1 -0
  18. package/lib/module/base/setupVexflowReactNative.js +96 -0
  19. package/lib/module/base/setupVexflowReactNative.js.map +1 -0
  20. package/lib/module/base/types.js +4 -0
  21. package/lib/module/base/types.js.map +1 -0
  22. package/lib/module/base/utils.js +18 -0
  23. package/lib/module/base/utils.js.map +1 -0
  24. package/lib/module/musicxml/MusicXmlParser/InternalTypes.js +4 -0
  25. package/lib/module/musicxml/MusicXmlParser/InternalTypes.js.map +1 -0
  26. package/lib/module/musicxml/MusicXmlParser/MeasureParser.js +269 -0
  27. package/lib/module/musicxml/MusicXmlParser/MeasureParser.js.map +1 -0
  28. package/lib/module/musicxml/MusicXmlParser/MusicXmlDirections.js +62 -0
  29. package/lib/module/musicxml/MusicXmlParser/MusicXmlDirections.js.map +1 -0
  30. package/lib/module/musicxml/MusicXmlParser/MusicXmlTiming.js +124 -0
  31. package/lib/module/musicxml/MusicXmlParser/MusicXmlTiming.js.map +1 -0
  32. package/lib/module/musicxml/MusicXmlParser/PartParser.js +41 -0
  33. package/lib/module/musicxml/MusicXmlParser/PartParser.js.map +1 -0
  34. package/lib/module/musicxml/MusicXmlParser/Spanners.js +149 -0
  35. package/lib/module/musicxml/MusicXmlParser/Spanners.js.map +1 -0
  36. package/lib/module/musicxml/MusicXmlParser/XmlOrder.js +106 -0
  37. package/lib/module/musicxml/MusicXmlParser/XmlOrder.js.map +1 -0
  38. package/lib/module/musicxml/MusicXmlParser/index.js +110 -0
  39. package/lib/module/musicxml/MusicXmlParser/index.js.map +1 -0
  40. package/lib/module/musicxml/MusicXmlParser/mappers.js +312 -0
  41. package/lib/module/musicxml/MusicXmlParser/mappers.js.map +1 -0
  42. package/lib/module/musicxml/MusicXmlParser/types.js +10 -0
  43. package/lib/module/musicxml/MusicXmlParser/types.js.map +1 -0
  44. package/lib/module/musicxml/index.js +4 -0
  45. package/lib/module/musicxml/index.js.map +1 -0
  46. package/lib/module/musicxml/testfiles/lg-8102429.xml +79620 -0
  47. package/lib/module/renderer/Layout/DocumentLayout.js +156 -0
  48. package/lib/module/renderer/Layout/DocumentLayout.js.map +1 -0
  49. package/lib/module/renderer/Layout/LayoutMetrics.js +13 -0
  50. package/lib/module/renderer/Layout/LayoutMetrics.js.map +1 -0
  51. package/lib/module/renderer/Layout/VerticalSpacing.js +69 -0
  52. package/lib/module/renderer/Layout/VerticalSpacing.js.map +1 -0
  53. package/lib/module/renderer/ScoreRenderer/ScoreRenderer.js +232 -0
  54. package/lib/module/renderer/ScoreRenderer/ScoreRenderer.js.map +1 -0
  55. package/lib/module/renderer/ScoreRenderer/ScoreScrollbar.js +108 -0
  56. package/lib/module/renderer/ScoreRenderer/ScoreScrollbar.js.map +1 -0
  57. package/lib/module/renderer/ScoreRenderer/index.js +7 -0
  58. package/lib/module/renderer/ScoreRenderer/index.js.map +1 -0
  59. package/lib/module/renderer/ScoreRenderer/types.js +4 -0
  60. package/lib/module/renderer/ScoreRenderer/types.js.map +1 -0
  61. package/lib/module/renderer/ScoreRenderer/useScoreScroll.js +77 -0
  62. package/lib/module/renderer/ScoreRenderer/useScoreScroll.js.map +1 -0
  63. package/lib/module/renderer/colorScheme.js +17 -0
  64. package/lib/module/renderer/colorScheme.js.map +1 -0
  65. package/lib/module/renderer/constants.js +18 -0
  66. package/lib/module/renderer/constants.js.map +1 -0
  67. package/lib/module/renderer/index.js +6 -0
  68. package/lib/module/renderer/index.js.map +1 -0
  69. package/lib/module/renderer/layout.js +153 -0
  70. package/lib/module/renderer/layout.js.map +1 -0
  71. package/lib/module/renderer/measure.js +202 -0
  72. package/lib/module/renderer/measure.js.map +1 -0
  73. package/lib/module/renderer/render.js +163 -0
  74. package/lib/module/renderer/render.js.map +1 -0
  75. package/lib/module/renderer/scoreParsing.js +230 -0
  76. package/lib/module/renderer/scoreParsing.js.map +1 -0
  77. package/lib/module/renderer/types.js +4 -0
  78. package/lib/module/renderer/types.js.map +1 -0
  79. package/lib/module/renderer/useScoreRecording.js +112 -0
  80. package/lib/module/renderer/useScoreRecording.js.map +1 -0
  81. package/lib/module/renderer/viewport.js +26 -0
  82. package/lib/module/renderer/viewport.js.map +1 -0
  83. package/lib/module/shared/Logger.js +67 -0
  84. package/lib/module/shared/Logger.js.map +1 -0
  85. package/lib/module/state/index.js +4 -0
  86. package/lib/module/state/index.js.map +1 -0
  87. package/lib/module/state/types.js +2 -0
  88. package/lib/module/state/types.js.map +1 -0
  89. package/lib/module/types.js +7 -0
  90. package/lib/module/types.js.map +1 -1
  91. package/lib/module/utils.js +42 -77
  92. package/lib/module/utils.js.map +1 -1
  93. package/lib/typescript/src/base/FontManager.d.ts +23 -0
  94. package/lib/typescript/src/base/FontManager.d.ts.map +1 -0
  95. package/lib/typescript/src/base/TextMeasureContext.d.ts +22 -0
  96. package/lib/typescript/src/base/TextMeasureContext.d.ts.map +1 -0
  97. package/lib/typescript/src/base/VexflowCanvas.d.ts +5 -0
  98. package/lib/typescript/src/base/VexflowCanvas.d.ts.map +1 -0
  99. package/lib/typescript/src/base/VexflowColorScheme.d.ts +7 -0
  100. package/lib/typescript/src/base/VexflowColorScheme.d.ts.map +1 -0
  101. package/lib/typescript/src/base/VexflowRecordingContext.d.ts +74 -0
  102. package/lib/typescript/src/base/VexflowRecordingContext.d.ts.map +1 -0
  103. package/lib/typescript/src/base/VexflowRecordingReplay.d.ts +4 -0
  104. package/lib/typescript/src/base/VexflowRecordingReplay.d.ts.map +1 -0
  105. package/lib/typescript/src/base/VexflowRecordingTypes.d.ts +94 -0
  106. package/lib/typescript/src/base/VexflowRecordingTypes.d.ts.map +1 -0
  107. package/lib/typescript/src/base/index.d.ts +6 -0
  108. package/lib/typescript/src/base/index.d.ts.map +1 -0
  109. package/lib/typescript/src/base/setupVexflowReactNative.d.ts +11 -0
  110. package/lib/typescript/src/base/setupVexflowReactNative.d.ts.map +1 -0
  111. package/lib/typescript/src/base/types.d.ts +13 -0
  112. package/lib/typescript/src/base/types.d.ts.map +1 -0
  113. package/lib/typescript/src/base/utils.d.ts +2 -0
  114. package/lib/typescript/src/base/utils.d.ts.map +1 -0
  115. package/lib/typescript/src/musicxml/MusicXmlParser/InternalTypes.d.ts +30 -0
  116. package/lib/typescript/src/musicxml/MusicXmlParser/InternalTypes.d.ts.map +1 -0
  117. package/lib/typescript/src/musicxml/MusicXmlParser/MeasureParser.d.ts +4 -0
  118. package/lib/typescript/src/musicxml/MusicXmlParser/MeasureParser.d.ts.map +1 -0
  119. package/lib/typescript/src/musicxml/MusicXmlParser/MusicXmlDirections.d.ts +5 -0
  120. package/lib/typescript/src/musicxml/MusicXmlParser/MusicXmlDirections.d.ts.map +1 -0
  121. package/lib/typescript/src/musicxml/MusicXmlParser/MusicXmlTiming.d.ts +6 -0
  122. package/lib/typescript/src/musicxml/MusicXmlParser/MusicXmlTiming.d.ts.map +1 -0
  123. package/lib/typescript/src/musicxml/MusicXmlParser/PartParser.d.ts +8 -0
  124. package/lib/typescript/src/musicxml/MusicXmlParser/PartParser.d.ts.map +1 -0
  125. package/lib/typescript/src/musicxml/MusicXmlParser/Spanners.d.ts +4 -0
  126. package/lib/typescript/src/musicxml/MusicXmlParser/Spanners.d.ts.map +1 -0
  127. package/lib/typescript/src/musicxml/MusicXmlParser/XmlOrder.d.ts +20 -0
  128. package/lib/typescript/src/musicxml/MusicXmlParser/XmlOrder.d.ts.map +1 -0
  129. package/lib/typescript/src/musicxml/MusicXmlParser/index.d.ts +5 -0
  130. package/lib/typescript/src/musicxml/MusicXmlParser/index.d.ts.map +1 -0
  131. package/lib/typescript/src/musicxml/MusicXmlParser/mappers.d.ts +15 -0
  132. package/lib/typescript/src/musicxml/MusicXmlParser/mappers.d.ts.map +1 -0
  133. package/lib/typescript/src/musicxml/MusicXmlParser/types.d.ts +13 -0
  134. package/lib/typescript/src/musicxml/MusicXmlParser/types.d.ts.map +1 -0
  135. package/lib/typescript/src/musicxml/index.d.ts +2 -0
  136. package/lib/typescript/src/musicxml/index.d.ts.map +1 -0
  137. package/lib/typescript/src/renderer/Layout/DocumentLayout.d.ts +10 -0
  138. package/lib/typescript/src/renderer/Layout/DocumentLayout.d.ts.map +1 -0
  139. package/lib/typescript/src/renderer/Layout/LayoutMetrics.d.ts +6 -0
  140. package/lib/typescript/src/renderer/Layout/LayoutMetrics.d.ts.map +1 -0
  141. package/lib/typescript/src/renderer/Layout/VerticalSpacing.d.ts +9 -0
  142. package/lib/typescript/src/renderer/Layout/VerticalSpacing.d.ts.map +1 -0
  143. package/lib/typescript/src/renderer/ScoreRenderer/ScoreRenderer.d.ts +14 -0
  144. package/lib/typescript/src/renderer/ScoreRenderer/ScoreRenderer.d.ts.map +1 -0
  145. package/lib/typescript/src/renderer/ScoreRenderer/ScoreScrollbar.d.ts +24 -0
  146. package/lib/typescript/src/renderer/ScoreRenderer/ScoreScrollbar.d.ts.map +1 -0
  147. package/lib/typescript/src/renderer/ScoreRenderer/index.d.ts +5 -0
  148. package/lib/typescript/src/renderer/ScoreRenderer/index.d.ts.map +1 -0
  149. package/lib/typescript/src/renderer/ScoreRenderer/types.d.ts +12 -0
  150. package/lib/typescript/src/renderer/ScoreRenderer/types.d.ts.map +1 -0
  151. package/lib/typescript/src/renderer/ScoreRenderer/useScoreScroll.d.ts +13 -0
  152. package/lib/typescript/src/renderer/ScoreRenderer/useScoreScroll.d.ts.map +1 -0
  153. package/lib/typescript/src/renderer/colorScheme.d.ts +9 -0
  154. package/lib/typescript/src/renderer/colorScheme.d.ts.map +1 -0
  155. package/lib/typescript/src/renderer/constants.d.ts +5 -0
  156. package/lib/typescript/src/renderer/constants.d.ts.map +1 -0
  157. package/lib/typescript/src/renderer/index.d.ts +4 -0
  158. package/lib/typescript/src/renderer/index.d.ts.map +1 -0
  159. package/lib/typescript/src/renderer/layout.d.ts +56 -0
  160. package/lib/typescript/src/renderer/layout.d.ts.map +1 -0
  161. package/lib/typescript/src/renderer/measure.d.ts +23 -0
  162. package/lib/typescript/src/renderer/measure.d.ts.map +1 -0
  163. package/lib/typescript/src/renderer/render.d.ts +9 -0
  164. package/lib/typescript/src/renderer/render.d.ts.map +1 -0
  165. package/lib/typescript/src/renderer/scoreParsing.d.ts +76 -0
  166. package/lib/typescript/src/renderer/scoreParsing.d.ts.map +1 -0
  167. package/lib/typescript/src/renderer/types.d.ts +51 -0
  168. package/lib/typescript/src/renderer/types.d.ts.map +1 -0
  169. package/lib/typescript/src/renderer/useScoreRecording.d.ts +21 -0
  170. package/lib/typescript/src/renderer/useScoreRecording.d.ts.map +1 -0
  171. package/lib/typescript/src/renderer/viewport.d.ts +3 -0
  172. package/lib/typescript/src/renderer/viewport.d.ts.map +1 -0
  173. package/lib/typescript/src/shared/Logger.d.ts +18 -0
  174. package/lib/typescript/src/shared/Logger.d.ts.map +1 -0
  175. package/lib/typescript/src/state/index.d.ts +2 -0
  176. package/lib/typescript/src/state/index.d.ts.map +1 -0
  177. package/lib/typescript/src/state/types.d.ts +190 -0
  178. package/lib/typescript/src/state/types.d.ts.map +1 -0
  179. package/lib/typescript/src/types.d.ts +0 -6
  180. package/lib/typescript/src/types.d.ts.map +1 -1
  181. package/lib/typescript/src/utils.d.ts +0 -6
  182. package/lib/typescript/src/utils.d.ts.map +1 -1
  183. package/package.json +41 -13
  184. package/src/base/FontManager.ts +206 -0
  185. package/src/base/TextMeasureContext.ts +59 -0
  186. package/src/base/VexflowCanvas.tsx +60 -0
  187. package/src/base/VexflowColorScheme.ts +32 -0
  188. package/src/base/VexflowRecordingContext.ts +495 -0
  189. package/src/base/VexflowRecordingReplay.ts +211 -0
  190. package/src/base/VexflowRecordingTypes.ts +117 -0
  191. package/src/base/index.ts +6 -0
  192. package/src/base/setupVexflowReactNative.ts +124 -0
  193. package/src/base/types.ts +17 -0
  194. package/src/base/utils.ts +21 -0
  195. package/src/musicxml/MusicXmlParser/InternalTypes.ts +41 -0
  196. package/src/musicxml/MusicXmlParser/MeasureParser.ts +469 -0
  197. package/src/musicxml/MusicXmlParser/MusicXmlDirections.ts +84 -0
  198. package/src/musicxml/MusicXmlParser/MusicXmlTiming.ts +153 -0
  199. package/src/musicxml/MusicXmlParser/PartParser.ts +69 -0
  200. package/src/musicxml/MusicXmlParser/Spanners.ts +215 -0
  201. package/src/musicxml/MusicXmlParser/XmlOrder.ts +170 -0
  202. package/src/musicxml/MusicXmlParser/index.ts +144 -0
  203. package/src/musicxml/MusicXmlParser/mappers.ts +307 -0
  204. package/src/musicxml/MusicXmlParser/types.ts +24 -0
  205. package/src/musicxml/index.ts +5 -0
  206. package/src/musicxml/testfiles/lg-8102429.xml +79620 -0
  207. package/src/renderer/Layout/DocumentLayout.ts +240 -0
  208. package/src/renderer/Layout/LayoutMetrics.ts +21 -0
  209. package/src/renderer/Layout/VerticalSpacing.ts +122 -0
  210. package/src/renderer/ScoreRenderer/ScoreRenderer.tsx +310 -0
  211. package/src/renderer/ScoreRenderer/ScoreScrollbar.tsx +231 -0
  212. package/src/renderer/ScoreRenderer/index.ts +13 -0
  213. package/src/renderer/ScoreRenderer/types.ts +14 -0
  214. package/src/renderer/ScoreRenderer/useScoreScroll.ts +146 -0
  215. package/src/renderer/colorScheme.ts +28 -0
  216. package/src/renderer/constants.ts +19 -0
  217. package/src/renderer/index.ts +3 -0
  218. package/src/renderer/layout.ts +309 -0
  219. package/src/renderer/measure.tsx +304 -0
  220. package/src/renderer/render.tsx +281 -0
  221. package/src/renderer/scoreParsing.tsx +352 -0
  222. package/src/renderer/types.ts +60 -0
  223. package/src/renderer/useScoreRecording.ts +177 -0
  224. package/src/renderer/viewport.ts +45 -0
  225. package/src/shared/Logger.ts +85 -0
  226. package/src/state/index.ts +1 -0
  227. package/src/state/types.ts +295 -0
  228. package/src/types.ts +6 -6
  229. package/src/utils.tsx +32 -98
  230. package/lib/module/SkiaVexflowContext.js +0 -285
  231. package/lib/module/SkiaVexflowContext.js.map +0 -1
  232. package/lib/module/VexflowCanvas.js +0 -101
  233. package/lib/module/VexflowCanvas.js.map +0 -1
  234. package/lib/module/colorUtils.js +0 -34
  235. package/lib/module/colorUtils.js.map +0 -1
  236. package/lib/module/colorUtils.test.js +0 -37
  237. package/lib/module/colorUtils.test.js.map +0 -1
  238. package/lib/module/constants.js +0 -30
  239. package/lib/module/constants.js.map +0 -1
  240. package/lib/module/index.js +0 -8
  241. package/lib/module/index.js.map +0 -1
  242. package/lib/typescript/src/SkiaVexflowContext.d.ts +0 -101
  243. package/lib/typescript/src/SkiaVexflowContext.d.ts.map +0 -1
  244. package/lib/typescript/src/VexflowCanvas.d.ts +0 -15
  245. package/lib/typescript/src/VexflowCanvas.d.ts.map +0 -1
  246. package/lib/typescript/src/colorUtils.d.ts +0 -2
  247. package/lib/typescript/src/colorUtils.d.ts.map +0 -1
  248. package/lib/typescript/src/colorUtils.test.d.ts +0 -2
  249. package/lib/typescript/src/colorUtils.test.d.ts.map +0 -1
  250. package/lib/typescript/src/constants.d.ts +0 -21
  251. package/lib/typescript/src/constants.d.ts.map +0 -1
  252. package/lib/typescript/src/index.d.ts +0 -7
  253. package/lib/typescript/src/index.d.ts.map +0 -1
  254. package/src/SkiaVexflowContext.ts +0 -462
  255. package/src/VexflowCanvas.tsx +0 -140
  256. package/src/colorUtils.test.ts +0 -52
  257. package/src/colorUtils.ts +0 -55
  258. package/src/constants.tsx +0 -34
  259. package/src/index.ts +0 -8
@@ -0,0 +1,240 @@
1
+ import type { MeasuredScore } from '../measure';
2
+ import type { RendererPoint, ScoreOptions } from '../types';
3
+ import type {
4
+ GroupLayoutContext,
5
+ GroupLayoutResult,
6
+ MeasureLayoutPlan,
7
+ MeasuredGroupMeasure,
8
+ SystemLayoutPlan,
9
+ } from '../layout';
10
+ import { getSystemGap } from './LayoutMetrics';
11
+ import { resolveSystemVerticalLayout } from './VerticalSpacing';
12
+
13
+ type DocumentLine = {
14
+ measures: MeasuredGroupMeasure[];
15
+ intrinsicWidth: number;
16
+ };
17
+
18
+ export function resolveDocumentEvenMeasureWidth(
19
+ measuredScore: MeasuredScore,
20
+ availableWidth: number
21
+ ): {
22
+ measuresPerFullLine: number;
23
+ equalMeasureWidth: number;
24
+ } {
25
+ const widestMeasure = Math.max(measuredScore.maxIntrinsicNoteWidth, 1);
26
+
27
+ if (availableWidth <= 0) {
28
+ return {
29
+ measuresPerFullLine: 1,
30
+ equalMeasureWidth: widestMeasure,
31
+ };
32
+ }
33
+
34
+ const measuresPerFullLine = Math.max(
35
+ 1,
36
+ Math.floor(availableWidth / widestMeasure)
37
+ );
38
+
39
+ return {
40
+ measuresPerFullLine,
41
+ equalMeasureWidth: availableWidth / measuresPerFullLine,
42
+ };
43
+ }
44
+
45
+ export function layoutDocumentEvenGroup(
46
+ group: GroupLayoutContext,
47
+ origin: RendererPoint,
48
+ availableWidth: number,
49
+ equalMeasureWidth: number,
50
+ measuresPerFullLine: number,
51
+ options: ScoreOptions
52
+ ): GroupLayoutResult {
53
+ const systems: SystemLayoutPlan[] = [];
54
+ const measures: MeasureLayoutPlan[] = [];
55
+
56
+ if (group.measures.length === 0) {
57
+ return {
58
+ groupId: group.groupId,
59
+ systems,
60
+ measures,
61
+ nextY: origin.y,
62
+ };
63
+ }
64
+
65
+ let cursorY = origin.y;
66
+ const systemGap = getSystemGap(options.spacing.staffGap);
67
+
68
+ for (
69
+ let systemIndex = 0, startIndex = 0;
70
+ startIndex < group.measures.length;
71
+ systemIndex++, startIndex += measuresPerFullLine
72
+ ) {
73
+ const chunk = group.measures.slice(
74
+ startIndex,
75
+ startIndex + measuresPerFullLine
76
+ );
77
+ const verticalLayout = resolveSystemVerticalLayout(
78
+ chunk,
79
+ group.staves.length,
80
+ options.spacing.staffGap
81
+ );
82
+ let cursorX = origin.x;
83
+
84
+ systems.push({
85
+ groupId: group.groupId,
86
+ systemIndex,
87
+ x: origin.x,
88
+ y: cursorY,
89
+ width:
90
+ chunk.length === measuresPerFullLine
91
+ ? availableWidth
92
+ : chunk.length * equalMeasureWidth,
93
+ height: verticalLayout.height,
94
+ staffCount: group.staves.length,
95
+ staffYOffsets: verticalLayout.staffYOffsets,
96
+ measureIndices: chunk.map((measure) => measure.measureIndex),
97
+ });
98
+
99
+ for (const measure of chunk) {
100
+ measures.push({
101
+ groupId: group.groupId,
102
+ measureIndex: measure.measureIndex,
103
+ x: cursorX,
104
+ y: cursorY,
105
+ width: equalMeasureWidth,
106
+ height: verticalLayout.height,
107
+ staffYOffsets: verticalLayout.staffYOffsets,
108
+ systemIndex,
109
+ });
110
+
111
+ cursorX += equalMeasureWidth;
112
+ }
113
+
114
+ cursorY += verticalLayout.height + systemGap;
115
+ }
116
+
117
+ return {
118
+ groupId: group.groupId,
119
+ systems,
120
+ measures,
121
+ nextY: cursorY,
122
+ };
123
+ }
124
+
125
+ export function layoutDocumentGroup(
126
+ group: GroupLayoutContext,
127
+ origin: RendererPoint,
128
+ availableWidth: number,
129
+ options: ScoreOptions
130
+ ): GroupLayoutResult {
131
+ const systems: SystemLayoutPlan[] = [];
132
+ const measures: MeasureLayoutPlan[] = [];
133
+
134
+ if (group.measures.length === 0) {
135
+ return {
136
+ groupId: group.groupId,
137
+ systems,
138
+ measures,
139
+ nextY: origin.y,
140
+ };
141
+ }
142
+
143
+ const lines = splitDocumentLines(group.measures, availableWidth);
144
+ let cursorY = origin.y;
145
+ const systemGap = getSystemGap(options.spacing.staffGap);
146
+
147
+ for (const [systemIndex, line] of lines.entries()) {
148
+ const lineWidth = availableWidth > 0 ? availableWidth : line.intrinsicWidth;
149
+ const stretchRatio = lineWidth / line.intrinsicWidth;
150
+ const verticalLayout = resolveSystemVerticalLayout(
151
+ line.measures,
152
+ group.staves.length,
153
+ options.spacing.staffGap
154
+ );
155
+ let cursorX = origin.x;
156
+
157
+ systems.push({
158
+ groupId: group.groupId,
159
+ systemIndex,
160
+ x: origin.x,
161
+ y: cursorY,
162
+ width: lineWidth,
163
+ height: verticalLayout.height,
164
+ staffCount: group.staves.length,
165
+ staffYOffsets: verticalLayout.staffYOffsets,
166
+ measureIndices: line.measures.map((measure) => measure.measureIndex),
167
+ });
168
+
169
+ for (const measure of line.measures) {
170
+ const width = getMeasureLineWidth(measure) * stretchRatio;
171
+
172
+ measures.push({
173
+ groupId: group.groupId,
174
+ measureIndex: measure.measureIndex,
175
+ x: cursorX,
176
+ y: cursorY,
177
+ width,
178
+ height: verticalLayout.height,
179
+ staffYOffsets: verticalLayout.staffYOffsets,
180
+ systemIndex,
181
+ });
182
+
183
+ cursorX += width;
184
+ }
185
+
186
+ cursorY += verticalLayout.height + systemGap;
187
+ }
188
+
189
+ return {
190
+ groupId: group.groupId,
191
+ systems,
192
+ measures,
193
+ nextY: cursorY,
194
+ };
195
+ }
196
+
197
+ function splitDocumentLines(
198
+ measures: MeasuredGroupMeasure[],
199
+ availableWidth: number
200
+ ): DocumentLine[] {
201
+ const lines: DocumentLine[] = [];
202
+ let lineMeasures: MeasuredGroupMeasure[] = [];
203
+ let lineWidth = 0;
204
+
205
+ for (const measure of measures) {
206
+ const measureWidth = getMeasureLineWidth(measure);
207
+
208
+ if (lineMeasures.length === 0) {
209
+ lineMeasures = [measure];
210
+ lineWidth = measureWidth;
211
+ continue;
212
+ }
213
+
214
+ if (availableWidth > 0 && lineWidth + measureWidth > availableWidth) {
215
+ lines.push({
216
+ measures: lineMeasures,
217
+ intrinsicWidth: lineWidth,
218
+ });
219
+ lineMeasures = [measure];
220
+ lineWidth = measureWidth;
221
+ continue;
222
+ }
223
+
224
+ lineMeasures.push(measure);
225
+ lineWidth += measureWidth;
226
+ }
227
+
228
+ if (lineMeasures.length > 0) {
229
+ lines.push({
230
+ measures: lineMeasures,
231
+ intrinsicWidth: lineWidth,
232
+ });
233
+ }
234
+
235
+ return lines;
236
+ }
237
+
238
+ function getMeasureLineWidth(measure: MeasuredGroupMeasure): number {
239
+ return Math.max(measure.intrinsicWidth, 1);
240
+ }
@@ -0,0 +1,21 @@
1
+ import type { RendererRect, ScoreOptions } from '../types';
2
+
3
+ const MIN_SYSTEM_GAP = 82;
4
+ const SYSTEM_GAP_DIVISOR = 8;
5
+
6
+ export const VEXFLOW_STAVE_TOP_LINE_OFFSET = 40;
7
+ export const VEXFLOW_STAVE_BOTTOM_LINE_OFFSET = 80;
8
+
9
+ export function getAvailableDocumentWidth(
10
+ viewport: RendererRect,
11
+ options: ScoreOptions
12
+ ): number {
13
+ return Math.max(
14
+ 0,
15
+ viewport.width - options.insets.left - options.insets.right
16
+ );
17
+ }
18
+
19
+ export function getSystemGap(staffGap: number): number {
20
+ return Math.max(MIN_SYSTEM_GAP, staffGap / SYSTEM_GAP_DIVISOR);
21
+ }
@@ -0,0 +1,122 @@
1
+ import type { StaffVerticalBounds } from '../measure';
2
+ import type { MeasuredGroupMeasure } from '../layout';
3
+ import {
4
+ VEXFLOW_STAVE_BOTTOM_LINE_OFFSET,
5
+ VEXFLOW_STAVE_TOP_LINE_OFFSET,
6
+ } from './LayoutMetrics';
7
+
8
+ const STAFF_ARTIFACT_CLEARANCE = 12;
9
+
10
+ export interface SystemVerticalLayout {
11
+ height: number;
12
+ staffYOffsets: number[];
13
+ visibleBottom: number;
14
+ visibleTop: number;
15
+ }
16
+
17
+ export function resolveSystemVerticalLayout(
18
+ measures: MeasuredGroupMeasure[],
19
+ staffCount: number,
20
+ staffGap: number
21
+ ): SystemVerticalLayout {
22
+ const staffBounds = resolveStaffBounds(measures, staffCount);
23
+ const rawStaffYOffsets = resolveRawStaffYOffsets(staffBounds, staffGap);
24
+ const rawVisibleTop = getVisibleTop(staffBounds, rawStaffYOffsets);
25
+ const rawVisibleBottom = getVisibleBottom(staffBounds, rawStaffYOffsets);
26
+ const topShift = rawVisibleTop < 0 ? -rawVisibleTop : 0;
27
+ const staffYOffsets = rawStaffYOffsets.map((offset) => offset + topShift);
28
+ const visibleTop = rawVisibleTop + topShift;
29
+ const visibleBottom = rawVisibleBottom + topShift;
30
+
31
+ return {
32
+ height: Math.max(0, visibleBottom),
33
+ staffYOffsets,
34
+ visibleBottom,
35
+ visibleTop,
36
+ };
37
+ }
38
+
39
+ function resolveStaffBounds(
40
+ measures: MeasuredGroupMeasure[],
41
+ staffCount: number
42
+ ): StaffVerticalBounds[] {
43
+ return Array.from({ length: staffCount }, (_, staffIndex) => {
44
+ const bounds = measures
45
+ .map((measure) => measure.staffBounds[staffIndex])
46
+ .filter((staffBounds): staffBounds is StaffVerticalBounds =>
47
+ Boolean(staffBounds)
48
+ );
49
+
50
+ return bounds.reduce(
51
+ (merged, staffBounds) => ({
52
+ top: Math.min(merged.top, staffBounds.top),
53
+ bottom: Math.max(merged.bottom, staffBounds.bottom),
54
+ }),
55
+ defaultStaffBounds()
56
+ );
57
+ });
58
+ }
59
+
60
+ function resolveRawStaffYOffsets(
61
+ staffBounds: StaffVerticalBounds[],
62
+ staffGap: number
63
+ ): number[] {
64
+ if (staffBounds.length === 0) {
65
+ return [];
66
+ }
67
+
68
+ const offsets = [0];
69
+
70
+ for (let staffIndex = 1; staffIndex < staffBounds.length; staffIndex++) {
71
+ const previousBounds = staffBounds[staffIndex - 1]!;
72
+ const currentBounds = staffBounds[staffIndex]!;
73
+ const previousOffset = offsets[staffIndex - 1]!;
74
+ const minimumOriginOffset = previousOffset + staffGap;
75
+ const minimumCollisionFreeOffset =
76
+ previousOffset +
77
+ previousBounds.bottom +
78
+ STAFF_ARTIFACT_CLEARANCE -
79
+ currentBounds.top;
80
+
81
+ offsets.push(Math.max(minimumOriginOffset, minimumCollisionFreeOffset));
82
+ }
83
+
84
+ return offsets;
85
+ }
86
+
87
+ function getVisibleTop(
88
+ staffBounds: StaffVerticalBounds[],
89
+ staffYOffsets: number[]
90
+ ): number {
91
+ if (staffBounds.length === 0) {
92
+ return 0;
93
+ }
94
+
95
+ return Math.min(
96
+ ...staffBounds.map((bounds, staffIndex) => {
97
+ return staffYOffsets[staffIndex]! + bounds.top;
98
+ })
99
+ );
100
+ }
101
+
102
+ function getVisibleBottom(
103
+ staffBounds: StaffVerticalBounds[],
104
+ staffYOffsets: number[]
105
+ ): number {
106
+ if (staffBounds.length === 0) {
107
+ return 0;
108
+ }
109
+
110
+ return Math.max(
111
+ ...staffBounds.map((bounds, staffIndex) => {
112
+ return staffYOffsets[staffIndex]! + bounds.bottom;
113
+ })
114
+ );
115
+ }
116
+
117
+ function defaultStaffBounds(): StaffVerticalBounds {
118
+ return {
119
+ top: VEXFLOW_STAVE_TOP_LINE_OFFSET,
120
+ bottom: VEXFLOW_STAVE_BOTTOM_LINE_OFFSET,
121
+ };
122
+ }
@@ -0,0 +1,310 @@
1
+ import type React from 'react';
2
+ import { memo, useCallback, useMemo, useState } from 'react';
3
+ import {
4
+ Canvas,
5
+ Group,
6
+ Picture,
7
+ Skia,
8
+ type SkTypefaceFontProvider,
9
+ type Transforms3d,
10
+ useCanvasRef,
11
+ } from '@shopify/react-native-skia';
12
+ import { StyleSheet, View, type LayoutChangeEvent } from 'react-native';
13
+ import { GestureDetector } from 'react-native-gesture-handler';
14
+ import { useDerivedValue } from 'react-native-reanimated';
15
+
16
+ import type { VexflowRecordingCommand } from '../../base';
17
+ import { renderVexflowRecordingCommands } from '../../base/VexflowRecordingReplay';
18
+ import { resolveScoreColorScheme } from '../colorScheme';
19
+ import { insets, renderOptions, spacing } from '../constants';
20
+ import type {
21
+ RendererSize,
22
+ RendererType,
23
+ ScoreOptions,
24
+ ScoreRendererProps,
25
+ Viewport,
26
+ } from '../types';
27
+ import { useScoreRecording } from '../useScoreRecording';
28
+ import { createVisibleViewport } from '../viewport';
29
+ import ScoreScrollbar from './ScoreScrollbar';
30
+ import { getMaxScroll, useScoreScroll } from './useScoreScroll';
31
+
32
+ const EMPTY_OPTIONS: Partial<ScoreOptions> = {};
33
+ const EMPTY_SIZE: RendererSize = { width: 0, height: 0 };
34
+
35
+ const ScoreRenderer: React.FC<ScoreRendererProps> = ({
36
+ score,
37
+ rendererType,
38
+ defaultFont,
39
+ fontManager,
40
+ colorScheme,
41
+ options: userOptions = EMPTY_OPTIONS,
42
+ scrollEnabled = true,
43
+ showScrollbars = true,
44
+ }) => {
45
+ const options = useMemo(() => withDefaultOptions(userOptions), [userOptions]);
46
+ const resolvedColorScheme = useMemo(
47
+ () => resolveScoreColorScheme(colorScheme),
48
+ [colorScheme]
49
+ );
50
+ const backgroundStyle = useMemo(
51
+ () => ({ backgroundColor: resolvedColorScheme.background }),
52
+ [resolvedColorScheme.background]
53
+ );
54
+ const canvasStyle = useMemo(
55
+ () => StyleSheet.flatten([styles.canvas, backgroundStyle]),
56
+ [backgroundStyle]
57
+ );
58
+ const effectiveRendererType = rendererType ?? 'document';
59
+
60
+ const canvasRef = useCanvasRef();
61
+ const [viewportSize, setViewportSize] = useState<RendererSize>(EMPTY_SIZE);
62
+ const handleLayout = useCallback((event: LayoutChangeEvent) => {
63
+ const { height, width } = event.nativeEvent.layout;
64
+
65
+ setViewportSize((currentSize) => {
66
+ if (currentSize.width === width && currentSize.height === height) {
67
+ return currentSize;
68
+ }
69
+
70
+ return { width, height };
71
+ });
72
+ }, []);
73
+ const hasViewportSize = viewportSize.width > 0 && viewportSize.height > 0;
74
+ const viewport = useMemo(
75
+ () => ({
76
+ x: 0,
77
+ y: 0,
78
+ width: viewportSize.width,
79
+ height: viewportSize.height,
80
+ }),
81
+ [viewportSize.height, viewportSize.width]
82
+ );
83
+ const { commands: recordedCommands, layoutPlan } = useScoreRecording({
84
+ defaultFont,
85
+ enabled: hasViewportSize,
86
+ fontManager,
87
+ colorScheme: resolvedColorScheme,
88
+ options,
89
+ rendererType: effectiveRendererType,
90
+ score,
91
+ viewport,
92
+ });
93
+ const contentSize = layoutPlan.contentSize;
94
+ const scrollState = useScoreScroll({
95
+ contentSize,
96
+ rendererType: effectiveRendererType,
97
+ scrollEnabled,
98
+ viewportSize,
99
+ });
100
+ const hasScrollableOverflow =
101
+ getMaxScroll(effectiveRendererType, viewportSize, contentSize) > 0;
102
+
103
+ const viewportClip = useMemo(
104
+ () => Skia.XYWHRect(0, 0, viewportSize.width, viewportSize.height),
105
+ [viewportSize.height, viewportSize.width]
106
+ );
107
+
108
+ const picture = useMemo(() => {
109
+ if (!hasViewportSize) {
110
+ return undefined;
111
+ }
112
+
113
+ return createScorePicture({
114
+ contentSize,
115
+ defaultFont,
116
+ fontManager,
117
+ recordedCommands,
118
+ });
119
+ }, [
120
+ contentSize,
121
+ defaultFont,
122
+ fontManager,
123
+ hasViewportSize,
124
+ recordedCommands,
125
+ ]);
126
+
127
+ const pictureTransform = useDerivedValue(() => {
128
+ return createPictureTransform(
129
+ scrollState.scrollOffset.value,
130
+ effectiveRendererType,
131
+ { width: viewportSize.width, height: viewportSize.height },
132
+ contentSize
133
+ );
134
+ }, [
135
+ contentSize,
136
+ effectiveRendererType,
137
+ scrollState.scrollOffset,
138
+ viewportSize.height,
139
+ viewportSize.width,
140
+ ]);
141
+
142
+ return (
143
+ <View style={[styles.container, backgroundStyle]}>
144
+ <GestureDetector gesture={scrollState.panGesture}>
145
+ <View
146
+ style={[styles.gestureSurface, backgroundStyle]}
147
+ onLayout={handleLayout}
148
+ >
149
+ <Canvas style={canvasStyle} ref={canvasRef}>
150
+ <Group clip={viewportClip}>
151
+ <Group transform={pictureTransform}>
152
+ {picture ? <Picture picture={picture} /> : null}
153
+ </Group>
154
+ </Group>
155
+ </Canvas>
156
+ </View>
157
+ </GestureDetector>
158
+
159
+ {showScrollbars && hasScrollableOverflow ? (
160
+ <ScoreScrollbar
161
+ axis={scrollState.axis}
162
+ contentSize={scrollState.contentSize}
163
+ enabled={scrollEnabled}
164
+ scrollOffset={scrollState.scrollOffset}
165
+ style={
166
+ scrollState.axis === 'horizontal'
167
+ ? styles.horizontalScrollbar
168
+ : styles.verticalScrollbar
169
+ }
170
+ viewportSize={scrollState.viewportSize}
171
+ />
172
+ ) : null}
173
+ </View>
174
+ );
175
+ };
176
+
177
+ export default memo(ScoreRenderer);
178
+
179
+ export function createScorePicture({
180
+ contentSize,
181
+ defaultFont,
182
+ fontManager,
183
+ recordedCommands,
184
+ }: {
185
+ contentSize: RendererSize;
186
+ defaultFont: string;
187
+ fontManager: SkTypefaceFontProvider;
188
+ recordedCommands: readonly VexflowRecordingCommand[];
189
+ }) {
190
+ try {
191
+ const start = nowMs();
192
+ const recorder = Skia.PictureRecorder();
193
+ const canvas = recorder.beginRecording(
194
+ Skia.XYWHRect(0, 0, contentSize.width, contentSize.height)
195
+ );
196
+
197
+ renderVexflowRecordingCommands(
198
+ canvas,
199
+ recordedCommands,
200
+ fontManager,
201
+ defaultFont
202
+ );
203
+ const picture = recorder.finishRecordingAsPicture();
204
+
205
+ logScorePictureProfile({
206
+ commandCount: recordedCommands.length,
207
+ contentSize,
208
+ durationMs: nowMs() - start,
209
+ });
210
+
211
+ return picture;
212
+ } catch (error) {
213
+ console.error('ScoreRenderer picture render failed', error);
214
+ throw error;
215
+ }
216
+ }
217
+
218
+ export function createPictureTransform(
219
+ scrollOffset: number,
220
+ rendererType: RendererType,
221
+ viewport: Viewport,
222
+ contentSize: RendererSize
223
+ ): Transforms3d {
224
+ 'worklet';
225
+
226
+ const visibleViewport = createVisibleViewport(
227
+ scrollOffset,
228
+ rendererType,
229
+ viewport,
230
+ contentSize
231
+ );
232
+
233
+ return [
234
+ {
235
+ translateX: visibleViewport.x === 0 ? 0 : -visibleViewport.x,
236
+ },
237
+ {
238
+ translateY: visibleViewport.y === 0 ? 0 : -visibleViewport.y,
239
+ },
240
+ ];
241
+ }
242
+
243
+ function withDefaultOptions(options: Partial<ScoreOptions>): ScoreOptions {
244
+ return {
245
+ insets: { ...insets, ...(options.insets || {}) },
246
+ spacing: { ...spacing, ...(options.spacing || {}) },
247
+ render: { ...renderOptions, ...(options.render || {}) },
248
+ };
249
+ }
250
+
251
+ function logScorePictureProfile({
252
+ commandCount,
253
+ contentSize,
254
+ durationMs,
255
+ }: {
256
+ commandCount: number;
257
+ contentSize: RendererSize;
258
+ durationMs: number;
259
+ }) {
260
+ if (!isDevBuild()) {
261
+ return;
262
+ }
263
+
264
+ console.info('[ScoreRenderer] picture profile', {
265
+ contentSize,
266
+ commandCount,
267
+ durationMs: roundMs(durationMs),
268
+ });
269
+ }
270
+
271
+ function nowMs(): number {
272
+ return globalThis.performance?.now?.() ?? Date.now();
273
+ }
274
+
275
+ function roundMs(value: number): number {
276
+ return Math.round(value * 10) / 10;
277
+ }
278
+
279
+ function isDevBuild(): boolean {
280
+ if (typeof process !== 'undefined' && process.env?.NODE_ENV === 'test') {
281
+ return false;
282
+ }
283
+
284
+ return typeof __DEV__ === 'undefined' ? false : __DEV__;
285
+ }
286
+
287
+ const styles = StyleSheet.create({
288
+ canvas: {
289
+ flex: 1,
290
+ },
291
+ container: {
292
+ flex: 1,
293
+ position: 'relative',
294
+ },
295
+ gestureSurface: {
296
+ flex: 1,
297
+ },
298
+ horizontalScrollbar: {
299
+ bottom: 12,
300
+ height: 6,
301
+ left: 12,
302
+ right: 12,
303
+ },
304
+ verticalScrollbar: {
305
+ bottom: 20,
306
+ right: 2,
307
+ top: 4,
308
+ width: 6,
309
+ },
310
+ });