< prev index next >

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

Print this page




   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
  23  * questions.
  24  */
  25 
  26 package sun.font;
  27 



  28 import java.awt.Font;
  29 import java.awt.font.FontRenderContext;
  30 import java.awt.geom.AffineTransform;
  31 import java.lang.ref.Reference;
  32 import java.lang.ref.SoftReference;
  33 import java.util.concurrent.ConcurrentHashMap;
  34 import java.util.Locale;
  35 
  36 public abstract class Font2D {
  37 
  38     /* Note: JRE and FONT_CONFIG ranks are identical. I don't know of a reason
  39      * to distingish these. Possibly if a user adds fonts to the JRE font
  40      * directory that are the same font as the ones specified in the font
  41      * configuration but that is more likely to be the legitimate intention
  42      * than a problem. One reason why these should be the same is that on
  43      * Linux the JRE fonts ARE the font configuration fonts, and although I
  44      * believe all are assigned FONT_CONFIG rank, it is conceivable that if
  45      * this were not so, that some JRE font would not be allowed to joint the
  46      * family of its siblings which were assigned FONT_CONFIG rank. Giving
  47      * them the same rank is the easy solution for now at least.


  58         "bold", "demibold", "demi-bold", "demi bold", "negreta", "demi", };
  59 
  60     private static final String[] italicNames = {
  61         "italic", "cursiva", "oblique", "inclined", };
  62 
  63     private static final String[] boldItalicNames = {
  64           "bolditalic", "bold-italic", "bold italic",
  65           "boldoblique", "bold-oblique", "bold oblique",
  66           "demibold italic", "negreta cursiva","demi oblique", };
  67 
  68     private static final FontRenderContext DEFAULT_FRC =
  69         new FontRenderContext(null, false, false);
  70 
  71     public Font2DHandle handle;
  72     protected String familyName;           /* Family font name (english) */
  73     protected String fullName;             /* Full font name (english)   */
  74     protected int style = Font.PLAIN;
  75     protected FontFamily family;
  76     protected int fontRank = DEFAULT_RANK;
  77 


  78     /*
  79      * A mapper can be independent of the strike.
  80      * Perhaps the reference to the mapper ought to be held on the
  81      * scaler, as it may be implemented via scaler functionality anyway
  82      * and so the mapper would be useless if its native portion was
  83      * freed when the scaler was GC'd.
  84      */
  85     protected CharToGlyphMapper mapper;
  86 
  87     /*
  88      * The strike cache is maintained per "Font2D" as that is the
  89      * principal object by which you look up fonts.
  90      * It means more Hashmaps, but look ups can be quicker because
  91      * the map will have fewer entries, and there's no need to try to
  92      * make the Font2D part of the key.
  93      */
  94     protected ConcurrentHashMap<FontStrikeDesc, Reference<FontStrike>>
  95         strikeCache = new ConcurrentHashMap<>();
  96 
  97     /* Store the last Strike in a Reference object.


 461      * to check the font class before attempting to run, rather than needing
 462      * to promote this method up from TrueTypeFont
 463      */
 464     protected byte[] getTableBytes(int tag) {
 465         return null;
 466     }
 467 
 468     /* implemented for fonts backed by an sfnt that has
 469      * OpenType or AAT layout tables.
 470      */
 471     protected long getLayoutTableCache() {
 472         return 0L;
 473     }
 474 
 475     /* Used only on OS X.
 476      */
 477     protected long getPlatformNativeFontPtr() {
 478         return 0L;
 479     }
 480 

















 481     /* for layout code */
 482     protected long getUnitsPerEm() {
 483         return 2048;
 484     }
 485 
 486     boolean supportsEncoding(String encoding) {
 487         return false;
 488     }
 489 
 490     public boolean canDoStyle(int style) {
 491         return (style == this.style);
 492     }
 493 
 494     /*
 495      * All the important subclasses override this which is principally for
 496      * the TrueType 'gasp' table.
 497      */
 498     public boolean useAAForPtSize(int ptsize) {
 499         return true;
 500     }


 542 
 543     public float getItalicAngle(Font font, AffineTransform at,
 544                                 Object aaHint, Object fmHint) {
 545         /* hardwire psz=12 as that's typical and AA vs non-AA for 'gasp' mode
 546          * isn't important for the caret slope of this rarely used API.
 547          */
 548         int aa = FontStrikeDesc.getAAHintIntVal(aaHint, this, 12);
 549         int fm = FontStrikeDesc.getFMHintIntVal(fmHint);
 550         FontStrike strike = getStrike(font, at, aa, fm);
 551         StrikeMetrics metrics = strike.getFontMetrics();
 552         if (metrics.ascentY == 0 || metrics.ascentX == 0) {
 553             return 0f;
 554         } else {
 555             /* ascent is "up" from the baseline so its typically
 556              * a negative value, so we need to compensate
 557              */
 558             return metrics.ascentX/-metrics.ascentY;
 559         }
 560     }
 561 












 562 }


   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
  23  * questions.
  24  */
  25 
  26 package sun.font;
  27 
  28 import sun.java2d.Disposer;
  29 import sun.java2d.DisposerRecord;
  30 
  31 import java.awt.Font;
  32 import java.awt.font.FontRenderContext;
  33 import java.awt.geom.AffineTransform;
  34 import java.lang.ref.Reference;
  35 import java.lang.ref.SoftReference;
  36 import java.util.concurrent.ConcurrentHashMap;
  37 import java.util.Locale;
  38 
  39 public abstract class Font2D {
  40 
  41     /* Note: JRE and FONT_CONFIG ranks are identical. I don't know of a reason
  42      * to distingish these. Possibly if a user adds fonts to the JRE font
  43      * directory that are the same font as the ones specified in the font
  44      * configuration but that is more likely to be the legitimate intention
  45      * than a problem. One reason why these should be the same is that on
  46      * Linux the JRE fonts ARE the font configuration fonts, and although I
  47      * believe all are assigned FONT_CONFIG rank, it is conceivable that if
  48      * this were not so, that some JRE font would not be allowed to joint the
  49      * family of its siblings which were assigned FONT_CONFIG rank. Giving
  50      * them the same rank is the easy solution for now at least.


  61         "bold", "demibold", "demi-bold", "demi bold", "negreta", "demi", };
  62 
  63     private static final String[] italicNames = {
  64         "italic", "cursiva", "oblique", "inclined", };
  65 
  66     private static final String[] boldItalicNames = {
  67           "bolditalic", "bold-italic", "bold italic",
  68           "boldoblique", "bold-oblique", "bold oblique",
  69           "demibold italic", "negreta cursiva","demi oblique", };
  70 
  71     private static final FontRenderContext DEFAULT_FRC =
  72         new FontRenderContext(null, false, false);
  73 
  74     public Font2DHandle handle;
  75     protected String familyName;           /* Family font name (english) */
  76     protected String fullName;             /* Full font name (english)   */
  77     protected int style = Font.PLAIN;
  78     protected FontFamily family;
  79     protected int fontRank = DEFAULT_RANK;
  80 
  81     private HarfbuzzFaceRef harfbuzzFaceRef;
  82 
  83     /*
  84      * A mapper can be independent of the strike.
  85      * Perhaps the reference to the mapper ought to be held on the
  86      * scaler, as it may be implemented via scaler functionality anyway
  87      * and so the mapper would be useless if its native portion was
  88      * freed when the scaler was GC'd.
  89      */
  90     protected CharToGlyphMapper mapper;
  91 
  92     /*
  93      * The strike cache is maintained per "Font2D" as that is the
  94      * principal object by which you look up fonts.
  95      * It means more Hashmaps, but look ups can be quicker because
  96      * the map will have fewer entries, and there's no need to try to
  97      * make the Font2D part of the key.
  98      */
  99     protected ConcurrentHashMap<FontStrikeDesc, Reference<FontStrike>>
 100         strikeCache = new ConcurrentHashMap<>();
 101 
 102     /* Store the last Strike in a Reference object.


 466      * to check the font class before attempting to run, rather than needing
 467      * to promote this method up from TrueTypeFont
 468      */
 469     protected byte[] getTableBytes(int tag) {
 470         return null;
 471     }
 472 
 473     /* implemented for fonts backed by an sfnt that has
 474      * OpenType or AAT layout tables.
 475      */
 476     protected long getLayoutTableCache() {
 477         return 0L;
 478     }
 479 
 480     /* Used only on OS X.
 481      */
 482     protected long getPlatformNativeFontPtr() {
 483         return 0L;
 484     }
 485 
 486     protected boolean isAAT() {
 487         return false;
 488     }
 489 
 490     synchronized long getHarfbuzzFacePtr() {
 491         if (harfbuzzFaceRef == null) {
 492             long harfbuzzFaceNativePtr = createHarfbuzzFace(isAAT(), getPlatformNativeFontPtr());
 493             if (harfbuzzFaceNativePtr == 0) return 0;
 494             harfbuzzFaceRef = new HarfbuzzFaceRef(harfbuzzFaceNativePtr);
 495             Disposer.addObjectRecord(this, harfbuzzFaceRef);
 496         }
 497         return harfbuzzFaceRef.harfbuzzFaceNativePtr;
 498     }
 499 
 500     private native long createHarfbuzzFace(boolean aat, long platformNativeFontPtr);
 501     private static native void disposeHarfbuzzFace(long harfbuzzFaceNativePtr);
 502 
 503     /* for layout code */
 504     protected long getUnitsPerEm() {
 505         return 2048;
 506     }
 507 
 508     boolean supportsEncoding(String encoding) {
 509         return false;
 510     }
 511 
 512     public boolean canDoStyle(int style) {
 513         return (style == this.style);
 514     }
 515 
 516     /*
 517      * All the important subclasses override this which is principally for
 518      * the TrueType 'gasp' table.
 519      */
 520     public boolean useAAForPtSize(int ptsize) {
 521         return true;
 522     }


 564 
 565     public float getItalicAngle(Font font, AffineTransform at,
 566                                 Object aaHint, Object fmHint) {
 567         /* hardwire psz=12 as that's typical and AA vs non-AA for 'gasp' mode
 568          * isn't important for the caret slope of this rarely used API.
 569          */
 570         int aa = FontStrikeDesc.getAAHintIntVal(aaHint, this, 12);
 571         int fm = FontStrikeDesc.getFMHintIntVal(fmHint);
 572         FontStrike strike = getStrike(font, at, aa, fm);
 573         StrikeMetrics metrics = strike.getFontMetrics();
 574         if (metrics.ascentY == 0 || metrics.ascentX == 0) {
 575             return 0f;
 576         } else {
 577             /* ascent is "up" from the baseline so its typically
 578              * a negative value, so we need to compensate
 579              */
 580             return metrics.ascentX/-metrics.ascentY;
 581         }
 582     }
 583 
 584     private static class HarfbuzzFaceRef implements DisposerRecord {
 585         private final long harfbuzzFaceNativePtr;
 586 
 587         private HarfbuzzFaceRef(long harfbuzzFaceNativePtr) {
 588             this.harfbuzzFaceNativePtr = harfbuzzFaceNativePtr;
 589         }
 590 
 591         @Override
 592         public void dispose() {
 593             disposeHarfbuzzFace(harfbuzzFaceNativePtr);
 594         }
 595     }
 596 }
< prev index next >