Uploaded image for project: 'PDFBox'
  1. PDFBox
  2. PDFBOX-3354

PDCIDFont.getAverageFontWidth always returns 0

VotersWatch issueWatchersLinkCloneUpdate Comment AuthorReplace String in CommentUpdate Comment VisibilityDelete Comments
    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • 2.0.1, 2.0.2, 3.0.0 PDFBox
    • 2.0.2, 3.0.0 PDFBox
    • None
    • None

    Description

      PDCIDFont.getAverageFontWidth method is using the field averageWidth as a cache, but fails to update its contents. Instead all the calculations go into a local variable with identical name,

      PDCIDFont.java
          public float getAverageFontWidth()
          {
              if (averageWidth == 0)
              {
                  float totalWidths = 0.0f;
                  float characterCount = 0.0f;
                  if (widths != null)
                  {
                      characterCount = widths.size();
                      Collection<Float> widthsValues = widths.values();
                      for (Float width : widthsValues)
                      {
                          totalWidths += width;
                      }
                  }
                  float averageWidth = totalWidths / characterCount;
                  if (averageWidth <= 0 || Float.isNaN(averageWidth))
                  {
                      averageWidth = getDefaultWidth();
                  }
              }
              return averageWidth;
          }
      

      A potential fix is the removal of the local variable declaration.

      float averageWidth = totalWidths / characterCount;

      Attachments

        Activity

          This comment will be Viewable by All Users Viewable by All Users
          Cancel

          People

            tilman Tilman Hausherr
            gabriel.carabas@gmail.com Gabriel Carabas
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Slack

                Issue deployment