jdk/src/share/classes/javax/swing/text/Utilities.java
changeset 5762 6b50a6f25d81
parent 5506 202f599c92aa
child 7668 d4a77089c587
equal deleted inserted replaced
5597:ab490f66d2cf 5762:6b50a6f25d81
   402         // didn't find, return end offset
   402         // didn't find, return end offset
   403         return txtCount;
   403         return txtCount;
   404     }
   404     }
   405 
   405 
   406     /**
   406     /**
       
   407      * Adjust text offset so that the length of a resulting string as a whole
       
   408      * fits into the specified width.
       
   409      */
       
   410     static int adjustOffsetForFractionalMetrics(
       
   411             Segment s, FontMetrics fm, int offset, int width) {
       
   412         // Sometimes the offset returned by getTabbedTextOffset is beyond the
       
   413         // available area, when fractional metrics are enabled. We should
       
   414         // guard against this.
       
   415         if (offset < s.count) {
       
   416             while (offset > 0 &&
       
   417                     fm.charsWidth(s.array, s.offset, offset + 1) > width) {
       
   418                 offset--;
       
   419             }
       
   420         }
       
   421         return offset;
       
   422     }
       
   423 
       
   424     /**
   407      * Determine where to break the given text to fit
   425      * Determine where to break the given text to fit
   408      * within the given span. This tries to find a word boundary.
   426      * within the given span. This tries to find a word boundary.
   409      * @param s  the source of the text
   427      * @param s  the source of the text
   410      * @param metrics the font metrics to use for the calculation
   428      * @param metrics the font metrics to use for the calculation
   411      * @param x0 the starting view location representing the start
   429      * @param x0 the starting view location representing the start
   423         char[] txt = s.array;
   441         char[] txt = s.array;
   424         int txtOffset = s.offset;
   442         int txtOffset = s.offset;
   425         int txtCount = s.count;
   443         int txtCount = s.count;
   426         int index = Utilities.getTabbedTextOffset(s, metrics, x0, x,
   444         int index = Utilities.getTabbedTextOffset(s, metrics, x0, x,
   427                                                   e, startOffset, false);
   445                                                   e, startOffset, false);
   428 
   446         index = adjustOffsetForFractionalMetrics(s, metrics, index, x - x0);
   429 
   447 
   430         if (index >= txtCount - 1) {
   448         if (index >= txtCount - 1) {
   431             return txtCount;
   449             return txtCount;
   432         }
   450         }
   433 
   451