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

QSvgGenerator doesn't convert all QFont::Weights properly

    XMLWordPrintable

Details

    • Bug
    • Resolution: Unresolved
    • P2: Important
    • None
    • 5.12.1
    • SVG Support
    • None
    • QSvgGenerator
      Windows 10 64bit
      Firefox, Chrome or Internet Explorer latest

    Description

      Today's Browsers except following CSS values for the SVG font-weight property:
      100 | 200 | 300 | 400 | 500 | 600 | 700 | 800 | 900
      (For example 400=Normal, 500=Medium, 700=Bold, 800=Black)
      The values must be full hundreds. Otherwise browsers are not able to identify the font weight property and therefore show them only in normal weight.

      Most of the time QSvgGenerator simply converts QFont::Weight values by multiplying them with 10 to get the resulting SVG font-weight. For example QFont::Weight=57 is converted to SVG font-weight=570. This will result in non valid SVG font-weight values, because the resulting SVG font-weight value is not a full hundred.

      There are 3 exceptions: The QFont::Weight constants QFont::Light, QFont::Normal and QFont::Bold are converted to valid SVG font-weight values:

      QFont::Light=25 => SVG font-weight=100
      QFont::Normal=50 => SVG font-weight=400
      QFont::Bold=75 => SVG font-weight=700

      For other SVG font-weight values you have to set QFont::Weight numerically in values of full tens to get resulting SVG font-weight values of full hundreds.

      But even the latter doesn't work properly if one wants to get a resulting SVG font-weight=500 (SVG medium font weight) to display for example a 'Roboto Medium' font. Because QSvgConverter converts QFont::Weight=50 to SVG font-weight=400 which is a 'normal' font weight in SVG. (Look above at QFont::Normal constant conversion). Because of that there is no way to display a medium font in a SVG which was converted by QSvgConverter.

      Following simple changes in qsvggenerator.cpp / class QSvgPaintEngine will solve these problems:

      // Actual code:
      
      void qfontToSvg(const QFont &sfont)
      {
      ... line 462
          int svgWeight = d->font.weight();
          switch (svgWeight) {
          case QFont::Light:
              svgWeight = 100;
              break;
          case QFont::Normal:
              svgWeight = 400;
              break;
          case QFont::Bold:
              svgWeight = 700;
              break;
          default:
              svgWeight *= 10;
          }
      ... line 476    
      }
      
      // New code:
      
      void qfontToSvg(const QFont &sfont)
      {
      ... line 462
          int svgWeight = d->font.weight();
          switch (svgWeight) {
          case QFont::Thin:
              svgWeight = 100;
              break;
          case QFont::ExtraLight:
              svgWeight = 200;
              break;
          case QFont::Light:
              svgWeight = 300;
              break;
          case QFont::Normal:
              svgWeight = 400;
              break;
          case QFont::Medium:
              svgWeight = 500;
              break;
          case QFont::DemiBold:
              svgWeight = 600;
              break;
          case QFont::Bold:
              svgWeight = 700;
              break;
          case QFont::ExtraBold:
              svgWeight = 700;
              break;
          case QFont::Black:
              svgWeight = 800;
              break;
          default:
              if (svgWeight >= 0 && svgWeight < 30) {
                  svgWeight += 10;
              }
              else if (svgWeight >= 45 && svgWeight < 50) {
                  svgWeight -= 10;
              }
              svgWeight = qRound(svgWeight/10.0) *100; // Limit to multiple of hundred
          }
      
          if (svgWeight > 900) {
              svgWeight = 900;    // SVG maximum weight
          }
      
          // This code changes ensure that all QFont::Weight values are
          // always converted to valid SVG font-weight values (full hundreds).
      ...
      }
      

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            Unassigned Unassigned
            mireiner Chris
            Votes:
            2 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes