Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8230672

Specification for java.awt.FontMetrics.getMaxAdvance() is too prescriptive.



    • Bug
    • Resolution: Fixed
    • P3
    • 15
    • 11, 14
    • client-libs
    • 2d
    • b22


      The specification says
      public int getMaxAdvance()

      Gets the maximum advance width of any character in this Font.
      The advance is the distance from the leftmost point to the rightmost point on the string's baseline.
      The advance of a String is not necessarily the sum of the advances of its characters.

          the maximum advance width of any character in the Font,
          or -1 if the maximum advance width is not known.


      The problem here is mainly with the first line "Gets the maximum advance width of any character in this Font."

      The only practical way to obtain this information is to read it from a field in the font.

      If the font is incorrect, this value may be incorrect.

      That being due to a bug in the font is one of the least likely reasons it may be incorrect.

      More likely ones are

      - Errors due to rendering at small sizes

      - Variation due to rendering behaviour, which may be determined by the platform, and opaque to the caller

      - Variation due to a necessity to use an algorithm to synthesise a requested style such as bold which is not otherwise possible.

      What are the uses of this API ? Presumably to determine an amount of space to allocate to be sure

      some given number of characters in a font will fit.

      But as noted "The advance of a String is not necessarily the sum of the advances of its characters"

      It could be slightly more, or notably with Arabic, much less.

      And anyway advance width is not the same thing as width in pixels of the glyph image

      So anyone using this to calculate size needs to be ready for it to be less than perfect.

      Perhaps this API should be deprecated, but there is not an easy replacement.

      At a minimum, we should add caveats to the documentation along the lines above.

      The following cautionary text is proposed

      "The accuracy of this API is dependent on the font itself, and the extent to which requested rendering

      behaviour may affect the advance width of characters. Notably, at small sizes, and when synthetic bolding is in effect.

      Exceeding the maximum advance by at least one user space pixel is then possible."

      Also after the text "not necessarily the sum of the advances of its characters", add

      "Nor is advance width the same as character image width, so if this value is used to determine

      the space need to render, then in extreme cases, this may result in slightly less space than required,

      or much more space than required, depending on the specific text being drawn".


        Issue Links



              prr Philip Race
              prr Philip Race
              0 Vote for this issue
              2 Start watching this issue