Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-115648

QSvgRenderer::boundsOnElement() can return wrong results with multiple child <text> elements

    XMLWordPrintable

Details

    • d257737a5 (dev), 6ee6fafa1 (6.6), 6242980de (6.5), 1370f271e (tqtc/lts-6.2)

    Description

      Code

      #include <QGuiApplication>
      #include <QSvgRenderer>
      #include <QDebug>
      
      int main(int argc, char *argv[])
      {
          QGuiApplication a(argc, argv);
      
          const QByteArrayList elements{
              R"x(<text id="x" transform="matrix(0 -1 1 0 16 27.5)" font-family="Arial" font-size="10px">X</text>)x",
              R"y(<text id="y" transform="matrix(0 -1 1 0 27.5 27.5)" font-family="Arial" font-size="10px">Y</text>)y"
          };
      
          for (int i = 0; i < 3; ++i)
          {
              QByteArray data = R"header(<svg xmlns="http://www.w3.org/2000/svg"><g id="all">)header";
              if (i % 2 == 0)
                  data += elements[0];
              if (i > 0)
                  data += elements[1];
              data += "</g></svg>";
      
              qDebug() << "=== Test" << i << "===";
              qDebug() << data;
      
              QSvgRenderer renderer(data);
      
              QRectF unionRect;
              if (renderer.elementExists("x"))
              {
                  auto xRect = renderer.boundsOnElement("x");
                  qDebug() << "            X:" << xRect;
                  unionRect = unionRect.united(xRect);
              }
              if (renderer.elementExists("y"))
              {
                  auto yRect = renderer.boundsOnElement("y");
                  qDebug() << "            Y:" << yRect;
                  unionRect = unionRect.united(yRect);
              }
      
              qDebug() << "All, method 1:" << unionRect;
              qDebug() << "All, method 2:" << renderer.boundsOnElement("all");
              qDebug("\n");
          }
      }
      

       

      Expected outcomes
      At the end of every test case, we should see that renderer.boundsOnElement("all") == unionRect

       

      Actual outcomes

      • When there is only 1 text element (Test 0 and Test 1), the result is as expected
      • When there is more than 1 text element (Test 2), renderer.boundsOnElement("all") is much larger than unionRect
      === Test 0 ===
      "<svg xmlns=\"http://www.w3.org/2000/svg\"><g id=\"all\"><text id=\"x\" transform=\"matrix(0 -1 1 0 16 27.5)\" font-family=\"Arial\" font-size=\"10px\">X</text></g></svg>"
                  X: QRectF(6.94688,20.8313 11.1719x6.66875)
      All, method 1: QRectF(6.94688,20.8313 11.1719x6.66875)
      All, method 2: QRectF(6.94688,20.8313 11.1719x6.66875)
      
      === Test 1 ===
      "<svg xmlns=\"http://www.w3.org/2000/svg\"><g id=\"all\"><text id=\"y\" transform=\"matrix(0 -1 1 0 27.5 27.5)\" font-family=\"Arial\" font-size=\"10px\">Y</text></g></svg>"
                  Y: QRectF(18.4469,20.8313 11.1719x6.66875)
      All, method 1: QRectF(18.4469,20.8313 11.1719x6.66875)
      All, method 2: QRectF(18.4469,20.8313 11.1719x6.66875)
      
      === Test 2 ===
      "<svg xmlns=\"http://www.w3.org/2000/svg\"><g id=\"all\"><text id=\"x\" transform=\"matrix(0 -1 1 0 16 27.5)\" font-family=\"Arial\" font-size=\"10px\">X</text><text id=\"y\" transform=\"matrix(0 -1 1 0 27.5 27.5)\" font-family=\"Arial\" font-size=\"10px\">Y</text></g></svg>"
                  X: QRectF(6.94688,20.8313 11.1719x6.66875)
                  Y: QRectF(18.4469,20.8313 11.1719x6.66875)
      All, method 1: QRectF(6.94688,20.8313 22.6719x6.66875)
      All, method 2: QRectF(6.94688,-2.11875 36.5531x29.6187)
      
      

       

      Notes

      • The issue doesn't seem to occur if there is no transform attribute on the text elements
      • The issue is not applicable to Qt 5.15 as boundsOnElement() only returns null rectangles in this test code

      Attachments

        For Gerrit Dashboard: QTBUG-115648
        # Subject Branch Project Status CR V

        Activity

          People

            vgt Eirik Aavitsland
            skoh-qt Sze Howe Koh
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews