Skip to content

Commit

Permalink
Merge pull request #1150 from DDMAL/zone-0-fix
Browse files Browse the repository at this point in the history
Handle all-zero zones when loading files
  • Loading branch information
yinanazhou authored Dec 7, 2023
2 parents 76c2946 + bbc12ad commit c29223f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
33 changes: 31 additions & 2 deletions src/utils/ConvertMei.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,20 @@ export function getSyllableText (syllable: Element): string {
return sylText;
}

/**
* Check if zone is all-zero
*/
export function isInvalidBBox (zone: Element): boolean {
if ((parseInt(zone.getAttribute('lrx')) === 0) &&
(parseInt(zone.getAttribute('lry')) === 0) &&
(parseInt(zone.getAttribute('ulx')) === 0) &&
(parseInt(zone.getAttribute('uly')) === 0)) {
return true;
}
return false;
}


/**
* Convert sb to staff for verovio
* Also do heuristic checks:
Expand All @@ -182,9 +196,24 @@ export function convertToVerovio(sbBasedMei: string): string {
const mei = meiDoc.documentElement;
let hasCols = false;

// Check if there is zones with 0, 0, 0, 0
const surface = mei.getElementsByTagName('surface')[0];
let hasInvalidBBox = false;
for (const zone of surface.getElementsByTagName('zone')) {
if (isInvalidBBox(zone)) {
const element = mei.querySelector(`*[facs="${'#'+zone.getAttribute('xml:id')}"]`);
if (element) element.parentNode.removeChild(element);
zone.parentNode.removeChild(zone);
hasInvalidBBox = true;
}
}
if (hasInvalidBBox) {
Notification.queueNotification('Removed invalid zones contained in this file', 'warning');
}

// Check if there is <colLayout> element and remove them
// There will only be one <colLayout> element
const colLayout = Array.from(mei.getElementsByTagName('colLayout')).at(0);
const colLayout = mei.getElementsByTagName('colLayout')[0];
if (colLayout) {
hasCols = true;
colLayout.parentNode.removeChild(colLayout);
Expand Down Expand Up @@ -302,7 +331,7 @@ export function convertToVerovio(sbBasedMei: string): string {
// Remove cb element and its zone
const cb = layerChildren.at(currentIdx-1);
const cbFacs = cb.getAttribute('facs');
const cbZone = Array.from(mei.querySelectorAll('facsimile > surface > zone'))
const cbZone = Array.from(surface.querySelectorAll('zone'))
.find(zone => zone.getAttribute('xml:id') === cbFacs.slice(1));
cb.parentNode.removeChild(cb);
cbZone.parentNode.removeChild(cbZone);
Expand Down
4 changes: 2 additions & 2 deletions src/utils/EditContents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const navbarDropdownFileMenu: HTMLDivElement = document.createElement('di
navbarDropdownFileMenu.classList.add('navbar-item', 'has-dropdown', 'is-hoverable');
const fileDropdownBtn = document.createElement('div');
fileDropdownBtn.classList.add('navbar-btn');
fileDropdownBtn.innerHTML = `<div>File</div>`;
fileDropdownBtn.innerHTML = '<div>File</div>';
const fileNavbarContents = document.createElement('div');
fileNavbarContents.classList.add('navbar-dropdown');
fileNavbarContents.id = 'navbar-dropdown-options';
Expand All @@ -34,7 +34,7 @@ export const navbarDropdownMEIActionsMenu: HTMLDivElement = document.createEleme
navbarDropdownMEIActionsMenu.classList.add('navbar-item', 'has-dropdown', 'is-hoverable');
const meiActionsDropdownBtn = document.createElement('div');
meiActionsDropdownBtn.classList.add('navbar-btn');
meiActionsDropdownBtn.innerHTML = `<div>MEI Actions</div>`;
meiActionsDropdownBtn.innerHTML = '<div>MEI Actions</div>';
const meiActionsNavbarContents = document.createElement('div');
meiActionsNavbarContents.classList.add('navbar-dropdown');
meiActionsNavbarContents.id = 'navbar-dropdown-options';
Expand Down

0 comments on commit c29223f

Please sign in to comment.