MUSX Document Model
Loading...
Searching...
No Matches
ShapeDesigner.h
1/*
2 * Copyright (C) 2025, Robert Patterson
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22#pragma once
23
24#include "musx/util/Logger.h"
25#include "musx/util/Fraction.h"
26
27#include "BaseClasses.h"
28#include "CommonClasses.h"
29#include "Details.h"
30
31 // do not add other dom class dependencies. Use Implementations.cpp for implementations that need total class access.
32
33namespace musx {
34namespace dom {
35
39{
40 Blank,
44
45 // Add more known types here
46};
47
51{
54
59 Bracket,
60
68
72
75 CurveTo,
76
80
84 Ellipse,
85
89
93
96 FillAlt,
97
100 FillSolid,
101
105
108 GoToStart,
109
112 LineWidth,
113
117 Rectangle,
118
121 RLineTo,
122
125 RMoveTo,
126
131
134 SetBlack,
135
138 SetDash,
139
142 SetFont,
143
146 SetGray,
147
150 SetWhite,
151
155 Slur,
156
160
169
172 Stroke,
173
178};
179
183{
187 std::vector<int> data;
188 };
189
194
196 struct CloneChar {
199 int unused2{};
201 char32_t codePoint{};
202 };
203
213
215 struct DrawChar {
216 char32_t codePoint{};
217 };
218
220 struct Ellipse {
223 };
224
231
233 struct LineWidth {
235 };
236
238 struct Rectangle {
241 };
242
244 struct RLineTo {
247 };
248
250 struct RMoveTo {
253 };
254
260 int endFlags{};
261 };
262
268
270 struct SetFont {
272 SetFont(const DocumentWeakPtr& document) : font(document)
273 {}
275 };
276
278 struct SetGray {
279 int gray{};
280 };
281
293
308
324
326 enum class VerticalAlign {
327 Center = 1,
328 Left = 2,
329 Right = 3
330 };
331
336
337
339 using InstructionData = std::variant<
340 std::monostate, // for commands with no data
342 Bracket,
343 CloneChar,
344 CurveTo,
345 DrawChar,
346 Ellipse,
348 LineWidth,
349 Rectangle,
350 RLineTo,
351 RMoveTo,
353 SetDash,
354 SetFont,
355 SetGray,
356 Slur,
360 >;
361
364 struct Decoded
365 {
367 InstructionData data{std::monostate{}};
368
370 bool valid() const noexcept { return m_valid; }
371
375 template <typename T>
376 void setPayload(std::optional<T>&& optData)
377 {
378 if (optData) {
379 // Move the contained T into the variant
380 data = std::move(*optData);
381 m_valid = true;
382 } else {
383 // We *tried* to parse data and failed
384 m_valid = false;
385 }
386 }
387
388 private:
389 bool m_valid{true};
390 };
391
393 static std::optional<Undocumented> parseUndocumented(const std::vector<int>& data);
394
396 static std::optional<Bracket> parseBracket(const std::vector<int>& data);
397
399 static std::optional<CloneChar> parseCloneChar(const std::vector<int>& data);
400
402 static std::optional<CurveTo> parseCurveTo(const std::vector<int>& data);
403
405 static std::optional<DrawChar> parseDrawChar(const std::vector<int>& data);
406
408 static std::optional<Ellipse> parseEllipse(const std::vector<int>& data);
409
411 static std::optional<ExternalGraphic> parseExternalGraphic(const std::vector<int>& data);
412
414 static std::optional<LineWidth> parseLineWidth(const std::vector<int>& data);
415
417 static std::optional<Rectangle> parseRectangle(const std::vector<int>& data);
418
420 static std::optional<RLineTo> parseRLineTo(const std::vector<int>& data);
421
423 static std::optional<RMoveTo> parseRMoveTo(const std::vector<int>& data);
424
426 static std::optional<SetArrowhead> parseSetArrowhead(const std::vector<int>& data);
427
429 static std::optional<SetDash> parseSetDash(const std::vector<int>& data);
430
432 static std::optional<SetFont> parseSetFont(const DocumentWeakPtr& document, const std::vector<int>& data);
433
435 static std::optional<SetGray> parseSetGray(const std::vector<int>& data);
436
438 static std::optional<Slur> parseSlur(const std::vector<int>& data);
439
441 static std::optional<StartGroup> parseStartGroup(const std::vector<int>& data);
442
444 static std::optional<StartObject> parseStartObject(const std::vector<int>& data);
445
447 static std::optional<VerticalMode> parseVerticalMode(const std::vector<int>& data);
448};
449
450namespace others {
451
458class ShapeData : public OthersArray<int>
459{
460 std::string_view xmlTag() const override { return XmlNodeName; }
461
462public:
464
465 constexpr static std::string_view XmlNodeName = "shapeData";
466 static const xml::XmlElementArray<ShapeData>& xmlMappingArray();
467};
468
469#ifndef DOXYGEN_SHOULD_IGNORE_THIS
470class ShapeDefTestAccessor; // used by tests to access private iterator in ShapeDef
471#endif
472
473class ShapeInstructionList;
480class ShapeDef : public OthersBase
481{
482private:
484 bool iterateInstructions(std::function<bool(ShapeDefInstructionType, std::vector<int>)> callback) const;
485
486 friend class ShapeDefTestAccessor; // gives tests access, but users don't see it
487
488public:
490 explicit ShapeDef(const DocumentWeakPtr& document, Cmper partId, ShareMode shareMode, Cmper cmper)
491 : OthersBase(document, partId, shareMode, cmper)
492 {
493 }
494
499 enum class ShapeType
500 {
501 Other = 0,
502 Articulation = 1,
503 Barline = 2,
504 Executable = 3,
505 Expression = 4,
506 CustomStem = 5,
507 Frame = 6,
508 Arrowhead = 7,
509 Fretboard = 8,
510 Clef = 9
511 };
512
513 Cmper instructionList{};
514 Cmper dataList{};
515 ShapeType shapeType{};
516
518 bool isBlank() const
519 { return instructionList == 0; }
520
524 bool iterateInstructions(std::function<bool(const ShapeDefInstruction::Decoded&)> callback) const;
525
528 std::optional<KnownShapeDefType> recognize() const;
529
536 std::optional<Evpu> calcWidth() const;
537
542 CurveContourDirection calcSlurContour() const;
543
544 constexpr static std::string_view XmlNodeName = "shapeDef";
545 static const xml::XmlElementArray<ShapeDef>& xmlMappingArray();
546};
547
554class ShapeInstructionList : public OthersBase
555{
556public:
558 explicit ShapeInstructionList(const DocumentWeakPtr& document, Cmper partId, ShareMode shareMode, Cmper cmper)
559 : OthersBase(document, partId, shareMode, cmper)
560 {
561 }
562
573
574 std::vector<std::shared_ptr<InstructionInfo>> instructions;
575
576 constexpr static std::string_view XmlNodeName = "shapeList";
577 static const xml::XmlElementArray<ShapeInstructionList>& xmlMappingArray();
578};
579
580} // namespace others
581} // namespace dom
582} // namespace musx
ShareMode
Describes how this instance is shared between part and score.
Definition BaseClasses.h:91
Represents the default font settings for a particular element type.
Definition CommonClasses.h:66
OthersArray(const DocumentWeakPtr &document, Cmper partId, ShareMode shareMode, Cmper cmper)
Constructor function.
Definition BaseClasses.h:359
OthersBase(const DocumentWeakPtr &document, Cmper partId, ShareMode shareMode, Cmper cmper, std::optional< Inci > inci=std::nullopt)
Constructs an OthersBase object.
Definition BaseClasses.h:299
BracketStyle
Bracket types.
Definition Details.h:646
An instruction in the shape.
Definition ShapeDesigner.h:565
int numData
the number of data items consumed by this instruction (See ShapeData.)
Definition ShapeDesigner.h:568
static const xml::XmlElementArray< InstructionInfo > & xmlMappingArray()
Required for musx::factory::FieldPopulator.
ShapeDefInstructionType type
the type of instruction
Definition ShapeDesigner.h:569
@ Other
Other/unknown text encoding.
CurveContourDirection
Curve contour direction for ties and slurs.
Definition EnumClasses.h:38
KnownShapeDefType
Enumerates the shape types we can recognize semantically.
Definition ShapeDesigner.h:39
@ SlurTieCurveRight
Horizontal slur or tie shape curving toward the right.
@ TenutoMark
A horizontal tenuto mark, typically used as an articulation symbol.
@ SlurTieCurveLeft
Horizontal slur or tie shape curving toward the left.
int32_t Efix
EFIX value (64 per EVPU, 64*288=18432 per inch)
Definition Fundamentals.h:60
int32_t Evpu16ths
1/16 of an EVPU.
Definition Fundamentals.h:59
int32_t Evpu
EVPU value (288 per inch)
Definition Fundamentals.h:57
uint16_t Cmper
Enigma "comperator" key type.
Definition Fundamentals.h:55
@ Blank
Blank clef (invisible, no symbol).
ShapeDefInstructionType
Defines the instruction types for Shape Designer shapes.
Definition ShapeDesigner.h:51
@ Undocumented
catch-all for possible undocumented instruction tags.
std::weak_ptr< Document > DocumentWeakPtr
Shared weak Document pointer.
Definition BaseClasses.h:57
std::vector< XmlElementDescriptor< T > > XmlElementArray
an array type for XmlElementDescriptor instances.
Definition XmlInterface.h:127
object model for musx file (enigmaxml)
Definition BaseClasses.h:36
Holds the parsed data for a Bracket instruction.
Definition ShapeDesigner.h:191
details::Bracket::BracketStyle bracketStyle
The style of bracket being created.
Definition ShapeDesigner.h:192
Holds the parsed data for a CloneChar instruction.
Definition ShapeDesigner.h:196
int baselineShift
Baseline shift in EMs (1/1000 of current point size).
Definition ShapeDesigner.h:200
int unused2
Unused/undocumented field at index 2.
Definition ShapeDesigner.h:199
char32_t codePoint
The 32-bit codepoint of the character to clone.
Definition ShapeDesigner.h:201
Evpu dy
The Y coordinate of the endpoint (currently ignored).
Definition ShapeDesigner.h:198
Evpu dx
The X coordinate of the endpoint (relative to current point).
Definition ShapeDesigner.h:197
Holds the parsed data for a CurveTo instruction.
Definition ShapeDesigner.h:205
Evpu c1dx
Relative X offset of the first control point.
Definition ShapeDesigner.h:206
Evpu c2dy
Relative Y offset of the second control point.
Definition ShapeDesigner.h:209
Evpu edy
Relative Y offset of the end point.
Definition ShapeDesigner.h:211
Evpu c2dx
Relative X offset of the second control point.
Definition ShapeDesigner.h:208
Evpu c1dy
Relative Y offset of the first control point.
Definition ShapeDesigner.h:207
Evpu edx
Relative X offset of the end point.
Definition ShapeDesigner.h:210
Contains the parsed instruction type with its data.
Definition ShapeDesigner.h:365
bool valid() const noexcept
Returns true is the data is valid.
Definition ShapeDesigner.h:370
void setPayload(std::optional< T > &&optData)
Sets the payload.
Definition ShapeDesigner.h:376
ShapeDefInstructionType type
Instruction type.
Definition ShapeDesigner.h:366
InstructionData data
Data for instruction.
Definition ShapeDesigner.h:367
Holds the parsed data for a DrawChar instruction.
Definition ShapeDesigner.h:215
char32_t codePoint
The 32-bit codepoint of the character to draw.
Definition ShapeDesigner.h:216
Holds the parsed data for an Ellipse instruction.
Definition ShapeDesigner.h:220
Evpu height
The height of the bounding rectangle.
Definition ShapeDesigner.h:222
Evpu width
The width of the bounding rectangle.
Definition ShapeDesigner.h:221
Holds the parsed data for an ExternalGraphic instruction.
Definition ShapeDesigner.h:226
Evpu height
The height of the placed graphic.
Definition ShapeDesigner.h:228
Cmper cmper
The cmper of the graphic. See others::PageGraphicAssign::graphicCmper for explanation.
Definition ShapeDesigner.h:229
Evpu width
The width of the placed graphic.
Definition ShapeDesigner.h:227
Holds the parsed data for a LineWidth instruction.
Definition ShapeDesigner.h:233
Efix efix
The line width in Efix units.
Definition ShapeDesigner.h:234
Holds the parsed data for an RLineTo instruction.
Definition ShapeDesigner.h:244
Evpu dx
The relative X coordinate delta in Evpu.
Definition ShapeDesigner.h:245
Evpu dy
The relative Y coordinate delta in Evpu.
Definition ShapeDesigner.h:246
Holds the parsed data for an RMoveTo instruction.
Definition ShapeDesigner.h:250
Evpu dx
The relative X coordinate delta in Evpu.
Definition ShapeDesigner.h:251
Evpu dy
The relative Y coordinate delta in Evpu.
Definition ShapeDesigner.h:252
Holds the parsed data for a Rectangle instruction.
Definition ShapeDesigner.h:238
Evpu height
The height of the rectangle.
Definition ShapeDesigner.h:240
Evpu width
The width of the rectangle.
Definition ShapeDesigner.h:239
Holds the parsed data for a SetArrowhead instruction.
Definition ShapeDesigner.h:256
int endFlags
Flags for the end arrowhead (built-in vs custom, etc.).
Definition ShapeDesigner.h:260
int startArrowId
Identifier of the start arrowhead.
Definition ShapeDesigner.h:257
int startFlags
Flags for the start arrowhead (built-in vs custom, etc.).
Definition ShapeDesigner.h:259
int endArrowId
Identifier of the end arrowhead.
Definition ShapeDesigner.h:258
Holds the parsed data for a SetDash instruction.
Definition ShapeDesigner.h:264
Efix dashLength
Length of each dash segment.
Definition ShapeDesigner.h:265
Efix spaceLength
Space between dashes.
Definition ShapeDesigner.h:266
Holds the parsed data for a SetFont instruction.
Definition ShapeDesigner.h:270
SetFont(const DocumentWeakPtr &document)
Constructor function.
Definition ShapeDesigner.h:272
FontInfo font
the parsed font
Definition ShapeDesigner.h:274
Holds the parsed data for a SetGray instruction.
Definition ShapeDesigner.h:278
int gray
Gray value between 0 (black) and 100 (white).
Definition ShapeDesigner.h:279
Holds the parsed data for a Slur instruction.
Definition ShapeDesigner.h:285
Evpu16ths edx
Relative X offset of the end point (1/16 Evpu units).
Definition ShapeDesigner.h:290
Evpu16ths c2dx
Relative X offset of the second control point (1/16 Evpu units).
Definition ShapeDesigner.h:288
Evpu16ths c1dx
Relative X offset of the first control point (1/16 Evpu units).
Definition ShapeDesigner.h:286
Evpu16ths c2dy
Relative Y offset of the second control point (1/16 Evpu units).
Definition ShapeDesigner.h:289
Evpu16ths edy
Relative Y offset of the end point (1/16 Evpu units).
Definition ShapeDesigner.h:291
Evpu16ths c1dy
Relative Y offset of the first control point (1/16 Evpu units).
Definition ShapeDesigner.h:287
Holds the parsed data for a StartGroup instruction.
Definition ShapeDesigner.h:311
Evpu bottom
Bottom of the bounding rectangle.
Definition ShapeDesigner.h:317
Evpu originY
Y coordinate of the origin point.
Definition ShapeDesigner.h:313
int unused10
Undocumented/unused field at index 10.
Definition ShapeDesigner.h:322
int scaleY
Y scale transform (scale ratio * 1000).
Definition ShapeDesigner.h:319
int rotation
Rotation transform.
Definition ShapeDesigner.h:320
Evpu top
Top of the bounding rectangle.
Definition ShapeDesigner.h:315
Evpu right
Right of the bounding rectangle.
Definition ShapeDesigner.h:316
int unused9
Undocumented/unused field at index 9.
Definition ShapeDesigner.h:321
int scaleX
X scale transform (scale ratio * 1000).
Definition ShapeDesigner.h:318
Evpu left
Left of the bounding rectangle.
Definition ShapeDesigner.h:314
Evpu originX
X coordinate of the origin point.
Definition ShapeDesigner.h:312
Holds the parsed data for a StartObject instruction.
Definition ShapeDesigner.h:295
Evpu left
Left of the bounding rectangle.
Definition ShapeDesigner.h:298
Evpu originY
Y coordinate of the origin point.
Definition ShapeDesigner.h:297
Evpu bottom
Bottom of the bounding rectangle.
Definition ShapeDesigner.h:301
int scaleY
Y scale transform (scale ratio * 1000).
Definition ShapeDesigner.h:303
int unused10
Undocumented/unused field at index 10.
Definition ShapeDesigner.h:306
int scaleX
X scale transform (scale ratio * 1000).
Definition ShapeDesigner.h:302
Evpu top
Top of the bounding rectangle.
Definition ShapeDesigner.h:299
int unused9
Undocumented/unused field at index 9.
Definition ShapeDesigner.h:305
Evpu originX
X coordinate of the origin point.
Definition ShapeDesigner.h:296
int rotation
Rotation transform.
Definition ShapeDesigner.h:304
Evpu right
Right of the bounding rectangle.
Definition ShapeDesigner.h:300
Holds the parsed data for an Undocumented instruction.
Definition ShapeDesigner.h:186
std::vector< int > data
Raw data items for the undocumented instruction.
Definition ShapeDesigner.h:187
Holds the parsed data for a VerticalMode instruction.
Definition ShapeDesigner.h:333
VerticalAlign mode
The pen vertical alignment mode.
Definition ShapeDesigner.h:334
Helper functions and structs for decoding instruction data vectors in others::ShapeDef.
Definition ShapeDesigner.h:183
static std::optional< SetFont > parseSetFont(const DocumentWeakPtr &document, const std::vector< int > &data)
Attempts to parse a SetFont instruction.
Definition ShapeDesigner.cpp:208
static std::optional< ExternalGraphic > parseExternalGraphic(const std::vector< int > &data)
Attempts to parse an ExternalGraphic instruction.
Definition ShapeDesigner.cpp:127
static std::optional< Slur > parseSlur(const std::vector< int > &data)
Attempts to parse a Slur instruction.
Definition ShapeDesigner.cpp:232
static std::optional< LineWidth > parseLineWidth(const std::vector< int > &data)
Attempts to parse a LineWidth instruction.
Definition ShapeDesigner.cpp:140
static std::optional< VerticalMode > parseVerticalMode(const std::vector< int > &data)
Attempts to parse a VerticalMode instruction.
Definition ShapeDesigner.cpp:287
static std::optional< SetGray > parseSetGray(const std::vector< int > &data)
Attempts to parse a SetGray instruction.
Definition ShapeDesigner.cpp:221
static std::optional< DrawChar > parseDrawChar(const std::vector< int > &data)
Attempts to parse a DrawChar instruction.
Definition ShapeDesigner.cpp:106
static std::optional< StartObject > parseStartObject(const std::vector< int > &data)
Attempts to parse a StartObject instruction.
Definition ShapeDesigner.cpp:266
VerticalAlign
Pen vertical alignment modes for the VerticalMode instruction.
Definition ShapeDesigner.h:326
@ Center
Draw line centered on the coordinates.
@ Right
Draw line on the "right" side of the coordinates.
@ Left
Draw line on the "left" side of the coordinates.
static std::optional< SetArrowhead > parseSetArrowhead(const std::vector< int > &data)
Attempts to parse a SetArrowhead instruction.
Definition ShapeDesigner.cpp:182
static std::optional< StartGroup > parseStartGroup(const std::vector< int > &data)
Attempts to parse a StartGroup instruction.
Definition ShapeDesigner.cpp:245
static std::optional< Rectangle > parseRectangle(const std::vector< int > &data)
Attempts to parse a Rectangle instruction.
Definition ShapeDesigner.cpp:149
static std::optional< RLineTo > parseRLineTo(const std::vector< int > &data)
Attempts to parse an RLineTo instruction.
Definition ShapeDesigner.cpp:161
static std::optional< RMoveTo > parseRMoveTo(const std::vector< int > &data)
Attempts to parse an RMoveTo instruction.
Definition ShapeDesigner.cpp:170
static std::optional< Ellipse > parseEllipse(const std::vector< int > &data)
Attempts to parse an Ellipse instruction.
Definition ShapeDesigner.cpp:115
static std::optional< SetDash > parseSetDash(const std::vector< int > &data)
Attempts to parse a SetDash instruction.
Definition ShapeDesigner.cpp:196
std::variant< std::monostate, Undocumented, Bracket, CloneChar, CurveTo, DrawChar, Ellipse, ExternalGraphic, LineWidth, Rectangle, RLineTo, RMoveTo, SetArrowhead, SetDash, SetFont, SetGray, Slur, StartGroup, StartObject, VerticalMode > InstructionData
A variant for passing specific data per instruction type.
Definition ShapeDesigner.h:360
static std::optional< CloneChar > parseCloneChar(const std::vector< int > &data)
Attempts to parse a CloneChar instruction.
Definition ShapeDesigner.cpp:78
static std::optional< CurveTo > parseCurveTo(const std::vector< int > &data)
Attempts to parse a CurveTo instruction.
Definition ShapeDesigner.cpp:93
static std::optional< Undocumented > parseUndocumented(const std::vector< int > &data)
Attempts to parse an Undocumented instruction (raw passthrough).
Definition ShapeDesigner.cpp:44
static std::optional< Bracket > parseBracket(const std::vector< int > &data)
Attempts to parse a Bracket instruction.
Definition ShapeDesigner.cpp:51