Remove the bTrem element

Remove the bTrem element

All of the information about the tremolo was contained on the note anyway.

function GenerateMEIHeader () {
    //$module(ExportGenerators.mss)
    // takes in a Sibelius Score object
    // returns a libmei tree (i.e., nested objects and arrays) with a MEI header with metadata
    score = Self._property:ActiveScore;
    header = libmei.MeiHead();
    fileD = libmei.FileDesc();
    titleS = libmei.TitleStmt();
    libmei.AddChild(header, fileD);
    libmei.AddChild(fileD, titleS);

    workDesc = libmei.WorkDesc();
    wd_work = libmei.Work();
    wd_titleStmt = libmei.TitleStmt();
    wd_title = libmei.Title();
    wd_respStmt = libmei.RespStmt();
    libmei.AddChild(wd_titleStmt, wd_title);
    libmei.AddChild(wd_titleStmt, wd_respStmt);
    libmei.AddChild(wd_work, wd_titleStmt);
    libmei.AddChild(workDesc, wd_work);

    title = libmei.Title();
    libmei.AddChild(titleS, title);

    if (score.Title != '')
    {
        libmei.SetText(title, score.Title);
        libmei.SetText(wd_title, score.Title);
    }
    if (score.Subtitle != '')
    {
        subtitle = libmei.Title();
        libmei.AddChild(titleS, subtitle);
        libmei.SetText(subtitle, score.Subtitle);
        libmei.AddAttribute(subtitle, 'type', 'subtitle');
    }
    if (score.Composer != '')
    {
        composer = libmei.Composer();
        libmei.AddChild(titleS, composer);
        libmei.SetText(composer, score.Composer);
        wd_composer = libmei.PersName();
        libmei.AddAttribute(wd_composer, 'role', 'composer');
        libmei.SetText(wd_composer, score.Composer);
        libmei.AddChild(wd_respStmt, wd_composer);
    }
    if (score.Lyricist != '')
    {
        lyricist = libmei.Lyricist();
        libmei.AddChild(titleS, lyricist);
        libmei.SetText(lyricist, score.Lyricist);
        wd_lyricist = libmei.PersName();
        libmei.AddAttribute(wd_lyricist, 'role', 'lyricist');
        libmei.SetText(wd_lyricist, score.Lyricist);
        libmei.AddChild(wd_respStmt, wd_lyricist);
    }
    if (score.Arranger != '')
    {
        arranger = libmei.Arranger();
        libmei.AddChild(titleS, arranger);
        libmei.SetText(arranger, score.Arranger);
        wd_arranger = libmei.PersName();
        libmei.AddAttribute(wd_arranger, 'role', 'arranger');
        libmei.SetText(wd_arranger, score.Arranger);
        libmei.AddChild(wd_respStmt, wd_arranger);
    }
    if (score.OtherInformation != '')
    {
        wd_notesStmt = libmei.NotesStmt();
        ns_annot = libmei.Annot();
        libmei.AddChild(wd_work, wd_notesStmt);
        libmei.AddChild(wd_notesStmt, ns_annot);
        libmei.SetText(ns_annot, score.OtherInformation);
    }

    respS = libmei.RespStmt();
    libmei.AddChild(titleS, respS);
    persN = libmei.PersName();
    libmei.AddChild(respS, persN);
    pubS = libmei.PubStmt();
    libmei.AddChild(fileD, pubS);

    avail = libmei.Availability();
    ur = libmei.UseRestrict();
    libmei.AddChild(avail, ur);
    libmei.AddChild(pubS, avail);
    libmei.SetText(ur, score.Copyright);

    encodingD = libmei.EncodingDesc();
    libmei.AddChild(header, encodingD);
    appI = libmei.AppInfo();
    libmei.AddChild(encodingD, appI);

    applic = libmei.Application();
    libmei.SetId(applic, 'sibelius7');
    libmei.AddChild(appI, applic);
    libmei.AddAttribute(applic, 'version', Sibelius.ProgramVersion);
    nm = libmei.Name();
    libmei.SetText(nm, 'Sibelius (' & Version & ')');
    libmei.AddChild(applic, nm);

    libmei.AddChild(header, workDesc);

    return header;
}  //$end

function GenerateMEIMusic () {
    //$module(ExportGenerators.mss)
    score = Self._property:ActiveScore;

    Self._property:TieResolver = CreateDictionary();
    Self._property:LyricWords = CreateDictionary();
    Self._property:SpecialBarlines = CreateDictionary();
    Self._property:SystemText = CreateDictionary();
    Self._property:LayerObjectPositions = null;
    Self._property:ObjectPositions = CreateDictionary();

    // the section parent is used in case we need to inject
    // a new parent later (e.g., for endings)
    Self._property:SectionParent = null;
    Self._property:VoltaBars = CreateDictionary();
    Self._property:ActiveVolta = null;
    Self._property:VoltaElement = null;

    // grab some global markers from the system staff
    // This will store it for later use.
    ProcessSystemStaff(score);

    music = libmei.Music();
    body = libmei.Body();
    mdiv = libmei.Mdiv();
    sco = libmei.Score();

    libmei.AddChild(music, body);
    libmei.AddChild(body, mdiv);
    libmei.AddChild(mdiv, sco);

    scd = sibmei2.GenerateScoreDef(score);
    libmei.AddChild(sco, scd);

    barmap = ConvertSibeliusStructure(score);
    numbars = barmap.GetPropertyNames();
    numbars = numbars.Length;
    Self._property:BarMap = barmap;

    section = libmei.Section();
    libmei.AddChild(sco, section);

    for j = 1 to numbars + 1
    {
        // this value may get changed by the volta processor
        // to inject a new parent in the hierarchy.
        progressMsg = utils.Format(_ExportingBars, j, numbars);
        cont = Sibelius.UpdateProgressDialog(j, progressMsg);

        if (cont = 0)
        {
            ExitPlugin();
        }

        m = GenerateMeasure(j);
        ending = ProcessVolta(j);

        if (ending != null)
        {
            libmei.AddChild(section, ending);
            libmei.AddChild(ending, m);
        }
        else
        {
            libmei.AddChild(section, m);
        }
    }

    return music;
}  //$end

function GenerateMeasure (num) {
    //$module(ExportGenerators.mss)

    score = Self._property:ActiveScore;
    // measureties
    Self._property:MeasureTies = CreateSparseArray();
    Self._property:MeasureLines = CreateSparseArray();

    m = libmei.Measure();
    libmei.AddAttribute(m, 'n', num);

    barmap = Self._property:BarMap;
    staves = barmap[num];
    children = CreateSparseArray();

    // since so much metadata about the staff and other context
    // is available on the bar that should now be on the measure, go through the bars
    // and try to extract it.

    for i = 1 to staves.Length + 1
    {
        this_staff = score.NthStaff(i);
        bar = this_staff[num];

        if (bar.ExternalBarNumberString)
        {
            libmei.AddAttribute(m, 'label', bar.ExternalBarNumberString);
        }

        s = GenerateStaff(i, num);
        libmei.AddChild(m, s);
    }

    mties = Self._property:MeasureTies;

    for each tie in mties
    {
        m.children.Push(tie);
    }

    mlines = Self._property:MeasureLines;

    for each line in mlines
    {
        m.children.Push(line);
    }

    specialbarlines = Self._property:SpecialBarlines;

    if (specialbarlines.PropertyExists(num))
    {
        bline = specialbarlines[num];

        if (bline = 'rptstart')
        {
            libmei.AddAttribute(m, 'left', bline);
        }
        else
        {
            libmei.AddAttribute(m, 'right', bline);
        }
    }

    systemtext = Self._property:SystemText;

    if (systemtext.PropertyExists(num))
    {
        textobjs = systemtext[num];
        for each textobj in textobjs
        {
            text = ConvertText(textobj);

            if (text != null)
            {
                libmei.AddChild(m, text);
            }
        }
    }

    return m;
}  //$end

function GenerateStaff (staffnum, measurenum) {
    //$module(ExportGenerators.mss)
    score = Self._property:ActiveScore;
    this_staff = score.NthStaff(staffnum);
    bar = this_staff[measurenum];

    stf = libmei.Staff();

    if (bar.OnHiddenStave)
    {
        libmei.AddAttribute(stf, 'visible', 'false');
    }

    libmei.AddAttribute(stf, 'n', staffnum);

    layers = GenerateLayers(staffnum, measurenum);
    // NB: Completely resets any previous children!
    libmei.SetChildren(stf, layers);

    return stf;
}  //$end

function GenerateLayers (staffnum, measurenum) {
    //$module(ExportGenerators.mss)
    layerdict = CreateDictionary();
    layers = CreateSparseArray();

    objectPositions = Self._property:ObjectPositions;

    if (objectPositions.PropertyExists(staffnum) = False)
    {
        objectPositions[staffnum] = CreateDictionary();
    }

    staffObjectPositions = objectPositions[staffnum];

    score = Self._property:ActiveScore;
    this_staff = score.NthStaff(staffnum);
    bar = this_staff[measurenum];

    for each bobj in bar
    {
        voicenumber = bobj.VoiceNumber;

        if (staffObjectPositions.PropertyExists(bar.BarNumber) = False)
        {
            staffObjectPositions[bar.BarNumber] = CreateDictionary();
        }

        barObjectPositions = staffObjectPositions[bar.BarNumber];

        if (layerdict.PropertyExists(voicenumber))
        {
            l = layerdict[voicenumber];
        }
        else
        {
            if (voicenumber != 0)
            {
                l = libmei.Layer();
                layers.Push(l._id);

                if (barObjectPositions.PropertyExists(voicenumber) = False)
                {
                    barObjectPositions[voicenumber] = CreateDictionary();
                }

                layerdict[voicenumber] = l;
                libmei.AddAttribute(l, 'n', voicenumber);
            }
        }

        obj = null;
        line = null;
        parent = null;
        beam = null;
        tuplet = null;

        switch (bobj.Type)
        {
            case('Clef')
            {
                clef = GenerateClef(bobj);
                libmei.AddChild(l, clef);
            }
            case('NoteRest')
            {
                note = GenerateNoteRest(bobj, l);

                // record the position of this element
                objVoice = barObjectPositions[voicenumber];
                objVoice[bobj.Position] = note._id;

                if (note._property:TieIds != null)
                {
                    ties = note._property:TieIds;
                    mties = Self._property:MeasureTies;
                    mties = mties.Concat(ties);
                    Self._property:MeasureTies = mties;
                }

                beam = ProcessBeam(bobj, l);
                tuplet = ProcessTuplet(bobj, note, l);

                if (beam != null)
                {
                    libmei.AddChild(beam, note);

                    if (tuplet != null)
                    {
                        if (beam._parent != tuplet._id)
                        {
                            libmei.AddChild(tuplet, beam);
                        }

                        if (tuplet._parent != l._id)
                        {
                            libmei.AddChild(l, tuplet);
                        }
                    }
                    else
                    {
                        if (beam._parent != l._id)
                        {
                            libmei.AddChild(l, beam);
                        }
                    }
                }
                else
                {
                    if (tuplet != null)
                    {
                        libmei.AddChild(tuplet, note);

                        if (tuplet._parent != l._id)
                        {
                            libmei.AddChild(l, tuplet);
                        }
                    }
                    else
                    {
                        libmei.AddChild(l, note);
                    }
                }

            }
            case('BarRest')
            {
                brest = GenerateBarRest(bobj);

                if (brest != null)
                {
                    libmei.AddChild(l, brest);
                }
            }
            case('Slur')
            {
                line = GenerateLine(bobj);
            }
            case('CrescendoLine')
            {
                line = GenerateLine(bobj);
            }
            case('DimuendoLine')
            {
                line = GenerateLine(bobj);
            }
            case('OctavaLine')
            {
                line = GenerateLine(bobj);
            }
            case('Trill')
            {
                line = GenerateLine(bobj);
            }
            case('RepeatTimeLine')
            {
                RegisterVolta(bobj);
            }
            case('Line')
            {
                line = GenerateLine(bobj);
            }
        }

        if (line != null)
        {
            mlines = Self._property:MeasureLines;
            mlines.Push(line._id);
            Self._property:MeasureLines = mlines;
        }
    }

    for each LyricItem lobj in bar
    {
        ProcessLyric(lobj, objectPositions);
    }

    for each SymbolItem sobj in bar
    {
        ProcessSymbol(sobj, objectPositions);
    }

    return layers;
}  //$end

function GenerateClef (bobj) {
    //$module(ExportGenerators.mss)
    clefinfo = ConvertClef(bobj.StyleId);
    clef_el = libmei.Clef();

    libmei.AddChild(l, clef_el);

    libmei.AddAttribute(clef_el, 'shape', clefinfo[0]);
    libmei.AddAttribute(clef_el, 'line', clefinfo[1]);
    libmei.AddAttribute(clef_el, 'dis', clefinfo[2]);
    libmei.AddAttribute(clef_el, 'dis.place', clefinfo[3]);

    return clef_el;
}  //$end

function GenerateNoteRest (bobj, layer) {
    //$module(ExportGenerators.mss)
    nr = null;

    switch(bobj.NoteCount)
    {
        case(0)
        {
            nr = GenerateRest(bobj);
        }
        case(1)
        {
            nobj = bobj[0];
            nr = GenerateNote(nobj);
        }
        default
        {
            nr = GenerateChord(bobj);
        }
    }

    if (bobj.Dx != 0)
    {
        libmei.AddAttribute(nr, 'ho', ConvertOffsets(bobj.Dx));
    }

    if (bobj.CueSize = true and libmei.GetName(nr) != 'space')
    {
        libmei.AddAttribute(nr, 'size', 'cue');
    }

    if (bobj.Hidden = true and libmei.GetName(nr) != 'space')
    {
        libmei.AddAttribute(nr, 'visible', 'false');
    }

    if (bobj.Color != 0)
    {
        nrest_color = ConvertColor(bobj);
        libmei.AddAttribute(nr, 'color', nrest_color);
    }

    if (bobj.GraceNote = true)
    {
        libmei.addAttribute(nr, 'grace', 'acc');
    }

    if (bobj.IsAppoggiatura = true)
    {
        libmei.AddAttribute(nr, 'ornam', 'A');
    }

    if (bobj.IsAcciaccatura = true)
    {
        libmei.AddAttribute(nr, 'ornam', 'a');
    }

    if (bobj.GetArticulation(PauseArtic))
    {
        fermata = libmei.Fermata();
        libmei.AddAttribute(fermata, 'form', 'norm');
        libmei.AddAttribute(fermata, 'shape', 'curved');
        libmei.AddAttribute(fermata, 'startid', '#' & nr._id);
        libmei.AddAttribute(fermata, 'layer', bobj.VoiceNumber);
        libmei.AddAttribute(fermata, 'staff', bobj.ParentBar.ParentStaff.StaffNum);

        mlines = Self._property:MeasureLines;
        mlines.Push(fermata._id);
    }

    if (bobj.GetArticulation(TriPauseArtic))
    {
        fermata = libmei.Fermata();
        libmei.AddAttribute(fermata, 'form', 'norm');
        libmei.AddAttribute(fermata, 'shape', 'angular');
        libmei.AddAttribute(fermata, 'startid', '#' & nr._id);
        libmei.AddAttribute(fermata, 'layer', bobj.VoiceNumber);
        libmei.AddAttribute(fermata, 'staff', bobj.ParentBar.ParentStaff.StaffNum);

        mlines = Self._property:MeasureLines;
        mlines.Push(fermata._id);
    }

    if (bobj.GetArticulation(SquarePauseArtic))
    {
        fermata = libmei.Fermata();
        libmei.AddAttribute(fermata, 'form', 'norm');
        libmei.AddAttribute(fermata, 'shape', 'square');
        libmei.AddAttribute(fermata, 'startid', '#' & nr._id);
        libmei.AddAttribute(fermata, 'layer', bobj.VoiceNumber);
        libmei.AddAttribute(fermata, 'staff', bobj.ParentBar.ParentStaff.StaffNum);

        mlines = Self._property:MeasureLines;
        mlines.Push(fermata._id);
    }

    if (bobj.GetArticulation(StaccatoArtic))
    {
        libmei.AddAttributeValue(nr, 'artic', 'stacc');
    }

    if (bobj.GetArticulation(DownBowArtic))
    {
        libmei.AddAttributeValue(nr, 'artic', 'dnbow');
    }

    if (bobj.GetArticulation(UpBowArtic))
    {
        libmei.AddAttributeValue(nr, 'artic', 'upbow');
    }

    if (bobj.GetArticulation(MarcatoArtic))
    {
        libmei.AddAttributeValue(nr, 'artic', 'marc');
    }

    if (bobj.GetArticulation(AccentArtic))
    {
        libmei.addAttributeValue(nr, 'artic', 'acc');
    }

    if (bobj.GetArticulation(TenutoArtic))
    {
        libmei.AddAttributeValue(nr, 'artic', 'ten');
    }

    if (bobj.GetArticulation(StaccatissimoArtic))
    {
        libmei.AddAttributeValue(nr, 'artic', 'stacciss');
    }

    if (bobj.GetArticulation(StaccatissimoArtic))
    {
        libmei.AddAttributeValue(nr, 'artic', 'stacciss');
    }

    if (bobj.GetArticulation(PlusArtic))
    {
        libmei.AddAttributeValue(nr, 'artic', 'stop');
    }

    if (bobj.FallType = FallTypeDoit)
    {
        libmei.AddAttributeValue(nr, 'artic', 'doit');
    }

    if (bobj.FallType = FallTypeNormal)
    {
        libmei.AddAttributeValue(nr, 'artic', 'fall');
    }

    if (bobj.ScoopType = ScoopTypePlop)
    {
        libmei.AddAttributeValue(nr, 'artic', 'plop');
    }

    // a tremolo is a parent of note or chord in MEI
    if ((bobj.SingleTremolos > 0) or (bobj.SingleTremolos = -1))
    {
        // btrem = libmei.BTrem();
        // libmei.AddChild(btrem, nr);

        if (bobj.SingleTremolos = ZOnStem)
        {
            stemmod = 'z';
        }
        else
        {
            stemmod = bobj.SingleTremolos & 'slash';
        }

        libmei.AddAttribute(nr, 'stem.mod', stemmod);
    }

    return nr;
}  //$end

function GenerateRest (bobj) {
    //$module(ExportGenerators.mss)
    // check if it's a rest
    // check if it's hidden, in which case make MEI <space> instead of <rest>
    dur = bobj.Duration;
    meidur = ConvertDuration(dur);

    if (bobj.Hidden)
    {
        r = libmei.Space();
    }
    else
    {
        r = libmei.Rest();
    }

    libmei.AddAttribute(r, 'dur', meidur[0]);
    libmei.AddAttribute(r, 'dur.ges', dur & 'p');
    libmei.AddAttribute(r, 'dots', meidur[1]);

    if (bobj.Dx != 0)
    {
        libmei.AddAttribute(r, 'ho', ConvertOffsets(bobj.Dx));
    }

    if (bobj.Dy != 0)
    {
        libmei.AddAttribute(r, 'vo', ConvertOffsets(bobj.Dy));
    }

    if (bobj.CueSize = true and libmei.GetName(r) != 'space')
    {
        libmei.AddAttribute(r, 'size', 'cue');
    }

    if (bobj.Hidden = true and libmei.GetName(r) != 'space')
    {
        libmei.AddAttribute(r, 'visible', 'false');
    }

    if (bobj.Color != 0)
    {
        nrest_color = ConvertColor(bobj);
        libmei.AddAttribute(r, 'color', nrest_color);
    }

    if (bobj.GetArticulation(PauseArtic))
    {
        libmei.AddAttribute(r, 'fermata', 'above');
    }

    return r;
}  //$end

function GenerateNote (nobj) {
    //$module(ExportGenerators.mss)
    dur = nobj.ParentNoteRest.Duration;
    meidur = ConvertDuration(dur);
    ntinfo = ConvertDiatonicPitch(nobj.DiatonicPitch);
    pos = nobj.ParentNoteRest.Position;
    keysig = nobj.ParentNoteRest.ParentBar.GetKeySignatureAt(pos);

    n = libmei.Note();
    hash = SimpleNoteHash(nobj);
    n._property:hash = hash;

    libmei.AddAttribute(n, 'pnum', nobj.Pitch);
    libmei.AddAttribute(n, 'pname', ntinfo[0]);
    libmei.AddAttribute(n, 'oct', ntinfo[1]);
    libmei.AddAttribute(n, 'dur', meidur[0]);
    libmei.AddAttribute(n, 'dur.ges', dur & 'p');
    libmei.AddAttribute(n, 'dots', meidur[1]);

    // Accidentals will always be encoded as child elements, not attributes
    staff = nobj.ParentNoteRest.ParentBar.ParentStaff.StaffNum;

    if (nobj.NoteStyle != NormalNoteStyle)
    {
        nstyle = ConvertNoteStyle(nobj.NoteStyle);
        libmei.AddAttribute(n, 'headshape', nstyle);
    }

    accid = ConvertAccidental(nobj, keysig.Sharps);
    accVal = accid[0];
    isVisible = accid[1];

    if (accVal != ' ')
    {
        child = libmei.Accid();
        libmei.AddChild(n, child);

        if (isVisible = True)
        {
            libmei.AddAttribute(child, 'accid', accVal);
        }
        else
        {
            libmei.AddAttribute(child, 'accid.ges', accVal);
        }

        switch (nobj.AccidentalStyle)
        {
            case (CautionaryAcc)
            {
                libmei.AddAttribute(child, 'func', 'caution');
            }
            case (BracketedAcc)
            {
                libmei.AddAttribute(child, 'enclose', 'paren');
            }
        }
    }

    /*
        Ties
    */
    tieresolver = Self._property:TieResolver;
    if (tieresolver.PropertyExists(hash))
    {
        // this note is the end of the tie.
        tie_id = tieresolver[hash];
        tie = libmei.getElementById(tie_id);
        libmei.AddAttribute(tie, 'endid', '#' & n._id);
    }

    if (nobj.Tied = True)
    {
        // set the end hash
        parent_nr = nobj.ParentNoteRest;
        next_object = parent_nr.NextItem(parent_nr.VoiceNumber, 'NoteRest');

        if (next_object = null)
        {
            // check the first object in the next bar
            next_bar_num = parent_nr.ParentBar.BarNumber + 1;
            next_bar = parent_nr.ParentBar.ParentStaff.NthBar(next_bar_num);
            next_object = next_bar.NthBarObject(0);
        }

        for each next_note in next_object
        {
            if (nobj.Pitch = next_note.Pitch)
            {
                // it's probably this one.
                endhash = SimpleNoteHash(next_note);
                tie = libmei.Tie();
                libmei.AddAttribute(tie, 'startid', '#' & n._id);
                tieresolver[endhash] = tie._id;

                n._property:TieIds = CreateSparseArray(tie._id);
            }
        }
    }

    return n;
}  //$end

function GenerateChord (bobj) {
    //$module(ExportGenerators.mss)
    n = libmei.Chord();
    dur = bobj.Duration;
    meidur = ConvertDuration(dur);

    libmei.AddAttribute(n, 'dur', meidur[0]);
    libmei.AddAttribute(n, 'dur.ges', dur & 'p');
    libmei.AddAttribute(n, 'dots', meidur[1]);

    tiearr = CreateSparseArray();

    for each note in bobj
    {
        sn = GenerateNote(note);
        libmei.AddChild(n, sn);

        if (sn._property:TieIds != null)
        {
            tieids = sn._property:TieIds;
            tiearr = tiearr.Concat(tieids);
        }
    }

    n._property:TieIds = tiearr;

    return n;
}  //$end

function GenerateBarRest (bobj) {
    //$module(ExportGenerators.mss)
    switch (bobj.RestType)
    {
        case (WholeBarRest)
        {
            obj = libmei.MRest();
        }
        case (OneBarRepeat)
        {
            obj = libmei.MRpt();
        }
        case (TwoBarRepeat)
        {
            obj = libmei.MRpt2();
        }
        case (FourBarRepeat)
        {
            warnings = Self._property:warnings;
            warnings.Push(utils.Format(_ObjectHasNoMEISupport, 'A four-bar repeat'));
            return null;
        }
    }

    if (bobj.Hidden = true)
    {
        libmei.AddAttribute(obj, 'visible', 'false');
    }

    return obj;
}  //$end

function GenerateScoreDef (score) {
    //$module(ExportGenerators.mss)
    scoredef = libmei.ScoreDef();
    Self._property:_GlobalScoreDef = libmei.GetId(scoredef);

    docSettings = score.DocumentSetup;

    // this will ensure that the units specified by the user is the one that is
    // represented in the output.
    sibUnits = docSettings.UnitsInDocumentSetupDialog;
    unit = '';

    switch(sibUnits)
    {
        case(DocumentSetupUnitsInches)
        {
            unit = 'in';
        }
        case(DocumentSetupUnitsPoints)
        {
            unit = 'pt';
        }
        default
        {
            // Millimeters is the default for Sibelius
            unit = 'mm';
        }
    }

    libmei.AddAttribute(scoredef, 'page.width', docSettings.PageWidth & unit);
    libmei.AddAttribute(scoredef, 'page.height', docSettings.PageHeight & unit);
    libmei.AddAttribute(scoredef, 'page.leftmar', docSettings.PageLeftMargin & unit);
    libmei.AddAttribute(scoredef, 'page.rightmar', docSettings.PageRightMargin & unit);
    libmei.AddAttribute(scoredef, 'page.topmar', docSettings.PageTopMargin & unit);
    libmei.AddAttribute(scoredef, 'page.botmar', docSettings.PageBottomMargin & unit);

    showCautionaryAccidentals = score.EngravingRules.CautionaryNaturalsInKeySignatures;
    if (showCautionaryAccidentals = true)
    {
        libmei.AddAttribute(scoredef, 'key.sig.showchange', 'true');
    }

    systf = score.SystemStaff;
    timesig = systf.CurrentTimeSignature(1);

    libmei.AddAttribute(scoredef, 'meter.count', timesig.Numerator);
    libmei.AddAttribute(scoredef, 'meter.unit', timesig.Denominator);
    libmei.AddAttribute(scoredef, 'meter.sym', ConvertNamedTimeSignature(timesig.Text));
    libmei.AddAttribute(scoredef, 'ppq', '256'); // sibelius' internal ppq.

    staffgrp = GenerateStaffGroups(score);
    libmei.AddChild(scoredef, staffgrp);

    return scoredef;
}  //$end

function GenerateStaffGroups (score) {
    //$module(ExportGenerators.mss)
    staffdict = CreateDictionary();
    parentstgrp = libmei.StaffGrp();
    numstaff = score.StaffCount;

    for each Staff s in score
    {
        std = libmei.StaffDef();
        libmei.XMLIdToObjectMap[std._id] = s;

        libmei.AddAttribute(std, 'n', s.StaffNum);
        libmei.AddAttribute(std, 'label', s.FullInstrumentName);
        libmei.AddAttribute(std, 'lines', s.InitialInstrumentType.NumStaveLines);

        clefinfo = ConvertClef(s.InitialClefStyleId);
        libmei.AddAttribute(std, 'clef.shape', clefinfo[0]);
        libmei.AddAttribute(std, 'clef.line', clefinfo[1]);
        libmei.AddAttribute(std, 'clef.dis', clefinfo[2]);
        libmei.AddAttribute(std, 'clef.dis.place', clefinfo[3]);
        libmei.AddAttribute(std, 'key.sig', ConvertKeySignature(s.InitialKeySignature.Sharps));

        if (s.InitialKeySignature.Major)
        {
            libmei.AddAttribute(std, 'key.mode', 'major');
        }
        else
        {
            libmei.AddAttribute(std, 'key.mode', 'minor');
        }

        staffdict[s.StaffNum] = std;
    }

    stgpdict = CreateDictionary();
    stgprevidx = CreateDictionary();
    stgpnum = 1;
    brackets = score.BracketsAndBraces;

    for each bkt in brackets
    {
        stgp = libmei.StaffGrp();
        libmei.AddAttribute(stgp, 'symbol', ConvertBracket(bkt.BracketType));
        libmei.AddAttribute(stgp, 'n', stgpnum);
        stgpdict[stgpnum] = stgp;

        if (bkt.BracketType != BracketSub)
        {
            for i = bkt.TopStaveNum to bkt.BottomStaveNum + 1
            {
                libmei.AddChild(stgp, staffdict[i]);
                stgprevidx[i] = stgpnum;
            }
        }
        stgpnum = stgpnum + 1;
    }

    // finally, we need to put them all in the right order.
    stgpnum_added = CreateSparseArray();
    for j = 1 to numstaff + 1
    {
        if (stgprevidx.PropertyExists(j))
        {
            // there is a group
            stgpnum = stgprevidx[j];
            if (utils.IsInArray(stgpnum_added, stgpnum) = false)
            {
                libmei.AddChild(parentstgrp, stgpdict[stgpnum]);
                stgpnum_added.Push(stgpnum);
            }
        }
        else
        {
            libmei.AddChild(parentstgrp, staffdict[j]);
        }

    }
    return parentstgrp;
}  //$end

function GenerateLine (bobj) {
    //$module(ExportGenerators.mss)
    line = null;
    bar = bobj.ParentBar;

    switch (bobj.Type)
    {
        case ('Slur')
        {
            line = libmei.Slur();
            slurrend = ConvertSlurStyle(bobj.StyleId);
            libmei.AddAttribute(line, 'rend', slurrend[1]);
        }
        case ('CrescendoLine')
        {
            line = libmei.Hairpin();
            libmei.AddAttribute(line, 'form', 'cres');
        }
        case ('DimuendoLine')
        {
            line = libmei.Hairpin();
            libmei.AddAttribute(line, 'form', 'dim');
        }
        case ('OctavaLine')
        {
            line = libmei.Octave();
            octrend = ConvertOctava(bobj.StyleId);
            libmei.AddAttribute(octl, 'dis', octrend[0]);
            libmei.AddAttribute(octl, 'dis.place', octrend[1]);
        }
        case ('Trill')
        {
            line = GenerateTrill(bobj);
            // NB: Return here since the trill already has its properties set.
            return line;
        }
        case ('Line')
        {
            // a generic line element.
            linecomps = MSplitString(bobj.StyleId, '.');
            switch(linecomps[2])
            {
                case('bracket')
                {
                    line = libmei.Line();
                    libmei.AddAttribute(line, 'type', 'bracket');
                    if (linecomps.Length > 4)
                    {
                        if (linecomps[4] = 'start')
                        {
                            libmei.AddAttribute(line, 'subtype', 'start');
                        }

                        if (linecomps[4] = 'end')
                        {
                            libmei.AddAttribute(line, 'subtype', 'end');
                        }
                    }
                }
            }
        }
    }

    if (line = null)
    {
        return null;
    }

    line = AddBarObjectInfoToElement(bobj, line);

    return line;
}  //$end

function GenerateTrill (bobj) {
    //$module(ExportGenerators.mss)
    /* There are two types of trills in Sibelius: A line object and a
        symbol object. This method normalizes both of these.
    */
    trill = libmei.Trill();
    voicenum = bobj.VoiceNumber;
    bar = bobj.ParentBar;

    if (voicenum = 0)
    {
        warnings = Self._property:warnings;
        warnings.Push(utils.Format(_ObjectAssignedToAllVoicesWarning, bobj.Type));
        voicenum = 1;
    }

    obj = GetNoteObjectAtPosition(bobj);

    if (obj != null)
    {
        libmei.AddAttribute(trill, 'startid', '#' & obj._id);
    }

    trill = AddBarObjectInfoToElement(bobj, trill);

    return trill;
}  //$end

function GenerateMEIHeader () {
    //$module(ExportGenerators.mss)
    // takes in a Sibelius Score object
    // returns a libmei tree (i.e., nested objects and arrays) with a MEI header with metadata
    score = Self._property:ActiveScore;
    header = libmei.MeiHead();
    fileD = libmei.FileDesc();
    titleS = libmei.TitleStmt();
    libmei.AddChild(header, fileD);
    libmei.AddChild(fileD, titleS);

    workDesc = libmei.WorkDesc();
    wd_work = libmei.Work();
    wd_titleStmt = libmei.TitleStmt();
    wd_title = libmei.Title();
    wd_respStmt = libmei.RespStmt();
    libmei.AddChild(wd_titleStmt, wd_title);
    libmei.AddChild(wd_titleStmt, wd_respStmt);
    libmei.AddChild(wd_work, wd_titleStmt);
    libmei.AddChild(workDesc, wd_work);

    title = libmei.Title();
    libmei.AddChild(titleS, title);

    if (score.Title != '')
    {
        libmei.SetText(title, score.Title);
        libmei.SetText(wd_title, score.Title);
    }
    if (score.Subtitle != '')
    {
        subtitle = libmei.Title();
        libmei.AddChild(titleS, subtitle);
        libmei.SetText(subtitle, score.Subtitle);
        libmei.AddAttribute(subtitle, 'type', 'subtitle');
    }
    if (score.Composer != '')
    {
        composer = libmei.Composer();
        libmei.AddChild(titleS, composer);
        libmei.SetText(composer, score.Composer);
        wd_composer = libmei.PersName();
        libmei.AddAttribute(wd_composer, 'role', 'composer');
        libmei.SetText(wd_composer, score.Composer);
        libmei.AddChild(wd_respStmt, wd_composer);
    }
    if (score.Lyricist != '')
    {
        lyricist = libmei.Lyricist();
        libmei.AddChild(titleS, lyricist);
        libmei.SetText(lyricist, score.Lyricist);
        wd_lyricist = libmei.PersName();
        libmei.AddAttribute(wd_lyricist, 'role', 'lyricist');
        libmei.SetText(wd_lyricist, score.Lyricist);
        libmei.AddChild(wd_respStmt, wd_lyricist);
    }
    if (score.Arranger != '')
    {
        arranger = libmei.Arranger();
        libmei.AddChild(titleS, arranger);
        libmei.SetText(arranger, score.Arranger);
        wd_arranger = libmei.PersName();
        libmei.AddAttribute(wd_arranger, 'role', 'arranger');
        libmei.SetText(wd_arranger, score.Arranger);
        libmei.AddChild(wd_respStmt, wd_arranger);
    }
    if (score.OtherInformation != '')
    {
        wd_notesStmt = libmei.NotesStmt();
        ns_annot = libmei.Annot();
        libmei.AddChild(wd_work, wd_notesStmt);
        libmei.AddChild(wd_notesStmt, ns_annot);
        libmei.SetText(ns_annot, score.OtherInformation);
    }

    respS = libmei.RespStmt();
    libmei.AddChild(titleS, respS);
    persN = libmei.PersName();
    libmei.AddChild(respS, persN);
    pubS = libmei.PubStmt();
    libmei.AddChild(fileD, pubS);

    avail = libmei.Availability();
    ur = libmei.UseRestrict();
    libmei.AddChild(avail, ur);
    libmei.AddChild(pubS, avail);
    libmei.SetText(ur, score.Copyright);

    encodingD = libmei.EncodingDesc();
    libmei.AddChild(header, encodingD);
    appI = libmei.AppInfo();
    libmei.AddChild(encodingD, appI);

    applic = libmei.Application();
    libmei.SetId(applic, 'sibelius7');
    libmei.AddChild(appI, applic);
    libmei.AddAttribute(applic, 'version', Sibelius.ProgramVersion);
    nm = libmei.Name();
    libmei.SetText(nm, 'Sibelius (' & Version & ')');
    libmei.AddChild(applic, nm);

    libmei.AddChild(header, workDesc);

    return header;
}  //$end

function GenerateMEIMusic () {
    //$module(ExportGenerators.mss)
    score = Self._property:ActiveScore;

    Self._property:TieResolver = CreateDictionary();
    Self._property:LyricWords = CreateDictionary();
    Self._property:SpecialBarlines = CreateDictionary();
    Self._property:SystemText = CreateDictionary();
    Self._property:LayerObjectPositions = null;
    Self._property:ObjectPositions = CreateDictionary();

    // the section parent is used in case we need to inject
    // a new parent later (e.g., for endings)
    Self._property:SectionParent = null;
    Self._property:VoltaBars = CreateDictionary();
    Self._property:ActiveVolta = null;
    Self._property:VoltaElement = null;

    // grab some global markers from the system staff
    // This will store it for later use.
    ProcessSystemStaff(score);

    music = libmei.Music();
    body = libmei.Body();
    mdiv = libmei.Mdiv();
    sco = libmei.Score();

    libmei.AddChild(music, body);
    libmei.AddChild(body, mdiv);
    libmei.AddChild(mdiv, sco);

    scd = sibmei2.GenerateScoreDef(score);
    libmei.AddChild(sco, scd);

    barmap = ConvertSibeliusStructure(score);
    numbars = barmap.GetPropertyNames();
    numbars = numbars.Length;
    Self._property:BarMap = barmap;

    section = libmei.Section();
    libmei.AddChild(sco, section);

    for j = 1 to numbars + 1
    {
        // this value may get changed by the volta processor
        // to inject a new parent in the hierarchy.
        progressMsg = utils.Format(_ExportingBars, j, numbars);
        cont = Sibelius.UpdateProgressDialog(j, progressMsg);

        if (cont = 0)
        {
            ExitPlugin();
        }

        m = GenerateMeasure(j);
        ending = ProcessVolta(j);

        if (ending != null)
        {
            libmei.AddChild(section, ending);
            libmei.AddChild(ending, m);
        }
        else
        {
            libmei.AddChild(section, m);
        }
    }

    return music;
}  //$end

function GenerateMeasure (num) {
    //$module(ExportGenerators.mss)

    score = Self._property:ActiveScore;
    // measureties
    Self._property:MeasureTies = CreateSparseArray();
    Self._property:MeasureLines = CreateSparseArray();

    m = libmei.Measure();
    libmei.AddAttribute(m, 'n', num);

    barmap = Self._property:BarMap;
    staves = barmap[num];
    children = CreateSparseArray();

    // since so much metadata about the staff and other context
    // is available on the bar that should now be on the measure, go through the bars
    // and try to extract it.

    for i = 1 to staves.Length + 1
    {
        this_staff = score.NthStaff(i);
        bar = this_staff[num];

        if (bar.ExternalBarNumberString)
        {
            libmei.AddAttribute(m, 'label', bar.ExternalBarNumberString);
        }

        s = GenerateStaff(i, num);
        libmei.AddChild(m, s);
    }

    mties = Self._property:MeasureTies;

    for each tie in mties
    {
        m.children.Push(tie);
    }

    mlines = Self._property:MeasureLines;

    for each line in mlines
    {
        m.children.Push(line);
    }

    specialbarlines = Self._property:SpecialBarlines;

    if (specialbarlines.PropertyExists(num))
    {
        bline = specialbarlines[num];

        if (bline = 'rptstart')
        {
            libmei.AddAttribute(m, 'left', bline);
        }
        else
        {
            libmei.AddAttribute(m, 'right', bline);
        }
    }

    systemtext = Self._property:SystemText;

    if (systemtext.PropertyExists(num))
    {
        textobjs = systemtext[num];
        for each textobj in textobjs
        {
            text = ConvertText(textobj);

            if (text != null)
            {
                libmei.AddChild(m, text);
            }
        }
    }

    return m;
}  //$end

function GenerateStaff (staffnum, measurenum) {
    //$module(ExportGenerators.mss)
    score = Self._property:ActiveScore;
    this_staff = score.NthStaff(staffnum);
    bar = this_staff[measurenum];

    stf = libmei.Staff();

    if (bar.OnHiddenStave)
    {
        libmei.AddAttribute(stf, 'visible', 'false');
    }

    libmei.AddAttribute(stf, 'n', staffnum);

    layers = GenerateLayers(staffnum, measurenum);
    // NB: Completely resets any previous children!
    libmei.SetChildren(stf, layers);

    return stf;
}  //$end

function GenerateLayers (staffnum, measurenum) {
    //$module(ExportGenerators.mss)
    layerdict = CreateDictionary();
    layers = CreateSparseArray();

    objectPositions = Self._property:ObjectPositions;

    if (objectPositions.PropertyExists(staffnum) = False)
    {
        objectPositions[staffnum] = CreateDictionary();
    }

    staffObjectPositions = objectPositions[staffnum];

    score = Self._property:ActiveScore;
    this_staff = score.NthStaff(staffnum);
    bar = this_staff[measurenum];

    for each bobj in bar
    {
        voicenumber = bobj.VoiceNumber;

        if (staffObjectPositions.PropertyExists(bar.BarNumber) = False)
        {
            staffObjectPositions[bar.BarNumber] = CreateDictionary();
        }

        barObjectPositions = staffObjectPositions[bar.BarNumber];

        if (layerdict.PropertyExists(voicenumber))
        {
            l = layerdict[voicenumber];
        }
        else
        {
            if (voicenumber != 0)
            {
                l = libmei.Layer();
                layers.Push(l._id);

                if (barObjectPositions.PropertyExists(voicenumber) = False)
                {
                    barObjectPositions[voicenumber] = CreateDictionary();
                }

                layerdict[voicenumber] = l;
                libmei.AddAttribute(l, 'n', voicenumber);
            }
        }

        obj = null;
        line = null;
        parent = null;
        beam = null;
        tuplet = null;

        switch (bobj.Type)
        {
            case('Clef')
            {
                clef = GenerateClef(bobj);
                libmei.AddChild(l, clef);
            }
            case('NoteRest')
            {
                note = GenerateNoteRest(bobj, l);

                // record the position of this element
                objVoice = barObjectPositions[voicenumber];
                objVoice[bobj.Position] = note._id;

                if (note._property:TieIds != null)
                {
                    ties = note._property:TieIds;
                    mties = Self._property:MeasureTies;
                    mties = mties.Concat(ties);
                    Self._property:MeasureTies = mties;
                }

                beam = ProcessBeam(bobj, l);
                tuplet = ProcessTuplet(bobj, note, l);

                if (beam != null)
                {
                    libmei.AddChild(beam, note);

                    if (tuplet != null)
                    {
                        if (beam._parent != tuplet._id)
                        {
                            libmei.AddChild(tuplet, beam);
                        }

                        if (tuplet._parent != l._id)
                        {
                            libmei.AddChild(l, tuplet);
                        }
                    }
                    else
                    {
                        if (beam._parent != l._id)
                        {
                            libmei.AddChild(l, beam);
                        }
                    }
                }
                else
                {
                    if (tuplet != null)
                    {
                        libmei.AddChild(tuplet, note);

                        if (tuplet._parent != l._id)
                        {
                            libmei.AddChild(l, tuplet);
                        }
                    }
                    else
                    {
                        libmei.AddChild(l, note);
                    }
                }

            }
            case('BarRest')
            {
                brest = GenerateBarRest(bobj);

                if (brest != null)
                {
                    libmei.AddChild(l, brest);
                }
            }
            case('Slur')
            {
                line = GenerateLine(bobj);
            }
            case('CrescendoLine')
            {
                line = GenerateLine(bobj);
            }
            case('DimuendoLine')
            {
                line = GenerateLine(bobj);
            }
            case('OctavaLine')
            {
                line = GenerateLine(bobj);
            }
            case('Trill')
            {
                line = GenerateLine(bobj);
            }
            case('RepeatTimeLine')
            {
                RegisterVolta(bobj);
            }
            case('Line')
            {
                line = GenerateLine(bobj);
            }
        }

        if (line != null)
        {
            mlines = Self._property:MeasureLines;
            mlines.Push(line._id);
            Self._property:MeasureLines = mlines;
        }
    }

    for each LyricItem lobj in bar
    {
        ProcessLyric(lobj, objectPositions);
    }

    for each SymbolItem sobj in bar
    {
        ProcessSymbol(sobj, objectPositions);
    }

    return layers;
}  //$end

function GenerateClef (bobj) {
    //$module(ExportGenerators.mss)
    clefinfo = ConvertClef(bobj.StyleId);
    clef_el = libmei.Clef();

    libmei.AddChild(l, clef_el);

    libmei.AddAttribute(clef_el, 'shape', clefinfo[0]);
    libmei.AddAttribute(clef_el, 'line', clefinfo[1]);
    libmei.AddAttribute(clef_el, 'dis', clefinfo[2]);
    libmei.AddAttribute(clef_el, 'dis.place', clefinfo[3]);

    return clef_el;
}  //$end

function GenerateNoteRest (bobj, layer) {
    //$module(ExportGenerators.mss)
    nr = null;
    switch(bobj.NoteCount)
    {
        case(0)
        {
            nr = GenerateRest(bobj);
        }
        case(1)
        {
            nobj = bobj[0];
            nr = GenerateNote(nobj);
        }
        default
        {
            nr = GenerateChord(bobj);
        }
    }

    if (bobj.Dx != 0)
    {
        libmei.AddAttribute(nr, 'ho', ConvertOffsets(bobj.Dx));
    }

    if (bobj.CueSize = true and libmei.GetName(nr) != 'space')
    {
        libmei.AddAttribute(nr, 'size', 'cue');
    }

    if (bobj.Hidden = true and libmei.GetName(nr) != 'space')
    {
        libmei.AddAttribute(nr, 'visible', 'false');
    }

    if (bobj.Color != 0)
    {
        nrest_color = ConvertColor(bobj);
        libmei.AddAttribute(nr, 'color', nrest_color);
    }

    if (bobj.GraceNote = true)
    {
        libmei.addAttribute(nr, 'grace', 'acc');
    }

    if (bobj.IsAppoggiatura = true)
    {
        libmei.AddAttribute(nr, 'ornam', 'A');
    }

    if (bobj.IsAcciaccatura = true)
    {
        libmei.AddAttribute(nr, 'ornam', 'a');
    }

    if (bobj.GetArticulation(PauseArtic))
    {
        fermata = libmei.Fermata();
        libmei.AddAttribute(fermata, 'form', 'norm');
        libmei.AddAttribute(fermata, 'shape', 'curved');
        libmei.AddAttribute(fermata, 'startid', '#' & nr._id);
        libmei.AddAttribute(fermata, 'layer', bobj.VoiceNumber);
        libmei.AddAttribute(fermata, 'staff', bobj.ParentBar.ParentStaff.StaffNum);

        mlines = Self._property:MeasureLines;
        mlines.Push(fermata._id);
    }

    if (bobj.GetArticulation(TriPauseArtic))
    {
        fermata = libmei.Fermata();
        libmei.AddAttribute(fermata, 'form', 'norm');
        libmei.AddAttribute(fermata, 'shape', 'angular');
        libmei.AddAttribute(fermata, 'startid', '#' & nr._id);
        libmei.AddAttribute(fermata, 'layer', bobj.VoiceNumber);
        libmei.AddAttribute(fermata, 'staff', bobj.ParentBar.ParentStaff.StaffNum);

        mlines = Self._property:MeasureLines;
        mlines.Push(fermata._id);
    }

    if (bobj.GetArticulation(SquarePauseArtic))
    {
        fermata = libmei.Fermata();
        libmei.AddAttribute(fermata, 'form', 'norm');
        libmei.AddAttribute(fermata, 'shape', 'square');
        libmei.AddAttribute(fermata, 'startid', '#' & nr._id);
        libmei.AddAttribute(fermata, 'layer', bobj.VoiceNumber);
        libmei.AddAttribute(fermata, 'staff', bobj.ParentBar.ParentStaff.StaffNum);

        mlines = Self._property:MeasureLines;
        mlines.Push(fermata._id);
    }

    if (bobj.GetArticulation(StaccatoArtic))
    {
        libmei.AddAttributeValue(nr, 'artic', 'stacc');
    }

    if (bobj.GetArticulation(DownBowArtic))
    {
        libmei.AddAttributeValue(nr, 'artic', 'dnbow');
    }

    if (bobj.GetArticulation(UpBowArtic))
    {
        libmei.AddAttributeValue(nr, 'artic', 'upbow');
    }

    if (bobj.GetArticulation(MarcatoArtic))
    {
        libmei.AddAttributeValue(nr, 'artic', 'marc');
    }

    if (bobj.GetArticulation(AccentArtic))
    {
        libmei.addAttributeValue(nr, 'artic', 'acc');
    }

    if (bobj.GetArticulation(TenutoArtic))
    {
        libmei.AddAttributeValue(nr, 'artic', 'ten');
    }

    if (bobj.GetArticulation(StaccatissimoArtic))
    {
        libmei.AddAttributeValue(nr, 'artic', 'stacciss');
    }

    if (bobj.GetArticulation(StaccatissimoArtic))
    {
        libmei.AddAttributeValue(nr, 'artic', 'stacciss');
    }

    if (bobj.GetArticulation(PlusArtic))
    {
        libmei.AddAttributeValue(nr, 'artic', 'stop');
    }

    if (bobj.FallType = FallTypeDoit)
    {
        libmei.AddAttributeValue(nr, 'artic', 'doit');
    }

    if (bobj.FallType = FallTypeNormal)
    {
        libmei.AddAttributeValue(nr, 'artic', 'fall');
    }

    if (bobj.ScoopType = ScoopTypePlop)
    {
        libmei.AddAttributeValue(nr, 'artic', 'plop');
    }

    // a tremolo is a parent of note or chord in MEI
    if ((bobj.SingleTremolos > 0) or (bobj.SingleTremolos = -1))
    {
        btrem = libmei.BTrem();
        libmei.AddChild(btrem, nr);

        if (bobj.SingleTremolos = ZOnStem)
        {
            stemmod = 'z';
        }
        else
        {
            stemmod = bobj.SingleTremolos & 'slash';
        }

        libmei.AddAttribute(nr, 'stem.mod', stemmod);

        return btrem;
    }

    return nr;
}  //$end

function GenerateRest (bobj) {
    //$module(ExportGenerators.mss)
    // check if it's a rest
    // check if it's hidden, in which case make MEI <space> instead of <rest>
    dur = bobj.Duration;
    meidur = ConvertDuration(dur);

    if (bobj.Hidden)
    {
        r = libmei.Space();
    }
    else
    {
        r = libmei.Rest();
    }

    libmei.AddAttribute(r, 'dur', meidur[0]);
    libmei.AddAttribute(r, 'dur.ges', dur & 'p');
    libmei.AddAttribute(r, 'dots', meidur[1]);

    if (bobj.Dx != 0)
    {
        libmei.AddAttribute(r, 'ho', ConvertOffsets(bobj.Dx));
    }

    if (bobj.Dy != 0)
    {
        libmei.AddAttribute(r, 'vo', ConvertOffsets(bobj.Dy));
    }

    if (bobj.CueSize = true and libmei.GetName(r) != 'space')
    {
        libmei.AddAttribute(r, 'size', 'cue');
    }

    if (bobj.Hidden = true and libmei.GetName(r) != 'space')
    {
        libmei.AddAttribute(r, 'visible', 'false');
    }

    if (bobj.Color != 0)
    {
        nrest_color = ConvertColor(bobj);
        libmei.AddAttribute(r, 'color', nrest_color);
    }

    if (bobj.GetArticulation(PauseArtic))
    {
        libmei.AddAttribute(r, 'fermata', 'above');
    }

    return r;
}  //$end

function GenerateNote (nobj) {
    //$module(ExportGenerators.mss)
    dur = nobj.ParentNoteRest.Duration;
    meidur = ConvertDuration(dur);
    ntinfo = ConvertDiatonicPitch(nobj.DiatonicPitch);
    pos = nobj.ParentNoteRest.Position;
    keysig = nobj.ParentNoteRest.ParentBar.GetKeySignatureAt(pos);

    n = libmei.Note();
    hash = SimpleNoteHash(nobj);
    n._property:hash = hash;

    libmei.AddAttribute(n, 'pnum', nobj.Pitch);
    libmei.AddAttribute(n, 'pname', ntinfo[0]);
    libmei.AddAttribute(n, 'oct', ntinfo[1]);
    libmei.AddAttribute(n, 'dur', meidur[0]);
    libmei.AddAttribute(n, 'dur.ges', dur & 'p');
    libmei.AddAttribute(n, 'dots', meidur[1]);

    // Accidentals will always be encoded as child elements, not attributes
    staff = nobj.ParentNoteRest.ParentBar.ParentStaff.StaffNum;

    if (nobj.NoteStyle != NormalNoteStyle)
    {
        nstyle = ConvertNoteStyle(nobj.NoteStyle);
        libmei.AddAttribute(n, 'headshape', nstyle);
    }

    accid = ConvertAccidental(nobj, keysig.Sharps);
    accVal = accid[0];
    isVisible = accid[1];

    if (accVal != ' ')
    {
        child = libmei.Accid();
        libmei.AddChild(n, child);

        if (isVisible = True)
        {
            libmei.AddAttribute(child, 'accid', accVal);
        }
        else
        {
            libmei.AddAttribute(child, 'accid.ges', accVal);
        }

        switch (nobj.AccidentalStyle)
        {
            case (CautionaryAcc)
            {
                libmei.AddAttribute(child, 'func', 'caution');
            }
            case (BracketedAcc)
            {
                libmei.AddAttribute(child, 'enclose', 'paren');
            }
        }
    }

    /*
        Ties
    */
    tieresolver = Self._property:TieResolver;
    if (tieresolver.PropertyExists(hash))
    {
        // this note is the end of the tie.
        tie_id = tieresolver[hash];
        tie = libmei.getElementById(tie_id);
        libmei.AddAttribute(tie, 'endid', '#' & n._id);
    }

    if (nobj.Tied = True)
    {
        // set the end hash
        parent_nr = nobj.ParentNoteRest;
        next_object = parent_nr.NextItem(parent_nr.VoiceNumber, 'NoteRest');

        if (next_object = null)
        {
            // check the first object in the next bar
            next_bar_num = parent_nr.ParentBar.BarNumber + 1;
            next_bar = parent_nr.ParentBar.ParentStaff.NthBar(next_bar_num);
            next_object = next_bar.NthBarObject(0);
        }

        for each next_note in next_object
        {
            if (nobj.Pitch = next_note.Pitch)
            {
                // it's probably this one.
                endhash = SimpleNoteHash(next_note);
                tie = libmei.Tie();
                libmei.AddAttribute(tie, 'startid', '#' & n._id);
                tieresolver[endhash] = tie._id;

                n._property:TieIds = CreateSparseArray(tie._id);
            }
        }
    }

    return n;
}  //$end

function GenerateChord (bobj) {
    //$module(ExportGenerators.mss)
    n = libmei.Chord();
    dur = bobj.Duration;
    meidur = ConvertDuration(dur);

    libmei.AddAttribute(n, 'dur', meidur[0]);
    libmei.AddAttribute(n, 'dur.ges', dur & 'p');
    libmei.AddAttribute(n, 'dots', meidur[1]);

    tiearr = CreateSparseArray();

    for each note in bobj
    {
        sn = GenerateNote(note);
        libmei.AddChild(n, sn);

        if (sn._property:TieIds != null)
        {
            tieids = sn._property:TieIds;
            tiearr = tiearr.Concat(tieids);
        }
    }

    n._property:TieIds = tiearr;

    return n;
}  //$end

function GenerateBarRest (bobj) {
    //$module(ExportGenerators.mss)
    switch (bobj.RestType)
    {
        case (WholeBarRest)
        {
            obj = libmei.MRest();
        }
        case (OneBarRepeat)
        {
            obj = libmei.MRpt();
        }
        case (TwoBarRepeat)
        {
            obj = libmei.MRpt2();
        }
        case (FourBarRepeat)
        {
            warnings = Self._property:warnings;
            warnings.Push(utils.Format(_ObjectHasNoMEISupport, 'A four-bar repeat'));
            return null;
        }
    }

    if (bobj.Hidden = true)
    {
        libmei.AddAttribute(obj, 'visible', 'false');
    }

    return obj;
}  //$end

function GenerateScoreDef (score) {
    //$module(ExportGenerators.mss)
    scoredef = libmei.ScoreDef();
    Self._property:_GlobalScoreDef = libmei.GetId(scoredef);

    docSettings = score.DocumentSetup;

    // this will ensure that the units specified by the user is the one that is
    // represented in the output.
    sibUnits = docSettings.UnitsInDocumentSetupDialog;
    unit = '';

    switch(sibUnits)
    {
        case(DocumentSetupUnitsInches)
        {
            unit = 'in';
        }
        case(DocumentSetupUnitsPoints)
        {
            unit = 'pt';
        }
        default
        {
            // Millimeters is the default for Sibelius
            unit = 'mm';
        }
    }

    libmei.AddAttribute(scoredef, 'page.width', docSettings.PageWidth & unit);
    libmei.AddAttribute(scoredef, 'page.height', docSettings.PageHeight & unit);
    libmei.AddAttribute(scoredef, 'page.leftmar', docSettings.PageLeftMargin & unit);
    libmei.AddAttribute(scoredef, 'page.rightmar', docSettings.PageRightMargin & unit);
    libmei.AddAttribute(scoredef, 'page.topmar', docSettings.PageTopMargin & unit);
    libmei.AddAttribute(scoredef, 'page.botmar', docSettings.PageBottomMargin & unit);

    showCautionaryAccidentals = score.EngravingRules.CautionaryNaturalsInKeySignatures;
    if (showCautionaryAccidentals = true)
    {
        libmei.AddAttribute(scoredef, 'key.sig.showchange', 'true');
    }

    systf = score.SystemStaff;
    timesig = systf.CurrentTimeSignature(1);

    libmei.AddAttribute(scoredef, 'meter.count', timesig.Numerator);
    libmei.AddAttribute(scoredef, 'meter.unit', timesig.Denominator);
    libmei.AddAttribute(scoredef, 'meter.sym', ConvertNamedTimeSignature(timesig.Text));
    libmei.AddAttribute(scoredef, 'ppq', '256'); // sibelius' internal ppq.

    staffgrp = GenerateStaffGroups(score);
    libmei.AddChild(scoredef, staffgrp);

    return scoredef;
}  //$end

function GenerateStaffGroups (score) {
    //$module(ExportGenerators.mss)
    staffdict = CreateDictionary();
    parentstgrp = libmei.StaffGrp();
    numstaff = score.StaffCount;

    for each Staff s in score
    {
        std = libmei.StaffDef();
        libmei.XMLIdToObjectMap[std._id] = s;

        libmei.AddAttribute(std, 'n', s.StaffNum);
        libmei.AddAttribute(std, 'label', s.FullInstrumentName);
        libmei.AddAttribute(std, 'lines', s.InitialInstrumentType.NumStaveLines);

        clefinfo = ConvertClef(s.InitialClefStyleId);
        libmei.AddAttribute(std, 'clef.shape', clefinfo[0]);
        libmei.AddAttribute(std, 'clef.line', clefinfo[1]);
        libmei.AddAttribute(std, 'clef.dis', clefinfo[2]);
        libmei.AddAttribute(std, 'clef.dis.place', clefinfo[3]);
        libmei.AddAttribute(std, 'key.sig', ConvertKeySignature(s.InitialKeySignature.Sharps));

        if (s.InitialKeySignature.Major)
        {
            libmei.AddAttribute(std, 'key.mode', 'major');
        }
        else
        {
            libmei.AddAttribute(std, 'key.mode', 'minor');
        }

        staffdict[s.StaffNum] = std;
    }

    stgpdict = CreateDictionary();
    stgprevidx = CreateDictionary();
    stgpnum = 1;
    brackets = score.BracketsAndBraces;

    for each bkt in brackets
    {
        stgp = libmei.StaffGrp();
        libmei.AddAttribute(stgp, 'symbol', ConvertBracket(bkt.BracketType));
        libmei.AddAttribute(stgp, 'n', stgpnum);
        stgpdict[stgpnum] = stgp;

        if (bkt.BracketType != BracketSub)
        {
            for i = bkt.TopStaveNum to bkt.BottomStaveNum + 1
            {
                libmei.AddChild(stgp, staffdict[i]);
                stgprevidx[i] = stgpnum;
            }
        }
        stgpnum = stgpnum + 1;
    }

    // finally, we need to put them all in the right order.
    stgpnum_added = CreateSparseArray();
    for j = 1 to numstaff + 1
    {
        if (stgprevidx.PropertyExists(j))
        {
            // there is a group
            stgpnum = stgprevidx[j];
            if (utils.IsInArray(stgpnum_added, stgpnum) = false)
            {
                libmei.AddChild(parentstgrp, stgpdict[stgpnum]);
                stgpnum_added.Push(stgpnum);
            }
        }
        else
        {
            libmei.AddChild(parentstgrp, staffdict[j]);
        }

    }
    return parentstgrp;
}  //$end

function GenerateLine (bobj) {
    //$module(ExportGenerators.mss)
    line = null;
    bar = bobj.ParentBar;

    switch (bobj.Type)
    {
        case ('Slur')
        {
            line = libmei.Slur();
            slurrend = ConvertSlurStyle(bobj.StyleId);
            libmei.AddAttribute(line, 'rend', slurrend[1]);
        }
        case ('CrescendoLine')
        {
            line = libmei.Hairpin();
            libmei.AddAttribute(line, 'form', 'cres');
        }
        case ('DimuendoLine')
        {
            line = libmei.Hairpin();
            libmei.AddAttribute(line, 'form', 'dim');
        }
        case ('OctavaLine')
        {
            line = libmei.Octave();
            octrend = ConvertOctava(bobj.StyleId);
            libmei.AddAttribute(octl, 'dis', octrend[0]);
            libmei.AddAttribute(octl, 'dis.place', octrend[1]);
        }
        case ('Trill')
        {
            line = GenerateTrill(bobj);
            // NB: Return here since the trill already has its properties set.
            return line;
        }
        case ('Line')
        {
            // a generic line element.
            linecomps = MSplitString(bobj.StyleId, '.');
            switch(linecomps[2])
            {
                case('bracket')
                {
                    line = libmei.Line();
                    libmei.AddAttribute(line, 'type', 'bracket');
                    if (linecomps.Length > 4)
                    {
                        if (linecomps[4] = 'start')
                        {
                            libmei.AddAttribute(line, 'subtype', 'start');
                        }

                        if (linecomps[4] = 'end')
                        {
                            libmei.AddAttribute(line, 'subtype', 'end');
                        }
                    }
                }
            }
        }
    }

    if (line = null)
    {
        return null;
    }

    line = AddBarObjectInfoToElement(bobj, line);

    return line;
}  //$end

function GenerateTrill (bobj) {
    //$module(ExportGenerators.mss)
    /* There are two types of trills in Sibelius: A line object and a
        symbol object. This method normalizes both of these.
    */
    trill = libmei.Trill();
    voicenum = bobj.VoiceNumber;
    bar = bobj.ParentBar;

    if (voicenum = 0)
    {
        warnings = Self._property:warnings;
        warnings.Push(utils.Format(_ObjectAssignedToAllVoicesWarning, bobj.Type));
        voicenum = 1;
    }

    obj = GetNoteObjectAtPosition(bobj);

    if (obj != null)
    {
        libmei.AddAttribute(trill, 'startid', '#' & obj._id);
    }

    trill = AddBarObjectInfoToElement(bobj, trill);

    return trill;
}  //$end


use default marker size to be forward compatible with new mapnik defaults

/* ---- PALETTE ---- */

@motorway: #F8D6E0; /* #90BFE0 */
@trunk: #FFFABB;
@primary: @trunk;
@secondary: @trunk;
@road: #bbb;
@track: @road;
@footway: #6B9;
@cycleway: #69B;

/* ---- ROAD COLORS ---- */

/*.highway.line { line-color: #f00; } /* debug */

.highway[TYPE='motorway'] {
  .line[zoom>=7]  {
    line-color:spin(darken(@motorway,36),-10);
    line-cap:round;
    line-join:round;
  }
  .fill[zoom>=10] {
    line-color:@motorway;
    line-cap:round;
    line-join:round;
  }
}

.highway[TYPE='motorway_link'] {
  .line[zoom>=7]  {
    line-color:spin(darken(@motorway,36),-10);
    line-cap:round;
    line-join:round;
  }
  .fill[zoom>=12] {
    line-color:@motorway;
    line-cap:round;
    line-join:round;
  }
}

.highway[TYPE='trunk'],
.highway[TYPE='trunk_link'] {
  .line[zoom>=7] {
    line-color:spin(darken(@trunk,36),-10);
    line-cap:round;
    line-join:round;
  }
  .fill[zoom>=11] {
    line-color:@trunk;
    line-cap:round;
    line-join:round;
  }
}

.highway[TYPE='primary'],
.highway[TYPE='primary_link'] {
  .line[zoom>=7] {
    line-color:spin(darken(@primary,36),-10);
    line-cap:round;
    line-join:round;
  }
  .fill[zoom>=12] {
    line-color:@primary;
    line-cap:round;
    line-join:round;
  }
}

.highway[TYPE='secondary'] {
  .line[zoom>=8] {
    line-color:spin(darken(@secondary,36),-10);
    line-cap:round;
    line-join:round;
  }
  .fill[zoom>=12] {
    line-color:@secondary;
    line-cap:round;
    line-join:round;
  }
}

.highway[TYPE='secondary_link'] {
  .line[zoom>=12] {
    line-color:spin(darken(@secondary,36),-10);
    line-cap:round;
    line-join:round;
  }
  .fill[zoom>=14] {
    line-color:@secondary;
    line-cap:round;
    line-join:round;
  }
}

.highway[TYPE='living_street'],
.highway[TYPE='residential'],
.highway[TYPE='road'],
.highway[TYPE='tertiary'],
.highway[TYPE='unclassified'] {
  .line[zoom>=10] {
    line-color:@road;
    line-cap:round;
    line-join:round;
  }
  .fill[zoom>=14] {
    line-color:#fff;
    line-cap:round;
    line-join:round;
  }
}

.highway[TYPE='service'] {
  .line[zoom>=13] {
    line-color:@road;
    line-cap:round;
    line-join:round;
  }
  .fill[zoom>=16] {
    line-color:#fff;
    line-cap:round;
    line-join:round;
  }
}

.highway[TYPE='track'] {
  .line[zoom>=13] {
    line-color:@track;
    line-cap:round;
    line-join:round;
  }
}

.highway[TYPE='footway'],
.highway[TYPE='path'],
.highway[TYPE='pedestrian'] {
  .line[zoom>=14] {
    line-color:@footway;
    line-cap:round;
    line-join:round;
  }
}

.highway[TYPE='cycleway'] {
  .line[zoom>=14] {
    line-color:@cycleway;
    line-cap:round;
    line-join:round;
  }
}

/* ---- ROAD WIDTHS ---- */

.highway[zoom=7] {
  .line[TYPE='motorway'] { line-width: 1.0; }
  .line[TYPE='trunk']    { line-width: 0.8; }
  .line[TYPE='primary']  { line-width: 0.6; }
}

.highway[zoom=8] {
  .line[TYPE='motorway'] { line-width: 1.0; }
  .line[TYPE='trunk']    { line-width: 0.8; }
  .line[TYPE='primary']  { line-width: 0.5; }
  .line[TYPE='secondary']{ line-width: 0.3; }
}

.highway[zoom=9] {
  .line[TYPE='motorway'] { line-width: 1.0; }
  .line[TYPE='trunk']    { line-width: 0.8; }
  .line[TYPE='primary']  { line-width: 0.6; }
  .line[TYPE='secondary']{ line-width: 0.4; }
}

.highway[zoom=10] {
  .line[TYPE='motorway'] { line-width: 0.8 + 1.6; }
  .fill[TYPE='motorway'] { line-width: 0.8; }

  .line[TYPE='trunk']    { line-width: 1.4; }

  .line[TYPE='primary']  { line-width: 1.2; }

  .line[TYPE='secondary']{ line-width: 0.8; }

  .line[TYPE='living_street'],
  .line[TYPE='residential'],
  .line[TYPE='road'],
  .line[TYPE='tertiary'],
  .line[TYPE='unclassified'] { line-width: 0.2; }
}

.highway[zoom=11] {
  .line[TYPE='motorway']      { line-width: 1.0 + 1.8; }
  .fill[TYPE='motorway']      { line-width: 1.0; }
  .line[TYPE='trunk']         { line-width: 0.8 + 1.6; }
  .fill[TYPE='trunk']         { line-width: 0.8; }
  .line[TYPE='primary']       { line-width: 1.4; }
  .line[TYPE='secondary']     { line-width: 1.0; }

  .line[TYPE='motorway_link'] { line-width: 0.6; }
  .line[TYPE='trunk_link']    { line-width: 0.5; }
  .line[TYPE='primary_link']  { line-width: 0.4; }

  .line[TYPE='living_street'],
  .line[TYPE='residential'],
  .line[TYPE='road'],
  .line[TYPE='tertiary'],
  .line[TYPE='unclassified'] { line-width: 0.4; }
}

.highway[zoom=12] {
  .line[TYPE='motorway']      { line-width: 1.2 + 2; }
  .fill[TYPE='motorway']      { line-width: 1.2; }
  .line[TYPE='trunk']         { line-width: 1.0 + 1.8; }
  .fill[TYPE='trunk']         { line-width: 1.0; }
  .line[TYPE='primary']       { line-width: 0.8 + 1.6; }
  .fill[TYPE='primary']       { line-width: 0.8; }
  .line[TYPE='secondary']     { line-width: 0.8 + 1.6; }
  .fill[TYPE='secondary']     { line-width: 0.8; }

  .line[TYPE='motorway_link'] { line-width: 1.0 + 1.8; }
  .fill[TYPE='motorway_link'] { line-width: 1.0; }
  .line[TYPE='trunk_link']    { line-width: 0.8 + 1.6; }
  .fill[TYPE='trunk_link']    { line-width: 0.8; }
  .line[TYPE='primary_link']  { line-width: 0.8 + 1.6; }
  .fill[TYPE='primary_link']  { line-width: 0.8; }
  .line[TYPE='secondary_link']  { line-width: 0.8; }

  .line[TYPE='living_street'],
  .line[TYPE='residential'],
  .line[TYPE='road'],
  .line[TYPE='tertiary'],
  .line[TYPE='unclassified']  { line-width: 0.6; }
}

.highway[zoom=13] {
  .line[TYPE='motorway']      { line-width: 2.0 + 2; }
  .fill[TYPE='motorway']      { line-width: 2.0; }
  .line[TYPE='trunk']         { line-width: 1.4 + 2; }
  .fill[TYPE='trunk']         { line-width: 1.4; }
  .line[TYPE='primary']       { line-width: 1.2 + 2; }
  .fill[TYPE='primary']       { line-width: 1.2; }
  .line[TYPE='primary_link'],
  .line[TYPE='secondary']     { line-width: 1.0 + 2; }
  .fill[TYPE='primary_link'],
  .fill[TYPE='secondary']     { line-width: 1.0; }

  .line[TYPE='motorway_link'] { line-width: 1.0 + 2; }
  .fill[TYPE='motorway_link'] { line-width: 1.0; }
  .line[TYPE='trunk_link']    { line-width: 1.0 + 2; }
  .fill[TYPE='trunk_link']    { line-width: 1.0; }
  .line[TYPE='primary_link']  { line-width: 1.0 + 2; }
  .fill[TYPE='primary_link']  { line-width: 1.0; }
  .line[TYPE='secondary_link']{ line-width: 0.8; }

  .line[TYPE='living_street'],
  .line[TYPE='residential'],
  .line[TYPE='road'],
  .line[TYPE='tertiary'],
  .line[TYPE='unclassified']  { line-width: 1.0; }
  .line[TYPE='service']       { line-width: 0.5; }

  .line[TYPE='track']         { line-width: 0.5; line-dasharray:2,3; }
}

.highway[zoom=14] {
  .line[TYPE='motorway']      { line-width: 4 + 2; }
  .fill[TYPE='motorway']      { line-width: 4; }
  .line[TYPE='trunk']         { line-width: 3 + 2; }
  .fill[TYPE='trunk']         { line-width: 3; }
  .line[TYPE='primary']       { line-width: 2 + 2; }
  .fill[TYPE='primary']       { line-width: 2; }
  .line[TYPE='secondary']     { line-width: 2 + 2; }
  .fill[TYPE='secondary']     { line-width: 2; }

  .line[TYPE='motorway_link'] { line-width: 1.4 + 2; }
  .fill[TYPE='motorway_link'] { line-width: 1.4; }
  .line[TYPE='trunk_link']    { line-width: 1.2 + 2; }
  .fill[TYPE='trunk_link']    { line-width: 1.2; }
  .line[TYPE='primary_link']  { line-width: 1.0 + 2; }
  .fill[TYPE='primary_link']  { line-width: 1.0; }
  .line[TYPE='secondary_link']{ line-width: 0.8 + 2; }
  .fill[TYPE='secondary_link']{ line-width: 0.8; }

  .line[TYPE='living_street'],
  .line[TYPE='residential'],
  .line[TYPE='road'],
  .line[TYPE='tertiary'],
  .line[TYPE='unclassified']  { line-width: 1.6 + 1.6; }
  .fill[TYPE='living_street'],
  .fill[TYPE='residential'],
  .fill[TYPE='road'],
  .fill[TYPE='tertiary'],
  .fill[TYPE='unclassified']  { line-width: 1.6; }
  .line[TYPE='service']       { line-width: 0.6; }

  .line[TYPE='track']         { line-width: 0.6; line-dasharray:2,3; }

  .line[TYPE='cycleway'],
  .line[TYPE='footway'],
  .line[TYPE='path'],
  .line[TYPE='pedestrian'] {
    line-dasharray:1,2;
    line-width:0.6;
  }
}

.highway[zoom=15] {
  .line[TYPE='motorway']      { line-width: 6 + 2; }
  .fill[TYPE='motorway']      { line-width: 6; }
  .line[TYPE='trunk']         { line-width: 5 + 2; }
  .fill[TYPE='trunk']         { line-width: 5; }
  .line[TYPE='primary']       { line-width: 4 + 2; }
  .fill[TYPE='primary']       { line-width: 4; }
  .line[TYPE='secondary']     { line-width: 4 + 2; }
  .fill[TYPE='secondary']     { line-width: 4; }

  .line[TYPE='motorway_link'] { line-width: 2 + 2; }
  .fill[TYPE='motorway_link'] { line-width: 2; }
  .line[TYPE='trunk_link']    { line-width: 1.6 + 2; }
  .fill[TYPE='trunk_link']    { line-width: 1.6; }
  .line[TYPE='primary_link']  { line-width: 1.4 + 2; }
  .fill[TYPE='primary_link']  { line-width: 1.4; }
  .line[TYPE='secondary_link']{ line-width: 1.0 + 2; }
  .fill[TYPE='secondary_link']{ line-width: 1.0; }

  .line[TYPE='living_street'],
  .line[TYPE='residential'],
  .line[TYPE='road'],
  .line[TYPE='tertiary'],
  .line[TYPE='unclassified']  { line-width: 4 + 2; }
  .fill[TYPE='living_street'],
  .fill[TYPE='residential'],
  .fill[TYPE='road'],
  .fill[TYPE='tertiary'],
  .fill[TYPE='unclassified']  { line-width: 4; }
  .line[TYPE='service']       { line-width: 1; }

  .line[TYPE='track']         { line-width: 1; line-dasharray:2,3; }

  .line[TYPE='cycleway'],
  .line[TYPE='footway'],
  .line[TYPE='path'],
  .line[TYPE='pedestrian'] {
    line-dasharray:1,2;
    line-width:0.8;
  }
}

.highway[zoom=16] {
  .line[TYPE='motorway']      { line-width: 9 + 3; }
  .fill[TYPE='motorway']      { line-width: 9; }
  .line[TYPE='trunk']         { line-width: 8 + 2.5; }
  .fill[TYPE='trunk']         { line-width: 8; }
  .line[TYPE='primary']       { line-width: 7 + 2; }
  .fill[TYPE='primary']       { line-width: 7; }
  .line[TYPE='secondary']     { line-width: 6 + 2; }
  .fill[TYPE='secondary']     { line-width: 6; }

  .line[TYPE='motorway_link'] { line-width: 3 + 2.5; }
  .fill[TYPE='motorway_link'] { line-width: 3; }
  .line[TYPE='trunk_link']    { line-width: 2 + 2; }
  .fill[TYPE='trunk_link']    { line-width: 2; }
  .line[TYPE='primary_link']  { line-width: 1.8 + 2; }
  .fill[TYPE='primary_link']  { line-width: 1.8; }
  .line[TYPE='secondary_link']{ line-width: 1.4 + 2; }
  .fill[TYPE='secondary_link']{ line-width: 1.4; }

  .line[TYPE='living_street'],
  .line[TYPE='residential'],
  .line[TYPE='road'],
  .line[TYPE='tertiary'],
  .line[TYPE='unclassified']  { line-width: 6 + 2; }
  .fill[TYPE='living_street'],
  .fill[TYPE='residential'],
  .fill[TYPE='road'],
  .fill[TYPE='tertiary'],
  .fill[TYPE='unclassified']  { line-width: 6; }
  .line[TYPE='service']       { line-width: 1.4 + 2; }
  .fill[TYPE='service']       { line-width: 1.4; }

  .line[TYPE='track']         { line-width: 1.2; line-dasharray:2,3; }

  .line[TYPE='cycleway'],
  .line[TYPE='footway'],
  .line[TYPE='path'],
  .line[TYPE='pedestrian'] {
    line-dasharray:1,2;
    line-width:1.0;
  }
}

.highway[zoom>=17] {
  .line[TYPE='motorway']      { line-width: 13 + 3; }
  .fill[TYPE='motorway']      { line-width: 13; }
  .line[TYPE='trunk']         { line-width: 10 + 2.5; }
  .fill[TYPE='trunk']         { line-width: 10; }
  .line[TYPE='primary']       { line-width: 9 + 2; }
  .fill[TYPE='primary']       { line-width: 9; }
  .line[TYPE='secondary']     { line-width: 8 + 2; }
  .fill[TYPE='secondary']     { line-width: 8; }

  .line[TYPE='motorway_link'] { line-width: 4 + 2.5; }
  .fill[TYPE='motorway_link'] { line-width: 4; }
  .line[TYPE='trunk_link']    { line-width: 3.5 + 2; }
  .fill[TYPE='trunk_link']    { line-width: 3.5; }
  .line[TYPE='primary_link']  { line-width: 3 + 2; }
  .fill[TYPE='primary_link']  { line-width: 3; }
  .line[TYPE='secondary_link']{ line-width: 2.5 + 2; }
  .fill[TYPE='secondary_link']{ line-width: 2.5; }

  .line[TYPE='living_street'],
  .line[TYPE='residential'],
  .line[TYPE='road'],
  .line[TYPE='tertiary'],
  .line[TYPE='unclassified']  { line-width: 8 + 2; }
  .fill[TYPE='living_street'],
  .fill[TYPE='residential'],
  .fill[TYPE='road'],
  .fill[TYPE='tertiary'],
  .fill[TYPE='unclassified']  { line-width: 8; }

  .line[TYPE='service']       { line-width: 2 + 2; }
  .fill[TYPE='service']       { line-width: 2; }

  .line[TYPE='track']         { line-width: 1.4; line-dasharray:2,3; }

  .line[TYPE='cycleway'],
  .line[TYPE='footway'],
  .line[TYPE='path'],
  .line[TYPE='pedestrian'] {
    line-dasharray:2,3;
    line-width:1.2;
  }
}

/* ---- ONE WAY ARROWS ---- */

.highway.fill::oneway_arrow[zoom>15][ONEWAY='yes'] {
  marker-type:arrow;
  marker-line-width:1;
  marker-line-opacity:0.5;
  marker-line-color:#fff;
  marker-spacing: 200;
  marker-fill:spin(darken(@motorway,50),-10);
  marker-opacity:0.8;
}

/* ---- PALETTE ---- */

@motorway: #F8D6E0; /* #90BFE0 */
@trunk: #FFFABB;
@primary: @trunk;
@secondary: @trunk;
@road: #bbb;
@track: @road;
@footway: #6B9;
@cycleway: #69B;

/* ---- ROAD COLORS ---- */

/*.highway.line { line-color: #f00; } /* debug */

.highway[TYPE='motorway'] {
  .line[zoom>=7]  {
    line-color:spin(darken(@motorway,36),-10);
    line-cap:round;
    line-join:round;
  }
  .fill[zoom>=10] {
    line-color:@motorway;
    line-cap:round;
    line-join:round;
  }
}

.highway[TYPE='motorway_link'] {
  .line[zoom>=7]  {
    line-color:spin(darken(@motorway,36),-10);
    line-cap:round;
    line-join:round;
  }
  .fill[zoom>=12] {
    line-color:@motorway;
    line-cap:round;
    line-join:round;
  }
}

.highway[TYPE='trunk'],
.highway[TYPE='trunk_link'] {
  .line[zoom>=7] {
    line-color:spin(darken(@trunk,36),-10);
    line-cap:round;
    line-join:round;
  }
  .fill[zoom>=11] {
    line-color:@trunk;
    line-cap:round;
    line-join:round;
  }
}

.highway[TYPE='primary'],
.highway[TYPE='primary_link'] {
  .line[zoom>=7] {
    line-color:spin(darken(@primary,36),-10);
    line-cap:round;
    line-join:round;
  }
  .fill[zoom>=12] {
    line-color:@primary;
    line-cap:round;
    line-join:round;
  }
}

.highway[TYPE='secondary'] {
  .line[zoom>=8] {
    line-color:spin(darken(@secondary,36),-10);
    line-cap:round;
    line-join:round;
  }
  .fill[zoom>=12] {
    line-color:@secondary;
    line-cap:round;
    line-join:round;
  }
}

.highway[TYPE='secondary_link'] {
  .line[zoom>=12] {
    line-color:spin(darken(@secondary,36),-10);
    line-cap:round;
    line-join:round;
  }
  .fill[zoom>=14] {
    line-color:@secondary;
    line-cap:round;
    line-join:round;
  }
}

.highway[TYPE='living_street'],
.highway[TYPE='residential'],
.highway[TYPE='road'],
.highway[TYPE='tertiary'],
.highway[TYPE='unclassified'] {
  .line[zoom>=10] {
    line-color:@road;
    line-cap:round;
    line-join:round;
  }
  .fill[zoom>=14] {
    line-color:#fff;
    line-cap:round;
    line-join:round;
  }
}

.highway[TYPE='service'] {
  .line[zoom>=13] {
    line-color:@road;
    line-cap:round;
    line-join:round;
  }
  .fill[zoom>=16] {
    line-color:#fff;
    line-cap:round;
    line-join:round;
  }
}

.highway[TYPE='track'] {
  .line[zoom>=13] {
    line-color:@track;
    line-cap:round;
    line-join:round;
  }
}

.highway[TYPE='footway'],
.highway[TYPE='path'],
.highway[TYPE='pedestrian'] {
  .line[zoom>=14] {
    line-color:@footway;
    line-cap:round;
    line-join:round;
  }
}

.highway[TYPE='cycleway'] {
  .line[zoom>=14] {
    line-color:@cycleway;
    line-cap:round;
    line-join:round;
  }
}

/* ---- ROAD WIDTHS ---- */

.highway[zoom=7] {
  .line[TYPE='motorway'] { line-width: 1.0; }
  .line[TYPE='trunk']    { line-width: 0.8; }
  .line[TYPE='primary']  { line-width: 0.6; }
}

.highway[zoom=8] {
  .line[TYPE='motorway'] { line-width: 1.0; }
  .line[TYPE='trunk']    { line-width: 0.8; }
  .line[TYPE='primary']  { line-width: 0.5; }
  .line[TYPE='secondary']{ line-width: 0.3; }
}

.highway[zoom=9] {
  .line[TYPE='motorway'] { line-width: 1.0; }
  .line[TYPE='trunk']    { line-width: 0.8; }
  .line[TYPE='primary']  { line-width: 0.6; }
  .line[TYPE='secondary']{ line-width: 0.4; }
}

.highway[zoom=10] {
  .line[TYPE='motorway'] { line-width: 0.8 + 1.6; }
  .fill[TYPE='motorway'] { line-width: 0.8; }

  .line[TYPE='trunk']    { line-width: 1.4; }

  .line[TYPE='primary']  { line-width: 1.2; }

  .line[TYPE='secondary']{ line-width: 0.8; }

  .line[TYPE='living_street'],
  .line[TYPE='residential'],
  .line[TYPE='road'],
  .line[TYPE='tertiary'],
  .line[TYPE='unclassified'] { line-width: 0.2; }
}

.highway[zoom=11] {
  .line[TYPE='motorway']      { line-width: 1.0 + 1.8; }
  .fill[TYPE='motorway']      { line-width: 1.0; }
  .line[TYPE='trunk']         { line-width: 0.8 + 1.6; }
  .fill[TYPE='trunk']         { line-width: 0.8; }
  .line[TYPE='primary']       { line-width: 1.4; }
  .line[TYPE='secondary']     { line-width: 1.0; }

  .line[TYPE='motorway_link'] { line-width: 0.6; }
  .line[TYPE='trunk_link']    { line-width: 0.5; }
  .line[TYPE='primary_link']  { line-width: 0.4; }

  .line[TYPE='living_street'],
  .line[TYPE='residential'],
  .line[TYPE='road'],
  .line[TYPE='tertiary'],
  .line[TYPE='unclassified'] { line-width: 0.4; }
}

.highway[zoom=12] {
  .line[TYPE='motorway']      { line-width: 1.2 + 2; }
  .fill[TYPE='motorway']      { line-width: 1.2; }
  .line[TYPE='trunk']         { line-width: 1.0 + 1.8; }
  .fill[TYPE='trunk']         { line-width: 1.0; }
  .line[TYPE='primary']       { line-width: 0.8 + 1.6; }
  .fill[TYPE='primary']       { line-width: 0.8; }
  .line[TYPE='secondary']     { line-width: 0.8 + 1.6; }
  .fill[TYPE='secondary']     { line-width: 0.8; }

  .line[TYPE='motorway_link'] { line-width: 1.0 + 1.8; }
  .fill[TYPE='motorway_link'] { line-width: 1.0; }
  .line[TYPE='trunk_link']    { line-width: 0.8 + 1.6; }
  .fill[TYPE='trunk_link']    { line-width: 0.8; }
  .line[TYPE='primary_link']  { line-width: 0.8 + 1.6; }
  .fill[TYPE='primary_link']  { line-width: 0.8; }
  .line[TYPE='secondary_link']  { line-width: 0.8; }

  .line[TYPE='living_street'],
  .line[TYPE='residential'],
  .line[TYPE='road'],
  .line[TYPE='tertiary'],
  .line[TYPE='unclassified']  { line-width: 0.6; }
}

.highway[zoom=13] {
  .line[TYPE='motorway']      { line-width: 2.0 + 2; }
  .fill[TYPE='motorway']      { line-width: 2.0; }
  .line[TYPE='trunk']         { line-width: 1.4 + 2; }
  .fill[TYPE='trunk']         { line-width: 1.4; }
  .line[TYPE='primary']       { line-width: 1.2 + 2; }
  .fill[TYPE='primary']       { line-width: 1.2; }
  .line[TYPE='primary_link'],
  .line[TYPE='secondary']     { line-width: 1.0 + 2; }
  .fill[TYPE='primary_link'],
  .fill[TYPE='secondary']     { line-width: 1.0; }

  .line[TYPE='motorway_link'] { line-width: 1.0 + 2; }
  .fill[TYPE='motorway_link'] { line-width: 1.0; }
  .line[TYPE='trunk_link']    { line-width: 1.0 + 2; }
  .fill[TYPE='trunk_link']    { line-width: 1.0; }
  .line[TYPE='primary_link']  { line-width: 1.0 + 2; }
  .fill[TYPE='primary_link']  { line-width: 1.0; }
  .line[TYPE='secondary_link']{ line-width: 0.8; }

  .line[TYPE='living_street'],
  .line[TYPE='residential'],
  .line[TYPE='road'],
  .line[TYPE='tertiary'],
  .line[TYPE='unclassified']  { line-width: 1.0; }
  .line[TYPE='service']       { line-width: 0.5; }

  .line[TYPE='track']         { line-width: 0.5; line-dasharray:2,3; }
}

.highway[zoom=14] {
  .line[TYPE='motorway']      { line-width: 4 + 2; }
  .fill[TYPE='motorway']      { line-width: 4; }
  .line[TYPE='trunk']         { line-width: 3 + 2; }
  .fill[TYPE='trunk']         { line-width: 3; }
  .line[TYPE='primary']       { line-width: 2 + 2; }
  .fill[TYPE='primary']       { line-width: 2; }
  .line[TYPE='secondary']     { line-width: 2 + 2; }
  .fill[TYPE='secondary']     { line-width: 2; }

  .line[TYPE='motorway_link'] { line-width: 1.4 + 2; }
  .fill[TYPE='motorway_link'] { line-width: 1.4; }
  .line[TYPE='trunk_link']    { line-width: 1.2 + 2; }
  .fill[TYPE='trunk_link']    { line-width: 1.2; }
  .line[TYPE='primary_link']  { line-width: 1.0 + 2; }
  .fill[TYPE='primary_link']  { line-width: 1.0; }
  .line[TYPE='secondary_link']{ line-width: 0.8 + 2; }
  .fill[TYPE='secondary_link']{ line-width: 0.8; }

  .line[TYPE='living_street'],
  .line[TYPE='residential'],
  .line[TYPE='road'],
  .line[TYPE='tertiary'],
  .line[TYPE='unclassified']  { line-width: 1.6 + 1.6; }
  .fill[TYPE='living_street'],
  .fill[TYPE='residential'],
  .fill[TYPE='road'],
  .fill[TYPE='tertiary'],
  .fill[TYPE='unclassified']  { line-width: 1.6; }
  .line[TYPE='service']       { line-width: 0.6; }

  .line[TYPE='track']         { line-width: 0.6; line-dasharray:2,3; }

  .line[TYPE='cycleway'],
  .line[TYPE='footway'],
  .line[TYPE='path'],
  .line[TYPE='pedestrian'] {
    line-dasharray:1,2;
    line-width:0.6;
  }
}

.highway[zoom=15] {
  .line[TYPE='motorway']      { line-width: 6 + 2; }
  .fill[TYPE='motorway']      { line-width: 6; }
  .line[TYPE='trunk']         { line-width: 5 + 2; }
  .fill[TYPE='trunk']         { line-width: 5; }
  .line[TYPE='primary']       { line-width: 4 + 2; }
  .fill[TYPE='primary']       { line-width: 4; }
  .line[TYPE='secondary']     { line-width: 4 + 2; }
  .fill[TYPE='secondary']     { line-width: 4; }

  .line[TYPE='motorway_link'] { line-width: 2 + 2; }
  .fill[TYPE='motorway_link'] { line-width: 2; }
  .line[TYPE='trunk_link']    { line-width: 1.6 + 2; }
  .fill[TYPE='trunk_link']    { line-width: 1.6; }
  .line[TYPE='primary_link']  { line-width: 1.4 + 2; }
  .fill[TYPE='primary_link']  { line-width: 1.4; }
  .line[TYPE='secondary_link']{ line-width: 1.0 + 2; }
  .fill[TYPE='secondary_link']{ line-width: 1.0; }

  .line[TYPE='living_street'],
  .line[TYPE='residential'],
  .line[TYPE='road'],
  .line[TYPE='tertiary'],
  .line[TYPE='unclassified']  { line-width: 4 + 2; }
  .fill[TYPE='living_street'],
  .fill[TYPE='residential'],
  .fill[TYPE='road'],
  .fill[TYPE='tertiary'],
  .fill[TYPE='unclassified']  { line-width: 4; }
  .line[TYPE='service']       { line-width: 1; }

  .line[TYPE='track']         { line-width: 1; line-dasharray:2,3; }

  .line[TYPE='cycleway'],
  .line[TYPE='footway'],
  .line[TYPE='path'],
  .line[TYPE='pedestrian'] {
    line-dasharray:1,2;
    line-width:0.8;
  }
}

.highway[zoom=16] {
  .line[TYPE='motorway']      { line-width: 9 + 3; }
  .fill[TYPE='motorway']      { line-width: 9; }
  .line[TYPE='trunk']         { line-width: 8 + 2.5; }
  .fill[TYPE='trunk']         { line-width: 8; }
  .line[TYPE='primary']       { line-width: 7 + 2; }
  .fill[TYPE='primary']       { line-width: 7; }
  .line[TYPE='secondary']     { line-width: 6 + 2; }
  .fill[TYPE='secondary']     { line-width: 6; }

  .line[TYPE='motorway_link'] { line-width: 3 + 2.5; }
  .fill[TYPE='motorway_link'] { line-width: 3; }
  .line[TYPE='trunk_link']    { line-width: 2 + 2; }
  .fill[TYPE='trunk_link']    { line-width: 2; }
  .line[TYPE='primary_link']  { line-width: 1.8 + 2; }
  .fill[TYPE='primary_link']  { line-width: 1.8; }
  .line[TYPE='secondary_link']{ line-width: 1.4 + 2; }
  .fill[TYPE='secondary_link']{ line-width: 1.4; }

  .line[TYPE='living_street'],
  .line[TYPE='residential'],
  .line[TYPE='road'],
  .line[TYPE='tertiary'],
  .line[TYPE='unclassified']  { line-width: 6 + 2; }
  .fill[TYPE='living_street'],
  .fill[TYPE='residential'],
  .fill[TYPE='road'],
  .fill[TYPE='tertiary'],
  .fill[TYPE='unclassified']  { line-width: 6; }
  .line[TYPE='service']       { line-width: 1.4 + 2; }
  .fill[TYPE='service']       { line-width: 1.4; }

  .line[TYPE='track']         { line-width: 1.2; line-dasharray:2,3; }

  .line[TYPE='cycleway'],
  .line[TYPE='footway'],
  .line[TYPE='path'],
  .line[TYPE='pedestrian'] {
    line-dasharray:1,2;
    line-width:1.0;
  }
}

.highway[zoom>=17] {
  .line[TYPE='motorway']      { line-width: 13 + 3; }
  .fill[TYPE='motorway']      { line-width: 13; }
  .line[TYPE='trunk']         { line-width: 10 + 2.5; }
  .fill[TYPE='trunk']         { line-width: 10; }
  .line[TYPE='primary']       { line-width: 9 + 2; }
  .fill[TYPE='primary']       { line-width: 9; }
  .line[TYPE='secondary']     { line-width: 8 + 2; }
  .fill[TYPE='secondary']     { line-width: 8; }

  .line[TYPE='motorway_link'] { line-width: 4 + 2.5; }
  .fill[TYPE='motorway_link'] { line-width: 4; }
  .line[TYPE='trunk_link']    { line-width: 3.5 + 2; }
  .fill[TYPE='trunk_link']    { line-width: 3.5; }
  .line[TYPE='primary_link']  { line-width: 3 + 2; }
  .fill[TYPE='primary_link']  { line-width: 3; }
  .line[TYPE='secondary_link']{ line-width: 2.5 + 2; }
  .fill[TYPE='secondary_link']{ line-width: 2.5; }

  .line[TYPE='living_street'],
  .line[TYPE='residential'],
  .line[TYPE='road'],
  .line[TYPE='tertiary'],
  .line[TYPE='unclassified']  { line-width: 8 + 2; }
  .fill[TYPE='living_street'],
  .fill[TYPE='residential'],
  .fill[TYPE='road'],
  .fill[TYPE='tertiary'],
  .fill[TYPE='unclassified']  { line-width: 8; }

  .line[TYPE='service']       { line-width: 2 + 2; }
  .fill[TYPE='service']       { line-width: 2; }

  .line[TYPE='track']         { line-width: 1.4; line-dasharray:2,3; }

  .line[TYPE='cycleway'],
  .line[TYPE='footway'],
  .line[TYPE='path'],
  .line[TYPE='pedestrian'] {
    line-dasharray:2,3;
    line-width:1.2;
  }
}

/* ---- ONE WAY ARROWS ---- */

.highway.fill::oneway_arrow[zoom>15][ONEWAY='yes'] {
  marker-type:arrow;
  marker-width:2;
  marker-line-width:1;
  marker-line-opacity:0.5;
  marker-line-color:#fff;
  marker-spacing: 200;
  marker-fill:spin(darken(@motorway,50),-10);
  marker-opacity:0.8;
}


fix s/cat/category/g false positive

fix s/cat/category/g false positive

/*
 * Author: Andrew Harvey <andrew.harvey4@gmail.com>
 * License: CC0 http://creativecommons.org/publicdomain/zero/1.0/
 * To the extent possible under law, the person who associated CC0
 * with this work has waived all copyright and related or neighboring
 * rights to this work.
 */

// white background (use #DBEEEE for blue ocean, or drop line for transparent)
Map { background-color: #FFFFFF; }

#mb {
  /* outline */
  line-color: #000000;
  line-width: 0.75;

  /* fill */
  [category='Residential'] {
    polygon-fill: #DDDDDD;
  }
  [category='Parkland'] {
    polygon-fill: #CEF7CA;
  }
  [category='Agricultural'] {
    polygon-fill: #EAD8BD;
  }
  [category='Commercial'] {
    polygon-fill: #F3D8D2;
  }
  [category='Education'] {
    polygon-fill: #F0F0D8;
  }
  [category='Industrial'] {
    polygon-fill: #DFD1D6;
  }
  [category='Water'] {
    polygon-fill: #B5D0D0;
  }
  [category='Transport'] {
    polygon-fill: #B6B591;
  }
  [category='Hospital/Medical'] {
    polygon-fill: #EBA4A4;
  }
  [category='Other'] {
    polygon-fill: #000000;
  }
}

/*
 * Author: Andrew Harvey <andrew.harvey4@gmail.com>
 * License: CC0 http://creativecommons.org/publicdomain/zero/1.0/
 * To the extent possible under law, the person who associated CC0
 * with this work has waived all copyright and related or neighboring
 * rights to this work.
 */

// white background (use #DBEEEE for blue ocean, or drop line for transparent)
Map { background-color: #FFFFFF; }

#mb {
  /* outline */
  line-color: #000000;
  line-width: 0.75;

  /* fill */
  [category='Residential'] {
    polygon-fill: #DDDDDD;
  }
  [category='Parkland'] {
    polygon-fill: #CEF7CA;
  }
  [category='Agricultural'] {
    polygon-fill: #EAD8BD;
  }
  [category='Commercial'] {
    polygon-fill: #F3D8D2;
  }
  [category='Educategoryion'] {
    polygon-fill: #F0F0D8;
  }
  [category='Industrial'] {
    polygon-fill: #DFD1D6;
  }
  [category='Water'] {
    polygon-fill: #B5D0D0;
  }
  [category='Transport'] {
    polygon-fill: #B6B591;
  }
  [category='Hospital/Medical'] {
    polygon-fill: #EBA4A4;
  }
  [category='Other'] {
    polygon-fill: #000000;
  }
}