< prev index next >

src/java.desktop/share/classes/sun/font/FontDesignMetrics.java

Print this page


   1 /*
   2  * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 470             }
 471             width = new TextLayout(str, font, frc).getAdvance();
 472         } else {
 473             int length = str.length();
 474             for (int i=0; i < length; i++) {
 475                 char ch = str.charAt(i);
 476                 if (ch < 0x100) {
 477                     width += getLatinCharWidth(ch);
 478                 } else if (FontUtilities.isNonSimpleChar(ch)) {
 479                     width = new TextLayout(str, font, frc).getAdvance();
 480                     break;
 481                 } else {
 482                     width += handleCharWidth(ch);
 483                 }
 484             }
 485         }
 486 
 487         return (int) (0.5 + width);
 488     }
 489 
 490     public int charsWidth(char data[], int off, int len) {
 491 
 492         float width = 0;
 493         if (font.hasLayoutAttributes()) {
 494             if (len == 0) {
 495                 return 0;
 496             }
 497             String str = new String(data, off, len);
 498             width = new TextLayout(str, font, frc).getAdvance();
 499         } else {
 500             /* Explicit test needed to satisfy superclass spec */
 501             if (len < 0) {
 502                 throw new IndexOutOfBoundsException("len="+len);
 503             }
 504             int limit = off + len;
 505             for (int i=off; i < limit; i++) {
 506                 char ch = data[i];
 507                 if (ch < 0x100) {
 508                     width += getLatinCharWidth(ch);
 509                 } else if (FontUtilities.isNonSimpleChar(ch)) {
 510                     String str = new String(data, off, len);
 511                     width = new TextLayout(str, font, frc).getAdvance();
 512                     break;
 513                 } else {
 514                     width += handleCharWidth(ch);
 515                 }
 516             }
 517         }
 518 
 519         return (int) (0.5 + width);
 520     }
 521 
 522     /**
 523      * This method is called from java.awt.Font only after verifying
 524      * the arguments and that the text is simple and there are no
 525      * layout attributes, font transform etc.
 526      */
 527     public Rectangle2D getSimpleBounds(char data[], int off, int len) {
 528 
 529         float width = 0;
 530         int limit = off + len;
 531         for (int i=off; i < limit; i++) {
 532             char ch = data[i];
 533             if (ch < 0x100) {
 534                 width += getLatinCharWidth(ch);
 535             } else {
 536                 width += handleCharWidth(ch);
 537             }
 538         }
 539 
 540         float height = ascent + descent + leading;
 541         return new Rectangle2D.Float(0f, -ascent, width, height);
 542      }
 543 
 544     /**
 545      * Gets the advance widths of the first 256 characters in the
 546      * {@code Font}.  The advance is the
 547      * distance from the leftmost point to the rightmost point on the


   1 /*
   2  * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 470             }
 471             width = new TextLayout(str, font, frc).getAdvance();
 472         } else {
 473             int length = str.length();
 474             for (int i=0; i < length; i++) {
 475                 char ch = str.charAt(i);
 476                 if (ch < 0x100) {
 477                     width += getLatinCharWidth(ch);
 478                 } else if (FontUtilities.isNonSimpleChar(ch)) {
 479                     width = new TextLayout(str, font, frc).getAdvance();
 480                     break;
 481                 } else {
 482                     width += handleCharWidth(ch);
 483                 }
 484             }
 485         }
 486 
 487         return (int) (0.5 + width);
 488     }
 489 
 490     public int charsWidth(char[] data, int off, int len) {
 491 
 492         float width = 0;
 493         if (font.hasLayoutAttributes()) {
 494             if (len == 0) {
 495                 return 0;
 496             }
 497             String str = new String(data, off, len);
 498             width = new TextLayout(str, font, frc).getAdvance();
 499         } else {
 500             /* Explicit test needed to satisfy superclass spec */
 501             if (len < 0) {
 502                 throw new IndexOutOfBoundsException("len="+len);
 503             }
 504             int limit = off + len;
 505             for (int i=off; i < limit; i++) {
 506                 char ch = data[i];
 507                 if (ch < 0x100) {
 508                     width += getLatinCharWidth(ch);
 509                 } else if (FontUtilities.isNonSimpleChar(ch)) {
 510                     String str = new String(data, off, len);
 511                     width = new TextLayout(str, font, frc).getAdvance();
 512                     break;
 513                 } else {
 514                     width += handleCharWidth(ch);
 515                 }
 516             }
 517         }
 518 
 519         return (int) (0.5 + width);
 520     }
 521 
 522     /**
 523      * This method is called from java.awt.Font only after verifying
 524      * the arguments and that the text is simple and there are no
 525      * layout attributes, font transform etc.
 526      */
 527     public Rectangle2D getSimpleBounds(char[] data, int off, int len) {
 528 
 529         float width = 0;
 530         int limit = off + len;
 531         for (int i=off; i < limit; i++) {
 532             char ch = data[i];
 533             if (ch < 0x100) {
 534                 width += getLatinCharWidth(ch);
 535             } else {
 536                 width += handleCharWidth(ch);
 537             }
 538         }
 539 
 540         float height = ascent + descent + leading;
 541         return new Rectangle2D.Float(0f, -ascent, width, height);
 542      }
 543 
 544     /**
 545      * Gets the advance widths of the first 256 characters in the
 546      * {@code Font}.  The advance is the
 547      * distance from the leftmost point to the rightmost point on the


< prev index next >