Home | History | Annotate | Download | only in font
      1 /*
      2  *  Licensed to the Apache Software Foundation (ASF) under one or more
      3  *  contributor license agreements.  See the NOTICE file distributed with
      4  *  this work for additional information regarding copyright ownership.
      5  *  The ASF licenses this file to You under the Apache License, Version 2.0
      6  *  (the "License"); you may not use this file except in compliance with
      7  *  the License.  You may obtain a copy of the License at
      8  *
      9  *     http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  *  Unless required by applicable law or agreed to in writing, software
     12  *  distributed under the License is distributed on an "AS IS" BASIS,
     13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  *  See the License for the specific language governing permissions and
     15  *  limitations under the License.
     16  */
     17 /**
     18  * @author Ilya S. Okomin
     19  * @version $Revision$
     20  */
     21 package org.apache.harmony.awt.gl.font;
     22 
     23 
     24 import com.android.internal.awt.AndroidGraphics2D;
     25 import com.android.internal.awt.AndroidGraphicsFactory;
     26 
     27 import java.awt.Graphics2D;
     28 import java.awt.Toolkit;
     29 import java.awt.geom.AffineTransform;
     30 import java.awt.geom.Rectangle2D;
     31 import java.awt.peer.FontPeer;
     32 
     33 import java.awt.font.FontRenderContext;
     34 import java.awt.font.LineMetrics;
     35 import java.util.ArrayList;
     36 import java.util.Locale;
     37 
     38 import org.apache.harmony.awt.internal.nls.Messages;
     39 
     40 import android.graphics.Paint;
     41 
     42 /**
     43  * Abstract class for platform dependent peer implementation of the Font class.
     44  */
     45 public abstract class FontPeerImpl implements FontPeer{
     46 
     47     // ascent of this font peer (in pixels)
     48     int ascent;
     49 
     50     // descent of this font peer (in pixels)
     51     int descent;
     52 
     53     // leading of this font peer (in pixels)
     54     int leading;
     55 
     56     // logical maximum advance of this font peer (in pixels)
     57     int maxAdvance;
     58 
     59     // the height of this font peer
     60     float height;
     61 
     62     // the style of this font peer
     63     int style;
     64 
     65     // the point size of this font peer (in pixels)
     66     int size;
     67 
     68     // the logical hight of this font peer (in pixels)
     69     int logicalHeight;
     70 
     71     // the name of this font peer
     72     String name;
     73 
     74     // family name of this font peer
     75     String fontFamilyName;
     76 
     77     // the Face name of this font peer
     78     String faceName;
     79 
     80     // bounds rectanlge of the largest character in this font peer
     81     Rectangle2D maxCharBounds;
     82 
     83     // italic angle value of this font peer
     84     float italicAngle = 0.0f;
     85 
     86     // the number of glyphs supported by this font peer
     87     int numGlyphs = 0;
     88 
     89     // native font handle
     90     long pFont;
     91 
     92     // cached line metrics object
     93     LineMetricsImpl nlm;
     94 
     95     // the postscript name of this font peer
     96     String psName = null;
     97 
     98     /**
     99      * Default glyph index, that is used, when the desired glyph
    100      * is unsupported in this Font.
    101      */
    102     public char defaultChar = (char)0xFFFF;
    103 
    104     /**
    105      * Uniform LineMetrics flag, that is false for CompositeFont.
    106      * Default value is true.
    107      */
    108     boolean uniformLM = true;
    109 
    110     /**
    111      * Flag of the type of this Font that is indicate is the Font
    112      * has TrueType or Type1 type. Default value is FONT_TYPE_UNDEF.
    113      */
    114     int fontType = FontManager.FONT_TYPE_UNDEF;
    115 
    116     /**
    117      * Flag if this Font was created from stream,
    118      * this parameter used in finilize method.
    119      */
    120     private boolean createdFromStream = false;
    121 
    122     // temorary Font file name, if this FontPeerImpl was created from InputStream
    123     private String tempFontFileName = null;
    124 
    125     // cached FontExtraMetrics object related to this font peer
    126     FontExtraMetrics extraMetrix = null;
    127 
    128     public abstract FontExtraMetrics getExtraMetrics();
    129 
    130     /**
    131      * Returns LineMetrics object with specified parameters
    132      * @param str specified String
    133      * @param frc specified render context
    134      * @param at specified affine transform
    135      * @return
    136      */
    137     public abstract LineMetrics getLineMetrics(String str, FontRenderContext frc, AffineTransform at);
    138 
    139     /**
    140      * Returns postscript name of the font.
    141      */
    142     public abstract String getPSName();
    143 
    144 	//private Graphics2D g = ((AndroidGraphicsFactory)Toolkit.getDefaultToolkit().getGraphicsFactory()).getGraphics2D();
    145     //private Graphics2D g = AndroidGraphics2D.getInstance();
    146 
    147     /**
    148      * Set postscript name of the font to the specified parameter.
    149      */
    150     public void setPSName(String name){
    151         this.psName = name;
    152     }
    153 
    154     /**
    155      * Returns code of the missing glyph.
    156      */
    157     public abstract int getMissingGlyphCode();
    158 
    159     /**
    160      * Returns Glyph representation of the given char.
    161      * @param ch specified char
    162      */
    163     public abstract Glyph getGlyph(char ch);
    164 
    165     /**
    166      * Disposes nesessary resources.
    167      */
    168     public abstract void dispose();
    169 
    170     /**
    171      * Returns Glyph represeting missing char.
    172      */
    173     public abstract Glyph getDefaultGlyph();
    174 
    175     /**
    176      * Returns true if this FontPeerImpl can display the specified char
    177      */
    178     public abstract boolean canDisplay(char c);
    179 
    180     /**
    181      * Returns family name of the font in specified locale settings.
    182      * @param l specified Locale
    183      */
    184     public String getFamily(Locale l){
    185         return this.getFamily();
    186     }
    187 
    188     /**
    189      * Sets family name of the font in specified locale settings.
    190      */
    191     public void setFamily(String familyName){
    192         this.fontFamilyName = familyName;
    193     }
    194 
    195     /**
    196      * Returns face name of the font in specified locale settings.
    197      * @param l specified Locale
    198      */
    199     public String getFontName(Locale l){
    200         return this.getFontName();
    201     }
    202 
    203     /**
    204      * Sets font name of the font in specified locale settings.
    205      */
    206     public void setFontName(String fontName){
    207         this.faceName = fontName;
    208     }
    209 
    210     /**
    211      * Returns true, if this font peer was created from InputStream, false otherwise.
    212      * In case of creating fonts from InputStream some font peer implementations
    213      * may need to free temporary resources.
    214      */
    215     public boolean isCreatedFromStream(){
    216         return this.createdFromStream;
    217     }
    218 
    219     /**
    220      * Sets createdFromStream flag to the specified parameter.
    221      * If parameter is true it means font peer was created from InputStream.
    222      *
    223      * @param value true, if font peer was created from InputStream
    224      */
    225     public void setCreatedFromStream(boolean value){
    226         this.createdFromStream = value;
    227     }
    228 
    229     /**
    230      * Returns font file name of this font.
    231      */
    232     public String getTempFontFileName(){
    233         return this.tempFontFileName;
    234     }
    235 
    236     /**
    237      * Sets font file name of this font to the specified one.
    238      * @param value String representing font file name
    239      */
    240     public void setFontFileName(String value){
    241         this.tempFontFileName = value;
    242     }
    243 
    244     /**
    245      * Returns the advance width of the specified char of this FontPeerImpl.
    246      * Note, if glyph is absent in the font's glyphset - returned value
    247      * is the advance of the deafualt glyph. For escape-chars returned
    248      * width value is 0.
    249      *
    250      * @param ch the char which width is to be returned
    251      * @return the advance width of the specified char of this FontPeerImpl
    252      */
    253     public int charWidth(char ch) {
    254     	Paint p;
    255     	AndroidGraphics2D g = AndroidGraphics2D.getInstance();
    256     	if(g == null) {
    257     		throw new RuntimeException("AndroidGraphics2D not instantiated!");
    258     	}
    259    		p = ((AndroidGraphics2D)g).getAndroidPaint();
    260    		char[] ca = {ch};
    261    		float[] fa = new float[1];
    262    		p.getTextWidths(ca, 0, 1, fa);
    263    		return (int)fa[0];
    264     }
    265 
    266     /**
    267      * Returns the advance width of the specified char of this FontPeerImpl.
    268      *
    269      * @param ind the char which width is to be returned
    270      * @return the advance width of the specified char of this FontPeerImpl
    271      */
    272     public int charWidth(int ind) {
    273         return charWidth((char)ind);
    274     }
    275 
    276     /**
    277      * Returns an array of Glyphs that represent characters from the specified
    278      * Unicode range.
    279      *
    280      * @param uFirst start position in Unicode range
    281      * @param uLast end position in Unicode range
    282      * @return
    283      */
    284     public Glyph[] getGlyphs(char uFirst, char uLast) {
    285 
    286         char i = uFirst;
    287         int len = uLast - uFirst;
    288         ArrayList<Glyph> lst = new ArrayList<Glyph>(len);
    289 
    290         if (size < 0) {
    291             // awt.09=min range bound value is greater than max range bound
    292             throw new IllegalArgumentException(Messages.getString("awt.09")); //$NON-NLS-1$
    293         }
    294 
    295         while (i < uLast) {
    296             lst.add(this.getGlyph(i));
    297         }
    298 
    299         return (Glyph[]) lst.toArray();
    300     }
    301 
    302     /**
    303      * Returns an array of Glyphs representing given array of chars.
    304      *
    305      * @param chars specified array of chars
    306      */
    307     public Glyph[] getGlyphs(char[] chars) {
    308         if (chars == null){
    309             return null;
    310         }
    311 
    312         Glyph[] result = new Glyph[chars.length];
    313 
    314         for (int i = 0; i < chars.length; i++) {
    315             result[i] = this.getGlyph(chars[i]);
    316         }
    317         return result;
    318     }
    319 
    320     /**
    321      * Returns an array of Glyphs representing given string.
    322      *
    323      * @param str specified string
    324      */
    325     public Glyph[] getGlyphs(String str) {
    326         char[] chars = str.toCharArray();
    327         return this.getGlyphs(chars);
    328     }
    329 
    330     /**
    331      * Returns family name of this FontPeerImpl.
    332      */
    333     public String getFamily() {
    334         return fontFamilyName;
    335     }
    336 
    337     /**
    338      * Returns face name of this FontPeerImpl.
    339      */
    340     public String getFontName() {
    341         if (this.fontType == FontManager.FONT_TYPE_T1){
    342             return this.fontFamilyName;
    343         }
    344 
    345         return faceName;
    346     }
    347 
    348     /**
    349      * Returns height of this font peer in pixels.
    350      */
    351     public int getLogicalHeight() {
    352         return logicalHeight;
    353     }
    354 
    355     /**
    356      * Sets height of this font peer in pixels to the given value.
    357      *
    358      * @param newHeight new height in pixels value
    359      */
    360     public void setLogicalHeight(int newHeight) {
    361         logicalHeight = newHeight;
    362     }
    363 
    364     /**
    365      * Returns font size.
    366      */
    367     public int getSize() {
    368         return size;
    369     }
    370 
    371     /**
    372      * Returns font style.
    373      */
    374     public int getStyle() {
    375         return style;
    376     }
    377 
    378     /**
    379      * Returns font name.
    380      */
    381     public String getName() {
    382         return name;
    383     }
    384 
    385     /**
    386      * Returns the bounds of the largest char in this FontPeerImpl in
    387      * specified render context.
    388      *
    389      * @param frc specified FontRenderContext
    390      */
    391     public Rectangle2D getMaxCharBounds(FontRenderContext frc) {
    392         return maxCharBounds;
    393     }
    394 
    395     /**
    396      * Returns the number of glyphs in this FontPeerImpl.
    397      */
    398     public int getNumGlyphs() {
    399         return  numGlyphs;
    400     }
    401 
    402     /**
    403      * Returns tangens of the italic angle of this FontPeerImpl.
    404      * If the FontPeerImpl has TrueType font type, italic angle value can be
    405      * calculated as (CharSlopeRun / CharSlopeRise) in terms of GDI.
    406      */
    407     public float getItalicAngle() {
    408         return italicAngle;
    409     }
    410 
    411     /**
    412      * Returns height of this font peer.
    413      */
    414     public float getHeight(){
    415         return height;
    416     }
    417 
    418     /**
    419      * Returns cached LineMetrics object of this font peer.
    420      */
    421     public LineMetrics getLineMetrics(){
    422         return nlm;
    423     }
    424 
    425     /**
    426      * Returns native font handle of this font peer.
    427      */
    428     public long getFontHandle(){
    429         return pFont;
    430     }
    431 
    432     /**
    433      * Returns ascent of this font peer.
    434      */
    435     public int getAscent(){
    436     	Paint p;
    437     	AndroidGraphics2D g = AndroidGraphics2D.getInstance();
    438     	if(g == null) {
    439     		throw new RuntimeException("AndroidGraphics2D not instantiated!");
    440     	}
    441    		p = ((AndroidGraphics2D)g).getAndroidPaint();
    442    		return (int)p.ascent();
    443         //return ascent;
    444     }
    445 
    446     /**
    447      * Returns descent of this font peer.
    448      */
    449     public int getDescent(){
    450         return descent;
    451     }
    452 
    453     /**
    454      * Returns leading of this font peer.
    455      */
    456     public int getLeading(){
    457         return leading;
    458     }
    459 
    460     /**
    461      * Returns true if this font peer has uniform line metrics.
    462      */
    463     public boolean hasUniformLineMetrics(){
    464         return uniformLM;
    465     }
    466 
    467     /**
    468      * Returns type of this font.
    469      *
    470      * @return one of constant font type values.
    471      */
    472     public int getFontType(){
    473         return fontType;
    474     }
    475 
    476     /**
    477      * Sets new font type to the font object.
    478      *
    479      * @param newType new type value
    480      */
    481     public void setFontType(int newType){
    482         if (newType == FontManager.FONT_TYPE_T1 || newType == FontManager.FONT_TYPE_TT){
    483             fontType = newType;
    484         }
    485     }
    486 
    487     /**
    488      * Sets new font type to the font object.
    489      *
    490      * @param newType new type value
    491      */
    492     @Override
    493     protected void finalize() throws Throwable {
    494       super.finalize();
    495 
    496       dispose();
    497     }
    498 
    499 }
    500