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

HTML: Font size indices are miscalculated - off by 1 (vs. browsers)

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 1.4.0
    • 1.1.7, 1.3.0
    • client-libs
    • beta
    • generic, sparc
    • generic, solaris_2.6



      Name: krT82822 Date: 10/27/99


      The StyleSheet methods getIndexOfSize() and getPointSize() return the wrong results. Actually, font sizes are not officially part of HTML 3.2 but Swing supports them as do the browsers.
      Unfortunately, the Swing implementation does not match the browsers, it produces indices that are off by one.
      The browsers recognize 1-7 where as Swing recognizes 0-6. So in JTextPane everything is pushed up a size and there is no difference in appearance between a font with size=6 and one with size=7. As I said, this is not consistent with the browsers.

      The following HTML page illustrates the difference. Display it in Netscape or IE and then in JTextPane:

      <html><body>
      <p>
      <font size=0>zero</font><font size=1>one</font>
      <font size=6>six</font><font size=7>seven</font>
      </p>
      </body></html>

      There are two possible easy ways to fix this. In the class CSS make the following changes:

      //Add a dummy entry at index 0, making sizeMap.length == 8
      static int sizeMap[] = { 8, 10, 12, 14, 18, 24, 36 };

      static int getIndexOfSize(float pt) {
        for (int i = 0; i < sizeMap.length; i ++ ) //change 0 to 1 here
          if (pt <= sizeMap[i])
            return i;
        return sizeMap.length - 1;
      }

      float getPointSize(int index) {
        if (index < 0) //Change 0 to 1 here
          return sizeMap[0]; //Change 0 to 1 here
        else if (index > sizeMap.length - 1)
          return sizeMap[sizeMap.length - 1];
        else
          return sizeMap[index];
      }

      The other way is to leave the sizeMap array alone and make these changes:

      static int getIndexOfSize(float pt) {
        for (int i = 0; i < sizeMap.length; i ++ )
          if (pt <= sizeMap[i])
             return i; //Change i to i+1 here
        return sizeMap.length - 1; //Remove the "-1" here
      }

      float getPointSize(int index) {
        if (index < 0) //Change 0 to 1 here
          return sizeMap[0];
        else if (index > sizeMap.length - 1) //Remove the "-1" here
          return sizeMap[sizeMap.length - 1];
        else
          return sizeMap[index]; //Change index to index-1 here
      }

      Of course, all this is totally untested!

      This problem all causes relative sizes to be off by one. The browsers recognize -2 through +4 but Swing uses -3 through +3!

      msexter
      (Review ID: 96935)
      ======================================================================

            svioletsunw Scott Violet (Inactive)
            kryansunw Kevin Ryan (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: