LCOV - code coverage report
Current view: top level - src/engraving/rendering/score - textlayout.cpp Coverage Total Hit
Test: filtered_coverage.info Lines: 79.0 % 271 214
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) 2025 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              : #include "textlayout.h"
      23              : #include "tlayout.h"
      24              : #include "dom/box.h"
      25              : #include "dom/page.h"
      26              : #include "dom/staff.h"
      27              : #include "dom/textlinebase.h"
      28              : #include "types/typesconv.h"
      29              : 
      30              : using namespace mu::engraving::rendering::score;
      31              : using namespace mu::engraving;
      32              : using namespace muse::draw;
      33              : 
      34        31304 : void TextLayout::layoutBaseTextBase(const TextBase* item, TextBase::LayoutData* ldata)
      35              : {
      36        31304 :     IF_ASSERT_FAILED(item->ownershipParent()) {
      37            0 :         return;
      38              :     }
      39              : 
      40        31304 :     ldata->setPos(PointF());
      41              : 
      42        31304 :     if (item->placeBelow()) {
      43        24613 :         ldata->setPosY(item->staff() ? item->staff()->staffHeight(item->tick()) : 0.0);
      44              :     }
      45              : 
      46        31304 :     layoutBaseTextBase1(item, ldata);
      47              : }
      48              : 
      49          243 : void TextLayout::layoutBaseTextBase(TextBase* item, LayoutContext&)
      50              : {
      51          243 :     layoutBaseTextBase(item, item->mutldata());
      52          243 : }
      53              : 
      54        77349 : void TextLayout::layoutBaseTextBase1(const TextBase* item, TextBase::LayoutData* ldata)
      55              : {
      56        77364 :     TRACEFUNC;
      57              : 
      58        77349 :     if (item->layoutParent() && item->layoutToParentWidth()) {
      59        12568 :         LD_CONDITION(item->layoutParent()->ldata()->isSetBbox());
      60              :     }
      61              : 
      62        77349 :     if (ldata->layoutInvalid) {
      63        26293 :         item->createBlocks(ldata);
      64              :     }
      65              : 
      66        77349 :     double y = 0.0;
      67              : 
      68        77349 :     double maxBlockWidth = -DBL_MAX;
      69              :     // adjust the bounding box for the text item
      70       155256 :     for (size_t i = 0; i < ldata->blocks.size(); ++i) {
      71        77907 :         TextBlock& t = ldata->blocks[i];
      72        77907 :         layoutTextBlock(&t, item);
      73        77907 :         y += t.lineSpacing();
      74        77907 :         t.setY(y);
      75        77907 :         maxBlockWidth = std::max(maxBlockWidth, t.boundingRect().width());
      76              :     }
      77              : 
      78        77349 :     Shape shape;
      79        77349 :     textHorizontalLayout(item, shape, maxBlockWidth, ldata);
      80              : 
      81        77349 :     RectF bb = shape.bbox();
      82              : 
      83        77349 :     double yoff = 0;
      84        77349 :     double h    = 0;
      85        77349 :     const EngravingItem* parent = item->layoutParent();
      86        77349 :     if (parent) {
      87        75816 :         if (item->layoutToParentWidth()) {
      88        12568 :             if (parent->isTBox()) {
      89              :                 // hack: vertical alignment is always TOP
      90          131 :                 const_cast<TextBase*>(item)->setAlign({ item->align().horizontal, AlignV::TOP });
      91        12437 :             } else if (parent->isBox()) {
      92              :                 // consider inner margins of frame
      93        12191 :                 const Box* b = toBox(parent);
      94        12191 :                 yoff = b->topMargin() * DPMM;
      95              : 
      96        12191 :                 if (b->height() < bb.bottom()) {
      97           95 :                     h = b->height() / 2 + bb.height();
      98              :                 } else {
      99        12096 :                     h  = b->height() - yoff - b->bottomMargin() * DPMM;
     100              :                 }
     101          246 :             } else if (parent->isPage()) {
     102          246 :                 const Page* p = toPage(parent);
     103          246 :                 h = p->height() - p->tm() - p->bm();
     104          246 :                 yoff = p->tm();
     105            0 :             } else if (parent->isMeasure()) {
     106              :             } else {
     107            0 :                 h  = parent->height();
     108              :             }
     109              :         }
     110              :     } else {
     111         1533 :         ldata->setPos(PointF());
     112              :     }
     113              : 
     114        77349 :     if (item->align() == AlignV::BOTTOM) {
     115         3181 :         yoff += h - bb.bottom();
     116        74168 :     } else if (item->align() == AlignV::VCENTER) {
     117         3301 :         yoff +=  (h - (bb.top() + bb.bottom())) * .5;
     118        70867 :     } else if (item->align() == AlignV::BASELINE) {
     119        60482 :         yoff += h * .5 - ldata->blocks.front().lineSpacing();
     120              :     } else {
     121        10385 :         yoff += -bb.top();
     122              :     }
     123              : 
     124       155256 :     for (TextBlock& t : ldata->blocks) {
     125        77907 :         t.setY(t.y() + yoff);
     126              :     }
     127              : 
     128        77349 :     shape.translateY(yoff);
     129        77349 :     ldata->setShape(shape);
     130              : 
     131        77349 :     if (item->hasFrame()) {
     132          144 :         item->layoutFrame(ldata);
     133              :     }
     134              : 
     135        77349 :     if (!item->isDynamic() && !(parent && parent->isBox())) {
     136        63742 :         computeTextHighResShape(item, ldata);
     137              :     }
     138              : 
     139        77349 :     if (!muse::RealIsNull(item->textAngle())) {
     140            0 :         Transform t;
     141            0 :         t.rotate(item->textAngle());
     142            0 :         ldata->setShape(shape.transform(t));
     143            0 :         ldata->highResShape.mut_value().transform(t);
     144              :     }
     145              : 
     146        77349 :     PointF defaultPos = item->defaultPos();
     147        77349 :     ldata->move(defaultPos);
     148        77349 : }
     149              : 
     150        41533 : void TextLayout::layoutBaseTextBase1(TextBase* item, const LayoutContext&)
     151              : {
     152        41533 :     layoutBaseTextBase1(item, item->mutldata());
     153        41533 : }
     154              : 
     155        66116 : void TextLayout::computeTextHighResShape(const TextBase* item, TextBase::LayoutData* ldata)
     156              : {
     157        66116 :     Shape& shape = ldata->highResShape.mut_value();
     158        66116 :     shape.clear();
     159        66116 :     shape.elements().reserve(item->xmlText().size());
     160              : 
     161       132538 :     for (const TextBlock& block : ldata->blocks) {
     162        66422 :         double y = block.y();
     163       132024 :         for (const TextFragment& fragment : block.fragments()) {
     164        65602 :             FontMetrics fontMetrics = FontMetrics(fragment.font(item));
     165        65602 :             double x = fragment.pos.x();
     166        65602 :             size_t textSize = fragment.text.size();
     167       370757 :             for (size_t i = 0; i < textSize; ++i) {
     168       305155 :                 Char character = fragment.text.at(i);
     169       305155 :                 String text{ character };
     170       305155 :                 if (character.isHighSurrogate() && i + 1 < textSize) {
     171          444 :                     text += fragment.text.at(i + 1);
     172          444 :                     i++;
     173              :                 }
     174       305155 :                 RectF characterBoundingRect = fontMetrics.tightBoundingRect(text);
     175       305155 :                 characterBoundingRect.translate(x, y);
     176       305155 :                 shape.add(characterBoundingRect, item);
     177       305155 :                 if (i + 1 < textSize) {
     178       239570 :                     x += fontMetrics.horizontalAdvance(text);
     179              :                 }
     180       305155 :             }
     181        65602 :         }
     182              :     }
     183              : 
     184        66116 :     ldata->highResShape = shape;
     185        66116 : }
     186              : 
     187        77349 : void TextLayout::textHorizontalLayout(const TextBase* item, Shape& shape, double maxBlockWidth, TextBase::LayoutData* ldata)
     188              : {
     189        77349 :     double leftMargin = 0.0;
     190        77349 :     double layoutWidth = 0;
     191        77349 :     EngravingItem* parent = item->layoutParent();
     192        77349 :     if (parent && item->layoutToParentWidth()) {
     193        12568 :         layoutWidth = parent->width();
     194        12568 :         switch (parent->type()) {
     195        12322 :         case ElementType::HBOX:
     196              :         case ElementType::VBOX:
     197              :         case ElementType::TBOX: {
     198        12322 :             Box* b = toBox(parent);
     199        12322 :             layoutWidth -= ((b->leftMargin() + b->rightMargin()) * DPMM);
     200        12322 :             leftMargin = b->leftMargin() * DPMM;
     201              :         }
     202        12322 :         break;
     203          246 :         case ElementType::PAGE: {
     204          246 :             Page* p = toPage(parent);
     205          246 :             layoutWidth -= (p->lm() + p->rm());
     206          246 :             leftMargin = p->lm();
     207              :         }
     208          246 :         break;
     209              :             break;
     210            0 :         default:
     211            0 :             ASSERT_X("Lay out to parent width only valid for items with page or frame as parent");
     212              :         }
     213        64781 :     } else if (item->positionRelativeToNoteheadRest()) {
     214              :         // For items aligned to notehead/rests
     215         9849 :         double mag = item->staff() ? item->staff()->staffMag(item) : 1.0;
     216         9849 :         layoutWidth = item->symWidth(SymId::noteheadBlack) * mag;
     217              :     }
     218              : 
     219              :     // Position and alignment
     220       155256 :     for (size_t i = 0; i < ldata->blocks.size(); ++i) {
     221        77907 :         TextBlock& textBlock = ldata->blocks[i];
     222        77907 :         double xAdj = leftMargin - textBlock.boundingRect().left();
     223              : 
     224              :         // Set position relative to reference point
     225        77907 :         AlignH position = item->position();
     226        77907 :         if (position == AlignH::HCENTER) {
     227        19364 :             xAdj += (layoutWidth - maxBlockWidth) * .5;
     228        58543 :         } else if (position == AlignH::RIGHT) {
     229        46825 :             xAdj += layoutWidth - maxBlockWidth;
     230              :         }
     231              : 
     232        77907 :         double diff = maxBlockWidth - textBlock.boundingRect().width();
     233        77907 :         if (muse::RealIsNull(diff)) {
     234              :             // This is the longest line, don't align
     235       153817 :             for (TextFragment& f : textBlock.fragments()) {
     236        76463 :                 f.pos.rx() += xAdj;
     237              :             }
     238        77354 :             textBlock.shape().translate(PointF(xAdj, 0.0));
     239        77354 :             shape.add(textBlock.shape().translated(PointF(0.0, textBlock.y())));
     240        77354 :             continue;
     241        77354 :         }
     242              : 
     243              :         // Align relative to the longest line
     244          553 :         AlignH alignH = item->align().horizontal;
     245          553 :         if (alignH == AlignH::HCENTER) {
     246          185 :             xAdj += diff * 0.5;
     247          368 :         } else if (alignH == AlignH::RIGHT) {
     248          147 :             xAdj += diff;
     249          221 :         } else if (alignH == AlignH::JUSTIFY) {
     250              :             // Don't justify final line
     251            0 :             if (i != ldata->blocks.size() - 1) {
     252            0 :                 justifyLine(item, &textBlock, maxBlockWidth);
     253              :             }
     254              :         }
     255              : 
     256         1150 :         for (TextFragment& fragment : textBlock.fragments()) {
     257          597 :             fragment.pos.rx() += xAdj;
     258              :         }
     259          553 :         textBlock.shape().translate(PointF(xAdj, 0.0));
     260          553 :         shape.add(textBlock.shape().translated(PointF(0.0, textBlock.y())));
     261              :     }
     262        77349 : }
     263              : 
     264            0 : void TextLayout::justifyLine(const TextBase* item, TextBlock* textBlock, double maxBlockWidth)
     265              : {
     266              :     struct SubFragment {
     267              :         String text;
     268              :         CharFormat format;
     269              :         double width;
     270              :     };
     271            0 :     std::vector<SubFragment> subfrags;
     272            0 :     for (const TextFragment& f : textBlock->fragments()) {
     273            0 :         String current;
     274            0 :         FontMetrics fm(f.font(item));
     275            0 :         for (size_t i = 0; i < f.text.size(); ++i) {
     276            0 :             Char c = f.text.at(i);
     277            0 :             current += c;
     278            0 :             if (c.isSpace()) {
     279            0 :                 double w = fm.width(current);
     280            0 :                 subfrags.push_back({ current, f.format, w });
     281            0 :                 current.clear();
     282              :             }
     283              :         }
     284            0 :         if (!current.isEmpty()) {
     285            0 :             double w = fm.width(current);
     286            0 :             subfrags.push_back({ current, f.format, w });
     287              :         }
     288            0 :     }
     289              : 
     290            0 :     int numSpaces = 0;
     291            0 :     double textWidth = 0;
     292            0 :     for (const SubFragment& sf : subfrags) {
     293            0 :         numSpaces++;
     294            0 :         textWidth += sf.width; // accumulate text width
     295              :     }
     296            0 :     numSpaces--;
     297            0 :     double spaceToFill = maxBlockWidth - textWidth;
     298            0 :     if (numSpaces > 0 && spaceToFill > 0) {
     299            0 :         textBlock->fragments().clear();
     300            0 :         double extraSpacing = spaceToFill / numSpaces;
     301            0 :         double x = 0.0;
     302            0 :         for (const SubFragment& sf : subfrags) {
     303            0 :             TextFragment frag;
     304            0 :             frag.text = sf.text;
     305            0 :             frag.format = sf.format;
     306            0 :             frag.pos.rx() = x;
     307            0 :             textBlock->fragments().push_back(frag);
     308            0 :             x += sf.width + extraSpacing;
     309            0 :         }
     310              :     }
     311            0 : }
     312              : 
     313        77907 : void TextLayout::layoutTextBlock(TextBlock* item, const TextBase* t)
     314              : {
     315        77907 :     std::list<TextFragment>& fragments = item->fragments();
     316        77907 :     Shape& shape = item->shape();
     317        77907 :     shape.clear();
     318        77907 :     double x      = 0.0;
     319        77907 :     double lineSpacing = 0.0;
     320              : 
     321        77907 :     if (fragments.empty()) {
     322         2116 :         FontMetrics fm = t->fontMetrics();
     323         2116 :         shape.add(RectF(0.0, -fm.ascent(), 1.0, fm.ascent()), t);
     324         2116 :         lineSpacing = fm.lineSpacing();
     325        77907 :     } else if (fragments.size() == 1 && fragments.front().text.isEmpty()) {
     326          130 :         auto fi = fragments.begin();
     327          130 :         TextFragment& f = *fi;
     328          130 :         f.pos.setX(x);
     329          130 :         Font font = f.font(t);
     330          130 :         substituteMusicFont(font, f.calculatedFontSize(t));
     331          130 :         FontMetrics fm(font);
     332          130 :         if (f.format.valign() != VerticalAlignment::AlignNormal) {
     333            0 :             double voffset = fm.xHeight() / SUBSCRIPT_SIZE;   // use original height
     334            0 :             if (f.format.valign() == VerticalAlignment::AlignSubScript) {
     335            0 :                 voffset *= SUBSCRIPT_OFFSET;
     336              :             } else {
     337            0 :                 voffset *= SUPERSCRIPT_OFFSET;
     338              :             }
     339              : 
     340            0 :             f.pos.setY(voffset);
     341              :         } else {
     342          130 :             f.pos.setY(0.0);
     343              :         }
     344              : 
     345          130 :         RectF temp(0.0, -fm.ascent(), 1.0, fm.ascent());
     346          130 :         shape.add(temp, t);
     347          130 :         lineSpacing = std::max(lineSpacing, fm.lineSpacing());
     348          130 :     } else {
     349        75661 :         const auto fiLast = --fragments.end();
     350       152591 :         for (auto fi = fragments.begin(); fi != fragments.end(); ++fi) {
     351        76930 :             TextFragment& f = *fi;
     352        76930 :             f.pos.setX(x);
     353        76930 :             Font fragmentFont = f.font(t);
     354        76930 :             FontMetrics fm(fragmentFont);
     355        76930 :             if (f.format.valign() != VerticalAlignment::AlignNormal) {
     356            2 :                 double voffset = fm.xHeight() / SUBSCRIPT_SIZE;           // use original height
     357            2 :                 if (f.format.valign() == VerticalAlignment::AlignSubScript) {
     358            0 :                     voffset *= SUBSCRIPT_OFFSET;
     359              :                 } else {
     360            2 :                     voffset *= SUPERSCRIPT_OFFSET;
     361              :                 }
     362            2 :                 f.pos.setY(voffset);
     363              :             } else {
     364        76928 :                 f.pos.setY(0.0);
     365              :             }
     366              : 
     367              :             // Optimization: don't calculate character position
     368              :             // for the next fragment if there is no next fragment
     369        76930 :             if (fi != fiLast) {
     370         1269 :                 const double w  = fm.width(f.text);
     371         1269 :                 x += w;
     372              :             }
     373              : 
     374        76930 :             double yOffset = musicSymbolBaseLineAdjust(item, t, f, fi);
     375        76930 :             f.pos.ry() -= yOffset;
     376              : 
     377        76930 :             RectF textBRect = fm.tightBoundingRect(f.text).translated(f.pos);
     378        76930 :             bool useDynamicSymShape = fragmentFont.type() == Font::Type::MusicSymbol && t->isDynamic();
     379        76930 :             if (useDynamicSymShape) {
     380         1284 :                 const Dynamic* dyn = toDynamic(t);
     381         1284 :                 SymId symId = TConv::symId(dyn->dynamicType());
     382         1284 :                 if (symId != SymId::noSym) {
     383         1262 :                     shape.add(dyn->symShapeWithCutouts(symId).translated(f.pos));
     384              :                 } else {
     385           22 :                     shape.add(textBRect, t);
     386              :                 }
     387              :             } else {
     388        75646 :                 shape.add(textBRect, t);
     389              :             }
     390        76930 :             if (fragmentFont.type() == Font::Type::MusicSymbol || fragmentFont.type() == Font::Type::MusicSymbolText) {
     391              :                 // SEMI-HACK: Music fonts can have huge linespacing because of tall symbols, so instead of using the
     392              :                 // font linespacing value we just use the height of the individual fragment with some added margin
     393              : 
     394         2832 :                 lineSpacing = std::max(lineSpacing, 1.25 * (shape.bbox().height() - shape.bbox().bottom()) + yOffset);
     395              :             } else {
     396        74098 :                 lineSpacing = std::max(lineSpacing, fm.lineSpacing());
     397              :             }
     398        76930 :         }
     399              :     }
     400              : 
     401              :     // Apply style/custom line spacing
     402        77907 :     lineSpacing *= t->textLineSpacing();
     403        77907 :     item->setLineSpacing(lineSpacing);
     404        77907 : }
     405              : 
     406          130 : void TextLayout::substituteMusicFont(Font& font, double size)
     407              : {
     408          130 :     if (font.type() == Font::Type::MusicSymbol) {
     409            0 :         String textFontId(font.family().id() + String(u" Text"));
     410            0 :         font.setFamily(textFontId, Font::Type::MusicSymbolText);
     411            0 :         font.setPointSizeF(size);
     412          130 :     } else if (font.type() == Font::Type::MusicSymbolText) {
     413            0 :         font.setPointSizeF(size);
     414              :     }
     415          130 : }
     416              : 
     417        76930 : double TextLayout::musicSymbolBaseLineAdjust(const TextBlock* block, const TextBase* t, const TextFragment& f,
     418              :                                              const std::list<TextFragment>::iterator fi)
     419              : {
     420        76930 :     Font fragmentFont = f.font(t);
     421        76930 :     FontMetrics fm(fragmentFont);
     422        76930 :     const bool adjustSymbol = fragmentFont.type() == Font::Type::MusicSymbolText && t->isMarker();
     423        76930 :     if (!adjustSymbol) {
     424        76672 :         return 0.0;
     425              :     }
     426              : 
     427              :     // Align the x-height of the coda symbol to half the x-height of the surrounding text
     428          258 :     Font refFont;
     429          258 :     if (block->fragments().size() == 1) {
     430          236 :         refFont = t->font();
     431              :     } else {
     432           44 :         TextFragment& refFragment = fi != block->fragments().begin() ? *(std::prev(fi)) : *(std::next(fi));
     433           22 :         refFont = refFragment.font(t);
     434              :     }
     435          258 :     FontMetrics refFm(refFont);
     436              : 
     437          258 :     const double middle = (fm.tightBoundingRect(f.text).height() / 2) - fm.tightBoundingRect(f.text).bottom();
     438          258 :     const double refXHeight = refFm.capHeight() / 2;
     439          258 :     return refXHeight - middle;
     440        76930 : }
        

Generated by: LCOV version 2.0-1