Skip to content

Commit

Permalink
fix: add AddSyl editor action
Browse files Browse the repository at this point in the history
- Add <syl> element with text
- Skip adding default syl for syllables for Neon

refs: DDMAL/Neon#1265
  • Loading branch information
yinanazhou committed Jan 28, 2025
1 parent a9ad94e commit 05ae709
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 11 deletions.
2 changes: 2 additions & 0 deletions include/vrv/editortoolkit_neume.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class EditorToolkitNeume : public EditorToolkit {
* Experimental editor functions.
*/
///@{
bool AddSyl(std::string elementId, std::string sylText);
bool Chain(jsonxx::Array actions);
bool DisplaceClefOctave(std::string elementId, std::string direction);
bool Drag(std::string elementId, int x, int y, bool topLevel = true);
Expand Down Expand Up @@ -71,6 +72,7 @@ class EditorToolkitNeume : public EditorToolkit {
* Parse JSON instructions for experimental editor functions.
*/
///@{
bool ParseAddSylAction(jsonxx::Object param, std::string *elementId, std::string *sylText);
bool ParseDisplaceClefAction(jsonxx::Object param, std::string *elementId, std::string *direction);
bool ParseDragAction(jsonxx::Object param, std::string *elementId, int *x, int *y);
bool ParseInsertAction(jsonxx::Object param, std::string *elementType, std::string *startId, std::string *endId);
Expand Down
7 changes: 0 additions & 7 deletions src/doc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -887,13 +887,6 @@ void Doc::PrepareData()
PrepareLayerElementPartsFunctor prepareLayerElementParts;
this->Process(prepareLayerElementParts);

/************ Add default syl for syllables (if applicable) ************/
ListOfObjects syllables = this->FindAllDescendantsByType(SYLLABLE);
for (Object *object : syllables) {
Syllable *syllable = dynamic_cast<Syllable *>(object);
syllable->MarkupAddSyl();
}

/************ Resolve @facs ************/
if (this->IsFacs()) {
// Associate zones with elements
Expand Down
79 changes: 75 additions & 4 deletions src/editortoolkit_neume.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,14 @@ bool EditorToolkitNeume::ParseEditorAction(const std::string &json_editorAction)
m_editInfo.import("message", "'param' can only be an array for a chain action.");
return false;
}

if (action == "drag") {
if (action == "addSyl") {
std::string elementId, sylText;
if (this->ParseAddSylAction(json.get<jsonxx::Object>("param"), &elementId, &sylText)) {
return this->AddSyl(elementId, sylText);
}
LogWarning("Could not parse the addSyl action");
}
else if (action == "drag") {
std::string elementId;
int x, y;
if (this->ParseDragAction(json.get<jsonxx::Object>("param"), &elementId, &x, &y)) {
Expand Down Expand Up @@ -277,6 +283,60 @@ bool EditorToolkitNeume::Chain(jsonxx::Array actions)
return status;
}

bool EditorToolkitNeume::AddSyl(std::string elementId, std::string sylText)
{
if (!m_doc->GetDrawingPage()) {
LogError("Could not get the drawing page.");
m_editInfo.import("status", "FAILURE");
m_editInfo.import("message", "Could not get the drawing page.");
return true;
}

Syllable *syllable = dynamic_cast<Syllable *>(m_doc->GetDrawingPage()->FindDescendantByID(elementId));
if (syllable == NULL) {
LogError("Unable to find syllable with id %s", elementId.c_str());
m_editInfo.import("status", "FAILURE");
m_editInfo.import("message", "Unable to find neume with id " + elementId + ".");
return false;
}

// Create a new syl element
Syl *syl = new Syl();
Text *text = new Text();
text->SetText(UTF8to32(sylText));
syl->AddChild(text);
syllable->AddChild(syl);

// Create default bounding box if facs
if (m_doc->HasFacsimile()) {
Zone *zone = new Zone();
Staff *staff = syllable->GetAncestorStaff();
const int staffSize = m_doc->GetDrawingDoubleUnit(staff->m_drawingStaffSize);

zone->SetUlx(syllable->GetFirst(NEUME)->GetFirst(NC)->GetFacsimileInterface()->GetZone()->GetUlx());
zone->SetUly(staff->GetFacsimileInterface()->GetZone()->GetLry());
zone->SetLrx(syllable->GetLast(NEUME)->GetLast(NC)->GetFacsimileInterface()->GetZone()->GetLrx());
zone->SetLry(zone->GetUly() + staffSize * 2);

// Make bbox larger if it has less than 2 ncs
if (syllable->GetChildCount(NC, 2) <= 2) {
zone->SetLrx(zone->GetLrx() + 50);
}

assert(m_doc->GetFacsimile());
m_doc->GetFacsimile()->FindDescendantByType(SURFACE)->AddChild(zone);
FacsimileInterface *fi = syl->GetFacsimileInterface();
assert(fi);
fi->AttachZone(zone);

if (m_doc->IsTranscription() && m_doc->HasFacsimile()) m_doc->SyncFromFacsimileDoc();
}

m_editInfo.import("uuid", elementId);
m_editInfo.import("status", "OK");
m_editInfo.import("message", "");
return true;
}
//////////////////////////////////////////////////////////////////////////////////////////////////
// Author: jacob-hutnyk
//
Expand Down Expand Up @@ -2214,12 +2274,14 @@ void EditorToolkitNeume::UnlinkSyllable(Syllable *syllable)
// Create default bounding box if facs
if (m_doc->HasFacsimile()) {
Zone *zone = new Zone();
Staff *staff = linkedSyllable->GetAncestorStaff();
const int staffSize = m_doc->GetDrawingDoubleUnit(staff->m_drawingStaffSize);

zone->SetUlx(
linkedSyllable->GetFirst(NEUME)->GetFirst(NC)->GetFacsimileInterface()->GetZone()->GetUlx());
zone->SetUly(linkedSyllable->GetAncestorStaff()->GetFacsimileInterface()->GetZone()->GetLry());
zone->SetUly(staff->GetFacsimileInterface()->GetZone()->GetLry());
zone->SetLrx(linkedSyllable->GetLast(NEUME)->GetLast(NC)->GetFacsimileInterface()->GetZone()->GetLrx());
zone->SetLry(zone->GetUly() + 100);
zone->SetLry(zone->GetUly() + staffSize * 2);

// Make bbox larger if it has less than 2 ncs
if (linkedSyllable->GetChildCount(NC, 2) <= 2) {
Expand Down Expand Up @@ -3865,6 +3927,15 @@ bool EditorToolkitNeume::ChangeStaffTo(std::string elementId, std::string staffI
return true;
}

bool EditorToolkitNeume::ParseAddSylAction(jsonxx::Object param, std::string *elementId, std::string *sylText)
{
if (!param.has<jsonxx::String>("elementId")) return false;
(*elementId) = param.get<jsonxx::String>("elementId");
if (!param.has<jsonxx::String>("sylText")) return false;
(*sylText) = param.get<jsonxx::String>("sylText");
return true;
}

bool EditorToolkitNeume::ParseDragAction(jsonxx::Object param, std::string *elementId, int *x, int *y)
{
if (!param.has<jsonxx::String>("elementId")) return false;
Expand Down

0 comments on commit 05ae709

Please sign in to comment.