1 /*
   2  * Copyright 2003-2008 Sun Microsystems, Inc.  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.  Sun designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  22  * CA 95054 USA or visit www.sun.com if you need additional information or
  23  * have any questions.
  24  */
  25 
  26 package sun.font;
  27 
  28 import java.awt.Font;
  29 import java.awt.geom.AffineTransform;
  30 import java.awt.geom.GeneralPath;
  31 import java.awt.geom.Point2D;
  32 import java.awt.geom.Rectangle2D;
  33 import java.util.concurrent.ConcurrentHashMap;
  34 
  35 
  36 public abstract class PhysicalStrike extends FontStrike {
  37 
  38     static final long INTMASK = 0xffffffffL;
  39 
  40     private PhysicalFont physicalFont;
  41     protected CharToGlyphMapper mapper;
  42     /* the ScalerContext is a native structure pre-filled with the
  43      * info needed to setup the scaler for this strike. Its immutable
  44      * so we set it up when the strike is created and free it when the
  45      * strike is disposed. There's then no need to pass the info down
  46      * separately to native on every call to the scaler.
  47      */
  48     protected long pScalerContext;
  49 
  50     /* Only one of these two arrays is non-null.
  51      * use the one that matches size of an address (32 or 64 bits)
  52      */
  53     protected long[] longGlyphImages;
  54     protected int[] intGlyphImages;
  55 
  56     /* Used by the TrueTypeFont subclass, which is the only client
  57      * of getGlyphPoint(). The field and method are here because
  58      * there is no TrueTypeFontStrike subclass.
  59      * This map is a cache of the positions of points on the outline
  60      * of a TrueType glyph. It is used by the OpenType layout engine
  61      * to perform mark positioning. Without this cache every position
  62      * request involves scaling and hinting the glyph outline potentially
  63      * over and over again.
  64      */
  65     ConcurrentHashMap<Integer, Point2D.Float> glyphPointMapCache;
  66 
  67     protected boolean getImageWithAdvance;
  68     protected static final int complexTX =
  69         AffineTransform.TYPE_FLIP |
  70         AffineTransform.TYPE_GENERAL_SCALE |
  71         AffineTransform.TYPE_GENERAL_ROTATION |
  72         AffineTransform.TYPE_GENERAL_TRANSFORM |
  73         AffineTransform.TYPE_QUADRANT_ROTATION;
  74 
  75     PhysicalStrike(PhysicalFont physicalFont, FontStrikeDesc desc) {
  76         this.physicalFont = physicalFont;
  77         this.desc = desc;
  78     }
  79 
  80     protected PhysicalStrike() {
  81     }
  82     /* A number of methods are delegated by the strike to the scaler
  83      * context which is a shared resource on a physical font.
  84      */
  85 
  86     public int getNumGlyphs() {
  87         return physicalFont.getNumGlyphs();
  88     }
  89 
  90     /* These 3 metrics methods below should be implemented to return
  91      * values in user space.
  92      */
  93     StrikeMetrics getFontMetrics() {
  94         if (strikeMetrics == null) {
  95             strikeMetrics =
  96                 physicalFont.getFontMetrics(pScalerContext);
  97         }
  98         return strikeMetrics;
  99     }
 100 
 101     float getCodePointAdvance(int cp) {
 102         return getGlyphAdvance(physicalFont.getMapper().charToGlyph(cp));
 103     }
 104 
 105    Point2D.Float getCharMetrics(char ch) {
 106         return getGlyphMetrics(physicalFont.getMapper().charToGlyph(ch));
 107     }
 108 
 109     int getSlot0GlyphImagePtrs(int[] glyphCodes, long[] images, int  len) {
 110         return 0;
 111     }
 112 
 113     /* Used by the OpenType engine for mark positioning.
 114      */
 115     Point2D.Float getGlyphPoint(int glyphCode, int ptNumber) {
 116         Point2D.Float gp = null;
 117         Integer ptKey = Integer.valueOf(glyphCode<<16|ptNumber);
 118         if (glyphPointMapCache == null) {
 119             synchronized (this) {
 120                 if (glyphPointMapCache == null) {
 121                     glyphPointMapCache =
 122                         new ConcurrentHashMap<Integer, Point2D.Float>();
 123                 }
 124             }
 125         } else {
 126             gp = glyphPointMapCache.get(ptKey);
 127         }
 128 
 129         if (gp == null) {
 130             gp = (physicalFont.getGlyphPoint(pScalerContext, glyphCode, ptNumber));
 131             adjustPoint(gp);
 132             glyphPointMapCache.put(ptKey, gp);
 133         }
 134         return gp;
 135     }
 136 
 137     protected void adjustPoint(Point2D.Float pt) {
 138     }
 139 }