LCOV - code coverage report
Current view: top level - src/engraving/dom - volta.cpp Coverage Total Hit
Test: filtered_coverage.info Lines: 84.4 % 167 141
Test Date: 2026-11-19 04:14:36

            Line data    Source code
       1              : /*
       2              :  * SPDX-License-Identifier: GPL-3.0-only
       3              :  * MuseScore-Studio-CLA-applies
       4              :  *
       5              :  * MuseScore Studio
       6              :  * Music Composition & Notation
       7              :  *
       8              :  * Copyright (C) 2021 MuseScore Limited and others
       9              :  *
      10              :  * This program is free software: you can redistribute it and/or modify
      11              :  * it under the terms of the GNU General Public License version 3 as
      12              :  * published by the Free Software Foundation.
      13              :  *
      14              :  * This program is distributed in the hope that it will be useful,
      15              :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      16              :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      17              :  * GNU General Public License for more details.
      18              :  *
      19              :  * You should have received a copy of the GNU General Public License
      20              :  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
      21              :  */
      22              : 
      23              : #include "volta.h"
      24              : 
      25              : #include <algorithm>
      26              : #include <vector>
      27              : 
      28              : #include "barline.h"
      29              : #include "dummyparent.h"
      30              : #include "keysig.h"
      31              : #include "measure.h"
      32              : #include "score.h"
      33              : #include "staff.h"
      34              : #include "system.h"
      35              : #include "text.h"
      36              : 
      37              : #include "log.h"
      38              : 
      39              : using namespace mu;
      40              : using namespace mu::engraving;
      41              : 
      42              : namespace mu::engraving {
      43              : static const ElementStyle voltaStyle {
      44              :     { Sid::voltaFontFace,                      Pid::BEGIN_FONT_FACE },
      45              :     { Sid::voltaFontFace,                      Pid::CONTINUE_FONT_FACE },
      46              :     { Sid::voltaFontFace,                      Pid::END_FONT_FACE },
      47              :     { Sid::voltaFontSize,                      Pid::BEGIN_FONT_SIZE },
      48              :     { Sid::voltaFontSize,                      Pid::CONTINUE_FONT_SIZE },
      49              :     { Sid::voltaFontSize,                      Pid::END_FONT_SIZE },
      50              :     { Sid::voltaFontStyle,                     Pid::BEGIN_FONT_STYLE },
      51              :     { Sid::voltaFontStyle,                     Pid::CONTINUE_FONT_STYLE },
      52              :     { Sid::voltaFontStyle,                     Pid::END_FONT_STYLE },
      53              :     { Sid::voltaAlign,                         Pid::BEGIN_TEXT_ALIGN },
      54              :     { Sid::voltaAlign,                         Pid::CONTINUE_TEXT_ALIGN },
      55              :     { Sid::voltaAlign,                         Pid::END_TEXT_ALIGN },
      56              :     { Sid::voltaPosition,                      Pid::BEGIN_TEXT_POSITION },
      57              :     { Sid::voltaPosition,                      Pid::CONTINUE_TEXT_POSITION },
      58              :     { Sid::voltaPosition,                      Pid::END_TEXT_POSITION },
      59              :     { Sid::voltaOffset,                        Pid::BEGIN_TEXT_OFFSET },
      60              :     { Sid::voltaOffset,                        Pid::CONTINUE_TEXT_OFFSET },
      61              :     { Sid::voltaOffset,                        Pid::END_TEXT_OFFSET },
      62              :     { Sid::voltaMusicalSymbolSize,             Pid::BEGIN_TEXT_MUSIC_SYMBOLS_SIZE },
      63              :     { Sid::voltaMusicalSymbolSize,             Pid::CONTINUE_TEXT_MUSIC_SYMBOLS_SIZE },
      64              :     { Sid::voltaMusicalSymbolSize,             Pid::END_TEXT_MUSIC_SYMBOLS_SIZE },
      65              :     { Sid::dummyMusicalSymbolsScale,           Pid::BEGIN_TEXT_MUSICAL_SYMBOLS_SCALE },
      66              :     { Sid::dummyMusicalSymbolsScale,           Pid::CONTINUE_TEXT_MUSICAL_SYMBOLS_SCALE },
      67              :     { Sid::dummyMusicalSymbolsScale,           Pid::END_TEXT_MUSICAL_SYMBOLS_SCALE },
      68              :     { Sid::voltaLineWidth,                     Pid::LINE_WIDTH },
      69              :     { Sid::voltaLineStyle,                     Pid::LINE_STYLE },
      70              :     { Sid::voltaDashLineLen,                   Pid::DASH_LINE_LEN },
      71              :     { Sid::voltaDashGapLen,                    Pid::DASH_GAP_LEN },
      72              :     { Sid::voltaHook,                          Pid::BEGIN_HOOK_HEIGHT },
      73              :     { Sid::voltaHook,                          Pid::END_HOOK_HEIGHT },
      74              :     { Sid::voltaFontSpatiumDependent,          Pid::TEXT_SIZE_SPATIUM_DEPENDENT },
      75              : };
      76              : 
      77              : //---------------------------------------------------------
      78              : //   VoltaSegment
      79              : //---------------------------------------------------------
      80              : 
      81          217 : VoltaSegment::VoltaSegment(Volta* sp)
      82          217 :     : TextLineBaseSegment(ElementType::VOLTA_SEGMENT, sp, ElementFlag::MOVABLE | ElementFlag::ON_STAFF | ElementFlag::SYSTEM)
      83              : {
      84          217 :     m_text->setTextStyleType(propertyDefault(Pid::TEXT_STYLE).value<TextStyleType>());
      85          217 :     m_endText->setTextStyleType(propertyDefault(Pid::TEXT_STYLE).value<TextStyleType>());
      86          217 : }
      87              : 
      88              : //---------------------------------------------------------
      89              : //   propertyDelegate
      90              : //---------------------------------------------------------
      91              : 
      92         1537 : PropertyDelegate VoltaSegment::resolvePropertyDelegate(Pid pid) const
      93              : {
      94         1537 :     if (pid == Pid::BEGIN_HOOK_TYPE || pid == Pid::END_HOOK_TYPE || pid == Pid::VOLTA_ENDING) {
      95            0 :         return PropertyDelegate(spanner());
      96              :     }
      97         1537 :     return TextLineBaseSegment::resolvePropertyDelegate(pid);
      98              : }
      99              : 
     100              : //---------------------------------------------------------
     101              : //   Volta
     102              : //---------------------------------------------------------
     103              : 
     104          213 : Volta::Volta(DummyParent* parent)
     105          213 :     : TextLineBase(ElementType::VOLTA, parent, ElementFlag::SYSTEM)
     106              : {
     107          213 :     setPlacement(PlacementV::ABOVE);
     108          213 :     initElementStyle(&voltaStyle);
     109              : 
     110          213 :     setBeginTextPlace(TextPlace::BELOW);
     111          213 :     setContinueTextPlace(TextPlace::BELOW);
     112          213 :     setLineVisible(true);
     113          213 :     resetProperty(Pid::BEGIN_TEXT);
     114          213 :     resetProperty(Pid::CONTINUE_TEXT);
     115          213 :     resetProperty(Pid::END_TEXT);
     116          213 :     resetProperty(Pid::BEGIN_TEXT_PLACE);
     117          213 :     resetProperty(Pid::CONTINUE_TEXT_PLACE);
     118          213 :     resetProperty(Pid::END_TEXT_PLACE);
     119          213 :     resetProperty(Pid::BEGIN_HOOK_TYPE);
     120          213 :     resetProperty(Pid::END_HOOK_TYPE);
     121          213 : }
     122              : 
     123              : ///
     124              : /// \brief sorts the provided list in ascending order
     125              : ///
     126          185 : void Volta::setEndings(const std::vector<int>& l)
     127              : {
     128          185 :     m_endings = l;
     129          185 :     std::sort(m_endings.begin(), m_endings.end());
     130          185 : }
     131              : 
     132              : //---------------------------------------------------------
     133              : //   setText
     134              : //---------------------------------------------------------
     135              : 
     136           57 : void Volta::setText(const String& s)
     137              : {
     138           57 :     setBeginText(s);
     139           57 : }
     140              : 
     141              : //---------------------------------------------------------
     142              : //   text
     143              : //---------------------------------------------------------
     144              : 
     145           29 : String Volta::text() const
     146              : {
     147           29 :     return beginText();
     148              : }
     149              : 
     150              : //---------------------------------------------------------
     151              : //   createLineSegment
     152              : //---------------------------------------------------------
     153              : 
     154              : static const ElementStyle voltaSegmentStyle {
     155              :     { Sid::voltaMinDistance,                   Pid::MIN_DISTANCE },
     156              : };
     157              : 
     158          217 : LineSegment* Volta::createLineSegment()
     159              : {
     160          217 :     VoltaSegment* vs = new VoltaSegment(this);
     161          217 :     vs->setTrack(track());
     162          217 :     vs->initElementStyle(&voltaSegmentStyle);
     163          217 :     return vs;
     164              : }
     165              : 
     166              : //---------------------------------------------------------
     167              : //   hasEnding
     168              : //---------------------------------------------------------
     169              : 
     170          576 : bool Volta::hasEnding(int repeat) const
     171              : {
     172          955 :     for (int ending : endings()) {
     173          715 :         if (ending == repeat) {
     174          336 :             return true;
     175              :         }
     176          576 :     }
     177          240 :     return false;
     178              : }
     179              : 
     180              : //---------------------------------------------------------
     181              : //   firstEnding
     182              : //---------------------------------------------------------
     183              : 
     184            1 : int Volta::firstEnding() const
     185              : {
     186            1 :     if (m_endings.empty()) {
     187            0 :         return 0;
     188              :     }
     189            1 :     return m_endings.front();
     190              : }
     191              : 
     192              : //---------------------------------------------------------
     193              : //   lastEnding
     194              : //---------------------------------------------------------
     195              : 
     196           15 : int Volta::lastEnding() const
     197              : {
     198           15 :     if (m_endings.empty()) {
     199            0 :         return 0;
     200              :     }
     201           15 :     return m_endings.back();
     202              : }
     203              : 
     204              : //---------------------------------------------------------
     205              : //   getProperty
     206              : //---------------------------------------------------------
     207              : 
     208         2062 : PropertyValue Volta::getProperty(Pid propertyId) const
     209              : {
     210         2062 :     switch (propertyId) {
     211            0 :     case Pid::VOLTA_ENDING:
     212            0 :         return PropertyValue::fromValue(endings());
     213         2062 :     default:
     214         2062 :         break;
     215              :     }
     216         2062 :     return TextLineBase::getProperty(propertyId);
     217              : }
     218              : 
     219              : //---------------------------------------------------------
     220              : //   setProperty
     221              : //---------------------------------------------------------
     222              : 
     223         8592 : bool Volta::setProperty(Pid propertyId, const PropertyValue& val)
     224              : {
     225         8592 :     switch (propertyId) {
     226            0 :     case Pid::VOLTA_ENDING: {
     227            0 :         setEndings(val.value<std::vector<int> >());
     228            0 :     } break;
     229         8592 :     default:
     230         8592 :         if (!TextLineBase::setProperty(propertyId, val)) {
     231            0 :             return false;
     232              :         }
     233         8592 :         break;
     234              :     }
     235         8592 :     triggerLayout();
     236         8592 :     return true;
     237              : }
     238              : 
     239              : //---------------------------------------------------------
     240              : //   propertyDefault
     241              : //---------------------------------------------------------
     242              : 
     243         4013 : PropertyValue Volta::propertyDefault(Pid propertyId) const
     244              : {
     245         4013 :     switch (propertyId) {
     246            0 :     case Pid::VOLTA_ENDING:
     247            0 :         return PropertyValue::fromValue(std::vector<int>());
     248          268 :     case Pid::BEGIN_HOOK_TYPE:
     249          268 :         return HookType::HOOK_90;
     250          268 :     case Pid::END_HOOK_TYPE:
     251          268 :         return HookType::NONE;
     252          804 :     case Pid::BEGIN_TEXT:
     253              :     case Pid::CONTINUE_TEXT:
     254              :     case Pid::END_TEXT:
     255          804 :         return "";
     256           55 :     case Pid::LINE_VISIBLE:
     257           55 :         return true;
     258          804 :     case Pid::BEGIN_TEXT_PLACE:
     259              :     case Pid::CONTINUE_TEXT_PLACE:
     260              :     case Pid::END_TEXT_PLACE:
     261          804 :         return TextPlace::ABOVE;
     262              : 
     263           55 :     case Pid::PLACEMENT:
     264           55 :         return PlacementV::ABOVE;
     265              : 
     266          434 :     case Pid::TEXT_STYLE:
     267          434 :         return TextStyleType::VOLTA;
     268              : 
     269          440 :     case Pid::BEGIN_FILLED_ARROW_HEIGHT:   // No arrow endings for voltas
     270              :     case Pid::BEGIN_FILLED_ARROW_WIDTH:
     271              :     case Pid::END_FILLED_ARROW_HEIGHT:
     272              :     case Pid::END_FILLED_ARROW_WIDTH:
     273              :     case Pid::BEGIN_LINE_ARROW_HEIGHT:
     274              :     case Pid::BEGIN_LINE_ARROW_WIDTH:
     275              :     case Pid::END_LINE_ARROW_HEIGHT:
     276              :     case Pid::END_LINE_ARROW_WIDTH:
     277          440 :         return 0.0;
     278              : 
     279          885 :     default:
     280          885 :         return TextLineBase::propertyDefault(propertyId);
     281              :     }
     282              : }
     283              : 
     284              : //---------------------------------------------------------
     285              : //   setChannel
     286              : //---------------------------------------------------------
     287              : 
     288          162 : void Volta::setChannel() const
     289              : {
     290          162 :     Measure* startMeasure = Spanner::startMeasure();
     291          162 :     Measure* endMeasure = Spanner::endMeasure();
     292              : 
     293          162 :     if (startMeasure && endMeasure) {
     294          162 :         if (!endMeasure->repeatEnd()) {
     295           80 :             return;
     296              :         }
     297              : 
     298           82 :         Fraction startTick = startMeasure->tick() - Fraction::eps();
     299           82 :         Fraction endTick  = endMeasure->endTick() - Fraction::eps();
     300           82 :         Staff* st = staff();
     301          410 :         for (voice_idx_t voice = 0; voice < VOICES; ++voice) {
     302          328 :             int channel = st->channel(startTick, voice);
     303          328 :             st->insertIntoChannelList(voice, endTick, channel);
     304              :         }
     305              :     }
     306              : }
     307              : 
     308              : //---------------------------------------------------------
     309              : //   accessibleInfo
     310              : //---------------------------------------------------------
     311              : 
     312            0 : String Volta::accessibleInfo() const
     313              : {
     314            0 :     return String(u"%1: %2").arg(EngravingItem::accessibleInfo(), text());
     315              : }
     316              : 
     317          740 : PointF Volta::linePos(Grip grip, System** system) const
     318              : {
     319          740 :     bool start = grip == Grip::START;
     320              : 
     321          740 :     Segment* segment = score()->tick2leftSegment(start ? tick() : tick2(), true,
     322              :                                                  SegmentType::ChordRest | SegmentType::StartRepeatBarLine | SegmentType::EndBarLine);
     323          740 :     if (!segment) {
     324            0 :         return PointF();
     325              :     }
     326              : 
     327          740 :     const Measure* measure = segment->measure();
     328          740 :     bool isAtSystemStart = segment->rtick().isZero() && measure && measure->system() && measure->isFirstInSystem();
     329          740 :     bool searchForPrevBarline = start ? segment->rtick().isZero() && (measure->repeatStart() || !isAtSystemStart) : true;
     330              : 
     331          740 :     SegmentType barlineType = start ? (SegmentType::StartRepeatBarLine | SegmentType::EndBarLine) : SegmentType::EndBarLine;
     332              : 
     333          740 :     if (searchForPrevBarline) {
     334          711 :         Segment* prev = segment;
     335         1388 :         while (prev && !prev->isType(barlineType) && prev->tick() == segment->tick()) {
     336          677 :             prev = prev->prev1MMenabled();
     337              :         }
     338              : 
     339          711 :         if (prev && prev->isType(barlineType)) {
     340          704 :             segment = prev;
     341              :         }
     342              :     }
     343              : 
     344          740 :     if (start && !segment->isType(SegmentType::BarLineTypes) && style().styleB(Sid::voltaAlignStartBeforeKeySig)) {
     345            0 :         Segment* prev = segment;
     346            0 :         while (prev && !prev->isType(SegmentType::KeySig) && prev->tick() == segment->tick()) {
     347            0 :             prev = prev->prev1MMenabled();
     348              :         }
     349            0 :         if (prev && prev->isType(SegmentType::KeySig)) {
     350            0 :             segment = prev;
     351              :         }
     352              :     }
     353              : 
     354          740 :     *system = segment->measure()->system();
     355          740 :     double x = segment->x() + segment->measure()->x();
     356              : 
     357          740 :     if (start) {
     358          370 :         bool alignLeftOfRepeatBarLine = false;
     359          370 :         if (segment->isChordRestType()) {
     360           29 :             x -= style().styleAbsolute(Sid::barNoteDistance);
     361          341 :         } else if (segment->isKeySigType()) {
     362            0 :             KeySig* sig = toKeySig(segment->element(track()));
     363            0 :             if (sig && !sig->ldata()->keySymbols.empty()) {
     364            0 :                 KeySym keySym = sig->ldata()->keySymbols.front();
     365            0 :                 PointF cutoutNW = score()->engravingFont()->smuflAnchor(keySym.sym, SmuflAnchorId::cutOutNW, 1.0);
     366            0 :                 x += cutoutNW.x();
     367              :             }
     368          341 :         } else if (segment->segmentType() & SegmentType::BarLineTypes && !isAtSystemStart) {
     369          341 :             x += segment->width();
     370          341 :             const BarLine* barline = toBarLine(segment->element(track()));
     371          341 :             alignLeftOfRepeatBarLine = barline && barline->barLineType() == BarLineType::END_REPEAT
     372          682 :                                        && style().styleB(Sid::voltaAlignEndLeftOfBarline);
     373          341 :             if (alignLeftOfRepeatBarLine) {
     374            0 :                 x -= style().styleAbsolute(Sid::endBarWidth);
     375              :             }
     376              :         }
     377          370 :         x += (isAtSystemStart || alignLeftOfRepeatBarLine ? 0.5 : -0.5) * absoluteFromSpatium(lineWidth());
     378              :     } else {
     379          370 :         if ((*system) && segment->tick() == (*system)->endTick()) {
     380          101 :             staff_idx_t si = backSegment()->effectiveStaffIdx();
     381          101 :             if (si == muse::nidx) {
     382            0 :                 return PointF(x, 0.0);
     383              :             }
     384          101 :             x += segment->staffShape(si).right();
     385          101 :             x -= 0.5 * absoluteFromSpatium(lineWidth());
     386          269 :         } else if (segment->segmentType() & SegmentType::BarLineTypes) {
     387          269 :             BarLine* barLine = toBarLine(segment->element(track()));
     388          269 :             if (barLine->barLineType() == BarLineType::END_REPEAT || barLine->barLineType() == BarLineType::END_START_REPEAT) {
     389          186 :                 x += symWidth(SymId::repeatDot) + style().styleAbsolute(Sid::repeatBarlineDotSeparation);
     390              :             }
     391          269 :             x += 0.5 * absoluteFromSpatium(lineWidth());
     392              :         }
     393              :     }
     394              : 
     395          740 :     return PointF(x, 0.0);
     396              : }
     397              : 
     398              : //---------------------------------------------------------
     399              : //   setVoltaType
     400              : //    deprecated
     401              : //---------------------------------------------------------
     402              : 
     403           33 : void Volta::setVoltaType(Type val)
     404              : {
     405           33 :     setEndHookType(Type::CLOSED == val ? HookType::HOOK_90 : HookType::NONE);
     406           33 : }
     407              : 
     408              : //---------------------------------------------------------
     409              : //   voltaType
     410              : //    deprecated
     411              : //---------------------------------------------------------
     412              : 
     413           22 : Volta::Type Volta::voltaType() const
     414              : {
     415           22 :     return endHookType() != HookType::NONE ? Type::CLOSED : Type::OPEN;
     416              : }
     417              : 
     418          494 : Sid Volta::defaultPosSid() const
     419              : {
     420          494 :     return Sid::voltaPosAbove;
     421              : }
     422              : }
        

Generated by: LCOV version 2.0-1